The Disjoint Set (also called Union-Find) is a data structure used to manage a collection of non-overlapping sets. It supports two main operations:
- Find: Determine which set a particular element belongs to.
- Union: Merge two sets into a single set.
Disjoint Set is particularly useful in problems involving connected components, cycle detection in graphs, and Kruskal's algorithm for Minimum Spanning Tree.
To improve efficiency, it uses two optimizations:
- Union by Rank or Union by Size: Always attach the smaller tree under the root of the larger tree.
- Path Compression: Flatten the tree whenever Find is used, making future queries faster.