The Single-Source Shortest Paths Problem
Problem Statement
Given a weighted, directed graph with weight function and a designated source vertex , find the shortest path (minimum total weight) from to every other vertex.
Shortest-path weight from to :
where for path .
A shortest path from to is any path satisfying . Shortest paths are not necessarily unique; there may be multiple paths with the same minimum weight.
Variants of Shortest-Path Problems
| Variant | Input | Output |
|---|---|---|
| SSSP (Single-Source) | for all | |
| SDSP (Single-Destination) | for all | |
| SPSP (Single-Pair) | ||
| APSP (All-Pairs) | for all pairs |
SDSP reduces to SSSP on . SPSP has no known algorithm asymptotically faster than SSSP in the worst case. APSP can be solved by iterations of SSSP or by dedicated algorithms (Floyd-Warshall, Johnson).
Assumptions and Edge Cases
- No negative-weight cycles reachable from . If a negative cycle is reachable, you can loop forever, decreasing path weight without bound: for any vertex on the cycle, and the problem is ill-defined.
- Positive-weight cycles are never part of a shortest path: removing such a cycle strictly reduces the total weight. Hence shortest paths can be assumed simple (no vertex repeated).
- Zero-weight cycles do not affect path weight; they can be removed without changing the result. So we assume shortest paths have at most edges.
- Distances are undefined (or ) if a negative-weight cycle is reachable on the path to that vertex.
Optimal Substructure
Shortest paths exhibit optimal substructure: any subpath of a shortest path is itself a shortest path.
Proof: Let be a shortest path, and let be any contiguous subpath. If there were a shorter path from to , replacing the subpath with would produce a path strictly shorter than , contradicting optimality of .
This property is the foundation for dynamic programming (Floyd-Warshall) and greedy (Dijkstra) approaches.
The Relaxation Operation
All shortest-path algorithms in this course share a common subroutine: relaxation of an edge. The idea is to test whether going through improves the current estimate for :
RELAX(u, v, w):
if d[v] > d[u] + w(u, v):
d[v] = d[u] + w(u, v)
π[v] = u
- is the current estimate of .
- is the predecessor of on the current candidate shortest path.
- Initialisation sets and for all , and all .
Upper-bound property: After any sequence of relaxations, always holds. Relaxation never makes estimates too low; it only lowers overestimates towards the true value.
Predecessor Subgraph
The edges for all with form the predecessor subgraph . At any point during execution, is a tree rooted at (a “shortest-path tree” in progress). When the algorithm terminates correctly, the unique path in from to any reachable is a shortest path in .
To reconstruct a path: start at , follow pointers backwards until reaching , then reverse the sequence.
Algorithm Landscape
All SSSP algorithms differ only in the order in which they relax edges:
| Algorithm | Constraint | Edge Order | Time |
|---|---|---|---|
| BFS | Unweighted ( for all edges) | By distance levels | |
| DAG-SSSP | DAG only | Topological order | |
| Dijkstra | Greedy by | ||
| Bellman-Ford | None (no neg. cycles) | All edges times |
Worked Example: Relaxation Sequence
Graph: (5), (3), (1), (2).
| Operation | ||||
|---|---|---|---|---|
| Initialise | 0 | |||
| RELAX(s,a) | 0 | 5 | ||
| RELAX(s,b) | 0 | 5 | 3 | |
| RELAX(a,b) | 0 | 5 | 3 | |
| RELAX(b,c) | 0 | 5 | 3 | 5 |
Final: , , (path ).
The order of relaxation matters: if were relaxed before , we would temporarily set , but then corrects it to 3. Bellman-Ford handles this by relaxing everything repeatedly; Dijkstra avoids it by always processing vertices in increasing distance order.
Summary
| Concept | Description |
|---|---|
| Minimum path weight from to | |
| Optimal substructure | Subpath of shortest path is shortest |
| Relaxation | if d[v] > d[u] + w(u,v): update |
| Upper bound | always |
| Predecessor | Encodes shortest-path tree |
| Max simple path length | edges |
Past paper questions: y2024p1q9 (opportunities, relaxed costs), y2023p1q9 (SSSP variants).