0/1 Knapsack Problem
Given a knapsack (bag or container) with:
- M – capacity of the knapsack
- n – number of objects
- w – an array consisting of the weights w1, w2, w3, …, wn
- P – an array consisting of the profits P1, P2, P3, …, Pn
- x – an array consisting of 0 or 1: xi = 0 means the ith object has not been selected, and xi = 1 means it has been selected.
The main objective is to place the objects into the knapsack so that the maximum profit is obtained:
Dynamic programming for 0/1 knapsack
Let i represent the ith object in question, to be selected or not, with weight wi and profit Pi, and let j be the remaining capacity, the aim being to get the maximum profit. is the profit obtained by considering all i objects with capacity j.
Case 1
If there are no objects (i = 0) or the capacity of the knapsack itself is zero (j = 0), the profit is 0:
Case 2
(the weight of the ith object is greater than the remaining capacity j). In this case the object is not selected, because it cannot fit into the knapsack.
Case 3
wi is less than or equal to j, so there are two alternatives:
- Reject the ith object ⇒ profit
- Select the ith object ⇒ profit
Whichever yields the maximum profit is selected:
Complete recurrence
gives the optimal profit obtained.
Example
Apply the bottom-up dynamic programming technique to the following instance of the knapsack problem with capacity M = 5.
| Item | Weight | Value |
|---|---|---|
| 1 | 2 | $12 |
| 2 | 1 | $10 |
| 3 | 3 | $20 |
| 4 | 2 | $15 |
Solution: N = 4, w1 = 2, w2 = 1, w3 = 3, w4 = 2, and P1 = 12, P2 = 10, P3 = 20, P4 = 15.
The solution matrix consists of N + 1 rows and M + 1 columns.
Step 1
In the bottom-up approach, start from the initial solutions i = 0 or j = 0. For i = 0 there are no objects, and for j = 0 the capacity is zero, so these entries are 0 (the profit table).
| i ↓ j → | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | |||||
| 2 | 0 | |||||
| 3 | 0 | |||||
| 4 | 0 |
Step 2: i = 1, wi = 2, Pi = 12
Check whether object 1 can be selected or not.
| wi vs j | Calculation |
|---|---|
| 2 > 1 | |
| 2 ≤ 2 | |
| 2 ≤ 3 | |
| 2 ≤ 4 | |
| 2 ≤ 5 |
Step 3: i = 2, wi = 1, Pi = 10
| wi vs j | Calculation |
|---|---|
| 1 ≤ 1 | |
| 1 ≤ 2 | |
| 1 ≤ 3 | |
| 1 ≤ 4 | |
| 1 ≤ 5 |
Step 4: i = 3, wi = 3, Pi = 20
| wi vs j | Calculation |
|---|---|
| 3 > 1 | |
| 3 > 2 | |
| 3 ≤ 3 | |
| 3 ≤ 4 | |
| 3 ≤ 5 |
Step 5: i = 4, wi = 2, Pi = 15
| wi vs j | Calculation |
|---|---|
| 2 > 1 | |
| 2 ≤ 2 | |
| 2 ≤ 3 | |
| 2 ≤ 4 | |
| 2 ≤ 5 |
The completed profit table
| j (capacity) → M | ||||||
|---|---|---|---|---|---|---|
| i ↓ | 0 | 1 | 2 | 3 | 4 | 5 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 12 | 12 | 12 | 12 |
| 2 | 0 | 10 | 12 | 22 | 22 | 22 |
| 3 | 0 | 10 | 12 | 22 | 30 | 32 |
| 4 | 0 | 10 | 15 | 25 | 30 | 37 |
, the optimal solution (the highlighted cells are the ones visited when finding the objects selected).
Objects selected
Initially , which indicates that no objects are selected. The remaining capacity is j = 5.
| Step | Compare | Result | Decision | Next |
|---|---|---|---|---|
| 1 | V[4, 5] = 37 and V[3, 5] = 32 | different | object 4 is selected, x(4) = 1 | j = 5 − w4 = 5 − 2 = 3; i = i − 1 = 3, so consider V[3, 3] |
| 2 | V[3, 3] = 22 and V[2, 3] = 22 | equal | object 3 is not selected, x(3) = 0 | j stays 3; i = 2 |
| 3 | V[2, 3] = 22 and V[1, 3] = 12 | different | object 2 is selected, x(2) = 1 | j = 3 − w2 = 3 − 1 = 2; i = 1, so consider V[1, 2] |
| 4 | V[1, 2] = 12 and V[0, 2] = 0 | different | object 1 is selected, x(1) = 1 | j = 2 − w1 = 2 − 2 = 0; i = 0 |
Check: profit = 12 + 10 + 15 = 37, and weight = 2 + 1 + 2 = 5 ≤ M.
Algorithm KNAPSACK(n, m, w, P, V)
ALGORITHM KNAPSACK(n, m, w, P, V)
/* To find the optimal solution for the knapsack problem using
dynamic programming.
n - number of objects
m - capacity of the knapsack
w - weights of all the objects
P - profits of all the objects
Output: V - optimal solution */
for i ← 0 to n do
for j ← 0 to m do
if (i = 0 or j = 0)
V[i,j] = 0
else if (w[i] > j)
V[i,j] = V[i-1,j]
else
V[i,j] = max(V[i-1,j], V[i-1,j-w[i]] + P[i])
end if
end for
end for
return