Computer Science

Union-Find (Disjoint Set)

Visualize how the Union-Find data structure efficiently tracks and merges disjoint sets.

Union-Find (Disjoint Set)

Concept Overview

The Union-Find data structure, also known as a disjoint-set data structure, is a powerful tool used to keep track of a set of elements partitioned into a number of disjoint (non-overlapping) subsets. It provides near-constant-time operations to add new sets, to merge existing sets, and to determine whether elements are in the same set. This makes it incredibly efficient for tasks such as finding connected components in a network or cycle detection in graphs.

Mathematical Definition

A disjoint-set data structure maintains a collection S = {S1, S2, ..., Sk} of disjoint dynamic sets. Each set is identified by a representative, which is some member of the set. The two core operations are:

1. Find(x): Returns the representative of the set containing element x.
2. Union(x, y): Merges the two sets that contain elements x and y into a single set.

Time Complexity (with Path Compression and Union by Rank):
Amortized O(α(N)) per operation, where N is the number of elements and α is the inverse Ackermann function.
For all practical values of N, α(N) ≤ 4, making it effectively O(1).

Space Complexity: O(N) to store the parent array and rank/size array.

Key Concepts

  • Tree Representation: Sets are typically represented as rooted trees, where each node points to its parent. The root of the tree serves as the representative of the set.
  • Path Compression: An optimization for the Find operation. When executing Find(x), we traverse the path from x to the root. Path compression flattens the tree by making every node on this path point directly to the root, significantly speeding up future queries.
  • Union by Rank / Size: An optimization for the Union operation. To prevent trees from becoming too deep, we attach the root of the smaller tree to the root of the larger tree. This keeps the trees relatively flat. Rank is an upper bound on the height of the tree.

Historical Context

The disjoint-set data structure was first described by Bernard A. Galler and Michael J. Fisher in 1964. The precise analysis of its time complexity was a major challenge in computer science for many years. In 1975, Robert Tarjan proved the upper bound of O(m α(n)) on the time required for m operations on n elements, introducing the inverse Ackermann function to algorithmic complexity analysis. This result was later shown to be optimal by Fredman and Saks in 1989.

Real-world Applications

  • Network Connectivity: Determining if two computers are connected in a network, or managing dynamic connectivity as links go up and down.
  • Kruskal's Algorithm: Finding the Minimum Spanning Tree (MST) of a graph efficiently by sorting edges and using Union-Find to avoid cycles.
  • Image Processing: Connected-component labeling to find regions of adjacent pixels with the same color or intensity in an image.
  • Game Development: Hex or Go board evaluation, procedural generation (e.g., ensuring a maze is solvable).

Related Concepts

  • Graph Traversal (BFS/DFS) — An alternative way to find connected components, though less efficient for dynamic updates.
  • Minimum Spanning Trees — Kruskal's algorithm heavily relies on this structure.

Experience it interactively

Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Union-Find (Disjoint Set) module.

Try Union-Find (Disjoint Set) on Riano →

More in Computer Science