1 / 49

Matrix Chain Multiplication

Matrix Chain Multiplication. matrix multiplication. c. b. b. =. a. A = a x b matrix B = b x c matrix. Multiplying the Matrix. c. b. b. =. a. Time used = Θ (abc). Naïve Method. for (i = 1; i <= a;i++) { for (j = 1; i <= c;j++) { sum = 0; for (k = 1;k <= b;k++) {

keahi
Télécharger la présentation

Matrix Chain 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. Matrix Chain Multiplication

  2. matrix multiplication c b b = a A = a x b matrix B = b x c matrix

  3. Multiplying the Matrix c b b = a Time used = Θ(abc)

  4. Naïve Method for (i = 1; i <= a;i++) { for (j = 1; i <= c;j++) { sum = 0; for (k = 1;k <= b;k++) { sum += A[i][k] * B[k][j]; } C[i][j] = sum; } } O(abc)

  5. Matrix Chain Multiplication N x N matrix N x N matrix N x 1 matrix B C A How to compute ABC ?

  6. Matrix Multiplication • ABC = (AB)C = A(BC) • (AB)C differs from A(BC)? • Same result, different efficiency • What is the cost of (AB)C? • What is the cost of A(BC)?

  7. (AB)C N x N N x N N*N*N N x N N x 1 N*N*1 N x 1

  8. A(BC) N x N N x 1 N*N*1 N x N N x 1 N*N*1 N x 1

  9. The Problem • Input: a1,a2,a3,….,an • n-1 matrices of sizes • a1 x a2 B1 • a2 x a3 B2 • a3 x a4 B3 • …. • an-1 x an Bn-1 • Output • The order of multiplication • How to parenthesize the chain

  10. Example INPUT • a1 a2 a3 a4 a5 a6 • 10 x 5 x 1 x 5 x 10 x 2 B1 B2 B3 B4 B5 Possible Output ((B1B2)(B3B4))B5 (B1B2)((B3B4)B5) (B1((B2B3)B4))B5 And much more…

  11. Consider the Output What do (B1B2)((B3B4)B5) (B1B2)(B3(B4B5)) What do have in common? ((B1B2)(B3B4))B5 (((B1B2)B3)B4))B5 have in common?

  12. Solving B1 B2 B3 B4 … Bn-1 Min cost of B1 (B2 B3 B4 … Bn-1 ) (B1 B2) (B3 B4 … Bn-1 ) (B1 B2 B3) (B4 … Bn-1 ) … (B1 B2 B3 B4 …) Bn-1

  13. Solving B1 B2 B3 B4 … Bn-1 Min cost of B1(B2 B3 B4 … Bn-1 ) (B1 B2)(B3 B4 … Bn-1 ) (B1 B2 B3) (B4 … Bn-1 ) … (B1 B2 B3 B4 …) Bn-1 Sub problem Sub problem Sub problem Sub problem Sub problem Sub problem

  14. Matrix Chain Multiplication B1…BN-1 (B1)(B2…BN-1) (B1B2)(B3…BN-1) (B1…)(BN-1) … (B…)(B2…BN-2) … … … … (B2…BN-2) (BN-1)

  15. Deriving the Recurrent • mcm(l,r) • The least cost to multiply Bl … Br • The solution is mcm(1,n-1)

  16. The Recurrence • Initial Case • mcm(x,x) = 0 • mcm(x,x+1) = a[x] * a[x+1] * a[x+2]

  17. The Recurrence min cost of Bl(Bl+1 Bl+2 Bl+3… Br ) + al•al+1•ar+1 min cost of (Bl Bl+1)(Bl+2 Bl+3… Br ) + al•al+2•ar+1 min cost of (Bl Bl+1 Bl+2) (Bl+3… Br ) + al•al+3•ar+1 … min cost of (Bl Bl+1 Bl+2 Bl+3…)Br + al•ar•ar+1 mcm(l,r) = min of

  18. The Recurrence 0 mcm(l+1,r) + Bl(Bl+1 Bl+2 Bl+3… Br ) + al•al+1•ar+1 (Bl Bl+1)(Bl+2 Bl+3… Br ) + al•al+2•ar+1 (Bl Bl+1 Bl+2) (Bl+3… Br ) + al•al+3•ar+1 … (Bl Bl+1 Bl+2 Bl+3…)Br + al•ar•ar+1 + mcm(l,l+1) mcm(l+2,r) + + mcm(l,r) = min of mcm(l,l+2) mcm(l+3,r) + + mcm(l,r-1) 0 + +

  19. Matrix Chain Multiplication intmcm(intl,int r) { if (l < r) { minCost = MAX_INT; for (inti = l;i < r;i++) { my_cost = mcm(l,i) + mcm(i+1,r) + (a[l] * a[i+1] * a[r+1]); minCost = min(my_cost,minCost); } returnminCost; } else { return 0; } }

  20. Using bottom-up DP • Design the table • M[i,j] = the best solution (min cost) for multiplying Bi…Bj • The solution is at M[i,n-1]

  21. What is M[i,j]? • Trivial case • What is m[x,x] ? • No multiplication, m[x,x] = 0

  22. What is M[i,j]? • Simple case • What is m[x,x+1] ? • BxBx+1 • Only one solution = ax* ax+1* ax+2

  23. What is M[i,j]? • General case • What is m[x,x+k] ? • BxBx+1Bx+2…Bx+k Bx(Bx+1 Bx+2 Bx+3 … Bx-k) + ax•ax+1•ax+k+1 (Bx Bx+1)(Bx+2 Bx+3 … Bx+k) + ax•ax+2•ax-k+1 (Bx Bx+1 Bx+2) (Bx+3 … Bx+k) + ax•ax+3•ax+k+1 … (Bx Bx+1 Bx+2 Bx+3 …)Bx+k + ax•ax+k•ax+k+1 min of

  24. Filling the Table M[1,1] M[1,6] (our solution)

  25. Filling the Table Trivial case

  26. Filling the Table Arbitrary case

  27. Filling the Table Arbitrary case Plus a1•a2•a6

  28. Filling the Table Arbitrary case Plus a1•a3•a6

  29. Filling the Table Arbitrary case Plus a1•a4•a6

  30. Filling the Table Arbitrary case Plus a1•a5•a6

  31. Filling the Table

  32. Filling the Table 5 4 3 2 1

  33. Example • a1 a2 a3 a4 a5 a6 • 10 x 5 x 1 x 5 x 10 x 2 B1 B2 B3 B4 B5

  34. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2

  35. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2

  36. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2

  37. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2

  38. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2

  39. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2

  40. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2 Option 1 = 0 + 25 + 10 x 5 x 5 = 275

  41. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2 Option 2 = 50 + 25 + 10 x 1 x 5 = 100 minimal

  42. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2 (2) means that the minimal solution is by dividing at B2 Option 2 = 50 + 25 + 10 x 1 x 5 = 100 minimal

  43. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2 Option 1 = 0+ 50 + 5x 1 x 10 = 100

  44. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2 Option 1 = 25+ 0 + 5x 5 x 10 = 275

  45. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2 Option 1 is better

  46. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2

  47. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2

  48. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2

  49. Example a1 a2 a3 a4 a5 a6 10 x 5 x 1 x 5 x 10 x 2

More Related