Graph Theory37 sections · 1633 units
Open in Course

Backward DP

Reverse edges for DP

Sometimes you want dp[u] to represent paths from uu to a fixed destination, not from a source to uu. Reverse all edges in the graph and run DP in the reversed graph. If the original graph has edge u→vu \to v, the reversed graph has edge v→uv \to u. Compute topological order on the reversed graph, initialize dp[destination] = 1, and propagate.

Now dp[u] in the reversed graph represents paths from uu to the destination in the original graph. This is useful for problems like counting paths that can reach a specific node.