On this page
Sometimes a route has negative costs: a refund, an energy gain, a good exchange rate. Dijkstra's algorithm is not built for this; Bellman–Ford is. Its roots include Richard Bellman's 1958 paper On a routing problem, on finding the quickest path between cities. New to graphs? Start with how adjacency matrix powers count walks or the wolf, goat and cabbage state graph.
What does the Bellman–Ford algorithm do?
Bellman–Ford finds the shortest distance from one source to every vertex of a directed graph whose edge weights may be negative. Its only move is relaxation: for an edge u → v of weight w, if d(u) + w is less than d(v), set d(v) = d(u) + w.
It relaxes every edge n − 1 times, where n is the number of vertices. Without a negative cycle, a shortest path never repeats a vertex, so it has at most n − 1 edges, as MIT's 6.006 notes on Bellman–Ford prove. Bellman–Ford takes time proportional to vertices × edges (Sedgewick and Wayne, Algorithms, section 4.4).
How does Bellman–Ford work on a 5-vertex example?
Take vertices S, A, B, C, D and seven edges, relaxed in this order every round:
C → D, weight 2
A → C, weight 3
B → A, weight −3
S → A, weight 4
S → B, weight 5
B → D, weight 6
D → C, weight −1
Start with d(S) = 0 and every other distance ∞.
Round 1: S → A gives A = 4, S → B gives B = 5, B → D gives D = 11, D → C gives C = 10.
Round 2: A → C gives C = 4 + 3 = 7, then B → A gives A = 5 − 3 = 2. Now A 2, B 5, C 7, D 11.
Round 3: C → D gives D = 7 + 2 = 9, then A → C gives C = 2 + 3 = 5. Now A 2, B 5, C 5, D 9.
Round 4: C → D gives D = 5 + 2 = 7. Final: A 2, B 5, C 5, D 7.
Round 4 is round n − 1, and the answers are final, with shortest-path tree S → B → A → C → D. A costs 2 through the negative edge, not 4 directly. Stop one round early and D is still 9.
How does Bellman–Ford detect a negative cycle?
Run one more round, the n-th. If some distance still drops, a negative cycle is reachable from the source, and if nothing drops, none is (Sedgewick and Wayne, section 4.4). The MIT 6.006 notes call a vertex whose n-edge distance beats its (n − 1)-edge distance a witness.
Change D → C from −1 to −3. Now the loop C → D → C costs 2 − 3 = −1. After round 4 the distances are A 2, B 5, C 4, D 7; in round 5, C → D lowers D to 6. That change is the alarm: C and D have no shortest distance, because you can keep circling.
Why does Dijkstra fail with negative edges?
Dijkstra is greedy: it settles the vertex with the smallest tentative distance and never revisits it. Sedgewick and Wayne state it for non-negative weights and note that a version which never revisits a vertex may yield incorrect results with negative edges (section 4.4).
On our example, Dijkstra settles S at 0, then A at 4, setting C = 7. It settles B at 5, but A is already final, so S → B → A at 2 is ignored. It ends with A 4, C 7, D 9, against the correct 2, 5 and 7.
How does Bellman–Ford spot currency arbitrage?
An arbitrage is a loop of currency trades that ends with more money than it started with. Sedgewick and Wayne's example uses USD → EUR 0.741, EUR → CAD 1.366 and CAD → USD 0.995: 1,000 dollars become 741 euros, then 1,012.206 Canadian dollars, then 1,007.14497 dollars.
Rates multiply, but path lengths add, so give each trade the weight −ln(rate), as the same page does. A product above 1 becomes a sum below 0: an arbitrage loop is a negative cycle. Here the weights are about 0.300, −0.312 and 0.005, adding to about −0.007, and the n-th round check flags it.
Which shortest-path method should you use?
All weights zero or positive: Dijkstra.
Undirected graph with a negative edge: that edge alone forms a negative cycle, as the MIT notes point out.
Directed graph with negative edges: Bellman–Ford, n − 1 rounds, then the n-th round check.
By hand: fix the edge order and write one row of distances per round.
To practise traces like this, the Fermi Problem Library has worked problems with a step-by-step solver that explains the reasoning rather than printing an answer. For another algorithm with a stopping rule, see how Gale–Shapley finds a stable matching.



