0
0
DSA Cprogramming~10 mins

Path Compression in Union Find in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Path Compression in Union Find
Start: Find(x)
Is parent[x
No
Recursively find parent[x
Set parent[x
Return root
The find operation checks if a node is its own parent. If not, it recursively finds the root and updates the node's parent to the root to flatten the structure.
Execution Sample
DSA C
int find(int x) {
  if (parent[x] != x) {
    parent[x] = find(parent[x]);
  }
  return parent[x];
}
This code finds the root of x and compresses the path by making x point directly to the root.
Execution Table
StepOperationNode xparent[x] beforeRecursive Callparent[x] afterVisual State
1Call find(4)43find(3)TBD4->3->1->1
2Call find(3)31find(1)TBD4->3->1->1
3Call find(1)11No recursion14->3->1->1
4Set parent[3] = 131Return 114->3->1->1
5Set parent[4] = 143Return 114->1->1->1
6Return root for find(4)41Return 114->1->1->1
7Final parent array----parent = [0,1,2,1,1]
💡 Recursion ends when node is its own parent; path compression updates intermediate parents to root.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 5Final
parent[4]333311
parent[3]111111
parent[1]111111
Key Moments - 3 Insights
Why does find(4) call find(3) before returning?
Because parent[4] is 3, not 4 itself, so we must find the root of 3 first (see execution_table step 1 and 2).
How does path compression update parent pointers?
After finding the root, each node on the path sets its parent directly to the root (see steps 4 and 5 in execution_table).
Why does recursion stop when parent[x] == x?
Because that means x is the root of its set, so no further recursion is needed (step 3 in execution_table).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is parent[4] after step 5?
A3
B4
C1
D0
💡 Hint
Check the 'parent[x] after' column for step 5 in execution_table.
At which step does the recursion stop because parent[x] == x?
AStep 2
BStep 3
CStep 1
DStep 5
💡 Hint
Look for 'No recursion' in the Recursive Call column in execution_table.
If parent[4] was initially 4 (itself), what would happen when find(4) is called?
ANo recursion, find returns 4 immediately
BRecursion would continue deeper
Cparent[4] would be updated to 1
DAn error would occur
💡 Hint
Refer to the condition 'parent[x] == x' stopping recursion in concept_flow.
Concept Snapshot
Path Compression in Union Find:
- Used in find(x) to flatten tree.
- If parent[x] != x, recursively find root.
- Set parent[x] = root to speed future finds.
- Reduces tree height, improves efficiency.
- Key for almost O(1) amortized find operations.
Full Transcript
Path Compression in Union Find improves the find operation by making each node on the path point directly to the root. The find function checks if a node is its own parent; if not, it recursively finds the root and updates the node's parent to that root. This flattens the structure, making future finds faster. The execution trace shows find(4) calling find(3), which calls find(1), the root. Then parent pointers of 3 and 4 are updated to 1. This process stops recursion when a node is its own parent. The variable tracker shows how parent pointers change step-by-step. Key moments clarify why recursion happens and how path compression updates parents. The quiz tests understanding of parent updates and recursion stopping conditions.