The 0-1 knapsack problem involves selecting items with given values and weights to maximize the total value without exceeding a weight capacity. It can be solved using a brute force approach in O(2^n) time or dynamic programming in O(n*c) time. Dynamic programming constructs a value matrix where each cell represents the maximum value for a given item and weight. The cell value is either from the item above or adding the item's value if the weight is less than remaining capacity. The last cell provides the maximum value solution.
2. Statement of the problem:
Given n items, each with corresponding value p and weight w,
find which items to place in the knapsack such that sum of all p
is maximum, and the sum of all w does not exceed the maximum
weight capacity c of the knapsack.
3. We can also express the problem as follows:
1
n
i i
i
p x
is maximum and
1
n
i i
i
w x c
where
0 item is not taken
1 item is taken
ix
4. Solution #1 : Brute force
We take all possible item combinations.
For any n items, the total number of combinations is
0
,
n
i
C i n
= 2n
We pick the combinations that satisfy the constraint and sort
each p and get the maximum.
This approach has complexity 2n
O .
5. Solution #2:
Dynamic Programming (Bottom-Top Computation)
Construct an n c value matrix V to compute a value in each cell
for every row in the matrix.
The last cell V[n, c] will give the solution to the maximum total
value.
7. Example:
i 1 2 3 4 5
p 30 20 40 70 60
w 4 1 2 5 3
n = 5
c = 10
8. Solution:
The value matrix can be viewed as bottom-top, with the first
row (i = 0) the bottom, and moving up to the succeeding
rows up to the top (i = n).
Row 0 of the value matrix all start with 0.
The column k starts at 0 and ends at c (the constraint).
k
i
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
9. Value at V[i, k] = Max(V[i - 1, k], pi + V[i- 1, k - wi])
where
ip - the value of the item at row i
iw - the weight of the item at row i
For each row, we search each column where 0ik w , i.e.,
the maximum is V[i - 1, k] (the cell above the current cell).
If 0ik w , compare 1,V i k and 1,i ip V i k w . The
maximum of the two is the value of ,V i k .
10. k
i
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 30 30 30 30 30 30 30
at i = 1, k = 4:
1, 0,4 0V i k V
1 130, 4p w , 14 4 4 0ik w w
11, 0,0 30 0 30i ip V i k w p V
11. Completing the value matrix:
k
i
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 30 30 30 30 30 30 30
2 0 20 20 20 30 50 50 50 50 50 50
3 0 20 40 60 60 60 70 90 90 90 90
4 0 20 40 60 60 70 90 110 130 130 130
5 0 20 40 60 80 100 120 120 130 150 170
The last cell at ,V n c is the solution to the maximum value.
12. The value matrix only showed the solution to the maximum
value, but not the individual items chosen.
Modify the last pseudocode to mark the cells where the
maximum is 1,i ip V i k w , where 0ik w .
k
i
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 30 30 30 30 30 30 30
2 0 20 20 20 30 50 50 50 50 50 50
3 0 20 40 60 60 60 70 90 90 90 90
4 0 20 40 60 60 70 90 110 130 130 130
5 0 20 40 60 80 100 120 120 130 150 170
13. Pseudocode to find the items selected:
k = c
for i = n down to 0:
if ,V i k is marked:
output item i
ik k w
15. Bottom-top computation has complexity O nc .
For large n, a vast improvement to 2n
O .
Any problem involving maximizing a total value while
satisfying a constraint can use this method, as long as the
items can only be either chosen or not, i.e., the item cannot
be broken into smaller parts.