📖 Scenario: Imagine you are managing groups of friends at a party. Each friend can be in one group. Sometimes, two groups join together to become one big group. You want to keep track of which friends are in the same group.
🎯 Goal: You will build a simple Union Find Disjoint Set data structure in C++ to manage friend groups. You will create the groups, join them, and check if two friends are in the same group.
📋 What You'll Learn
Create an array to represent the parent of each friend
Create an array to represent the size of each group
Write a function findParent to find the leader of a friend's group
Write a function unionSets to join two groups
Check if two friends are in the same group using findParent
💡 Why This Matters
🌍 Real World
Union Find is used in social networks to find friend circles, in network connectivity to check if computers are connected, and in clustering problems.
💼 Career
Understanding Union Find helps in solving problems related to grouping, connectivity, and network design which are common in software engineering and competitive programming.
Progress0 / 4 steps
1
Create parent and size arrays
Create an integer array called parent of size 5 where each element is its own parent (0 to 4). Also create an integer array called size of size 5 where each element is 1.
DSA C++
Hint
Use a for loop from 0 to 4 to set parent[i] = i and size[i] = 1.
2
Write the findParent function
Write a function called findParent that takes an integer node and returns the leader (parent) of that node. Use path compression by setting parent[node] to the result of findParent(parent[node]) if node is not its own parent.
DSA C++
Hint
Check if node is its own parent. If yes, return it. Otherwise, call findParent recursively on parent[node] and update parent[node].
3
Write the unionSets function
Write a function called unionSets that takes two integers a and b. Find their parents using findParent. If they are different, attach the smaller size group to the bigger size group and update the size array.
DSA C++
Hint
Find parents of a and b. If different, attach smaller group to bigger group and update size.
4
Use unionSets and findParent to check groups
In main, call unionSets(0, 1), unionSets(1, 2), and unionSets(3, 4). Then print findParent(2) and findParent(4) separated by a space.
DSA C++
Hint
Call unionSets to join groups, then print the leaders of friends 2 and 4.
Practice
(1/5)
1. What is the primary purpose of the Union Find Disjoint Set data structure?
easy
A. To efficiently track and merge groups of connected elements
B. To sort elements in ascending order
C. To store key-value pairs for fast lookup
D. To implement a queue with priority
Solution
Step 1: Understand the role of Union Find
The Union Find data structure is designed to keep track of which elements belong to which groups or sets, especially when these groups can merge.
Step 2: Compare with other data structures
Sorting elements, storing key-value pairs, or implementing priority queues are different tasks handled by other data structures.
Final Answer:
To efficiently track and merge groups of connected elements -> Option A
Quick Check:
Union Find = Track and merge groups [OK]
Hint: Union Find = track connected groups fast [OK]
Common Mistakes:
Confusing Union Find with sorting algorithms
Thinking it stores key-value pairs like a map
Assuming it manages priority like a heap
2. Which of the following is the correct way to declare a parent array for Union Find of size n in C++?
easy
A. int parent[n+1]; for(int i=1; i<=n; i++) parent[i] = i;
B. int parent[n]; for(int i=0; i
C. int parent[n]; for(int i=0; i<=n; i++) parent[i] = i;
D. int parent[n]; for(int i=1; i
Solution
Step 1: Initialize parent array correctly
The parent array should have size n, and each element should initially be its own parent, so indices 0 to n-1 must be set.
Step 2: Check loop boundaries
Loop must run from 0 to n-1 (i < n) to avoid out-of-bounds errors.
Final Answer:
int parent[n]; for(int i=0; i -> Option B
Quick Check:
Initialize parents from 0 to n-1 [OK]
Hint: Initialize parents from 0 to n-1 [OK]
Common Mistakes:
Using <= n causing out-of-bounds error
Starting loop from 1 when array is 0-indexed
Declaring array size incorrectly
3. Consider the following C++ code using Union Find:
int parent[5];
for(int i=0; i<5; i++) parent[i] = i;
// Union function
void unionSet(int a, int b) {
int rootA = findSet(a);
int rootB = findSet(b);
if(rootA != rootB) parent[rootB] = rootA;
}
// Find function
int findSet(int x) {
if(parent[x] == x) return x;
return parent[x] = findSet(parent[x]);
}
unionSet(0, 1);
unionSet(1, 2);
unionSet(3, 4);
cout << (findSet(2) == findSet(0)) << " " << (findSet(4) == findSet(0));
What is the output?
medium
A. 0 0
B. 0 1
C. 1 1
D. 1 0
Solution
Step 1: Perform unions and find roots
unionSet(0,1) merges sets of 0 and 1; unionSet(1,2) merges sets of 1 and 2, so 0,1,2 are connected. unionSet(3,4) connects 3 and 4.
Step 2: Check if 2 and 0 share the same root, and if 4 and 0 share the same root
findSet(2) == findSet(0) is true (1), findSet(4) == findSet(0) is false (0).
Final Answer:
1 0 -> Option D
Quick Check:
Connected(2,0) = 1, Connected(4,0) = 0 [OK]
Hint: Check connected sets after unions [OK]
Common Mistakes:
Assuming 4 is connected to 0
Forgetting path compression in findSet
Mixing up union order
4. The following code snippet for Union Find has a bug. What is the error?
int parent[5];
for(int i=0; i<5; i++) parent[i] = i;
int findSet(int x) {
if(parent[x] == x) return x;
return findSet(parent[x]);
}
void unionSet(int a, int b) {
int rootA = findSet(a);
int rootB = findSet(b);
if(rootA != rootB) parent[rootB] = rootA;
}
unionSet(0, 1);
unionSet(1, 2);
cout << (findSet(2) == findSet(0));
medium
A. Incorrect initialization of parent array
B. Union function does not check if roots are equal
C. Missing path compression in findSet function
D. Array size is too small for elements
Solution
Step 1: Analyze findSet function
The findSet function recursively finds the root but does not update parent[x] to root, missing path compression optimization.
Step 2: Understand impact of missing path compression
Without path compression, findSet is slower on deep trees but still correct. Other options are incorrect because initialization and union checks are correct, and array size matches elements.
Final Answer:
Missing path compression in findSet function -> Option C
Quick Check:
findSet lacks path compression [OK]
Hint: Check if findSet updates parent for path compression [OK]
Common Mistakes:
Thinking union lacks root equality check
Assuming parent array is wrongly initialized
Confusing array size with element count
5. You have 6 elements (0 to 5) initially in separate sets. After these operations: