Disjoint Set Union Simulator

Disjoint Set Union—also called Union-Find—maintains a collection of non-overlapping sets. It answers which component an element belongs to and efficiently merges two components using path compression and union by rank.

Make SetO(1)
FindAmortized O(α(n))
UnionAmortized O(α(n))
OptimizationsRank + compression

Union-Find operations

400 ms

Example Union-Find forest loaded. Choose an operation.

Elements0
Components0
Maximum depth0
Parent rewrites0

Find path

Last operation

Current components

Parent, rank, and size table

ElementParentRepresentativeRankComponent size

Optimized operations

find(x):
  if parent[x] != x:
    parent[x] = find(parent[x])
  return parent[x]

union(a, b):
  rootA = find(a)
  rootB = find(b)

  attach lower-rank root
  below higher-rank root

  if ranks are equal:
    choose one root
    increase its rank

Union-Find concepts

Disjoint components

Every element belongs to exactly one component. Each component is represented by a rooted parent tree.

Path compression

Find rewrites parent links along the search path to point directly to the representative, flattening future searches.

Union by rank

The lower-rank root is attached below the higher-rank root. Equal ranks require one rank increment.

Typical applications

Union-Find is used in Kruskal's minimum spanning tree algorithm, connectivity queries, image segmentation, and dynamic grouping.

Terminology

TermMeaning
RepresentativeThe root element that identifies a component.
ParentThe next element followed toward the representative.
RankAn upper-bound estimate of tree height used for merging.
Component sizeThe number of elements represented by a root.
α(n)The inverse Ackermann function, which grows extremely slowly.