DR B.C. Roy Engineering College, Durgapur: "Affiliated To MAKAUT"
DR B.C. Roy Engineering College, Durgapur: "Affiliated To MAKAUT"
“Affiliated to MAKAUT”
CA-2 (MCAN-303)
“REPORT WRITING”
ON
0-1 Knapsack Problem using Dynamic Programming approach
Roll: 2208055
Semester: 3rd
Session: 2022-2023
1. Introduction
2. Problem Statement
3. Example
4. Time Complexity
5. Applications
6. Conclusion
7. References
Introduction:
The knapsack problem is an optimization problem used to illustrate both problem and
solution. It derives its name from a scenario where one is constrained in the number of items
that can be placed inside a fixed-size knapsack. Given a set of items with specific weights
and values, the aim is to get as much value into the knapsack as possible given the weight
constraint of the knapsack. One of the classic combinatorial optimization problems is the 0-1
knapsack problem which finds many applications in the computing domain.
A simple solution is to consider all subsets of items and calculate the total weight and profit
of all subsets. Consider the only subsets whose total weight is smaller than W. From all such
subsets, pick the subset with maximum profit.
Problem Statement:
Given a Knapsack/Bag with W weight capacity and a list of N items with given v i value and
wi weight. Put these items in the knapsack in order to maximise the value of all the placed items
without exceeding the limit of the Knapsack. Given N items where each item has some weight and
profit associated with it and also given a bag with capacity W, [i.e., the bag can hold at most W
weight in it]. The task is to put the items into the bag such that the sum of profits associated with
them is the maximum possible.
Example:
Weights: {3, 4, 6, 5}
Profits: {2, 3, 1, 4}
xi = {1, 0, 0, 1}
= {0, 0, 0, 1}
= {0, 1, 0, 1}
The above are the possible combinations. 1 denotes that the item is completely picked and 0 means
that no item is picked. Since there are 4 items so possible combinations will be:
24 = 16; So, there are 16 possible combinations that can be made by using the above problem. Once
all the combinations are made, we have to select the combination that provides the maximum profit.
First,
we create a matrix shown as below:
0 1 2 3 4 5 6 7 8
0
1
2
3
4
In the above matrix, columns represent the weight, i.e., 8. The rows represent the profits and
weights of items. Here we have not taken the weight 8 directly, problem is divided into sub-
problems, i.e., 0, 1, 2, 3, 4, 5, 6, 7, 8. First, we write the weights in the ascending order and profits
according to their weights shown as below:
wi = {3, 4, 5, 6}
pi = {2, 3, 4, 1}
The first row and the first column would be 0 as there is no item for w=0
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
w1 = 3; Since we have only one item in the set having weight 3, but the capacity of the knapsack is
1. We cannot fill the item of 3kg in the knapsack of capacity 1 kg so add 0 at M[1][1] shown as
below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0
2 0
3 0
4 0
When i = 1, W = 2
w1 = 3; Since we have only one item in the set having weight 3, but the capacity of the knapsack is
2. We cannot fill the item of 3kg in the knapsack of capacity 2 kg so add 0 at M[1][2] shown as
below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0
2 0
3 0
4 0
w1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is also 3; therefore, we can fill the knapsack with an item of weight equal to 3. We put profit
corresponding to the weight 3, i.e., 2 at M[1][3] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2
2 0
3 0
4 0
When i=1, W = 4
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is 4; therefore, we can fill the knapsack with an item of weight equal to 3. We put profit
corresponding to the weight 3, i.e., 2 at M[1][4] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2
2 0
3 0
4 0
When i=1, W = 5
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is 5; therefore, we can fill the knapsack with an item of weight equal to 3. We put profit
corresponding to the weight 3, i.e., 2 at M[1][5] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2
2 0
3 0
4 0
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is 6; therefore, we can fill the knapsack with an item of weight equal to 3. We put profit
corresponding to the weight 3, i.e., 2 at M[1][6] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2
2 0
3 0
4 0
When i=1, W = 7
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is 7; therefore, we can fill the knapsack with an item of weight equal to 3. We put profit
corresponding to the weight 3, i.e., 2 at M[1][7] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2
2 0
3 0
4 0
When i =1, W =8
W1 = 3; Since we have only one item in the set having weight equal to 3, and weight of the knapsack
is 8; therefore, we can fill the knapsack with an item of weight equal to 3. We put profit
corresponding to the weight 3, i.e., 2 at M[1][8] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0
3 0
4 0
Now the value of 'i' gets incremented, and becomes 2.
When i =2, W = 1
The weight corresponding to the value 2 is 4, i.e., w 2 = 4. Since we have only one item in the set
having weight equal to 4, and the weight of the knapsack is 1. We cannot put the item of weight 4 in
a knapsack, so we add 0 at M[2][1] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0
3 0
4 0
When i =2, W = 2
The weight corresponding to the value 2 is 4, i.e., w 2 = 4. Since we have only one item in the set
having weight equal to 4, and the weight of the knapsack is 2. We cannot put the item of weight 4 in
a knapsack, so we add 0 at M[2][2] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0
3 0
4 0
When i =2, W = 3
The weight corresponding to the value 2 is 4, i.e., w 2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 3. We can put the item of weight 3 in a knapsack,
so we add 2 at M[2][3] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2
3 0
4 0
When i =2, W = 4
The weight corresponding to the value 2 is 4, i.e., w 2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 4. We can put item of weight 4 in a knapsack as
the profit corresponding to weight 4 is more than the item having weight 3, so we add 3 at M[2][4]
shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3
3 0
4 0
When i = 2, W = 5
The weight corresponding to the value 2 is 4, i.e., w 2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 5. We can put item of weight 4 in a knapsack and
the profit corresponding to weight is 3, so we add 3 at M[2][5] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3
3 0
4 0
When i = 2, W = 6
The weight corresponding to the value 2 is 4, i.e., w 2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 6. We can put item of weight 4 in a knapsack and
the profit corresponding to weight is 3, so we add 3 at M[2][6] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3
3 0
4 0
When i = 2, W = 7
The weight corresponding to the value 2 is 4, i.e., w 2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 7. We can put item of weight 4 and 3 in a
knapsack and the profits corresponding to weights are 2 and 3; therefore, the total profit is 5, so we
add 5 at M[2][7] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5
3 0
4 0
When i = 2, W = 8
The weight corresponding to the value 2 is 4, i.e., w 2 = 4. Since we have two items in the set having
weights 3 and 4, and the weight of the knapsack is 7. We can put item of weight 4 and 3 in a
knapsack and the profits corresponding to weights are 2 and 3; therefore, the total profit is 5, so we
add 5 at M[2][7] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0
4 0
When i = 3, W = 1
The weight corresponding to the value 3 is 5, i.e., w 3 = 5. Since we have three items in the set
having weights 3, 4, and 5, and the weight of the knapsack is 1. We cannot put neither of the items
in a knapsack, so we add 0 at M[3][1] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0
4 0
When i = 3, W = 2
The weight corresponding to the value 3 is 5, i.e., w 3 = 5. Since we have three items in the set
having weight 3, 4, and 5, and the weight of the knapsack is 1. We cannot put neither of the items in
a knapsack, so we add 0 at M[3][2] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0
4 0
When i = 3, W = 3
The weight corresponding to the value 3 is 5, i.e., w 3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively and weight of the knapsack is 3. The item with a weight 3 can be put
in the knapsack and the profit corresponding to the item is 2, so we add 2 at M[3][3] shown as
below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 2
4 0
When i = 3, W = 4
The weight corresponding to the value 3 is 5, i.e., w 3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively, and weight of the knapsack is 4. We can keep the item of either
weight 3 or 4; the profit (3) corresponding to the weight 4 is more than the profit corresponding to
the weight 3 so we add 3 at M[3][4] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3
4 0
When i = 3, W = 5
The weight corresponding to the value 3 is 5, i.e., w 3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively, and weight of the knapsack is 5. We can keep the item of either
weight 3, 4 or 5; the profit (3) corresponding to the weight 4 is more than the profits corresponding
to the weight 3 and 5 so we add 3 at M[3][5] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3
4 0
When i =3, W = 6
The weight corresponding to the value 3 is 5, i.e., w 3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively, and weight of the knapsack is 6. We can keep the item of either
weight 3, 4 or 5; the profit (3) corresponding to the weight 4 is more than the profits corresponding
to the weight 3 and 5 so we add 3 at M[3][6] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3
4 0
When i =3, W = 7
The weight corresponding to the value 3 is 5, i.e., w 3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively, and weight of the knapsack is 7. In this case, we can keep both the
items of weight 3 and 4, the sum of the profit would be equal to (2 + 3), i.e., 5, so we add 5 at M[3]
[7] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5
4 0
When i = 3, W = 8
The weight corresponding to the value 3 is 5, i.e., w 3 = 5. Since we have three items in the set of
weight 3, 4, and 5 respectively, and the weight of the knapsack is 8. In this case, we can keep both
the items of weight 3 and 4, the sum of the profit would be equal to (2 + 3), i.e., 5, so we add 5 at
M[3][8] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0
When i = 4, W = 1
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 1. The weight of all the items
is more than the weight of the knapsack, so we cannot add any item in the knapsack; Therefore, we
add 0 at M[4][1] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0
When i = 4, W = 2
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 2. The weight of all the items
is more than the weight of the knapsack, so we cannot add any item in the knapsack; Therefore, we
add 0 at M[4][2] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0
When i = 4, W = 3
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 3. The item with a weight 3
can be put in the knapsack and the profit corresponding to the weight 4 is 2, so we will add 2 at
M[4][3] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2
When i = 4, W = 4
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 4. The item with a weight 4
can be put in the knapsack and the profit corresponding to the weight 4 is 3, so we will add 3 at
M[4][4] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3
When i = 4, W = 5
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 5. The item with a weight 4
can be put in the knapsack and the profit corresponding to the weight 4 is 3, so we will add 3 at
M[4][5] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3 3
When i = 4, W = 6
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 6. In this case, we can put the
items in the knapsack either of weight 3, 4, 5 or 6 but the profit, i.e., 4 corresponding to the weight 6
is highest among all the items; therefore, we add 4 at M[4][6] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3 3 4
When i = 4, W = 7
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 7. Here, if we add two items of
weights 3 and 4 then it will produce the maximum profit, i.e., (2 + 3) equals to 5, so we add 5 at
M[4][7] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3 3 4 5
When i = 4, W = 8
The weight corresponding to the value 4 is 6, i.e., w4 = 6. Since we have four items in the set of
weights 3, 4, 5, and 6 respectively, and the weight of the knapsack is 8. Here, if we add two items of
weights 3 and 4 then it will produce the maximum profit, i.e., (2 + 3) equals to 5, so we add 5 at
M[4][8] shown as below:
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0 0 0 2 2 2 2 2 2
2 0 0 0 2 3 3 3 5 5
3 0 0 0 1 3 3 3 5 5
4 0 0 0 2 3 3 4 5 5
The last entry represents the maximum possible value that can be put into the
knapsack. As we can observe in the above table that 5 is the maximum profit among
all the entries.
So, maximum possible value that can be put into the knapsack = 5.
Time Complexity:
Each entry of the table requires constant time θ(1) for its computation.
It takes θ(nw) time to fill (n+1)(w+1) table entries.
It takes θ(n) time for tracing the solution since tracing process traces the n rows.
Thus, overall θ(nw) time is taken to solve 0/1 knapsack problem using dynamic
programming.
While making this report on 0-1 Knapsack problem using dynamic programming, we have learnt a
lot about the real-life problem. In this article we have learned about the knapsack problem, its types,
formulas, and the methods to solve this problem. The knapsack problem is a way to solve a problem
in such a way so that the capacity constraint of the knapsack doesn't break and we receive maximum
profit. In the next article, we will see it’s the first approach in detail to solve this problem.
References:
https://www.javatpoint.com/0-1-knapsack-problem
https://www.geeksforgeeks.org/0-1-knapsack-using-least-count-branch-and-bound/?
ref=lbp
https://www.gatevidyalay.com/0-1-knapsack-problem-using-dynamic-programming-
approach/#google_vignette
https://www.gatevidyalay.com/tag/application-of-0-1-knapsack-problem/
#google_vignette