What is the main purpose of the Union Find Disjoint Set data structure?
It helps to keep track of elements divided into groups called sets, and quickly find which set an element belongs to or merge two sets.
Click to reveal answer
beginner
Explain the 'find' operation in Union Find Disjoint Set.
The 'find' operation returns the representative or leader of the set that an element belongs to. It helps check if two elements are in the same set.
Click to reveal answer
beginner
What does the 'union' operation do in the Union Find Disjoint Set?
The 'union' operation merges two different sets into one by connecting their leaders, so elements from both sets become part of the same group.
Click to reveal answer
intermediate
Why is path compression important in the Union Find Disjoint Set?
Path compression makes the 'find' operation faster by making every node on the path point directly to the leader, reducing the tree height.
Click to reveal answer
intermediate
What is the role of 'union by rank' or 'union by size' in Union Find?
It helps keep the tree shallow by always attaching the smaller tree under the root of the larger tree, improving efficiency of operations.
Click to reveal answer
What does the 'find' operation return in a Union Find Disjoint Set?
AThe leader or representative of the set
BThe size of the set
CThe number of sets
DThe parent of the element
✗ Incorrect
The 'find' operation returns the leader or representative of the set that the element belongs to.
What is the main goal of the 'union' operation?
ATo delete an element from a set
BTo split a set into two
CTo find the size of a set
DTo merge two sets into one
✗ Incorrect
The 'union' operation merges two different sets into a single set.
Which technique helps speed up the 'find' operation by flattening the tree?
ABreadth-first search
BUnion by size
CPath compression
DDepth-first search
✗ Incorrect
Path compression flattens the tree by making nodes point directly to the leader, speeding up future 'find' operations.
What does 'union by rank' help to achieve?
AKeeps the tree balanced by attaching smaller tree under larger
BDeletes the smaller tree
CFinds the rank of an element
DSplits the set into ranks
✗ Incorrect
'Union by rank' attaches the smaller tree under the larger tree to keep the tree shallow and efficient.
If two elements have the same leader, what does it mean?
AThey belong to different sets
BThey belong to the same set
CThey are not connected
DThey are the leaders themselves
✗ Incorrect
If two elements share the same leader, they belong to the same set.
Describe how the Union Find Disjoint Set data structure works and why it is useful.
Think about grouping elements and checking if they belong together.
You got /5 concepts.
Explain the optimizations 'path compression' and 'union by rank' in Union Find and how they improve performance.
Consider how trees can become tall and how to keep them short.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of the Union Find Disjoint Set data structure?
easy
A. To sort elements in ascending order
B. To efficiently track and merge groups of connected elements
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 merge.
Step 2: Compare with other data structures
Sorting, key-value storage, and priority queues serve different purposes unrelated to grouping connected components.
Final Answer:
To efficiently track and merge groups of connected elements -> Option B
Quick Check:
Union Find = track and merge groups [OK]
Hint: Union Find = manage connected groups fast [OK]
Common Mistakes:
Confusing Union Find with sorting algorithms
Thinking it stores key-value pairs
Assuming it manages priority queues
2. Which of the following is the correct way to initialize a Union Find parent array for n elements in C?
easy
A. int parent[n]; for(int i=0; i
B. int parent[n]; for(int i=0; i
C. int parent[n]; for(int i=0; i
D. int parent[n]; for(int i=0; i
Solution
Step 1: Understand initialization of Union Find
Each element starts as its own parent, so parent[i] = i for all i from 0 to n-1.
Step 2: Check each option
int parent[n]; for(int i=0; i
Final Answer:
int parent[n]; for(int i=0; i -> Option A
Quick Check:
Initialize parent[i] = i [OK]
Hint: Parent array starts with self indices [OK]
Common Mistakes:
Setting all parents to zero
Using -1 as parent without meaning
Assigning parent to n (out of range)
3. Consider the following C code using Union Find for 5 elements (0 to 4):
int parent[5];
for(int i=0; i<5; i++) parent[i] = i;
// Union operations
union_set(parent, 0, 1);
union_set(parent, 1, 2);
union_set(parent, 3, 4);
// Find operations
int a = find_set(parent, 2);
int b = find_set(parent, 4);
printf("%d %d", a, b);
Assuming union_set merges sets and find_set returns the root parent, what is the printed output?
medium
A. 0 3
B. 1 3
C. 2 4
D. 0 4
Solution
Step 1: Trace union operations
Initially, parent[i] = i for i=0..4. union_set(0,1) merges sets containing 0 and 1, root becomes 0. union_set(1,2) merges sets containing 1 and 2, root is 0 (since 1's root is 0). union_set(3,4) merges sets containing 3 and 4, root becomes 3.
Step 2: Trace find operations
find_set(2) returns root of 2, which is 0. find_set(4) returns root of 4, which is 3.
Final Answer:
0 3 -> Option A
Quick Check:
Roots after unions: 2->0, 4->3 [OK]
Hint: Follow union roots to find final parents [OK]
Common Mistakes:
Assuming find_set returns the element itself
Not updating roots after unions
Mixing up which element is root
4. The following C code attempts to implement union_set but has a bug:
void union_set(int parent[], int a, int b) {
int rootA = find_set(parent, a);
int rootB = find_set(parent, b);
if (rootA != rootB) {
parent[a] = rootB;
}
}
What is the bug in this code?
medium
A. It should assign parent[rootB] = rootA instead
B. It does not check if rootA equals rootB before union
C. It assigns parent[a] instead of parent[rootA], breaking the set structure
D. It uses find_set incorrectly by passing wrong parameters
Solution
Step 1: Analyze union assignment
The union should link the root of one set to the root of the other. Assigning parent[a] = rootB is wrong because 'a' may not be a root.
Step 2: Correct union assignment
It should assign parent[rootA] = rootB to merge sets properly. This maintains the tree structure of the disjoint sets.
Final Answer:
It assigns parent[a] instead of parent[rootA], breaking the set structure -> Option C
Quick Check:
Union must link roots, not elements [OK]
Hint: Always link root parents in union [OK]
Common Mistakes:
Assigning parent of non-root element
Skipping root equality check
Confusing parameters in find_set
5. You have 7 elements (0 to 6) initially separate. After these union operations: