Professional Documents
Culture Documents
B @ @ @ A9
4"
3 3
& 56 " $2
4" 4"
& 56 " $2
! " 47
) ) 10) ) ) 10)
which has
. .
rows and
. .
matrix
columns.
. .
matrix:
. .
is a two-
2
3 S B " Q R" & @ @ @ 9 I D C I H G D G F I G PF I F H & E" 3 Q I D D F !
D B @ 9 I F "
D B I 3 I G " @ 9 H
D H F "
"
"
and a
for
and
then
Example: If
3
S B " Q H @ A9 Q R" F Q R" & Q R" Q C H C
00) ) )
If
) 0) )
is dened,
Note that has entries and each entry takes time to compute so the total procedure takes time.
for
and
"
"
Given a matrix , a matrix and a matrix , then can be computed in two ways and : The number of multiplications needed are:
"
When
and
, then
&
The Chain Matrix Multiplication Problem Given dimensions corresponding to matrix sequence , , , where has dimension , determine the multiplication sequence that minimizes the number of scalar multiplications in computing . That is, determine how to parenthisize the multiplications.
Exhaustive search:
. Yes DP
S S 01S
S S 01S
10) ) )
Developing a Dynamic Programming Algorithm Step 1: Determine the structure of an optimal solution (in this case, a parenthesization). Decompose the problem into subproblems: For each pair , determine the multiplication sequence for that minimizes the number of multiplications.
Clearly,
is a
matrix.
10) ) )
2
"
Developing a Dynamic Programming Algorithm Step 1: Determine the structure of an optimal solution (in this case, a parenthesization). High-Level Parenthesization for For any optimal multiplication sequence, at the last step you are multiplying two matrices and for some . That is,
S
Example
S
Here
01) ) )
01) ) )
Developing a Dynamic Programming Algorithm Step 1 Continued: Thus the problem of determining the optimal sequence of multiplications is broken down into 2 questions:
How do we decide where to split the chain (what is )? (Search all possible values of )
How do we parenthesize the subchains and ? (Problem has optimal substructure property that and must be optimal so we can apply the same procedure recursively)
10
Developing a Dynamic Programming Algorithm Step 1 Continued: Optimal Substructure Property: If nal optimal soinvolves splitting into and lution of at nal step then parenthesization of and in nal optimal solution must also be optimal for the subproblems standing alone: If parenthisization of was not optimal we could replace it by a better parenthesization and get a cheaper nal solution, leading to a contradiction. Similarly, if parenthisization of was not optimal we could replace it by a better parenthesization and get a cheaper nal solution, also leading to a contradiction.
11
Developing a Dynamic Programming Algorithm Step 2: Recursively dene the value of an optimal solution. As with the 0-1 knapsack problem, we will store the solutions to the subproblems in an array. For , let denote the minimum number of multiplications needed to compute . The optimum cost can be described by the following recursive denition.
4
12
"
Developing a Dynamic Programming Algorithm Step 2: Recursively dene the value of an optimal solution.
) 0
13
for some , where the sequences of multiplications for and also are optimal. Hence
S
Proof: Any optimal sequence of multiplication for is equivalent to some choice of splitting
' (
#
% $ &
("
" !
Developing a Dynamic Programming Algorithm Step 2 Continued: We know that, for some
("
We dont know what is, though But, there are only possible values of so we can check them all and nd the one which returns a smallest cost.
3
14
) 0
Therefore
' (
# # % $ & #
!
we must have already evaluated and For both cases, the corresponding length of the . Hence, the algorithm matrix-chain are both less than should ll the table in increasing order of the length of the matrixchain.
' #
#
% $ & #
!
to calculate
. . .
15
Dynamic Programming Design Warning!! When designing a dynamic programming algorithm there are two parts: 1. Finding an appropriate optimal substructure property and corresponding recurrence relation on table items. Example:
16
4
2. Filling in the table properly. This requires nding an ordering of the table elements so that when a table item is calculated using the recurrence relation, all the table values needed by the recurrence relation have already been calculated.
In our example this means that by the time is calculated all of the values and were already calculated.
("
("
Example for the Bottom-Up Computation Example: Given a chain of four matrices and , with , , , . Find . S0: Initialization
, and
A1 p0 p1
A2 p2
A3 p3
A4 p4
0 7
m[i,j] 2 3 4 i
17
&
" (2
S Q & "
& $'
& 6
& (8$"
" (2
& " ('$#
Stp 1: Computing
Example Continued
120
p0
A1
p1
A2
p2
By denition
A3
p3
m[i,j]
A4
p4
18
S C F
$
( $"
& 6
( 5 &
Stp 2: Computing
Example Continued
& $'
& 6
120
48
p0
A1
p1
A2
p2
By denition
A3
p3
m[i,j]
A4
p4
19
( 5 &
S C
C
$"
C
C 5
Stp3: Computing
Example Continued
$
120
48
By denition
84
p0
A1
p1
A2
p2
A3
p3
m[i,j]
A4
p4
20
C
5
$ 56 &
$5
( $"
" (2
( $# "
Stp4: Computing
Example Continued
S F F
88
120
48
By denition
84
p0 A1
p1 A2
p2 6 A3 0
p3
m[i,j]
A4
p4 7
21
( $# "
C C
C 5
C
$"
Stp5: Computing
Example Continued
S C
Q R"
88
By denition
104
120
48
84
p0 A1
p1 A2
p2 6 A3 0
p3
m[i,j]
A4
p4 7
22
C & 5
C
C C C
C 5 & 5
$"
" (2
C
" $#
St6: Computing
Example Continued
158
S D F
"
)
88
By denition
104
120
48
84
We are done!
p0
A1
p1
A2
p2
A3
p3
m[i,j]
A4
p4
23
C " $#
Step 4: Construct an optimal solution from computed information extract the actual sequence.
Idea: Maintain an array , where denotes for the optimal splitting in computing . The array can be used recursively to recover the multiplication sequence.
' % % !% % ! ' % % % ' % % ! ' % %
! !
S S
S S
"
" 2
4
S S
S S
" 7
" #
. . .
. . .
24
Developing a Dynamic Programming Algorithm Step 4: Construct an optimal solution from computed information extract the actual sequence. Example of Finding the Multiplication Sequence: Consider . Assume that the array has been computed. The multiplication sequence is recovered as follows.
I S S " 7 I S S " 2
"
( $# "
I $# "
I
25
for (
to
26
. Space complexity
'
'
values. .
return
and ; (Optimum in
if (
) ; ;
for (
to
) ;
)
The actual multiplication code uses the value to determine how to split the current sequence. Assume that the matrices are stored in an array of matrices , and that is global to this recursive procedure. The procedure returns a matrix.
Mult(
if (
) ; , where ;
("
and
else return
To compute
, call Mult(
).
27
" $7
10) ) )
is now
is
00) ) )
10) ) )
S S
" 2
' '
! ' ' ! ' ! ! ! ' ' ' ! ' ! ! ' % ! ! ' ' ! ' ! ! ' ' ' ! ' ! ! ' % ! ! ' ' ! ' ! ! ' ' ! ' % ! ! !' % ! ! ' ! ' ' ! ' % !
'
. Assume Consider the example earlier, where that the array has been computed. The multiplication sequence is recovered as follows.
" #
'
! ! ! ! !
S S
S S
" %
28
S
" "