Dynamic Programming: CSE373: Design and Analysis of Algorithms
Dynamic Programming: CSE373: Design and Analysis of Algorithms
Dynamic Programming
CSE373: Design and Analysis of Algorithms
The Knapsack Problem
The 0-1 knapsack problem
A thief robbing a store finds n items: the i-th item is worth vi
dollars and weights wi pounds (vi, wi integers)
The thief can only carry W pounds in his knapsack
Items must be taken entirely or left behind
Which items should the thief take to maximize the value of his
load?
Goal:
find xi such that for all xi = {0, 1}, i = 1, 2, .., n
wixi W and
xivi is maximum
0-1 Knapsack - Greedy Strategy
E.g.:
Item 3 30 $120
Item 2 50 50 50 +
20 $100
Item 1 30
20 + 20 $100
10 10 $60
P(i, w) =
vi + P(i - 1, w-wi)
Case 2: thief does not take item i
P(i, w) =
P(i - 1, w)
0-1 Knapsack - Dynamic Programming (DP)
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
0-1 Knapsack – DP Algorithm
for i ← 0 to n
do P(i,0) = 0
for w ← 0 to W
do P(0,w) = 0
for i from 1 to n
do for w from 0 to W
do if wi > w //if the capacity is not enough
then P(i, w) = P(i-1, w)
else
P(i,w) = max{ vi + P(i-1, w-wi), P(i-1, w) }
W = 5 Item Weight Value
Example: 1 2 12
2 1 10
P(i, w) = max {vi + P(i - 1, w-wi), P(i - 1, w) }
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+P(0,0), 0} = max{12+0, 0} = 12
2 0 10 12 22 22 22 P(1, 3) = max{12+P(0,1), 0} = max{12+0, 0} = 12
3 0 10 12 22 30 32 P(1, 4) = max{12+P(0,2), 0} = max{12+0, 0} = 12
4 0 10 15 25 30 37 P(1, 5) = max{12+P(0,3), 0} = max{12+0, 0} = 12
• 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