Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Path Compression in Union Find in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Path Compression in Union Find
Start: Find(x)
↓
Is parent[x
No↓
Recursively find parent of parent[x
↓
Set parent[x
↓
Return root
Path Compression flattens the tree by making each node point directly to the root during find operation.
Execution Sample
DSA Typescript
function find(x: number): number {
  if (parent[x] !== x) {
    parent[x] = find(parent[x]);
  }
  return parent[x];
}
This code 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)TBDparent[4] points to 3
2Call find(3)31find(1)TBDparent[3] points to 1
3Call find(1)11No recursion1parent[1] points to itself (root)
4Return from find(1)31-1parent[3] updated to 1 (path compressed)
5Return from find(3)43-1parent[4] updated to 1 (path compressed)
6Final return41-1Root of 4 is 1, path compressed
7Call find(5)55No recursion5parent[5] points to itself (root)
8Final return55-5Root of 5 is 5
💡 Recursion stops when parent[x] == x, meaning root is found.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 4After Step 5Final
parent[4]333311
parent[3]111111
parent[1]111111
parent[5]555555
Key Moments - 3 Insights
Why does find(4) call find(3) before updating parent[4]?
Because parent[4] is 3, which is not root, so we must find root of 3 first (see steps 1 and 2 in execution_table).
Why is parent[3] updated to 1 after find(1) returns?
Path compression sets parent[3] directly to root 1 to flatten the tree (see step 4).
What stops the recursion in find function?
When parent[x] equals x, meaning x is root, recursion stops (see step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is parent[4] after step 5?
A3
B4
C1
D5
💡 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 1
BStep 3
CStep 5
DStep 7
💡 Hint
Look for 'No recursion' in the 'Recursive call' column in execution_table.
If parent[4] was initially 4, how many recursive calls would find(4) make?
A0
B1
C2
D3
💡 Hint
If parent[4] == 4, recursion stops immediately (see variable_tracker start values).
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 compress path.
- Speeds up future find operations.
- Stops recursion when parent[x] == x.
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 the node is its own parent (root). If not, it recursively finds the root of its parent and updates the node's parent to this root. This flattens the tree structure, reducing the time for future finds. The recursion stops when a node is its own parent. The execution table shows step-by-step how parent pointers change during find(4) and find(5). Variable tracker shows parent array changes. Key moments clarify why recursion happens and when it stops. The visual quiz tests understanding of parent updates and recursion stopping conditions.

Practice

(1/5)
1. What is the main purpose of path compression in a Union Find data structure?
easy
A. To sort elements inside each set
B. To speed up the find operation by flattening the tree
C. To delete elements from the set efficiently
D. To merge two sets without checking their roots

Solution

  1. Step 1: Understand the find operation

    The find operation locates the root of an element's set in Union Find.
  2. Step 2: Role of path compression

    Path compression flattens the tree by making nodes point directly to the root, speeding up future find calls.
  3. Final Answer:

    To speed up the find operation by flattening the tree -> Option B
  4. Quick Check:

    Path compression = faster find [OK]
Hint: Path compression flattens trees to speed up find [OK]
Common Mistakes:
  • Confusing path compression with union operation
  • Thinking it deletes elements
  • Assuming it sorts elements
2. Which of the following is the correct TypeScript syntax for the find function with path compression in Union Find?
easy
A. function find(x: number): number { if (parent[x] !== x) parent[x] = find(parent[x]); return parent[x]; }
B. function find(x: number): number { while (parent[x] !== x) x = parent[x]; return x; }
C. function find(x: number): number { if (parent[x] === x) return x; else return find(parent[x]); }
D. function find(x: number): number { parent[x] = x; return x; }

Solution

  1. Step 1: Identify path compression in code

    Path compression updates parent[x] to root during recursion.
  2. Step 2: Check each option

    function find(x: number): number { if (parent[x] !== x) parent[x] = find(parent[x]); return parent[x]; } updates parent[x] recursively, implementing path compression correctly.
  3. Final Answer:

    function find(x: number): number { if (parent[x] !== x) parent[x] = find(parent[x]); return parent[x]; } -> Option A
  4. Quick Check:

    Path compression updates parent[x] recursively [OK]
Hint: Look for recursive parent update in find [OK]
Common Mistakes:
  • Missing parent update for path compression
  • Using loop without updating parent
  • Returning without compression
3. Given the following TypeScript code snippet, what will be the output of console.log(parent) after calling find(3)?
let parent = [0, 0, 1, 2, 4];
function find(x: number): number {
  if (parent[x] !== x) parent[x] = find(parent[x]);
  return parent[x];
}
find(3);
console.log(parent);
medium
A. [0, 0, 1, 2, 4]
B. [0, 1, 1, 1, 4]
C. [0, 0, 2, 2, 4]
D. [0, 0, 0, 0, 4]

Solution

  1. Step 1: Trace find(3) calls

    find(3) checks parent[3] = 2, calls find(2). find(2) checks parent[2] = 1, calls find(1). find(1) checks parent[1] = 0, calls find(0). find(0) returns 0 since parent[0] = 0.
  2. Step 2: Apply path compression updates

    Unwinding: parent[1] = 0 (already), returns 0; parent[2] = 0, returns 0; parent[3] = 0, returns 0. Final parent = [0, 0, 0, 0, 4].
  3. Final Answer:

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

    Path compression sets all path nodes to root [OK]
Hint: Follow recursive calls and update parents on return [OK]
Common Mistakes:
  • Not updating parent array after recursion
  • Confusing indices and values
  • Assuming all parents become root immediately
4. Identify the bug in this TypeScript find function implementing path compression:
function find(x: number): number {
  if (parent[x] === x) return x;
  find(parent[x]);
  return parent[x];
}
medium
A. Missing assignment to parent[x] for path compression
B. Incorrect base case condition
C. Function should return void
D. Using recursion without a return statement

Solution

  1. Step 1: Analyze recursion and return

    The function calls find(parent[x]) but does not assign its result back to parent[x].
  2. Step 2: Identify missing path compression update

    Without parent[x] = find(parent[x]), path compression does not happen, so the tree is not flattened.
  3. Final Answer:

    Missing assignment to parent[x] for path compression -> Option A
  4. Quick Check:

    Path compression requires updating parent[x] [OK]
Hint: Ensure recursive find result is assigned to parent[x] [OK]
Common Mistakes:
  • Forgetting to assign recursive call result
  • Returning without recursion
  • Wrong base case logic
5. You have a Union Find with parent = [0, 0, 1, 2, 3, 5]. After performing union(4, 5) and then find(5) with path compression, what will be the final parent array?
hard
A. [0, 0, 1, 2, 3, 4]
B. [0, 0, 1, 2, 4, 4]
C. [0, 0, 0, 0, 0, 0]
D. [0, 0, 1, 2, 3, 0]

Solution

  1. Step 1: Perform union(4, 5)

    Initial parent = [0, 0, 1, 2, 3, 5]. Simple union links by setting parent[5] = 4. Now parent = [0, 0, 1, 2, 3, 4].
  2. Step 2: Call find(5) with path compression

    find(5): parent[5]=4 -> find(4): parent[4]=3 -> find(3)=2 -> find(2)=1 -> find(1)=0 -> find(0)=0.
  3. Step 3: Update parent array after compression

    Unwinding sets parent[4]=0, parent[3]=0, parent[2]=0, parent[1]=0, but parent[5] remains 4 because find(5) returns 0 but parent[5] was set to 4 and not updated in this code snippet. So after path compression, parent array is [0, 0, 1, 2, 0, 4] if path compression is applied only on the path from 5 to root. However, since find(5) calls find(4), which calls find(3), etc., and path compression updates parent[x] = find(parent[x]) recursively, parent[5] should be updated to 0 as well. So final parent array is [0, 0, 0, 0, 0, 0].
  4. Final Answer:

    [0, 0, 0, 0, 0, 0] -> Option C
  5. Quick Check:

    Union sets and compress path update parents [OK]
Hint: Union sets then compress path to update parents [OK]
Common Mistakes:
  • Not updating parent after union
  • Ignoring path compression updates
  • Assuming parent stays same after find
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