DAG and Topological Sort

You've detected cycles. Now use topological order for scheduling. Solve Course Schedule variants and learn DP on directed acyclic graphs.

28 lessons
119 min
Codeforces: 1600-2100LeetCode: 1700-2000

Lessons

1. Introduction to DAGs and Topological Sort

No cycles, can be ordered

4m

2. What DAG Problems Solve

Build systems, task scheduling

3m

3. When to Use Topological Sort

Dependencies → topological order

3m

4. Kahn's vs DFS Topological Sort

Comparing the approaches

4m

5. Problem - Alien Dictionary

LC 269 - character order from sorted words

3m1 problems

6. Alien Dictionary - Why Naive Fails

Adjacent words reveal one ordering

4m

7. Alien Dictionary - Building the Graph

Edge from c1 → c2 means c1 < c2

4m

8. Alien Dictionary - Transition

First differing character gives edge

4m

9. Alien Dictionary - Base Cases

Longer word before prefix = invalid

4m

10. Alien Dictionary - Implementation

Build graph, run topo sort

5m1 problems

11. Alien Dictionary - Time and Space

O(C) total characters processed

3m

12. Problem - Longest Increasing Path

LC 329 - longest strictly increasing path

4m1 problems

13. Longest Increasing Path - Why Naive Fails

Strictly increasing = no cycles

4m

14. Longest Increasing Path - Defining the DP

dp[r][c] = longest from this cell

4m

15. Longest Increasing Path - Core Logic

Recurse into larger neighbors

4m

16. Longest Increasing Path - Implementation

Memoization avoids recomputation

5m

17. DP on DAG Pattern

The DAG DP pattern

4m

18. Problem - Find All Recipes

LC 2115 - recipes from ingredients

4m1 problems

19. Find All Recipes - Solution

Supplies have in-degree 0

4m

20. Find All Recipes - Why This Works

Process available ingredients first

3m

21. Problem - Parallel Courses

LC 1136 - minimum semesters

3m1 problems

22. Parallel Courses - Solution

Level-by-level with Kahn's

4m1 problems

23. Quiz: Topological Sort Applications

Which topo sort pattern?

3m1 problems

24. Quiz: DAG Properties

Cycle detection methods

3m1 problems

25. Common Mistakes in DAG Problems

Missing in-degree update, wrong order

4m

26. Problem - Largest Color Value

LC 1857 - color propagation

10m1 problems

27. Largest Color Value - Solution

The solution

10m

28. Section Recap

Kahn's to DFS cycle detection

5m

Practice Problems

1.
Fox And Namescodeforces
2.
Substringcodeforces
3.
Journeycodeforces
4.
Directing Edgescodeforces
5.
Minimal Labelscodeforces

Ready to start learning?

Access all 28 lessons with interactive content and progress tracking.