= Bellman-Ford algorithm
{c}
{title2=$\delta(s,v)$}
{wiki}
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>.<Algorithms, 4th edition by Robert Sedgewick and others/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]{ref} If no negative-cost <directed cycle> is reachable from the source, at most $|V|-1$ complete passes suffice for all distances; a further check detects a reachable negative-cost <directed cycle>.<6.006 Introduction to Algorithms, Fall 2011 by Massachusetts Institute of Technology, 6.006 course staff/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]{ref}
There are two orientations to keep distinct. The usual single-source problem asks for distances \i[from] $s$, extending routes along outgoing <directed edges>. The <Bellman inequalities> above instead concern distances \i[to] a sink $r$, 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 $S$, <vertex set> $V=\{S,A,B,C,T\}$, and precisely these <directed edges>:
$$
w_{SA}=4,\qquad w_{SB}=5,\qquad w_{AB}=-2,\qquad w_{BC}=3,\qquad w_{CT}=2,\qquad w_{AT}=8.
$$
The negative <edge> $A\to B$ is harmless: this <directed graph> has no <directed cycle>. Its best route to $T$ is $S\to A\to B\to C\to T$, of cost $4-2+3+2=7$. The other routes to $T$ cost $12$ through $S,A,T$ and $10$ through $S,B,C,T$.
\Image[/bellman-ford-negative-edge-passes.png]
{title=Four in-place <Bellman-Ford algorithm> passes propagate an improvement across a negative-weight <edge>}
Initialize $d(S)=0$ and the other <tentative distances> to $\infty$. In each pass apply single-source <edge relaxation>, $d(v)\gets\min\{d(v),d(u)+w_{uv}\}$, scanning in the order $A\to T$, $C\to T$, $B\to C$, $A\to B$, $S\to B$, $S\to A$. Skip a candidate with $d(u)=\infty$. The figure records labels \i[after] each complete pass and uses current labels immediately: it is an in-place trace. In pass 2, $B\to C$ still uses $d(B)=5$, giving $8$; only later does $A\to B$ lower $B$ to $2$. Pass 3 carries that improvement to $C$, and pass 4 carries it to $T$. The final labels in order $(S,A,B,C,T)$ are $(0,4,2,5,7)$; the final <shortest-path predecessors> are $S,A,B,C$ for $A,B,C,T$ respectively.
The scan order explains the delay. Scanning instead as $S\to A$, $S\to B$, $A\to B$, $B\to C$, $C\to T$, $A\to T$ 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 $|V|-1$ pass guarantee under the stated no-reachable-negative-cycle hypothesis.
With a stored list of <edges>, complete-pass <Bellman-Ford algorithm> has <time complexity> $O(|V||E|+|V|)$ and uses $O(|V|)$ extra space for labels and <shortest-path predecessors>, using <Big O notation> and constant-cost arithmetic. The extra $|V|$ accounts for initialization even when $E$ 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 $O(|V||E|)$ worst-case running time on its usual nontrivial instances.<Algorithms, 4th edition by Robert Sedgewick and others/BellmanFordSP.java>[Algorithms, 4th edition by Robert Sedgewick and others; Robert Sedgewick and Kevin Wayne, BellmanFordSP.java, class documentation, constructor and relax method]{ref} 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.
Back to article page