Algorithm Search⌘K

Proofs: Tarjan's subtree disassembly

These are the human proofs of Tarjan's subtree disassembly, at full length.

Subtree disassembly returns the distances or a reachable negative cycle full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) have nn vertices, real weights ww and source ss. Line numbers refer to SUBTREE-DISASSEMBLY on the main page. A label change is one execution of line L9. A vertex is active if it is in the tree TT, and inactive if it has a finite label but is not in TT.

Invariant 1 (structure). At the test of line L2: (a) TT is a tree rooted at ss whose arcs are arcs of GG, given by π\pi; (b) every tree arc is tight, d(x)=d(π(x))+w(π(x),x)d(x) = d(\pi(x)) + w(\pi(x), x); (c) d(s)=0d(s) = 0 and ss is in TT; (d) every vertex in QQ is in TT.

Proof. At the start T={s}T = \{s\} and all four parts hold. Consider a label change of vv through the arc (u,v)(u, v) during the scan of uu. The scan runs only for u∈Tu \in T, and uu stays in TT during its own scan: it could leave TT only at line L8 as a proper descendant of some vv, but then line L6 would have returned. Line L6 has failed, so v≠uv \ne u and vv is not an ancestor of uu. Line L8 removes the proper descendants of vv from TT and from QQ; the rest of TT is still a tree, and all its arcs are unchanged and tight. Line L9 then lowers d(v)d(v) and makes vv a child of uu. The vertex uu is not in the subtree of vv, so no cycle is created and TT stays a tree. The only arcs with head or tail vv in TT are now (u,v)(u, v), which is tight by line L9, since vv has no children after line L8. So (a) and (b) hold. For (c): v=sv = s would need ss to be an ancestor-or-equal of uu, which is always the case, so line L6 returns before d(s)d(s) changes. Part (d) holds because line L8 removes from QQ every vertex it removes from TT, and line L10 adds vv, which is in TT. □\square

Fact 2 (labels are path weights). At all times, every finite label d(x)d(x) is the weight of a simple path from ss to xx.

Proof. For x∈Tx \in T, by Invariant 1 (b) and (c), summing the tight arcs along the tree path gives d(x)=w(tree path from s to x)d(x) = w(\text{tree path from } s \text{ to } x), which is a simple path. An inactive vertex keeps the label it had when it was last in TT, which was such a weight then. □\square

Lemma 3 (returned cycles). If line L7 returns, the returned cycle is a negative cycle reachable from ss.

Proof. If v=uv = u, the cycle is the self-loop (u,u)(u, u) with w(u,u)<d(u)−d(u)=0w(u, u) < d(u) - d(u) = 0. Otherwise vv is a proper ancestor of uu in TT, and the tree path PP from vv to uu has weight d(u)−d(v)d(u) - d(v) by tightness. With the arc (u,v)(u, v), it closes a cycle of distinct vertices (a tree path is simple) of weight d(u)−d(v)+w(u,v)<0d(u) - d(v) + w(u, v) < 0 by the test of line L5. Its vertices are in TT, so they are reachable from ss. □\square

Lemma 4 (termination). The algorithm terminates.

Proof. By Fact 2 every finite label is the weight of one of the finitely many simple paths from ss. Each label change makes a label strictly smaller, so each vertex has finitely many label changes, and line L10 adds finitely many entries to QQ. Each iteration of line L2 removes one entry. □\square

Invariant 5 (violated arcs). At the test of line L2, every arc (x,y)(x, y) with d(x)<∞d(x) < \infty and d(x)+w(x,y)<d(y)d(x) + w(x, y) < d(y) has x∈Qx \in Q or xx inactive.

Proof. At the start only d(s)d(s) is finite and s∈Qs \in Q. When xx is scanned, each arc out of xx is relaxed and becomes non-violated; a later label change in the same scan lowers only the label of some head v≠xv \ne x (the case v=xv = x returns), which cannot violate an arc out of xx. When d(v)d(v) goes down at line L9, the arcs out of vv may become violated, and line L10 puts vv in QQ. A vertex removed at line L8 leaves QQ but becomes inactive. Lowering d(y)d(y) never violates an arc into yy. □\square

