Bellman-Ford for Negative Weights
The Bellman-Ford algorithm computes single-source shortest paths in directed graphs that may contain negative edge weights by relaxing every edge for V-1 passes. A V-th pass detects negative cycles reachable from the source.
Tutorial
Why Bellman-Ford? The Edge Relaxation Step
The Bellman-Ford algorithm computes single-source shortest paths in a directed graph that may contain negative edge weights.
Dijkstra's algorithm fails on negative edges because its greedy strategy permanently finalizes the closest unvisited vertex. Once a vertex is finalized, a later negative edge that would shorten its distance is never considered. Bellman-Ford avoids this by never committing: it repeatedly improves a running estimate of the shortest-path distance from the source to each vertex .
Initialize
The edge relaxation of a directed edge with weight is the test
For example, suppose at some point , , and . We compute the candidate distance
Since , the test succeeds and we update . If had instead been , the candidate would not beat and we would leave unchanged.