📖 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: You will build a Union Find data structure with path compression to quickly find the leader (representative) of each friend group. This helps in checking and merging friend groups efficiently.
📋 What You'll Learn
Create a slice called parent representing each person's parent in the friend group.
Create a function find that uses path compression to find the leader of a group.
Create a function union to merge two friend groups.
Demonstrate the union and find operations with sample data.
💡 Why This Matters
🌍 Real World
Union Find with path compression is used in social networks to quickly find connected friend groups and merge them when new friendships form.
💼 Career
Understanding Union Find and path compression is important for jobs involving graph algorithms, network connectivity, and efficient data structure design.
Progress0 / 4 steps
1
Create the initial parent slice
Create a slice called parent of length 5 where each element is initialized to its own index (0 to 4).
DSA Go
Hint
Each person starts as their own leader, so parent[i] = i.
2
Add the find function with path compression
Add a function called find that takes parent slice and an integer x, and returns the leader of x. Use path compression by setting parent[x] to the leader during the search.
DSA Go
Hint
Use recursion to find the leader and update parent[x] to the leader for path compression.
3
Add the union function to merge groups
Add a function called union that takes parent slice and two integers a and b. It should find the leaders of a and b using find and set the leader of b's group to the leader of a's group.
DSA Go
Hint
Find leaders of both elements and link one leader to the other to merge groups.
4
Demonstrate union and find with output
In main, perform union(parent, 0, 1) and union(parent, 1, 2). Then print the leader of each element from 0 to 4 using find in the format: Leader of 0: X.
DSA Go
Hint
Use union to merge groups and find to print leaders for each element.
Practice
(1/5)
1. What is the main purpose of path compression in the Union Find data structure?
easy
A. To speed up the find operation by making nodes point directly to the root
B. To merge two sets by size
C. To initialize the parent array
D. To count the number of connected components
Solution
Step 1: Understand the role of path compression
Path compression is used during the find operation to flatten the tree structure by making nodes point directly to the root.
Step 2: Compare with other operations
Merging sets by size is done in union by size/rank, not path compression. Initialization and counting components are unrelated to path compression.
Final Answer:
To speed up the find operation by making nodes point directly to the root -> Option A
Quick Check:
Path compression optimizes find = To speed up the find operation by making nodes point directly to the root [OK]
Hint: Path compression flattens trees to speed up find [OK]
Common Mistakes:
Confusing path compression with union by size/rank
Thinking it initializes the data structure
Believing it counts components
2. Which of the following is the correct Go syntax for the find function with path compression in Union Find?
easy
A. func find(parent []int, x int) int { for parent[x] != x { x = parent[x] } return x }
B. func find(parent []int, x int) int { parent[x] = x; return x }
C. func find(parent []int, x int) int { if parent[x] == x { return x } else { return find(parent, x) } }
D. func find(parent []int, x int) int { if parent[x] != x { parent[x] = find(parent, parent[x]) } return parent[x] }
Solution
Step 1: Identify path compression in the code
func find(parent []int, x int) int { if parent[x] != x { parent[x] = find(parent, parent[x]) } return parent[x] } recursively updates parent[x] to the root, which is path compression.
Step 2: Check other options for correctness
func find(parent []int, x int) int { for parent[x] != x { x = parent[x] } return x } uses a loop but no path compression. func find(parent []int, x int) int { if parent[x] == x { return x } else { return find(parent, x) } } calls find without updating parent[x]. func find(parent []int, x int) int { parent[x] = x; return x } wrongly resets parent[x] to itself always.
Final Answer:
func find(parent []int, x int) int { if parent[x] != x { parent[x] = find(parent, parent[x]) } return parent[x] } -> Option D
Quick Check:
Recursive update of parent[x] = find(...) = func find(parent []int, x int) int { if parent[x] != x { parent[x] = find(parent, parent[x]) } return parent[x] } [OK]
Hint: Look for recursive parent update for path compression [OK]
Common Mistakes:
Missing parent update in recursion
Using loop without compression
Resetting parent incorrectly
3. What will be the output of the following Go code snippet using Union Find with path compression?
package main
import "fmt"
func find(parent []int, x int) int {
if parent[x] != x {
parent[x] = find(parent, parent[x])
}
return parent[x]
}
func main() {
parent := []int{0, 1, 2, 3, 4}
parent[1] = 0
parent[2] = 1
fmt.Println(find(parent, 2))
fmt.Println(parent)
}
medium
A. 1
[0 0 1 3 4]
B. 0
[0 0 0 3 4]
C. 2
[0 1 2 3 4]
D. 0
[0 1 0 3 4]
Solution
Step 1: Trace the find call for element 2
find(2) calls find(1), which calls find(0). Since parent[0] = 0, root is 0.
Step 2: Apply path compression updates
parent[2] is updated to 0, parent[1] is updated to 0. Final parent array is [0 0 0 3 4].
Final Answer:
0
[0 0 0 3 4] -> Option B
Quick Check:
Root of 2 is 0 and parent array updated = 0
[0 0 0 3 4] [OK]
Hint: Follow recursion and update parents on return [OK]
Common Mistakes:
Not updating parent array after find
Returning intermediate parent instead of root
Confusing indices in parent array
4. Identify the bug in the following Go code implementing find with path compression:
func find(parent []int, x int) int {
if parent[x] == x {
return x
}
parent[x] = find(parent, x)
return parent[x]
}
medium
A. The function should not update parent[x] during recursion
B. The base case should check parent[x] != x instead of parent[x] == x
C. The recursive call should be find(parent, parent[x]) instead of find(parent, x)
D. The function should return x always
Solution
Step 1: Analyze the recursive call argument
The recursive call uses find(parent, x), which causes infinite recursion because x does not change.
Step 2: Correct the recursive call
It should call find(parent, parent[x]) to move up the tree towards the root.
Final Answer:
The recursive call should be find(parent, parent[x]) instead of find(parent, x) -> Option C
Quick Check:
Recursive call must move up tree = The recursive call should be find(parent, parent[x]) instead of find(parent, x) [OK]
Hint: Recursive find must use parent[x], not x [OK]
Common Mistakes:
Calling find with same x causing infinite recursion
Wrong base case condition
Not updating parent[x] for compression
5. Given a Union Find structure with parent = [0, 0, 1, 2, 3], what will be the parent array after calling find(parent, 4) with path compression?