5-DynamicProgramming
5-DynamicProgramming
Dynamic programming
2
Dynamic programming
Difference with divide and conquer
algorithm
◼ Dynamic programming is applied when the
subproblems are not independent
Common subproblems
◼ When subproblems are not independent
Applying divide and conquer algorithm
▪ Do the same work (solve the same subproblem)
multiple times
Applying dynamic programming algorithm
▪ Each subproblem is solved once and the result is saved
to an array, then if the subproblem is encountered
again, only the result is reused
▪ Reduce complexity
3
Dynamic programming
Dynamic programming = Divide and
conquer + Memory
5
Some applications
Binomial expansion (a+b)n
Matrix chain multiplication
Longest common subsequence
Backpacking
6
Binomial expansion (a+b)n
The binomial (a+b)n is developed according to the following
formula n
(a + b) n = Cnk a n − k b k
k =0
+ n −1 1 k n − 1
n k
n −1
C C
7
Binomial expansion (a+b)n
Divide and Conquer Algorithm
C(k, n)
begin
if (k = 0 or k = n) then
return (1)
else
return (C(k-1, n-1) + C(k, n-1))
end
8
Binomial expansion (a+b)n
Divide and Conquer Algorithm
C(4,6)
C(3,5) C(4,5)
9
Binomial expansion (a+b)n
Divide and Conquer Algorithm
◼ Calculate running time
Let C(n) be the worst case running time and compute
C(k, n) for all k
So from the algorithm, we have
c if n = 1
C ( n) =
2C (n − 1) + d else
where c and d are constants
10
Binomial expansion (a+b)n
Divide and Conquer Algorithm
◼ Calculate running time
C (n) = 2C (n − 1) + d
= 2(C (n − 2) + d ) + d = 4C (n − 2) + 2d + d
= 4(2C (n − 3) + d ) + 2d + d = 8C (n − 3) + 4d + 2d + d
i −1
= 2 C (n − i ) + d 2 k
i
k =0
n−2
2 n −1 − 1
= 2 C (1) + d 2 = 2
n −1 k n −1
c+d
k =0 2 −1
= 2 n −1 (c + d ) − d
◼ So: C(n) = (2n)
11
Binomial expansion (a+b)n
Dynamic programming algorithm
◼ Use array C[0..n,0..k] to store intermediate results
C[i, j] contains the value Ci j
C[i,j] is calculated
1 j = 0 hay j = i
C[i, j ] =
C[i − 1, j − 1] + C[i − 1, j ]
Calculate the values of Pascal's triangle
n/k 0 1 2 3 4 5
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
12
5 1 5 10 10 5 1
Binomial expansion (a+b)n
Dynamic programming algorithm
C1(k, n)
begin
// Initialize column 0 and diagonal
for i from 0 to nk do C[i,0] = 1 endfor
for i from 0 to k do C[i,i] = 1 endfor
// Calculate each column
for j from 1 to k do
for i from j+1 to n-k+j do
C[i,j] = C[i-1,j-1] + C[i-1,j]
endfor
endfor
return (C[n,k])
end
13
Binomial expansion (a+b)n
Dynamic programming algorithm
◼ Example: n=8, k=5
n/k 0 1 2 3 4 5 n/k 0 1 2 3 4 5
0 1 0 1
1 1 1 1 1 1
2 1 1 2 1 2 1
3 1 1 3 1 3 3 1
4 1 4 4 6 4 1
5 1 5 10 10 5 1
6 6 20 15 6
7 7 35 21
8 8 56
Initialize column 0 and diagonal Calculate by column
14
Just calculate the values needed for calculating C[8,5]
Binomial expansion (a+b)n
Dynamic programming algorithm (4)
◼ Running time
Number of values to calculate (assuming addition is the
basic operation)
k(nk) = nk – k2 ≤ nk
So running time O(nk)
Use an array with nk memory cells to store intermediate
values, or space complexity O(nk)
15
Binomial expansion (a+b)n
Dynamic programming algorithm
C2(k, n)
begin
// use one-dimensional array C[0..n]
C[0] = 1 // initialize row 1
C[1] = 1
Use only one- // calculate each row
dimensional array for i from 2 to n do
to store current p1 = 1
line of Pascal's for j from 1 to i-1 do
triangle p2 = C[j]
C[j] = p1 + p2
p1 = p2
endfor
C[i] = 1 // element on the diagonal
endfor
return (C[k]) 16
end
Binomial expansion (a+b)n
Dynamic programming algorithm
◼ Example: n=8, k=5
n/k 0 1 2 3 4 5 6 7 8
0
1 1 1
2 1 2 1
p1 p2
3 1 3 3 1
4 1 4 6 4 1 Cj
5 1 5 10 10 5 1
6 1 6 15 20 15 6 1
7 1 7 21 35 35 21 7 1
8 1 8 28 56 70 56 28 8 1
17
Binomial expansion (a+b)n
Dynamic programming algorithm
◼ Running time
Number of values to calculate (assuming addition is the
basic operation)
n n −1
n(n − 1)
i =2
(i − 1) = k =
1 2
So time complexity O(n2)
Use an array of n memory cells to store intermediate
values, or space complexity O(n)
18
Dynamic programming algorithm
design
Identification
◼ Build a divide and conquer algorithm/simple algorithm
◼ Complexity assessment (exponential)
◼ The same subproblem is solved multiple times
Build
◼ Extract the ”conquer" part of the divide and conquer
algorithm and replace the recursive calls with searching
for values in an array
◼ Instead of returning the value, save the value to the
array
◼ Use the divide and conquer algorithm's stopping
condition to initialize the array values
◼ Find a way to calculate the values of array
◼ Build a loop to calculate the values of array
19
Dynamic programming algorithm
design
C(k, n) Divide and conquer
begin
if (k = 0 or k = n) then return (1)
else return (C(k-1, n-1) + C(k, n-1))
end
Dynamic
C1(k, n)
programming
begin
for i from 0 to n-k do C[i,0] = 1 endfor
for i from 0 to k do C[i,i] = 1 endfor
for j from 1 to k do
for i from j+1 to n-k+j do
C[i,j] = C[i-1,j-1] + C[i-1,j]
endfor
endfor
return (C[n,k])
end 20
Matrix chain multiplication
Problem
◼ There are n matrices M1, M2, … Mn. we need to calculate
the product M1.M2 .….Mn so that the fewest
multiplications are performed
21
Matrix chain multiplication
Example
◼ Matrix multiplication
M1(10x20).M2(20x50).M3(50x1).M4(1x100)
((M1.(M2.M3)).M4): 2200
(((M1.M2).M3).M4): 60500
22
Matrix chain multiplication
Brute Force Algorithm (1)
◼ Try all possible combinations
◼ Calculate the number of multiplications for each method
◼ Choose the best way
P ( n ) = ( 2 n )
24
Matrix chain multiplication
Divide and conquer algorithm (1)
◼ Let mi,j be the minimum number of multiplications when
performing the product Mi Mi+1…Mj
Obviously, mi,i = 0
◼ Suppose the dimensions of the matrices Mi, Mi+1,…, Mj are (di-1,
di), (di,di+1), …, (dj-1,dj ) respectively
◼ Suppose that we know the combination MiMi+1…Mj with the
minimum cost (smallest mi,j) is (MiMi+1…Mk ).(Mk+1…Mj)
means k already known
then:
ik j
Matrix chain multiplication
Divide and conquer algorithm (2)
◼ Therefore, we build a divide-and-conquer algorithm for
calculating mi,j with all possible values of k
matrixChainRecur(i, j)
m = +
begin
if (i=j) then m = 0
else for k from i to j-1 do
r = matrixChainRecur(i,k) + matrixChainRecur(k+1,j) + di-1dkdj
if (r < m) then
m=r
endif
endfor
endif
return (m)
end
27
Matrix chain multiplication
Divide and conquer algorithm (4)
◼ Example
chainMatrixRecur(1,4)
(2.2) (3,4) (2,3) (4.4) (1,1) (2.2) (3.3) (4.4) (1,1) (2,3) (1,2) (3.3)
28
Matrix chain multiplication
Dynamic programming algorithm (1)
◼ We have to use the array m[1..n,1..n]
◼ For each m[i,j], ij, only the upper half of the main
diagonal of array m is used
◼ Each m[i,j] is calculated by the formula
0 i= j
mi , j =
min (mi ,k + mk +1, j + d i −1d k d j ) i j
ik j
29
Matrix chain multiplication
Dynamic programming algorithm (2)
◼ Illustration of how to calculate mi,j
i j
m[i,j]
0 i= j
mi , j =
i min (mi ,k + mk +1, j + d i −1d k d j ) i j
ik j
m[i,k]
j
30
Matrix chain multiplication
Dynamic programming algorithm (3)
◼ The array m will be built gradually for each diagonal
◼ Diagonal s will consist of elements m[i,j] such that j-i=s
◼ So
0 s=0
mi , j =
min (mi ,k + mk +1,i + s + d i −1d k d i + s ) 0 s n
ik j
Build diagonal s = 1
▪ m[1,2] = min(m[1,k] + m[k+1,2] + d 0 d 1 d 2 ), with 1 k<2
= min(m[1,1] + m[2,2] + d 0 d 1 d 2 )
= d 0 d 1 d 2 = 10x20x50 = 10000
g[1,2] = k = 1
▪ m[2,3] = min(m[2,k] + m[k+1,3] + d 1 d 2 d 3 ), with 2 k<3
= d 1 d 2 d 3 = 20x50x1 = 1000
g[2,3] = k = 2
▪ m[3,4] = d 2 d 3 d 4 = 50x1x100 = 5000
g[3,4] = k = 3
33
Matrix multiplication
Dynamic programming algorithm (6)
◼ Example
Build diagonal s = 2
▪ m[1,3] = min(m[1,k] + m[k+1,3] + d 0 d k d 3 ), with 1 k<3
= min(m[1,1] + m[2,3] + d 0 d 1 d 3 ,
m[1,2] + m[3,3] + d 0 d 2 d 3 )
=min(0 + 1000 + 200, 10000 + 0 + 500)
= 1200
g[1,3] = k = 1
▪ m[2,4] = min(m[2,k] + m[k+1,4] + d 1 d k d 4 ), with 2 k<4
= min( m[2,2] + m[3,4] + d 1 d 2 d 4 ,
m[2,3] + m[4,4] + d 1 d 3 d 4 )
= min(0 + 5000 + 100000, 1000 + 0 + 2000)
= 3000
g[2,4] = k = 3
34
Matrix multiplication
Dynamic programming algorithm (7)
◼ Example
Build diagonal s = 3
▪ m[1,4] = min(m[1,k] + m[k+1,4] + d 0 d k d 4 ), with 1 k<4
= min(m[1,1] + m[2,4] + d 0 d 1 d 4 ,
m[1,2] + m[3,4] + d 0 d 2 d 4 ,
m[1,3] + m[4,4] + d 0 d 3 d 4 )
= min(0 + 3000 + 20000,
10000 + 5000 + 50000,
1200 + 0 + 1000)
= 2200
g[1,4] = k = 3
35
Matrix chain multiplication
Dynamic programming algorithm (8)
◼ The array m and g
0
36
s=0
Matrix chain multiplication
Dynamic programming algorithm (9)
◼ Complexity assessment
The algorithm consists of 3 nested loops
Each loop does not repeat more than n times
So the running time is O(n3)
37
Matrix chain multiplication
Dynamic programming algorithm (10)
◼ Building the optimal solution
perform multiplications in optimal order
matrixChain only computes the optimal value of the
combination of multiplications, it does not perform the
multiplication itself
Use the information contained in the array g
▪ g[i,j] contains the value k, whose product Mi Mi-1 …Mj is split
between Mk and Mk+1
38
Matrix chain multiplication
Dynamic programming algorithm (11)
◼ Building the optimal solution
chainMatrixProduct(M, g, i, j)
// array of matrices
begin
if (i < j) then
X = chainMatrixProduct(M, g, i, g[i,j])
Y = chainMatrixProduct(M, g, g[i,j]+1,j)
return (matrixProduct(X,Y))
else // i = j
return (Mi)
endif
end
chainMatrixProduct(M, g, 1, 4) will calculate the product
M1.M2.M3.M4 in the order: ((M1.(M2.M3)).M4)
Because: g[1,4] = 3, g[1,3] = 1
39
Matrix chain multiplication
Dynamic programming algorithm (12)
◼ Exercise
Write an algorithm to print out the optimal combination
expression to perform matrix multiplication
▪ For example
Given the matrix array:
M1(10x20).M2(20x50).M3(50x1).M4(1x100)
Result: ((M 1 .(M 2 .M 3 )).M 4 )
40
Longest common subsequence
Problem
◼ Given two sequences of symbols X and Y, the longest
common subsequence (LCS) of X and Y is the sequence
of symbols obtained from X by deleting some elements
and also obtained from Y by deleting some elements
41
Longest common subsequence
Brute force algorithm
◼ Suppose X=x1x2…xn and Y = y1y2…ym
◼ Compare each subsequence of X with the
sequence of Y
There are 2n subsequences of X
Comparing each subsequence of X with Y will perform
m comparisons
◼ The complexity will be O(m2n)
Exponential function!
42
Longest common subsequence
Divide and conquer algorithm (1)
◼ The problem can be decomposed into smaller
subproblems
Subproblem: find the longest common subsequence of prefix
pairs of X and Y
▪ Given X=x1 x2 …xn , Xi = X=x1 x2 …xi is called the i-th prefix of X
43
Longest common subsequence
Divide and conquer algorithm (2)
◼ Recursive solution
Let c[i,j] be the length of the longest common
subsequence of Xi and Yj
Then, the length of the longest common subsequence of
X and Y will be c[n,m]
The length of the longest common subsequence of an
empty sequence and any sequence is always 0
▪ c[i,0] = 0 and c[0,j] = 0 for all i, j
So, we have
0 i = 0 hay j = 0
c[i, j ] = c[i − 1, j − 1] + 1 i, j 0, xi = y j
max(c[i − 1, j ], c[i, j − 1]) i, j 0, x y
i j
44
Longest common subsequence
Divide and conquer algorithm (3)
LCS-length(i,j)
begin
if (i = 0 or j = 0) then
return (0)
else
if (xi = yj) then
return (LCS-length(i-1,j-1) + 1)
else
return (max(LCS-length(i-1, j), LCS-length(i, j-1)))
endif
endif
end
45
Longest common subsequence
Divide and conquer algorithm (4)
◼ Complexity assessment
Suppose n m
worst case running time
So: C(k) 2C(m-1) = 22 C(m-2) = 2m C(0) = 2m
That is, the algorithm has complexity ( 2m)
46
Longest common subsequence
Dynamic programming algorithm (1)
◼ Store the length values of the longest common
subsequences of prefix pairs in the array c[0..n,0..m]
LCS-length (X,Y)
begin
n = length(X); m = length(Y)
for i from 0 to n do c[i,0]=0 endfor
for j from 0 to m do c[0,j]=0 endfor
for i from 1 to n do
for j from 1 to m do
if (xi = yj) then
c[i,j] = c[i-1,j-1] + 1
else
c[i,j] = max(c[i-1,j], c[i,j-1])
endif
endfor
endfor
return c 47
end
Longest common subsequence
Dynamic programming algorithm (2)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi
A
1
2 B
3 C
4 B
X=ABCB, Y=BDCAB 48
Longest common subsequence
Dynamic programming algorithm (3)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
A
1 0
2 B 0
3 C 0
4 B 0
for i from 0 to n do c[i,0]=0 49
for j from 0 to m do c[0,j]=0
Longest common subsequence
Dynamic programming algorithm (4)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0
2 B 0
3 C 0
4 B 0
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 50
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (5)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0
2 B 0
3 C 0
4 B 0
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 51
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (6)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1
2 B 0
3 C 0
4 B 0
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 52
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (7)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0
3 C 0
4 B 0
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 53
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (7)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1
3 C 0
4 B 0
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 54
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (8)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1
3 C 0
4 B 0
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 55
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (9)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1 2
3 C 0
4 B 0
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 56
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (10)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1 2
3 C 0 1 1
4 B 0
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 57
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (11)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1 2
3 C 0 1 1 2
4 B 0
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 58
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (12)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 59
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (13)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 60
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (14)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 61
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (15)
◼ Example j 0 1 2 3 4 5
i yj B D C A B
0 xi 0 0 0 0 0 0
1 A 0 0 0 0 1 1
2 B 0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
if (xi = yj) then c[i,j] = c[i-1,j-1] + 1 62
else c[i,j] = max(c[i-1,j], c[i,j-1])
Longest common subsequence
Dynamic programming algorithm (16)
◼ Complexity assessment
Algorithm to calculate array c with nxm elements
Compute each element in O(1) time
So the running time of the algorithm is O(nm)
63
Longest common subsequence
Dynamic programming algorithm (17)
◼ Building the optimal solution
LCS-length algorithm only calculates the length of the
longest common subsequence
Need to determine the longest common subsequence
Similar to the matrix chain multiplication algorithm, use
the array b[1..n,1..m] to store the elements of array c
corresponding to the optimal solution
▪ When c[i,j] = c[i-1,j-1] + 1 then memorize xi , because xi
belongs to the longest common subsequence
Starting from c[m,n] and working backwards
▪ If c[i,j] = c[i-1,j-1] + 1 then xi belongs to the longest
common subsequence
▪ If i = 0 or j = 0 then stop
Reverse the sequence of symbols to get the longest
common subsequence
64
Longest common subsequence
Dynamic programming algorithm (18)
◼ Example j 0 1 2 3 4 5
i Yj B D C A B
0 Xi 0 0 0 0 0 0
A
1 0 0 0 0 1 1
2 B 0 1 1 1 1 2
3 C 0 1 1 2 2 2
4 B 0 1 1 2 2 3
Longest common subsequence (reverse order): BCB
65
Longest common subsequence: BCB
Longest common subsequence
Dynamic programming algorithms (20)
◼ LCS-length algorithm, saving optimal solution into array b
LCS-length (X,Y)
begin
n = length(X), m = length(Y)
for i from 0 to n do c[i,0]=0 endfor
for j from 0 to m do c[0,j]=0 endfor
for i from 1 to n do
for j from 1 to m do
if (xi = yj) 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,j-1]
b[i,j] = ' ‘
else c[i,j] = c[i-1,j]
b[i,j] = ' '
endif endif endfor endfor
return c 66
end
Longest common subsequence
Dynamic programming algorithm (19)
◼ Building the optimal solution
print-LCS(b, X, i, j)
begin
if (i = 0 or j = 0) then
return
else
if (b[i,j] = '\') then //xi belongs to the longest common subsequence
print-LCS(b,X,i-1,j-1)
print xi
else
if (b[i,j]=‘’) then
print-LCS(b,X,i,j-1)
else //(b[i,j]=‘’)
print-LCS(b,X,i-1,j)
endif
endif
endif 67
end
Backpacking
Problem
◼ There is a backpack that can hold at most a weight W. There
are n objects, each object has a weight wi and a value bi
W, wi and bi are integers
◼ Put the items in the backpack so that the total value of the
backpack is the largest
◼ Example
Objects Weight - wi Value - bi
2 8
5 10
3 6
W = 12
Backpack 9 2
68
4 3
Backpacking
Brute force algorithm
◼ There are n objects, so there are 2n subsets of
objects of the set of n objects
◼ Iterate through all 2n subsets of objects and
select the set with the largest total value
whose total weight is less than W
69
Backpacking
Divide and conquer algorithm (1)
◼ The problem can be divided into subproblems
Instead of considering k objects, consider k-1 objects…
◼ Let vk,w be the maximum total value of the backpack
whose weight does not exceed w when using only 1…k
objects
◼ For each object k, we need to answer the question:
Can the current backpack contain object k?
◼ Answer
If the current remaining weight w of the backpack is less
than wk then the backpack cannot contain object k
On the contrary, w wk then two cases occur:
▪ Don't add object k into the backpack, use only 1…k-1 objects
▪ Add object k to the backpack, then the value of the backpack
increases by bk but the remaining weight of the backpack
decreases by wk (i.e. equal to w-wk)
▪ The case giving the largest total value of the backpack will be
selected
70
Backpacking
Divide and conquer algorithm (2)
◼ So, vk,w is calculated as follows
vk −1, w if wk w
vk , w =
max{vk −1, w , vk −1, w− wk + bk } else
◼ Explanation
If wk > w, it is not possible to add object k to the
backpack, only consider objects 1…k-1 (including k-1
objects)
If wk w,
▪ If using only objects 1…k-1 gives the largest total backpack
value, then don't use object k
▪ Use object k if the total value of the backpack is the largest,
then consider objects 1…k-1 with the remaining weight of
the backpack being w-wk
71
Backpacking
Divide and conquer algorithm (3)
◼ Note that, vk,w = 0 if k = 0
◼ So the full formula to calculate vk,w is
0 k =0
vk , w = vk −1, w wk w
max(v
k −1, w , vk −1, w− wk + bk ) wk w
72
Backpacking
Divide and conquer algorithm (4)
backpack (k, w)
begin
if (k = 0) then return 0
else
if (wk > w) then
return backpack(k-1,w) // don't use object k
else
x = backpack(k-1,w)
y = backpack(k-1,w-wk) + bk
return (max(x, y))
endif
endif
end
Use: backpack(n, W) 73
Backpacking
Divide and Conquer Algorithm (5)
◼ Complexity assessment
Let C(n) be the worst case complexity
From the algorithm, we have the recurrence relation
c n=0
C ( n) =
max(C (n − 1),2C (n − 1) ) + d n0
c n=0
=
2C (n − 1) + d n0
where c and d are constants
74
Backpacking
Dynamic programming algorithm (1)
◼ Use the array v[0..n,0..W] to store the solutions of the
subproblems
◼ v[k,w] is the maximum total value of the backpack
whose weight does not exceed w when using only
objects 1…k
◼ Initially
v[0,w] = 0 for all w
v[k,0] = 0 for all k
◼ Then v[k,w] will be calculated according to v[k-1,w] or
v[k-1,w-wk]
75
Backpacking
Dynamic programming algorithm (2)
backpack (n, W)
begin
for k from 0 to n do v[k,0] = 0 endfor
for w from 0 to W do v[0,w] = 0 endfor
for k from 1 to n do
for w from 1 to W do
if (wk w) then //can use object k
v[k,w] = max(v[k-1,w], v[k-1,w-wk] + bk)
else //wk > w
v[k,w] = v[k-1,w] // do not use object k
endif
endfor
endfor
end
76
Backpacking
Dynamic programming algorithm (3)
backpack (n, W)
begin
for k from 0 to n do v[k,0] = 0 endfor
for w from 0 to W do v[0,w] = 0 endfor
for k from 1 to n do
for w from 1 to W do
if (wk w) then //can use object k
if (bk + v[k-1,w-wk ] > v[k-1,w]) then
v[k,w] = bk + v[k-1,w-wk ] // use object k
else
v[k,w] = v[k-1,w] // do not use object k
endif
else //w k > w
v[k,w] = v[k-1,w] // do not use object k
endif
endfor
endfor
77
end
Backpacking
Dynamic programming algorithm (4)
◼ Complexity assessment
Algorithm to calculate nxW element array using two
nested loops
Running time O(nW)
◼ Example (1)
W = 5 (maximum weight the backpack can hold)
n = 4 (4 objects)
The objects have (weight, value) in order:
(2,3), (3,4), (4,5), (5,6)
78
Backpacking
Example (2)
k 0 1 2 3 4
W
0 0
1 0
2 0
3 0
4 0
5 0
for w = 0 to W do v[0,w] = 0
79
Backpacking
Example (3)
k 0 1 2 3 4
W
0 0 0 0 0 0
1 0
2 0
3 0
4 0
5 0
for k = 0 to n do v[k,0] = 0
80
Backpacking
Example (4)
k 0 1 2 3 4 Objects
W
0 0 0 0 0 0
1: (2,3)
k=1 2: (3,4)
1 0 0
bk= 3 3: (4,5)
2 0
wk =2 4: (5,6)
3 0
4 0 w= 1
5 0 w-wk=-1
if wk <= w then // the object cannot be used
if bk + v[k-1,w-wk ] > v[k-1,w] then
v[k,w] = bk + v[k-1,w-wk ]
else
v[k,w] = v[k-1,w] 81
Problem 2
◼ Catalan numbers defined
1 n =1
n −1
T ( n) =
T (i ).T (n − i ) n 1
i =1
97
Exercises
Problem 3
◼ Coin change: Given a set of coin denominations and a
target amount, find the minimum number of coins needed
to make up the target amount. If it's not possible to form
the target amount, return −1 (no solution).
◼ Hint:
Use an array dp[], where dp[i] represents the minimum
number of coins needed to form amount i.
For each coin c, update dp[i] as: dp[i]=min(dp[i],dp[i−c]+1).
98
Explanation
1 n =1
n −1
P ( n) =
P(k ) P(n − k ) n 2
k =1
2 n − 2 (do n 5)
◼ So P(n) = (2n )
99