Definition · Disjoint-set forestA partition of {1, …, n} represented as a forest: each element has a parent pointer, each set is a tree, and the root of a tree is its representative. find(x) follows parent pointers from x to the root. union(x, y) finds both roots and, if they differ, makes one the parent of the other.
The cost of find is the number of pointers walked, which is the depth of x. Everything about the performance of this structure is therefore a statement about how deep the trees get.
The naive version links the root of the first argument under the root of the second. Run union(0,1), union(1,2), …, union(10,11) that way and each union hangs the growing tree under a fresh singleton: the result is a path of twelve nodes. The twelve finds afterwards walk 11 + 10 + … + 0 = 66 pointers, and the deepest find walks 11.
Definition · Union by rankEach root carries a rank, initially 0. On a union, the root of smaller rank is made a child of the root of larger rank. If the two ranks are equal, either becomes the child and the new root’s rank increases by 1.
Rank is an upper bound on height and equals it when path compression is not used. The rule is two comparisons and costs nothing, and the next theorem is what it buys.
Theorem · A tree of rank r has at least 2ʳ elementsUnder union by rank, any root of rank r has at least 2ʳ elements in its tree. Consequently no rank exceeds log₂ n, and since rank bounds height, every find walks at most log₂ n pointers.
Induction on r. A rank-0 root is a singleton, and 2⁰ = 1. A root reaches rank r only by merging two roots of rank r − 1, each of which has at least 2ʳ⁻¹ elements by the inductive hypothesis; the merged tree has at least 2ʳ⁻¹ + 2ʳ⁻¹ = 2ʳ. Attaching a lower-rank tree does not change the root’s rank, so it cannot break the claim — it only adds elements. With n elements in total, 2ʳ ≤ n forces r ≤ log₂ n.
Every step of that induction is about the rule. It is the “only by merging two roots of equal rank” clause that supplies the doubling, and the naive union has no such clause. So the bound is not a fact about disjoint-set forests; it is a fact about disjoint-set forests built with union by rank, and the lab makes the difference concrete.
n = 12, union(i, i+1) for i = 0 … 10, then find on all 12
rules hops worst find tallest 2ʳ bound holds?
neither 66 11 11 2048 no
union by rank 20 1 1 2 yes
path compression only 21 11 11 2048 no
both 20 1 1 2 yes
the 2048 rows are the point: a tree of depth 11 would need 2048
elements to satisfy the bound, and this set holds 12
Read the first row as a refutation rather than a slow result. A depth of 11 on twelve elements is not a worse constant; it is the 2ʳ claim failing outright, because there is no rank rule in force to make it true. The lab prints the bound and the set size side by side with the verdict, and the verdict on that row is “no”.
Path compression
Definition · Path compressionDuring find(x), after the root is located, set the parent of every node on the path from x directly to the root.
The walk has already visited those nodes, so the rewriting is free up to a constant. Every subsequent find on any of them costs one hop. It changes no set and no root; it only shortens the paths it has just traversed.
On the chain sequence, compression alone brings the total from 66 hops to 21: the first find walks eleven pointers and flattens the entire path, so the remaining eleven finds cost one each. The worst single find is still 11, because the tall tree was built before any find happened — compression repairs, it does not prevent. And the 2ʳ bound still fails on that row, because it is a claim about union by rank and compression is not that.
Example · A sequence where the rules separate more finelyPair up, then pair the pairs: union(0,1), union(2,3), … then union(0,2), union(4,6), … and so on. On twelve elements the lab reports 39 hops with neither rule and a tallest tree of depth 4, whose 2ʳ bound of 16 exceeds the twelve elements present — failing again, on a pattern that was not even trying to be bad. With union by rank the tallest rank is 3, the bound is 8, the set holds 12, and it holds. Both rules together bring the hops to 30.
What is proved here and what is only stated
With both rules, a sequence of m operations on n elements costs O(m α(n)), where α is the inverse Ackermann function and is at most 4 for every n that will ever be written down. That bound is stated on this course and not proved: its proof needs machinery no Subject in this library teaches, and the course’s list of what it does not cover says so too.
What is proved here is the log n bound from union by rank alone, by the induction above, and it is the bound every later course quotes when it uses this structure — Kruskal’s algorithm sorts the edges and then does union and find, and the sort dominates a logarithmic union anyway. The near-constant bound is better and is not needed to make the structure worth using, which is the honest reason a course can state it without developing it.