Download Optimizing Matrix Multiplication Chains: Finding the Best Way to Multiply Matrices and more Slides Computer Science in PDF only on Docsity!
Matrix Multiplication Chains
- Determine the best way to compute the matrix product M 1 x M 2 x M 3 x … x M (^) q.
- Let the dimensions of M (^) i be ri x ri+1.
- q-1 matrix multiplications are to be done.
- Decide the matrices involved in each of these multiplications.
Decision Sequence
- M 1 x M 2 x M 3 x … x Mq
- Determine the q-1 matrix products in reverse order. What is the last multiplication? What is the next to last multiplication? And so on.
Verify Principle Of Optimality
- Let Mij = Mi x Mi+1 x … x Mj , i <= j.
- Suppose that the last multiplication in the best way to compute M (^) ij is M (^) ik x Mk+1,j for some k, i <= k < j.
- Irrespective of what k is, a best computation of Mij in which the last product is M (^) ik x M (^) k+1,j has the property that M (^) ik and M (^) k+1,j are computed in the best possible way.
- So the principle of optimality holds and dynamic programming may be applied. Docsity.com
Recurrence Equations
- Let c(i,j) be the cost of an optimal (best) way to compute M (^) ij , i <= j.
- c(1,q) is the cost of the best way to multiply the given q matrices.
- Let kay(i,j) = k be such that the last product in the optimal computation of Mij is M (^) ik x Mk+1,j.
- c(i,i) = 0, 1 <= i <= q. (M (^) ii = Mi )
- c(i,i+1) = ri ri+1ri+2, 1 <= i < q. (M (^) ii+1= M (^) i x Mi+1)
- kay(i,i+1) = i.
Recurrence Equations
min (^) i <= k < i+s {c(i,k) + c(k+1,i+s) + ri rk+1ri+s+1}
- c(,) terms on right side involve fewer matrices than does the c(,) term on the left side.
- So compute in the order s = 2, 3, …, q-1.
Recursive Implementation
- See text for recursive codes.
- Code that does not avoid recomputation of already computed c(i,j)s runs in Omega(2q) time.
- Code that does not recompute already computed c(i,j)s runs in O(q 3 ) time.
- Implement nonrecursively for best worst-case efficiency.
s = 0
c(i,i) and kay(i,i) , 1 <= i <= 4 are to be computed.
c(i,j), i <= j kay(i,j), i <= j
0 0 0 0 0 0 0 0
s = 1
c(i,i+1) and kay(i,i+1) , 1 <= i <= 3 are to be computed.
c(i,j), i <= j kay(i,j), i <= j
0 0 0 0 0 0 0 0
s = 2
- c(i,i+2) = min{c(i,i) + c(i+1,i+2) + ri ri+1ri+3,
c(i,i+1) + c(i+2,i+2) + ri ri+2ri+3}
- r = [r 1 ,r 2 ,r 3 ,r 4 ,r 5 ] = [10, 1, 10, 1, 10]
c(i,j), i <= j kay(i,j), i <= j
0 0 0 0 0 0 0 0
100 1 10 2 100 3
s = 2
- c(1,3) = min{c(1,1) + c(2,3) + r 1 r 2 r 4 ,
c(1,2) + c(3,3) + r 1 r 3 r 4 }
- r = [r 1 ,r 2 ,r 3 ,r 4 ,r 5 ] = [10, 1, 10, 1, 10]
- c(1,3) = min{0 + 10 + 10, 100 + 0 + 100}
c(i,j), i <= j kay(i,j), i <= j
0 0 0 0 0 0 0 0
100 1 10 2 100 3
20 1
s = 3
- c(1,4) = min{c(1,1) + c(2,4) + r 1 r 2 r 5 ,
c(1,2) + c(3,4) + r 1 r 3 r 5 , c(1,3) + c(4,4) + r 1 r 4 r 5 }
- r = [r 1 ,r 2 ,r 3 ,r 4 ,r 5 ] = [10, 1, 10, 1, 10]
- c(1,4) = min{0+20+100, 100+100+1000, 20+0+100}
c(i,j), i <= j kay(i,j), i <= j
0 0 0 0 0 0 0 0
100 1 10 2 100 3
20 1 20 3
120 1
Determine The Best Way To Compute M (^14)
- kay(1,4) = 1.
- So the last multiplication is M 14 = M 11 x M 24.
- M 11 involves a single matrix and no multiply.
- Find best way to compute M 24.
c(i,j), i <= j kay(i,j), i <= j
0 0 0 0 0 0 0 0
100 1 10 2 100 3
20 1 20 3
120 1
• The multiplications (in reverse order) are:
- The Best Way To Compute M
- M 14 = M 11 x M
- M 24 = M 23 x M
- M 23 = M 22 x M
Time Complexity
- O(q 2 ) c(i,j) values are to be computed, where q is the number of matrices.
- c(i,i+s) = min (^) i <= k < i+s{c(i,k) + c(k+1,i+s) + r (^) ir (^) k+1 r (^) i+s+1 }.
- Each c(i,j) is the min of O(q) terms.
- Each of these terms is computed in O(1) time.
- So all c(i,j) are computed in O(q 3 ) time.
1 2 3 4
c(i,j), i <= j
1 2 3 4
0 0 0 0
100 10 100
20 20
120