Disjoint Sets with Union–Find
Follow each element to its representative, then observe which roots are joined.
Core Intuition & How It Works
Disjoint Set Union (Union–Find) tracks a partition of elements into disjoint sets with near-constant-time find and union operations. Combining path compression with union by rank or size achieves O(α(n)) amortized time per operation.
In the Byte Sized Algorithms interactive classroom, you can step forward and backward through this algorithm with your keyboard while watching the visual diagram, memory state, and highlighted pseudocode update together.
Time & Space Complexity
| Metric | Complexity |
|---|---|
| Time Complexity | O(α(n)) amortized (~O(1)) |
| Space Complexity | O(n) |
| Topic Chapter | Chapter 12: Shortest Paths & Spanning Trees |
| Difficulty Level | Advanced |
Key Pattern to Recognize
Always compare roots (find(a) === find(b)) before merging, and attach the smaller-rank tree under the larger-rank root.
Common Pitfalls & Edge Cases
Forgetting path compression or union by rank, which allows chains of depth O(n) to form.
Real-World & Technical Interview Applications
Kruskal’s MST, dynamic connectivity, image segmentation, equivalence classes, and cycle detection in undirected graphs.