Union-find: merging groups fast
About this lesson
Union-find (also called a disjoint-set structure) keeps track of groups that only ever merge, and answers "are these two in the same group?" almost instantly. Each element stores one number, its parent, so every group is a tree whose root names the group. Follow six elements through four unions, watch find climb the parent arrows, and see union by size and path compression flatten the trees until every element points straight at one root. The lesson "Connected components and grid flood fill" finds the same groups by walking the graph; the Software lessons on graphs and on BFS and DFS cover the graphs themselves.
Groups that only merge
Six people, numbered 0 to 5. Friendships arrive one at a time, and we keep asking: are a and b in the same group? Groups only ever merge. Union-find answers this quickly, without searching the whole graph again.
no structure yet: just six separate elements parent = [0, 1, 2, 3, 4, 5] size = [1, 1, 1, 1, 1, 1]
Read the lesson step by step
Groups that only merge
Six people, numbered 0 to 5. Friendships arrive one at a time, and we keep asking: are a and b in the same group? Groups only ever merge. Union-find answers this quickly, without searching the whole graph again.
no structure yet: just six separate elements parent = [0, 1, 2, 3, 4, 5] size = [1, 1, 1, 1, 1, 1]Everyone starts alone
Each element stores one number: its parent. At the start parent[i] = i, so every element points at itself. An element that is its own parent is a root, and the root names its group. Six roots: six groups.
nothing new runs parent = [0, 1, 2, 3, 4, 5] size = [1, 1, 1, 1, 1, 1]union(0, 1) links two roots
union(0, 1) first finds each root: 0 and 1, both alone. They differ, so root 1 becomes a child of root 0: parent[1] = 0. One write merges two groups, and size[0] becomes 2.
runs: union(0, 1) parent = [0, 0, 2, 3, 4, 5] size = [2, 1, 1, 1, 1, 1]Two more unions
union(2, 3) and union(4, 5) work the same way: parent[3] = 2 and parent[5] = 4. Now there are three groups of two. Each group is a small tree, and every arrow points from a child up to its parent.
runs: union(2, 3), union(4, 5) parent = [0, 0, 2, 2, 4, 4] size = [2, 1, 2, 1, 2, 1]Merging two trees
union(1, 3): find(1) climbs to root 0 and find(3) climbs to root 2. Both trees have size 2, a tie, so root 2 goes under root 0: parent[2] = 0. Element 3 comes along; its parent is still 2.
runs: union(1, 3) parent = [0, 0, 0, 2, 4, 4] size = [4, 1, 2, 1, 2, 1]find(3) climbs to the root
find(x) follows parent arrows until it reaches an element that points at itself. find(3) goes 3 → 2 → 0: two hops, and 0 is the root. The deeper an element sits, the longer its find takes.
nothing new runs find(3): 3 → 2 → 0 (2 hops) parent = [0, 0, 0, 2, 4, 4] size = [4, 1, 2, 1, 2, 1]Path compression
On its way back, find(3) points 3 straight at the root: parent[3] = 0. The answer is the same, but the next find(3) takes one hop. This is path compression: every find flattens the path it walked.
runs: find(3) find(3): 3 → 2 → 0 (2 hops) parent = [0, 0, 0, 0, 4, 4] size = [4, 1, 2, 1, 2, 1]Union by size
union(5, 3): find(5) gives root 4, size 2; find(3) gives root 0, size 4. The smaller tree hangs under the bigger root: parent[4] = 0 and size[0] = 6. Only a root’s size is ever read. No element ends up deeper than log₂ n.
runs: union(5, 3) parent = [0, 0, 0, 0, 0, 4] size = [6, 1, 2, 1, 2, 1]Are 5 and 1 connected?
Same group? Compare roots. find(5) climbs 5 → 4 → 0, then compresses: parent[5] = 0. find(1) reaches 0 in one hop. Both roots are 0, so the answer is yes. Now every element points directly at 0.
runs: find(5), find(1) find(5): 5 → 4 → 0 (2 hops) find(1): 1 → 0 (1 hop) parent = [0, 0, 0, 0, 0, 0] size = [6, 1, 2, 1, 2, 1]Nearly constant time
With both tricks, any m operations on n elements take O(m α(n)) time. α, the inverse Ackermann function, is at most 4 for any n you could ever store. Kruskal’s spanning tree algorithm, later in this course, relies on it.
nothing new runs parent = [0, 0, 0, 0, 0, 0] size = [6, 1, 2, 1, 2, 1]
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01parent = [0, 0, 1, 3]. What does find(2) return, before any path compression?
find climbs the arrows until an element points at itself: 2 → 1 → 0, and parent[0] = 0, so the root is 0. Element 3 is a separate group: it is its own root.
02In the same array, parent = [0, 0, 1, 3], what does path compression change when find(2) runs?
On the way back, every element find walked is pointed straight at the root. 2 now points at 0, so the next find(2) takes one hop. parent[1] is already 0.
03With union by size, a root with size 3 and a root with size 5 are merged. What happens?
The smaller tree hangs under the bigger root, and the bigger root's size grows by the smaller one's: 5 + 3 = 8. Only one parent entry is written.
04Why does union by size keep every element at most log₂ n deep?
An element sinks one level only when its tree is hung under a tree at least as big, so its group's size at least doubles each time. Starting from 1, it can double at most log₂ n times before reaching n.
05With union by size and path compression together, how long do m operations on n elements take?
Tarjan proved the O(m α(n)) bound in 1975. It is amortised: one find can still take a few hops, but the total is nearly linear in m, because α grows so slowly.
Glossary
- Union-find
- A structure that keeps elements in groups that can merge, and answers which group an element is in. Also called a disjoint-set structure.
- Parent array
- A list where parent[x] is the element x points at. Following these pointers always ends at a root.
- Root
- An element whose parent is itself. It names its group: two elements are in the same group when they have the same root.
- find(x)
- Climb from x along parent pointers to the root and return it.
- union(a, b)
- Find the roots of a and b and, if they differ, point one root at the other, merging the two groups.
- Union by size
- When merging, hang the root of the smaller group under the root of the bigger one. It keeps every tree at most log₂ n deep.
- Path compression
- During find, point every element on the path straight at the root, so later finds are shorter.
- Inverse Ackermann function, α(n)
- A function that grows so slowly that it is at most 4 for any input size that could ever be stored.
Notes and sources
- This lesson uses union by size, as in Sedgewick and Wayne. CLRS uses union by rank, which links by an upper bound on tree height instead of member count. Both keep every tree at most log₂ n deep, and both give the O(m α(n)) bound with path compression.
- Why log₂ n: an element only gets deeper when its tree is hung under a tree at least as big, so its group at least doubles each time. A group cannot double more than log₂ n times.
- The O(m α(n)) bound was first proved by Tarjan in 1975. It is amortised: a single find can still take several hops, but the total over m operations is at most proportional to m α(n).
- The code is real Python 3. Set up parent = list(range(6)) and size = [1] * 6, then run union(0, 1), union(2, 3), union(4, 5), union(1, 3), find(3), union(5, 3), find(5) and find(1): parent ends as [0, 0, 0, 0, 0, 0]. find calls itself once per hop, so with union by size it never recurses deeper than log₂ n.
- Union-find gives the same groups as the connected components of the friendship graph, but it never walks the graph: it only follows parent pointers. It cannot split a group again, so it suits edges that are only ever added.
- Sedgewick and Wayne, Algorithms 4th ed., §1.5 Case Study: Union-Find
- Sedgewick and Wayne, WeightedQuickUnionUF.java (union by size, tie rule)
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 3rd ed. (MIT Press, 2009), chapter 21: Data Structures for Disjoint Sets
- Disjoint-set data structure: history of the O(m α(n)) bound (Tarjan 1975)
- MIT 6.046J Design and Analysis of Algorithms (Spring 2015), Recitation 3: Amortization: Union-find
- Python Standard Library: range
- Python Language Reference: Assignment statements
Quiz Unlocks after the last step.