0% found this document useful (0 votes)
27 views

Lecture 7 DP 0-1 Knapsack

This document discusses dynamic programming and the 0-1 knapsack problem. It defines dynamic programming as solving problems by combining optimal solutions to overlapping subproblems. The 0-1 knapsack problem aims to maximize the total value of items packed in a knapsack of capacity W, where each item either fits entirely or not at all. A dynamic programming algorithm is presented to solve the 0-1 knapsack problem using a bottom-up approach storing results in a 2D table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Lecture 7 DP 0-1 Knapsack

This document discusses dynamic programming and the 0-1 knapsack problem. It defines dynamic programming as solving problems by combining optimal solutions to overlapping subproblems. The 0-1 knapsack problem aims to maximize the total value of items packed in a knapsack of capacity W, where each item either fits entirely or not at all. A dynamic programming algorithm is presented to solve the 0-1 knapsack problem using a bottom-up approach storing results in a 2D table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Dynamic Programmimg

Course Code: CSC2211 Course Title: Algorithms

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 07 Week No: 07 Semester: Spring 22-23


Lecturer: Md. Faruk Abdullah Al Sohan; [email protected]
Lecture Outline

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

 Given a problem P, obtain a


sequence of problems Q0, Q1,
…., Qm, where:
 You have a solution to Q0

 The solution to a problem Qj,


j > 0, can be obtained from
solutions to problems Q0...Qk,
k < j, that appear earlier in the
“sequence”.
Elements of Dynamic Programming

 DP is used to solve problems with the following characteristics:


 Optimal sub-structure (Principle of Optimality)
 an optimal solution to the problem contains within its optimal
solutions to sub-problems.
 Overlapping sub problems
 there exist some places where we solve the same sub problem more
than once
Example: Fibonacci Numbers

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)

 We keep calculating the same value over and over!


 Subproblems are overlapping – they share sub-subproblems
Steps to Designing a
Dynamic Programming Algorithm

1. Characterize optimal sub-structure

2. Recursively define the value of an optimal solution

3. Compute the value bottom up

4. (if needed) Construct an optimal solution


1.Optimal Sub-Structure

 An optimal solution to the problem contains optimal solutions to sub-


problems
 Solution to a problem:
 Making a choice out of a number of possibilities (look what possible choices there can be)

 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

 Write a recursive solution for the value of an optimal solution.

Combination of M opt for all subproblems resulting from choice k 


M opt  Optimal  
over all k choices   The cost associated with making the choice k 

 Show that the number of different instances of sub-problems is


bounded by a polynomial.
3. Bottom Up

 Compute the value of an optimal solution in a bottom-up fashion,


so that you always have the necessary sub-results pre-computed
(or use memoization)

 Check if it is possible to reduce the space requirements, by


“forgetting” solutions to sub-problems that will not be used any
more
4. Construct Optimal Solution

 Construct an optimal solution from computed


information (which records a sequence of choices
made that lead to an optimal solution)
Knapsack Problem
The Knapsack Problem

 The famous knapsack problem:


 A thief breaks into a museum. Fabulous paintings, sculptures,
and jewels are everywhere. The thief has a good eye for the
value of these objects, and knows that each will fetch hundreds
or thousands of dollars on the clandestine art collector’s market.
But, the thief has only brought a single knapsack to the scene of
the robbery, and can take away only what he can carry. What
items should the thief take to maximize the haul?
Knapsack 0-1 Problem

 The goal is to maximize the value of a


knapsack that can hold at most W
units (i.e. lbs or kg) worth of goods
from a list of items I0, I1, … In-1.

 Each item has 2 attributes:


 Value – let this be vi for item Ii
 Weight – let this be wi for item Ii
Knapsack 0-1 Problem

 The difference between this problem


and the fractional knapsack one is
that you CANNOT take a fraction of an
item.

 You can either take it or not.


 Hence the name Knapsack 0-1
problem.
0-1 Knapsack problem

 Given a knapsack with maximum capacity W, and a set S


consisting of n items

 Each item i has some weight wi and benefit value bi (all wi , bi


and W are integer values)

 Problem: How to pack the knapsack to achieve maximum


total value of packed items?
Knapsack 0-1 Problem

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..

Solve for smaller Knapsacks:


