Bellman–Ford and Negative Edge Weights
Track edge relaxations and check what a further improvement implies about reachable negative cycles.
Core Intuition & How It Works
Bellman–Ford computes single-source shortest paths even when edges have negative weights by relaxing every edge V - 1 times. An extra Vth relaxation pass that still reduces any distance detects a reachable negative-weight cycle.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(V × E) |
| Space Complexity | O(V) |
| Topic Chapter | Chapter 12: Shortest Paths & Spanning Trees |
| Difficulty Level | Advanced |
Key Pattern to Recognize
Terminate early if an entire relaxation pass makes zero updates, and check for negative cycles on pass V.
Common Pitfalls & Edge Cases
Using Bellman–Ford when all weights are non-negative (Dijkstra is much faster) or ignoring overflow when relaxing from Infinity.
Real-World & Technical Interview Applications
Currency arbitrage detection, distance-vector routing (RIP), and Johnson’s all-pairs shortest-path preprocessing.