Matrix-Chain Multiplication
Restatement: Given matrices
M1,M2,...,Mn where Mi has ri
rows and ri+1 columns, find
the way to compute the
product of the matrices that
uses the smallest number of
scalar multiplies
A recursive solution:
best (i,j):
// Least cost for M[i]...M[j] //
if (i = j) return 0;
min = infinity;
for k = i to j-1 do
   cost = best(i,k) + best(k+1,j)
   + r[i]*r[k+1]*r[j+1];
   if (cost < min) min = cost;
return min;
end best.
CS409 - Spring 2000
5