Union-Find & the Inverse Ackermann Function
Path compression, union by rank and Tarjan’s analysis.
Two changes of a few lines each, linking by rank and compressing paths, make union-find so fast that operations cost , where for any that could ever be stored. Tarjan proved the bound in 1975 and showed in 1979 that it is tight for a broad class of pointer algorithms; in 1989 Fredman and Saks proved that no data structure whatsoever can beat it.
What you’ll learn
- Prove the rank lemmas of union by rank and the height bound, and compare the four combinations of linking and compression.
- Define Ackermann's function and its inverse in the CLRS convention, and compute their first values.
- Prove an bound for union by rank with path compression, and sketch Tarjan's analysis with its potential function.
- State the matching lower bounds with their models of computation, and know the variants: path halving and splitting, union by size, randomized linking, and the linear-time special case.
Four ways to run union-find
| Linking | Find | Worst-case total for operations |
|---|---|---|
| naive | plain | : a path of length can form, and every find may walk it |
| by rank or size | plain | : heights stay |
| naive | path compression | for finds, so when |
| by rank or size | compression, halving or splitting |
(1) A link puts root under root only if , and raises 's rank on a tie, so the inequality is strict afterwards. Ranks change only at roots, and compression moves under the root, an ancestor of its old parent, whose rank is larger still.
(2) By induction over links. A root's rank grows from to only when it absorbs another root of rank , and both trees have at least nodes; compression moves nodes within a tree and never changes a root's tree.
(3) When a node reaches rank , charge it to the or more nodes of its tree at that moment. No node is charged by two different rank- nodes and : sets only merge, so at the later of the two moments, say 's, the root has in its tree as a non-root of rank at least , and (1) would force . So the rank- nodes own disjoint groups of size at least :
Ackermann’s function and its inverse
Level 1 adds one, times:
Level 2 applies to , times; by induction :
At :
Level 4 explodes: , and already has more than 600 digits:
So reaches 4 only at and stays there far beyond the roughly atoms of the observable universe:
Using the definition, compute .
Show the answer
63
: apply four times to 3, giving . The closed form agrees: .
For comparison, the iterated logarithm counts how many times must be applied before the value drops to at most 1: , , , and . It is the inverse of a tower of 2s, which is roughly level 3 of the hierarchy; inverts the whole diagonal and grows far more slowly still.
Conventions vary. Tarjan's theorems use a two-argument , which shrinks as the ratio grows, and other texts start Ackermann's function from other base cases. All the variants grow at the same absurdly slow rate, and each theorem holds with its own constants.
From log* n to α(n): the analysis
Blocks of ranks. Let and , so Block 0 holds ranks 0 and 1, and block holds the ranks in . Ranks are at most , so only about blocks are ever used.
Charging. A find walks pointers . Charge the step to the find if is the root or lies in a different block from : ranks increase along the path, so that happens at most times per find. Charge every other step to the node .
Each node pays at most times. A node is charged only as a non-root, so its rank, in block , is fixed. Each charge is followed by compression, which gives a parent of strictly larger rank. After at most charges the parent's rank has left block , and from then on every step out of is charged to a find.
Each block pays at most . By the rank lemma, the nodes whose rank lies in block number at most
Total. At most blocks pay each, the finds pay , and each union is two finds plus :
In this proof, why is a node whose rank lies in block charged at most about times?
- Each charge compresses 's path, which raises 's own rank by at least 1
- Each charge is followed by giving a parent of strictly larger rank, and once that rank leaves block the step from is charged to the find
- Each find charges at most one node per block
- Block contains at most nodes
Show the answer
B. Each charge is followed by giving a parent of strictly larger rank, and once that rank leaves block the step from is charged to the find
Ranks of non-roots never change, but the rank of 's parent rises with every compression that passes through . Only values fit in the block, and steps that leave a block are paid by the find.
Bounded potentials. The ranges of level and iter give , and every node starts with potential 0, so the total potential is never negative and the potential method applies.
Links. Linking root under root costs . Only 's rank can rise, by 1, which adds at most to ; the potentials of and of 's other children can only fall, because the ranks of their parents only grow.
Finds. Let a find visit nodes, and let (of rank at least 1) be followed higher on the path by some non-root with . After compression, 's parent is the root, and ranks increase along the path, so
So grows by at least 1, or grows, and either way drops by at least 1. The nodes without such a are the root, the first node if its rank is 0, and the last node of each level, at most in all. Scaling the potential so that one unit pays for one pointer step:
Lower bounds, variants and the frontier
| Variant | What changes | Bound |
|---|---|---|
| Union by size | link by the number of nodes instead of the rank | same |
| Path halving | while walking up, point every other node at its grandparent: one pass, no stack | same (Tarjan & van Leeuwen, 1984) |
| Path splitting | point every node on the path at its grandparent | same (Tarjan & van Leeuwen, 1984) |
| Randomized linking | link by random priorities instead of ranks | same, in expectation (Goel, Khanna, Larkin & Tarjan, 2014) |
| Unions known in advance | the union tree is given beforehand, as in offline LCA | on the word RAM (Gabow & Tarjan, 1985) |
| Rollback | undo the last union; no compression allowed, union by rank or size only | per operation, worst case |
A single operation can still cost with both heuristics. Blum (1986) showed how to bound every operation by in the worst case. And the inverse Ackermann function is not peculiar to union-find: it reappears in the fastest deterministic MST algorithm (next lesson) and in computational geometry, where the lower envelope of line segments in the plane can have pieces (Davenport–Schinzel sequences).
Key takeaways
- Union by rank: ranks strictly increase toward the root, a root of rank has at least nodes, and at most nodes have rank , so ranks and heights are at most .
- , , , and , so for every practical .
- With both heuristics, blocks of ranks under a tower of 2s give , and Tarjan's level-and-iteration potential gives ; a single find can still cost .
- is tight: Tarjan (1979) for separating pointer machines, Fredman and Saks (1989) for any cell-probe structure. Union by size, path halving or splitting, and randomized linking match it, and unions known in advance allow linear time.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
In the CLRS convention, what is ?
- 3
- 4
- 5
- 20
Show the answer
B. 4
, so the least with is 4.
Path compression changes parent pointers but never touches ranks. What does that do to the analysis?
- It breaks union by rank, so ranks must be recomputed as heights after every find
- Nothing harmful: ranks become upper bounds on heights, and the rank lemmas, which are all the analysis uses, still hold
- Ranks become lower bounds on heights, so the height bound fails
- Compression must then be limited to paths of length at most
Show the answer
B. Nothing harmful: ranks become upper bounds on heights, and the rank lemmas, which are all the analysis uses, still hold
Compression only shortens paths, so a root's rank still bounds its tree's height from above. The rank lemmas, strictly increasing ranks along paths and at most nodes of rank , are unaffected, and both the and the analyses are built on them.
With union by rank and path compression on elements, how many pointers can a single find follow in the worst case?
- At most
- At most 2, after the first compression
- Up to
- Up to
Show the answer
C. Up to
The bound is amortized over the whole sequence, not per operation. A single find can still walk a path as long as the rank bound allows: pairing elements, then pairs of pairs, with no finds in between, builds a tree of height 19, and the first find from its deepest leaf follows 19 pointers.
Fredman and Saks (1989) proved an amortized lower bound per operation for union-find in the cell-probe model. What does it rule out?
- Any data structure on a word RAM with -bit words that handles arbitrary sequences of unions and finds in amortized time per operation
- Only pointer-based forests; array tricks or bit parallelism could still beat
- Any structure with worst-case time per operation
- Linear total time even when the shape of the unions is known in advance
Show the answer
A. Any data structure on a word RAM with -bit words that handles arbitrary sequences of unions and finds in amortized time per operation
The cell-probe model charges only for memory accesses and gives all computation away, so its lower bound holds for every word-RAM program, whatever tricks it uses. Pointer machines are the setting of Tarjan's 1979 bound.
Further reading
- R. E. Tarjan, "Efficiency of a good but not linear set union algorithm," Journal of the ACM 22(2) (1975), 215–225.
- R. E. Tarjan, "A class of algorithms which require nonlinear time to maintain disjoint sets," Journal of Computer and System Sciences 18(2) (1979), 110–127.
- R. E. Tarjan & J. van Leeuwen, "Worst-case analysis of set union algorithms," Journal of the ACM 31(2) (1984), 245–281.
- M. L. Fredman & M. E. Saks, "The cell probe complexity of dynamic data structures," Proceedings of the 21st ACM Symposium on Theory of Computing (1989), 345–354.
- T. H. Cormen, C. E. Leiserson, R. L. Rivest & C. Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 19.