Algorithm Search⌘K

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 1,…,n1, \ldots, n and real weights. WW is built as on the main page: W[i][i]=min⁡(0,c)W[i][i] = \min(0, c) over the self-loops i→ii \to i of weight cc (0 if there are none), W[i][j]W[i][j] is the least weight of an arc i→ji \to j for i≠ji \ne j, and ∞\infty if there is none. For r≥1r \ge 1 let λr(i,j)\lambda_r(i, j) be the infimum of w(p)w(p) over walks pp from ii to jj with at most rr arcs, where the empty walk ⟨i⟩\langle i \rangle counts for i=ji = j. There are finitely many such walks, so λr(i,j)\lambda_r(i, j) is a real number or ∞\infty. Let L(r)L^{(r)} be the value of LL after the loop step for rr (and L(1)=WL^{(1)} = W).

Lemma 1. For every r≥1r \ge 1 and all i,ji, j, L(r)[i][j]=λr(i,j)L^{(r)}[i][j] = \lambda_r(i, j).

Proof. By induction on rr. For r=1r = 1: a walk with at most one arc from ii to j≠ij \ne i is one arc i→ji \to j, and the least weight is W[i][j]W[i][j]. For i=ji = j the walks are ⟨i⟩\langle i \rangle of weight 0 and the self-loops, and the least weight is W[i][i]W[i][i].

Step from r−1r - 1 to r≥2r \ge 2. Line L8 gives L(r)[i][j]=min⁡k(λr−1(i,k)+W[k][j])L^{(r)}[i][j] = \min_k \big(\lambda_{r-1}(i, k) + W[k][j]\big).

(≥\ge) Each term is the weight of a walk from ii to jj with at most rr arcs, or ∞\infty. If W[k][j]W[k][j] is finite and k≠jk \ne j, it is an arc k→jk \to j, and a best walk to kk with at most r−1r - 1 arcs followed by that arc has at most rr arcs. If k=jk = j, then W[j][j]W[j][j] is 0 (append nothing) or a self-loop (append it). So each term is at least λr(i,j)\lambda_r(i, j).

(≤\le) Let pp be a walk from ii to jj with at most rr arcs. If pp has no arc, then i=ji = j, and the term k=jk = j is at most λr−1(j,j)+W[j][j]≤0+0=w(p)\lambda_{r-1}(j, j) + W[j][j] \le 0 + 0 = w(p). Otherwise let k→jk \to j be its last arc and p′p' the rest, which has at most r−1r - 1 arcs. The term for kk is at most w(p′)+W[k][j]≤w(p′)+w(k,j)=w(p)w(p') + W[k][j] \le w(p') + w(k, j) = w(p); for k=jk = j this uses W[j][j]≤W[j][j] \le the weight of every self-loop at jj. So the minimum is at most λr(i,j)\lambda_r(i, j). □\square

Lemma 2. If the graph has no negative cycle, then λn−1(i,j)=λn(i,j)=δ(i,j)\lambda_{n-1}(i, j) = \lambda_n(i, j) = \delta(i, j) for all i,ji, j.

Proof. Every walk from ii to jj that repeats a vertex contains a cycle, and removing it gives a shorter walk from ii to jj of weight at most as large, since the cycle has weight at least 0. Repeating this gives a simple path, with at most n−1n - 1 arcs and weight at most w(p)w(p). So the infimum over all walks equals the infimum over walks with at most n−1n - 1 arcs, and also over walks with at most nn arcs, since these lie in between. For an unreachable pair both sides are ∞\infty. □\square

Lemma 3. The graph has a negative cycle if and only if λn(i,i)<0\lambda_n(i, i) < 0 for some ii.

Proof. (⇒\Rightarrow) A negative cycle CC has at most nn arcs, as its vertices are distinct. For a vertex ii on CC, the walk around CC from ii is a walk from ii to ii with at most nn arcs and negative weight, so λn(i,i)<0\lambda_n(i, i) < 0. (⇐\Leftarrow) If there is no negative cycle, Lemma 2 gives λn(i,i)=δ(i,i)=0\lambda_n(i, i) = \delta(i, i) = 0, because the empty walk has weight 0 and no closed walk is negative. □\square

Theorem. SLOW-APSP returns "negative cycle" if and only if the graph has a negative cycle. Otherwise it returns L[i][j]=δ(i,j)L[i][j] = \delta(i, j) for all i,ji, j, with ∞\infty exactly for the pairs where jj is not reachable from ii.

Proof. After the loop of lines L2 and L3, L=L(n)L = L^{(n)} (for n=1n = 1 the loop is empty and L=L(1)=WL = L^{(1)} = W). By Lemma 1, L=λnL = \lambda_n. Line L4 tests the condition of Lemma 3. If it does not return, there is no negative cycle, and Lemma 2 gives L=δL = \delta. A pair has a finite entry exactly when some walk joins it, which is reachability. □\square

Termination. All loops have fixed bounds: n−1n - 1 calls of EXTEND, each with n3n^3 steps, and nn tests in line L4. ■\blacksquare

Back to Slow all-pairs shortest paths by matrix multiplication