J · Specialist · 12 min

Union-Find: Parent, Rank, Path Compression

Disjoint-set union answers 'are these in the same component?' while edges arrive online. Parent pointers plus rank (or size) plus path compression make almost-O(1) finds.

BFS and DFS compute components when you already have the whole graph. Union-find (DSU) maintains components while you union edges one at a time and find representatives in between.

Each node points at a parent. A root points at itself. find(x) walks to the root — that root is the component id. union(a, b) finds both roots and hangs one under the other. Two nodes are connected iff find(a) === find(b).

DSU with union by rank and path compressionjs
function createDsu(n) {
  const parent = Array.from({ length: n }, (_, i) => i);
  const rank = Array(n).fill(0);
  function find(x) {
    while (parent[x] !== x) {
      parent[x] = parent[parent[x]]; // compress one hop
      x = parent[x];
    }
    return x;
  }
  function union(a, b) {
    a = find(a);
    b = find(b);
    if (a === b) return false; // already same component
    if (rank[a] < rank[b]) [a, b] = [b, a];
    parent[b] = a;
    if (rank[a] === rank[b]) rank[a]++;
    return true;
  }
  return { find, union };
}

TRACE

n = 5 nodes 0..4, all roots. Union (1,2), (3,4), (2,3), then find(1) vs find(4).

Start

parent = [0,1,2,3,4]. Five components.

00
11
22
33
44

everyone is a root

1 / 5

Queries. 'Are u and v connected after the first k roads?' Sort the roads, union in order, find. 'Add this edge unless it makes a cycle' — union returning false means a cycle in an undirected graph (Kruskal's reject step). Counting components: start at n, subtract one on every successful merge.

DSU does not give distances or shortest paths. It only knows sameness of component. For unweighted distance, go back to BFS.

CHECK

After several unions, find(a) === find(b). What can you conclude?

CHECK

Why compress the parent pointer inside find?

Checks 0/2

Next lesson