Union-Find (Disjoint Set Union)
Imagine a school where students start out as strangers. One by one, friendships are formed. At any moment you want to ask: are these two students in the same friend group? And: merge the friend groups of these two students.
Union-Find (also called Disjoint Set Union, DSU) is the data structure built for exactly this — tracking which elements belong to the same group, with near \(O(1)\) operations.
The core idea
Each element points to a representative (the group leader). Two elements are in the same group if and only if they share the same representative. Initially every element is its own leader — all isolated.
← → arrow keys to step · click elements to interact
Union-Find Operations
Find with path compression
To find the representative of an element, follow parent pointers up to the root. Path compression is the key optimization: on the way back, make every element point directly to the root. Future queries on those elements take \(O(1)\).
Union by rank
To merge two groups, attach one root under the other. Union by rank: always attach the shorter tree under the taller one. This keeps trees flat, keeping future finds fast. Equal ranks — pick either and increment the rank.
Why it is nearly \(O(1)\)
With both path compression and union by rank, operations run in \(O(α(n)\)) amortized time, where α is the inverse Ackermann function. For any practical input size, α(n) ≤ 4. In practice: constant time.