Lecture 7 DP 0-1 Knapsack
Lecture 7 DP 0-1 Knapsack
1. Dynamic Programming
2. Elements of Dynamic Programming
3. 0-1 knapsack Problem.
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(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
Items ,
Find a subset that fit in the Knapsack of Knapsack of
capacity W
maximum value
Max
W
Knapsack 0-1 Problem
( 𝑤 1 , 𝒗 𝟏 ) , ( 𝑤2 , 𝒗 𝟐 ) , ⋯ , ( 𝑤 𝑛 , 𝒗 𝒏 ) Knapsack of
capacity W
( 2,3 ) , ( 3,4 ) , ( 4 ,5 ) , ( 5,8 ) ,(9,10)
3 4 5 8
10
W
Knapsack 0-1 Problem
( 𝑤 1 , 𝒗 𝟏 ) , ( 𝑤2 , 𝒗 𝟐 ) , ⋯ , ( 𝑤 𝑛 , 𝒗 𝒏 ) Knapsack of
capacity W
( 2,3 ) , ( 3,4 ) , ( 4 ,5 ) , ( 5,8 ) ,(9,10)
3 4 5 8
10
W
Knapsack 0-1 Problem
( 𝑤 1 , 𝒗 𝟏 ) , ( 𝑤2 , 𝒗 𝟐 ) , ⋯ , ( 𝑤 𝑛 , 𝒗 𝒏 ) Knapsack of
capacity W
( 2,3 ) , ( 3,4 ) , ( 4 ,5 ) , ( 5,8 ) ,(9,10) 3
4 5
8 W
10
26
A recursive solution?
( 𝑤 1 , 𝒗 𝟏 ) , ( 𝑤2 , 𝒗 𝟐 ) , ⋯ , ( 𝑤 𝑛 , 𝒗 𝒏 ) Knapsack of
capacity W
The optimal solution for
may not be so helpful..
The best subset of Sk that has the total weight w, either contains item k or not.
Second case: wk ≤ w
Then the item k can be in the solution, and we choose the case with greater value.
Knapsack 0-1 Algorithm
for w = 0 to W { // Initialize 1st row to 0’s
B[0,w] = 0 }
for i = 1 to n { // Initialize 1st column to 0’s
B[i,0] = 0}
for i = 1 to n {
for w = 0 to W {
if wi <= w { //item i can be in the solution
if vi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = vi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
}
else B[i,w] = B[i-1,w] // wi > w
}
}
Knapsack 0-1 Example
for w = 0 to W
B[0,w] = 0
for i = 1 to n
B[i,0] = 0
Items:
We’re DONE!!
The max possible value that can be carried in this knapsack is $7
How do we compute the items ?
• The information is in the table
while
if then
add the item to the Knapsack
else
Find the Knapsack
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Find the Knapsack
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Find the Knapsack
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7
4 0 0 3 4 5 7
Overlapping substructures
𝐵 ( 𝑘 ,𝑊 )=
{ 𝐵 ( 𝑘 − 1, 𝑊 ) 𝑤 𝑘>𝑊
¿ max {𝐵 ( 𝑘 − 1 ,𝑊 ) , 𝒗 𝒌 + 𝐵(𝑘 −1 ,𝑊 −𝑤𝑘 )}𝑤𝑘 ≤ 𝑊
,
5,7
4,7 4,6
( ) (
𝑇 𝑛 , 𝑊 =Ω 2 min { 𝑛 , 𝑊 }
)
¿ 𝑠𝑢𝑏𝑝𝑟𝑜𝑏𝑙𝑒𝑚𝑠=𝑂 (𝑛𝑊 )
Running time
O(nW)
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 >
What is the running time of this
algorithm?
O(n*W)
Remember that the brute-force algorithm takes O(2n)
Comments
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
Class works
Knapsack Size = 25
Knapsack size = 60
Books
https://algorithmist.com/wiki/Dynamic_programming
https://www.topcoder.com/community/competitive-programming/tutorials/dynami
c-programming-from-novice-to-advanced/