x:y:
1x
Union-Find / DSU Visualizer

Union-Find — DSU Operations

Step 1 of 6

Disjoint Set Union (DSU), also called Union-Find, is a data structure that tracks a set of elements partitioned into disjoint subsets. Two key operations run in near-O(1) amortized time thanks to Path Compression and Union by Rank.

Find — Path Compression

Find(x) returns the root representative of x's component. Path Compression flattens the tree: every node on the path from x to root is directly attached to the root, making future finds O(1).

Before Find(3)
3 → 2 → 1 → 0 (deep chain)
Walk to root
path = [3, 2, 1], root = 0
After compression
3 → 0, 2 → 0, 1 → 0 (flat!)