← chapter

Union-find and disjoint sets

A tree for GROUPING, not searching. Merge groups + test "same group?" in ≈ constant time.

Sets as trees named by their root

Watch merges + path compression

Merge pairs → fours → one. Then find(7) compresses the path to the root. The structure flattens itself as you use it.

O(α(n)) — near constant

α(n) ≤ 4 for any real n. 566× faster than naive O(n)-union at n=16000.

Merge only, never split

Takeaway

A few lines beat a 566× hardware speedup — the wins are algorithmic. Drives Kruskal, connected components, cycle detection. Next tier: graphs.