Beruflich Dokumente
Kultur Dokumente
http://www.cse.unl.edu/~goddard/Courses/CSCE310J
1 2
3 4
1
Knapsack problem 0-1 Knapsack problem
There are two versions of the problem: Given a knapsack with maximum capacity W, and
1. “0-1 knapsack problem” and
2. “Fractional knapsack problem” a set S consisting of n items
Each item i has some weight wi and benefit value
1. Items are indivisible; you either take an item or bi (all wi , bi and W are integer values)
not. Solved with dynamic programming
Problem: How to pack the knapsack to achieve
2. Items are divisible: you can take any fraction of maximum total value of packed items?
an item. Solved with a greedy algorithm.
We have already seen this version
7 8
2
Defining a Subproblem Defining a Subproblem
w1 =2 w2 =4 w3 =5 w4 =3 Weight Benefit
If items are labeled 1..n, then a subproblem would be b1 =3 b2 =5 b3 =8 b4 =4 Item wi bi
#
to find an optimal solution for Sk = {items labeled ? 1 2 3
1, 2, .. k} Max weight: W = 20
S4 2 3 4
For S4:
Total weight: 14; S5
3 4 5
This is a reasonable subproblem definition. Maximum benefit: 20
4 5 8
The question is: can we describe the final solution 5 9 10
(Sn ) in terms of subproblems (Sk)? w1 =2 w2 =4 w3 =5 w5 =9
b1 =3 b2 =5 b3 =8 b5 =10
Unfortunately, we can’t do that. Solution for S4 is
For S5:
Total weight: 20 not part of the
13
Maximum benefit: 26 solution for S5!!! 14
As we have seen, the solution for S4 is not part of Recursive formula for subproblems:
the solution for S5 B[ k − 1, w ] if wk > w
B[ k , w ] =
15 16
for i = 1 to n
B[i,0] = 0
The best subset of Sk that has the total weight w, for i = 1 to n
either contains item k or not. for w = 0 to W
First case: wk>w. Item k can’t be part of the if wi <= w // item i can be part of the solution
solution, since if it was, the total weight would be if bi + B[i-1,w-wi] > B[i-1,w]
> w, which is unacceptable. B[i,w] = bi + B[i-1,w- wi]
Second case: wk ≤ w. Then the item k can be in else
the solution, and we choose the case with greater B[i,w] = B[i-1,w]
value. else B[i,w] = B[i-1,w] // wi > w
17 18
3
Running time Example
for w = 0 to W
O(W)
B[0,w] = 0
for i = 1 to n Let’s run our algorithm on the
B[i,0] = 0 following data:
for i = 1 to n Repeat n times
for w = 0 to W O(W) n = 4 (# of elements)
< the rest of the code >
W = 5 (max weight)
What is the running time of this algorithm?
Elements (weight, benefit):
O(n*W) (2,3), (3,4), (4,5), (5,6)
Remember that the brute-force algorithm
takes O(2n) 19 20
for w = 0 to W for i = 1 to n
B[0,w] = 0 B[i,0] = 0
21 22
Items: Items:
1: (2,3) 1: (2,3)
Example (4) 2: (3,4) Example (5) 2: (3,4)
3: (4,5) 3: (4,5)
i\W 0 1 2 3 4 5 i\W 0 1 2 3 4 5
i=1 4: (5,6) i=1 4: (5,6)
0 0 0 0 0 0 0 0 0 0 0 0 0 0
bi=3 bi=3
1 0 0 1 0 0 3
wi=2 wi=2
2 0 2 0
w=1 w=2
3 0 3 0
w-wi =-1 w-wi =0
4 0 4 0
if wi <= w // item i can be part of the solution if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w] if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi] B[i,w] = bi + B[i-1,w- wi]
else else
B[i,w] = B[i-1,w] B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w else B[i,w] = B[i-1,w] // wi > w
23 24
4
Items: Items:
1: (2,3) 1: (2,3)
Example (6) 2: (3,4) Example (7) 2: (3,4)
3: (4,5) 3: (4,5)
i\W 0 1 2 3 4 5 i\W 0 1 2 3 4 5
i=1 4: (5,6) i=1 4: (5,6)
0 0 0 0 0 0 0 0 0 0 0 0 0 0
bi=3 bi=3
1 0 0 3 3 1 0 0 3 3 3
wi=2 wi=2
2 0 2 0
w=3 w=4
3 0 3 0
w-wi =1 w-wi =2
4 0 4 0
if wi <= w // item i can be part of the solution if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w] if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi] B[i,w] = bi + B[i-1,w- wi]
else else
B[i,w] = B[i-1,w] B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w else B[i,w] = B[i-1,w] // wi > w
25 26
Items: Items:
1: (2,3) 1: (2,3)
Example (8) 2: (3,4) Example (9) 2: (3,4)
3: (4,5) 3: (4,5)
i\W 0 1 2 3 4 5 i\W 0 1 2 3 4 5
i=1 4: (5,6) i=2 4: (5,6)
0 0 0 0 0 0 0 0 0 0 0 0 0 0
bi=3 bi=4
1 0 0 3 3 3 3 1 0 0 3 3 3 3
wi=2 wi=3
2 0 2 0 0
w=5 w=1
3 0 3 0
w-wi =3 w-wi =-2
4 0 4 0
if wi <= w // item i can be part of the solution if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w] if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi] B[i,w] = bi + B[i-1,w- wi]
else else
B[i,w] = B[i-1,w] B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w else B[i,w] = B[i-1,w] // wi > w
27 28
Items: Items:
1: (2,3) 1: (2,3)
Example (10) 2: (3,4) Example (11) 2: (3,4)
3: (4,5) 3: (4,5)
i\W 0 1 2 3 4 5 i\W 0 1 2 3 4 5
i=2 4: (5,6) i=2 4: (5,6)
0 0 0 0 0 0 0 0 0 0 0 0 0 0
bi=4 bi=4
1 0 0 3 3 3 3 1 0 0 3 3 3 3
wi=3 wi=3
2 0 0 3 2 0 0 3 4
w=2 w=3
3 0 3 0
w-wi =-1 w-wi =0
4 0 4 0
if wi <= w // item i can be part of the solution if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w] if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi] B[i,w] = bi + B[i-1,w- wi]
else else
B[i,w] = B[i-1,w] B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w else B[i,w] = B[i-1,w] // wi > w
29 30
5
Items: Items:
1: (2,3) 1: (2,3)
Example (12) 2: (3,4) Example (13) 2: (3,4)
3: (4,5) 3: (4,5)
i\W 0 1 2 3 4 5 i\W 0 1 2 3 4 5
i=2 4: (5,6) i=2 4: (5,6)
0 0 0 0 0 0 0 0 0 0 0 0 0 0
bi=4 bi=4
1 0 0 3 3 3 3 1 0 0 3 3 3 3
wi=3 wi=3
2 0 0 3 4 4 2 0 0 3 4 4 7
w=4 w=5
3 0 3 0
w-wi =1 w-wi =2
4 0 4 0
if wi <= w // item i can be part of the solution if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w] if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi] B[i,w] = bi + B[i-1,w- wi]
else else
B[i,w] = B[i-1,w] B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w else B[i,w] = B[i-1,w] // wi > w
31 32
Items: Items:
1: (2,3) 1: (2,3)
Example (14) 2: (3,4) Example (15) 2: (3,4)
3: (4,5) 3: (4,5)
i\W 0 1 2 3 4 5 i\W 0 1 2 3 4 5
i=3 4: (5,6) i=3 4: (5,6)
0 0 0 0 0 0 0 0 0 0 0 0 0 0
bi=5 bi=5
1 0 0 3 3 3 3 1 0 0 3 3 3 3
wi=4 wi=4
2 0 0 3 4 4 7 2 0 0 3 4 4 7
w= 1..3 w= 4
3 0 0 3 4 3 0 0 3 4 5
w- wi=0
4 0 4 0
if wi <= w // item i can be part of the solution if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w] if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi] B[i,w] = bi + B[i-1,w- wi]
else else
B[i,w] = B[i-1,w] B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w else B[i,w] = B[i-1,w] // wi > w
33 34
Items: Items:
1: (2,3) 1: (2,3)
Example (16) 2: (3,4) Example (17) 2: (3,4)
3: (4,5) 3: (4,5)
i\W 0 1 2 3 4 5 i\W 0 1 2 3 4 5
i=3 4: (5,6) i=4 4: (5,6)
0 0 0 0 0 0 0 0 0 0 0 0 0 0
bi=5 bi=6
1 0 0 3 3 3 3 1 0 0 3 3 3 3
wi=4 wi=5
2 0 0 3 4 4 7 2 0 0 3 4 4 7
w= 5 w= 1..4
3 0 0 3 4 5 7 3 0 0 3 4 5 7
w- wi=1
4 0 4 0 0 3 4 5
if wi <= w // item i can be part of the solution if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w] if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi] B[i,w] = bi + B[i-1,w- wi]
else else
B[i,w] = B[i-1,w] B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w else B[i,w] = B[i-1,w] // wi > w
35 36
6
Items:
1: (2,3)
Example (18) 2: (3,4) Comments
3: (4,5)
i\W 0 1 2 3 4 5 This algorithm only finds the max possible value
i=4 4: (5,6)
0 0 0 0 0 0 0
bi=6 that can be carried in the knapsack
1 0 0 3 3 3 3 » I.e., the value in B[n,W]
wi=5
2 0 0 3 4 4 7 To know the items that make this maximum value,
w= 5
3 0 0 3 4 5 7 an addition to this algorithm is necessary.
w- wi=0
4 0 0 3 4 5 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
37 38
Items:
How to find actual Knapsack 1: (2,3)
Items Finding the Items 2: (3,4)
3: (4,5)
All of the information we need is in the table. i\W 0 1 2 3 4 5 i=4 4: (5,6)
B[n,W] is the maximal value of items that can be 0 0 0 0 0 0 0 k= 5
placed in the Knapsack. 1 0 0 3 3 3 3 bi=6
Let i=n and k=W 2 0 0 3 4 4 7 wi=5
if B[i,k] ≠ B[i−1,k] then 3 0 0 3 4 5 7 B[i,k] = 7
mark the ith item as in the knapsack B[i−1,k] =7
4 0 0 3 4 5 7
i = i−1, k = k-wi i=n, k=W
else while i,k > 0
if B[i,k] ≠ B[i−1,k] then
i = i−1 // Assume the ith item is not in the knapsack mark the ith item as in the knapsack
// Could it be in the optimally packed knapsack? i = i−1, k = k-wi
else
39
i = i−1 40
Items: Items:
1: (2,3) 1: (2,3)
Finding the Items (2) 2: (3,4) Finding the Items (3) 2: (3,4)
3: (4,5) 3: (4,5)
i\W 0 1 2 3 4 5 i=4 i\W 0 1 2 3 4 5 i=3
4: (5,6) 4: (5,6)
0 0 0 0 0 0 0 k= 5 0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=6 1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5 2 0 0 3 4 4 7 wi=4
3 0 0 3 4 5 7 B[i,k] = 7 3 0 0 3 4 5 7 B[i,k] = 7
B[i−1,k] =7 B[i−1,k] =7
4 0 0 3 4 5 7 4 0 0 3 4 5 7
i=n, k=W i=n, k=W
while i,k > 0 while i,k > 0
if B[i,k] ≠ B[i−1,k] then if B[i,k] ≠ B[i−1,k] then
mark the ith item as in the knapsack mark the ith item as in the knapsack
i = i−1, k = k-wi i = i−1, k = k-wi
else else
i = i−1 41
i = i−1 42
7
Items: Items:
1: (2,3) 1: (2,3)
Finding the Items (4) 2: (3,4) Finding the Items (5) 2: (3,4)
3: (4,5) 3: (4,5)
i\W 0 1 2 3 4 5 i=2 i\W 0 1 2 3 4 5 i=1
4: (5,6) 4: (5,6)
0 0 0 0 0 0 0 k= 5 0 0 0 0 0 0 0 k= 2
1 0 0 3 3 3 3 bi=4 1 0 0 3 3 3 3 bi=3
2 0 0 3 4 4 7 wi=3 2 0 0 3 4 4 7 wi=2
3 0 0 3 4 5 7 B[i,k] = 7 3 0 0 3 4 5 7 B[i,k] = 3
B[i−1,k] =3 B[i−1,k] =0
4 0 0 3 4 5 7 4 0 0 3 4 5 7
k − wi=2 k − wi=0
i=n, k=W i=n, k=W
while i,k > 0 while i,k > 0
if B[i,k] ≠ B[i−1,k] then if B[i,k] ≠ B[i−1,k] then
mark the ith item as in the knapsack mark the ith item as in the knapsack
i = i−1, k = k-wi i = i−1, k = k-wi
else else
i = i−1 43
i = i−1 44
Items: Items:
1: (2,3) 1: (2,3)
Finding the Items (6) 2: (3,4) Finding the Items (7) 2: (3,4)
3: (4,5) 3: (4,5)
i\W 0 1 2 3 4 5 i=0 i\W 0 1 2 3 4 5
4: (5,6) 4: (5,6)
0 0 0 0 0 0 0 k= 0 0 0 0 0 0 0 0
1 0 0 3 3 3 3 1 0 0 3 3 3 3
2 0 0 3 4 4 7 2 0 0 3 4 4 7
3 0 0 3 4 5 7 The optimal 3 0 0 3 4 5 7 The optimal
knapsack knapsack
4 0 0 3 4 5 7 4 0 0 3 4 5 7
should contain should contain
i=n, k=W {1, 2} i=n, k=W {1, 2}
while i,k > 0 while i,k > 0
if B[i,k] ≠ B[i−1,k] then if B[i,k] ≠ B[i−1,k] then
mark the nth item as in the knapsack mark the nth item as in the knapsack
i = i−1, k = k-wi i = i−1, k = k-wi
else else
i = i−1 45
i = i−1 46
47 48
8
The Knapsack Problem:
Greedy Vs. Dynamic Memoization
The fractional problem can be solved Memoization is another way to deal with overlapping
subproblems in dynamic programming
greedily » After computing the solution to a subproblem, store it in a table
The 0-1 problem can be solved with a » Subsequent calls just do a table lookup
With memoization, we implement the algorithm
dynamic programming approach recursively:
» If we encounter a subproblem we have seen, we look up the
answer
» If not, compute the solution and add it to the list of subproblems
we have seen.
Must useful when the algorithm is easiest to implement
recursively
» Especially if we do not need solutions to all subproblems.
49 50
Conclusion
51