What is the main purpose of the Union Find Disjoint Set data structure?
It helps to keep track of elements divided into disjoint sets and efficiently supports two operations: finding which set an element belongs to, and uniting two sets.
Click to reveal answer
beginner
What does the 'find' operation do in Union Find?
The 'find' operation returns the representative or root of the set that an element belongs to, helping to check if two elements are in the same set.
Click to reveal answer
intermediate
What is 'union by rank' in the Union Find data structure?
Union by rank is a technique to attach the tree with smaller rank under the root of the tree with larger rank during union, keeping the tree shallow and operations fast.
Click to reveal answer
intermediate
Explain 'path compression' in Union Find.
Path compression flattens the structure of the tree whenever 'find' is called by making each node point directly to the root, speeding up future operations.
Click to reveal answer
advanced
What is the time complexity of Union Find operations with path compression and union by rank?
Almost constant time, specifically O(α(n)) where α is the inverse Ackermann function, which grows very slowly and is practically constant for all reasonable input sizes.
Click to reveal answer
What does the 'union' operation do in Union Find?
ADeletes an element from a set
BFinds the root of a set
CMerges two sets into one
DCreates a new set with one element
✗ Incorrect
The 'union' operation merges two disjoint sets into a single set.
Which technique helps speed up the 'find' operation by flattening the tree?
APath compression
BDepth-first search
CBreadth-first search
DUnion by rank
✗ Incorrect
Path compression flattens the tree during 'find' to speed up future queries.
In Union Find, what is the purpose of 'union by rank'?
ATo attach smaller tree under larger tree to keep trees shallow
BTo keep track of the number of sets
CTo find the root of a set
DTo delete sets efficiently
✗ Incorrect
Union by rank attaches the smaller tree under the larger tree to keep the tree height minimal.
What is the initial state of each element in Union Find?
AAll elements are in one set
BElements are sorted
CElements are grouped randomly
DEach element is in its own set
✗ Incorrect
Initially, each element is in its own separate set.
What is the practical time complexity of Union Find operations with optimizations?
AO(n)
BO(α(n)) (inverse Ackermann function)
CO(log n)
DO(1)
✗ Incorrect
With path compression and union by rank, operations run in almost constant time O(α(n)).
Describe how the Union Find data structure works and why it is useful.
Think about how to group items and check if they belong together quickly.
You got /5 concepts.
Explain the optimizations 'union by rank' and 'path compression' and how they improve Union Find.
Consider how to keep the structure shallow and fast to navigate.
You got /4 concepts.
Practice
(1/5)
1. What is the main 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 fast enqueue and dequeue
Solution
Step 1: Understand the role of Union Find
The Union Find data structure helps keep track of which elements belong to the same group or set.
Step 2: Identify its main operation
It supports merging two groups and checking if two elements are in the same group efficiently.
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 connects groups, not sorting or key-value storage [OK]
Common Mistakes:
Confusing Union Find with sorting algorithms
Thinking it stores key-value pairs like a dictionary
Assuming it works like a queue or stack
2. Which of the following is the correct way to initialize a Union Find parent array for 5 elements in JavaScript?
easy
A. const parent = new Array(5).fill(-1);
B. const parent = [0, 1, 2, 3, 4];
C. const parent = new Array(5).fill(0);
D. const parent = [1, 2, 3, 4, 5];
Solution
Step 1: Understand parent array initialization
Each element is initially its own parent, so parent[i] = i for all i.
Step 2: Check each option
const parent = [0, 1, 2, 3, 4]; correctly sets parent to [0,1,2,3,4]. const parent = new Array(5).fill(0); fills all with 0, wrong. const parent = new Array(5).fill(-1); fills with -1, wrong. const parent = [1, 2, 3, 4, 5]; starts from 1, wrong index.
Final Answer:
const parent = [0, 1, 2, 3, 4]; -> Option B
Quick Check:
Parent array = self indices [OK]
Hint: Parent array starts with each element as its own parent [OK]
Common Mistakes:
Filling parent array with zeros or -1
Starting indices from 1 instead of 0
Not initializing parent array at all
3. Consider the following JavaScript code using Union Find:
const parent = [0, 1, 2, 3, 4];
function find(x) {
if (parent[x] !== x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
function union(a, b) {
const rootA = find(a);
const rootB = find(b);
if (rootA !== rootB) {
parent[rootB] = rootA;
}
}
union(0, 1);
union(1, 2);
console.log(parent);
What will be printed?
medium
A. [0, 0, 0, 3, 4]
B. [0, 1, 2, 3, 4]
C. [1, 1, 1, 3, 4]
D. [0, 0, 1, 3, 4]
Solution
Step 1: Perform union(0, 1)
Find roots: root of 0 is 0, root of 1 is 1. Set parent[1] = 0. Parent becomes [0,0,2,3,4].
Step 2: Perform union(1, 2)
Find roots: root of 1 is find(0) = 0, root of 2 is 2. Set parent[2] = 0. Parent becomes [0,0,0,3,4].
Final Answer:
[0, 0, 0, 3, 4] -> Option A
Quick Check:
Union updates parents to root 0 [OK]
Hint: Union sets parent of one root to the other root [OK]
Common Mistakes:
Not applying path compression in find
Confusing which root becomes parent
Assuming parent array stays unchanged
4. The following code attempts to implement Union Find but has a bug:
const parent = [0, 1, 2, 3];
function find(x) {
if (parent[x] === x) return x;
return find(parent[x]);
}
function union(a, b) {
const rootA = find(a);
const rootB = find(b);
if (rootA !== rootB) {
parent[rootA] = rootB;
}
}
union(0, 1);
union(1, 2);
console.log(parent);
What is the bug in this code?
medium
A. Union assigns parent[rootA] = rootB instead of parent[rootB] = rootA
B. The find function does not return a value
C. Parent array is not initialized correctly
D. Missing path compression in find function
Solution
Step 1: Examine the find function
The find recurses to the root but lacks path compression: it should set parent[x] = find(parent[x]) before returning when parent[x] !== x.
Step 2: Verify other options
Find returns correctly. Parent is initialized properly as self-references. Union direction (parent[rootA] = rootB) is valid.
Final Answer:
Missing path compression in find function -> Option D