Algorithm Search⌘K

Proofs: Blum's disjoint-set structure

These are the human proofs of Blum's disjoint-set structure, at full length.

Blum's k-UF trees stay valid and answer finds correctly full

This proof shows: correctness, termination.

The leaves of a tree are its nodes of height 0, which are elements. We prove that after each operation: (S) the leaves of the tree of each root are exactly the elements of its set, and the root stores the name of an element of the set; (K) each tree satisfies the three rules of the page; and (H) the height field of each node equals the length of every path from it down to a leaf. At the start each element is a tree of one leaf of height 0, so all three hold.

FIND. The parent pointers form a forest, so the walk of line L1 ends at the root of the tree of xx, after at most the height of that tree. By (S), the name there is an element of the set of xx, and two elements get the same name exactly when they are in the same tree. FIND changes nothing.

UNION. Line L3 finds the roots. If they are equal, nothing changes. Otherwise let rr and ss be the two roots after line L4, so r.h≥s.hr.h \ge s.h. In every case below, the leaves of the new tree are the leaves of the two old trees, because nodes are only moved or added, never removed except an emptied root, which is not a leaf; and the root of the new tree stores the name of rr or of the new root tt, which is the name of rr, an element of the union. So (S) holds. We check (K) and (H).

Line L6, equal heights hh, and h=0h = 0 or both roots have at least kk children. The new root tt has height h+1h + 1 and the two children rr and ss, both of height hh, so (H) holds and the leaves are at depth h+1h + 1, which is rule 1. Rule 2 holds for tt. The nodes rr and ss are now inner non-root nodes if h≥1h \ge 1, and they have at least kk children, which is rule 3; if h=0h = 0 they are leaves and rule 3 does not apply. Other nodes are unchanged.

Line L8, equal heights h≥1h \ge 1, and not both roots have kk children. After line L7, ss has the fewer children, so fewer than kk. Its children have height h−1h - 1 by (H) and become children of rr, which has height hh; so (H) holds and all leaves stay at depth hh. The root rr had at least 2 children and keeps them, so rule 2 holds. The moved nodes keep their own children, so rule 3 holds. The node ss is dropped.

Line L10, r.h>s.hr.h > s.h, and ss is a leaf or has at least kk children. The leftmost path of rr has a node of each height from r.hr.h down to 0, by (H), so the node vv of height s.h+1s.h + 1 exists, and vv is an inner node. ss becomes a child of vv; then (H) holds at vv, and the leaves of ss have depth (r.h−s.h−1)+1+s.h=r.h(r.h - s.h - 1) + 1 + s.h = r.h, which is rule 1. Now ss is a non-root; if it is inner, it has at least kk children, which is rule 3. vv only gains a child.

Line L11, r.h>s.h≥1r.h > s.h \ge 1, and ss has fewer than kk children. The node vv of height s.h≥1s.h \ge 1 on the leftmost path of rr exists, as above. The children of ss, of height s.h−1s.h - 1, become children of vv, so (H) holds and their leaves have depth r.hr.h. vv only gains children, and the moved nodes keep theirs, so rules 2 and 3 hold. The node ss is dropped.

So (S), (K) and (H) hold after every operation, and FIND and UNION are correct.

Termination and the height bound. By (K), a tree of height h≥1h \ge 1 has at least two subtrees of height h−1h - 1 below the root, and every inner non-root node has at least kk children, so it has at least 2kh−12k^{h-1} leaves. With at most nn leaves, h≤1+log⁡k(n/2)h \le 1 + \log_k(n/2). Each walk up in FIND or ROOT and each walk down in lines L10 and L11 takes at most hh steps, and the moves of line L8 or L11 take fewer than kk steps. So every operation terminates, in O(k+log⁡kn)O(k + \log_k n) steps. ■\blacksquare

Back to Blum's disjoint-set structure