0-1 CXCXVC
0-1 CXCXVC
1
Knapsack problem
Given some items, pack the knapsack to get
the maximum total value. Each item has some
weight and some value. Total weight that we can
carry is no more than some fixed number W.
So we must consider weights of items as well as
their values.
3
0-1 Knapsack problem
▪ Given a knapsack with maximum capacity W, and
a set S consisting of n items
▪ Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
▪ Problem: How to pack the knapsack to achieve
maximum total value of packed items?
4
0-1 Knapsack problem
▪ Problem, in other words, is to find
max bi subject to wi W
iT iT
5
0-1 Knapsack problem:
brute-force approach
6
0-1 Knapsack problem:
dynamic programming approach
▪ We can do better with an algorithm based on
dynamic programming
7
Defining a Subproblem
▪ Given a knapsack with maximum capacity W, and
a set S consisting of n items
▪ Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
▪ Problem: How to pack the knapsack to achieve
maximum total value of packed items?
8
Defining a Subproblem
▪ Let’s add another parameter: w, which will represent
the maximum weight for each subset of items
9
Recursive Formula for
subproblems
▪ The subproblem will then be to compute V[k,w], i.e.,
to find an optimal solution for Sk = {items labeled 1,
2, .. k} in a knapsack of size w
10
Recursive Formula for
subproblems (continued)
11
Recursive Formula
V [k − 1, w] if wk w
V [ k , w] =
max{V [k − 1, w],V [k − 1, w − wk ] + bk } else
12
0-1 Knapsack Algorithm
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n
for w = 0 to W
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
13
Running time
for w = 0 to W
O(W)
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n Repeat n times
for w = 0 to W O(W)
< the rest of the code >
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)
15
Example (2)
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1
2
3
4
for w = 0 to W
V[0,w] = 0
16
Example (3)
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
for i = 1 to n
V[i,0] = 0
17
Items:w,b
1: (2,3)
Example (4) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 bi=3
1 0 0
wi=2
2 0
w=1
3 0
w-wi =-1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
18
Items:
1: (2,3)
Example (5) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 bi=3
1 0 0 3
wi=2
2 0
w=2
3 0
w-wi =0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
19
Items:
1: (2,3)
Example (6) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 bi=3
1 0 0 3 3
wi=2
2 0
w=3
3 0
w-wi =1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
20
Items:
1: (2,3)
Example (7) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 bi=3
1 0 0 3 3 3
wi=2
2 0
w=4
3 0
w-wi =2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
21
Items:
1: (2,3)
Example (8) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 bi=3
1 0 0 3 3 3 3
wi=2
2 0
w=5
3 0
w-wi =3
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
22
Items:
1: (2,3)
Example (9) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3
wi=3
2 0 0
w=1
3 0
w-wi =-2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
23
Items:
1: (2,3)
Example (10) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3
w=2
3 0
w-wi =-1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
24
Items:
1: (2,3)
Example (11) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4
w=3
3 0
w-wi =0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
25
Items:
1: (2,3)
Example (12) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4 4
w=4
3 0
w-wi =1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
26
Items:
1: (2,3)
Example (13) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4 4 7
w=5
3 0
w-wi =2
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
27
Items:
1: (2,3)
Example (14) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=3 4: (5,6)
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
w= 1..3
3 0 0 3 4
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
28
Items:
1: (2,3)
Example (15) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=3 4: (5,6)
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
w= 4
3 0 0 3 4 5
w- wi=0
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
29
Items:
1: (2,3)
Example (16) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=3 4: (5,6)
0 0 0 0 0 0 0 bi=5
1 0 0 3 3 3 3
wi=4
2 0 0 3 4 4 7
w= 5
3 0 0 3 4 5 7
w- wi=1
4 0
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
30
Items:
1: (2,3)
Example (17) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0 bi=6
1 0 0 3 3 3 3
wi=5
2 0 0 3 4 4 7
w= 1..4
3 0 0 3 4 5 7
4 0 0 3 4 5
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
31
Items:
1: (2,3)
Example (18) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0 bi=6
1 0 0 3 3 3 3
wi=5
2 0 0 3 4 4 7
w= 5
3 0 0 3 4 5 7
w- wi=0
4 0 0 3 4 5 7
if wi <= w // item i can be part of the solution
if bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w- wi]
else
V[i,w] = V[i-1,w]
else V[i,w] = V[i-1,w] // wi > w
32
Comments
▪ This algorithm only finds the max possible value
that can be carried in the knapsack
» i.e., the value in V[n,W]
▪ To know the items that make this maximum value,
an addition to this algorithm is necessary
33
How to find actual Knapsack
Items
▪ All of the information we need is in the table.
▪ V[n,W] is the maximal value of items that can be
placed in the Knapsack.
▪ Let i=n and k=W
if V[i,k] V[i−1,k] then
mark the ith item as in the knapsack
i = i−1, k = k-wi
else
i = i−1 // Assume the ith item is not in the knapsack
// Could it be in the optimally packed knapsack?
34
Items:
1: (2,3)
Finding the Items 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5
3 0 0 3 4 5 7 V[i,k] = 7
V[i−1,k] =7
4 0 0 3 4 5 7
i=n, k=W
while i,k > 0
if V[i,k] V[i−1,k] then
mark the ith item as in the knapsack
i = i−1, k = k-wi
else
i = i−1 35
Items:
1: (2,3)
Finding the Items (2) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5
3 0 0 3 4 5 7 V[i,k] = 7
V[i−1,k] =7
4 0 0 3 4 5 7
i=n, k=W
while i,k > 0
if V[i,k] V[i−1,k] then
mark the ith item as in the knapsack
i = i−1, k = k-wi
else
i = i− 1 36
Items:
1: (2,3)
Finding the Items (3) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=3 4: (5,6)
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=5
2 0 0 3 4 4 7 wi=4
3 0 0 3 4 5 7 V[i,k] = 7
V[i−1,k] =7
4 0 0 3 4 5 7
i=n, k=W
while i,k > 0
if V[i,k] V[i−1,k] then
mark the ith item as in the knapsack
i = i−1, k = k-wi
else
i = i− 1 37
Items:
1: (2,3)
Finding the Items (4) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=2 4: (5,6)
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=4
2 0 0 3 4 4 7 wi=3
3 0 0 3 4 5 7 V[i,k] = 7
V[i−1,k] =3
4 0 0 3 4 5 7
k − wi=2
i=n, k=W
while i,k > 0
if V[i,k] V[i−1,k] then
mark the ith item as in the knapsack
i = i−1, k = k-wi
else
i = i− 1 38
Items:
1: (2,3)
Finding the Items (5) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0 k= 2
1 0 0 3 3 3 3 bi=3
2 0 0 3 4 4 7 wi=2
3 0 0 3 4 5 7 V[i,k] = 3
V[i−1,k] =0
4 0 0 3 4 5 7
k − wi=0
i=n, k=W
while i,k > 0
if V[i,k] V[i−1,k] then
mark the ith item as in the knapsack
i = i−1, k = k-wi
else
i = i− 1 39
Items:
1: (2,3)
Finding the Items (6) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=0 4: (5,6)
0 0 0 0 0 0 0 k= 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7 The optimal
knapsack
4 0 0 3 4 5 7
should contain
i=n, k=W {1, 2}
while i,k > 0
if V[i,k] V[i−1,k] then
mark the nth item as in the knapsack
i = i−1, k = k-wi
else
i = i−1 40
Items:
1: (2,3)
Finding the Items (7) 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 4: (5,6)
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7 The optimal
knapsack
4 0 0 3 4 5 7
should contain
i=n, k=W {1, 2}
while i,k > 0
if V[i,k] V[i−1,k] then
mark the nth item as in the knapsack
i = i−1, k = k-wi
else
i = i−1 41
Conclusion
▪ Dynamic programming is a useful technique of
solving certain kind of problems
▪ When the solution can be recursively described in
terms of partial solutions, we can store these
partial solutions and re-use them as necessary
(memorization)
▪ Running time of dynamic programming algorithm
vs. naïve algorithm:
» 0-1 Knapsack problem: O(W*n) vs. O(2n)
42
Exercise
43
Memorization (Memory Function Method)
▪ Goal:
» Solve only subproblems that are necessary and solve it only once
▪ Memorization is another way to deal with overlapping subproblems
in dynamic programming
▪ With memorization, we implement the algorithm recursively:
» If we encounter a new subproblem, we compute and store the solution.
» If we encounter a subproblem we have seen, we look up the answer
▪ Most useful when the algorithm is easiest to implement recursively
» Especially if we do not need solutions to all subproblems.
44
0-1 Knapsack Memory Function Algorithm
for i = 1 to n MFKnapsack(i, w)
for w = 1 to W if V[i,w] < 0
V[i,w] = -1 if w < wi
value = MFKnapsack(i-1, w)
for w = 0 to W else
V[0,w] = 0 value = max(MFKnapsack(i-1, w),
for i = 1 to n bi + MFKnapsack(i-1, w-wi))
V[i,0] = 0 V[i,w] = value
return V[i,w]
45
Fractional Knapsack
▪ In this case, items can be broken into smaller pieces, hence the thief can select
fractions of items.
▪ According to the problem statement,
There are n items in the store
Weight of ith item wi >0
Profit for ith item pi i>0 and
Capacity of the Knapsack is W
▪ In this version of Knapsack problem, items can be broken into smaller pieces.
So, the thief may take only a fraction xi of ith item.
0⩽ xi ⩽1
The ith item contributes the weight xi . wi to the total weight in the knapsack and
profit xi . pi to the total profit.
46
▪ The ith item contributes the weight xi.wi to the total weight in the knapsack
and profit xi..pi to the total profit.
▪ Hence, the objective of this algorithm is to
maximize∑(xi.pi)
subject to constraint,
∑ (xi.wi)⩽W
▪ It is clear that an optimal solution must fill the knapsack exactly, otherwise we
could add a fraction of one of the remaining items and increase the overall
profit.
▪ Thus, an optimal solution can be obtained by
∑(xi.wi)=W
▪ In this context, first we need to sort those items according to the value
of pi/wi, so that (pi+1)/(wi+1) ≤ pi/wi. Here, x is an array to store the fraction
of items.
47
Problem with weight of
knapsack W = 60
Item A B C D
Weight 40 10 20 24
Ratio (pi/wi)
(piwi) 7 10 6 5
48
As the provided items are not sorted based on pi/wi. After sorting, the items are as shown in the following table.
Item B A C D
Profit 100 280 120 120
Weight 10 40 20 24
Ratio (pi/
10 7 6 5
wi)
49
▪ After sorting all the items according to pi/wi. First all of B is chosen as
weight of B is less than the capacity of the knapsack. Next, item A is chosen,
as the available capacity of the knapsack is greater than the weight of A.
Now, C is chosen as the next item. However, the whole item cannot be
chosen as the remaining capacity of the knapsack is less than the weight of C.
▪ Hence, fraction of C (i.e. (60 − 50)/20) is chosen.
▪ Now, the capacity of the Knapsack is equal to the selected items. Hence, no
more item can be selected.
▪ The total weight of the selected items is 10 + 40 + 20 * (10/20) = 60
▪ And the total profit is 100 + 280 + 120 * (10/20) = 380 + 60 = 440
50
Algorithm: Greedy-Fractional-Knapsack
(w[1..n], p[1..n], W) If the provided items are
for i = 1 to n already sorted into a
do x[i] = 0 decreasing order of pi/wi,
weight = 0 then the while loop takes a
for i = 1 to n time in O(n); Therefore, the
if weight + w[i] ≤ W then total time including the sort
x[i] = 1 is in O(n logn)
weight = weight + w[i]
else
x[i] = (W - weight) / w[i]
weight = W
break
return x 51