📖 Scenario: Imagine you are managing a social network where people form friend groups. You want to quickly find out if two people are in the same friend group and efficiently merge groups when new friendships form.
🎯 Goal: Build a Union Find data structure with path compression to efficiently find and union friend groups.
📋 What You'll Learn
Create a list called parent to represent each person's parent in the friend group.
Create a function find that uses path compression to find the root parent of a person.
Create a function union to merge two friend groups.
Use the find and union functions to process friendship connections.
Print the final parent list showing the root parent of each person after unions.
💡 Why This Matters
🌍 Real World
Union Find with path compression is used in social networks, network connectivity, and clustering to quickly find connected components.
💼 Career
Understanding Union Find is important for software engineers working on graph algorithms, network design, and optimization problems.
Progress0 / 4 steps
1
Create the initial parent list
Create a list called parent with 5 elements where each element is its own index (0 to 4). This means each person is initially their own parent.
DSA Python
Hint
Each person starts as their own group leader, so the parent list is just their own indices.
2
Define the find function with path compression
Define a function called find that takes a parameter x. It should return the root parent of x. Use path compression by setting parent[x] to the root parent during the search.
DSA Python
Hint
Use recursion to find the root and update parent[x] to the root to flatten the tree.
3
Define the union function to merge groups
Define a function called union that takes two parameters a and b. It should find the root parents of a and b using the find function and set the parent of b's root to a's root to merge the groups.
DSA Python
Hint
Find the roots of both elements and link one root to the other to merge sets.
4
Use union and find, then print the parent list
Use the union function to merge these pairs: (0, 1), (1, 2), and (3, 4). Then print the parent list after these unions.
DSA Python
Hint
After unions, the parent list shows the root parent for each person. Use print(parent) to display it.
Practice
(1/5)
1. What is the main purpose of path compression in the Union Find data structure?
easy
A. To delete elements from the set efficiently
B. To merge two sets without checking their roots
C. To speed up the find operation by flattening the tree structure
D. To sort the elements in each set
Solution
Step 1: Understand the role of path compression
Path compression is used during the find operation to make future queries faster by making nodes point directly to the root.
Step 2: Identify the effect on the tree structure
This flattens the tree, reducing the height and speeding up subsequent find calls.
Final Answer:
To speed up the find operation by flattening the tree structure -> Option C
Quick Check:
Path compression = speed up find [OK]
Hint: Path compression flattens trees to speed up find [OK]
Common Mistakes:
Confusing path compression with union operation
Thinking it deletes elements
Believing it sorts elements
2. Which of the following is the correct syntax for the find function with path compression in Python?
easy
A. def find(x): parent[x] = x return x
B. def find(x): while parent[x] != x: x = parent[x] return x
C. def find(x): return x if parent[x] == x else find(parent[x])
D. def find(x): if parent[x] != x: parent[x] = find(parent[x]) return parent[x]
Solution
Step 1: Check for path compression in the code
def find(x): if parent[x] != x: parent[x] = find(parent[x]) return parent[x] updates parent[x] to the root during recursion, which is path compression.
Step 2: Verify other options
def find(x): while parent[x] != x: x = parent[x] return x uses iteration but no path compression update. def find(x): return x if parent[x] == x else find(parent[x]) lacks assignment to parent[x]. def find(x): parent[x] = x return x incorrectly resets parent[x] always.
Final Answer:
def find(x): if parent[x] != x: parent[x] = find(parent[x]) return parent[x] -> Option D
Quick Check:
Path compression requires assignment in find [OK]
Hint: Path compression assigns parent[x] during find [OK]
Common Mistakes:
Missing assignment to parent[x]
Using iteration without updating parents
Resetting parent[x] incorrectly
3. Given the following code snippet with path compression, what will be the output of print(parent) after the operations?
find(3) updates parent[3] to root 1 (already 1), so no change. Final parent array: [0,1,1,1,4]
Final Answer:
[0, 1, 1, 1, 4] -> Option A
Quick Check:
Parents after unions and find = [0,1,1,1,4] [OK]
Hint: Trace unions then find with path compression [OK]
Common Mistakes:
Not updating parent during find
Confusing root assignments
Ignoring path compression effect
4. Identify the bug in the following find function that is supposed to implement path compression:
def find(x):
if parent[x] == x:
return x
else:
find(parent[x])
medium
A. Incorrect base case condition
B. Missing return statement in the else branch
C. Parent array is not initialized
D. Using recursion instead of iteration
Solution
Step 1: Check the else branch
The else branch calls find(parent[x]) but does not return its result, so the function returns None.
Step 2: Understand path compression requirement
Without returning the recursive call result, path compression and correct root finding fail.
Final Answer:
Missing return statement in the else branch -> Option B
Quick Check:
Return recursive find result [OK]
Hint: Always return recursive call in find [OK]
Common Mistakes:
Forgetting return in recursion
Misunderstanding base case
Assuming iteration is required
5. You have a Union Find structure with 5 elements (0 to 4). After performing union(0,1), union(1,2), and union(3,4), you call find(2) and find(4) with path compression. Which of the following statements is true about the parent array after these operations?
hard
A. Elements 0, 1, and 2 will have the same root, and 3 and 4 will have the same root, with path compression applied
B. All elements will have different roots because path compression does not merge sets
C. Elements 0 and 4 will share the same root after path compression
D. Path compression will cause all elements to point to element 0
Solution
Step 1: Analyze unions
union(0,1) and union(1,2) merge 0,1,2 into one set. union(3,4) merges 3 and 4 into another set.
Step 2: Effect of find with path compression
find(2) compresses path so 2 points directly to root of set {0,1,2}. find(4) compresses path so 4 points directly to root of set {3,4}.