Bellman ford algorithm

Graphs Shortest Path Algorithms Hard

Given a weighted and directed graph of V vertices and E edges. An edge is represented as [ai, bi, wi], meaning there is a directed edge from ai to bi having weight wi. Find the shortest distance of all the vertices from the source vertex S. If a vertex can't be reached from the S then mark the distance as 109.


If the graph contains a negative cycle then return -1.

Examples:


Input : V = 6, Edges = [[3, 2, 6], [5, 3, 1], [0, 1, 5], [1, 5, -3], [1, 2, -2], [3, 4, -2], [2, 4, 3]], S = 0

Output: 0 5 3 3 1 2

Explanation:

For node 1, shortest path is 0->1 (distance=5).

For node 2, shortest path is 0->1->2 (distance=3)

For node 3, shortest path is 0->1->5->3 (distance=3)

For node 4, shortest path is 0->1->5->3->4 (distance=1)

For node 5, shortest path is 0->1->5 (distance=2)

Input : V = 2, Edges = [[0,1,9]], S = 0

Output: 0 9

Explanation: For node 1, the shortest path is 0->1 (distance=9)

Input: V=3, Edges = [[0,1,5],[1,0,3],[1,2,-1],[2,0,1]], S = 2

Constraints

  • 1 ≤ V ≤ 500
  • 1 ≤ E ≤ V*(V-1)
  • -1000 ≤ edges[i][3] ≤ 1000
  • 0 ≤ S < V

Hints

  • If any distance further decreases on the Vth iteration, a negative weight cycle exists, so return -1. Return the dist[] array with distances for all vertices, replacing unreachable nodes with 10^9.
  • Initialize a dist[] array with infinity (10^9) for all vertices except dist[S] = 0. Relax all edges V-1 times (since the shortest path in a graph with V nodes requires at most V-1 edges).

Company Tags

Roche Pinterest JPMorgan Chase Ubisoft HashiCorp DoorDash Cloudflare MongoDB Unity Technologies American Express Rockstar Games Stripe Lyft Roblox KPMG Salesforce Electronic Arts Teladoc Health Twilio Red Hat Ernst & Young Micron Technology Walmart Intel Texas Instruments Google Microsoft Amazon Meta Apple Netflix Adobe