Algorithm Search⌘K

Proofs: D'Esopo-Pape algorithm

These are the human proofs of D'Esopo-Pape algorithm, at full length.

The D'Esopo-Pape algorithm with edge counts is correct and ends full

This proof shows: correctness, termination.

Claim. Let GG be a directed graph with real weights and a source ss. DESOPO-PAPE ends on every input. 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 vertices not reachable from ss), and for each v≠sv \ne s with d[v]<∞d[v] < \infty, d[v]=d[π[v]]+w(π[v],v)d[v] = d[\pi[v]] + w(\pi[v], v).

None of the steps uses the position at which a vertex enters the deque; they hold for any rule that puts a vertex into the deque when its label drops and it is not already there.

Step 1: labels are walk weights. Number the label changes in time order. To each finite label assign a walk: Ws=(s)W_s = (s) for the initial d[s]=0d[s] = 0, and when line L7 sets d[v]=d[u]+w(u,v)d[v] = d[u] + w(u, v), let WvW_v be the current walk of uu followed by the edge (u,v)(u, v). By induction, at all times d[v]=w(Wv)d[v] = w(W_v) and h[v]h[v] is the number of edges of WvW_v. In particular d[v]≥δ(s,v)d[v] \ge \delta(s, v) whenever no negative cycle is reachable from ss, since then every walk from ss to vv weighs at least δ(s,v)\delta(s, v).

Step 2: a repeated vertex means a negative cycle. Every prefix of WvW_v that ends at a vertex xx is the walk WxW_x as it was at some earlier label setting of xx, and the settings along WvW_v are in increasing time from the start to the end. Suppose xx occurs twice in WvW_v, and the two prefixes ending there correspond to settings at times t1<t2t_1 < t_2. Then Wx(t2)=Wx(t1) CW_x(t_2) = W_x(t_1)\, C for a closed walk CC from xx, and dt2[x]=dt1[x]+w(C)d_{t_2}[x] = d_{t_1}[x] + w(C). Each setting strictly lowers the label, so dt2[x]<dt1[x]d_{t_2}[x] < d_{t_1}[x], and w(C)<0w(C) < 0. A closed walk of negative weight splits into simple cycles, one of which is negative, and it is reachable from ss along WvW_v.

Step 3: soundness of the report. Line L7 returns NEGATIVE-CYCLE only when h[v]≥nh[v] \ge n, so WvW_v has at least n+1n + 1 vertex occurrences and repeats a vertex. By Step 2 a negative cycle is reachable from ss.

Step 4: termination. As long as no report happens, every WvW_v has at most n−1n - 1 edges, so every label value is the weight of one of the finitely many walks from ss with at most n−1n - 1 edges. Labels only decrease, strictly, so each vertex has finitely many label changes. Each entry into the deque, apart from the first entry of ss, follows a label change, and each pass of line L3 removes one entry. So the loop ends.

Step 5: an empty deque means no tense edge. Call (u,v)(u, v) tense if d[u]<∞d[u] < \infty and d[u]+w(u,v)<d[v]d[u] + w(u, v) < d[v]. Invariant: every tense edge (u,v)(u, v) has uu in the deque. At the start only ss has a finite label and ss is in the deque. Scanning uu in lines L5 to L9 relaxes each edge out of uu, after which none of them is tense, as long as d[u]d[u] is unchanged; if a self-loop lowers d[u]d[u] during the scan, uu is put back in the deque by line L9. An edge (x,y)(x, y) can become tense only when d[x]d[x] drops (a drop of d[y]d[y] cannot make it tense), and line L7 to L9 then puts xx into the deque unless it is already there. So the invariant holds, and when the loop ends with an empty deque, no edge is tense.

Step 6: no negative cycle gives the distances. Suppose no negative cycle is reachable from ss. By Step 3 there is no report, and by Step 4 the loop ends. d[s]=0d[s] = 0: the label of ss is a walk weight from ss to ss, at least δ(s,s)=0\delta(s, s) = 0 by Step 1, and it starts at 0. For vv reachable from ss, take a shortest path s=v0,…,vk=vs = v_0, \ldots, v_k = v. By Step 5, d[vi+1]≤d[vi]+w(vi,vi+1)d[v_{i+1}] \le d[v_i] + w(v_i, v_{i+1}) for each ii, once d[vi]<∞d[v_i] < \infty, which holds by induction from d[v0]=0d[v_0] = 0. Adding gives d[v]≤δ(s,v)d[v] \le \delta(s, v), and with Step 1, d[v]=δ(s,v)d[v] = \delta(s, v). A vertex that is not reachable never gets a finite label, since a finite label is the weight of a walk from ss. For the predecessors: 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), and the edge is not tense at the end, so equality holds.

Step 7: 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 suppose the algorithm returns distances. By Step 4 it ended with an empty deque, so by Step 5 no edge is tense. Every vertex on a path from ss then has a finite label, by induction along the path as in Step 6, so all cic_i have finite labels and 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 kk inequalities, the labels cancel and 0≤w(C)0 \le w(C), a contradiction. So the algorithm returns NEGATIVE-CYCLE.

Steps 3, 4, 6 and 7 give the claim. ■\blacksquare

Back to D'Esopo-Pape algorithm