Solving problems with dynamic programming [00QU]
In general, here are the steps you should follow to solve a problem with dynamic programming:
- Try something else first! (Greedy, reduce to standard graph algorithm, ...)
- Consider different ways of splitting up or restricting the overall problem into subproblems or subcases, and come up with a recurrence. This is the hardest part! It does get easier with experience.
- Prove the recurrence correct (via induction).
- Check: do the recursive subproblems overlap?
- No: rejoice! You have a divide & conquer algorithm.
- Yes: proceed with the rest of the steps!
- Memoize the recurrence, either by adding a table to the recursive algorithm, or by directly filling in a table with some loop(s). Note that it is also possible to memoize with more exotic data structures (dictionaries, lazy infinite trees, ...) but in this class we will only see examples using arrays!
- Optional: to reconstruct an optimal solution, remember the choices made along the way, then reconstruct the solution in reverse. (We'll see examples of this later.)