You are on page 1of 16

Unit V

Dynamic Programming
2
Introduction
Optimization problem: there can be many possible solution. Each solution has a
value, and we wish to find a solution with the optimal (minimum or maximum)
value
Dynamic Programming VS. Divide-and-Conquer
Solve problems by combining the solutions to sub-problems
The sub-problems of D-A-C are non-overlap
The sub-problems of D-P are overlap
Sub-problems share sub-sub-problems
D-A-C solves the common sub-sub-problems repeatedly
D-P solves every sub-sub-problems once and stores its answer in
a table
Programming refers to a tabular method
3
Development of A Dynamic-
Programming Algorithm
Characterize the structure of an optimal
solution
Recursively define the value of an
optimal solution
Compute the value of an optimal
solution in a bottom-up fashion
Construct an optimal solution from
computed information
Matrix-Chain Multiplication
5
Overview
Given a sequence (chain) <A
1
, A
2
,, A
n
> of n matrices to be multiplied, compute the
product A
1
A
2
A
n
in a way that minimizes the number of scalar multiplications
Matrix multiplication
Two matrices A and B can be multiplied only if they are compatible
The number of columns of A must equal the number of rows of B
A(p*q) * B(q*r) C(p*r)
The number of scalar multiplication is p*q*r
Example: <A
1
, A
2
, A
3
> (10*100, 100*5, 5*50)
((A
1
A
2
)A
3
) 10*100*5 + 10*5*50 = 5000 + 2500 = 7500
(A
1
(A
2
A
3
)) 100*5*50 + 10*100*50 = 25000 + 50000 = 75000
6
Matrix-Chain Multiplication
Problem
Given a sequence (chain) <A
1
, A
2
,, A
n
> of n matrices to be
multiplied, where i=1,2,, n, matrix A
i
has dimension
p
i-1
*p
i
, fully parenthesize the product A
1
A
2
A
n
in a way that minimizes
the number of scalar multiplications
Determine an order for multiplying matrices that has the lowest
cost
Counting the number of parenthesizations

>
=
=

=
1
1
2 ) ( ) (
1 1
) (
n
k
n if k n P k P
n if
n P


O(2
n
)
Impractical to check all possible parenthesizations
7
Matrix Multiplication
8
Step 1
The structure of an optimal parenthesization
Notation: A
i..j
= result from evaluating A
i
A
i+1
A
j
(i s j)
Any parenthesization of A
i
A
i+1
A
j
must split the product between A
k
and
A
k+1
for some integer k in the range i s k < j
Cost = cost of computing A
i..k
+ cost of computing A
k+1..j
+ cost of
multiplying A
i..k
and A
k+1..j
together
Suppose that an optimal parenthesization of A
i
A
i+1
A
j
splits the product
between A
k
and A
k+1
.
The parenthesization of the prefix sub-chain A
i
A
i+1
A
k
must be an
optimal parenthesization of A
i
A
i+1
A
k
The parenthesization of the prefix sub-chain A
k+1
A
i+1
A
j
must be an
optimal parenthesization of A
k+1
A
i+1
A
j

9
Illustration of Optimal
SubStructure
A
1
A
2
A
3
A
4
A
5
A
6
A
7
A
8
A
9
Suppose
((A
7
A
8
)A
9
)
is optimal
((A
1
A
2
)(A
3
((A
4
A
5
)A
6
)))
Minimal
Cost_A
1..6
+ Cost_A
7..9
+p
0
p
6
p
9

(A
3
((A
4
A
5
)A
6
)) (A
1
A
2
)
Then must be optimal for A
1
A
2
A
3
A
4
A
5
A
6
Otherwise, if
((A
4
A
5
)A
6
) (A
1
(A
2
A
3
))
is optimal for A
1
A
2
A
3
A
4
A
5
A
6
Then
((A
1
(A
2
A
3
)) ((A
4
A
5
)A
6
)) ((A
7
A
8
)A
9
)
will be better than
((A
7
A
8
)A
9
) ((A
1
A
2
)(A
3
((A
4
A
5
)A
6
)))
10
Step 2
A Recursive Solution
Sub-problem: determine the minimum cost of a parenthesization of
A
i
A
i+1
A
j
(1 s i s j s n)
m[i..j] = the minimum number of scalar multiplications needed to
compute the matrix A
i..j
s[i, j] = k, such that m[i, j] = m[i, k] + m[k+1, j] + p
i-1
p
k
p
j
We need to compute m[1..n]
A recursive solution takes exponential time
Encounter each sub-problem many times in different branches
of its recursion tree overlapping sub-problems

< + + +
=
=

< s


j i if p p p j k m k i m
j i if
j i m
j k i
j k
1
1
] , 1 [ ] , [ {
0
] , [
min
11
Step 3
Computing the optimal costs
How much sub-problems in total?
One for each choice of i and j satisfying 1 s i s j s n O(n
2
)
MATRIX-CHAIN-ORDER(p)
Input: a sequence p= <P
1
, P
2
,, P
n
> (length[p] = n+1)
Try to fill in the table m in a manner that corresponds to
solving the parenthesization problem on matrix chains of
increasing length
Lines 4-12: compute m[i, i+1], m[i, i+2], each time

12
O(n
3
), O (n
3
) O(n
3
) running time
O(n
2
) space
13
l = 2
10*20*25
=5000
35*15*5=
2625
l =3
m[3,5] = min
m[3,4]+m[5,5] + 15*10*20
=750 + 0 + 3000 = 3750
m[3,3]+m[4,5] + 15*5*20
=0 + 1000 + 1500 = 2500
14
Step 4
Constructing an optimal solution
Each entry s[i, j] records the value of k such that the optimal
parenthesization of A
i
A
i+1
A
j
splits the product between A
k
and A
k+1
A
1..n
A
1..s[1..n]
A
s[1..n]+1..n
A
1..s[1..n]
A
1..s[1, s[1..n]]
A
s[1, s[1..n]]+1..s[1..n]
Recursive
Elements of Dynamic
Programming
Optimal substructure
Overlapping subproblems
13 March 2013
Unit V Dynamic Programming and
String Matching 16
Example
1
a b a c a a b a c a b a c a b a a b b
7
8
19 18 17 15
a b a c a b
16 14
13
2 3 4 5 6
9
a b a c a b
a b a c a b
a b a c a b
a b a c a b
10 11 12
c
0
c
3
1
a
4 5 2 1 0 j
2 1 0 0
F(j)
b a b a P[j]

You might also like