0
0
DSA Typescriptprogramming~10 mins

Path Compression in Union Find in DSA Typescript - 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 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.