You are on page 1of 15

Dynamic Programming Exercises

L2 version

1
Maximum Sum problem
Let A be a sequence of n positive numbers a1, a2, . . , an.
Find the subset S of A that has the maximum sum, provided that if we
select ai in S, then we cannot select ai-1 or ai+1.

For example, for A= 1, 8, 6, 3, 7, we have:


S = {8,7}

2
Dynamic Programming solution
General idea:
Let Ai be the subsequence of A containing the first i numbers
(i n): a1, a2, . . . , ai
Let Si be the solution of problem Ai.
Let Wi be the sum of numbers in Si.

For number ai, there are two cases:


ai is in Si. Then, ai-1 is not in Si, and Wi = Wi-2+ ai.
ai is not in Si. Then ai-1 could be in Si and Wi = Wi-1.
We take the greater of the two cases: Wi = max{Wi-2+ ai, Wi-1}.
We solve the problem incrementally starting from small size:
i.e., A1, A2, ..., An. The final solution is An.
DP pseudocode
W[1] = a1 ; b[1] = true // in general b[i] = true means that ai is in Si
If a2 > a1
W[2] = a2 ; b[2] = true // a2 is in S2
else
W[2] = a1 ; b[2] = false // a2 is not in S2
for i =3 to n
If W[i2] + ai > W[i1]
W[i] = W[i2] + ai
b[i] = true // ai is in Si
else
W[i] = W[i1]
b[i] = false // ai is not in Si

Cost: (n)
Example: for A= 1, 8, 6, 3, 7, we have
W[1] = 1, b[1] = 1
W[2] = 8, b[2] = 1
W[3] = 8, b[3] = 0
W[4] = 11, b[4] = 1
W[5] = 15, b[5] = 1
Printing the solution
i=n
while i > 0
if b[i] is true
Output ai
i = i2
else
i = i1

Example: for A= 1, 8, 6, 3, 7, we have


b[5] = 1; therefore, we print a5=7 and set i=3
b[3] = 0; therefore, we set i=2
b[2] = 1; therefore, we print a2=8 and set i=0
Minimum number of Coins
Input: Amount n and k denominations d1, ,dk
Output: Minimum number of coins for amount n

Greedy solution: Give as many coins as possible from


the largest denomination, then from the from second
largest, and so on.

Greedy solution may not be optimal for arbitrary coin


denominations
n = 30c, d1 = 25c, d2 = 10c, d3 = 1c
Greedy solution: 6 (1x25+5X1)
Optimal solution: 3 (3x10)
DP approach
We will find a formula that expresses the solution
for amount n, as a recurrence of solutions for smaller
amounts
This is the most crucial step
Then, we will start solving the small problems,
gradually increasing their size, until we reach n.
We will keep all solutions in a table
The last solution in the table corresponds to the
minimum number of coins for amount n
If want to remember which denominations were used,
we need an additional table
Recurrence
Let C[n] be the minimum number of coins
Let di be the denomination of a coin that I
use
Then: C[n] = 1 + C[n-di]
Because after using 1 coin, the amount
left is n-di
But I have to consider all possible (k)
denominations
Similar to Rod-Cutting, where I have to
consider all possible cuts for the first part
Therefore: C[n] = 1 + min{C[n-di]}, for d1, ,dk
Algorithm
DP-Coin(n)
C[< 0] = ; C[0] = 0 // initialization
For p = 1 to n // for each amount (problem size)
min =
For i = 1 to k // for each denomination
if (p di) // If amount is at least as large as the coin
if (1+C[p di]) < min)
min = C[p di] + 1
coin = di
C[p] = min // min number of coins for amount p
S[p] = coin // last coin used for amount p
Cost: (nk)
Printing the solution
While n > 0
print S[n] // last coin used
n = n S[n] // remaining amount
Matrix-chain Multiplication
The product of two matrices Apq and Bqr (with dimensions pq
and qr) is a matrix Cpr. Generating Cpr requires pqr scalar
multiplications.
For three matrices (e.g., A10100, B1005, and C550) there are 2 ways to
parenthesize
((AB)C) = D105 C550
AB 101005=5,000 scalar multiplications
DC 10550 =2,500 scalar multiplications
(A(BC)) = A10100 E10050
BC 100550=25,000 scalar multiplications
AE 1010050 =50,000 scalar multiplications

General problem: We have a sequence or chain A1, A2, , An of n


matrices and we want to determine the optimal way to
parenthesize (i.e., the solution with the minimum number of
scalar multiplications).
11-11
Structure of an optimal solution
Ai..j : matrix that results from the product Ai Ai+1 Aj
An optimal parenthesization of A1A2An splits the product
between Ak and Ak+1 for some integer k where 1 k < n
First compute matrices A1..k and Ak+1..n ; then, multiply them to
get the final matrix A1..n
Observation: parenthesizations of the subchains A1A2Ak and
Ak+1Ak+2An must also be optimal if the parenthesization of
the chain A1A2An is optimal (why?)
The optimal solution to the problem contains within it the
optimal solution to subproblems

11-12
Recursive definition for optimal solution
Let m[i, j] be the minimum number of scalar multiplications
necessary to compute Ai..j
Suppose the optimal parenthesization of Ai..j splits the product
between Ak and Ak+1 for some integer k where i k < j
Ai..j = (Ai Ai+1Ak)(Ak+1Ak+2Aj)= Ai..k Ak+1..j

Cost of computing Ai..j = cost of computing Ai..k + cost of computing


Ak+1..j + cost of multiplying Ai..k and Ak+1..j
Cost of multiplying Ai..k and Ak+1..j is pi-1pk pj
But optimal parenthesization occurs at one value of k among all
possible i k < j. Check all these and select the best one
0 if i=j
m[i, j ] =
min {m[i, k] + m[k+1, j ] + pi-1pk pj } if i<j
i k< j
11-13
DP Algorithm
Input: Array p[0n] containing matrix dimensions and n
Result: Minimum-cost table m and split table s
MATRIX-CHAIN-ORDER(p[ ], n)
for i 1 to n
m[i, i] 0
for l 2 to n
for i 1 to n-l+1
j i+l-1
m[i, j]
for k i to j-1
q m[i, k] + m[k+1, j] + p[i-1] p[k] p[j]
if q < m[i, j]
m[i, j] q
s[i, j] k
return m and s

Time: O(n3), Space: O(n2)

11-14
Example
The initial set of dimensions are <5, 4, 6, 2, 7.
We are multiplying A1 (5x4) x A2 (4x6) x A3 (6x2) x A4 (2x7).
Optimal sequence is (A1 (A2A3 )) A4.

You might also like