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 be the tree of and (if and are in different trees, expose returns nil, which is correct). Let be the exposed set. For a cluster , let be its set of edges and the ends of those edges. During the loop, call a cluster top if it has no parent yet. Write for the tree path from to .
We assume the user's functions satisfy the following: the information of a base cluster describes its edge; compress describes the concatenation of the cluster paths of and and the union of their edges; rake describes the cluster path of 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 rakes into and is also removed, is not exposed. A compressing vertex has no unexposed leaf neighbor, so is not compressed; so rakes and is a leaf whose only neighbor is , 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.
Invariant (I). After every round, with the live vertices:
- the top clusters partition , and each is the edge set of a subtree of ;
- for each live edge of the live tree there is a top path cluster with boundary , and its cluster path is ;
- every other top cluster is a point cluster at a live vertex , the only vertex it shares with other top clusters; there is at most one per live vertex;
- every vertex of for a top cluster is removed and not exposed, and it touches only edges of ;
- the live tree is a tree on that contains .
Proof. At the start, the top clusters are the edges, , and the live tree is . Assume (I) before a round; by Lemma 1, apply the removals one at a time.
Rake of a leaf into . The live edge is , and may have a point cluster . By item 4 and since is a leaf of the live tree, every edge at lies in or . If there is no , then by item 2, is a subtree whose only vertex touching outside edges is (the vertex is not exposed and has no edge outside), so it is a point cluster at . If there is , the rake of onto is a subtree with the same property. The new point cluster at is then raked onto the point cluster already at , if there is one; the result is a subtree, and its only vertex touching other top clusters is , because both parts had as their only shared vertex. The live tree loses the leaf .
Compress of with live edges to and . By item 4, every edge at lies in , or the point cluster at . Raking onto gives a cluster with boundary and the same path . Its edges and those of share only the vertex , which is not exposed and has no other edges, so the compress gives a subtree with boundary ; its cluster path is followed by , and these two paths share only , so in a tree their concatenation is . The live tree replaces the path by the edge .
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 .
Termination. If a live vertex outside 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 , so it is the path from to 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 .
Lemma 2 (binary). Every cluster above the edges has exactly two children, and the root contains every edge of 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 , and line L7 merges all remaining top clusters into one.
Theorem. If , the root returned by expose is a cluster with , boundary and cluster path . If , the root has and boundary . In both cases the root information is that of the path from to and of the whole tree.
Proof. When the loop ends, . If , the live tree is the single edge , so by item 2 the top path cluster has path , and by item 3 the other top clusters are point clusters at or . Line L7 rakes each of them onto the path cluster, which keeps the boundary (both vertices are exposed) and the path . If , the live tree is the single vertex , there is no path cluster, and line L7 joins the point cluster at ; if has no edges, the root is the empty cluster. By item 1 and Lemma 2 the root has all edges of . The information follows from the assumption on the user's functions, by induction over the merges.