150 likes | 436 Views
2 x 4. 4 x 3. 2 x 3. Recalling Matrix multiplication. More generally, if A is p x q, B is q x r, then AB is a p x r matrix with:. matrix multiplication implementation. for (i=0; i<p; i++) { for (j=0; j<r; j++) { result[i][j] = 0; for (k=0; k<q; k++) {
E N D
2 x 4 4 x 3 2 x 3 Recalling Matrix multiplication More generally, if A is pxq, B is qxr, then AB is a pxr matrix with:
matrix multiplication implementation for (i=0; i<p; i++) { for (j=0; j<r; j++) { result[i][j] = 0; for (k=0; k<q; k++) { result[i][j] += a[i][k] * b[k][j]; } } }
Complexity • matrix multiplication for A:(p x q) * B:(q x r) requires pqr scalar multiplies. • For square matrices, complexity is O(n3).
Multiplication is associative • Let A:(pxq), B:(qxs), C:(sxr), so: • A(BC) = (AB)C • A(BC) requires qsr + pqr = (s+p)qr multiplies. • (AB)C requires pqs + psr = (q+r)ps multiplies. • Do they necessarily take the same amount of time to compute?
Example • Let A:(10 x 100), B:(100 x 5), and C:(5 x 50) • Now (p,q,s,r) = (10,100,5,50), and • A(BC) needs: qsr + pqr multiplications = 100*5*50 +10*100*50 25,000 + 50,000 = 75,000 • Now (AB)C needs: pqs + psr = 10*100*5 + 10*5*50 5000 + 2500 = 7,500 The multiplication “sequence” is important!!
The Matrix Chain Multiplication Problem • Given n matrices A1… An to be multiplied in this order, determine how to group the matrices so that the number of scalar multiplications is minimized
The MCM Problem • Divide the original problem A1… An where Ai (pi-1 x pi) into two sub-problems • The first k matrices • The last n-k matrices • Multiply the first k matrices to obtain a matrix M • Multiply the last n-k matrices to obtain a matrix N • Multiply M by N to obtain the solution How to determine k? • Calculate all (the number of scalar multiplications) for all k, and choose the minimum
let m[i][j] to be the min # of scalar multiplications to multiply matrices Ai … Aj then m[i][j] = 0 when i=j m[i][j] = minimum over k of m[i][k] + m[k+1][j] + pi-1 pk pj Where m[i][k]: is the min # of scalar multiplications to multiply matrices i through k m[k+1][j]: is the min # of scalar multiplications to multiply matrices k+1 through j pi-1 pk pj is the # of scalar multiplications to compute the product of the two matrices that result 8
A recursive solution int minMult(int i, int j) { if (i==j) return 0; else { best[i][j] = ; for (int k=i; k<j; k++) { cost = minMult(i,k) + minMult(k+1,j) + p[i-1]*p[k]*p[j]; if (cost < best[i][j]) { best[i][j] = cost; split[i][j] = k; } return best[i][j]; } } } 9
A DP algorithm • begins by computing the simplest sub-problems first, • m[i][j] = 0 when i=j, • Then it uses the equation bellow to compute m[i][j] • m[i][j] = minimum over k of m[i][k] + m[k+1][j] + pi-1 pk pj • After each value is computed, it is stored in matrix m • When the value is needed later, it is obtained from the matrix
Overall running time is O(n3) A DP solution for (int i=1; i<=n; i++) m[i][i] = 0; for (int d=2; d<=n; d++) { //d=length of subchain for (int i=1; i<n-d+1; i++) {//i=start of subchain int j = i+d-1;//j=end of subchain m[i][j] = ; for (int k=i; k<j; k++) { int cost = m[i][k] + m[k+1][j]+p[i-1]*p[k]*p[j]; if (cost < m[i][j]) m[i][j] = cost; split[i][j] = k; }}} return m[0][n-1]; 11
Optimal multiplication • Given a chain of matrices, suppose split is computed, then • MM (i, j) {if i == j return Ai else {k = split[i][j]; t1 = MM(i, k); t2 = MM(k+1, j); return t1*t2; } }
Example Solve the following MCM instance P=<30, 35, 15, 5, 10, 20, 25> Let the sequence be A1A2…A6 and A1 30×35 A2 35×15 A3 15×5 A4 5×10 A5 10×20 A6 20×25