23. All-Pairs Shortest Paths
Exercises
23.1-1
Each iteration of Slow-APSP produces the following matrices:
The Faster-APSP algorithm achieves the same result more efficiently by repeatedly squaring the matrices:
by equation (23.4)
23.1-2
Available in the latest revision of the IM.
23.1-3
Available in the latest revision of the IM.
23.1-4
Available in the latest revision of the IM.
23.1-5
Available in the latest revision of the IM.
23.1-6
Slow-APSP still works correctly, although not producing the same intermediary matrices as the original variant (it "converges" faster). The core insight is that represents the "old" value at the start of the procedure. Obviously, (this is how Relax behaves), so initially "setting" is safe. During the updates of some row (rows are independent of each other) it could happen that uses an already updated value where . But this doesn't cause any issue at the end, just that some temporary values may be fast forwarded in time.
Faster-APSP doesn't need the matrix neither. For the same reason as above, relying on the Relax feature, it can only converge faster.
23.1-7
Available in the latest revision of the IM.
23.1-8
Available in the latest revision of the IM.
23.1-9
Available in the latest revision of the IM.
23.1-10
Available in the latest revision of the IM.
23.2-1
23.2-2
Available in the latest revision of the IM.
23.2-3
We first prove the hint by induction on .
Base Case (): If , equation (23.7) implies that , , and . Substituting with , the inequality becomes , which is trivially true.
Inductive Step: Assume the claim holds for . If , equation (23.8) dictates two possible cases:
is not an intermediate vertex: Here, and . By the inductive hypothesis, . Because shortest-path distances never increase as grows, . Therefore, .
is an intermediate vertex: Here, and . Applying the inductive hypothesis to the subpath from to gives . Substituting this into the equation yields . We know that , which means . Therefore, .
The inequality holds for both cases, completing the induction.
Assume for contradiction that contains a cycle where . For every edge in this cycle, the predecessor of is , meaning . Applying our previously proven inequality with and yields:
Summing this inequality for all edges around the cycle :
Because , the sum of the distances on the left side is identical to the sum of the distances on the right side. Assuming the graph contains no negative-weight cycles, these distances are finite. Subtracting them from both sides gives:
Thus, would have to be exactly 0. However, the strict inequality condition guarantees that predecessor pointers are only updated when a strictly shorter path is found. A 0-weight cycle offers no strict improvement, meaning the algorithm will not alter pointers to form one. Therefore, cannot contain any cycles and is acyclic.
Because is a connected, acyclic subgraph where every reachable vertex has exactly one predecessor leading back to , it forms a tree rooted at . Since the distances correspond to the proven final shortest paths calculated by Floyd-Warshall, it is indeed a shortest-paths tree.
23.2-4
Available in the latest revision of the IM.
23.2-5
Available in the latest revision of the IM.
23.2-6
Available in the latest revision of the IM.
23.2-7
The recurrence formula is
The following procedure assumes we already know a valid path exists.
The matrix is conceptually identical to the table used in the matrix-chain multiplication algorithm:
Optimal Splitting: In matrix-chain multiplication, the table stores the optimal index used to split the product sequence into two smaller halves.
Divide and Conquer: Similarly, the matrix stores the optimal vertex , or
NIL, that strictly splits the shortest path from to into two optimal subpaths: and . Both tables act as blueprints for reconstructing a solution recursively from the top down.
23.2-8
Available in the latest revision of the IM.
23.2-9
Available in the latest revision of the IM.
23.3-1
23.3-2
Available in the latest revision of the IM.
23.3-3
Available in the latest revision of the IM.
23.3-4
Available in the latest revision of the IM.
23.3-5
Available in the latest revision of the IM.
23.3-6
Available in the latest revision of the IM.
Problems
23-1 Transitive closure of a dynamic graph
Available in the latest revision of the IM.
23-2 Shortest paths in ϵ-dense graphs
a.
A -ary min-heap containing elements has a height of :
INSERT: Adding an element at the bottom and bubbling it up takes time proportional to the height of the tree. The running time is .EXTRACT-MIN: Removing the root, replacing it with the last leaf, and trickling it down requires comparing up to children at each level to find the minimum. Over the height of the tree, this yields a running time of .DECREASE-KEY: Decreasing a key's value and bubbling it up towards the root takes time proportional to the height. The running time is .
The running times when for a constant are based on the next observation:
Applying this constant height to the standard operations:
INSERT:EXTRACT-MIN:DECREASE-KEY:
A Fibonacci heap has amortized running times of for INSERT, for DECREASE-KEY, and for EXTRACT-MIN.
b.
Run Dijkstra's algorithm using a -ary min-heap where :
Each of the
DECREASE-KEYoperations takes time. The total time for allDECREASE-KEYoperations is .Each of the
EXTRACT-MINoperations takes time. The total time for allEXTRACT-MINoperations is .
Summing the costs of both primary operations yields a final asymptotic running time of .
c.
Run the algorithm of part (b) from each vertex resulting in total time.
d.
Apply the idea from Johnson’s algorithm for sparse graphs by first reweighting the edges and then running part (c).
Last updated