The Knapsack Problem CMPSC 465 – Kleinberg/Tardos 6.4 I. The Problem Here’s the scenario for the knapsack problem: • We have n potential items we could pick up and a knapsack to put them in • Each item i… o has a weight of wi kg o has a value of vi s.t. vi > 0 • The knapsack has a capacity of W kg • Our goal is to fill the knapsack while maximizing the total value Example Instance: Let’s say we have a knapsack of capacity 11 kg. Here’s a set of weights and values: II. Diversion: Greedy Problem: Take a few minutes and work with a partner to propose a greedy algorithm to fill your knapsack. Why is it greedy? Try it out on the example instance above. pick item with max ratio of value/weight {5, 2, 1} is output but gets value 35. but {3, 4} gets 40. greedy not optimal Page 1 of 1 Prepared by D. Hogan referencing Algorithm Design by Kleinberg/Tardos and slides by K. Wayne for PSU CMPSC 465 III. Building a Solution via Dynamic Programming Recall what we did with the weighted interval scheduling problem: • We called an optimal solution for the first i itervals Oi • We defined OPT(i) to be the value of an optimal solution using the first i intervals • We figured out that recursively computing subproblems resulted in solving the same subproblem many times, so we instead memoized and iteratively built up a table of the values of OPT(i). So, let’s see how far we can get building up an OPT(i) for knapsack. Let’s define OPT(i) as… There are two cases to consider: • Item i is not selected in an optimal solution • Item i is selected in an optimal solution Observations: So, let’s tweak our definition of OPT(i) and make it OPT(i, w): Or two cases now change as follows: • Item i is not selected in an optimal solution with weight limit w • Item i is selected in an optimal solution Page 2 of 2 Prepared by D. Hogan referencing Algorithm Design by Kleinberg/Tardos and slides by K. Wayne for PSU CMPSC 465 We can summarize via the following function: Now, let’s turn that function into an iterative algorithm: Page 3 of 3 Prepared by D. Hogan referencing Algorithm Design by Kleinberg/Tardos and slides by K. Wayne for PSU CMPSC 465 IV. Example Back to this example instance: Our knapsack has capacity 11 kg. Here’s a set of weights and values: Use the algorithm we just developed to solve this instance of the knapsack problem. weights n items selected from 0 1 2 3 4 5 6 7 8 9 10 11 0 1 2 3 4 5 V. Analysis Question: What is the runtime of this algorithm? Theta(nW) NP-complete Homework: 1. Describe an algorithm to recover the optimal subset from the table M generated by our algorithm and the algorithm’s inputs. 2. Try the knapsack algorithm on an input with items of weights 3 lb., 2 lb., 1 lb., 4 lb., 5 lb. and values $25, $20, $15, $40, and $50, respectively. The knapsack capacity is 6 lb. (Levitin Exercise 8.4.1a) I want to prove the correctness of that algorithm somewhere – maybe recitation? Page 4 of 4 Prepared by D. Hogan referencing Algorithm Design by Kleinberg/Tardos and slides by K. Wayne for PSU CMPSC 465