Intermediate Topic 30 of 52 Data Structures
Disjoint Set Union
Structure tracking partitions with union and find operations.
How it works
Disjoint set union, or union-find, tracks a partition of elements into groups that merge over time. Find returns the representative of an element's set, and union merges two sets. Stored as a forest of parent pointers, with union by rank or size and path compression, operations run in nearly constant amortized time, bounded by the inverse Ackermann function.
Step-by-step walkthrough
- Every item starts as its own set.
- union(1, 2) makes 2 point at root 1.
- union(3, 4) merges those two sets.
- union(1, 3) links one root under the other.
- find(4) follows parents to the root 1, so 4 and 2 are in the same set.
Where it is used
Kruskal's minimum spanning tree algorithm, detecting cycles in undirected graphs, connected components in dynamic graphs, image segmentation, network connectivity and grouping equivalent items in puzzles use DSU.
Common pitfall
It supports merging but not splitting sets. Forgetting to compare roots rather than raw elements in union causes errors. Without union by rank or path compression, trees can become long chains. Store the size at roots only, and update it carefully when merging.
Where it fits in the course
Disjoint Set Union is topic 30 of 52 in Data Structures, rated Intermediate. It follows Adjacency Matrix. The next topic is Path Compression.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Data Structures quiz.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Data Structures quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Disjoint Set Union?
Where do people use Disjoint Set Union?
What is the most common mistake with Disjoint Set Union?
What should I learn before this topic?
How can I practice it?
Is Disjoint Set Union hard to learn?
How long does it take to learn Disjoint Set Union?
Which topics are related to Disjoint Set Union?
What should I study after Disjoint Set Union?
Related topics
- Adjacency Matrix (Data Structures)
- Path Compression (Data Structures)
- Adjacency List (Data Structures)
- Skip Lists (Data Structures)