1. Disjoint sets
A disjoint-set structure maintains a collection of non-overlapping groups. The two operations are find, which returns the representative of an element's group, and union, which merges two groups. The structure does not care about the order inside a group, only about which elements belong together.
The standard implementation stores a parent pointer per element. A root points to itself. Every other element points to a parent; following the pointers eventually reaches the root, which is the group identifier.
2. Find
Find walks the parent chain to the root. Without any optimization, a long chain makes find O(n) in the worst case. Two optimizations make the amortized cost effectively constant: union by rank keeps trees shallow, and path compression rewires the chain during find.
def find(x):
if parent[x] != x:
parent[x] = find(parent[x])
return parent[x]The assignment is the compression: after the recursive call, the element points directly at the root. The next find on that element takes one step.
3. Union by rank
Union merges two roots. If you always attach the taller tree under the shorter one, the height can grow linearly. Union by rank attaches the shorter tree under the taller one and increases the rank only when the ranks are equal. The result is a tree of height O(log n) before compression.
ra, rb = find(a), find(b)
if ra == rb: return False
if rank[ra] < rank[rb]: parent[ra] = rb
elif rank[ra] > rank[rb]: parent[rb] = ra
else:
parent[rb] = ra
rank[ra] += 1
return True4. Path compression
Path compression does not change the roots; it changes the pointers. Every node on the path from the queried element to the root is rewired directly to the root. The tree gets flatter, and later finds get cheaper. Union by rank plus path compression gives an amortized cost of O(α(n)), where α is the inverse Ackermann function — below 5 for any input that fits in the observable universe.
Example: union the pairs (0,1), (2,3), (4,5), (6,7), then (0,2), (4,6), (0,4)
After the first four unions there are four two-element trees. After (0,2) and (4,6) there are two four-element trees. After (0,4) there is one component containing all eight elements.
A later find(7) walks 7 → 6 → 4 → 0 and compresses every node on that path to 0. The next find(7) is one step.
5. Applications
Union-find is the right tool whenever you need to merge groups and ask connectivity questions incrementally. It is not a general graph traversal; it answers “same group?” very quickly, but it does not give you a path.
- Connected components — union every edge, then count the roots.
- Kruskal's MST — add the next cheapest edge if its endpoints are in different sets; union them.
- Cycle detection — an edge whose endpoints are already connected closes a cycle.
- Percolation / image segmentation — merge adjacent open cells or similar pixels.
- Accounts merge — union records that share an email or identifier.
Union-find vs BFS/DFS
BFS and DFS traverse a graph and can return a path. Union-find only maintains the partition; it is faster for incremental connectivity and for Kruskal, where the graph is processed edge by edge.
6. Practice
What does the parent array look like after union(0,1) and union(2,3) with union by rank?
Why does union by rank alone give O(log n) height?
How would you count connected components after processing all edges?
Can union-find tell you the shortest path between two nodes?
7. Resources
The CS track now has the data structures and the algorithms that use them: heaps power Dijkstra, sorting orders the edges for Kruskal, and union-find merges the components. The next natural step is weighted graphs.