Union Find
The disjoint-set data structure, also known as the union-find data structure, is used to keep track of a collection of non-overlapping sets or elements that are partitioned into disjoint subsets.
The primary operations associated with this data structure are:
- Find: Determine which subset an element belongs to. This is typically used to check if two elements are in the same subset.
- Union: Join two subsets together, combining them into a single subset.
The disjoint-set data structure is commonly used to solve problems that involve checking for connectivity between elements or managing partitions of sets. It's particularly efficient in applications like Kruskal's algorithm for finding minimum spanning trees, as it can quickly check if adding an edge would create a cycle in the graph.
A popular implementation of disjoint-set data structure is using an array or a map with a "parent" pointer for each element. The parent pointers are used to represent the tree structure that keeps track of the connected components. The efficiency of the find and union operations can be improved by applying path compression and union by rank heuristics.