What is Union Find?
Union Find (Disjoint Set Union) tracks elements partitioned into disjoint sets. It supports near O(1) operations for finding which set an element belongs to and merging sets.Quick Recognition: Problems involving grouping, connectivity, or dynamic component counting. Keywords: “connected components”, “group together”, “same group”, “cycle detection”.
Pattern Recognition Checklist
Use Union Find When
- Counting connected components
- Checking if two nodes are connected
- Cycle detection in undirected graphs
- Dynamic connectivity queries
- Kruskal’s MST algorithm
- Grouping elements by some relation
Don't Use When
- Need shortest path (use BFS/Dijkstra)
- Directed graph cycle detection (use DFS)
- Need to traverse paths
- Static graph (BFS/DFS might be simpler)
Complexity Reference
| Operation | Without Optimization | With Path Compression + Union by Rank |
|---|---|---|
| Find | O(n) | O(alpha(n)) nearly O(1) |
| Union | O(n) | O(alpha(n)) nearly O(1) |
| Connected | O(n) | O(alpha(n)) nearly O(1) |
When to Use
Connected Components
Counting groups, checking connectivity
Cycle Detection
Detect cycles in undirected graphs
Dynamic Connectivity
Online queries about connectivity
MST Algorithms
Kruskal’s algorithm
Implementation
Pattern Variations
1. Number of Islands (Union Find approach)
2. Accounts Merge
3. Redundant Connection (Cycle Detection)
Classic Problems
| Problem | Pattern | Key Insight |
|---|---|---|
| Number of Islands | Grid connectivity | 2D to 1D mapping |
| Accounts Merge | Email grouping | Union emails, group by root |
| Redundant Connection | Cycle detection | Edge creating cycle |
| Longest Consecutive | Value connectivity | Union consecutive numbers |
| Kruskal’s MST | Edge processing | Union in weight order |
Interview Problems by Company
- Medium
- Hard
| Problem | Company | Key Concept |
|---|---|---|
| Number of Provinces | All FAANG | Basic connectivity |
| Redundant Connection | Google, Amazon | Cycle detection |
| Accounts Merge | Meta, Amazon | Email grouping |
| Satisfiability | Equality constraints |
Interview Tips
How to Explain Your Approach
How to Explain Your Approach
Script for interviews:
- “I’ll use Union Find for efficient connectivity queries.”
- “I’ll implement with path compression and union by rank.”
- “Find operation compresses the path to root.”
- “Union attaches smaller tree under larger.”
- “Both operations are nearly O(1) amortized.”
Union Find Template
Union Find Template
Common Variations
Common Variations
| Variation | Use Case |
|---|---|
| Track component size | Largest component problems |
| Weighted Union Find | Equations, ratios |
| 2D to 1D mapping | Grid problems: i * cols + j |
| Virtual nodes | Connect to boundary/source |
Practice Problems
Number of Provinces
Connected components
Accounts Merge
Email grouping
Redundant Connection
Cycle detection
Longest Consecutive
Value connectivity
Practice Roadmap
Day 1: Basic Union Find
- Solve: Number of Provinces, Redundant Connection
- Focus: Implement find with path compression, union by rank
Day 2: Grid Problems
- Solve: Number of Islands (UF approach), Surrounded Regions
- Focus: 2D to 1D coordinate mapping
Day 3: Advanced Applications
- Solve: Accounts Merge, Satisfiability of Equations
- Focus: Grouping with additional data