DrawCode Algorithm Visualizer • Union-Find • Medium

Path Compression

Tags: Union-Find, Path Compression, Inverse Ackermann, DSU

1. One-Liner

Path compression makes find flatten the tree: every visited node’s parent becomes the root directly.


2. The Problem It Solves

After many unions, paths get long; path compression shortens future finds—this is what gives inverse-Ackermann amortized complexity with rank.


3. The Core Idea

Recursive or iterative find: set parent[x] = find(parent[x]) before returning; first traversal pays, later hops are O(1) to root.


4. How It Works (Step-by-Step)

StepAction
1On find(x), if parent[x]!=x, recurse/iterate
2Set parent[x] to root after finding root
3Return root id
4Combine with union-by-rank for theory bounds

5. Dry Run Example

Chain 0→1→2→3: first find(0) walks and flattens so 0,1,2 point to 3; next find(0) is one hop.


6. Key Properties

PropertyDetail
Amortizedα(n) with rank
Single findStill proportional to old path length once
IterativePath halving variant also exists

7. Where It Is Used

Domain / SystemUse
MST / connectivityIndustry DSU
Image labelingUnion pixels
Social graphsComponent queries

8. Interview Tips

Mention two-pass iterative find if avoiding recursion stack. Pair with union by rank for standard proof.


9. Comparison with Other Algorithms

TechniqueNotes
No compressionWeaker bounds
Path splittingOlder heuristic
Link-cut treeDynamic trees beyond DSU

10. Complexity

----
TimeO(α(n)) amortized find/union together
SpaceO(n)

Implementation Example (PYTHON)

class UnionFind:
    def __init__(self, n):
        self.p = list(range(n))
        self.r = [0] * n

    def find(self, x):
        if self.p[x] != x:
            self.p[x] = self.find(self.p[x])
        return self.p[x]

    def union(self, a, b):
        ra, rb = self.find(a), self.find(b)
        if ra == rb:
            return False
        if self.r[ra] < self.r[rb]:
            ra, rb = rb, ra
        self.p[rb] = ra
        if self.r[ra] == self.r[rb]:
            self.r[ra] += 1
        return True

Interactive Visualizer Workspace

Explore step-by-step interactive animations, memory state tracking, and live multi-language execution in DrawCode.

Launch Interactive Visualizer