Professional Documents
Culture Documents
L12 PDF
L12 PDF
#
#
$#
matrix
..
..
%"
&
..
!
which has
rows and
-
"
$#
..
,
3
/
43
(
(
')(
' (
matrix:
,
1
.2/
1
,
columns.
"
"
is a two-
for
of a matrix
matrix given by
and
and a
.
Example: If
' (
/
1
!
,
' (
.
*
,
then
43
43
.
+
0
+
3
43
')(
0
3
*
5
If
is defined,
Given a matrix
way of multiplying
for
and
and a matrix , the direct
is to compute each
.
,
,
,
+
and
.
, then
.
5
A big difference!
Implication: The multiplication sequence
(parenthesization) is important!!
555
corresponding to matrix sequence
,
,
,
555
,
where
has dimension
determine the multiplication sequence that minimizes
the number of scalar multiplications in computing
Exhaustive search:
+
.
Yes DP
that minimizes
sequence for
the number of multiplications.
Clearly,
is a
matrix.
tion for
.
High-Level Parenthesization for
For any optimal multiplication sequence, at the last
and
step you are multiplying two matrices
for some . That is,
5
Example
Here
5
,
10
If parenthisization of
was not optimal we could
replace it by a better parenthesization and get a cheaper
final solution, leading to a contradiction.
Similarly, if parenthisization of
was not optimal we could replace it by a better parenthesization
and get a cheaper final solution, also leading to a contradiction.
11
"
For
, let
denote the minimum
number of multiplications needed to compute
.
The optimum cost can be described by the following
recursive definition.
12
for some , where the sequences of multiplications for
and
also are optimal. Hence
5
13
Therefore
14
to calculate
corresponding
. Hence, the algorithm
matrix-chain are both less than
should fill the table in increasing order of the length of the matrixchain.
That is, we calculate in the order
! ! "#
$%&
'
(
)**+,
- &
)/.0*1
..
.
"
16
+ , 1 ,
, ,
and
, with
0
. Find
.
+
S0: Initialization
m[i,j]
3
j
0
4
A1
p0
A2
p1
2
A3
p2
7
A4
p3
,
and
p4
17
Example Continued
Stp 1: Computing
By definition
&
"
$
3
5
m[i,j]
3
j
120
1
0
4
A1
p0
A2
p1
2
A3
p2
7
A4
p3
p4
18
Example Continued
Stp 2: Computing
#
$
By definition
$
#
.
+
5
m[i,j]
3
j
120
1
48
0
4
A1
p0
A2
p1
2
A3
p2
7
A4
p3
p4
19
Example Continued
#
Stp3: Computing
#
By definition
+
+
.
+
5
m[i,j]
3
j
120
1
48
84
0
4
A1
p0
A2
p1
2
A3
p2
7
A4
p3
p4
20
Example Continued
Stp4: Computing
"
By definition
&
#
$#
5
m[i,j]
3
j
88
2
120
1
48
84
0
4
A1
p0
A2
p1
2
A3
p2
7
A4
p3
p4
21
Example Continued
#
Stp5: Computing
#
$
$#&
By definition
+
$#
43
+
#
+
5
m[i,j]
3
j
88
2
104
120
1
48
84
0
4
A1
p0
A2
p1
2
A3
p2
7
A4
p3
p4
22
Example Continued
St6: Computing
By definition
+
.
,
+
#
+
#
5
m[i,j]
158
3
j
2
88
2
104
120
1
48
84
0
4
A1
p0
A2
p1
2
A3
p2
7
A4
p3
p4
We are done!
23
%
"
...
,
5
25
Matrix-Chain(
)
for ( to )
for (
to )
for (
to
;
;
for (
to
if (
return
;
;
and ; (Optimum in
values.
. Space complexity
26
"
if (
is now
;
, where is
;
is now
; multiply matrices
return
else return
To compute
and
;
, call Mult(
%
).
27
. Assume
Consider the example
earlier,
where
1
1
that the array
has been computed. The
5 5
5 5
multiplication sequence is recovered as follows.
Mult
Mult
Mult
Mult
Mult
Hence the product is computed as follows
5
28