15 Dynamic Programming 4
15 Dynamic Programming 4
1
Knapsack problem 0-1
Thief has a knapsack with maximum capacity W, and a
set S consisting of n items
Each item i has some weight wi and benefit value vi (all
wi , vi and W are integer values)
Problem: How to pack the knapsack to achieve
maximum total value of packed items?
Goal:
find xi such that for all xi = {0, 1}, i = 1, 2, .., n
wixi W and
xivi is maximum
2
Knapsack - Greedy Strategy 0-1
E.g.1:
Item 3 30 $120
Item 2 50 50 50 +
20 $100
Item 1 30
20 + 20 $100
10 10 $60
4
Knapsack - Dynamic Programming 0-1
P(i, w) – the maximum profit that can be
obtained from items 1 to i, if the
knapsack has size w
Case 1: thief takes item i
P(i, w) =vi + P(i - 1, w-wi)
Case 2: thief does not take item i
P(i, w) =P(i - 1, w)
Recursive Formula
P[i 1, w] if wi w
P[i, w]
max{vi P[i 1, w wk ], P[i 1, w]} else
The best subset that has the total weight w, either
contains item i or not.
First case: wi>w. Item i can’t be part of the
solution, since if it was, the total weight would be
> w, which is unacceptable
Second case: wi <=w. Then the item i can be in
the solution, and we choose the case with greater
value.
6
Knapsack - Dynamic Programming 0-1
Item i was taken Item i was not taken
0 0 0 0 0 0 0 0 0 0 0 0
0 first
0 second
i-1 0
i 0
0
n 0
W = 5 Item Weight Value
Example: 1 2 12
P(i, w) = max {vi + P(i - 1, w-wi), P(i - 1, w) } 2 1 10
3 3 20
0 1 2 3 4 5
4 2 15
0 0 0 0 0 0 0 P(1, 1) = P(0, 1) = 0
1 0 0 12 12 12 12 P(1, 2) = max{12+0, 0} = 12
2 0 10 12 22 22 22 P(1, 3) = max{12+0, 0} = 12
3 0 10 12 22 30 32 P(1, 4) = max{12+0, 0} = 12
4 0 10 15 25 30 37 P(1, 5) = max{12+0, 0} = 12
1 0 0 12 12 12 12
• Item 2
2 0 10 12 22 22 22
3 0 10 12 22 30 32 • Item 1
4 0 10 15 25 30 37
• Start at P(n, W)
• When you go left-up item i has been taken
• When you go straight up item i has not
been taken
Optimal Substructure
Consider the most valuable load that weights
at most W pounds
If we remove item j from this load
The remaining load must be the most
valuable load weighing at most W – wj that
can be taken from the remaining n – 1 items
Knapsack Algorithm 0-1
for w = 0 to W
P[0,w] = 0
O(n*W)
for i = 0 to n
P[i,0] = 0
for w = 0 to W
if wi <= w // item i can be part of the solution
if vi + P[i-1,w-wi] > P[i-1,w]
P[i,w] = vi + P[i-1,w- wi]
else
P[i,w] = P[i-1,w]
else P[i,w] = P[i-1,w] // wi > w
11
Overlapping Subproblems
P(i, w) = max {vi + P(i - 1, w-wi), P(i - 1, w) }
0: 1 w W
0 0 0 0 0 0 0 0 0 0 0 0
0
0
i-1 0
i 0
0
n 0
E.g.: all the subproblems shown in grey
may depend on P(i-1, w)
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)
13
Conclusion
Dynamic programming is a useful technique of
solving certain kind of problems
14