Chapter 13 · Simulation

Disjoint Sets Simulator

← Chapter 13
Elements0
Components0
Max depth0
Parent rewrites0

Example Union-Find forest loaded. Pick an operation.

Find path

—

Last operation

—

Current components

Parent, rank, and size table

ElementParentRootRankSize

Disjoint Sets Simulator

A disjoint sets structure, also called Union-Find, maintains a collection of non-overlapping sets. It answers which component an element belongs to and merges two components cheaply, thanks to path compression and union by rank.

How to use it

  • Make Set adds a new element as a component of its own. Pick two elements and press Union or Connected?.
  • Find Representative walks the find path step by step and then compresses it; Compress All Paths flattens every tree.
  • Each arrow points from an element to its parent. The table lists every element's parent, representative (Root), rank, and component size (Size).

Union-Find ideas

  • Disjoint components: every element belongs to exactly one component, which is represented by a rooted tree of parent links.
  • Path compression: find rewrites the parent links along the search path to point straight at the representative, so later finds are shorter.
  • Union by rank: the root of lower rank goes under the root of higher rank; when the ranks are equal, the new root's rank grows by one.
  • Applications: Kruskal's minimum spanning tree algorithm, connectivity queries, image segmentation, and dynamic grouping.

Terms

TermMeaning
RepresentativeThe root element that identifies a component.
ParentThe next element on the way to the representative.
RankAn upper bound on tree height, used to decide unions.
Component sizeThe number of elements under a root.
α(n)The inverse Ackermann function; with both improvements, each operation costs amortized O(α(n)).