📖 Scenario: Imagine you are managing groups of friends at a party. You want to keep track of which friends belong to the same group. When two friends meet, their groups merge. You want to quickly find out if two friends are in the same group.
🎯 Goal: Build a simple disjoint set (union-find) structure to manage friend groups. You will create the initial groups, set up a helper structure, perform unions to merge groups, and complete the structure to find group leaders.
📋 What You'll Learn
Create a dictionary called parent where each friend is their own parent initially
Create a dictionary called rank to keep track of tree depth for each friend
Write a function find that returns the leader (root parent) of a friend
Write a function union that merges two friend groups by their leaders
💡 Why This Matters
🌍 Real World
Disjoint sets are used in social networks to find connected friend groups, in computer networks to detect connected components, and in image processing to group pixels.
💼 Career
Understanding disjoint sets is important for software engineers working on graph algorithms, network connectivity, clustering, and optimization problems.
Progress0 / 4 steps
1
Create initial parent dictionary
Create a dictionary called parent with keys 'Alice', 'Bob', 'Charlie', 'Diana', and 'Eve'. Set each friend's parent to themselves as a string value.
Data Structures Theory
Hint
Each friend starts as their own group leader, so their parent is themselves.
2
Create rank dictionary
Create a dictionary called rank with the same keys as parent. Set each friend's rank to 0 as an integer.
Data Structures Theory
Hint
Rank helps keep the tree shallow by tracking the depth of each group leader's tree.
3
Write the find function
Write a function called find that takes a friend's name as input and returns the root parent of that friend by following the parent links until the friend is their own parent.
Data Structures Theory
Hint
Keep following the parent until you find a friend who is their own parent.
4
Write the union function
Write a function called union that takes two friends, finds their root parents using find, and merges their groups by attaching the smaller rank tree under the larger rank tree. If ranks are equal, attach the second to the first and increase the first's rank by 1.
Data Structures Theory
Hint
Compare ranks to decide which root becomes the parent. Increase rank if both are equal.
Practice
(1/5)
1. What is the main purpose of a Disjoint Set (Union-Find) data structure?
easy
A. To sort elements in ascending order
B. To store key-value pairs for fast lookup
C. To efficiently keep track of which elements belong to which groups
D. To perform arithmetic calculations on sets
Solution
Step 1: Understand the role of Disjoint Set
Disjoint Set is designed to manage groups of elements and quickly find which group an element belongs to.
Step 2: Compare with other options
Sorting, key-value storage, and arithmetic are unrelated to Disjoint Set's main function.
Final Answer:
To efficiently keep track of which elements belong to which groups -> Option C
Quick Check:
Disjoint Set = group tracking [OK]
Hint: Disjoint Set = fast group membership check [OK]
Common Mistakes:
Confusing Disjoint Set with sorting algorithms
Thinking it stores key-value pairs like a dictionary
Assuming it performs arithmetic on sets
2. Which of the following is the correct way to perform a union operation between two sets containing elements a and b in a Disjoint Set?
easy
A. merge(a, b)
B. union(a, b)
C. unionSet(a, b)
D. combine(a, b)
Solution
Step 1: Identify standard Disjoint Set operation names
The common and standard name for merging two sets is union.
Step 2: Check other options
Other names like unionSet, merge, or combine are not standard and may cause confusion.
Final Answer:
union(a, b) -> Option B
Quick Check:
Union operation = union(a, b) [OK]
Hint: Union operation is always called 'union' [OK]
Common Mistakes:
Using non-standard method names
Confusing union with find
Assuming merge or combine are standard
3. Consider the following sequence of operations on a Disjoint Set with elements {1, 2, 3, 4}:
A. The assignment does not update the parent of rootA
B. The find function is called incorrectly
C. The union function should return a value
D. The if condition should check rootA == rootB
Solution
Step 1: Analyze the union operation
The union operation should update the parent of one root to the other to merge sets.
Step 2: Identify the error in assignment
Assigning rootA = rootB only changes the local variable, not the parent array that tracks sets.
Final Answer:
The assignment does not update the parent of rootA -> Option A
Quick Check:
Union must update parent array, not local variables [OK]
Hint: Update parent array, not local variables [OK]
Common Mistakes:
Assigning roots instead of updating parent array
Confusing equality check in if condition
Expecting union to return a value
5. You have 5 elements {1, 2, 3, 4, 5} initially in separate sets. After performing union(1, 2), union(3, 4), and union(2, 3), which of the following statements is true?