Proofs: Slow all-pairs shortest paths by matrix multiplication
These are the human proofs of Slow all-pairs shortest paths by matrix multiplication, at full length.
SLOW-APSP computes hop-bounded distances and all distances full
This proof shows: correctness, termination.
The graph has vertices and real weights. is built as on the main page: over the self-loops of weight (0 if there are none), is the least weight of an arc for , and if there is none. For let be the infimum of over walks from to with at most arcs, where the empty walk counts for . There are finitely many such walks, so is a real number or . Let be the value of after the loop step for (and ).
Lemma 1. For every and all , .
Proof. By induction on . For : a walk with at most one arc from to is one arc , and the least weight is . For the walks are of weight 0 and the self-loops, and the least weight is .
Step from to . Line L8 gives .
() Each term is the weight of a walk from to with at most arcs, or . If is finite and , it is an arc , and a best walk to with at most arcs followed by that arc has at most arcs. If , then is 0 (append nothing) or a self-loop (append it). So each term is at least .
() Let be a walk from to with at most arcs. If has no arc, then , and the term is at most . Otherwise let be its last arc and the rest, which has at most arcs. The term for is at most ; for this uses the weight of every self-loop at . So the minimum is at most .
Lemma 2. If the graph has no negative cycle, then for all .
Proof. Every walk from to that repeats a vertex contains a cycle, and removing it gives a shorter walk from to of weight at most as large, since the cycle has weight at least 0. Repeating this gives a simple path, with at most arcs and weight at most . So the infimum over all walks equals the infimum over walks with at most arcs, and also over walks with at most arcs, since these lie in between. For an unreachable pair both sides are .
Lemma 3. The graph has a negative cycle if and only if for some .
Proof. () A negative cycle has at most arcs, as its vertices are distinct. For a vertex on , the walk around from is a walk from to with at most arcs and negative weight, so . () If there is no negative cycle, Lemma 2 gives , because the empty walk has weight 0 and no closed walk is negative.
Theorem. SLOW-APSP returns "negative cycle" if and only if the graph has a negative cycle. Otherwise it returns for all , with exactly for the pairs where is not reachable from .
Proof. After the loop of lines L2 and L3, (for the loop is empty and ). By Lemma 1, . Line L4 tests the condition of Lemma 3. If it does not return, there is no negative cycle, and Lemma 2 gives . A pair has a finite entry exactly when some walk joins it, which is reachability.
Termination. All loops have fixed bounds: calls of EXTEND, each with steps, and tests in line L4.
Back to Slow all-pairs shortest paths by matrix multiplication