Matrix-chain multiplication
问题描述
Input:
- {p0, p1 ,... , pn}. a chain {A1, A2,..., An} of n matrices for i = 1, 2,...,n , matrix Ai has dimension pi-1 * pi.
值得注意的该问题的输入定义及其巧妙,不仅定义了矩阵的行以及列,还说明了它们是可乘的。
Output:
- fully parenthesize the product A1, A2,..., An in a way that minimizes the number of scalar multiplications.
定义1, A product of matrices is fully parenthesized, if it is either a single matrix or the product of two fully parenthesized matrix products, surrounded by parentheses.
当一个矩阵链乘问题是 fully parenthesized 时,就可以使用两个矩阵相乘的简单算法进行处理整个矩阵链。对于p * q的矩阵乘以q * r的矩阵,其所进行的 scalar multiplications 的次数为p * q * r(参见下方伪代码中第8行的执行次数)。而问题中的 the number of scalar multiplications 为计算以该算法计算整个矩阵链乘时所进行的 scalar multiplications 次数。
两个矩阵相乘的简单算法的算法伪代码如下:
MATRIX-MULTIPLY(A,B)
1. if A.columns != B.rows
2. error “incompatible dimensions”
3. else let C be a new A.rows * B.columns matrix
4. for i = 1 to A.rows
5. for j = 1 to B.columns
6. c[i,j] = 0
7. for k = 1 to A.columns
8. c[i,j] = c[i,j] + A[i,k] * B[k,j]
9. return C
暴力算法分析
Denote the number of alternative parenthesizations of a sequence of n matrices by P(n).
- When n = 1, P(n) = 1。
- When n > 1, a fully parenthesized matrix product is the product of two fully parenthesized matrix subproducts, and the split between the two subproducts may occur between the kth and (k + 1)st matrices for any k = 1,2,3,...,n-1.
so
The number of solutions is thus exponential in n, and the brute-force method of exhaustive search makes for a poor strategy when determining how to optimally parenthesize a matrix chain.
Applying dynamic programming
Step 1: The structure of an optimal parenthesization
定理1,若Ai~j 的最优解中最后一次乘法在k处,即在Ak与Ak+1之间,那么,Ai~j的最优解的乘法序列的可以在在k处断开矩阵乘法链。则,Ai~j的最优解乘法序列对应子问题Ai~k的解必须是子问题计算Ai *...* Ak的优化解,Ai~j的最优解乘法序列对应子问题Ak+1~j的解必须是子问题计算Ak+1 *...* Aj的优化解。
证明,如果Ai~j的优化顺序对应子问题Ai~k的解不是子问题Ai~k的优化解,那么,完全可以使用子问题Ai~k的优化解来取代Ai~j的优化顺序对应子问题Ai~k的解,最终可以得到比Ai~j此时的优化解代价更小解,矛盾,故而假设错误,A1n的优化顺序对应子问题A1k的解必是子问题A1~k的优化解。
同理可以证明Ak+1 ~ n部分。
Step 2: overlapping subproblems
Step 3: A recursive solution
定义:m[ i, j ] = 计算Ai~j的最小乘法数。
- m[ i,j ] = 0 , if i =j
- m[ i,j ] = mini≤k<j { m[ i,k ] + m[ k+1,j ] + pi-1pkpj } , if i < j, 其中,Pm表示第m个矩阵的列数。
Step 4: Computing the optimal costs
Matrix-Chain-Order(p)
1. n=length(p)-1;
2. let m[1..n , 1..n] and s[1..n-1 , 2..n] be new chain length
3. FOR i=1 TO n DO
4. m[i, i]=0;
5. FOR l=2 TO n DO /* 计算从对角线开始,第l条对角线 */
6. FOR i=1 TO n-l+1 DO /*n-l+1是第l条对角线上的元素*/
7. j = i+l-1; /*计算纵坐标*/
8. m[i, j] = ∞;
9. FOR k =i To j-1 DO /* 计算m[i,j] */
10. q=m[i, k]+m[k+1, j]+Pi-1PkPj
11. IF q<m[i, j]
12. THEN
13. m[i,j]=q;
14. s[i.j]=k;
15. Return m
显然其算法时间复杂度:Θ(n3),空间复杂度O(n2)。
Step 4: Constructing an optimal solution
PRINT-OPTIMAL-PARENS(s,i,j)
1. if i == j
2. print “A”
3. else
4. print “(”
5. PRINT-OPTIMAL-PARENS(s,i,s[i,j])
6. PRINT-OPTIMAL-PARENS(s,s[i,j]+1,j)
7. print “)”

浙公网安备 33010602011771号