Sub topic 13
All-Pairs Path Reconstruction with a Next-Hop Matrix
This chapter assumes you know how Floyd-Warshall fills its distance matrix with the recurrence over intermediate vertices. It also assumes you have seen single-source path recovery with a predecessor array. Floyd-Warshall gives you every shortest distance, but a distance does not tell you which route to take. A next-hop matrix stores the routes themselves, using O(V²) extra space.
What the Matrix Stores
nxt[i][j] holds the first vertex after i on a shortest path from i to j. To rebuild the full path, start at i, jump to nxt[i][j], and repeat until you reach j. Every step uses the same target column j, because each later vertex also needs its own first hop toward j.
Initialization follows directly from the definition:
nxt[i][i] = ifor every vertex.nxt[u][v] = vfor each edgeu → v. When there are parallel edges, use the one with the smallest weight.- Every other entry is
None, which means no path is known yet.
The Update Rule
When vertex k improves the pair (i, j), the new route is "go from i to k, then from k to j." The first hop of that route is the first hop toward k:
nxt[i][j] = nxt[i][k]A common mistake is writing nxt[i][j] = k. That only works when i and k are directly connected. Usually they are not, and in that case the walk would skip vertices.
Tracing the Example Graph
The example uses the graph from earlier chapters, with edges 0→1 (4), 0→2 (5), 1→2 (-3), 2→3 (4), and 1→3 (6).
| Step | Improvement | `nxt` update |
|---|---|---|
| k = 1 | dist[0][2]: 5 → 1 | nxt[0][2] = nxt[0][1] = 1 |
| k = 1 | dist[0][3]: ∞ → 10 | nxt[0][3] = nxt[0][1] = 1 |
| k = 2 | dist[0][3]: 10 → 5 | nxt[0][3] = nxt[0][2] = 1 |
| k = 2 | dist[1][3]: 6 → 1 | nxt[1][3] = nxt[1][2] = 2 |
When the path to 3 improves at k = 2, the first hop nxt[0][3] stays 1. Only the middle of the route changed. The walk reads nxt[0][3] = 1, then nxt[1][3] = 2, then nxt[2][3] = 3.
A Guarded Implementation
import math
def floyd_warshall_next(n, edges):
dist = [[math.inf] * n for _ in range(n)]
nxt = [[None] * n for _ in range(n)]
for i in range(n):
dist[i][i] = 0
nxt[i][i] = i
for u, v, w in edges:
if w < dist[u][v]:
dist[u][v] = w
nxt[u][v] = v
for k in range(n):
for i in range(n):
for j in range(n):
if dist[i][k] + dist[k][j] < dist[i][j]:
dist[i][j] = dist[i][k] + dist[k][j]
nxt[i][j] = nxt[i][k]
return dist, nxt
def path(nxt, i, j):
if nxt[i][j] is None:
return [] # j unreachable from i
n = len(nxt)
p = [i]
while i != j:
i = nxt[i][j]
p.append(i)
if len(p) > n:
raise ValueError('path runs through a negative cycle')
return p
edges = [(0, 1, 4), (0, 2, 5), (1, 2, -3), (2, 3, 4), (1, 3, 6)]
dist, nxt = floyd_warshall_next(4, edges)
print(dist[0][3], path(nxt, 0, 3)) # 5 [0, 1, 2, 3]
print(path(nxt, 3, 0)) # []Next-Hop vs. Predecessor Matrix
You can also store pred[i][j], the last vertex before j, and update it with pred[i][j] = pred[k][j]. That version rebuilds the path backward and then reverses it, the same way the single-source predecessor array does. Both matrices cost O(V²) space. The next-hop form produces the path in forward order, and it matches how a network routing table works: each router only needs to know the next hop toward each destination.
Exercise
After the run above, what is nxt[0][2], and why is it not 2 even though the edge 0→2 exists?
Show answer
nxt[0][2] = 1. The route 0→1→2 costs 4 + (-3) = 1, which is less than the direct edge's 5. At k = 1 the update set nxt[0][2] = nxt[0][1] = 1, replacing the initial value of 2.
Exercise
Why does the update rule use nxt[i][j] = nxt[i][k] instead of nxt[i][j] = k?
Show answer
Vertex k is somewhere in the middle of the new path and is often not adjacent to i. The first step of the path from i through k to j is the first step from i to k, which is nxt[i][k]. Setting the entry to k would skip every vertex between i and k.