← Back

Disjoint Set Union Simulator

What is Disjoint Set Union?

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

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
        and 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 every parent along the search path to point directly to the representative, flattening future searches.

Union by rank

The shorter estimated tree is attached below the taller one. This prevents unnecessary depth growth.

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.