1 / 14

Recalling Matrix multiplication

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++) {

zuzela
Télécharger la présentation

Recalling Matrix multiplication

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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:

  2. 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]; } } }

  3. Complexity • matrix multiplication for A:(p x q) * B:(q x r) requires pqr scalar multiplies. • For square matrices, complexity is O(n3).

  4. 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?

  5. 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!!

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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; } }

  13. 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

  14. The best and split tables

More Related