Bird
Raised Fist0
DSA Goprogramming~10 mins

Path Compression in Union Find in DSA Go - Execution Trace

Choose your learning style10 modes available
Concept Flow - Path Compression in Union Find
Start: Find(x)
↓
Is parent[x
↓
Return x
↓
Set parent[x
↓
Return root
Find the root of x, and update x's parent directly to root to flatten the tree.
Execution Sample
DSA Go
func find(x int) int {
  if parent[x] != x {
    parent[x] = find(parent[x])
  }
  return parent[x]
}
This function finds the root of x and compresses the path by pointing x directly to the root.
Execution Table
StepOperationNode xparent[x] beforeRecursive callparent[x] afterVisual State
1Call find(4)43find(3)?parent[4] points to 3
2Call find(3)31find(1)?parent[3] points to 1
3Call find(1)11No recursive call1parent[1] points to itself (root)
4Return from find(1)11-1No change
5Set parent[3] = 131-1parent[3] updated to 1 (no change)
6Return from find(3)31-1No change
7Set parent[4] = 143-1parent[4] updated to 1 (path compressed)
8Return from find(4)41-1No change
9Final root returned41-1Root of 4 is 1
💡 Recursion ends when parent[x] == x (root found), then path compression updates parents on return.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 7Final
parent[4]333311
parent[3]111111
parent[1]111111
Key Moments - 3 Insights
Why do we update parent[x] after the recursive call?
Because after finding the root, we set parent[x] directly to root to flatten the tree, as shown in steps 7 and 8 in the execution_table.
What stops the recursion in find?
Recursion stops when parent[x] == x, meaning x is the root. This is shown in step 3 where find(1) returns immediately.
Why does parent[4] change but parent[3] stays the same?
parent[3] was already pointing to root 1, so no change needed (step 5). parent[4] pointed to 3, so it updates to 1 to compress the path (step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is parent[4] after step 7?
A4
B3
C1
D0
💡 Hint
Check the 'parent[x] after' column at step 7 in the execution_table.
At which step does the recursion stop because parent[x] == x?
AStep 3
BStep 1
CStep 5
DStep 7
💡 Hint
Look for the step where 'No recursive call' happens in the 'Recursive call' column.
If parent[3] was initially 2 instead of 1, what would change in the execution_table?
ARecursion would stop earlier
BMore recursive calls to find(2) would happen
Cparent[4] would not change
Dparent[1] would change
💡 Hint
Consider how find calls depend on parent[x] values shown in the 'Recursive call' column.
Concept Snapshot
Path Compression in Union Find:
- find(x) returns root of x
- If parent[x] != x, recursively find root
- Update parent[x] = root to flatten tree
- Speeds up future find calls
- Key: recursion + pointer update
Full Transcript
Path Compression in Union Find is a technique to speed up the find operation by making nodes point directly to the root. The find function checks if a node is its own parent (root). 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 find(4) calls find(3), which calls find(1). Since 1 is root, recursion stops. Then parent[3] and parent[4] are updated to 1, compressing the path. This process improves efficiency by shortening paths in the union-find data structure.

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

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

    To speed up the find operation by making nodes point directly to the root -> Option A
  4. 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

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

    func find(parent []int, x int) int { if parent[x] != x { parent[x] = find(parent, parent[x]) } return parent[x] } -> Option D
  4. 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

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

    0 [0 0 0 3 4] -> Option B
  4. 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

  1. Step 1: Analyze the recursive call argument

    The recursive call uses find(parent, x), which causes infinite recursion because x does not change.
  2. Step 2: Correct the recursive call

    It should call find(parent, parent[x]) to move up the tree towards the root.
  3. Final Answer:

    The recursive call should be find(parent, parent[x]) instead of find(parent, x) -> Option C
  4. 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?
hard
A. [0, 0, 0, 0, 0]
B. [0, 0, 1, 2, 0]
C. [0, 1, 1, 2, 3]
D. [0, 0, 1, 3, 4]

Solution

  1. Step 1: Trace the find call for element 4

    find(4) calls find(3), find(3) calls find(2), find(2) calls find(1), find(1) calls find(0). Root is 0.
  2. Step 2: Apply path compression updates

    During recursion unwind: parent[1] = 0 (already), parent[2] = 0, parent[3] = 0, parent[4] = 0. Final parent array is [0, 0, 0, 0, 0].
  3. Final Answer:

    [0, 0, 0, 0, 0] -> Option A
  4. Quick Check:

    All nodes on path updated to root 0 = [0, 0, 0, 0, 0] [OK]
Hint: Update only nodes on path to root during find [OK]
Common Mistakes:
  • Only updating parent[4] to root
  • Failing to compress the full path
  • Confusing parent indices
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