DP on DAGs

Shortest/longest path in DAGs, counting paths, and DP with topological order.

50 lessons
156 min

Lessons

1. Intro

The Goal

2m

2. Why DAGs Are Special

No cycles = DP works

3m

3. Topological Order Refresher

Linear ordering of nodes

3m

4. The DP on DAGs Pattern

Process in topological order

3m

5. Quiz: Topological Order

Knowledge check

2m1 problems

6. Shortest Path in DAGs

Better than Dijkstra

3m

7. Defining the DP State

What dp[u] means

3m

8. Shortest Path Algorithm

Step by step process

3m

9. Shortest Path Example

Walking through an instance

4m

10. Longest Path in DAGs

Flip the minimization

3m

11. When You Need Longest Paths

Scheduling and critical paths

3m

12. Quiz: Path Types

Knowledge check

2m1 problems

13. Problem - Longest Flight Route

CSES 1680

3m1 problems

14. Core idea - Counting Nodes

Edge weights are 1

3m

15. Checking for Cycles

Networks can have cycles

3m

16. State Definition

dp[u] = max cities to u

3m

17. Reconstructing the Route

Backtracking from destination

3m

18. Longest Flight Route - Implementation

Complete solution

4m1 problems

19. Edge Case - Unreachable Destination

Handling IMPOSSIBLE

3m

20. Walkthrough Example

Step by step trace

4m

21. Lessons Learned

summary

3m

22. Counting Paths

How many ways to reach

3m

23. Problem - Game Routes

CSES 1681

3m1 problems

24. DP State for Counting

dp[u] = number of paths to u

3m

25. Why you Add Counts

Paths are independent

3m

26. Handling Large Counts

Output modulo 10^9+7

3m

27. Game Routes - Implementation

Complete solution

3m1 problems

28. Walkthrough Example

Tracing the DP

4m

29. Quiz: Counting Paths

Knowledge check

2m1 problems

30. Common Mistake - Initialization

Base case matters

3m

31. Common Mistake - Modulo Placement

Apply after every operation

3m

32. Multiple Sources

Initialize multiple base cases

3m

33. Multiple Destinations

Sum over all endpoints

3m

34. Backward DP

Reverse edges for DP

4m

35. Combining Two DPs

Paths through a node

4m

36. Quiz: DP Details

Knowledge check

2m1 problems

37. Problem - High Score

CSES 1673

3m1 problems

38. Positive Cycles

Infinite score problem

3m

39. Using Bellman-Ford

Negate for maximization

4m

40. Cycles Must Be On Path

Reachable from 1 and reaches n

4m

41. High Score - Implementation

Complete solution

4m1 problems

42. When DAG DP Fails

Cycles break the pattern

3m

43. DP on Trees

Trees are DAGs

3m

44. Vocabulary - Relaxation

Updating distance estimates

3m

45. Vocabulary - Topological Sort

Linear ordering of DAG nodes

3m

46. Practice - Course Schedule II

LeetCode 210

3m1 problems

47. Practice - Cheapest Flights

LeetCode 787 with constraint

4m1 problems

48. When to Use DP on DAGs

Problem recognition

3m

49. Implicit DAGs

State space graphs

4m

50. Section Recap

What we learned

3m

Practice Problems

2.
Parallel Courses IIILeetCodemedium
3.
Game RoutesCSESmedium
4.
Substring OrderingCodeforcesmedium
5.
Berland ColliderCodeforceshard
6.
PolandBall and ForestCodeforceseasy

Ready to start learning?

Access all 50 lessons with interactive content and progress tracking.