Union-Find

Union-Find tracks connected components with near-constant time operations. Learn path compression.

35 lessons
208 min

Lessons

1. Intro

Why Union-Find matters

2m

2. Basic Structure

Parent array representation

3m

3. Basic Union

Merging two components

3m

4. Path Compression

Flattening during find

3m

5. Union by Rank

Keeping trees balanced

3m

6. Union by Size

Alternative balancing

3m

7. Complexity Analysis

The inverse Ackermann function

3m

8. Problem - Number of Connected Components

Classic Union-Find application

2m1 problems

9. Connected Components Solution

Track component count

10m1 problems

10. Problem - Redundant Connection

Detect cycles in graphs

3m1 problems

11. Cycle Detection with Union-Find

When union fails

10m1 problems

12. Problem - Number of Provinces

Friend circles as components

2m1 problems

13. Provinces Solution

Union-Find on matrix

10m1 problems

14. Problem - Graph Valid Tree

Tree = connected + acyclic

2m1 problems

15. Valid Tree Solution

Check edges and connectivity

10m1 problems

16. Weighted Union-Find

Tracking relationships

4m

17. Problem - Evaluate Division

Division as graph traversal

3m1 problems

18. Evaluate Division: Weighted UF

Ratios as weights

5m

19. Evaluate Division Solution

Implement your solution

20m1 problems

20. Problem - Accounts Merge

Grouping by shared elements

3m1 problems

21. Accounts Merge Approach

Emails as nodes

4m

22. Accounts Merge Solution

Implement your solution

20m1 problems

23. Problem - Largest Component Size by Factor

Union by common factors

3m1 problems

24. Union by Prime Factors

Factor-based Union-Find

5m

25. Largest Component Solution

Implement your solution

15m1 problems

26. Problem - Swimming in Rising Water

Union-Find with sorting

3m1 problems

27. Swimming Solution

Sort cells by elevation

15m1 problems

28. Kruskal's Algorithm

MST with Union-Find

4m

29. Problem - Min Cost to Connect Points

MST on points

3m1 problems

30. Min Cost Solution

Kruskal on complete graph

15m1 problems

31. Union-Find with Rollback

Undoing union operations

3m

32. Online vs Offline Problems

Query ordering matters

3m

33. Common Union-Find Bugs

Debugging tips

3m

34. Quiz: Union-Find

Test your understanding

5m1 problems

35. Section Recap

What you learned

3m

Practice Problems

1.
News DistributionCodeforceseasy

Classic DSU for connected components. Find group sizes after merging users by shared groups.

2.

Transform forest to tree with minimum edge changes. DSU to detect components and count needed edges.

3.

DSU for character equivalence classes. Build smallest string satisfying equality constraints.

4.

MST with degree constraint using DSU. Greedy edge selection with component tracking.

5.
Ice SkatingCodeforceseasy

Count connected components on coordinate grid. DSU for points sharing x or y coordinate.

6.
Cutting a GraphCodeforcesmedium

Offline queries with reverse DSU. Process edge deletions as additions in reverse order.

7.
Learning LanguagesCodeforceseasy

Connect people who share languages. Count additional languages needed for full connectivity.

8.
Rank of PermutationCodeforcesmedium

DSU with rollback for dynamic connectivity. Understand union-find history for queries.

9.
Connected ComponentsCodeforcesmedium

Find components in complement graph. DSU with set for unvisited vertices optimization.

10.

MST queries using DSU and LCA. Find MST weight including specific edge.

11.
String EqualityCodeforcesmedium

Check string transformability with character groups. DSU for equivalent characters.

12.
BipartitenessCodeforcesmedium

DSU with parity for bipartite checking. Weighted union-find tracking odd/even distances.

13.
Number of ProvincesLeetCodemedium

Count connected components in adjacency matrix. Classic DSU introduction problem.

14.
Redundant ConnectionLeetCodemedium

Find cycle-creating edge in tree. First edge where both nodes already in same component.

15.
Accounts MergeLeetCodemedium

DSU with string mapping. Merge accounts sharing emails, complex real-world application.

16.

Can be solved with DSU unioning consecutive numbers. Alternative to hash set approach.

17.
Number of IslandsLeetCodemedium

Count connected components in grid. DSU alternative to DFS/BFS flood fill.

18.

Stones in same row/column form component. Answer is stones - components.

19.

DSU for equality, then check inequality. Process == first to build components.

20.

Sort characters within each DSU component. Build lexicographically smallest string.

Ready to start learning?

Access all 35 lessons with interactive content and progress tracking.