Given a graph and a source vertex src in graph, find shortest paths from src to all vertices in the given graph. The graph may contain negative weight edges.
We have discussed Dijkstra’s algorithm for this problem. Dijksra’s algorithm is a Greedy algorithm and time complexity is O(VLogV) (with the use of Fibonacci heap). Dijkstra doesn’t work for Graphs with negative weight edges, Bellman-Ford works for such graphs. Bellman-Ford is also simpler than Dijkstra and suites well for distributed systems. But time complexity of Bellman-Ford is O(VE), which is more than Dijkstra.

Algorithm
Following are the detailed steps.

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.

  • 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.
  • This step calculates shortest distances. Do following |V|-1 times where |V| is the number of vertices in given graph.
    • …..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
  • 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

How does this work? Like other Dynamic Programming Problems, the algorithm calculate shortest paths in bottom-up manner. It first calculates the shortest distances for the shortest paths which have at-most one edge in the path. Then, it calculates shortest paths with at-nost 2 edges, and so on. After the ith iteration of outer loop, the shortest paths with at most i edges are calculated. There can be maximum |V| – 1 edges in any simple path, that is why the outer loop runs |v| – 1 times. The idea is, assuming that there is no negative weight cycle, if we have calculated shortest paths with at most i edges, then an iteration over all edges guarantees to give shortest path with at-most (i+1) edges.

[ad type=”banner”]

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.

Bellman Algorithm

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.

Bellman ford

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).

Bellman ford algorithm

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.

