Download - 0/1 knapsack
![Page 1: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/1.jpg)
05/02/23 1
CS 332 - Algorithms
Dynamic programming0-1 Knapsack problem
![Page 2: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/2.jpg)
05/02/23 2
Review: Dynamic programming DP is a method for solving certain kind of
problems DP can be applied when the solution of a
problem includes solutions to subproblems We need to find a recursive formula for the
solution We can recursively solve subproblems, starting
from the trivial case, and save their solutions in memory
In the end we’ll get the solution of the whole problem
![Page 3: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/3.jpg)
05/02/23 3
Properties of a problem that can be solved with dynamic programming
Simple Subproblems– We should be able to break the original problem to
smaller subproblems that have the same structure Optimal Substructure of the problems
– The solution to the problem must be a composition of subproblem solutions
Subproblem Overlap– Optimal subproblems to unrelated problems can
contain subproblems in common
![Page 4: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/4.jpg)
05/02/23 4
Review: Longest Common Subsequence (LCS)
Problem: how to find the longest pattern of characters that is common to two text strings X and Y
Dynamic programming algorithm: solve subproblems until we get the final solution
Subproblem: first find the LCS of prefixes of X and Y.
this problem has optimal substructure: LCS of two prefixes is always a part of LCS of bigger strings
![Page 5: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/5.jpg)
05/02/23 5
Review: Longest Common Subsequence (LCS) continued
Define Xi, Yj to be prefixes of X and Y of length i
and j; m = |X|, n = |Y| We store the length of LCS(Xi, Yj) in c[i,j] Trivial cases: LCS(X0 , Yj ) and LCS(Xi, Y0) is
empty (so c[0,j] = c[i,0] = 0 ) Recursive formula for c[i,j]:
otherwise]),1[],1,[max(
],[][ if1]1,1[],[
jicjicjyixjic
jic
c[m,n] is the final solution
![Page 6: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/6.jpg)
05/02/23 6
Review: Longest Common Subsequence (LCS)
After we have filled the array c[ ], we can use this data to find the characters that constitute the Longest Common Subsequence
Algorithm runs in O(m*n), which is much better than the brute-force algorithm: O(n 2m)
![Page 7: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/7.jpg)
05/02/23 7
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 , bi and W are integer values)
Problem: How to pack the knapsack to achieve maximum total value of packed items?
![Page 8: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/8.jpg)
05/02/23 8
0-1 Knapsack problem:a picture
W = 20
wi bi
109
85
54
43
32
Weight Benefit value
This is a knapsackMax weight: W = 20
Items
![Page 9: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/9.jpg)
05/02/23 9
0-1 Knapsack problem Problem, in other words, is to find
Ti
iTi
i Wwb subject to max
The problem is called a “0-1” problem, because each item must be entirely accepted or rejected.
Just another version of this problem is the “Fractional Knapsack Problem”, where we can take fractions of items.
![Page 10: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/10.jpg)
05/02/23 10
0-1 Knapsack problem: brute-force approach
Let’s first solve this problem with a straightforward algorithm
Since there are n items, there are 2n possible combinations of items.
We go through all combinations and find the one with the most total value and with total weight less or equal to W
Running time will be O(2n)
![Page 11: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/11.jpg)
05/02/23 11
0-1 Knapsack problem: brute-force approach
Can we do better? Yes, with an algorithm based on dynamic
programming We need to carefully identify the
subproblems
Let’s try this:If items are labeled 1..n, then a subproblem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k}
![Page 12: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/12.jpg)
05/02/23 12
Defining a Subproblem If items are labeled 1..n, then a subproblem
would be to find an optimal solution for Sk = {items labeled 1, 2, .. k}
This is a valid subproblem definition. The question is: can we describe the final
solution (Sn ) in terms of subproblems (Sk)? Unfortunately, we can’t do that.
Explanation follows….
![Page 13: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/13.jpg)
05/02/23 13
Defining a Subproblem
Max weight: W = 20For S4:Total weight: 14;total benefit: 20
w1 =2b1 =3
w2=3b2 =4
w3 =4b3 =5
w4 =5b4 =8 wi bi
10
85
54
43
32
Weight Benefit
9
Item#
4
3
2
1
5
S4
S5
w1 =2b1 =3
w2=4b2 =5
w3 =5b3 =8
w4 =9b4 =10
For S5:Total weight: 20total benefit: 26
Solution for S4 is not part of the solution for S5!!!
?
![Page 14: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/14.jpg)
05/02/23 14
Defining a Subproblem (continued)
As we have seen, the solution for S4 is not part of the solution for S5
So our definition of a subproblem is flawed and we need another one!
Let’s add another parameter: w, which will represent the exact weight for each subset of items
The subproblem then will be to compute B[k,w]
![Page 15: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/15.jpg)
05/02/23 15
Recursive Formula for subproblems
It means, that the best subset of Sk that has total weight w is one of the two:
1) the best subset of Sk-1 that has total weight w, or
2) the best subset of Sk-1 that has total weight w-wk plus the item k
else }],1[],,1[max{
if ],1[],[
kk
k
bwwkBwkBwwwkB
wkB
Recursive formula for subproblems:
![Page 16: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/16.jpg)
05/02/23 16
Recursive Formula
The best subset of Sk that has the total weight w, either contains item k or not.
First case: wk>w. Item k can’t be part of the solution, since if it was, the total weight would be > w, which is unacceptable
Second case: wk <=w. Then the item k can be in the solution, and we choose the case with greater value
else }],1[],,1[max{
if ],1[],[
kk
k
bwwkBwkBwwwkB
wkB
![Page 17: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/17.jpg)
05/02/23 17
0-1 Knapsack Algorithmfor w = 0 to W
B[0,w] = 0for i = 0 to n
B[i,0] = 0for w = 0 to Wif wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
elseB[i,w] = B[i-1,w]else B[i,w] = B[i-1,w] // wi > w
![Page 18: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/18.jpg)
05/02/23 18
Running timefor w = 0 to W
B[0,w] = 0for i = 0 to n
B[i,0] = 0for w = 0 to W
< the rest of the code >
What is the running time of this algorithm?
O(W)
O(W)
Repeat n times
O(n*W)Remember that the brute-force algorithm
takes O(2n)
![Page 19: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/19.jpg)
05/02/23 19
Example
Let’s run our algorithm on the following data:
n = 4 (# of elements)W = 5 (max weight)Elements (weight, benefit):(2,3), (3,4), (4,5), (5,6)
![Page 20: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/20.jpg)
05/02/23 20
Example (2)
for w = 0 to WB[0,w] = 0
0000
00
W0123
4
5
i 0 1 2 3
4
![Page 21: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/21.jpg)
05/02/23 21
Example (3)
for i = 0 to nB[i,0] = 0
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0
4
![Page 22: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/22.jpg)
05/02/23 22
Example (4)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=1bi=3wi=2w=1w-wi =-1
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
0
![Page 23: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/23.jpg)
05/02/23 23
Example (5)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=1bi=3wi=2w=2w-wi =0
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
03
![Page 24: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/24.jpg)
05/02/23 24
Example (6)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=1bi=3wi=2w=3w-wi=1
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
033
![Page 25: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/25.jpg)
05/02/23 25
Example (7)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=1bi=3wi=2w=4w-wi=2
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
0333
![Page 26: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/26.jpg)
05/02/23 26
Example (8)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=1bi=3wi=2w=5w-wi=2
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
03333
![Page 27: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/27.jpg)
05/02/23 27
Example (9)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=2bi=4wi=3w=1w-wi=-2
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
03333
0
![Page 28: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/28.jpg)
05/02/23 28
Example (10)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=2bi=4wi=3w=2w-wi=-1
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
03333
03
![Page 29: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/29.jpg)
05/02/23 29
Example (11)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=2bi=4wi=3w=3w-wi=0
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
03333
034
![Page 30: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/30.jpg)
05/02/23 30
Example (12)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=2bi=4wi=3w=4w-wi=1
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
03333
0344
![Page 31: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/31.jpg)
05/02/23 31
Example (13)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=2bi=4wi=3w=5w-wi=2
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
03333
03447
![Page 32: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/32.jpg)
05/02/23 32
Example (14)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=3bi=5wi=4w=1..3
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
03333
003447
034
![Page 33: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/33.jpg)
05/02/23 33
Example (15)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=3bi=5wi=4w=4w- wi=0
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
0 003447
0345
3333
![Page 34: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/34.jpg)
05/02/23 34
Example (15)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=3bi=5wi=4w=5w- wi=1
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
0 003447
03457
3333
![Page 35: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/35.jpg)
05/02/23 35
Example (16)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=3bi=5wi=4w=1..4
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
0 003447
03457
0345
3333
![Page 36: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/36.jpg)
05/02/23 36
Example (17)
if wi <= w // item i can be part of the solution if bi + B[i-1,w-wi] > B[i-1,w] B[i,w] = bi + B[i-1,w- wi] else B[i,w] = B[i-1,w] else B[i,w] = B[i-1,w] // wi > w
0000
00
W0123
4
5
i 0 1 2 3
0 0 0 0i=3bi=5wi=4w=5
Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)
4
0 003447
03457
03457
3333
![Page 37: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/37.jpg)
05/02/23 37
Comments
This algorithm only finds the max possible value that can be carried in the knapsack
To know the items that make this maximum value, an addition to this algorithm is necessary
Please see LCS algorithm from the previous lecture for the example how to extract this data from the table we built
![Page 38: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/38.jpg)
05/02/23 38
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
Running time (Dynamic Programming algorithm vs. naïve algorithm):– LCS: O(m*n) vs. O(n * 2m)– 0-1 Knapsack problem: O(W*n) vs. O(2n)
![Page 39: 0/1 knapsack](https://reader036.vdocuments.us/reader036/viewer/2022062902/58ef06671a28ab7f788b45dd/html5/thumbnails/39.jpg)
05/02/23 39
The End