Proofs: Hopcroft-Ullman bound
These are the human proofs of Hopcroft-Ullman bound, at full length.
Union by rank with path compression runs in O(m log* n) time full
This proof shows: statement.
We prove that a sequence of operations MAKE-SET, UNION and FIND, of which are MAKE-SET, on a disjoint-set forest with union by rank and path compression takes time. At the end we show that the same proof works for union by size.
Setup. A UNION is two FINDs and one LINK of two roots, so the sequence becomes at most operations MAKE-SET, FIND and LINK. MAKE-SET and LINK take time, so they cost in all. A FIND on the path , where and is the root, takes time. We count one unit for each node with of each FIND, plus one unit per FIND; this is the time of the FINDs up to a constant factor.
Rank facts. From the union by rank page:
- For each non-root , . So ranks increase strictly along each path.
- The rank of a node changes only while it is a root. Once a node is a non-root, it never becomes a root again, and its rank is fixed.
- Each rank is at most .
- For each , at most nodes ever have rank .
Blocks. Define and for , so , , , . Put rank in block if , and in block if for . Each rank is in exactly one block, and is non-decreasing in .
Number of blocks. Let . We show that each rank has . If then and . Otherwise, means that applications of take to a value at most 1, so , since applied times to gives , and is increasing. Then , so and . So all ranks lie in the blocks . Write for .
Charging. Consider one FIND on the path , before the compression, and a node with , so is not the root. Charge the unit of :
- to the FIND, if is the root (), or if ;
- to the node , otherwise, that is, if is not the root and .
By fact 1, always, so these cases cover all nodes.
Charges to a FIND. The units charged to one FIND are its own unit, at most one for , and one for each with . Along the path the blocks do not decrease and lie in , so they increase at most times. So one FIND has at most charges, and all FINDs together at most .
Charges to a node. Fix a node . Its rank does not change once it is a non-root (fact 2), and it is charged only as a non-root, so is fixed during all its charges. Let .
Each time is charged, in a FIND with , its parent is not the root, so the root is a proper ancestor of , and by fact 1. Path compression sets . So after each charge, is larger than before the charge. Between charges, does not fall: the parent of changes only by compression, to a proper ancestor of the old parent, which has a larger rank, and the old parent, if it stays, keeps or raises its rank.
So the ranks of the parent of at its successive charges strictly increase. At each charge, , so ; and . A strictly increasing sequence of integers in has at most terms. So is charged at most times. In block 0 this gives at most charge.
Charges to the nodes of one block. Let be the number of nodes with at the time they are charged; by fact 2 each node is counted in at most one block. For , , so block 0 takes at most charges. For , by fact 4,
So the nodes of block take at most charges. Over the blocks, the nodes take at most charges.
Total. Every unit of every FIND is charged once, to the FIND or to a node. So the FINDs cost at most units, and the whole sequence costs
because .
Union by size. With union by size, define for a non-root the number , where is the size of the tree of at the moment it was linked below another root, and for a root . We check facts 1 to 4 for in place of rank; the proof above uses nothing else.
Fact 2: is fixed once is a non-root, and a root's size only grows. Fact 3: . Fact 1: when is linked below the root , union by size gives , so the new tree of has at least nodes. Every later ancestor of has in its tree, so the size that has when it stops being a root, or its present size if it is a root, is at least the size of the tree of after this link, which is at least . So . Compression only gives new parents that are ancestors, so increases strictly along each path. Fact 4: let both be non-roots with . Their trees at the time of their links have at least nodes each. If one of these trees contained the other node, say in the tree of , then is an ancestor of and by fact 1, a contradiction. The trees of a union history are nested or disjoint, so the two trees are disjoint. So at most non-roots have . Roots are never charged, and the counting of is only over charged nodes, which are non-roots. So the proof holds for union by size, which is the form of Hopcroft and Ullman.