Algorithm Search⌘K

Proofs: Generic shortest path algorithm

These are the human proofs of Generic shortest path algorithm, at full length.

The generic relaxation method is correct for every order full

This proof shows: correctness, termination.

Claim. Let GG be a directed graph with real weights and a source ss, and fix any rule for line L4. GENERIC-SSSP ends. It returns NEGATIVE-CYCLE if and only if a cycle of negative weight is reachable from ss. Otherwise it returns d[v]=δ(s,v)d[v] = \delta(s, v) for every vv, with +∞+\infty for the vertices that ss does not reach, and d[v]=d[π[v]]+w(π[v],v)d[v] = d[\pi[v]] + w(\pi[v], v) for every v≠sv \ne s with d[v]<∞d[v] < \infty.

Step 1: labels are walk weights. Assign to each finite label a walk: Ws=(s)W_s = (s) for d[s]=0d[s] = 0, and when line L5 sets d[v]=d[u]+w(u,v)d[v] = d[u] + w(u, v), let WvW_v be the current WuW_u followed by the edge (u,v)(u, v). By induction on the steps, d[v]=w(Wv)d[v] = w(W_v) and h[v]h[v] is the number of edges of WvW_v at all times. Hence, if no negative cycle is reachable from ss, d[v]≥δ(s,v)d[v] \ge \delta(s, v) at all times.

Step 2: a repeated vertex gives a negative cycle. Every prefix of WvW_v ending at a vertex xx is WxW_x as it was at an earlier setting of d[x]d[x], and these settings occur in increasing time along WvW_v. If xx occurs twice, at settings t1<t2t_1 < t_2, then Wx(t2)=Wx(t1) CW_x(t_2) = W_x(t_1)\, C for a closed walk CC, and dt2[x]=dt1[x]+w(C)d_{t_2}[x] = d_{t_1}[x] + w(C). Each setting strictly lowers the label (line L3 requires a tense edge), so w(C)<0w(C) < 0. A closed walk of negative weight contains a simple cycle of negative weight, and it is reachable from ss along WvW_v.

Step 3: the report is sound. Line L6 fires only when WvW_v has at least nn edges, hence at least n+1n + 1 vertex occurrences, and so repeats a vertex. By Step 2, a negative cycle is reachable from ss.

Step 4: termination. As long as line L6 has not fired, every WvW_v has at most n−1n - 1 edges, so every label is the weight of one of the finitely many walks from ss with at most n−1n - 1 edges. Each step strictly lowers one label, so the number of steps is finite. Each step either fires line L6 or loops back to line L3, which ends the loop when no tense edge is left. So the algorithm ends.

Step 5: no negative cycle gives the distances. Suppose no negative cycle is reachable from ss. By Step 3, line L6 never fires, so by Step 4 the loop ends with no tense edge: d[v]≤d[u]+w(u,v)d[v] \le d[u] + w(u, v) for every edge with d[u]<∞d[u] < \infty. The label d[s]d[s] is the weight of a closed walk from ss, at least δ(s,s)=0\delta(s, s) = 0 by Step 1, and it starts at 0, so d[s]=0d[s] = 0. For a reachable vv with a shortest path s=p0,…,pr=vs = p_0, \ldots, p_r = v, induction along the path gives d[pi]<∞d[p_i] < \infty and d[pi+1]≤d[pi]+w(pi,pi+1)d[p_{i+1}] \le d[p_i] + w(p_i, p_{i+1}), and adding up, d[v]≤w(P)=δ(s,v)d[v] \le w(P) = \delta(s, v). With Step 1, d[v]=δ(s,v)d[v] = \delta(s, v). An unreachable vv has no walk from ss, so its label stays ∞\infty. For the predecessor of v≠sv \ne s: when d[v]d[v] was last set, d[v]=dthen[π[v]]+w(π[v],v)≥d[π[v]]+w(π[v],v)d[v] = d_{\text{then}}[\pi[v]] + w(\pi[v], v) \ge d[\pi[v]] + w(\pi[v], v) since labels only fall, and the edge is not tense at the end, so equality holds.

Step 6: a reachable negative cycle is reported. Suppose a negative cycle C=(c0,…,ck−1,c0)C = (c_0, \ldots, c_{k-1}, c_0) is reachable from ss and the algorithm returns labels. By Step 4 the loop ended with no tense edge. As in Step 5, every vertex on a path from ss has a finite label, so d[ci+1]≤d[ci]+w(ci,ci+1)d[c_{i+1}] \le d[c_i] + w(c_i, c_{i+1}) for each ii (indices modulo kk); adding these, the labels cancel and 0≤w(C)<00 \le w(C) < 0, a contradiction. So the algorithm returns NEGATIVE-CYCLE.

Steps 3 to 6 give the claim. ■\blacksquare

Back to Generic shortest path algorithm