Union Find (Disjoint Sets)

intermediate40 min

Union Find Overview

Union-find tracks items split into groups. find returns a group id; union merges two groups—nearly constant time per operation with path compression and union by rank.

Operations

  • Find: Determine which subset an element belongs to
  • Union: Join two subsets into a single subset
  • Connected: Check if two elements are in the same subset

Basic Union Find Implementation

Number of Islands Problem

Redundant Connection Problem

Common Applications

  • Graph Problems: Cycle detection, connected components
  • Network Problems: Network connectivity, clustering
  • Image Processing: Connected component labeling
  • Kruskal’s Algorithm: Minimum spanning tree
  • Game Theory: Board games with connected regions