An edge-weight matrix \(W\) represents a graph \(G\) in which the weight of edge \(i, j\) is \(W[i,j]\). For all \(j\), \(W[j, j] = 0\). The graph has no cycles of negative length. Let \(sp(W)\) be a matrix of the same dimensions as \(W\) and whose \((i, j)\)-th element is the length of the shortest path from vertex \(i\) to vertex \(j\).
Prove that the following loop terminates and that at termination \(D = sp(W)\).
D = W for all i, j, k: D[i,j] + D[j, k] < D[i,k] -> D[i,k] = D[i,j] + D[j, k]
\(D \leq W\)
and
for all \(i, j\): \(D[i,j]\) is the length of a path from
\(i\) to \(j\).
Proof
The predicate holds initially because \(D = W\). The proof that the execution of every command maintains the predicate is straightforward.For each \(i, j\), there exist only a bounded number of values for \(D[i,j]\) that satisfy the invariant. This is because there are only a bounded number of cycle-free paths from \(i\) to \(j\). \(D\) is bounded below by the matrix of shortest-path lengths. Therefore \(D\) can decrease only a finite number of times before the lower bound is reached.
The execution of any command with a True guard reduces \(D\). The proof is straightforward.
We prove the result by induction on \(m\), the number of hops in shortest paths between vertices. The result holds for shortest paths consisting of a single hop, i.e. if the shortest path from \(i\) to \(j\) is along edge \(i, j\) then:
\( D[i, j] = sp(W)[i, j] \).
Assume that the result holds for all shortest paths with \(m\) hops and prove that it holds for shortest paths with \(m+1\) hops. Let the shortest path from \(i\) to \(k\) take \(m+1\) hops. Let \(j\) be the prefinal vertex on this shortest path. Then
\( sp(W)[i,k] = sp(W)[i,j] + sp(W)[j, k] \) The result follows from the induction assumption.
K. Mani Chandy, Emeritus Simon Ramo Professor, California Institute of Technology