"Dijkstra trusts that getting farther can only cost more. Negative edges break that trust. Bellman-Ford is the patient, slower algorithm that makes no such assumption — and can even detect when 'cheapest' has no answer at all."
The Case Dijkstra Can't Handle
Some edges genuinely have negative weights: a currency exchange that nets a gain, a financial flow that's a credit, a game move that restores health. Dijkstra fails here because its core assumption — finalizing the closest node is safe — relies on every edge adding cost. A negative edge could let a node you'd already finalized be reached more cheaply later. Bellman-Ford drops that assumption entirely, at the price of being slower.
The Algorithm: Relax Everything, V−1 Times
Bellman-Ford is almost brutally simple: relax every edge in the graph, and repeat that V−1 times. 'Relax edge u→v' means: if reaching v through u is cheaper than v's current distance, update it. Why exactly V−1 rounds? Because a shortest path in a graph of V nodes uses at most V−1 edges, and each full round of relaxing all edges guarantees correct distances propagate one more edge outward from the source. After V−1 rounds, every shortest path (at most V−1 edges long) has been fully resolved. It's O(V·E) — slower than Dijkstra's O((V+E) log V) — but it tolerates negatives.
The Superpower: Negative-Cycle Detection
Here's something Dijkstra can't even attempt. If a graph has a negative cycle — a loop whose total weight is negative — then 'shortest path' is meaningless: you could circle that loop forever, getting cheaper each time, distance diving to negative infinity. Bellman-Ford detects this elegantly: after the V−1 rounds, do one more round. If any edge can still be relaxed (still improves), a negative cycle exists. This is exactly how currency arbitrage is detected — model exchange rates as a graph, and a negative cycle is a sequence of trades that returns more money than you started with. The algorithm that finds 'no answer exists' is sometimes the most valuable one.