Proofs: All-pairs shortest paths by repeated squaring
These are the human proofs of All-pairs shortest paths by repeated squaring, at full length.
Repeated squaring computes all distances or detects a negative cycle full
This proof shows: correctness, termination.
Claim. Let be a directed graph on the vertices with real weights, parallel edges and self-loops allowed. APSP-REPEATED-SQUARING returns NEGATIVE-CYCLE if and only if has a cycle of negative weight. Otherwise it returns the matrix with for all , where when no path exists.
Termination. The variable starts at 1 and doubles in each pass of line L4, so the loop runs times (zero times when ). Each pass has loops with fixed bounds. So the algorithm ends.
Notation. A walk may repeat vertices and edges; the empty walk from to has weight 0. Let be the least weight of a walk from to with at most edges, or if there is none. This is a minimum over finitely many walks (finitely many sequences of at most edges), so it is well defined. Write , with .
Step 1: the start. After line L2, for is the least weight of an edge from to , and . A walk with at most one edge from to is a single edge, or, when , also the empty walk. So . Also for every , because of the empty walk.
Step 2: the product. For , .
() Take with and finite, and walks from to and from to that attain them. The joined walk has at most edges and weight , so this sum is at least . Terms with an infinite factor are and do not matter.
() If there is nothing to show. Otherwise take a walk from to with edges and weight . Let be the vertex of after edges. The part of before has at most edges and the part after has edges, so .
Step 3: the copy in line L5. In a pass, with , let be the true product and the computed matrix, . Since , the term gives , so . By Step 2 and induction on the passes, after each pass, with the current value. At the end, for some (for , ).
Step 4: no negative cycle. Suppose has no cycle of negative weight. Any walk from to that repeats a vertex contains a closed sub-walk, which consists of cycles, each of weight at least 0; removing it gives a walk with fewer edges and no larger weight. Repeating this gives a simple path, with at most edges. So the least walk weight over walks of any length is attained by a simple path, it equals , and since . In particular , line L9 does not fire, and line L10 returns the distances. A pair with no path has no walk at all, so its entry is .
Step 5: a negative cycle. Suppose has a cycle of negative weight. Write it as a closed walk; splitting at repeated vertices writes it as a union of simple cycles, and since their weights add up to a negative number, one simple cycle has . It has at most edges. For a vertex on , is a walk from to with at most edges, so , and line L9 returns NEGATIVE-CYCLE.
Step 6: no false report. If line L9 fires, is the weight of a closed walk from , and by the splitting in Step 5 one of its simple cycles is negative. So a report happens only when has a negative cycle.
Steps 4 to 6 give the claim.