Lecture 8

In this lecture we introduced dynamic programming. Dynamic programming is like divide-and-conquer except that we reuse the solutions to subproblems, rather than just repeatedly finding them. I drew a picture which captured this idea.

The example used for the entire lecture was 0/1 KNAPSACK. You are a thief trying to steal the most valuable set of items that you can carry. Thus the INSTANCE is a set of numbers {v_1, ..., v_n} ("values"), a set of numbers {w_1, ..., w_n} ("weights"), and a number B ("maximal weight you can carry"). The problem is to FIND a set I\subseteq{1,..., n} such that \sum_{i in I} v_i is maximized subject to the constraint that \sum_{i in I} w_i <= B.

We slowly and carefully developed a solution to this problem. First we wrote a recurrence relation; then we implemented it in an (inefficient) recursive program; then we modified the program to use a table and thereby be much more efficient. Just this once, we carried this all the way to the level of code, and we showed the execution times for the "wrong" and "right" implementation. Our basic solution found the *value* of a maximal solution, not the solution itself, so we showed how to modify the code to recover the solution.