Lesson 68 of 86AdvancedPro Interactive Lesson

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

MetricComplexity
Time ComplexityO(α(n)) amortized (~O(1))
Space ComplexityO(n)
Topic ChapterChapter 12: Shortest Paths & Spanning Trees
Difficulty LevelAdvanced

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.

Unlock the full course

All 86 lessons across 15 chapters

View pricing

Delete account

This permanently deletes your account and ends any Pro access.