Algorithm Search⌘K

Proofs: Top tree

These are the human proofs of Top tree, at full length.

Expose builds a top tree whose root holds the path from u to v full

This proof shows: correctness.

Let TT be the tree of uu and vv (if uu and vv are in different trees, expose returns nil, which is correct). Let X={u,v}X = \{u, v\} be the exposed set. For a cluster CC, let E(C)E(C) be its set of edges and V(C)V(C) the ends of those edges. During the loop, call a cluster top if it has no parent yet. Write π(a,b)\pi(a, b) for the tree path from aa to bb.

We assume the user's functions satisfy the following: the information of a base cluster describes its edge; compress(A,B)(A, B) describes the concatenation of the cluster paths of AA and BB and the union of their edges; rake(A,B)(A, B) describes the cluster path of BB and the union of the edges. For path sum, path max and total weight, these are the functions of the implementation. So it suffices to show that the clusters have the claimed boundaries and cluster paths.

Lemma 1 (no conflicts). In one round, no two adjacent vertices are both removed, and no removed vertex receives a rake.

Proof. Exposed vertices are never removed. If a leaf xx rakes into yy and yy is also removed, yy is not exposed. A compressing vertex has no unexposed leaf neighbor, so yy is not compressed; so yy rakes and is a leaf whose only neighbor is xx, and then only the smaller of the two rakes. Two adjacent vertices cannot both compress, since each needs heads for itself and tails for the other. The forced compress of line L4 happens only when no other vertex was chosen. □\square

Invariant (I). After every round, with LL the live vertices:

  1. the top clusters partition E(T)E(T), and each is the edge set of a subtree of TT;
  2. for each live edge {a,b}\{a, b\} of the live tree there is a top path cluster KabK_{ab} with boundary {a,b}\{a, b\}, and its cluster path is π(a,b)\pi(a, b);
  3. every other top cluster is a point cluster at a live vertex yy, the only vertex it shares with other top clusters; there is at most one per live vertex;
  4. every vertex of V(K)∖bound(K)V(K) \setminus \mathrm{bound}(K) for a top cluster KK is removed and not exposed, and it touches only edges of KK;
  5. the live tree is a tree on LL that contains XX.

Proof. At the start, the top clusters are the edges, L=V(T)L = V(T), and the live tree is TT. Assume (I) before a round; by Lemma 1, apply the removals one at a time.

Rake of a leaf xx into yy. The live edge is KxyK_{xy}, and xx may have a point cluster PP. By item 4 and since xx is a leaf of the live tree, every edge at xx lies in KxyK_{xy} or PP. If there is no PP, then by item 2, KxyK_{xy} is a subtree whose only vertex touching outside edges is yy (the vertex xx is not exposed and has no edge outside), so it is a point cluster at yy. If there is PP, the rake of PP onto KxyK_{xy} is a subtree with the same property. The new point cluster at yy is then raked onto the point cluster already at yy, if there is one; the result is a subtree, and its only vertex touching other top clusters is yy, because both parts had yy as their only shared vertex. The live tree loses the leaf xx.

Compress of xx with live edges to aa and bb. By item 4, every edge at xx lies in KaxK_{ax}, KxbK_{xb} or the point cluster PP at xx. Raking PP onto KaxK_{ax} gives a cluster with boundary {a,x}\{a, x\} and the same path π(a,x)\pi(a, x). Its edges and those of KxbK_{xb} share only the vertex xx, which is not exposed and has no other edges, so the compress gives a subtree with boundary {a,b}\{a, b\}; its cluster path is π(a,x)\pi(a, x) followed by π(x,b)\pi(x, b), and these two paths share only xx, so in a tree their concatenation is π(a,b)\pi(a, b). The live tree replaces the path a,x,ba, x, b by the edge abab.

In every case, the vertex removed is not exposed and becomes interior to a cluster that holds all its edges, which gives item 4; the clusters replaced are removed from the top level, which keeps item 1; item 5 holds because removing a leaf or contracting a vertex of degree two keeps a tree that still contains XX. □\square

Termination. If a live vertex outside XX exists, either some unexposed vertex is a leaf of the live tree, which rakes or whose leaf neighbor rakes, or every leaf is exposed. In the second case the live tree has at most two leaves, both in XX, so it is the path from uu to vv and every unexposed live vertex has degree two with no unexposed leaf neighbor; line L4 compresses at least one of them. So every round removes a vertex, and the loop ends with L=XL = X.

Lemma 2 (binary). Every cluster above the edges has exactly two children, and the root contains every edge of TT once.

Proof. Every merge in lines L5 to L7 is a rake or a compress of two clusters, and a leaf edge with no point cluster is reused, not merged. By item 1 the top clusters always partition E(T)E(T), and line L7 merges all remaining top clusters into one. □\square

Theorem. If u≠vu \neq v, the root returned by expose(u,v)(u, v) is a cluster with E=E(T)E = E(T), boundary {u,v}\{u, v\} and cluster path π(u,v)\pi(u, v). If u=vu = v, the root has E=E(T)E = E(T) and boundary {u}\{u\}. In both cases the root information is that of the path from uu to vv and of the whole tree.

Proof. When the loop ends, L={u,v}L = \{u, v\}. If u≠vu \neq v, the live tree is the single edge {u,v}\{u, v\}, so by item 2 the top path cluster KuvK_{uv} has path π(u,v)\pi(u, v), and by item 3 the other top clusters are point clusters at uu or vv. Line L7 rakes each of them onto the path cluster, which keeps the boundary {u,v}\{u, v\} (both vertices are exposed) and the path π(u,v)\pi(u, v). If u=vu = v, the live tree is the single vertex uu, there is no path cluster, and line L7 joins the point cluster at uu; if TT has no edges, the root is the empty cluster. By item 1 and Lemma 2 the root has all edges of TT. The information follows from the assumption on the user's functions, by induction over the merges. ■\blacksquare

Back to Top tree