You are on page 1of 27

Dynamic Programming

Introduction to Algorithms „ Well known algorithm design techniques:


‰ Brute-Force
B t F (iterative)
(it ti ) algorithms
l ith
‰ Divide-and-conquer algorithms

„ Another strategy for designing algorithms is dynamic


p g
programming. g
‰ Used when problem breaks down into recurring small
subproblems

„ Dynamic programming is typically applied to


optimization problems
problems.
‰ In such problem there can be many solutions. Each solution
has a value, and we wish to find a solution with the optimal
Chapter 15: Dynamic Programming value.

Divide-and-conquer Divide-and-conquer - Example


„ Divide-and-conquer method for algorithm design:

„ Divide: If the input size is too large to deal with in a


straightforward manner, divide the problem into two or
more disjoint
j subproblems
p

„ Conquer:
q conquer
q recursively
y to solve the subproblems
p

„ Combine: Take the solutions to the subproblems and


“merge” these solutions into a solution for the original
problem
Dynamic programming Difference between DP and Divide
Divide-
„ Dynamic
y p
programming
g g is a way
y of improving
p g on and-Conquer
inefficient divide-and-conquer algorithms.
„ Using Divide-and-Conquer to solve these
„ By “inefficient”, we mean that the same recursive call
is made over and over. problems
bl iis inefficient
i ffi i t because
b th
the same
common subproblems have to be solved
„ If same subproblem is solved several times
times, we can use many times.
ti
table to store result of a subproblem the first time it is
computed and thus never have to recompute it again.
„ Dynamic programming is applicable when the „ DP will solve each of them once and their
p
subproblems are dependent,
p , that is,, when subproblems
p answers are stored in a table for future use
use.
share subsubproblems.
„ Programming refers to a tabular method
“Programming”

Elements of Dynamic Programming (DP) Steps to Designing a Dynamic


DP is used to solve problems with the following Programming Algorithm
characteristics:
1. Characterize optimal substructure
„ Simple subproblems
‰ We should be able to break the original problem to 2. Recursively define the value of an optimal
smaller subproblems that have the same structure
solution
l i
„ Optimal substructure of the problems
‰ The optimal
Th l ti to
ti l solution t the
th problem
bl contains
t i within
ithi 3 Compute the value bottom up
3.
optimal solutions to its subproblems.
„ O l
Overlapping
i sub-problems
b bl 4 (if needed) Construct an optimal solution
4.
‰ there exist some places where we solve the same
subproblem more than once.
once
Fibonacci Numbers Fibonacci Numbers
„ Fn= Fnn-11+ Fnn-22
Fn n≥2
„ F0 = 0,, F1 = 1
„ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …

„ Straightforward recursive procedure is slow!

„ Let’s draw the recursion tree

Fibonacci Numbers Ex1:Assembly-line


Ex1:Assembly line scheduling
„ We can calculate Fn in linear time by „ Cars factory with two assembly lines.
remembering solutions to the solved subproblems ‰ Each line has the same number “n” of stations. Numbered j = 1, 2, ...,
n.
– dynamic programming
‰ We denote the jth station on line i (where i is 1 or 2) by Si,j .
„ Compute solution in a bottom-up fashion ‰ The jth station on line 1 (S1,j) performs the same function as the jth
station on line 2 (S2,j ).
)
„ In this case, only two values need to be ‰ The time required at each station varies, even between stations at the
remembered at any time same position on the two different lines, as each assembly line has
different technology.

‰ time required at station Si,j,j is (ai,j,j) .

‰ There is also an entry time (ei) for the chassis to enter assembly line i
and an exit time (xi) for the completed auto to exit assembly line i.
Ex1:Assembly line scheduling
Ex1:Assembly-line Ex1:Assembly-line
y schedulingg
•(Time between adjacent stations are nearly 0).
„ After going through station Si,j, can either
‰ q stay on same line
„ next station is Si,j+1
„ no transfer cost , or

‰ q transfer to other line


