Proofs: Gabow-Tarjan linear-time static union-find
These are the human proofs of Gabow-Tarjan linear-time static union-find, at full length.
The Gabow-Tarjan structure returns the nearest unlinked ancestor full
This proof shows: correctness, termination.
Write for the nearest ancestor of in , included, that is not linked. The root of is never linked, so exists. Links are never undone, so the set of linked nodes only grows.
Lemma 1 (cutting). After INIT: (a) each node is in exactly one microset; (b) each microset has at most nodes; (c) for each , or , and if then contains the root of ; (d) if then has a smaller number than ; (e) the number of microsets is at most .
Proof. Call the list held by a node after its step its open list. By induction from the leaves up: the open list of starts with , is a connected set of nodes whose top is , lists each node after its parent, and has fewer than nodes, where . Indeed, concatenates the open lists of some of its children into , each of size at most , closing when , so the part left has fewer than nodes, and is put in front. Each closed group is a concatenation of open lists of children of , each a connected set with top a child of and in parent-first order, which gives (c) with and (d). Its size is at least , and at most , because had fewer than nodes before the last list was added; this gives (b). The root closes its own open list with root NONE, a connected set with top the root of , of size at most . Each node is put in exactly one open list, and moves with it until that list is closed, which gives (a). All closed groups but the last have at least nodes, which gives (e).
A piece of is a maximal part of connected by parent edges inside . By (c), the top of a piece has , or is the root of . INIT computes in the order of the numbers, so by (d) the value for is ready, and is the set of nodes on the path from up to the top of its piece.
Lemma 2 (micro find). is always the set of linked nodes of , by line L5. MICRO-FIND returns the deepest unlinked node on the path from to the top of its piece, or NONE if all of them are linked. If it returns a node, that node is .
Proof. is the set of unlinked nodes on that path. The nodes of the path have numbers that grow with depth, by (d), so the largest number in is the deepest of them, the first one met when walking up from . All nodes below it on the path are linked, so it is .
Call exhausted when MICRO-FIND is NONE. This is permanent, since marks only grow.
Lemma 3 (leaving a microset). If is exhausted, then and .
Proof. All nodes from to the top of its piece are linked. The root of is never linked, so is not the root, and by (c). The walk from to passes through and continues at , so .
Invariant (N) of the macro forest. For each macro set with name : , and every with is exhausted and has .
Consequence. Let . Follow while the current node is not . By (N) each step stays in , and each step goes to a proper ancestor, so the walk is finite and must stop, which happens only at . By Lemma 3, which holds at every moment, each step keeps the answer. So, at every moment, is an ancestor of every member of (or equal to it), and for every .
At INIT every set is with name , so (N) holds. A link changes no macro set, and exhaustion is permanent, so (N) stays true. Path compression and union by size change neither the sets nor the names.
Line L12. Here is the name of its set , and is exhausted. Let , which is not NONE by Lemma 3, so ; it is a proper ancestor of . Let be its set, with name , which is an ancestor of by the consequence. If , then would be an ancestor of , a proper ancestor of , which is impossible. So and the union merges two sets, with the name . For the merged set: the members of other than satisfy (N) as before; the members of other than are exhausted and have their in , which is in the merged set; and is exhausted with in the merged set. So (N) holds.
FIND is correct. If line L9 returns, the answer is right by Lemma 2. Otherwise is exhausted and by Lemma 3. Line L10 sets to the name of the set of , so by the consequence of (N). The loop keeps , because line L12 keeps (N) and line L13 takes the new name. When the loop stops, $w = $ MICRO-FIND, and by Lemma 2.
Termination. Each pass through lines L12 and L13 merges two different macro sets, so the loop runs at most times over the whole run. MICRO-FIND and LINK take steps, and the macro finds terminate because the macro forest is a forest. INIT walks each node a constant number of times.