Reputation: 5576
As I gain an understanding of dynamic programming, I am finding it easier and easier to develop a notion of optimal substructure in a given situation. For example, with finding the optimal ordering to multiply a matrix chain, I understand that (sorry to be verbose; it helps me out) the minimum number of multiplications needed to compute Ai * Ai+1 * ... * Aj can be found by finding the split/parenetheses placement point k between i and j that minimizes the sum of the multiplications needed for Ai*...Ak and Ak+1...*Aj, plus the cost that comes with the actual dimensions. In other words, M(i,j) = mink(M(i,k) + M(k+1,j) + di-1dkdj).
Likewise, in finding the longest palindromic substring of a string, the optimal substructure is that the length l[i,j] of a maximum length palindrome between indices i and j and the input array is either 2 + l[i+1, j-1], when the elements at i and j are the same and can thus be added, or otherwise the maximum of l[i+1, j], l[i, j-1] (correct me if I mix anything up...)
But how, in any situation, do I translate this into an algorithm to find the length of an ideal sequence such as the above or even its contents? Do I basically just run loops to tabulate everything, and then essentially 'choose' what is needed from the table? With the matrix chain, that seems to be exactly what to do, but for the palindrome, I'm a little confused on how to construct the loops.
Thanks!
Upvotes: 1
Views: 378
Reputation: 4778
Do I basically just run loops to tabulate everything, and then essentially 'choose' what is needed from the table?
In short, yes. Dynamic programming relies on two things: making the original problem smaller (which is well described in your question) and base cases: those (almost always small) situations where the solution is obvious, and no longer requires dividing the problem into sub-problems. For example, in your matrix multiplication example, once the sub-problem is reduced to 2 matrices, you no longer have a choice: you have to multiply them as is. In your palindrome example, I would choose a base case of a substring of length 1, which is obviously a palindrome.
Therefore, once you create the memoization array / matrix etc, what you typically do is set the base case values in that array, and let the algorithm run. The end conditions are usually either when you reach the correct point in the array, or when there is nothing left to calculate (at which point you 'choose' what you need from the array / matrix etc)
I hope that's concrete enough to be useful.
Upvotes: 1