Sie sind auf Seite 1von 2

Formal denition of the problem

Given a chain of n matrices A1 , A2 , ..., An where for i = 1, 2, ..., n, matrix Ai has dimensions pi1 pi , fully parenthesize the product A1 A2 . . . An to minimizes the number of scalar multiplications. A product of matrices is fully parenthesized if it is either a single matrix or the product of two fully parenthesized matrix products which are surrounded by parentheses.

Dynamic programming approach


1. Characterize the structure of an optimal solution
First, we dene some notations which will be using later: P (i, j ) - A fully parenthesization of the product Ai ...Aj where 1 i j n. OP (i, j ) - The optimal parenthesization of the product Ai ...Aj , where 1 i j n, with the minimal number of scalar multiplications. N (i, j ) - The number of scalar multiplications of the optimal parethesization of the product Ai ...Aj . When i = j , OP (i, j ) = Ai ; when i < j , we can nd i k < j , such that OP (i, j ) = (P (i, k ))(P (k + 1, j )). It must be true that P (i, k ) and P (k +1, j ) are OP (i, k ) and OP (k +1, j ), or else we can just cut out them and paste in OP (i, k ) and OP (k + 1, j ), such that there exists a better parenthesization OP (i, j ) = (OP (i, k ))(OP (k + 1, j )), contradicting with the optimality of OP (i, j ). So this problem exhibits the optimal substructure and can be solved using dynamic programming.

2. Recursively dene the value of an optimal solution


When i j , the problem is trivial, and N (i, j ) = 0. When i < j , the problem is to nd the k . Assuming that all the sub-problems related have already been solve, i.e., we have already know the value of N (i, k ) and N (k + 1, j for each i k < j , it must be true that N (i, j ) = min (N (i, k ) + N (k + 1, j ) + pi pk pj )
ik<j

3. Compute the value of an optimal solution and record computed information


Based on the recurrence above, we can devise an algorithm as follow, where p is an array for the dimensions (p[i] = pi ). Algorithm 1: MIN-SCALAR-MULTIPLICATION(p)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

n = p.length; let n[1..n, 1..n] be a new table; let s[1..n-1, 2..n] be a new table; for i = 1 to n do n[i, i] = 0; end for d = 1 to n-1 do for i = 1 to n-1 do j = i + d; n[i, j] = ; for k = i to j-1 do if n[i, j ] > n[i, k ] + n[k + 1, j ] + p[i 1]p[k ]p[j ] then n[i, j] = n[i, k] + n[k+1, j] + p[i-1]p[k]p[j]; s[i, j] = k; end end end end return n and s;

The space complexity of this procedure is clearly O(n2 ), for m and s require O(n2 ) space. The time complexity of this procedure is O(n3 ), for there are three-level nested loops, and each of them iterates at most n times.

4. Construct an optimal solution from computed information


We can use the table s returned by the above procedure to build up an algorithm which actually does the multiplication, as showed below. Algorithm 2: MCM(s, i, j)
1 2 3 4 5 6 7 8

if i == j then return A[i]; else k = s[i, j]; X = MCM(s, i, k); Y = MCM(s, k+1, j); return XY end

Das könnte Ihnen auch gefallen