Knapsack
Knapsack
Unlike in fractional knapsack, the items are always stored fully without using the fractional
part of them. Its either the item is added to the knapsack or not. That is why, this method is
known as the 0-1 Knapsack problem.
Hence, in case of 0-1 Knapsack, the value of xi can be either 0 or 1, where other constraints
remain the same.
0-1 Knapsack cannot be solved by Greedy approach. Greedy approach does not ensure an
optimal solution in this method. In many instances, Greedy approach may give an
optimal solution.
The set of items to take can be deduced from the table, starting at c[n, w] and tracing
backwards where the optimal values came from.
If c[i, w] = c[i-1, w], then item i is not part of the solution, and we continue tracing with c[i-1,
w]. Otherwise, item i is part of the solution, and we continue tracing with c [i-1, w-W].
Dynamic-0-1-knapsack (v, w, n, W)
for w = 0 to W do
c[0, w] = 0
for i = 1 to n do
c[i, 0] = 0
for w = 1 to W do
if wi ≤ w then
if vi + c[i-1, w-wi] then
c[i, w] = vi + c[i-1, w-wi]
else c[i, w] = c[i-1, w]
else
c[i, w] = c[i-1, w]
The following examples will establish our statement.
Example
Let us consider that the capacity of the knapsack is W = 8 and the items are as shown in the
following table.
Item A B C D
Profit 2 4 7 10
Weight 1 3 5 7
Solution
Using the greedy approach of 0-1 knapsack, the weight that’s stored in the knapsack would
be A+B = 4 with the maximum profit 2 + 4 = 6. But, that solution would not be the optimal
solution.
Therefore, dynamic programming must be adopted to solve 0-1 knapsack problems.
Step 1
Construct an adjacency table with maximum weight of knapsack as rows and items with
respective weights and profits as columns.
Values to be stored in the table are cumulative profits of the items whose weights do not
exceed the maximum weight of the knapsack (designated values of each row)
The remaining values are filled with the maximum profit achievable with respect to the items
and weight per column that can be stored in the knapsack.
The formula to store the profit values is −
c[i,w]=max{c[i−1,w−w[i]]+P[i]}
To find the items to be added in the knapsack, recognize the maximum profit from the table
and identify the items that make up the profit, in this example, its {1, 7}.
The optimal solution is {1, 7} with the maximum profit is 12.
Analysis
This algorithm takes Ɵ(n.w) times as table c has (n+1).(w+1) entries, where each entry
requires Ɵ(1) time to compute.
Example
Following is the final implementation of 0-1 Knapsack Algorithm using Dynamic
Programming Approach.
#include <stdio.h>
#include <string.h>
int findMax(int n1, int n2){
if(n1>n2) {
return n1;
} else {
return n2;
}
}
int knapsack(int W, int wt[], int val[], int n){
int K[n+1][W+1];
for(int i = 0; i<=n; i++) {
for(int w = 0; w<=W; w++) {
if(i == 0 || w == 0) {
K[i][w] = 0;
} else if(wt[i-1] <= w) {
K[i][w] = findMax(val[i-1] + K[i-1][w-wt[i-1]], K[i-1]
[w]);
} else {
K[i][w] = K[i-1][w];
}
}
}
return K[n][W];
}
int main(){
int val[5] = {70, 20, 50};
int wt[5] = {11, 12, 13};
int W = 30;
int len = sizeof val / sizeof val[0];
printf("Maximum Profit achieved with this knapsack: %d",
knapsack(W, wt, val, len));
}