LearnViz CompSci
Home / Learn / Data Structures / Disjoint Set Union

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

  1. Every item starts as its own set.
  2. union(1, 2) makes 2 point at root 1.
  3. union(3, 4) merges those two sets.
  4. union(1, 3) links one root under the other.
  5. 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

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. 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?
Structure tracking partitions with union and find operations. It belongs to Data Structures and is an intermediate-level topic.
Where do people use Disjoint Set Union?
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.
What is the most common mistake with Disjoint Set Union?
It supports merging but not splitting sets.
What should I learn before this topic?
Start with Adjacency Matrix, the topic just before it in Data Structures.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Data Structures quiz.
Is Disjoint Set Union hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Disjoint Set Union?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Disjoint Set Union?
Adjacency Matrix, Path Compression, Adjacency List are closely connected. The full list is in the related topics section of this page.
What should I study after Disjoint Set Union?
Continue with Path Compression, the next topic in Data Structures.

Related topics