„ next station is S3-i,j+1
„ transfer cost from Si,j to S3-i,j+1 is ti,j ( j = 1, … , n–1)
„ No ti,n because the assembly line is complete after Si,n

13

Problem Definition Step 1: Optimal Solution Structure


„ Problem: Given all these costs, what stations „ optimal
p substructure : choosing
g the best p
path
should be chosen from line 1 and from line 2 for to Sij.
minimizing the total time for car assembly.
„ The structure of the fastest way through the
„ “Brute
Brute force
force” is to try all possibilities
possibilities. factory (from the starting point)
‰ requires to examine Ω(2n) possibilities

„ The fastest possible way to get through Si,1 (i =


‰ Trying all 2n subsets is infeasible when n is large.
1, 2)
‰ Only one way: from entry starting point to Si,1
‰ Simple example : 2 stations Æ (2n) possibilities =4
‰ take time is entry time (ei)
Step 1: Optimal Solution Structure Step 1: Optimal Solution Structure
„ The fastest possible way to get through Si,j (i = 1, 2) (j = 2,
3 ..., n).
3, ) TTwo choices:
h i „ An optimal solution to a problem
‰ finding the fastest way to get through Si,j
‰ Stay in the same line: Si,j-1 Æ Si,j
‰ Time is Ti,j-1 + ai,j „ contains within it an optimal solution to sub-problems
sub problems
„ If the fastest way ‰ finding the fastest way to get through either Si,j-1 or S3-i,j-1
through g Si,j
i j is through
g „ Fastest way
y from starting
gppoint to Si,ji j is either:
Si,j-1, it must have ‰ The fastest way from starting point to Si,j-1 and then directly from
taken a fastest way Si,j-1 to Si,j
through Si,j-1 or
‰ Transfer to other line: ‰ The fastest way from starting point to S3-i,j-1 then a transfer from
S33-i,j-1
i,j 1 Æ Si,j line 3-i to line i and finallyy to Si,ji j
‰ Time is T3-i,j-1 + t3-i,j-1 + ai,j
„ Same as above „ ÆOptimal Substructure.

Example
Step 2: Recursive Solution
„ Define the value of an optimal solution recursively in
terms off the
h optimal
i l solution
l i to sub-problems
b bl

„ S b
Sub-problem
bl h
here
‰ finding the fastest way through station j on both lines (i=1,2)
‰ Let fi [j] be the fastest p
possible time to g
go from starting
gppoint
through Si,j

„ The fastest time to go all the way through the factory: f*

„ x1 and x2 are the exit times from lines 1 and 2,


respectively
p y

Step 2: Recursive Solution Example


„ The fastest time to go through Si,j
‰ e1 and e2 are the entry times for lines 1 and 2
Example Step 2: Recursive Solution
„ To help us keep track of how to construct an
optimal solution, let us define
‰ li[j ]: line # whose station j-1
j 1 is used in a fastest way
through Si,j (i = 1, 2, and j = 2, 3,..., n)

„ we avoid defining li[1] because no station


precedes station 1 on either lines.

„ We also define
‰ l*: the line whose station n is used in a fastest way
through the entire factory

Step 2: Recursive Solution Step 2: Recursive Solution


„ Using the values of l* and li[j] shown in Figure (b) in next
slide we would trace a fastest way through the factory
slide,
shown in part (a) as follows

‰ The fastest total time comes from choosing stations


‰ Line 1: 1, 3, & 6 Line 2: 2, 4, & 5
Step 3: Optimal Solution Value Step 3: Optimal Solution Value

Step 3: Optimal Solution Value Step 3: Optimal Solution Value

Use substitution method to solve it Æ O(2n) Æ exponential


Step 3: Optimal Solution Value Step 3: Optimal Solution Value

Step 3: Optimal Solution Value Step 4: Optimal Solution


„ Constructing the fastest way through the
factory
Multiplying Matrices Multiplying
p y g Matrices
„ matrices, A with (p x q) matrix and B with (q x r)
Two matrices
matrix, can be multiplied to get C with dimensions p x r,
using
g scalar multiplications
p

