Bird
Raised Fist0
DSA Pythonprogramming~10 mins

Path Compression in Union Find in DSA Python - Execution Trace

Choose your learning style10 modes available
Concept Flow - Path Compression in Union Find
Find node's parent
↓
Is parent itself?
↓
Return parent
↓
Update node's parent to root
↓
Return root
This flow shows how path compression finds the root parent of a node and updates the node's parent directly to the root to flatten the structure.
Execution Sample
DSA Python
def find(parent, x):
    if parent[x] != x:
        parent[x] = find(parent, parent[x])
    return parent[x]
This code finds the root parent of x and compresses the path by making x point directly to the root.
Execution Table
StepOperationNodeParent BeforeRecursive CallParent AfterVisual State
1Call find4parent[4] = 3find(3)parent[4] updated to root4->3->1->1
2Call find3parent[3] = 1find(1)parent[3] updated to root3->1->1
3Call find1parent[1] = 1No recursive callparent[1] unchanged1->1
4Return from find(1)1parent[1] = 1None11->1
5Return from find(3)3parent[3] = 1None13->1
6Return from find(4)4parent[4] = 3None14->1
7Final parent array-[0,1,2,1,1]--0->0,1->1,2->2,3->1,4->1
💡 All recursive calls return after updating parents to root, path compression complete.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
parent[0,1,2,1,3][0,1,2,1,3][0,1,2,1,3][0,1,2,1,3][0,1,2,1,1]
x44311
Key Moments - 3 Insights
Why does the parent of node 4 change from 3 to 1 during execution?
Because find(4) calls find(3), which returns 1 as root. Then parent[4] is updated to 1 to compress the path, as shown in execution_table rows 1 and 6.
What happens when a node is its own parent?
The recursion stops and the node is returned as root without changes, as seen in execution_table row 3.
Why is path compression important?
It flattens the tree by making nodes point directly to root, speeding up future find operations, demonstrated by parent updates in rows 5 and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the parent of node 1?
A3
B4
C1
D0
💡 Hint
Check the 'Parent Before' column at step 3 in execution_table.
At which step does the parent of node 4 get updated to the root?
AStep 2
BStep 6
CStep 4
DStep 1
💡 Hint
Look at the 'Parent After' and 'Operation' columns for node 4 in execution_table.
If node 3's parent was initially 2 instead of 1, how would the final parent of node 4 change?
AIt would be 2
BIt would remain 1
CIt would be 3
DIt would be 4
💡 Hint
Consider the recursive calls and path compression shown in execution_table and variable_tracker.
Concept Snapshot
Path Compression in Union Find:
- Used in find operation to flatten tree.
- Recursively find root parent.
- Update each node's parent to root on return.
- Speeds up future finds by shortening paths.
- Syntax: if parent[x]!=x: parent[x]=find(parent,parent[x])
Full Transcript
Path Compression in Union Find helps speed up the find operation by making nodes point directly to their root parent. 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 tree structure, reducing the time for future finds. The execution trace shows how node 4's parent changes from 3 to 1 after recursive calls, compressing the path. Key moments include understanding when recursion stops and why parents update. The visual quiz tests understanding of parent updates and effects of initial parent changes.

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

  1. 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.
  2. Step 2: Identify the effect on the tree structure

    This flattens the tree, reducing the height and speeding up subsequent find calls.
  3. Final Answer:

    To speed up the find operation by flattening the tree structure -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    def find(x): if parent[x] != x: parent[x] = find(parent[x]) return parent[x] -> Option D
  4. 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?
parent = [0, 1, 2, 3, 4]
def find(x):
    if parent[x] != x:
        parent[x] = find(parent[x])
    return parent[x]
def union(a, b):
    rootA = find(a)
    rootB = find(b)
    if rootA != rootB:
        parent[rootB] = rootA

union(1, 2)
union(2, 3)
find(3)
print(parent)
medium
A. [0, 1, 1, 1, 4]
B. [0, 1, 2, 2, 4]
C. [0, 1, 1, 2, 4]
D. [0, 1, 3, 3, 4]

Solution

  1. Step 1: Perform unions and track parents

    union(1,2): parent[2] = 1; union(2,3): parent[3] = find(2) = 1, so parent[3] = 1.
  2. Step 2: Apply find(3) with path compression

    find(3) updates parent[3] to root 1 (already 1), so no change. Final parent array: [0,1,1,1,4]
  3. Final Answer:

    [0, 1, 1, 1, 4] -> Option A
  4. 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

  1. Step 1: Check the else branch

    The else branch calls find(parent[x]) but does not return its result, so the function returns None.
  2. Step 2: Understand path compression requirement

    Without returning the recursive call result, path compression and correct root finding fail.
  3. Final Answer:

    Missing return statement in the else branch -> Option B
  4. 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

  1. 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.
  2. 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}.
  3. Step 3: Confirm roots and parent array

    Sets remain separate; 0,1,2 share root; 3,4 share root; path compression flattens trees.
  4. Final Answer:

    Elements 0, 1, and 2 will have the same root, and 3 and 4 will have the same root, with path compression applied -> Option A
  5. Quick Check:

    Unions merge sets; path compression flattens but does not merge sets [OK]
Hint: Unions merge sets; path compression flattens trees only [OK]
Common Mistakes:
  • Thinking path compression merges sets
  • Assuming all elements share one root
  • Confusing union and find roles
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard