DSA Notes
Learn Bellman-Ford shortest path algorithm that handles negative edge weights, detects negative cycles, with complete implementation and analysis.
Why Bellman-Ford?
Dijkstra's algorithm fails when graphs have negative edge weights. Consider a graph where going through a negative-weight edge actually makes a path shorter — Dijkstra's greedy approach would miss this because it finalizes vertices too early. Bellman-Ford solves this by taking a more patient approach: relax ALL edges, not just once, but V-1 times.
The Core Idea
The algorithm is beautifully simple:
- Relax every edge in the graph V-1 times
- After V-1 iterations, all shortest paths are guaranteed to be found
- If a V-th iteration still improves any distance, a negative cycle exists
Why V-1? Because the shortest path between any two vertices has at most V-1 edges (it visits each vertex at most once). Each iteration of relaxation fixes the shortest path for paths with one more edge. So after V-1 iterations, all paths of length up to V-1 are optimal.
Algorithm Steps
Step-by-Step Example
Graph (directed)
A --(-1)--> B --(3)--> C
| ^
+--(4)---> D --(-2)-->+
Edges: (A,B,-1), (A,D,4), (B,C,3), (D,C,-2)
Source: A, Vertices = 4, so we do 3 iterations
Initial: dist = {A:0, B:inf, C:inf, D:inf}
Iteration 1 (relax all edges)
(A,B,-1): dist[A]+(-1) = -1 < inf → dist[B] = -1
(A,D,4): dist[A]+4 = 4 < inf → dist[D] = 4
(B,C,3): dist[B]+3 = 2 < inf → dist[C] = 2
(D,C,-2): dist[D]+(-2) = 2 = 2 → no change
dist = {A:0, B:-1, C:2, D:4}
Iteration 2
(A,B,-1): 0+(-1)=-1 = -1 → no change
(A,D,4): 0+4=4 = 4 → no change
(B,C,3): -1+3=2 = 2 → no change
(D,C,-2): 4+(-2)=2 = 2 → no change
No changes — algorithm converged early!
Iteration 3: No changes.
Negative cycle check: No edge improves. Safe!
Final: dist = {A:0, B:-1, C:2, D:4}
Python Implementation
C++ Implementation
Time and Space Complexity
- Time: O(V × E) — V-1 iterations, each processing all E edges
- Space: O(V) — for distance and predecessor arrays
- Slower than Dijkstra O((V+E) log V) but handles negative weights
Bellman-Ford vs Dijkstra
| Feature | Dijkstra | Bellman-Ford |
|---|---|---|
| Negative weights | Cannot handle | Handles correctly |
| Negative cycle detection | No | Yes |
| Time complexity | O((V+E) log V) | O(VE) |
| Approach | Greedy | Dynamic Programming |
| Implementation | Priority queue | Simple loop |
| Best for | Non-negative weights, speed | Negative weights, simplicity |
Negative Cycle Detection
A negative cycle is a cycle whose total edge weight is negative. If such a cycle is reachable from the source, then shortest paths to some vertices are negative infinity (keep going around the cycle). Bellman-Ford detects this: if after V-1 iterations any edge can still be relaxed, the graph has a reachable negative cycle.
Applications
- Currency arbitrage detection: Currencies as vertices, exchange rates as log-edges. A negative cycle means profitable circular exchange.
- Network routing: Distance vector protocols (like RIP) use Bellman-Ford
- Constraint systems: Solving systems of difference constraints
- As a subroutine: Johnson's all-pairs algorithm uses Bellman-Ford to reweight edges
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Bellman-Ford Algorithm.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, graphs, bellman, ford
Related Data Structures & Algorithms Topics