Disjoint set is a data structure that stores elements which are split into one or more disjoint sets, the elements in these sets are non-overlapping. An union-find algorithm performs two main operations to find representative of a disjoint set and to merge disjoint sets to a single set.
Operation
makeSet(x)
- Create a new set containing the element
x
.
- Create a new set containing the element
find(x)
- Return the representative (also known as the “parent” or “root”) of the set that contains the element
x
. This operation is used to determine which set an element belongs to.
- Return the representative (also known as the “parent” or “root”) of the set that contains the element
union(x, y)
- Merge the sets containing elements
x
andy
into a single set.
- Merge the sets containing elements
Performance optimisation
Union by rank
Implementation
Back to parent page: Data Structures and Algorithms
Reference: