Short description: Execute a given DP algorithm on examples; identify DP components and failures.
Relevant notes: Topic F: Dynamic Programming.
Given a DP problem and algorithm, identify each of the DP components of the algorithm. Execute the algorithm on a given example, filling in the array and giving the final answer. Identify failures of incorrect variants.
Consider the problem of knapsack with duplicates allowed:
Here is a dynamic programming algorithm:
1 knapsack_dups(v, w, W):
2 let C[0] = 0
3 for x = 1 to W:
4 C[x] = C[x-1]
5 for i = 1 to n, if w[i] <= x:
6 C[x] = max(C[x], v[i] + C[x - w[i]])
7 return C[W]
Part a. What are the DP components of this solution? Briefly explain each.
Part b. Execute the algorithm on the following input, including filling in the array C. Let $W = 9$. For x = 0 to 4, briefly describe how you arrived at the value you found for C[x].
| Item | Value | Weight |
|---|---|---|
| 1 | 4 | 2 |
| 2 | 5 | 3 |
| 3 | 8 | 5 |
Part a.
Subproblem definition: C[x] = the maximum possible value for a set of items with duplicates of total weight at most x, for x = 0 to W.
Recurrence, base case: C[0] = 0, meaning that a weight limit of zero has a value of zero.
Recurrence, inductive case: C[x] = max(C[x-1], max_i v[i] + C[x - w[i]]), where the inner maximum is only over items i where w[i] <= x. Explanation: the maximum value for weight x is either the maximum value for weight x-1, where we just use that same set of items; or the maximum, over all items that can fit, of the best value of including a copy of that item and then optimally using the remaining space.
Final answer: we just return C[W], because that equals the original problem.
Reconstructing the object: not part of this solution.
Part b.
| x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|---|---|
| C[x] | 0 | 0 | 4 | 5 | 8 | 9 | 12 | 13 | 16 | 17 |
C[0] was the base case. For C[1], none of the items fit, so the only choice in the recurrence was C[1-1] = C[0] = 0. For C[2], only item 1 fit, with weight 2 and value 4, so C[2] = 4. For C[3], we could either fit item 1 or 2. Item 1 gave value 4 + C[1] = 4. Item 2 gave value 5 + C[0] = 5. So item 2 was better. For C[4], we could either fit item 1 or 2. Item 1 gave value 4 + C[2] = 8. Item 2 have value 5 + C[1] = 5. So item 1 was better.
The final answer is C[9] = 17.
To pass, a solution must follow the course requirements for writing and proofs.
To pass, a solution must pass both parts. For Part a, each component must be clearly identified, except that Reconstructing can be omitted in this case. Each component must have a correct, brief explanation. However, these explanations can be brief and not comprehensive, as long as the component was identified correctly.
For Part b, the table and final answer must be filled in correctly. A solution may still pass if it forgets to include x=0, if everything else is filled in correctly. The explanations for x=0,...,4 must be correct, but can be brief and not comprehensive.
The numerical rubric for this problem is: