Week 09 (Dynamic Programming -Knapsack) (1)
Week 09 (Dynamic Programming -Knapsack) (1)
F(6) =
8
F(5 F(4
) )
F(4 F(3 F(3 F(2
) ) ) )
F(3 F(2 F(2 F(1 F(2 F(1 F(1 F(0
) ) ) ) ) ) ) )
F(2 F(1 F(1 F(0 F(1 F(0 F(1 F(0
) ) ) ) ) ) ) )
F(1 F(0
) )
FibonacciImproved(n)
FibonacciImproved(n)
01 if nn ≤
01 if ≤11 then
then return
return nn
02 Fim2 ←0
02 Fim2 ←0
03 Fim1 ←1
03 Fim1 ←1
04 for ii ←
04 for ← 22 to
to nn do
do
05
05 Fi ←
Fi ← Fim1
Fim1 ++ Fim2
Fim2
06
06 Fim2 ←
Fim2 ← Fim1
Fim1
07
07 Fim1 ←
Fim1 ← Fi
Fi
05
05 return
return FiFi
History
? Dynamic programming
? Invented in the 1957 by Richard Bellman as a general method for
optimizing multistage decision processes
? The term “programming” refers to a tabular method.
? Often used for optimization problems.
Dynamic Programming
🡽 Solves problems by combining the solutions to sub problems that contain common
sub-sub-problems.
🡽 Difference between DP and Divide-and-Conquer
🡽 Using Divide and Conquer to solve these problems is inefficient as the same
common sub-sub-problems must be solved many times.
🡽 DP will solve each of them once and their answers are stored in a table for future
reference.
Intuitive Explanation
F(6) =
8
F(5 F(4
) )
F(4 F(3 F(3 F(2
) ) ) )
F(3 F(2 F(2 F(1 F(2 F(1 F(1 F(0
) ) ) ) ) ) ) )
F(2 F(1 F(1 F(0 F(1 F(0 F(1 F(0
) ) ) ) ) ) ) )
F(1 F(0
) )
? Solving one or more sub-problems that are the result of a choice (characterize the space of
sub-problems)
? Show that solutions to sub-problems must themselves be optimal for the whole
solution to be optimal.
2. Recursive Solution
Knapsack size = 60
0-1 Knapsack problem: a picture
Weight Benefit value
Item wi bi
s 2
3
4
This is a knapsack 3
Max weight: W = 20 4 5
5 8
W = 20
9 10
The Knapsack Problem
? Can we do better?
4 5 8
5 9 10
w1 =2 w3 =4 w4 =5 w5 =9
b1 =3 b3=5 b4 =8 b5 =10
2) the best subset of Sk-1 that has total weight w-wk plus the
item k
Recursive Formula
? To show this for the 0-1 problem, consider the most valuable
load weighing at most W pounds
? If we remove item j from the load, what do we know about the
remaining load?
? A: remainder must be the most valuable load weighing at most
W - wj that thief could take from museum, excluding item j
Solving The Knapsack Problem
? Greedy-fractional-knapsack (w, v, W)
? FOR i =1 to n
do x[i] =0
weight = 0
while weight < W
do i = best remaining item
IF weight + w[i] ≤ W
then x[i] = 1
weight = weight + w[i]
else
x[i] = (w - weight) / w[i]
weight = W
return x
0-1 Knapsack Algorithm
for w = 0 to W
B[0,w] = 0
for i = 0 to n
B[i,0] = 0
for w = 0 to W
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
Running time
for w = 0 to W O(W)
B[0,w] = 0
for i = 0 to n
Repeat n
B[i,0] = 0 times
for w = 0 to W O(W)
< the rest of the code >
What is the running time of this
algorithm?
O(n*W)
Remember that the brute-force algorithm takes O(2n)
Example
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)
Example (2)
i 0 1 2 3 4
W
0 0
1 0
2 0
3 0
4 0
5 0
for w = 0 to W
B[0,w] = 0
Example (3)
i 0 1 2 3 4
W
0 0 0 0 0 0
1 0
2 0
3 0
4 0
5 0
for i = 0 to n
B[i,0] = 0
Example (4) Items:
i 0 1 2 3 4
1: (2,3)
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 i=1 4: (5,6)
2 0 bi=3
3 0 wi=2
4 0 w=1
5 0
w-wi =-1
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
Example (5) Items:
i 0 1 2 3 4
1: (2,3)
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 i=1 4: (5,6)
2 0 3 bi=3
3 0 wi=2
4 0 w=2
5 0
w-wi =0
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
Example (6) Items:
i 0 1 2 3 4
1: (2,3)
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 i=1 4: (5,6)
2 0 3 bi=3
3 0 3 wi=2
4 0 w=3
5 0
w-wi=1
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
Example (7) Items:
i 0 1 2 3 4
1: (2,3)
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 i=1 4: (5,6)
2 0 3 bi=3
3 0 3 wi=2
4 0 3 w=4
5 0
w-wi=2
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
Example (8) Items:
i 0 1 2 3 4
1: (2,3)
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 i=1 4: (5,6)
2 0 3 bi=3
3 0 3 wi=2
4 0 3 w=5
5 0 3
w-wi=2
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
Example (9) Items:
i 0 1 2 3 4
1: (2,3)
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 i=2 4: (5,6)
2 0 3 bi=4
3 0 3 wi=3
4 0 3 w=1
5 0 3
w-wi=-2
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
Example (10) Items:
i 0 1 2 3 4
1: (2,3)
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 i=2 4: (5,6)
2 0 3 3 bi=4
3 0 3 wi=3
4 0 3 w=2
5 0 3
w-wi=-1
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
Example (11) Items:
i 0 1 2 3 4
1: (2,3)
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 i=2 4: (5,6)
2 0 3 3 bi=4
3 0 3 4 wi=3
4 0 3 w=3
5 0 3
w-wi=0
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
Example (12) Items:
i 0 1 2 3 4
1: (2,3)
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 i=2 4: (5,6)
2 0 3 3 bi=4
3 0 3 4 wi=3
4 0 3 4 w=4
5 0 3
w-wi=1
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
Example (13) Items:
i 0 1 2 3 4
1: (2,3)
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 i=2 4: (5,6)
2 0 3 3 bi=4
3 0 3 4 wi=3
4 0 3 4 w=5
5 0 3 7
w-wi=2
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
Example (14) Items:
i 0 1 2 3 4
1: (2,3)
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
1 0 0 0 0 i=3 4: (5,6)
2 0 3 3 3 bi=5
3 0 3 4 4 wi=4
4 0 3 4 w=1..3
5 0 3 7
? 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 lecture for the example how to
extract this data from the table we built
Books
🡽 https://algorithmist.com/wiki/Dynamic_programming
🡽 https://www.topcoder.com/community/competitive-programming/tutorials/dynami
c-programming-from-novice-to-advanced/
🡽 CLRS: 15.3