Graph Theory37 sections · 1633 units
Open in Course

Walkthrough Example

Tracing the DP

Consider n=4n = 4, edges: 121 \to 2, 131 \to 3, 232 \to 3, 242 \to 4, 343 \to 4. Topological order: [11, 22, 33, 44]. Initialize: dp=[1,0,0,0]dp = [1, 0, 0, 0]. Process level 11: Update levels 22 and 33. dp=[1,1,1,0]dp = [1, 1, 1, 0]. Process level 22: Update levels 33 and 44. dp[3] = (1 + 1) = 2, dp[4] = (0 + 1) = 1.

Now dp=[1,1,2,1]dp = [1, 1, 2, 1]. Process level 33: Update level 44. dp[4] = (1 + 2) = 3. Now dp=[1,1,2,3]dp = [1, 1, 2, 3]. Answer: dp[4] = 3 paths.