Mastering Algorithms

What is Union-Find?

The Union-Find data structure, also known as Disjoint Set Union (DSU), is a powerful data structure used to efficiently manage and query disjoint sets. It provides an elegant solution for tracking which elements belong to the same set and for merging sets together. The data structure maintains a collection of disjoint (non-overlapping) sets and supports two fundamental operations:

The Union-Find data structure uses a tree-based representation where each set is represented as a tree, with one element serving as the root (representative) of that set. Initially, each element is its own parent, forming n singleton sets. The Find operation traverses up the tree to find the root, while Union connects two trees by making one root point to the other.

To achieve near-constant time complexity, Union-Find employs two key optimizations:

With these optimizations, both Find and Union operations achieve nearly constant amortized time complexity, approximately O(α(n)), where α(n) is the inverse Ackermann function, which grows extremely slowly and is effectively constant for all practical purposes. The space complexity is O(n) to store the parent and rank arrays.

Union-Find is commonly applied in problems involving connected components, such as Kruskal's algorithm for finding Minimum Spanning Trees (MST), network connectivity analysis, social network clustering, image processing for connected component labeling, and dynamic connectivity problems. It's also essential in solving problems that require tracking relationships and determining if elements are in the same group or connected in some way.

Union-Find Algorithm Pseudocode

    
    Initialize:
        parent = [i for i in range(n)]
        rank = [1] * n
    
    Find(x):
        if x != parent[x]:
            parent[x] = Find(parent[x])  # Path compression
        return parent[x]

    Union(x, y):
        root_x = Find(x)
        root_y = Find(y)
        if root_x != root_y:
            if rank[root_x] > rank[root_y]:
                parent[root_y] = root_x
            elif rank[root_x] < rank[root_y]:
                parent[root_x] = root_y
            else:
                parent[root_y] = root_x
                rank[root_x] += 1
    
            

Common Trends in Problems where Union-Find is Applied

Union-Find Framework