Knapsack Problem
gap-in-knowledge Video on how to compute the table: https://www.youtube.com/watch?v=nLmhmB6NzcM (understand)
From CS341
We’ve only looked at 0/1-KnapSack.
Input:
- items with weights and values
- a capacity
Output:
- a choice of items
- that satisfies the constraint
- and maximizes the value
Example:
- optimum with weight and value
See also:
- fractional knapsack (items can be divided), solved with a greedy algorithm
Note
- We want to respect the weight constraint of the chosen items by optimizing the number of elements we put in.
- Does this have the constraint of only being able to pick an item once?
- In Fractional Knapsack, we can break items for maximizing the total value of the knapsack.
Setting up the recurrence
Basic idea: either we choose item or not.
- then the optimum is the max of two values:
- , if we choose (and )
- Choosing item : If we choose item , we add its value to the total value. After choosing item , we reduce the available capacity of the knapsack by the weight of item , which is . This means we now have to consider the remaining items (from to ) with the reduced capacity.
- Indexing for remaining items: In dynamic programming, when considering the subproblems, we often index the items from to . So, if we choose item , we need to consider the subproblem with items to (i.e., excluding item ) to find the maximum value achievable with the reduced capacity.
- , if we don’t choose
maximum value achievable using a knapsack of capacity and items
Initial conditions
- for any
- for any
Summary
Therefore, the term "" represents the maximum value achievable with the remaining capacity considering only the first items, which is why it’s and not . We exclude item from consideration after choosing it for the knapsack.
Algorithm
- it should be in the else and instead of s.
Discussion:
This is called a pseudo-polynomial algorithm
- in our word RAM model, we have been assuming all s and s fit in a word
- so input size is words
- but the runtime also depends on the values of the inputs
01-knapsack is NP-Complete, so we don’t really expect to do much better.
More notes from the lecture notes…
Pseudocode:
- Doesn’t show how to recover the solution in this pseudocode!
- Update maximum value: If the weight of the current item is less than or equal to the current capacity, we have two choices: either include the current item or exclude it. We choose the option that maximizes the value. The maximum value achievable considering the current item is calculated as the maximum of two values:
- : Value of the current item plus the maximum value achievable with the remaining capacity and considering only the items up to .to-understand
- : current value of item , that we are considering including or not.
- : represents the maximum value achievable with the remaining capacity and considering only the tiems up to -th item. It’s the max value achievable with the remaining capacity , if we decide to include item in the knapsack. We consider only the items from the first to the -th item because we have already made a decision about including item . We cannot include the same item more than once in the 0/1 Knapsack Problem, so we only consider the items before .
- Putting these two terms together, represents the total value achieved by including item in the knapsack. It adds the value of item ii to the maximum value achievable with the remaining capacity if item is included.
- We compare this value with the maximum value achievable without including item ii, which is . We choose the maximum of these two values, as it represents the optimal decision for including or excluding item ii in the knapsack.
- : Maximum value achievable without including the current item.
- : Value of the current item plus the maximum value achievable with the remaining capacity and considering only the items up to .to-understand
Runtime:
- Something is hidden?
- What is the actual input size? Roughly what are the parameters of the input size? Twice of so and we also have for each of those numbers: . And also is given. It is an integer. How many bits you need to store in memory. = . Is written in terms of input size. NO. Let’s rewrite it in a way we can see the input size: .
Prof’s notes:
- 0/1 Knapsack example:
- Base case is first row and column being 0.
- Order of computation: go column by column (make sure to write the order of computation and not just draw arrows on the final exam)
- So check first if we can, if so, then look to the left and also the cell of the row - the value. and whatever in the cell + value?
- Flag those places in is taken or not: if not taken: taken from left, if taken, then the diagonal is taken.
Make sure to compute the values by yourself later!
- Watch a video on it
He’s keeping track of values in his array!!!