Beruflich Dokumente
Kultur Dokumente
Dynamic programming
0-1 Knapsack problem
09/28/16
Simple Subproblems
We should be able to break the original problem to
smaller subproblems that have the same structure
Subproblem Overlap
Optimal subproblems to unrelated problems can
contain subproblems in common
09/28/16
Items
This is a knapsack
Max weight: W = 20
W = 20
09/28/16
Benefit value
wi
bi
2
3
10
max bi subject to wi W
iT
iT
10
Can we do better?
Yes, with an algorithm based on dynamic
programming
We need to carefully identify the
subproblems
11
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.
12
Defining a Subproblem
w1 =2 w2 =4
b1 =3 b2 =5
w3 =5
b3 =8
Weight Benefit
w4 =3
b4 =4
?
Max weight: W = 20
For S4:
Total weight: 14;
total benefit: 20
w1 =2 w2 =4
b1 =3 b2 =5
w3 =5
b3 =8
#
S4
S5
w4 =9
b4 =10
For S5:
Total weight: 20
total benefit: 26
09/28/16
wi
bi
10
Item
Solution for S4 is
not part of the
solution for S5!!! 13
Defining a Subproblem
(continued)
09/28/16
14
B[k 1, w]
if wk w
B[k , w]
max{B[k 1, w], B[k 1, w wk ] bk } else
09/28/16
15
Recursive Formula
B[k 1, w]
if wk w
B[k , w]
max{B[k 1, w], B[k 1, w wk ] bk } else
16
17
Running time
for w = 0 to W
O(W)
B[0,w] = 0
for i = 0 to n
Repeat n times
B[i,0] = 0
for w = 0 to W
O(W)
< the rest of the code >
18
Example
Lets 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)
09/28/16
19
W
0
1
2
3
4
5
Example (2)
0
0
0
0
0
0
0
for w = 0 to W
B[0,w] = 0
09/28/16
20
W
0
1
2
3
4
5
Example (3)
0
0
0
0
0
0
0
for i = 0 to n
B[i,0] = 0
09/28/16
21
W
0
1
2
3
4
5
Example (4)
0
0
0
0
0
0
0
0
0
i=1
bi=3
wi=2
w=1
w-wi =-1
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
22
W
0
1
2
3
4
5
Example (5)
0
0
0
0
0
0
0
0
0
3
i=1
bi=3
wi=2
w=2
w-wi =0
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
23
W
0
1
2
3
4
5
Example (6)
0
0
0
0
0
0
0
0
0
3
3
i=1
bi=3
wi=2
w=3
w-wi=1
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
24
W
0
1
2
3
4
5
Example (7)
0
0
0
0
0
0
0
0
0
3
3
3
i=1
bi=3
wi=2
w=4
w-wi=2
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
25
W
0
1
2
3
4
5
Example (8)
0
0
0
0
0
0
0
0
0
3
3
3
3
i=1
bi=3
wi=2
w=5
w-wi=2
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
26
W
0
1
2
3
4
5
Example (9)
0
0
0
0
0
0
0
0
0
3
3
3
3
0
0
i=2
bi=4
wi=3
w=1
w-wi=-2
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
27
W
0
1
2
3
4
5
Example (10)
0
0
0
0
0
0
0
0
0
3
3
3
3
0
0
3
i=2
bi=4
wi=3
w=2
w-wi=-1
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
28
W
0
1
2
3
4
5
Example (11)
0
0
0
0
0
0
0
0
0
3
3
3
3
0
0
3
4
i=2
bi=4
wi=3
w=3
w-wi=0
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
29
W
0
1
2
3
4
5
Example (12)
0
0
0
0
0
0
0
0
0
3
3
3
3
0
0
3
4
4
i=2
bi=4
wi=3
w=4
w-wi=1
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
30
W
0
1
2
3
4
5
Example (13)
0
0
0
0
0
0
0
0
0
3
3
3
3
0
0
3
4
4
7
i=2
bi=4
wi=3
w=5
w-wi=2
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
31
W
0
1
2
3
4
5
Example (14)
0
0
0
0
0
0
0
0
0
3
3
3
3
0
0
3
4
4
7
0
0
3
4
i=3
bi=5
wi=4
w=1..3
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
32
W
0
1
2
3
4
5
Example (15)
0
0
0
0
0
0
0
0
0
3
3
3
3
0
0
3
4
4
7
0
0
3
4
5
i=3
bi=5
wi=4
w=4
w- wi=0
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
33
W
0
1
2
3
4
5
Example (15)
0
0
0
0
0
0
0
0
0
3
3
3
3
0
0
3
4
4
7
0
0
3
4
5
7
i=3
bi=5
wi=4
w=5
w- wi=1
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
34
W
0
1
2
3
4
5
Example (16)
0
0
0
0
0
0
0
0
0
3
3
3
3
0
0
3
4
4
7
0
0
3
4
5
7
0
0
3
4
5
i=3
bi=5
wi=4
w=1..4
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
35
W
0
1
2
3
4
5
Example (17)
0
0
0
0
0
0
0
0
0
3
3
3
3
0
0
3
4
4
7
0
0
3
4
5
7
0
0
3
4
5
7
i=3
bi=5
wi=4
w=5
09/28/16
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
36
Comments
37
Conclusion
38
The End
09/28/16
39