Definition
Union-Find Data Structure
The union-find data structure is a data structure that stores a collection of disjoint sets (
) and provides the following three major functions:
: Creates a new disjoint set where is called representative of . : Merges two disjoint sets, and , where and are the representatives of their respective sets. : Finds the set to which belongs.
Implementation
The main idea of the implementation is to represent the disjoint sets as strongly connected graph components:
The above shows two disjoint sets
Trivially, creating a new disjoint set using
To union those two sets, we just have to change the parent of
Merging two disjoint sets using
Creating a set using
However, contra-intuitively,
where each array access has a time complexity of