Proofs: Rake-compress tree
These are the human proofs of Rake-compress tree, at full length.
The RC-tree represents the forest, and its queries are correct full
This proof shows: correctness.
Let be the forest with positive or arbitrary integer weights, and fix the coins. For a cluster , let be its set of edges and the set of the ends of those edges, together with the vertex if . Write for the weight of the tree path from to .
Lemma 1 (no conflicts). In one round, no two adjacent live vertices are both removed, and no vertex is both chosen and the target of a rake.
Proof. A finalized vertex has no live neighbor. If a leaf rakes into and is also removed, then is not compressed, since a compressing vertex has no leaf neighbor, and is not finalized, so rakes, which means is a leaf whose only neighbor is . Then both are leaves and only the smaller one rakes, a contradiction. Two adjacent compressing vertices would both have heads while each needs the other to have tails.
So each round is a set of independent local changes, and their order inside the round does not matter.
Invariant (I). After every round, with the live vertices and the top clusters (clusters with no parent yet):
- the sets for partition ;
- each binary is a live edge between its two boundary vertices , is the edge set of a subtree of that contains the path from to , and its stored weight is ;
- each unary hangs on one live vertex , its boundary, and is the edge set of a subtree of that contains ;
- two distinct clusters of share no vertex except a common boundary vertex, and every vertex of is removed;
- two live vertices are in one tree of the live forest if and only if they are in one tree of .
Proof. Before round 1, the top clusters are the base clusters, all vertices are live, and the live forest is ; all items hold. Assume (I) before a round. By Lemma 1 we may apply the removals one at a time.
A finalize of joins the unary clusters hanging on , and no binary cluster since has no live edge, into with no boundary. By item 4 these clusters meet only at , so is connected and is a subtree; nothing else changes.
A rake of into joins the live edge and the unary clusters on into with boundary . Each part is a subtree containing (items 2 and 3), so the union is a subtree containing . The vertex leaves ; any other vertex of was already interior to a part, hence removed. Removing a leaf does not change connectivity among the other live vertices.
A compress of with live edges and joins them and the unary clusters on into with boundary . The union is a subtree that contains the path from to and the path from to . These two paths share only , because and share only (item 4), so their union is the path from to , and , the stored sum. The live forest replaces the path by the edge , which keeps connectivity among the live vertices.
In all three cases the new cluster takes the place of its parts in , so item 1 holds, and it shares with other top clusters only its boundary vertices, which gives item 4.
Termination. In each round with a live vertex, the live forest has a vertex of degree at most one. If is isolated it is finalized; if it is a leaf with neighbor , then rakes, or is a leaf with and rakes. So some vertex is removed in every round, so the number of live vertices strictly drops, and the loop ends. When it ends, is empty, every top cluster has no boundary, and every vertex owns the cluster made when it was removed.
Lemma 2 (connectivity). and are in one tree of if and only if and have the same root.
Proof. By item 5 and induction over the rounds, the vertices of one tree of stay in one live tree until the last of them is finalized, and the clusters on them are joined into the cluster of that last vertex; clusters of different trees never meet, because a new cluster only joins clusters at a common live vertex or a live edge, which lie in one tree. The cluster of a finalized vertex has no parent. So each tree of gives exactly one root, and lies below the root of the tree of because and clusters only grow upward.
Lemma 3 (containment). Let be a vertex and a cluster on the path from to its root. Then , and for every the tree path from to uses only edges of .
Proof. is the edge set of a subtree that contains , and the boundary vertices (items 2 and 3 at the time is made, and ). In a tree, the path between two vertices of a subtree stays in the subtree.
Lemma 4 (the climb). In the walk up from , after reaching , the value stored for each is .
Proof. At : , and each boundary vertex of is the other end of a binary child of with boundary , since is the set of boundary vertices of its children other than ; by item 2, is the weight of .
Step from a child to its parent , assuming the claim for . The vertex is a boundary vertex of , so is known. Let . If , its value is already . Otherwise is the other end of a binary child of with boundary . By Lemma 3 the path from to lies in . It starts in and ends at a vertex not in , since by item 4 the only vertices shares with are common boundary vertices, and . The children of meet only at , so the path leaves through and then follows the path from to , which lies in . Hence , which is the value set.
Lemma 5 (meeting point). Let be connected, and let be the first cluster on the walk up from that is also on the walk up from . Then .
Proof. By Lemma 2 such a exists. If or the claim is trivial. Otherwise lies in a child of and in a child of , with by the choice of . By Lemma 3 the path from to lies in . All children of have in their boundary, and by item 4 at the time was made, two of them share only the vertex . Inside the subtree , which is the union of the children's subtrees, the path from a vertex of to a vertex of must therefore pass through .
Theorem. After any sequence of links and cuts, connected is true exactly when and are in one tree, and path weight returns for connected and .
Proof. Each update rebuilds the contraction for the current forest, so (I) and the lemmas apply to it. Connectivity is Lemma 2. For path weight, the walk from stops at the cluster of Lemma 5, and by Lemma 4 applied to both walks the returned value is ; for the walk stops at and returns .