Home▸Articles▸Algorithms & AI

Union-Find (Disjoint Sets): 2D Forest

An essential data structure algorithm used in managing dynamic connectivity problems efficiently.

mysimulator teamUpdated June 2026≈ 3 min read▶ Open the simulation

What is Union-Find?

Union-Find, also known as Disjoint Sets, is an abstract data type that maintains a collection of elements partitioned into a number of disjoint (non-overlapping) sets. It supports two main operations: `union`, which merges two sets, and `find`, which determines which set a particular element belongs to.

This algorithm is particularly useful in scenarios where we need to manage dynamic connectivity problems, such as determining if two elements are connected or finding the smallest spanning tree.

How Does Union-Find Work?

The core of the Union-Find data structure lies in its ability to efficiently handle these operations. Each element is stored in a tree-like structure, where each node points to its parent. The `find` operation traverses this structure from an element to the root, which represents the set containing that element.

Optimizations like path compression and union by rank are applied to speed up these operations. Path compression flattens the structure of the trees during a find operation, making future finds faster. Union by rank ensures that smaller trees are always attached under larger ones, reducing the depth of the tree.

live demo · related simulation● LIVE

Why Does It Matter?

Union-Find is crucial in various applications such as network connectivity problems, image processing, and even in the implementation of Kruskal's algorithm for finding minimum spanning trees. Its efficiency makes it a cornerstone in many algorithms that require dynamic set management.

The live demonstration of tree depth and find cost in the 2D forest simulation helps visualize how these optimizations work in practice, providing insights into their impact on performance.

Real-World Applications

Union-Find is used in social network analysis to determine connected components of users. It's also vital in the design of distributed systems where nodes need to be grouped based on certain criteria.

In computer graphics, it helps manage regions and boundaries for efficient rendering and collision detection.

Frequently asked questions

What is path compression?

Path compression is an optimization technique that flattens the structure of trees during a find operation by making each node point directly to the root. This significantly speeds up future queries on the same elements.

How does union by rank work?

Union by rank ensures that when two sets are merged, the tree with fewer nodes (smaller rank) is attached under the tree with more nodes (larger rank). This helps in keeping the trees short and flat, reducing the depth of the structure.

Can Union-Find be used for any type of data?

Union-Find is typically used with elements that can be compared using an equality relation. It's particularly effective when dealing with sets of integers or other comparable types.

What are the time complexities of union and find operations in Union-Find?

With path compression and union by rank, both `find` and `union` operations can be nearly constant-time on average, making them very efficient. The amortized time complexity for these operations is close to O(α(n)), where α is the inverse Ackermann function.

Try it live

Everything above runs in your browser — open Union-Find (Disjoint Sets): 2D Forest and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.

▶ Open Union-Find (Disjoint Sets): 2D Forest simulation

What did you find?

Add reproduction steps (optional)