Union Find in 5 minutes — Data Structures & Algorithms
Channel: Potato Coders
Full video link:
https://www.youtube.com/watch?v=ayW5B2W9hfoData 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 |
Prefer video learning? This explainer gives a quick visual walkthrough of the core idea before you dive into the detailed sections below.
Union Find in 5 minutes — Data Structures & Algorithms
Channel: Potato Coders
Full video link:
https://www.youtube.com/watch?v=ayW5B2W9hfoLearn the core building blocks and terminology in one place before comparisons, so the mechanics are clear and duplicates are removed.
Parent array
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.
Find
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.
Union
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.
Rank/size
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.
Path compression
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.
Representative
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.
Union by rank
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.
Amortized complexity
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.
Disjoint sets
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.
This walkthrough connects the core concepts of Disjoint Set Union (Union-Find) into one end-to-end execution flow.
Step 1
Points each node to representative parent.
Before
parent=
After
Transition
Why this step matters: Parent array is a required building block for understanding how Disjoint Set Union (Union-Find) stays correct and performant on large inputs.
Step 2
Resolve component representative.
Before
After
parent
Transition
Why this step matters: Find is a required building block for understanding how Disjoint Set Union (Union-Find) stays correct and performant on large inputs.
Step 3
Merge two components.
Before
After
parent
Transition
Why this step matters: Union is a required building block for understanding how Disjoint Set Union (Union-Find) stays correct and performant on large inputs.
Step 4
Heuristic for shallow trees.
Before
After
Transition
Why this step matters: Rank/size is a required building block for understanding how Disjoint Set Union (Union-Find) stays correct and performant on large inputs.
vs Graph traversal per query
When to choose this: Choose Union-Find for many connectivity checks over many unions.
Tradeoff: Traversal better when full path details are required.
vs Adjacency matrix
When to choose this: Choose Union-Find for sparse dynamic edge streams.
Tradeoff: Matrix offers O(1) direct edge existence lookup but high memory.
vs Topological sort
When to choose this: Choose Union-Find for undirected connectivity grouping.
Tradeoff: Topological sort is for directed acyclic dependency ordering.
Networking platforms
Union-Find style component grouping helps reason about cluster and segment connectivity.
Takeaway: Connectivity grouping is a common infrastructure primitive.
GIS tools
Spatial region merging and connectivity analyses use disjoint-set logic.
Takeaway: Union-Find is practical for geometric grouping workflows.
Data quality pipelines
Entity resolution systems merge records into clusters of same real-world entity.
Takeaway: Union operations model transitive equivalence elegantly.
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]
}
}
Use these signals to decide if this data structure/algorithm is the right fit before implementation.
| # | 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) |