Proofs: Disjoint-set forest
These are the human proofs of Disjoint-set forest, at full length.
The operations of a disjoint-set forest keep the forest and rank invariants and implement the disjoint-set type full
This proof shows: correctness, termination.
We write for the set of elements made so far, and for the set of nodes whose path of parent pointers ends at the root . The sets of the structure are the sets over all roots . We prove that the two invariants of the page hold after each operation:
(F) For each , the sequence reaches a root, an element with , after at most steps.
(R) For each non-root , . Each root has .
Under (F), the sets are pairwise disjoint and cover , because each element reaches exactly one root. So the structure represents a collection of disjoint sets, each with its root as representative.
Lemma 1 (FIND). Assume (F) and (R). Then FIND stops, returns the root of , and after it (F) and (R) hold, each set is the same as before, and no rank has changed.
Proof. Let be the path from to its root, with and . By (R), .
Termination and result. By induction on . If , then , line L3 fails, and line L5 returns . If , line L4 calls FIND, whose path is shorter. By the induction hypothesis it stops and returns , and its only changes are to set for . Line L4 sets , and line L5 returns .
Invariants. So the only changes of the whole call are for , made in the order . Take one such change. Before it, reaches ; after it, reaches in one step. Each other node whose path passes through reached through before, and still reaches through after, by a path that is not longer. No other path changes. So (F) holds and each keeps its members. For (R): , by the chain of ranks above, so after the change. No rank changes, so the root part of (R) is untouched, and the sets do not change, so their sizes do not change.
Lemma 2 (MAKE-SET). If (F) and (R) hold and , then after MAKE-SET they hold for , and the sets are the old sets and , with representative .
Proof. Lines L1 and L2 make a root of rank 0 with , and . No other field changes, so each other path and each other set is as before.
Lemma 3 (UNION). Assume (F) and (R), and let with sets and . After UNION, (F) and (R) hold. If , the sets are unchanged. Else the sets are the old sets with and replaced by , and the representative of each other set is unchanged.
Proof. By Lemma 1, line L6 sets and to the roots of and , and keeps the invariants and the sets. If , line L7 returns, and . Else are two roots, so . Write and .
Case (line L8). The only change is . Each node of now reaches and then , and each node of reaches as before, so and the other sets do not change. A path in is at most nodes long, so (F) holds. For (R), the new non-root has . The root has , and its rank is still .
Case (lines L9 and L10). The change makes , and (F) holds as in the first case. If , the new non-root has , and . If , line L10 sets . Then , so the edge from is fine. Each other child of had , which is still less than . And , by (R) for the two old roots.
In all cases only or stopped being a root, and the representative of each set other than and is its old root.
Theorem. Starting from the empty structure, after any sequence of MAKE-SET, FIND and UNION operations with valid arguments, (F) and (R) hold, each operation stops, and the sets with representatives are exactly the sets of the disjoint-set type after the same sequence. In particular, FIND$(x) = $ FIND if and only if and are in the same set, which is law (D3).
Proof. The empty structure has no elements, so (F) and (R) hold. By induction on the number of operations, using Lemma 2 for MAKE-SET, Lemma 1 for FIND and Lemma 3 for UNION, the invariants hold after each operation, and each operation changes the represented sets exactly as the type says. FIND returns the root of the tree of , which is the representative of its set. Two elements have the same root exactly when they are in the same tree, by (F).
Corollary (height). Under (R), each tree has height at most .
Proof. On a path to a root , the ranks strictly increase from , so . By (R), , so , and is an integer.