Dynamic Programming21 sections · 916 units
Open in Course

Challenge: DP on DAGs

Generalizing beyond trees

Tree DP works because trees have no cycles. What about DAGs (directed acyclic graphs)? Same principle: process nodes in topological order.

A node's answer depends only on its descendants, which are processed first. Key difference: a node might have multiple parents, so you can't just use post-order. Use explicit topological sort. Examples: longest path in DAG, counting paths, minimum cost paths. All solvable with DP on topological order.