Pattern Overview: What is Union-Find?
Union-Find (also called Disjoint Set Union or DSU) is a data structure that tracks elements partitioned into disjoint sets.
Two Core Operations:
- •Find(x): Which set does x belong to? (Returns the root/representative)
- •Union(x, y): Merge the sets containing x and y
Visual Representation:
When to Use Union-Find:
- •Dynamic connectivity (are X and Y connected?)
- •Counting connected components
- •Detecting cycles in undirected graphs
- •Grouping related items (accounts, friends)
- •Kruskal's MST algorithm
Key Insight: Union-Find answers connectivity queries efficiently without storing the actual graph structure.