Lemma 6 (no inactive vertex at the end). If the loop of line L2 ends because QQ is empty, no vertex is inactive.

Proof. For an inactive vertex yy, look at the last time τ(y)\tau(y) it was removed at line L8, during the label change of some vertex aa, and let p(y)p(y) be its parent just before that removal, so that p(y)p(y) is aa or a vertex removed at the same time, and the arc (p(y),y)(p(y), y) was tight with the labels just before time τ(y)\tau(y). Let h(y)h(y) be the depth of yy below aa at that moment. Since yy is inactive, d(y)d(y) has not changed since τ(y)\tau(y).

Suppose QQ is empty at the end and II, the set of inactive vertices, is not empty. Take y∈Iy \in I and p=p(y)p = p(y). Two cases.

  • d(p)d(p) is now smaller than just before τ(y)\tau(y). This holds if p=ap = a, whose label went down at time τ(y)\tau(y), or if p≠ap \ne a had a label change after τ(y)\tau(y). Then d(p)+w(p,y)<d(y)d(p) + w(p, y) < d(y), the arc is violated, and by Invariant 5 with QQ empty, p∈Ip \in I. In this case the last removal of pp came after time τ(y)\tau(y): for p=ap = a, aa was active right after τ(y)\tau(y); for p≠ap \ne a, pp was put back in TT by a label change after τ(y)\tau(y). So τ(p)>τ(y)\tau(p) > \tau(y).
  • d(p)d(p) is unchanged since just before τ(y)\tau(y). Then p≠ap \ne a, pp was removed at time τ(y)\tau(y) and has not been put back in TT, so p∈Ip \in I with τ(p)=τ(y)\tau(p) = \tau(y) and h(p)=h(y)−1h(p) = h(y) - 1.

In both cases p(y)∈Ip(y) \in I and the pair (τ(p),−h(p))(\tau(p), -h(p)) is lexicographically larger than (τ(y),−h(y))(\tau(y), -h(y)). Starting from any y∈Iy \in I and applying pp again and again gives an infinite sequence in the finite set II with strictly increasing pairs, which is impossible. □\square

Theorem. The algorithm terminates. If it returns a cycle, that cycle is a negative cycle reachable from ss. If it returns dd and π\pi, then no negative cycle is reachable from ss, d(v)=δ(s,v)d(v) = \delta(s, v) for all vv, with ∞\infty exactly for the unreachable vertices, and π\pi is a shortest-path tree. So it returns a cycle if and only if a negative cycle is reachable from ss.

Proof. Termination is Lemma 4, and the claim on returned cycles is Lemma 3. Suppose it returns at line L11, with QQ empty. By Lemma 6 there is no inactive vertex, so by Invariant 5 no arc with a finite tail is violated. As in the proof for SPFA, a reachable vertex with label ∞\infty would give a violated arc on a path from ss, so exactly the reachable vertices have finite labels. Summing the inequalities d(ci)≤d(ci−1)+w(ci−1,ci)d(c_i) \le d(c_{i-1}) + w(c_{i-1}, c_i) around any cycle through reachable vertices gives a nonnegative weight, so no negative cycle is reachable from ss. By Invariant 1 (c) d(s)=0d(s) = 0, by Fact 2 each finite label is the weight of a path from ss, and the triangle inequality holds on every arc with a reachable tail; Theorem B of the optimality conditions gives d=δ(s,⋅)d = \delta(s, \cdot). All reachable vertices are in TT, which is a tree of tight arcs by Invariant 1, so each tree path has weight d(v)=δ(s,v)d(v) = \delta(s, v), and TT is a shortest-path tree. Finally, if a negative cycle is reachable from ss, the run cannot return at line L11, so it returns a cycle. ■\blacksquare

Back to Tarjan's subtree disassembly