Why?
A similar observation holds for the parenthesization
of the sub chain A
k+1
A
k+2
…A
n
Thus, an optimal solution to an instance of the
matrix-chain multiplication problem contains within
it optimal solutions to sub problem instances.

Matrix Chain Multiplication Problem
A Recursive Solution
Define the value of an optimal solution
recursively in terms of the optimal
solutions to subproblems.
For this problem, we pick as our
subproblems
the problems of determining the
minimum cost of a parenthesization of
A
i
A
i+1
….A
j
,
for 1 <= i <= j <=n

A Recursive Solution
Let
m[i, j]
be the minimum number of
scalar multiplications needed to compute
the matrix
A
i..j
;
The cost of cheapest way to compute
A
1..n
would thus be
m[1, n]

A Recursive Solution
We can define m[i, j] recursively as follows:
If i = j, the chain consists of just one
matrix A
i..i
=A
i
, so
No scalar multiplications are necessary to
compute the product. Thus
m[i, i] = 0 for i = 1, 2, …..,n.
To compute m[i, j] when i < j, we take
advantage of the structure of an optimal
solution from step 1.
Assume that optimal parenthesization
splits the product A
i
, A
i+1
,….A
j
between A
k
and A
k+1
, where i<=k < j.

A Recursive Solution
Now m[i, j] is equal to the minimum cost for
Computing the subproducts A
1..k
, and
Computing the subproducts A
k+1..j
, plus
The cost of multiplying these two matrices
together.
For i = 1, 2, ….., n, matrix A
i
has dimensions
P
i-1
*P
i
Since computing the matrix product A
1..k
A
k+1..j
takes p
i-1
p
k
p
j
scalar multiplications, we obtain
m[i, j] = m[i, k] + m[k+1, j] + p
i-1
p
k
p
j
But we don’t know the value of k for an
optimum solution.
There are only j-1 possible values for k,
namely k=i,i+1, …j-1.

A Recursive Solution
Since, the optimal parenthesization must
use one of these values for k, we need only
check them all to find the best.
Thus, our recursive definition for the
minimum cost of parenthesizing the product
A
i
, A
i+1
, … A
j
becomes
j
i
}
]
,
1
[
]
,
[
{
min
j
i
0
]
,
[
1
if
p
p
p
j
k
m
k
i
m
if
j
i
m
j
k
i
j
k
i

Matrix Chain Multiplication Problem
Based on this recurrence, we can easily write the
recursive algorithm to compute the minimum cost m[1..n]
for multiplying A
1
, A
2
, ….,A
n
.
Recursive-Matrix-Chain(p, i, j)
{
1.
if i = j
2.
return 0
3.
MinCost =
4.
for(k = i; k <= j-1; k++)
{
q = Recursive-Matrix-Chain(p, i, k) + Recursive-
Matrix-
Chain(p, k+1, j)
+
p
i-1
p
k
p
j
5.
if(q < MinCost)
6.

#### You've reached the end of your free preview.

Want to read all 34 pages?

- Fall '19