Skip to content
Optimal Lab

DSA · interactive

Union-find: connected groups

Union-find tracks which items are connected as edges arrive. Each group has a root; two items are connected if they share one. Merging attaches one root under the other. It answers “are these connected?” almost instantly.

Edges

  1. 0–1
  2. 2–3
  3. 1–3
  4. 4–5
  5. 6–7
  6. 5–7
  7. 0–2
  8. 4–6

8 groups

root 0

0

root 1

1

root 2

2

root 3

3

root 4

4

root 5

5

root 6

6

root 7

7

parent[]

00d011d022d033d044d055d066d077d0

Every node starts as its own group: its parent is itself.

edge 0 of 8
  1. 1

    Each item points at a parent. The root points at itself and names the group.

    while (parent[x] !== x) x = parent[x]

  2. 2

  3. 3

  4. 4

In production

Tools and technologies

Union-find shows up in graphs, images and compilers.

Not tied to one language — use these names when you search docs or design a system.