Skip to content
DSA Visualizerpreview

Union-find (disjoint sets)

Keep groups as trees; joining two groups points one root at the other, and finding flattens the path.

almost O(1) amortised per operation time · O(n) space

Unions0-1, 2-3, 4-5, 1-3, 5-6, 3-6, 0-6

8 separate sets: every node is its own root.

8 sets; each node points to its parent; roots point to themselves.
  • compare
  • swap or write
  • settled

1 / 25

Speed

Space play · ← → step · Home End jump · drag the strip to scrub