← chapter

Bellman-Ford

Shortest paths with NEGATIVE edge weights — the case Dijkstra can't handle. Relax every edge, V−1 times.

Why Dijkstra breaks

Watch distances ripple

Numbers = distance estimates · red edge = negative (2→1, w=−3). Node 1 drops 4 → 2 via the negative edge; pass 2 propagates it onward.

What you're buying

30% negative edges → Dijkstra silently wrong on ~5% of nodes. Bellman-Ford: always right.

Cycle detection for free

Takeaway

Dijkstra faster but only valid on non-negative graphs — it lies, it doesn't crash. Bellman-Ford = dynamic programming on graphs, the truthful fallback. Next: all-pairs at once → Floyd-Warshall.