We have introduced Bellman Ford and discussed on implementation here.
Input: Graph and a source vertex srcĀ
Output: Shortest distance to all vertices from src. If there is a negative weight cycle, then shortest distances are not calculated, negative weight cycle is reported.
1) This step initializes distances from source to all vertices as infinite and distance to source itself as 0. Create an array dist[] of size |V| with all values as infinite except dist[src] where src is source vertex.
2) This step calculates shortest distances. Do following |V|-1 times where |V| is the number of vertices in given graph.Ā
ā¦..a) Do following for each edge u-vĀ
ā¦ā¦ā¦ā¦ā¦ā¦If dist[v] > dist[u] + weight of edge uv, then update dist[v]Ā
ā¦ā¦ā¦ā¦ā¦ā¦ā¦.dist[v] = dist[u] + weight of edge uv
3) This step reports if there is a negative weight cycle in graph. Do following for each edge u-vĀ
ā¦ā¦If dist[v] > dist[u] + weight of edge uv, then āGraph contains negative weight cycleāĀ
The idea of step 3 is, step 2 guarantees shortest distances if graph doesnāt contain negative weight cycle. If we iterate through all edges one more time and get a shorter path for any vertex, then there is a negative weight cycle
ExampleĀ
Let us understand the algorithm with following example graph. The images are taken from this source.
Let the given source vertex be 0. Initialize all distances as infinite, except the distance to source itself. Total number of vertices in the graph is 5, so all edges must be processed 4 times.
Ā
Let all edges are processed in following order: (B, E), (D, B), (B, D), (A, B), (A, C), (D, C), (B, C), (E, D). We get following distances when all edges are processed first time. The first row in shows initial distances. The second row shows distances when edges (B, E), (D, B), (B, D) and (A, B) are processed. The third row shows distances when (A, C) is processed. The fourth row shows when (D, C), (B, C) and (E, D) are processed.Ā
Ā
The first iteration guarantees to give all shortest paths which are at most 1 edge long. We get following distances when all edges are processed second time (The last row shows final values).Ā
Ā
The second iteration guarantees to give all shortest paths which are at most 2 edges long. The algorithm processes all edges 2 more times. The distances are minimized after the second iteration, so third and fourth iterations donāt update the distances.Ā
Ā
C++
// A C++ program for Bellman-Ford's single source // shortest path algorithm. #include <bits/stdc++.h> using namespace std; Ā
// The main function that finds shortest // distances from src to all other vertices // using Bellman-Ford algorithm. The function // also detects negative weight cycle // The row graph[i] represents i-th edge with // three values u, v and w. void BellmanFord( int graph[][3], int V, int E, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int src) { Ā Ā Ā Ā // Initialize distance of all vertices as infinite. Ā Ā Ā Ā int dis[V]; Ā Ā Ā Ā for ( int i = 0; i < V; i++) Ā Ā Ā Ā Ā Ā Ā Ā dis[i] = INT_MAX; Ā
Ā Ā Ā Ā // initialize distance of source as 0 Ā Ā Ā Ā dis[src] = 0; Ā
Ā Ā Ā Ā // Relax all edges |V| - 1 times. A simple Ā Ā Ā Ā // shortest path from src to any other Ā Ā Ā Ā // vertex can have at-most |V| - 1 edges Ā Ā Ā Ā for ( int i = 0; i < V - 1; i++) { Ā
Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < E; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (dis[graph[j][0]] != INT_MAX && dis[graph[j][0]] + graph[j][2] < Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[graph[j][1]]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[graph[j][1]] = Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[graph[j][0]] + graph[j][2]; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // check for negative-weight cycles. Ā Ā Ā Ā // The above step guarantees shortest Ā Ā Ā Ā // distances if graph doesn't contain Ā Ā Ā Ā // negative weight cycle.Ā If we get a Ā Ā Ā Ā // shorter path, then there is a cycle. Ā Ā Ā Ā for ( int i = 0; i < E; i++) { Ā Ā Ā Ā Ā Ā Ā Ā int x = graph[i][0]; Ā Ā Ā Ā Ā Ā Ā Ā int y = graph[i][1]; Ā Ā Ā Ā Ā Ā Ā Ā int weight = graph[i][2]; Ā Ā Ā Ā Ā Ā Ā Ā if (dis[x] != INT_MAX && Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[x] + weight < dis[y]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā cout << "Graph contains negative" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā " weight cycle" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā << endl; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā cout << "Vertex Distance from Source" << endl; Ā Ā Ā Ā for ( int i = 0; i < V; i++) Ā Ā Ā Ā Ā Ā Ā Ā cout << i << "\t\t" << dis[i] << endl; } Ā
// Driver program to test above functions int main() { Ā Ā Ā Ā int V = 5; // Number of vertices in graph Ā Ā Ā Ā int E = 8; // Number of edges in graph Ā
Ā Ā Ā Ā // Every edge has three values (u, v, w) where Ā Ā Ā Ā // the edge is from vertex u to v. And weight Ā Ā Ā Ā // of the edge is w. Ā Ā Ā Ā int graph[][3] = { { 0, 1, -1 }, { 0, 2, 4 }, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 1, 2, 3 }, { 1, 3, 2 }, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 1, 4, 2 }, { 3, 2, 5 }, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 3, 1, 1 }, { 4, 3, -3 } }; Ā
Ā Ā Ā Ā BellmanFord(graph, V, E, 0); Ā Ā Ā Ā return 0; } |
Java
// A Java program for Bellman-Ford's single source // shortest path algorithm. Ā
class GFG { Ā
// The main function that finds shortest // distances from src to all other vertices // using Bellman-Ford algorithm. The function // also detects negative weight cycle // The row graph[i] represents i-th edge with // three values u, v and w. static void BellmanFord( int graph[][], int V, int E, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int src) { Ā Ā Ā Ā // Initialize distance of all vertices as infinite. Ā Ā Ā Ā int []dis = new int [V]; Ā Ā Ā Ā for ( int i = 0 ; i < V; i++) Ā Ā Ā Ā Ā Ā Ā Ā dis[i] = Integer.MAX_VALUE; Ā
Ā Ā Ā Ā // initialize distance of source as 0 Ā Ā Ā Ā dis[src] = 0 ; Ā
Ā Ā Ā Ā // Relax all edges |V| - 1 times. A simple Ā Ā Ā Ā // shortest path from src to any other Ā Ā Ā Ā // vertex can have at-most |V| - 1 edges Ā Ā Ā Ā for ( int i = 0 ; i < V - 1 ; i++) Ā Ā Ā Ā { Ā
Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0 ; j < E; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (dis[graph[j][ 0 ]] != Integer.MAX_VALUE && dis[graph[j][ 0 ]] + graph[j][ 2 ] < Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[graph[j][ 1 ]]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[graph[j][ 1 ]] = Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[graph[j][ 0 ]] + graph[j][ 2 ]; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // check for negative-weight cycles. Ā Ā Ā Ā // The above step guarantees shortest Ā Ā Ā Ā // distances if graph doesn't contain Ā Ā Ā Ā // negative weight cycle. If we get a Ā Ā Ā Ā // shorter path, then there is a cycle. Ā Ā Ā Ā for ( int i = 0 ; i < E; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int x = graph[i][ 0 ]; Ā Ā Ā Ā Ā Ā Ā Ā int y = graph[i][ 1 ]; Ā Ā Ā Ā Ā Ā Ā Ā int weight = graph[i][ 2 ]; Ā Ā Ā Ā Ā Ā Ā Ā if (dis[x] != Integer.MAX_VALUE && Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[x] + weight < dis[y]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println( "Graph contains negative" Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + " weight cycle" ); Ā Ā Ā Ā } Ā
Ā Ā Ā Ā System.out.println( "Vertex Distance from Source" ); Ā Ā Ā Ā for ( int i = 0 ; i < V; i++) Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(i + "\t\t" + dis[i]); } Ā
// Driver code public static void main(String[] args) { Ā Ā Ā Ā int V = 5 ; // Number of vertices in graph Ā Ā Ā Ā int E = 8 ; // Number of edges in graph Ā
Ā Ā Ā Ā // Every edge has three values (u, v, w) where Ā Ā Ā Ā // the edge is from vertex u to v. And weight Ā Ā Ā Ā // of the edge is w. Ā Ā Ā Ā int graph[][] = { { 0 , 1 , - 1 }, { 0 , 2 , 4 }, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 1 , 2 , 3 }, { 1 , 3 , 2 }, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 1 , 4 , 2 }, { 3 , 2 , 5 }, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 3 , 1 , 1 }, { 4 , 3 , - 3 } }; Ā
Ā Ā Ā Ā BellmanFord(graph, V, E, 0 ); Ā Ā Ā Ā } } Ā
// This code is contributed by 29AjayKumar |
Python3
# Python3 program for Bellman-Ford's # single source shortest path algorithm. from sys import maxsize Ā
# The main function that finds shortest # distances from src to all other vertices # using Bellman-Ford algorithm. The function # also detects negative weight cycle # The row graph[i] represents i-th edge with # three values u, v and w. def BellmanFord(graph, V, E, src): Ā
Ā Ā Ā Ā # Initialize distance of all vertices as infinite. Ā Ā Ā Ā dis = [maxsize] * V Ā
Ā Ā Ā Ā # initialize distance of source as 0 Ā Ā Ā Ā dis[src] = 0 Ā
Ā Ā Ā Ā # Relax all edges |V| - 1 times. A simple Ā Ā Ā Ā # shortest path from src to any other Ā Ā Ā Ā # vertex can have at-most |V| - 1 edges Ā Ā Ā Ā for i in range (V - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā for j in range (E): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if dis[graph[j][ 0 ]] + \ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā graph[j][ 2 ] < dis[graph[j][ 1 ]]: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[graph[j][ 1 ]] = dis[graph[j][ 0 ]] + \ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā graph[j][ 2 ] Ā
Ā Ā Ā Ā # check for negative-weight cycles. Ā Ā Ā Ā # The above step guarantees shortest Ā Ā Ā Ā # distances if graph doesn't contain Ā Ā Ā Ā # negative weight cycle. If we get a Ā Ā Ā Ā # shorter path, then there is a cycle. Ā Ā Ā Ā for i in range (E): Ā Ā Ā Ā Ā Ā Ā Ā x = graph[i][ 0 ] Ā Ā Ā Ā Ā Ā Ā Ā y = graph[i][ 1 ] Ā Ā Ā Ā Ā Ā Ā Ā weight = graph[i][ 2 ] Ā Ā Ā Ā Ā Ā Ā Ā if dis[x] ! = maxsize and dis[x] + \ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā weight < dis[y]: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā print ( "Graph contains negative weight cycle" ) Ā
Ā Ā Ā Ā print ( "Vertex Distance from Source" ) Ā Ā Ā Ā for i in range (V): Ā Ā Ā Ā Ā Ā Ā Ā print ( "%d\t\t%d" % (i, dis[i])) Ā
# Driver Code if __name__ = = "__main__" : Ā Ā Ā Ā V = 5 # Number of vertices in graph Ā Ā Ā Ā E = 8 # Number of edges in graph Ā
Ā Ā Ā Ā # Every edge has three values (u, v, w) where Ā Ā Ā Ā # the edge is from vertex u to v. And weight Ā Ā Ā Ā # of the edge is w. Ā Ā Ā Ā graph = [[ 0 , 1 , - 1 ], [ 0 , 2 , 4 ], [ 1 , 2 , 3 ], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 1 , 3 , 2 ], [ 1 , 4 , 2 ], [ 3 , 2 , 5 ], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 3 , 1 , 1 ], [ 4 , 3 , - 3 ]] Ā Ā Ā Ā BellmanFord(graph, V, E, 0 ) Ā
# This code is contributed by # sanjeev2552 |
C#
// C# program for Bellman-Ford's single source // shortest path algorithm. using System; Ā Ā Ā Ā Ā class GFG { Ā
// The main function that finds shortest // distances from src to all other vertices // using Bellman-Ford algorithm. The function // also detects negative weight cycle // The row graph[i] represents i-th edge with // three values u, v and w. static void BellmanFord( int [,]graph, int V, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int E, int src) { Ā Ā Ā Ā // Initialize distance of all vertices as infinite. Ā Ā Ā Ā int []dis = new int [V]; Ā Ā Ā Ā for ( int i = 0; i < V; i++) Ā Ā Ā Ā Ā Ā Ā Ā dis[i] = int .MaxValue; Ā
Ā Ā Ā Ā // initialize distance of source as 0 Ā Ā Ā Ā dis[src] = 0; Ā
Ā Ā Ā Ā // Relax all edges |V| - 1 times. A simple Ā Ā Ā Ā // shortest path from src to any other Ā Ā Ā Ā // vertex can have at-most |V| - 1 edges Ā Ā Ā Ā for ( int i = 0; i < V - 1; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = 0; j < E; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (dis[graph[j, 0]] = int .MaxValue && dis[graph[j, 0]] + graph[j, 2] < Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[graph[j, 1]]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[graph[j, 1]] = Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[graph[j, 0]] + graph[j, 2]; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // check for negative-weight cycles. Ā Ā Ā Ā // The above step guarantees shortest Ā Ā Ā Ā // distances if graph doesn't contain Ā Ā Ā Ā // negative weight cycle. If we get a Ā Ā Ā Ā // shorter path, then there is a cycle. Ā Ā Ā Ā for ( int i = 0; i < E; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int x = graph[i, 0]; Ā Ā Ā Ā Ā Ā Ā Ā int y = graph[i, 1]; Ā Ā Ā Ā Ā Ā Ā Ā int weight = graph[i, 2]; Ā Ā Ā Ā Ā Ā Ā Ā if (dis[x] != int .MaxValue && Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[x] + weight < dis[y]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine( "Graph contains negative" + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā " weight cycle" ); Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Console.WriteLine( "Vertex Distance from Source" ); Ā Ā Ā Ā for ( int i = 0; i < V; i++) Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(i + "\t\t" + dis[i]); } Ā
// Driver code public static void Main(String[] args) { Ā Ā Ā Ā int V = 5; // Number of vertices in graph Ā Ā Ā Ā int E = 8; // Number of edges in graph Ā
Ā Ā Ā Ā // Every edge has three values (u, v, w) where Ā Ā Ā Ā // the edge is from vertex u to v. And weight Ā Ā Ā Ā // of the edge is w. Ā Ā Ā Ā int [,]graph = {{ 0, 1, -1 }, { 0, 2, 4 }, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 1, 2, 3 }, { 1, 3, 2 }, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 1, 4, 2 }, { 3, 2, 5 }, Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā { 3, 1, 1 }, { 4, 3, -3 }}; Ā
Ā Ā Ā Ā BellmanFord(graph, V, E, 0); } } Ā
// This code is contributed by Princi Singh |
PHP
<?php // A PHP program for Bellman-Ford's single // source shortest path algorithm. Ā
// The main function that finds shortest // distances from src to all other vertices // using Bellman-Ford algorithm. The function // also detects negative weight cycle // The row graph[i] represents i-th edge with // three values u, v and w. function BellmanFord( $graph , $V , $E , $src ) { Ā Ā Ā Ā // Initialize distance of all vertices as infinite. Ā Ā Ā Ā $dis = array (); Ā Ā Ā Ā for ( $i = 0; $i < $V ; $i ++) Ā Ā Ā Ā Ā Ā Ā Ā $dis [ $i ] = PHP_INT_MAX; Ā
Ā Ā Ā Ā // initialize distance of source as 0 Ā Ā Ā Ā $dis [ $src ] = 0; Ā
Ā Ā Ā Ā // Relax all edges |V| - 1 times. A simple Ā Ā Ā Ā // shortest path from src to any other Ā Ā Ā Ā // vertex can have at-most |V| - 1 edges Ā Ā Ā Ā for ( $i = 0; $i < $V - 1; $i ++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā for ( $j = 0; $j < $E ; $j ++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ( $dis [ $graph [ $j ][0]] != PHP_INT_MAX && $dis [ $graph [ $j ][0]] + $graph [ $j ][2] < Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $dis [ $graph [ $j ][1]]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $dis [ $graph [ $j ][1]] = $dis [ $graph [ $j ][0]] +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $graph [ $j ][2]; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // check for negative-weight cycles. Ā Ā Ā Ā // The above step guarantees shortest Ā Ā Ā Ā // distances if graph doesn't contain Ā Ā Ā Ā // negative weight cycle. If we get a Ā Ā Ā Ā // shorter path, then there is a cycle. Ā Ā Ā Ā for ( $i = 0; $i < $E ; $i ++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā $x = $graph [ $i ][0]; Ā Ā Ā Ā Ā Ā Ā Ā $y = $graph [ $i ][1]; Ā Ā Ā Ā Ā Ā Ā Ā $weight = $graph [ $i ][2]; Ā Ā Ā Ā Ā Ā Ā Ā if ( $dis [ $x ] != PHP_INT_MAX && Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā $dis [ $x ] + $weight < $dis [ $y ]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā echo "Graph contains negative weight cycle \n" ; Ā Ā Ā Ā } Ā
Ā Ā Ā Ā echo "Vertex Distance from Source \n" ; Ā Ā Ā Ā for ( $i = 0; $i < $V ; $i ++) Ā Ā Ā Ā Ā Ā Ā Ā echo $i , "\t\t" , $dis [ $i ], "\n" ; } Ā
// Driver Code $V = 5; // Number of vertices in graph $E = 8; // Number of edges in graph Ā
// Every edge has three values (u, v, w) where // the edge is from vertex u to v. And weight // of the edge is w. $graph = array ( array ( 0, 1, -1 ), array ( 0, 2, 4 ), Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā array ( 1, 2, 3 ), array ( 1, 3, 2 ), Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā array ( 1, 4, 2 ), array ( 3, 2, 5), Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā array ( 3, 1, 1), array ( 4, 3, -3 ) ); Ā
BellmanFord( $graph , $V , $E , 0); Ā
// This code is contributed by AnkitRai01 ?> |
Javascript
<script> Ā Ā Ā Ā // Javascript program for Bellman-Ford's single source // shortest path algorithm. Ā
// The main function that finds shortest // distances from src to all other vertices // using Bellman-Ford algorithm. The function // also detects negative weight cycle // The row graph[i] represents i-th edge with // three values u, v and w. function BellmanFord(graph, V, E, src) { Ā Ā Ā Ā // Initialize distance of all vertices as infinite. Ā Ā Ā Ā var dis = Array(V).fill(1000000000); Ā
Ā Ā Ā Ā // initialize distance of source as 0 Ā Ā Ā Ā dis[src] = 0; Ā
Ā Ā Ā Ā // Relax all edges |V| - 1 times. A simple Ā Ā Ā Ā // shortest path from src to any other Ā Ā Ā Ā // vertex can have at-most |V| - 1 edges Ā Ā Ā Ā for ( var i = 0; i < V - 1; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā for ( var j = 0; j < E; j++) Ā Ā Ā Ā Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if ((dis[graph[j][0]] + graph[j][2]) < dis[graph[j][1]]) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dis[graph[j][1]] = dis[graph[j][0]] + graph[j][2]; Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā // check for negative-weight cycles. Ā Ā Ā Ā // The above step guarantees shortest Ā Ā Ā Ā // distances if graph doesn't contain Ā Ā Ā Ā // negative weight cycle. If we get a Ā Ā Ā Ā // shorter path, then there is a cycle. Ā Ā Ā Ā for ( var i = 0; i < E; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā var x = graph[i][0]; Ā Ā Ā Ā Ā Ā Ā Ā var y = graph[i][1]; Ā Ā Ā Ā Ā Ā Ā Ā var weight = graph[i][2]; Ā Ā Ā Ā Ā Ā Ā Ā if ((dis[x] != 1000000000) && Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (dis[x] + weight < dis[y])) Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write( "Graph contains negative" + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā " weight cycle<br>" ); Ā Ā Ā Ā } Ā
Ā Ā Ā Ā document.write( "Vertex Distance from Source<br>" ); Ā Ā Ā Ā for ( var i = 0; i < V; i++) Ā Ā Ā Ā Ā Ā Ā Ā document.write(i + "Ā Ā Ā " + dis[i] + "<br>" ); } Ā
// Driver code var V = 5; // Number of vertices in graph var E = 8; // Number of edges in graph Ā
// Every edge has three values (u, v, w) where // the edge is from vertex u to v. And weight // of the edge is w. var graph = [[ 0, 1, -1 ], [ 0, 2, 4 ], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 1, 2, 3 ], [ 1, 3, 2 ], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 1, 4, 2 ], [ 3, 2, 5 ], Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā [ 3, 1, 1 ], [ 4, 3, -3 ]]; BellmanFord(graph, V, E, 0); Ā
// This code is contributed by importantly. </script> |
Vertex Distance from Source 0 0 1 -1 2 2 3 -2 4 1
Ā
Time Complexity: O(VE)
Space Complexity: O(V)
This implementation is suggested by PrateekGupta10
Ā
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!