Loading lessons...
Shortest Path Algorithms - Dijkstra & Bellman-Ford
Shortest Path Algorithms - Dijkstra & Bellman-Ford
A navigation app must find the cheapest route between two places, where each road has a length, time or toll. In a weighted graph the length (cost) of a path is the sum of its edge weights, and a shortest path from u to v is a path of minimum cost. This is different from a minimum spanning tree: an MST connects all vertices cheaply, while a shortest-path tree gives the cheapest route from one source to each vertex.
Dijkstra shortest path
1. Kinds of Shortest-Path Problems
| Problem | Question | Algorithm |
|---|---|---|
| Single-source | Shortest paths from one source s to every vertex | Dijkstra (no negative weights), Bellman-Ford (negative weights allowed) |
| Single-pair | Shortest path from s to one target t | Run single-source from s and stop at t |
| All-pairs | Shortest paths between every pair | Floyd-Warshall (Beyond Syllabus lesson), or Dijkstra from every vertex |
| Unweighted | Fewest edges | BFS, O(n + e) |
2. Relaxation - the Basic Step
Each algorithm keeps an estimate d[v] (best distance found so far from s; initially ∞, and d[s] = 0) and prev[v] (the vertex before v on that path). Relaxing edge (u, v) with weight w asks: is it cheaper to reach v by going through u?
Algorithm RELAX(U, V, W)
if D[U] + W(U, V) < D[V] then
D[V] ← D[U] + W(U, V) // found a shorter path to V
PREV[V] ← UExample: d[u] = 5, d[v] = 12, w(u, v) = 4 → 5 + 4 = 9 < 12, so d[v] becomes 9 and prev[v] = u.
The algorithms differ only in which edges they relax and in what order.
3. Dijkstra's Algorithm
Idea (greedy): keep a set of finished vertices whose shortest distance is final. Repeatedly pick the unfinished vertex with the smallest d, mark it finished, and relax all its outgoing edges. Because all weights are ≥ 0, no later path can be cheaper than the current smallest d, so the choice is safe.
Algorithm DIJKSTRA(G, W, S)
for each vertex V do
D[V] ← ∞; PREV[V] ← NIL; DONE[V] ← FALSE
D[S] ← 0
repeat n times
U ← vertex with DONE[U] = FALSE and smallest D[U]
DONE[U] ← TRUE // D[U] is now final
for each edge (U, V) do
if DONE[V] = FALSE then
RELAX(U, V, W)Worked example (directed graph)
| From | Edges (to : weight) |
|---|---|
| s | 2 : 9, 6 : 14, 7 : 15 |
| 2 | 3 : 24 |
| 3 | 5 : 2, t : 19 |
| 4 | 3 : 6, t : 6 |
| 5 | 4 : 11, t : 16 |
| 6 | 3 : 18, 5 : 30, 7 : 5 |
| 7 | 5 : 20, t : 44 |
Each row: the vertex picked (finished), the relaxations that improved a distance, and the d values afterwards (blank = already finished).
| Pick (d) | Improvements | s | 2 | 3 | 4 | 5 | 6 | 7 | t |
|---|---|---|---|---|---|---|---|---|---|
| s (0) | 2 = 9, 6 = 14, 7 = 15 | 0 | 9 | ∞ | ∞ | ∞ | 14 | 15 | ∞ |
| 2 (9) | 3 = 33 | 9 | 33 | ∞ | ∞ | 14 | 15 | ∞ | |
| 6 (14) | 3 = 32, 5 = 44 | 32 | ∞ | 44 | 14 | 15 | ∞ | ||
| 7 (15) | 5 = 35, t = 59 | 32 | ∞ | 35 | 15 | 59 | |||
| 3 (32) | 5 = 34, t = 51 | 32 | ∞ | 34 | 51 | ||||
| 5 (34) | 4 = 45, t = 50 | 45 | 34 | 50 | |||||
| 4 (45) | none | 45 | 50 | ||||||
| t (50) | - | 50 |
Final distances: 2 = 9, 6 = 14, 7 = 15, 3 = 32, 5 = 34, 4 = 45, t = 50.
Recovering the path: follow PREV back from t: t ← 5 ← 3 ← 6 ← s, so the shortest path is s → 6 → 3 → 5 → t (14 + 18 + 2 + 16 = 50).
Analysis
| Implementation | Time |
|---|---|
| Adjacency matrix, linear scan for the minimum | O(n2) |
| Adjacency list + binary min-heap | O((n + e) log n) |
Why Dijkstra fails with negative weights
Edges: S→A 2, S→B 5, B→A −4, A→C 1.
- Dijkstra finishes A first with d[A] = 2, then C with d[C] = 3, then B with d[B] = 5.
- But S → B → A costs 5 − 4 = 1, so the true d[A] = 1 and d[C] = 2. A was declared final too early.
With negative weights, use Bellman-Ford.
4. Bellman-Ford Algorithm
Idea: a shortest path uses at most n − 1 edges. Relax every edge, n − 1 times. After pass k, every shortest path that uses at most k edges is correct. One extra pass detects negative cycles.
Algorithm BELLMAN_FORD(G, W, S)
for each vertex V do D[V] ← ∞
D[S] ← 0
for I ← 1 to n - 1 do
for each edge (U, V) of G do
RELAX(U, V, W)
for each edge (U, V) of G do // n-th pass
if D[U] + W(U, V) < D[V] then
return "negative cycle"
return DTrace on the negative-weight example (edge order: SA, SB, AC, BA):
| Pass | d[S] | d[A] | d[B] | d[C] | Changes |
|---|---|---|---|---|---|
| start | 0 | ∞ | ∞ | ∞ | - |
| 1 | 0 | 1 | 5 | 3 | A = 2, B = 5, C = 3, then BA gives A = 1 |
| 2 | 0 | 1 | 5 | 2 | AC gives C = 2 |
| 3 | 0 | 1 | 5 | 2 | none - answer is final |
Negative cycle: a cycle whose total weight is negative (e.g. X → Y 1, Y → X −3). Going round it again and again makes the cost smaller without limit, so no shortest path exists. Bellman-Ford reports this when an edge can still be relaxed after n − 1 passes.
Complexity: O(n × e) - slower than Dijkstra, but more general.
5. Comparison
| Basis | BFS | Dijkstra | Bellman-Ford | Floyd-Warshall |
|---|---|---|---|---|
| Problem | Single source, unweighted | Single source | Single source | All pairs |
| Negative weights | - | No | Yes | Yes |
| Detects negative cycles | - | No | Yes | Yes |
| Strategy | Level order | Greedy | Repeated relaxation | Dynamic programming |
| Time | O(n + e) | O(n2) or O((n + e) log n) | O(n × e) | O(n3) |
MST vs shortest-path tree
| Basis | Minimum spanning tree | Shortest-path tree |
|---|---|---|
| Minimises | Total weight of all tree edges | Distance from the source to each vertex |
| Depends on a source | No | Yes |
| Graph | Undirected | Directed or undirected |
6. Applications
- Road maps and GPS navigation; flight connections.
- Network routing protocols (OSPF uses Dijkstra; RIP uses a Bellman-Ford style method).
- Robot and game path finding; currency arbitrage detection (negative cycles).
Key points to remember - Relax (u, v): if d[u] + w < d[v], then d[v] = d[u] + w and prev[v] = u. - Dijkstra: pick the unfinished vertex with the smallest d, relax its edges; non-negative weights only; O(n2) or O((n + e) log n). - Bellman-Ford: relax all edges n − 1 times; allows negative weights; one more pass detects negative cycles; O(n × e). - Worked example: shortest s → t = 50 via s → 6 → 3 → 5 → t.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Explain Dijkstra's algorithm. Find the shortest paths from s in the given graph. (10 Marks)
- Write: relaxation,
DIJKSTRA, the pick/distance table, the PREV path s → 6 → 3 → 5 → t = 50, and the complexity. - Q2. Why does Dijkstra's algorithm fail with negative edge weights? How does Bellman-Ford handle them? (7.5 Marks)
- Write: the S, A, B, C counter-example,
BELLMAN_FORD, its pass table, and negative-cycle detection. - Q3. Differentiate between a minimum spanning tree and a shortest-path tree. (2.5 Marks)