37 38

Ex2: Matrix
Matrix-chain
chain multiplication Matrix-chain lti li ti – cont.
M t i h i multiplication t
„ We are given a sequence (chain) <A1
<A1, A2,
A2 ..., An> of n
„ We can multiply two matrices A and B only if they are
matrices to be multiplied, and we wish to compute the
compatible
product. A1.A2…An.
p
‰ the number of columns of A must equal the number of rows of B
B.

„ Matrix multiplication
p is associative,, and so all „ If A is a p × q matrix and B is a q × r matrix,
matrix the resulting
parenthesizations yield the same product, matrix C is a p × r matrix.

(A1 (A2 (A3 A4))),


A4)))
(A1 ((A2 A3) A4)), „ The time to compute C is dominated by the number of
((A1 A2) (A3 A4)),
A4)) scalar multiplications,
p which is p q r.
((A1 (A2 A3)) A4),
(((A1 A2)) A3)) A4).
((( )

39 40
chain multiplication – cont.
Matrix-chain
Matrix cont chain multiplication – cont.
Matrix-chain
Matrix cont
„ Ex: consider the problem of a chain <A1, A2, A3> of three
matrices with the dimensions of 10 × 100,
matrices, 100 100 × 5,
5 and 5 × 50,
50 „ Matrix multiplication
p is associative
respectively. ‰ (AB)C = A(BC)

„ If we multiply according to ((A1 A2) A3)


A3), we perform 10 · 100 · 5 =
„ The parenthesization matters
5000 scalar multiplications to compute the 10 × 5 matrix product
A1 A2, plus another 10 · 5 · 50 = 2500 scalar multiplications to
multiply this matrix byA3 for a total of 7500 scalar multiplications „ Consider A X B X C X D, where
„ If instead , we multiply
p y as ((A1 ((A2 A3)),
)) we pperform 100 · 5 · 50 = ‰ A is 30x1, B is 1x40, C is 40x10, D is 10x25
25,000 scalar multiplications to compute the 100 × 50 matrix
product A2 A3, plus another 10 · 100 · 50 = 50,000 scalar „ Costs:
multiplications to multiply A1 by this matrix
matrix, for a total of 75,000
75 000 ‰ ((AB)C)D = 1200 + 12000 + 7500 = 20700
scalar multiplications. ‰ (AB)(CD) = 1200 + 10000 + 30000 = 41200
„ Thus,, computing
p g the p
product according g to the first ‰ A((BC)D) = 400 + 250 + 750 = 1400
parenthesization is 10 times faster.
41 42

Matrix Chain Multiplication (MCM) Problem Matrix Chain Multiplication (MCM) Problem
„ Input:
p „ Typically, the time invested in determining this
Typically
‰ Matrices A1, A2, …, An, each Ai of size pi-1 x pi, optimal order is more than paid for by the time
saved later on when actually performing the
„ Output:
‰ Fully parenthesised product A1 x A2 x … x An that
matrix multiplications
minimizes the number of scalar multiplications.
multiplications
„ So, exhaustively checking all possible
„ Note: parenthesizations does not yield an efficient
‰ In MCM problem, we are not actually multiplying algorithm
matrices
‰ O goall iis only
Our l tto d
determine
t i an order
d ffor multiplying
lti l i
matrices that has the lowest cost

43 44
Counting the Number of Parenthesizations Counting the Number of Parenthesizations
„ Denote the number of alternative parenthesizations of a
sequence off (n)
( ) matrices
ti by
b P(n)
P( ) then
th a fully
f ll
parenthesized matrix product is given by:

(A1 (A2 (A3 A4))) , If n = 4 <A1,A2,A3,A4> then the number of


(A1 ((A2 A3) A4)) , alternative parenthesis is =5
((A1 A2) (A3 A4)) , P(4) =P(1) P(4-1)
P(4 1) + P(2) P(4-2)
P(4 2) + P(3) P(4-3)
P(4 3)
((A1 (A2 A3)) A4) , = P(1) P(3) + P(2) P(2) + P(3) P(1)= 2+1+2=5
(((A1 A2) A3) A4). P(3) =P(1) P(2) + P(2) P(1) = 1+1=2
P(2) =P(1) P(1)= 1

45 46

Step 1: Optimal Sub-structure Step 1: Optimal Sub-structure

47 48
Step 1: Optimal Sub-structure
Sub structure Step 1: Optimal Sub-structure
Sub structure

49 50

Step 2: Recursive Solution Step 2: Recursive Solution


„ For MCM, sub-problems are:
‰ problems of determining the min-cost of a
parenthesization of Ai Ai+1…Aj for 1 <= i <= j <= n

„ Let m[i, j] = min # of scalar multiplications


needed
d d tto computet th t i Ai...j.
the matrix
‰ For the whole problem, we need to find m[1, n]

„ Since Ai..j can be obtained by breaking it into


Ai..k Ak+1..
1 j, We
W have
h h Ai off size
((each i pii-1
1 x pii)
‰ m[i, j] = m[i, k] + m[k+1, j] + pi-1pkpj

51 52
Step
p 3: Computing
p g the Optimal
p Costs Elements of Dynamic
y Programming
g g

53 54

Overlapping Sub-problems Step


p 3: Computing
p g the Optimal
p Costs
The following code assumes that matrix Ai has
dimensions ppi-1 x p
pi for i =1,2,
, , …,, n

55 56
Step
p 3: Computing
p g the Optimal
p Costs Step 3: Computing the Optimal Costs

57 58

Step 3: Computing the Optimal Costs Step 3: Computing the Optimal Costs

59 60
Step 3: Computing the Optimal Costs Step 3: Computing the Optimal Costs

61 62

Step 4: Constructing an Optimal Solution Step 4: Constructing an Optimal Solution

„ Although Matrix-Chain-Order determines the


optimal number of scalar multiplications
needed to compute a matrix-chain
matrix chain product
product, it
does not directly show how to multiply the
matrices

„ Using table s, we can construct an optimal


solution

63 64
Step 4: Constructing an Optimal Solution Step 4: Constructing an Optimal Solution

65 66

Step 4: Constructing an Optimal Solution Example: Matrix Chain Multiplication


„ What if we want to multiply a string of more than two matrices
together?
„ Matrix multiplication is associative, that is A(BC) = (AB)C, so
the result is the same no matter in what order we multiply the
matrices, but it can make a big difference in the amount of
work that is done.
A1 x A2 x A3 x A4
30 x 1 1 x 40 40 x 10 10 x 25
((A1A2)A3)A4 30*1*40 + 30*40*10 + 30*10*25 = 20,700
A1(A2(A3A4)) 40*10*25 + 1*40*25 + 30*1*25 = 11,750
11 750
(A1A2)(A3A4) 30*1*40 + 40*10*25 + 30*40*25 = 41,200
A1((A2A3)A4) 1*40*10 + 1*10*25 + 30*1*25 = 1,400
67 68
A1 x A2 x A3 x A4 A1 x A2 x A3 x A4
30 x 1 1 x 40 40 x 10 10 x 25 30 x 1 1 x 40 A x40
A x 10 10 x 25
A1 x A2 2 3
30*1*40
30 1 40 1*40*10
A3 x A4
[1] [2] [3] [4] [1] [2] [3] [4] 40*10*25

0 [1] 0 1200 [1]


0 [2] 0 400 [2]
0 [3] 0 10000 [3]
0 [4] 0 [4]
First, the costs of multiplying Then the costs of multiplying
chains
h i off llength
th 1 are initialized
i iti li d to
t 0 chains of length 2 are calculated
(only one way to multiply
chains of length two)

69 70

A1 x A2 x A3 x A4 A1 x A2 x A3 x A4
30 x 1 1 x 40 40 x 10 10 x 25 30 x 1 1 x 40 40 x 10 10 x 25

[1] [2] [3] [4] [1] [2] [3] [4]


0 1200 700 [1] 0 1200 700 [1]
0 400 650 [2] 0 400 650 [2]
0 10000 [3] 0 10000 [3]
BEST 0 [4] 0 [4]
A1 x (A2 x A3)
0 + 400 + 30*1*10 = 700
Then the best way to multiply A2 x (A3 x A4)
0 + 10,000 + 1*40*25 = 11,000
chains of length 3 are calculated (A2 x A3) x A4 BEST
(A1 x A2) x A3
400 + 0 + 1*10*25 = 650
1200 + 0 + 30*40*10 = 13,200
71 72
A1 x A2 x A3 x the
Then A4best wayy to A1 x A2 x A3 x A4
30 x 1 1 x 40 40 x 10 Multiply
10 x the
25 entire 30 x 1 1 x 40 40 x 10 10 x 25
chain is calculated
[1] [2] [3] [4]
[1] [2] [3] [4]
cost = 0 1200 700 1400 [1]
0 1200 700 1400 [1] 0 400 650 [2]

0 400 650 [2] 0 10000 [3]


0 [4]
0 10000 [3] split =
BEST 0 [4]
[2] [3] [4]
the array split is used
A1 x (A2 x A3 x A4)
(A1 x A2 x A3 ) x A4
1 1 1 [1]
0 + 650 + 30*1*25 = 1400
to reconstruct the
700 + 0 + 30*10*25 = 10,450 optimal ordering 2 3 [2]
3 [3]
(A1 x A2 )(A3 x A4)
1200 + 10,000 + 30*40*25 = 41,200
73 74

Dynamic Programming Solution [2] [3] [4]


1 1 1 [1]
PRINT-OPTIMAL-PARENS(split, ii, j)
PRINT-OPTIMAL-PARENS(split
2 3 [2]
if(i == j) // base case -- chain of length 1, just print Ai
print “A”
A i P-O-P(split,1,4)
(p , , ) 3 [[3]]
else
P-O-P(split,1,1) P-O-P(split,2,4)
print “(“
(
PRINT-OPTIMAL-PARENS(split, i, split[i,j])
P-O-P(split,2,3)
O (sp , ,3) P-O-P(split,4,4)
O (sp , , )
PRINT OPTIMAL PARENS(split split[i
PRINT-OPTIMAL-PARENS(split, split[i,j]+1,
j]+1 j)
print “)”
P-O-P(split,2,2)
(p , , ) P-O-P(split,3,3)
(p , , )

( A1 ( ( A2 A3 ) A4 ) )
75 76
Longest Common Subsequence Longest Common Subsequence
Similarity can be defined in different ways:
„ In biological applications
applications, we often want to
„ Two DNA strands are similar if one is a substring
compare the DNA of two (or more) different of the other.
organisms.
organisms
„ Two strands are similar if the number of changes
„ A strand
t d off DNA consists
i t off a string
t i off needed
d d tto tturn one iinto
t th
the other
th iis small.
ll
molecules called bases, where the possible
bases are adenine
adenine, quanine
quanine, cytosine
cytosine, and „ Th
There is
i a thirds
thi d strand
t d S3 ini which
hi h th
the b
bases iin
thymine (A, C, G, T). S3 appear in each of S1 and S2; these bases
must appear in the same order
order, but not
necessarily consecutively. The longer the strand
S3 we can find, the more similar S1 and S2 are.

Example of Longest Common Example


Subsequence
X = 〈A, B, C, B, D, A, B〉 X = 〈A, B, C, B, D, A, B〉
LCS for S1 and S2:
Y = 〈B, D, C, A, B, A〉 Y = 〈B, D, C, A, B, A〉
S1=ACCGGTCGAGTGCGCGGAAGCCGGCCGAA
ACCGGTCGAGTGCGCGGAAGCCGGCCGAA
S2=GTCGTTCGGAATGCCGTTGCTCTGTAAA „ 〈B, C, B, A〉 and 〈B, D, A, B〉 are longest common
S3=GTCGTCGGAAGCCGCCGAA subsequences of X and Y (length = 4)

„ 〈B, C, A〉, however is not a LCS of X and Y


LCS Brute-force solution A Recursive Solution
„ Enumerate all subsequences of X and check Case 1: xi = yj
each subsequence to see if it is also a
e.g.: X = 〈A, B, D, E〉
subsequence of Y, keeping track of the
longest subsequence found. Y = 〈Z, B, E〉

„ Each subsequence of X corresponds to a c[i, j] = c[i - 1, j - 1] + 1


subset of the indices {1, 2, …, m} of X.
‰ Append xi = yj to the LCS of Xi-1 and Yj-1
„ There are 2m subsequences of X.
‰ Must find a LCS of Xi-1 and Yj-1 ⇒ optimal solution to
„ Exponential Time! a problem includes optimal solutions to sub
problems

A Recursive Solution Computing the Length of the LCS


Case 2: xi ≠ yj
e.g.: X = 〈A, B, D, G〉
Y = 〈〈Z, B, D〉〉

c[i, j] = max { c[i - 1, j], c[i, j-1] }


‰ Must solve two problems
„ d Yj: Xi-1 = 〈A,
fi d a LCS off Xi-1 and
find 〈A B d Yj = 〈Z,
B, D〉 and 〈Z BB, D〉
„ find a LCS of Xi and Yj-1: Xi = 〈A, B, D, G〉 and Yj = 〈Z, B〉
„ Optimal solution to a problem includes optimal solutions to
subproblems
Additional Information LCS Length(X, Y) // dynamic programming solution
LCS-Length(X,
m = X.length()
n = Y.length()
for i = 0 to m do c[i,0] = 0
for j = 0 to n do c[0,j] = 0 O(nm)
for i = 1 to m do // row
for j = 1 to n do // cloumn
if xi = =y
yi then
c[i,j] = c[i-1,j-1] + 1
b[i,j] =“ ”
else if c[i-1, j] ≥ c[i,j-1] then
c[i,j] = c[i-1,j]
b[i j] = “^”
b[i,j] ^
else c[i,j] = c[i,j-1]
b[i,j]
b[ ,j] = “<”

j 0 1 2 3 4 5 6 j 0 1 2 3 4 5 6
i yj B D C A B A i yj B D C A B A

0 xi 0 0 0 0 0 0 0 0 xi 0 0 0 0 0 0 0
A A ^ ^ ^
1 0 1 0 0 0 0 1 <1 1
B B ^
2 0 2 0 1 <1 <1 1 2 <2
C C ^ ^
3 0 3 0 1 1 2
4 B 0 First Optimal-LCS initializes 4 B 0
row 0 and column 0
5 D 0 5 D 0 Next each c[i, j] is computed,
computed row
6 A 0 6 A 0
by row, starting at c[1,1].
If xi == yj then c[i, j] = c[i
c[i-1,
1, jj-1]+1
1] 1
7 B 0 7 B 0 and b[i, j] =
j 0 1 2 3 4 5 6 j 0 1 2 3 4 5 6
i yj B D C A B A i yj B D C A B A

0 xi 0 0 0 0 0 0 0 0 xi 0 0 0 0 0 0 0
A ^ ^ ^ <1 A ^ ^ ^ <1
1 0 0 0 0 1 1 1 0 0 0 0 1 1
B <1 <1 ^ <2 B <1 <1 ^ <2
2 0 1 1 2 2 0 1 1 2
C ^ ^ <2 C ^ ^ <2 ^
3 0 1 1 2 3 0 1 1 2 2
4 B 0 4 B 0
5 D 0 5 D 0
If xi <> yj then c[i, j] = if c[i-1, j] == c[i, j-1]
6 A 0 max(c[i-1, j], c[i, j-1]) 6 A 0 then b[i,j] points up
B and b[i,
b[i j] points to the larger value B
7 0 7 0

j 0 1 2 3 4 5 6 j 0 1 2 3 4 5 6

i yj B D C A B A i yj B D C A B A

xi 0 xi 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
^ ^ ^ A ^ ^ ^
1 A 0 0 0 0 1 <1 1 1 0 0 0 0 1 <1 1
^ B <1 <1 ^ <2
2 B 0 1 <1 <1 1 2 <2 2 0 1 1 2
^ ^ ^ ^ C ^ ^ <2 ^ ^
3 C 0 1 1 2 <2 2 2 3 0 1 1 2 2 2
^ ^ ^ B ^ ^ ^ <3
4 B 0 1 1 2 2 3 <3 4 0 1 1 2 2 3
^ ^ ^ ^ ^ D ^ ^ ^ ^ ^
5 D 0 1 2 2 2 3 3 5 0 1 2 2 2 3 3
^ ^ ^ ^ A ^ ^ ^ ^
6 A 0 1 2 2 3 3 4 6 0 1 2 2 3 3 4
^ ^ ^ ^ B ^ ^ ^ ^
7 B 0 1 2 2 3 4 4 7 0 1 2 2 3 4 4

To construct the LCS,, start in the bottom right-hand


g corner
and follow the arrows. A indicates a matching character. LCS
LCS: B C B A
Constructing an LCS Knapsack problem
Print-LCS(b,X,i,j)
Print LCS(b,X,i,j) „ There are two versions of the problem:
if i = 0 or j = 0 then (1) “0-1 knapsack problem” and
return (2) “Fractional knapsack problem”
if b[i,j] = “ ” then
Print-LCS(b, X, i-1, j-1) (1) Items are indivisible; you either take an item or
print xi not. Solved with dynamic programming
else if b[i,j] = “^” then (2) Items are divisible: you can take any fraction of
item. Solved with a greedy algorithm.
an item
Print-LCS(b, X, i-1, j)
else Print-LCS(b, ( , X,, i,, j-1)
j )
94

00-11 Knapsack problem 0-1 Knapsack - Greedy Strategy


„ E.g.1:
„ Thief has a knapsack with maximum capacity W, and
Item 3 30 $120
a set S consisting of n items
Item 2 50 50 50 +
„ Each item i has some weight wi and benefit value vi 20 $100
30
(all wi , vi and W are integer values) Item 1
20 + 20 $100
10 10 $60
„ Problem: How to pack the knapsack to achieve
maximum total value of packed items? $60 $100 $120 W $160 $220

„ Goal: $6/pound $5/pound $4/pound

„ E.g.2:
‰ find xi such that for all xi = {0, 1}, i = 1, 2, .., n ‰ Item: 1 2 3 4 5 6 7
‰ Benefit: 5 8 3 2 7 9 4
∑ wixi ≤ W and ‰ Weight: 7 8 4 10 4 6 4
‰ Knapsack
p holds a maximum of 22 p
pounds
∑ xivi is maximum ‰ Fill it to get the maximum benefit

95
0-1 Knapsack
p p
problem: brute-force 00-11 Knapsack - Dynamic Programming
approach
„ P(i w) – the maximum profit that can be
P(i,
„ Let’s first solve this problem with a straightforward
algorithm obtained from items 1 to i, if the
„ Since there are n items, there are 2n possible knapsack has size w
combinations
bi ti off items.
it
„ Case 1: thief takes item i
„ We go through all combinations and find the one
with the most total value and with total weight less P(i w)) = vi + P(i - 1,
P(i, 1 w-wi)
q to W
or equal
„ Case 2: thief does not take item i
„ Running time will be O(2n)
P(i w) = P(i - 1,
P(i, 1 w)
97

Recursive Formula 00-11 Knapsack - Dynamic Programming


⎧ P[i − 1, w] if wi > w Item i was taken Item i was not taken
P[ i , w ] = ⎨
⎩ max{ vi + P[i − 1, w − wk ], P[i − 1, w]} else
P(i, w) = max {vi + P(i - 1, w-wi), P(i - 1, w) }
„ The best subset that has the total weight w, either 0: 1 w - wi w W
contains item i or not. 0 0 0 0 0 0 0 0 0 0 0 0
„ First case: wi>w. Item i can’t be part of the 0 first

solution, since if it was, the total weight would be 0 second

> w, which is unacceptable i-1 0


i 0
„ S
Second d case: wi <=w
< . Then
Th the it i can be
th item b in
i
the solution, and we choose the case with greater 0
value.
l n 0
99
W = 5 Item Weight Value
Example: 1 2 12 Reconstructingg the Optimal
p Solution
S
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
0 1 2 3 4 5
4 2 15 • Item 4
0 0 0 0 0 0 0 P(1, 1) = P(0, 1) = 0 0 0 0 0 0 0 0

1 0 1 0 0 12 12 12 12
0 12 12 12 12 P(1, 2) = max{12
max{12+0,
0, 0} = 12 • Item 2
2 0 10 12 22 22 22 2 0 10 12 22 22 22
P(1, 3) = max{12+0, 0} = 12
3 0 10 12 22 30 32 • Item 1
3 0 10 12 22 30 32 P(1, 4) = max{12
max{12+0,
0, 0} = 12
4 0 10 15 25 30 37 4 0 10 15 25 30 37
P(1, 5) = max{12+0, 0} = 12

P(2, 1)= max{10+0, 0} = 10 P(3, 1)= P(2,1) = 10 P(4, 1)= P(3,1) = 10 Startt att P(n,
• St P( W)
P(2, 2)= max{10+0, 12} = 12 P(3, 2)= P(2,2) = 12 P(4, 2)= max{15+0, 12} = 15
• When you go left-up ⇒ item i has been taken
P(2, 3)= max{10+12, 12} = 22 P(3, 3)= max{20+0, 22}=22 P(4, 3)= max{15+10, 22}=25
• When you go straight up ⇒ item i has not been
P(2, 4)= max{10+12, 12} = 22 P(3, 4)= max{20+10,22}=30 P(4, 4)= max{15+12, 30}=30
taken
P(2, 5)= max{10+12, 12} = 22 P(4, 5)= max{20+12,22}=32 P(4, 5)= max{15+22, 32}=37

Optimal Substructure 0-1 Knapsack


p Algorithm
g
for w = 0 to W
„ Consider the most valuable load that weights P[0 ] = 0
P[0,w]
O(n*W)
for i = 0 to n
at most W pounds P[i,0] = 0
for w = 0 to W
„ If we remove item j from this load
if wi <= w // item i can be part of the solution
⇒ The remaining
g load must be the most if vi + P[i-1,w-w
[ , i] > P[i-1,w]
[ , ]
P[i,w] = vi + P[i-1,w- wi]
valuable load weighing at most W – wj that
else
can be taken from the remaining n – 1 items P[i,w] = P[i-1,w]
else P[i,w]
P[i w] = P[i-1,w]
P[i 1 w] // wi > w
104
Overlapping Subproblems Example
P(i w) = max {vi + P(i - 1,
P(i, 1 ww-w
wi),
) P(i - 1,
1 w) } Let s run our algorithm on the
Let’s
0: 1 w W
following data:
0 0 0 0 0 0 0 0 0 0 0 0
0 n = 4 (# of elements)
0 W = 5 (max weight)
i-1 0 Elements (weight, benefit):
i 0 (2 3) (3,4),
(2,3), (3 4) (4
(4,5),
5) (5
(5,6)
6)
0
n 0
E.g.: all the subproblems shown in grey may
depend on P(i-1, w)
106

Conclusion
„ Dynamic
D i programming i isi a useful
f l technique
t h i off
solving certain kind of problems

„ When the solution can be recursively described in


terms of partial solutions, we can store these partial
solutions and re
re-use
use them as necessary

107

You might also like