Union-Find
Система непересекающихся множеств
This data structure tracks groups that do not overlap. It lets you merge two groups and quickly check whether two items belong to the same group.
Also known as: DSU, disjoint set union
What Union-Find keeps track of
Union-Find, also called disjoint set union (DSU), answers a simple question: are these two items in the same group? Each item starts in a group of its own. A merge combines two entire groups, so if you merge A with B and then B with C, A and C belong to the same group even though you never merged them directly.
How find and union work
The usual implementation represents each group as a tree. Each item stores its parent's index, and the root points to itself. find(x) follows those parent references to the root, which serves as the group's representative. union(a, b) finds both roots and, if they differ, makes one root a child of the other. The representative is an implementation detail: it can change after a merge and need not be the group's smallest item.
Two optimizations keep the trees shallow. Union by size attaches the smaller tree to the larger one. Path compression shortens the parent chains visited by find, making later searches cheaper. Together they give find and union an amortized time bound of O(α(n)), where α is the inverse Ackermann function and n is the number of items. That's nearly constant per operation in practice, but the bound is amortized over a sequence, not a worst-case guarantee for each call. Setup takes O(n) time and the structure uses O(n) space.
Example: count components as edges arrive
Suppose an undirected graph starts with n isolated vertices numbered 0 through n - 1. Each edge reduces the component count by one only if it connects two previously separate components.
def component_counts(n, edges):
parent = list(range(n))
size = [1] * n
count = n
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for u, v in edges:
a, b = find(u), find(v)
if a != b:
if size[a] < size[b]:
a, b = b, a
parent[b] = a
size[a] += size[b]
count -= 1
yield count
For four vertices and edges (0, 1), (1, 2), (0, 2), the counts are 3, 2, 2. The last edge stays within an existing component. The find loop uses path halving, a variant of path compression that makes each visited node point to its grandparent before moving up.
When to use it in an interview
Look for repeated connectivity queries while edges are being added. In Kruskal's minimum spanning tree algorithm, for instance, Union-Find tells you whether accepting an edge would create a cycle: it would if both endpoints already share a root. For a one-time scan of a fixed graph, BFS or DFS is often simpler. Standard Union-Find cannot split a component after an edge deletion or tell you which graph edges form a path between two vertices. Its parent pointers represent group membership, not graph edges.
When implementing it, compare roots rather than immediate parents, and keep component sizes at the roots. Test a repeated edge or a self-loop: neither should change the number of components.