Union-Find
Union-Find tracks connected components with near-constant time operations. Learn path compression.
Lessons
1. Intro
Why Union-Find matters
2. Basic Structure
Parent array representation
3. Basic Union
Merging two components
4. Path Compression
Flattening during find
5. Union by Rank
Keeping trees balanced
6. Union by Size
Alternative balancing
7. Complexity Analysis
The inverse Ackermann function
8. Problem - Number of Connected Components
Classic Union-Find application
9. Connected Components Solution
Track component count
10. Problem - Redundant Connection
Detect cycles in graphs
11. Cycle Detection with Union-Find
When union fails
12. Problem - Number of Provinces
Friend circles as components
13. Provinces Solution
Union-Find on matrix
14. Problem - Graph Valid Tree
Tree = connected + acyclic
15. Valid Tree Solution
Check edges and connectivity
16. Weighted Union-Find
Tracking relationships
17. Problem - Evaluate Division
Division as graph traversal
18. Evaluate Division: Weighted UF
Ratios as weights
19. Evaluate Division Solution
Implement your solution
20. Problem - Accounts Merge
Grouping by shared elements
21. Accounts Merge Approach
Emails as nodes
22. Accounts Merge Solution
Implement your solution
23. Problem - Largest Component Size by Factor
Union by common factors
24. Union by Prime Factors
Factor-based Union-Find
25. Largest Component Solution
Implement your solution
26. Problem - Swimming in Rising Water
Union-Find with sorting
27. Swimming Solution
Sort cells by elevation
28. Kruskal's Algorithm
MST with Union-Find
29. Problem - Min Cost to Connect Points
MST on points
30. Min Cost Solution
Kruskal on complete graph
31. Union-Find with Rollback
Undoing union operations
32. Online vs Offline Problems
Query ordering matters
33. Common Union-Find Bugs
Debugging tips
34. Quiz: Union-Find
Test your understanding
35. Section Recap
What you learned
Practice Problems
Classic DSU for connected components. Find group sizes after merging users by shared groups.
Transform forest to tree with minimum edge changes. DSU to detect components and count needed edges.
DSU for character equivalence classes. Build smallest string satisfying equality constraints.
MST with degree constraint using DSU. Greedy edge selection with component tracking.
Count connected components on coordinate grid. DSU for points sharing x or y coordinate.
Offline queries with reverse DSU. Process edge deletions as additions in reverse order.
Connect people who share languages. Count additional languages needed for full connectivity.
DSU with rollback for dynamic connectivity. Understand union-find history for queries.
Find components in complement graph. DSU with set for unvisited vertices optimization.
MST queries using DSU and LCA. Find MST weight including specific edge.
Check string transformability with character groups. DSU for equivalent characters.
DSU with parity for bipartite checking. Weighted union-find tracking odd/even distances.
Count connected components in adjacency matrix. Classic DSU introduction problem.
Find cycle-creating edge in tree. First edge where both nodes already in same component.
DSU with string mapping. Merge accounts sharing emails, complex real-world application.
Can be solved with DSU unioning consecutive numbers. Alternative to hash set approach.
Count connected components in grid. DSU alternative to DFS/BFS flood fill.
Stones in same row/column form component. Answer is stones - components.
DSU for equality, then check inequality. Process == first to build components.
Sort characters within each DSU component. Build lexicographically smallest string.