Graph Theory37 sections · 1633 units
Open in Course

DP State for Counting

dp[u] = number of paths to u

Define dp[v] = number of ways to reach node vv from the source. Base case: dp[source] = 1 (one way to stay at source), all others start at 00. Process nodes in topological order. For each edge (u,v)(u, v), add dp[u] to dp[v]. Why addition?

Each path to uu can extend to vv by following edge (u,v)(u, v). So the total number of paths to vv is the sum of paths from all nodes that have edges to vv.