for W
In a Knapsack of size
Knapsack 0-1 Problem- Recursive Formula

 The best subset of Sk that has the total weight w, either contains item k or not.

 First case: wk > w


 Item k can’t be part of the solution! If it was the total weight would be > w, which is
unacceptable.

 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

Let’s run our algorithm on the following data:


 n = 4 (# of elements)
 W = 5 (max weight)
 Elements (weight, value): (2,3), (3,4), (4,5), (5,6)
Knapsack 0-1 Example
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0

// Initialize the base cases

for w = 0 to W

B[0,w] = 0

for i = 1 to n

B[i,0] = 0
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i = 1
1 0 0 vi = 3
2 0 wi = 2
3 0 w = 1
4 0 w-wi = -1

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 vi = 3
2 0 wi = 2
3 0 w=2
4 0 w-wi = 0

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 vi = 3
2 0 wi = 2
3 0 w=3
4 0 w-wi = 1

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 3 vi = 3
2 0 wi = 2
3 0 w=4
4 0 w-wi = 2

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=1
1 0 0 3 3 3 3 vi = 3
2 0 wi = 2
3 0 w=5
4 0 w-wi = 3

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 vi = 4
2 0 0 wi = 3
3 0 w=1
4 0 w-wi = -2

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 vi = 4
2 0 0 3 wi = 3
3 0 w=2
4 0 w-wi = -1

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 vi = 4
2 0 0 3 4 wi = 3
3 0 w=3
4 0 w-wi = 0

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 vi = 4
2 0 0 3 4 4 wi = 3
3 0 w=4
4 0 w-wi = 1

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=2
1 0 0 3 3 3 3 vi = 4
2 0 0 3 4 4 7 wi = 3
3 0 w=5
4 0 w-wi = 2

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=3
1 0 0 3 3 3 3 vi = 5
2 0 0 3 4 4 7 wi = 4
3 0 0 3 4 w = 1..3
4 0 w-wi = -3..-1

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=3
1 0 0 3 3 3 3 vi = 5
2 0 0 3 4 4 7 wi = 4
3 0 0 3 4 5 w=4
4 0 w-wi = 0

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=3
1 0 0 3 3 3 3 vi = 5
2 0 0 3 4 4 7 wi = 4
3 0 0 3 4 5 7 w=5
4 0 w-wi = 1

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=4
1 0 0 3 3 3 3 vi = 6
2 0 0 3 4 4 7 wi = 5
3 0 0 3 4 5 7 w = 1..4
4 0 0 3 4 5 w-wi = -4..-1

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
i/w 0 1 2 3 4 5
0 0 0 0 0 0 0 i=4
1 0 0 3 3 3 3 vi = 6
2 0 0 3 4 4 7 wi = 5
3 0 0 3 4 5 7 w=5
4 0 0 3 4 5 7 w-wi = 0

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
Items:

Knapsack 0-1 Example 1: (2,3)


2: (3,4)
3: (4,5)
4: (5,6)
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

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

3,7 3,5 3,6 3,4

2,7 2,4 2,5 2,2 2,6 2,3 2,4 2,1

1,4 1,4 1,4


Terrible running time
Consider the input in which
𝑇 ( 𝑛 , 𝑊 )=𝑇 ( 𝑛 −1 , 𝑊 ) + 𝑇 ( 𝑛 −1 , 𝑊 −𝑤𝑛 ) +1 ≥ 2𝑇 ( 𝑛− 1 ,𝑊 − 1 )+ 1

( ) (
𝑇 𝑛 , 𝑊 =Ω 2 min { 𝑛 , 𝑊 }
)

¿ 𝑠𝑢𝑏𝑝𝑟𝑜𝑏𝑙𝑒𝑚𝑠=𝑂 (𝑛𝑊 )
Running time

 O(nW)

 This is in general not polynomial since we only need bits to represent

 We say its pseudo-polynomial


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 >
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

1. Introduction to Algorithms, Third Edition, Thomas H. Cormen, Charle E. Leiserson,


Ronald L. Rivest, Clifford Stein (CLRS).
2. Fundamental of Computer Algorithms, Ellis Horowitz, Sartaj Sahni, Sanguthevar
Rajasekaran (HSR)
References

 https://algorithmist.com/wiki/Dynamic_programming

 https://www.topcoder.com/community/competitive-programming/tutorials/dynami
c-programming-from-novice-to-advanced/

You might also like