Graph Theory37 sections · 1633 units
Open in Course

Lessons from Game Routes

Counting on DAGs

This problem shows how DP on DAGs extends beyond finding the longest path. Instead of maximizing, we count all paths. The core insight is that counting paths follows the same topological order pattern, but uses addition instead of max.

The state ways[v] counts paths from source to vv. The transition sums contributions from all predecessors: ways[v] = Σ ways[u] for all edges uvu \to v. This works because each path through uu extends uniquely to vv.

You've now seen two DP patterns on DAGs: longest path (Longest Flight Route) and counting paths (Game Routes). The template is always the same: topological order plus DP transitions. The only difference is the operation (max vs sum).