Disjoint Set Union (DSU)

Union by rank/size, path compression, rollback DSU, and applications.

49 lessons
160 min

Lessons

1. Intro

Dynamic changes

2m

2. Disjoint Set Union (DSU)

The Structure

2m

3. The Intuition - Leaders

Who is the boss?

3m

4. The Parent Array

Tracking relationships

3m

5. The Find Operation

Finding the Leader

3m

6. The Union Operation

Merging Groups

3m

7. The Problem with Naive DSU

Long Chains

3m

8. Improvement 1 - Path Compression

The Magic Step

5m

9. Implementation - Find with Compression

Coding the Magic

5m

10. Quiz: Path Compression

Knowledge check

2m1 problems

11. Improvement 2 - Union by Size

Balancing the Tree

4m

12. Implementation - Union by Size

Coding the Merger

5m

13. The Full DSU Template

Copy-Paste Ready

5m

14. Cycle Detection

Application 1

3m

15. Quiz: Cycle Detection with DSU

Knowledge check

2m1 problems

16. LeetCode 684 Redundant Connection - Problem Statement

LeetCode 684

2m1 problems

17. LeetCode 684 Redundant Connection - Logic

First Hit

4m

18. Counting Components

Application 2

3m

19. LeetCode 547 Number of Provinces - Problem Statement

LeetCode 547 (DSU Version)

2m1 problems

20. LeetCode 547 Number of Provinces - Strategy

Iterating the Matrix

4m

21. Tracking Component Sizes

Application 3

3m

22. CSES 1676 Road Construction - Problem Statement

CSES Problem Set

3m1 problems

23. CSES 1676 Road Construction - Logic

Maintaining State

5m

24. Pattern - Logic & Equations

Symbols as Nodes

2m

25. Quiz: Component Counting

Knowledge check

2m1 problems

26. LeetCode 990 Satisfiability of Equality Equations - Problem Statement

LeetCode 990

3m1 problems

27. LeetCode 990 Satisfiability of Equality Equations - Strategy

Two Passes

4m

28. Pattern - Transitivity

Swapping Logic

2m

29. LeetCode 1202 Smallest String With Swaps - Problem Statement

LeetCode 1202

3m1 problems

30. LeetCode 1202 Smallest String With Swaps - Algorithm

Sorting Components

5m

31. Pattern - Indirect Grouping

Mapping Data

2m

32. LeetCode 721 Accounts Merge - Problem Statement

LeetCode 721

3m1 problems

33. LeetCode 721 Accounts Merge - The Graph

Emails are Edges

5m

34. Pattern - Grid Components

Connecting pixels

2m

35. Quiz: Indirect Grouping

Knowledge check

2m1 problems

36. LeetCode 827 Making A Large Island - Problem Statement

LeetCode 827

3m1 problems

37. LeetCode 827 Making A Large Island - Logic

DSU + Virtual Flip

6m

38. Pattern - Hub Nodes

Reducing Edges

3m

39. Codeforces 1263D Secret Passwords - Problem Statement

Codeforces 1263D

3m1 problems

40. Codeforces 1263D Secret Passwords - Logic

Letters are Hubs

5m

41. Pattern - Offline Queries

Reordering Time

3m

42. Quiz: Offline Processing

Knowledge check

2m1 problems

43. LeetCode 1697 Edge Limited Paths - Problem Statement

LeetCode 1697

4m1 problems

44. LeetCode 1697 Edge Limited Paths - Implementation Plan

Sorting Everything

5m

45. Pattern - The Reverse Time Trick

Destruction is hard

3m

46. USACO 644 Closing the Farm - Problem Statement

USACO Gold

3m1 problems

47. USACO 644 Closing the Farm - Solution

Adding Barns

5m

48. Quiz: Reverse Time Trick

Knowledge check

2m1 problems

49. Section Recap

Dynamic Connectivity

2m

Practice Problems

1.
2.
Redundant ConnectionLeetCodemedium
3.
Accounts MergeLeetCodemedium
4.
5.
6.
CthulhuCodeforcesmedium
7.
News DistributionCodeforceseasy
8.
The Number of ImpostersCodeforcesmedium
9.
Path QueriesCodeforceshard

Ready to start learning?

Access all 49 lessons with interactive content and progress tracking.