CODEBHAVYA โข ADS LEVEL 9
๐ Disjoint Sets / Union-Find
Maintain non-overlapping groups, find representatives efficiently and merge components using path compression and union by rank or size.
Objectives Operations
Optimizations Complexity
Premium Visualizer C Program
Program Tracing Practice
Revision
LEARNING GOALS
๐ฏ Learning Objectives
After completing this level, you should be able to:
Explain a partition and the representative of a disjoint set.
Perform Make-Set, Find and Union operations.
Represent a collection of sets as a forest of rooted trees.
Apply path compression during Find.
Apply union by rank and union by size.
Use Union-Find to solve connectivity, cycle-detection and Kruskal problems.
Justify the nearly constant amortized complexity of optimized operations.
FOUNDATION
๐งญ 1. What Is a Disjoint-Set Structure?
A disjoint-set data structure , also called Union-Find or DSU , maintains a collection of sets that do not share elements.
Disjoint means: for any two different sets A and B, A โฉ B = โ
. Every element belongs to exactly one current set.
{0, 1, 3} {2, 5} {4} {6, 7}
One element in each set is chosen as its representative . Two elements belong to the same set exactly when their representatives are equal.
Primary Question Are elements x and y currently in the same group?
Check Find(x) == Find(y)
Primary Update Combine the groups containing x and y.
Perform Union(x, y)
WHY IT MATTERS
๐ 2. Applications of Union-Find
๐ธ๏ธ Dynamic Connectivity Track whether two network nodes are connected while links are added.
๐ Cycle Detection An edge (u, v) creates a cycle if u and v already have the same representative.
๐ Kruskalโs MST Add a light edge only when its endpoints belong to different components.
๐งฉ Connected Components Merge related items and count the remaining representatives.
๐ผ๏ธ Image Segmentation Combine neighbouring pixels or regions with similar properties.
๐ฅ Grouping Problems Maintain friend circles, account groups or equivalence classes.
CORE OPERATIONS
โ๏ธ 3. Make-Set, Find and Union
1 Make-Set(x) Create a new set containing only x.
parent[x] = x
2 Find(x) Follow parent links until the root representative is reached.
parent[root] = root
3 Union(x, y) Find both roots and attach one root below the other.
rootX โ rootY
Important: Union must combine the roots, not arbitrary internal nodes. Always call Find before linking two sets.
INTERNAL STRUCTURE
๐ฒ 4. Forest Representation
Each set is stored as a rooted tree. All the trees together form a forest . An array named parent stores the immediate parent of every element.
Element i 0 1 2 3 4 5
parent[i] 0 0 0 2 4 4
A root satisfies parent[root] == root.
The root is used as the representative of the entire set.
The logical set is independent of which particular element becomes its representative.
REPRESENTATIVE SEARCH
๐ 5. Find Operation
The basic Find operation moves upward until it reaches a self-parent root.
Recursive Find Without Compression int findSet(int x) {
if (parent[x] == x)
return x;
return findSet(parent[x]);
}Reason: every recursive call moves one level closer to the root.
Iterative Find int findSet(int x) {
while (parent[x] != x)
x = parent[x];
return x;
}The cost depends on the tree height. A chain-shaped tree may require O(n) time.
MERGING COMPONENTS
๐ 6. Basic Union Operation
void unionSets(int a, int b) {
int rootA = findSet(a);
int rootB = findSet(b);
if (rootA != rootB)
parent[rootB] = rootA;
}
01 Find representative of a.
02 Find representative of b.
03 If roots differ, link one root to the other.
04 If roots match, no structural change is required.
Problem with arbitrary linking: repeatedly attaching a large tree below a small tree can create a long chain and slow down future Find operations.
ESSENTIAL OPTIMIZATIONS
๐ 7. Union by Rank, Union by Size and Path Compression
Union by Rank Attach the root with smaller estimated height below the root with larger rank.
if (rank[rootA] < rank[rootB])
parent[rootA] = rootB;If ranks are equal, choose one root and increase only its rank.
Union by Size Attach the smaller tree below the root of the larger tree.
if (size[rootA] < size[rootB])
swap(rootA, rootB);
parent[rootB] = rootA;
size[rootA] += size[rootB];
Path Compression During Find, make every visited node point directly to the root.
int findSet(int x) {
if (parent[x] != x)
parent[x] = findSet(parent[x]);
return parent[x];
}
Use one union heuristic: rank and size are two alternatives. Either can be combined with path compression. Do not update rank as though it were exact height after compression.
STEP-BY-STEP
๐งฉ 8. Worked Example
Start with elements 0 to 6. Every element is initially its own representative.
Operation Resulting Components Reason
Union(0, 1) {0, 1}, {2}, {3}, {4}, {5}, {6} Merge two singleton sets.
Union(2, 3) {0, 1}, {2, 3}, {4}, {5}, {6} 2 and 3 had different roots.
Union(1, 3) {0, 1, 2, 3}, {4}, {5}, {6} Find(1) and Find(3) identify the two roots.
Union(4, 5) {0, 1, 2, 3}, {4, 5}, {6} A third component is formed.
Find(3) Representative is 0 Path compression may connect 3 directly to 0.
Representative choice: depending on tie-breaking, a different valid root may be produced. Connectivity and set membership remain the same.
PERFORMANCE
๐ 9. Complexity Analysis
Implementation Make-Set Find Union Reason
Unoptimized forest O(1) O(n) worst case O(n) worst case A tree can become a chain of height nโ1.
Union by rank/size only O(1) O(log n) O(log n) Smaller-height tree is attached below the larger one.
Path compression + rank/size O(1) O(ฮฑ(n)) amortized O(ฮฑ(n)) amortized Repeated operations flatten paths while balanced merging limits height.
ฮฑ(n) is the inverse Ackermann function. It grows so slowly that ฮฑ(n) is less than 5 for every practical input size.
Space Complexity O(n) for parent and rank/size arrays.
Sequence of m Operations O(m ฮฑ(n)) after O(n) initialization.
Choose the number of elements and click Initialize Sets . Then perform Union, Find and connectivity queries. The structure will not run automatically when a value changes.
Number of elements
Initialize Sets
Click Initialize Sets to create singleton sets.
Element A
Element B
Union A and B
Find A
Are A and B Connected?
โป Reset
Choose an operation.
C IMPLEMENTATION
๐ป 11. Complete Union-Find Program
The program supports Union, Find and Connected queries using path compression and union by rank.
#include <stdio.h>
#define MAX 1000
int parent[MAX];
int rankValue[MAX];
void makeSet(int n) {
for (int i = 0; i < n; i++) {
parent[i] = i;
rankValue[i] = 0;
}
}
int findSet(int x) {
if (parent[x] != x)
parent[x] = findSet(parent[x]);
return parent[x];
}
void unionSets(int a, int b) {
int rootA = findSet(a);
int rootB = findSet(b);
if (rootA == rootB)
return;
if (rankValue[rootA] < rankValue[rootB]) {
parent[rootA] = rootB;
} else if (rankValue[rootA] > rankValue[rootB]) {
parent[rootB] = rootA;
} else {
parent[rootB] = rootA;
rankValue[rootA]++;
}
}
int main(void) {
int n, q;
scanf("%d %d", &n, &q);
if (n < 1 || n > MAX)
return 0;
makeSet(n);
while (q--) {
int type, a, b;
scanf("%d", &type);
if (type == 1) {
scanf("%d %d", &a, &b);
unionSets(a, b);
} else if (type == 2) {
scanf("%d", &a);
printf("Representative of %d: %d\n", a, findSet(a));
} else if (type == 3) {
scanf("%d %d", &a, &b);
if (findSet(a) == findSet(b))
printf("Connected\n");
else
printf("Not Connected\n");
}
}
return 0;
}
Sample Input 8 9
1 0 1
1 2 3
1 1 3
3 0 3
2 3
1 4 5
1 5 6
3 4 6
3 0 7
Sample Output Connected
Representative of 3: 0
Connected
Not Connected
Time Complexity Initialization: O(n) Each optimized Find/Union: O(ฮฑ(n)) amortized
Space Complexity O(n) for the parent and rank arrays, plus a very small recursive Find stack in practice.
C PROGRAM + TRACING
๐ 12. Program Tracing โ Union-Find Operations
Follow the sample input one operation at a time and observe how the parent and rank arrays change.
โถ Program Tracing โ Union-Find Operations
PROGRAM TRACING โ UNION-FIND
๐ป C Program
makeSet(n);
int rootA = findSet(a);
int rootB = findSet(b);
if (rootA == rootB) return;
if (rankValue[rootA] < rankValue[rootB])
parent[rootA] = rootB;
else if (rankValue[rootA] > rankValue[rootB])
parent[rootB] = rootA;
else { parent[rootB] = rootA;
rankValue[rootA]++; }
parent[x] = findSet(parent[x]);
printf(...);
๐ง What is happening?
Press Next to initialize eight singleton sets.
๐ Live Variables
Operation โ
Root A โ
Root B โ
Components 8
parent [0, 1, 2, 3, 4, 5, 6, 7]
rank [0, 0, 0, 0, 0, 0, 0, 0]
Output
โ
โ Previous
Next โ
โถ Auto Run
โธ Pause
โป Reset
Step 0 of 10
CLASSIC APPLICATION
๐ 13. Cycle Detection in an Undirected Graph
Process edges one by one. For an edge (u, v):
1 Find both representatives Compute rootU = Find(u) and rootV = Find(v).
2 Same representative means a cycle A path already connects u and v, so the new edge closes a cycle.
3 Different representatives are safe Union the two components and continue processing.
Scope: this simple DSU method detects cycles in an undirected graph. Directed-cycle detection requires other techniques.
INTERVIEW PREPARATION
๐ก 14. Important Points and Common Mistakes
โ Unioning original nodes Link roots returned by Find, not a and b directly.
โ Missing same-root check If both elements already share a root, Union should make no change.
โ Increasing rank every time Rank increases only when two equal-rank roots are merged.
โ Mixing rank and size rules Choose one union heuristic and maintain its metadata consistently.
โ Forgetting path assignment return findSet(parent[x]) finds the root but does not compress; assign the result to parent[x].
โ Expecting exact tree shape Tie-breaking may create a different but equally correct forest.
Union-Find answers connectivity under edge additions efficiently, but basic DSU does not support deletion easily.
Path compression changes parent links without changing set membership.
Rank is an upper-bound style balancing measure, not necessarily the current exact height.
Counting roots after all operations gives the number of connected components.
Kruskalโs algorithm uses DSU to reject edges that would create cycles.
CHECK YOUR UNDERSTANDING
โ๏ธ 15. Practice Problems
Solve each problem first. Use Hint only when needed and Show Answer to verify your reasoning.
1. What does Make-Set(5) store initially? Hint Show Answer
A singleton is its own representative.
parent[5] = 5 , with rank 0 or size 1 depending on the heuristic.
2. What condition identifies a root? Hint Show Answer
A representative points to itself.
parent[x] == x.
3. How do we test whether a and b belong to the same set? Hint Show Answer
Compare representatives.
They are in the same set when Find(a) == Find(b) .
4. Why must Union call Find first? Hint Show Answer
Entire trees must be merged.
Find locates each setโs root. Linking the roots merges the complete sets and preserves a valid forest.
5. What is the worst-case Find time in an unoptimized chain of n nodes? Hint Show Answer
Count parent edges from the deepest node.
O(n). The path may contain nโ1 parent links.
6. What does path compression change? Hint Show Answer
Think about nodes visited during Find.
It changes their parent links to point closer to, usually directly to, the root. It does not change set membership.
7. In union by rank, when is a rootโs rank increased? Hint Show Answer
Consider equal and unequal ranks.
Only when two roots with equal rank are merged; the chosen new rootโs rank increases by one.
8. In union by size, which root becomes the child? Hint Show Answer
Keep the resulting height small.
The root of the smaller set becomes a child of the larger setโs root.
9. Can union by rank and path compression be used together? Hint Show Answer
They improve different operations.
Yes. Rank balances merging, while path compression flattens paths during Find.
10. What is ฮฑ(n) in optimized DSU complexity? Hint Show Answer
It grows even more slowly than logarithm.
It is the inverse Ackermann function , which is below 5 for practical input sizes.
11. After Union(0,1), Union(1,2), are 0 and 2 connected? Hint Show Answer
Connectivity is transitive.
Yes. Both operations place 0, 1 and 2 in one component.
12. If Find(u) == Find(v), what happens when edge (u,v) is added to an undirected forest? Hint Show Answer
A path already exists between them.
The edge creates a cycle .
13. How many sets remain after n Make-Set operations and k successful Unions? Hint Show Answer
Every successful merge reduces the component count by one.
n โ k sets , provided every counted Union merged two previously different sets.
14. Why does Union on two already connected elements not reduce the set count? Hint Show Answer
Compare their roots.
Both Find operations return the same root, so no two distinct components are merged.
15. What extra arrays are commonly used in optimized DSU? Hint Show Answer
One stores links and another stores balancing metadata.
A parent array and either a rank array or a size array .
16. What is the DSU space complexity for n elements? Hint Show Answer
Count parent and metadata entries.
O(n).
17. Why is rank not necessarily the exact height after compression? Hint Show Answer
Compression shortens paths without decreasing stored rank.
Path compression can reduce actual height, while rank is retained as balancing metadata and is not recomputed.
18. Which graph algorithm commonly uses DSU? Hint Show Answer
Think about minimum spanning trees and cycle rejection.
Kruskalโs minimum spanning tree algorithm.
19. Can basic Union-Find efficiently separate a component after an edge deletion? Hint Show Answer
Its main update only merges sets.
No. Standard DSU supports merging efficiently but does not directly support splitting after deletions.
20. Give the amortized time for m optimized DSU operations after initialization. Hint Show Answer
Each operation costs ฮฑ(n) amortized.
O(m ฮฑ(n)) , plus O(n) to initialize n singleton sets.
QUICK RECALL
๐ 16. Quick Revision
Disjoint Set Union maintains a partition of elements into non-overlapping groups.
Make-Set creates a singleton; Find returns a representative; Union merges two sets.
A parent array represents each set as a rooted tree.
Union by rank or size avoids unnecessarily tall trees.
Path compression connects visited nodes closer to the root.
With both optimizations, operations take O(ฮฑ(n)) amortized time.
DSU is central to connectivity, undirected-cycle detection and Kruskalโs algorithm.
โ Previous: Performance Analysis
Next: Sparse Matrices โ