Union Find in 5 minutes Data Structures & Algorithms
Channel: Potato Coders
Watch on YouTube →Data Structure • extra hard
Union-Find (Disjoint Set Union) tracks which items are in the same connected component.
It is extremely efficient for repeated connectivity checks with dynamic unions.
Path compression + union by rank/size makes operations nearly constant in practice.
Typical Complexity Baseline
| Metric | Value |
|---|---|
| Complexity Note 1 | Near O(1) amortized |
See It
A small animated SVG that illustrates the core idea the moving parts you need to keep in your head when you read the rest of the guide.
find returns each set's root. union attaches the smaller-rank root under the larger one.
Watch First
A quick visual walkthrough before the detailed sections pick the format that helps you learn fastest.
Channel: Potato Coders
Watch on YouTube →Mechanics
The building blocks and terminology you need before everything else clicks. Skim or deep-read.
What it is Points each node to representative parent.
Why it matters Parent array is a required building block for understanding how Disjoint Set Union (Union-Find) stays correct and performant on large inputs.
What it is Resolve component representative.
Why it matters Find is a required building block for understanding how Disjoint Set Union (Union-Find) stays correct and performant on large inputs.
What it is Merge two components.
Why it matters Union is a required building block for understanding how Disjoint Set Union (Union-Find) stays correct and performant on large inputs.
What it is Heuristic for shallow trees.
Why it matters Rank/size is a required building block for understanding how Disjoint Set Union (Union-Find) stays correct and performant on large inputs.
What it is Flattens trees during find operations. Rewire node directly to root during find.
Why it matters Path compression is a required building block for understanding how Disjoint Set Union (Union-Find) stays correct and performant on large inputs. Rewire node directly to root during find. In Disjoint Set Union (Union-Find), this definition helps you reason about correctness and complexity when inputs scale.
What it is Canonical root id for component.
Why it matters Canonical root id for component. In Disjoint Set Union (Union-Find), this definition helps you reason about correctness and complexity when inputs scale.
What it is Attach smaller-depth tree under larger-depth root.
Why it matters Attach smaller-depth tree under larger-depth root. In Disjoint Set Union (Union-Find), this definition helps you reason about correctness and complexity when inputs scale.
What it is Average cost across operation sequence.
Why it matters Average cost across operation sequence. In Disjoint Set Union (Union-Find), this definition helps you reason about correctness and complexity when inputs scale.
What it is Non-overlapping groups of elements.
Why it matters Non-overlapping groups of elements. In Disjoint Set Union (Union-Find), this definition helps you reason about correctness and complexity when inputs scale.
Walkthrough
Every frame of the concept diagram, laid out top-to-bottom so you can scroll through the algorithm at your own pace.
7 elements, all in their own singleton set. parent[i] = i.
union(1, 2): roots are 1, 2. Different → attach 2 under 1.
Set {1, 2} formed.
union(3, 4): attach 4 under 3.
Three groups now: {1,2}, {3,4}, plus singletons {5}, {6}, {7}.
union(5, 6): attach 6 under 5.
Groups: {1,2}, {3,4}, {5,6}, {7}.
union(2, 4): first find(2). Walk to root → 1. (Then find(4) → 3.) Highlighting find(4) walk.
Roots 1 ≠ 3 → merge. Attach 3 under 1 (so the {3,4} subtree dangles under 1).
Set {1, 2, 3, 4} formed in one tree rooted at 1.
union(1, 5): roots 1 and 5. Attach 5 under 1.
Now {1, 2, 3, 4, 5, 6} all share root 1.
union(7, 6): find(7) → 7 (still singleton). find(6) → 5 → 1.
Attach 7 under 1.
All 7 elements in one connected component. Each find/union is amortized O(α(n)) with path compression + union by rank.
Tradeoffs
When this technique wins, when it loses, and what to reach for instead.
Choose this when Choose Union-Find for many connectivity checks over many unions.
Tradeoff Traversal better when full path details are required.
Choose this when Choose Union-Find for sparse dynamic edge streams.
Tradeoff Matrix offers O(1) direct edge existence lookup but high memory.
Choose this when Choose Union-Find for undirected connectivity grouping.
Tradeoff Topological sort is for directed acyclic dependency ordering.
In Production
Where this shows up at real companies and what the on-call engineer learned the hard way.
Union-Find style component grouping helps reason about cluster and segment connectivity.
Takeaway Connectivity grouping is a common infrastructure primitive.
Spatial region merging and connectivity analyses use disjoint-set logic.
Takeaway Union-Find is practical for geometric grouping workflows.
Entity resolution systems merge records into clusters of same real-world entity.
Takeaway Union operations model transitive equivalence elegantly.
Code
A clean reference implementation in TypeScript, Python, and Go. Switch languages with the toggle.
Maintain parent + rank arrays and compress path on every find.
Complexity: Amortized almost O(1)
Union-Find with path compression
class UnionFind {
private readonly parentByNode: number[]
private readonly rankByNode: number[]
constructor(nodeCount: number) {
this.parentByNode = Array.from({ length: nodeCount }, (_, index) => index)
this.rankByNode = new Array<number>(nodeCount).fill(0)
}
find(nodeId: number): number {
if (this.parentByNode[nodeId] !== nodeId) {
this.parentByNode[nodeId] = this.find(this.parentByNode[nodeId])
}
return this.parentByNode[nodeId]
}
}
Watch Out
The bugs and edge cases that bite engineers most often. Skim before you ship.
Pro Tips
Small habits that pay off every time you reach for this pattern.
Pattern Recognition
Concrete signals that tell you this is the right pattern both at work and on LeetCode.
Practice
A curated easy-to-hard problem ladder. Each one reinforces a specific aspect of the pattern.
| # | Problem | Difficulty | Typical Complexity |
|---|---|---|---|
| 1 | Number of Provinces | Medium | O(n^2) |
| 2 | Redundant Connection | Medium | O(n alpha(n)) |
| 3 | Accounts Merge | Medium | O(n alpha(n)) |
| 4 | Satisfiability of Equality Equations | Medium | O(n alpha(n)) |
| 5 | Number of Connected Components in an Undirected Graph | Medium | O(V+E) |
| 6 | Most Stones Removed with Same Row or Column | Medium | O(n alpha(n)) |
| 7 | Graph Valid Tree | Medium | O(V+E) |
| 8 | Min Cost to Connect All Points | Medium | O(n^2 log n) |
| 9 | Number of Islands II | Hard | O(k alpha(n)) |
| 10 | Swim in Rising Water | Hard | O(n^2 log n) |