📖 Scenario: Imagine you have a group of friends and you want to quickly find out if two people are in the same friend circle. We use a data structure called Union Find to help with this. Path Compression is a technique that makes finding the leader of a friend circle faster by shortening the path to the leader.
🎯 Goal: You will build a simple Union Find structure with path compression to quickly find the leader of any friend circle.
📋 What You'll Learn
Create an array called parent to represent each person's leader.
Create a function called find that uses path compression to find the leader of a person.
Create a function called union_sets to join two friend circles.
Print the leader of each person after some unions.
💡 Why This Matters
🌍 Real World
Union Find with path compression is used in social networks to quickly find connected groups, in network connectivity, and in clustering problems.
💼 Career
Understanding Union Find and path compression is important for software engineers working on graph algorithms, network design, and optimization problems.
Progress0 / 4 steps
1
Create the parent array
Create an integer array called parent of size 5 where each element is initialized to its own index (0 to 4).
DSA C++
Hint
Use a for loop from 0 to 4 and set parent[i] = i.
2
Write the find function with path compression
Write a function called find that takes an integer x and returns the leader of x. Use path compression by setting parent[x] to the leader during the search.
DSA C++
Hint
Check if parent[x] is not x. If so, recursively find the leader and update parent[x].
3
Write the union_sets function
Write a function called union_sets that takes two integers a and b. Find their leaders using find and set the leader of b's group to the leader of a's group.
DSA C++
Hint
Find leaders of a and b. If different, set parent[rootB] to rootA.
4
Print leaders after unions
Call union_sets(0, 1) and union_sets(1, 2). Then print the leader of each person from 0 to 4 using find. Use std::cout to print each leader on a new line.
DSA C++
Hint
After unions, the leader of 0, 1, and 2 should be the same. Print each leader on its own line.
Practice
(1/5)
1. What is the main purpose of path compression in the Union Find data structure?
easy
A. To merge two sets by size
B. To speed up the find operation by making nodes point directly to the root
C. To initialize the parent array
D. To count the number of connected components
Solution
Step 1: Understand the find operation
The find operation locates the root parent of a node in Union Find.
Step 2: Role of path compression
Path compression updates nodes along the path to point directly to the root, reducing future search time.
Final Answer:
To speed up the find operation by making nodes point directly to the root -> Option B
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 merges sets instead of optimizing find
Assuming it initializes data structures
2. Which of the following is the correct syntax for the find function with path compression in C++?
easy
A. int find(int x) { if(parent[x] != x) parent[x] = find(parent[x]); return parent[x]; }
B. int find(int x) { while(parent[x] != x) x = parent[x]; return x; }
C. int find(int x) { return parent[x]; }
D. int find(int x) { if(parent[x] == x) return x; else return find(parent[x]); }
Solution
Step 1: Identify path compression usage
Path compression updates parent[x] during recursion to root.
find(2): compresses path for 2 to 0 (already 0); find(5): compresses path for 5 to 3 (already 3); unionSet(2,5): parent[3]=find(2)=0; find(5): compresses path for 5 to 0, so parent[5]=0;