[pastacode lang=”java” manual=”%2F%2F%20A%20Java%20program%20for%20Bellman-Ford’s%20single%20source%20shortest%20path%0A%2F%2F%20algorithm.%0Aimport%20java.util.*%3B%0Aimport%20java.lang.*%3B%0Aimport%20java.io.*%3B%0A%20%0A%2F%2F%20A%20class%20to%20represent%20a%20connected%2C%20directed%20and%20weighted%20graph%0Aclass%20Graph%0A%7B%0A%20%20%20%20%2F%2F%20A%20class%20to%20represent%20a%20weighted%20edge%20in%20graph%0A%20%20%20%20class%20Edge%20%7B%0A%20%20%20%20%20%20%20%20int%20src%2C%20dest%2C%20weight%3B%0A%20%20%20%20%20%20%20%20Edge()%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20src%20%3D%20dest%20%3D%20weight%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%3B%0A%20%0A%20%20%20%20int%20V%2C%20E%3B%0A%20%20%20%20Edge%20edge%5B%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Creates%20a%20graph%20with%20V%20vertices%20and%20E%20edges%0A%20%20%20%20Graph(int%20v%2C%20int%20e)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20V%20%3D%20v%3B%0A%20%20%20%20%20%20%20%20E%20%3D%20e%3B%0A%20%20%20%20%20%20%20%20edge%20%3D%20new%20Edge%5Be%5D%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Ce%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20edge%5Bi%5D%20%3D%20new%20Edge()%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20The%20main%20function%20that%20finds%20shortest%20distances%20from%20src%0A%20%20%20%20%2F%2F%20to%20all%20other%20vertices%20using%20Bellman-Ford%20algorithm.%20%20The%0A%20%20%20%20%2F%2F%20function%20also%20detects%20negative%20weight%20cycle%0A%20%20%20%20void%20BellmanFord(Graph%20graph%2Cint%20src)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20V%20%3D%20graph.V%2C%20E%20%3D%20graph.E%3B%0A%20%20%20%20%20%20%20%20int%20dist%5B%5D%20%3D%20new%20int%5BV%5D%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Step%201%3A%20Initialize%20distances%20from%20src%20to%20all%20other%0A%20%20%20%20%20%20%20%20%2F%2F%20vertices%20as%20INFINITE%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3CV%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bi%5D%20%3D%20Integer.MAX_VALUE%3B%0A%20%20%20%20%20%20%20%20dist%5Bsrc%5D%20%3D%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Step%202%3A%20Relax%20all%20edges%20%7CV%7C%20-%201%20times.%20A%20simple%0A%20%20%20%20%20%20%20%20%2F%2F%20shortest%20path%20from%20src%20to%20any%20other%20vertex%20can%0A%20%20%20%20%20%20%20%20%2F%2F%20have%20at-most%20%7CV%7C%20-%201%20edges%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D1%3B%20i%3CV%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20j%3D0%3B%20j%3CE%3B%20%2B%2Bj)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20u%20%3D%20graph.edge%5Bj%5D.src%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20v%20%3D%20graph.edge%5Bj%5D.dest%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20weight%20%3D%20graph.edge%5Bj%5D.weight%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(dist%5Bu%5D!%3DInteger.MAX_VALUE%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bu%5D%2Bweight%3Cdist%5Bv%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bv%5D%3Ddist%5Bu%5D%2Bweight%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Step%203%3A%20check%20for%20negative-weight%20cycles.%20%20The%20above%0A%20%20%20%20%20%20%20%20%2F%2F%20step%20guarantees%20shortest%20distances%20if%20graph%20doesn’t%0A%20%20%20%20%20%20%20%20%2F%2F%20contain%20negative%20weight%20cycle.%20If%20we%20get%20a%20shorter%0A%20%20%20%20%20%20%20%20%2F%2F%20%20path%2C%20then%20there%20is%20a%20cycle.%0A%20%20%20%20%20%20%20%20for%20(int%20j%3D0%3B%20j%3CE%3B%20%2B%2Bj)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20u%20%3D%20graph.edge%5Bj%5D.src%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20v%20%3D%20graph.edge%5Bj%5D.dest%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20weight%20%3D%20graph.edge%5Bj%5D.weight%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(dist%5Bu%5D!%3DInteger.MAX_VALUE%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20dist%5Bu%5D%2Bweight%3Cdist%5Bv%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(%22Graph%20contains%20negative%20weight%20cycle%22)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20printArr(dist%2C%20V)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20A%20utility%20function%20used%20to%20print%20the%20solution%0A%20%20%20%20void%20printArr(int%20dist%5B%5D%2C%20int%20V)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20System.out.println(%22Vertex%20%20%20Distance%20from%20Source%22)%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3CV%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.println(i%2B%22%5Ct%5Ct%22%2Bdist%5Bi%5D)%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Driver%20method%20to%20test%20above%20function%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20int%20V%20%3D%205%3B%20%20%2F%2F%20Number%20of%20vertices%20in%20graph%0A%20%20%20%20%20%20%20%20int%20E%20%3D%208%3B%20%20%2F%2F%20Number%20of%20edges%20in%20graph%0A%20%0A%20%20%20%20%20%20%20%20Graph%20graph%20%3D%20new%20Graph(V%2C%20E)%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20add%20edge%200-1%20(or%20A-B%20in%20above%20figure)%0A%20%20%20%20%20%20%20%20graph.edge%5B0%5D.src%20%3D%200%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B0%5D.dest%20%3D%201%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B0%5D.weight%20%3D%20-1%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20add%20edge%200-2%20(or%20A-C%20in%20above%20figure)%0A%20%20%20%20%20%20%20%20graph.edge%5B1%5D.src%20%3D%200%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B1%5D.dest%20%3D%202%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B1%5D.weight%20%3D%204%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20add%20edge%201-2%20(or%20B-C%20in%20above%20figure)%0A%20%20%20%20%20%20%20%20graph.edge%5B2%5D.src%20%3D%201%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B2%5D.dest%20%3D%202%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B2%5D.weight%20%3D%203%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20add%20edge%201-3%20(or%20B-D%20in%20above%20figure)%0A%20%20%20%20%20%20%20%20graph.edge%5B3%5D.src%20%3D%201%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B3%5D.dest%20%3D%203%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B3%5D.weight%20%3D%202%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20add%20edge%201-4%20(or%20A-E%20in%20above%20figure)%0A%20%20%20%20%20%20%20%20graph.edge%5B4%5D.src%20%3D%201%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B4%5D.dest%20%3D%204%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B4%5D.weight%20%3D%202%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20add%20edge%203-2%20(or%20D-C%20in%20above%20figure)%0A%20%20%20%20%20%20%20%20graph.edge%5B5%5D.src%20%3D%203%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B5%5D.dest%20%3D%202%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B5%5D.weight%20%3D%205%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20add%20edge%203-1%20(or%20D-B%20in%20above%20figure)%0A%20%20%20%20%20%20%20%20graph.edge%5B6%5D.src%20%3D%203%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B6%5D.dest%20%3D%201%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B6%5D.weight%20%3D%201%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20add%20edge%204-3%20(or%20E-D%20in%20above%20figure)%0A%20%20%20%20%20%20%20%20graph.edge%5B7%5D.src%20%3D%204%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B7%5D.dest%20%3D%203%3B%0A%20%20%20%20%20%20%20%20graph.edge%5B7%5D.weight%20%3D%20-3%3B%0A%20%0A%20%20%20%20%20%20%20%20graph.BellmanFord(graph%2C%200)%3B%0A%20%20%20%20%7D%0A%7D” message=”Java” highlight=”” provider=”manual”/]

Output :

Vertex   Distance from Source
0                0
1                -1
2                2
3                -2
4                1

Notes

  • Negative weights are found in various applications of graphs. For example, instead of paying cost for a path, we may get some advantage if we follow the path.
  • Bellman-Ford works better (better than Dijksra’s) for distributed systems. Unlike Dijksra’s where we need to find minimum value of all vertices, in Bellman-Ford, edges are considered one by one.
[ad type=”banner”]