Algorithm Search⌘K

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 GG be a directed graph on the vertices 1,…,n1, \ldots, n with real weights, parallel edges and self-loops allowed. APSP-REPEATED-SQUARING returns NEGATIVE-CYCLE if and only if GG has a cycle of negative weight. Otherwise it returns the matrix DD with D[i][j]=δ(i,j)D[i][j] = \delta(i, j) for all i,ji, j, where δ(i,j)=+∞\delta(i, j) = +\infty when no path exists.

Termination. The variable kk starts at 1 and doubles in each pass of line L4, so the loop runs ⌈log⁡2n⌉\lceil \log_2 n \rceil times (zero times when n≤1n \le 1). Each pass has loops with fixed bounds. So the algorithm ends.

Notation. A walk may repeat vertices and edges; the empty walk from ii to ii has weight 0. Let L(k)[i][j]L^{(k)}[i][j] be the least weight of a walk from ii to jj with at most kk edges, or +∞+\infty if there is none. This is a minimum over finitely many walks (finitely many sequences of at most kk edges), so it is well defined. Write (A⋆B)[i][j]=min⁡t(A[i][t]+B[t][j])(A \star B)[i][j] = \min_t (A[i][t] + B[t][j]), with ∞+x=∞\infty + x = \infty.

Step 1: the start. After line L2, L[i][j]L[i][j] for i≠ji \ne j is the least weight of an edge from ii to jj, and L[i][i]=min⁡(0,the least self-loop weight at i)L[i][i] = \min(0, \text{the least self-loop weight at } i). A walk with at most one edge from ii to jj is a single edge, or, when i=ji = j, also the empty walk. So L=L(1)L = L^{(1)}. Also L(k)[i][i]≤0L^{(k)}[i][i] \le 0 for every k≥0k \ge 0, because of the empty walk.

Step 2: the product. For a,b≥1a, b \ge 1, L(a)⋆L(b)=L(a+b)L^{(a)} \star L^{(b)} = L^{(a + b)}.

(≥\ge) Take tt with L(a)[i][t]L^{(a)}[i][t] and L(b)[t][j]L^{(b)}[t][j] finite, and walks pp from ii to tt and qq from tt to jj that attain them. The joined walk pqpq has at most a+ba + b edges and weight L(a)[i][t]+L(b)[t][j]L^{(a)}[i][t] + L^{(b)}[t][j], so this sum is at least L(a+b)[i][j]L^{(a + b)}[i][j]. Terms with an infinite factor are ∞\infty and do not matter.

(≤\le) If L(a+b)[i][j]=∞L^{(a + b)}[i][j] = \infty there is nothing to show. Otherwise take a walk rr from ii to jj with ℓ≤a+b\ell \le a + b edges and weight L(a+b)[i][j]L^{(a + b)}[i][j]. Let tt be the vertex of rr after min⁡(a,ℓ)\min(a, \ell) edges. The part of rr before tt has at most aa edges and the part after has ℓ−min⁡(a,ℓ)≤b\ell - \min(a, \ell) \le b edges, so L(a)[i][t]+L(b)[t][j]≤w(r)L^{(a)}[i][t] + L^{(b)}[t][j] \le w(r).

Step 3: the copy in line L5. In a pass, with L=L(k)L = L^{(k)}, let P=L⋆LP = L \star L be the true product and MM the computed matrix, M[i][j]=min⁡(L[i][j],P[i][j])M[i][j] = \min(L[i][j], P[i][j]). Since L[i][i]≤0L[i][i] \le 0, the term t=it = i gives P[i][j]≤L[i][i]+L[i][j]≤L[i][j]P[i][j] \le L[i][i] + L[i][j] \le L[i][j], so M=PM = P. By Step 2 and induction on the passes, L=L(k)L = L^{(k)} after each pass, with kk the current value. At the end, L=L(K)L = L^{(K)} for some K≥nK \ge n (for n≤1n \le 1, K=1≥nK = 1 \ge n).

Step 4: no negative cycle. Suppose GG has no cycle of negative weight. Any walk from ii to jj 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 n−1n - 1 edges. So the least walk weight over walks of any length is attained by a simple path, it equals δ(i,j)\delta(i, j), and L(K)[i][j]=δ(i,j)L^{(K)}[i][j] = \delta(i, j) since K≥n−1K \ge n - 1. In particular L(K)[i][i]=δ(i,i)=0L^{(K)}[i][i] = \delta(i, i) = 0, 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 ∞\infty.

Step 5: a negative cycle. Suppose GG 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 CC has w(C)<0w(C) < 0. It has at most nn edges. For a vertex ii on CC, CC is a walk from ii to ii with at most n≤Kn \le K edges, so L(K)[i][i]≤w(C)<0L^{(K)}[i][i] \le w(C) < 0, and line L9 returns NEGATIVE-CYCLE.

Step 6: no false report. If line L9 fires, L(K)[i][i]<0L^{(K)}[i][i] < 0 is the weight of a closed walk from ii, and by the splitting in Step 5 one of its simple cycles is negative. So a report happens only when GG has a negative cycle.

Steps 4 to 6 give the claim. ■\blacksquare

Back to All-pairs shortest paths by repeated squaring