The Hitchhiker’s guide to Algorithms discusses the following pointers:
1.6 Counting or Optimizing Good Paths
In an n × m grid, we want to go from the left bottom corner to the upper right corner. Each
time we can only take a step to the right, or a step up. The number of ways we can do this
is exactly (n+m)!/(n! * m!). But what if we forbid some points on the grid? For example, if we
forbid all the points above the line y = x. Some of the problems has answer in closed formula.
But all of them can be solved quickly by dynamic programming.
Problem 1.6 Given a directed acyclic graph, how many paths are there from u to v? What is the longest one if there are weights on the edges?
My question is how are the two problems related? What is the relation between the grid here and a DAG. On stackoverflow itself I read that if we’re moving in only two directions in the grid then we can assume it’s a DAG. The question may sound very naive, but I’m a beginner and any help will be much appreciated.
Every point in the grid is a vertex. You have m * n vertices.
There is an edge going from each vertex to the vertex representing the point on its left and from each vertex to vertex representing the point on its top.
This way the DAG represents the grid.