- express the problem as a recurrence relation;
- implement the recurrence relation either via memoization or via a bottom up approach.
As far as I know, I have said everything about dynamic programming. I mean: dynamic programming does not give tools/rules/methods/theorems for expressing recurrence relations, nor for turning them into code. So, what’s special about dynamic programming? What does it give you, other than a vague method for approaching a certain kind of problems?
Asked By : hey hey
Answered By : D.W.
- If the input is a positive integer $n$, one candidate way to define a subproblem is by replacing $n$ with a smaller integer $n’$ (s.t. $0 le n’ le n$).
- If the input is a string $S[1..n]$, some candidate ways to define a subproblem include: replace $S[1..n]$ with a prefix $S[1..i]$; replace $S[1..n]$ with a suffix $S[j..n]$; replace $S[1..n]$ with a substring $S[i..j]$. (Here the subproblem is determined by the choice of $i,j$.)
- If the input is a list, do the same as you’d do for a string.
- If the input is a tree $T$, one candidate way to define a subproblem is to replace $T$ with any subtree of $T$ (i.e., pick a node $x$ and replace $T$ with the subtree rooted at $x$; the subproblem is determined by the choice of $x$).
- If the input is a pair $(x,y)$, then recursively look at the type of $x$ and the type of $y$ to identify a way to choose a subproblem for each. In other words, one candidate way to define a subproblem is to replace $(x,y)$ by $(x’,y’)$ where $x’$ is a subproblem for $x$ and $y’$ is a subproblem for $y$. (You can also consider subproblems of the form $(x,y’)$ or $(x’,y)$.)
And so on. This gives you a very useful heuristic: just by looking at the type signature of the method, you can come up with a list of candidate ways to define subproblems. In other words, just by looking at the problem statement — looking only at the types of the inputs — you can come up with a handful of candidate ways to define a subproblem. This is often very helpful. It doesn’t tell you what the recurrence relation is, but when you have a particular choice for how to define the subproblem, often it’s not too hard to work out a corresponding recurrence relation. So, it often turns design of a dynamic programming algorithm into a structured experience. You write down on scrap paper a list of candidate ways to define subproblems (using the heuristic above). Then, for each candidate, you try to write down a recurrence relation, and evaluate its running time by counting the number of subproblems and the time spent per subproblem. After trying each candidate, you keep the best one that you were able to find. Providing some structure to the algorithm design process is a major help, as otherwise algorithm design can be intimidating (there’s such a huge space of possible approaches, without some structure it can be unclear how to even get started).
Best Answer from StackOverflow
Question Source : http://cs.stackexchange.com/questions/47216