Sub topic 2
Single-Source vs. All-Pairs Shortest Paths
This chapter assumes you know what vertices and directed edges are, can read Big-O notation, and know how a weighted directed graph is stored as an adjacency list or matrix. It sets out the two versions of the shortest path problem that the rest of the tutorial solves.
The Problem in General
A path's weight is the sum of the weights of its edges. The shortest path from u to v is a path whose weight is as small as possible. We call that weight the distance, written d(u, v). If no path exists, the distance is infinity.
There are two ways to ask the question. They need different kinds of output, and different algorithms answer them.
The Running Example
Single-Source Shortest Paths
Single-source means you fix one source vertex s and want d(s, v) for every vertex v. The answer is a list of length V.
With source 0 in the graph above, the answer is [0, 4, 1, 5]. The distance to vertex 2 is 1, through 0 → 1 → 2, even though the direct edge 0 → 2 costs 5. The negative edge makes the longer route cheaper.
Bellman-Ford solves this version. It is the right choice when every query starts from the same place, for example a router computing routes from itself.
All-Pairs Shortest Paths
All-pairs means you want d(u, v) for every ordered pair of vertices. The answer is a V × V matrix:
| from \ to | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| 0 | 0 | 4 | 1 | 5 |
| 1 | ∞ | 0 | -3 | 1 |
| 2 | ∞ | ∞ | 0 | 4 |
| 3 | ∞ | ∞ | ∞ | 0 |
Row 0 is the single-source answer from vertex 0. Each row of the matrix is one single-source problem.
Floyd-Warshall solves this version directly. It fits when queries can start anywhere, for example a distance table between cities.
How the Two Relate
You can always answer all-pairs by running a single-source algorithm once from each vertex. Whether that is a good idea depends on cost:
| Approach | Time | Output size |
|---|---|---|
| Bellman-Ford, one source | O(V·E) | O(V) |
| Bellman-Ford from every source | O(V²·E) | O(V²) |
| Floyd-Warshall | O(V³) | O(V²) |
In a dense graph E approaches V², so repeated Bellman-Ford costs O(V⁴) and Floyd-Warshall is far better. In a sparse graph repeated runs can be competitive. A later chapter covers that trade-off and Johnson's algorithm.
Common Errors
- Mixing up the direction.
d(u, v)andd(v, u)usually differ in a directed graph. In the table,d(0, 1) = 4butd(1, 0) = ∞. - Setting the diagonal to anything but 0. Without a negative cycle, the distance from a vertex to itself is 0.
- Treating "unreachable" as 0 or as a large sentinel. Use infinity (
math.infin Python) so that additions and comparisons behave correctly.
Exercise
You need the shortest travel cost from a single warehouse to 5,000 stores, in a sparse road graph with negative-weight subsidy edges. Should you solve the single-source or the all-pairs problem, and which algorithm from this tutorial fits?
Show answer
Single-source, because every query starts at the warehouse. Use Bellman-Ford from the warehouse at O(V·E). Floyd-Warshall would spend O(V³) time and O(V²) memory on pairs you never use.
Exercise
In the example table, why is d(1, 3) equal to 1 and not 6?
Show answer
The path 1 → 2 → 3 weighs -3 + 4 = 1, which is less than the direct edge 1 → 3 with weight 6.