Disjoint Set Union (Union-Find) Data Structure

What is Disjoint Set Union (DSU)?

DSU, also known as Union-Find, is a data structure that keeps track of a partition of a set into disjoint subsets. It supports two useful operations efficiently:

Key Concepts and Optimizations

Python Implementation

class DisjointSetUnion:
    def __init__(self, n):
        self.parent = list(range(n))
        self.size = [1] * n

    def find(self, x):
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])  # Path compression
        return self.parent[x]

    def union(self, a, b):
        rootA = self.find(a)
        rootB = self.find(b)

        if rootA != rootB:
            # Union by size
            if self.size[rootA] < self.size[rootB]:
                rootA, rootB = rootB, rootA
            self.parent[rootB] = rootA
            self.size[rootA] += self.size[rootB]

# Usage Example
dsu = DisjointSetUnion(5)  # create 5 elements (0 to 4)
dsu.union(0, 1)
dsu.union(1, 2)
dsu.union(3, 4)
print("Parent array:", dsu.parent)
print("Are 0 and 2 connected?", dsu.find(0) == dsu.find(2))  # True
print("Are 0 and 4 connected?", dsu.find(0) == dsu.find(4))  # False

Applications