Bellman Ford
Computes shortest paths from a single source vertex to all of the other vertices in a weighted directed graph.
Slower than Dijkstra's algorithm. Its time complexity is
O(VE)
.Can handle graph with negative weight edges.
Works better (than Dijkstra's) for distributed system. Unlike Dijkstra's where we need to find minimum value of all vertices, Bellman-Force considers edges one by one.
Implementation
Let dist[u]
be the length of the shortest path from src
to u
.
Initially dist[src] = 0
and dist[u] = INF
for all other vertices.
Repeat V - 1
times (since the path in the graph is at most of length V - 1
):
For each edge
E = (u, v, weight)
, try to useE
to update thedist[v]
: Ifdist[u] + weight < dist[v]
, thendist[v] = dist[u] + weight
.
Problems
Reference
Last updated