Bellman-Ford algorithm Created 2026-10-14 Updated 2026-10-18
The Bellman-Ford algorithm solves the single-source shortest path problem in a finite directed graph with finite real edge weights, allowing negative weights. It repeatedly applies edge relaxation, so an improved tentative distance can improve its neighbours on a later visit. This makes it a label-correcting shortest-path algorithm.[ref]
BellmanFordSP.java. Algorithms, 4th edition by Robert Sedgewick and others; Robert Sedgewick and Kevin Wayne, BellmanFordSP.java, signed-weight single-source solver and queue-based relaxation
If no negative-cost directed cycle is reachable from the source, at most complete passes suffice for all distances; a further check detects a reachable negative-cost directed cycle.[ref]Lecture 17: Shortest Paths III: Bellman-Ford. 6.006 Introduction to Algorithms, Fall 2011 by Massachusetts Institute of Technology, 6.006 course staff; MIT 6.006, Lecture 17, pp. 3–4: complete-pass algorithm, proof and reachable-cycle corollary
There are two orientations to keep distinct. The usual single-source problem asks for distances from , extending routes along outgoing directed edges. The Bellman inequalities above instead concern distances to a sink , prepending incoming directed edges to known routes. The All-to-one Bellman-Ford recurrence below preserves that convention. Reversing every directed edge, with its weight unchanged, converts one problem into the other.
Here is an original single-source example with source , vertex set , and precisely these directed edges:The negative edge is harmless: this directed graph has no directed cycle. Its best route to is , of cost . The other routes to cost through and through .
Initialize and the other tentative distances to . In each pass apply single-source edge relaxation, , scanning in the order , , , , , . Skip a candidate with . The figure records labels after each complete pass and uses current labels immediately: it is an in-place trace. In pass 2, still uses , giving ; only later does lower to . Pass 3 carries that improvement to , and pass 4 carries it to . The final labels in order are ; the final shortest-path predecessors are for respectively.
The scan order explains the delay. Scanning instead as , , , , , gives those same final labels in just one in-place pass. The Synchronous Bellman-Ford recurrence has a precise bound on the number of edges used by a recorded route; an in-place pass can propagate along several edges and need not have that interpretation. Both implementations have the pass guarantee under the stated no-reachable-negative-cycle hypothesis.
With a stored list of edges, complete-pass Bellman-Ford algorithm has time complexity and uses extra space for labels and shortest-path predecessors, using Big O notation and constant-cost arithmetic. The extra accounts for initialization even when is empty. A pass with no strict decrease permits early termination. A queue variant instead revisits outgoing edges of vertices whose labels improved; the authored implementation provides this alternative and has worst-case running time on its usual nontrivial instances.[ref]
BellmanFordSP.java. Algorithms, 4th edition by Robert Sedgewick and others; Robert Sedgewick and Kevin Wayne, BellmanFordSP.java, class documentation, constructor and relax method
For nonnegative weights, the Dijkstra algorithm and its existing distance-frontier figure explain how permanent labels can avoid these repeated passes; the existing edge relaxation figure illustrates one update at a time. Bellman-Ford negative-cycle detection 2026-10-18
A negative-cost directed cycle matters to a particular destination precisely when it is reachable from and can itself reach .[ref]
Lecture 17: Shortest Paths III: Bellman-Ford. 6.006 Introduction to Algorithms, Fall 2011 by Massachusetts Institute of Technology, 6.006 course staff; MIT 6.006, Lecture 17, p. 1: cycle between source and destination; p. 4: reachable-cycle corollary
Repeating it times adds times its negative total cost to a route. Thus the infimum of directed walk costs is , rather than the cost of a finite shortest directed walk. The distinction is about reachability, not simply the presence of a negative edge or a negative-cost directed cycle anywhere in the graph.Add of weight to the worked example. Now has total weight . Add also an isolated vertex with a self-loop of weight . From , the distances of are , because all four are reachable from the first negative-cost directed cycle. The source retains distance : no directed edge leads back to . The unreachable retains distance , despite its negative-cost self-loop.
Relevant negative-cost directed cycles: descendants from a source become ancestors when every directed edge is reversed
. Details. For example, extra laps before proceeding to give costs . This construction also shows why requiring a directed path with no repeated vertices would change the problem: only directed walks permit arbitrarily many laps. Merely finding one reachable negative-cost directed cycle does not justify setting every reachable label to ; in the figure is a counterexample.
To detect and classify, perform full passes without clamping any label, then test all directed edges against a fixed copy of the resulting labels. Collect heads of edges satisfying and . An empty collection certifies that no negative-cost directed cycle is reachable from . Otherwise run graph search forwards from all collected vertices: precisely the reached vertices have distance . A vertex unreachable from still has distance ; each remaining vertex has its finite shortest distance.
Why is this classification exact? Every unaffected reachable vertex already has its shortest distance after passes, since any route ending there avoids reachable negative-cost directed cycles. Thus it cannot be the head of a strict improvement, or be reached from such a head. Conversely, all vertices on a reachable negative-cost directed cycle have finite labels by that time. If none of its edges could improve a label, summing around it would imply . At least one of its heads is therefore collected, and the forward graph search reaches the entire directed cycle and every affected descendant. Detecting a directed cycle alone and marking its descendants are separate tasks; finite intermediate labels in the affected region are not answers.
Bellman inequalities Created 2026-10-14 Updated 2026-10-18
The Bellman inequalities turn local directed edge checks into a global certificate for the shortest path problem. Here the convention is all-to-one: every journey ends at a fixed vertex , and is the cost of the original directed edge . Work in a finite directed graph with finite real costs. Write for candidate labels, normalized by , on the vertices that can reach . A feasible potential means precisely that these finite labels satisfyfor every directed edge within that set. These labels give lower bounds on costs to . A discovered directed path of cost instead gives an upper bound. The following constructed graph shows both bounds and the exact distances between them; its weights are illustrative costs, not measured travel data.
The arrows in the picture point toward the destination, whereas edge relaxation propagates an improved label in the opposite direction: an improved can lower . In the pictured graph, the directed paths from to cost , , and . Thus . The candidate in vertex order is feasible, but underestimates the exact distances . Merely satisfying the Bellman inequalities does not make a candidate exact.
Why do they give lower bounds? Along any directed path , successive substitution givesThe same calculation works for a directed walk, with repeated vertices allowed. Taking the infimum of route costs gives . A discovered route gives . If the bounds meet, , they certify an optimum without enumerating routes. Equivalently, check the Bellman inequalities for the proposed distances and exhibit directed paths realizing them. The authored Bellman-Ford algorithm implementation checks the corresponding single-source inequalities and equality on recorded route edges; reversing every directed edge gives this all-to-one convention.[ref]
BellmanFordSP.java. Algorithms, 4th edition by Robert Sedgewick and others; Sedgewick and Wayne, BellmanFordSP.java, optimality checks
For the exact labels in the picture, the slack is respectively on , , , , . Zero slack means equality; the zero-slack directed path realizes the cost . This is the useful beauty of the certificate: a few local inequalities rule out every cheaper journey, while one explicit journey attains the bound.
Keep unreachable vertices separate: when no directed path to exists, and a finite lower bound alone cannot certify that fact. Extended labels can use for finite , but never compute a slack as . If can reach a negative-cost directed cycle and then reach , repeatedly traversing the directed cycle makes route costs unbounded below: their infimum is , with no finite shortest directed walk. A finite feasible potential on such a directed cycle is impossible, since summing its Bellman inequalities would give . A negative-cost directed cycle that cannot reach does not affect distances to . These are statements about unrestricted directed walks; requiring a directed path with no repeated vertices is a different optimization problem when negative-cost directed cycles exist.[ref]
Lecture 17: Shortest Paths III: Bellman-Ford. 6.006 Introduction to Algorithms, Fall 2011 by Massachusetts Institute of Technology, 6.006 course staff; MIT 6.006, Lecture 17, pp. 1 and 5: relevant negative cycles and simple paths
For the single-source convention, the same original directed edge instead gives with , a lower bound on distance from . The endpoint carrying the label on the left therefore changes with the direction of the problem.


