SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Matrix Multiplication Chains 
• Determine the best way to compute the 
matrix product M1x M2 x M3 x … x Mq. 
• Let the dimensions of Mi be rix ri+1. 
• q-1 matrix multiplications are to be done. 
• Decide the matrices involved in each of 
these multiplications.
Decision Sequence 
• M1x M2 x M3 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.
Problem State 
• M1x M2 x M3 x … x Mq 
• The matrices involved in each multiplication are a 
contiguous subset of the given q matrices. 
• The problem state is given by a set of pairs of the 
form (i, j), i <= j. 
 The pair (i,j) denotes a problem in which the matrix 
product Mix Mi+1 x … x Mj is to be computed. 
 The initial state is (1,q). 
 If the last matrix product is (M1x M2 x … x Mk) x (Mk+1x Mk+2 
x … x Mq), the state becomes {(1,k), (k+1,q)}.
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 Mijis Mikx Mk+1,j for 
some k, i <= k < j. 
• Irrespective of what k is, a best computation 
of Mijin which the last product is Mikx Mk+1,j 
has the property that Mikand Mk+1,j are 
computed in the best possible way. 
• So the principle of optimality holds and 
dynamic programming may be applied.
Recurrence Equations 
• Let c(i,j) be the cost of an optimal (best) way to 
compute Mij, 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 Mikx Mk+1,j. 
• c(i,i) = 0, 1 <= i <= q. (Mii= Mi) 
• c(i,i+1) = riri+1ri+2, 1 <= i < q. (Mii+1= Mix Mi+1) 
• kay(i,i+1) = i.
c(i, i+s), 1 < s < q 
• The last multiplication in the best way to 
compute Mi,i+s is Mikx Mk+1,i+s for some k, i <= k < 
i+s. 
• If we knew k, we could claim: 
c(i,i+s) = c(i,k) + c(k+1,i+s) + rirk+1ri+s+1 
• Since i <= k < i+s, we can claim 
c(i,i+s) = min{c(i,k) + c(k+1,i+s) + rirk+1ri+s+1}, where 
the min is taken over i <= k < i+s. 
• kay(i,i+s) is the k that yields above min.
Recurrence Equations 
• c(i,i+s) = 
min i <= k < i+s{c(i,k) + c(k+1,i+s) + rirk+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(q3) time. 
• Implement nonrecursively for best worst-case 
efficiency.
Example 
• q = 4, (10 x 1) * (1 x 10) * (10 x 1) * (1 x 10) 
• r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] 
1 2 3 4 
1 
2 
3 
4 
c(i,j), i <= j kay(i,j), i <= j
s = 0 
c(i,i) and kay(i,i) , 1 <= i <= 4 are to be computed. 
0 0 
0 0 
0 0 
0 0 
c(i,j), i <= j kay(i,j), i <= j
s = 1 
c(i,i+1) and kay(i,i+1) , 1 <= i <= 3 are to be 
computed. 
0 0 
0 0 
0 0 
0 0 
c(i,j), i <= j kay(i,j), i <= j
s = 1 
• c(i,i+1) = riri+1ri+2, 1 <= i < q. (Mii+1= Mix Mi+1) 
• kay(i,i+1) = i. 
• r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] 
0 0 
100 1 
0 0 
10 2 
0 0 
100 3 
0 0 
c(i,j), i <= j kay(i,j), i <= j
s = 2 
• c(i,i+2) = min{c(i,i) + c(i+1,i+2) + riri+1ri+3, 
c(i,i+1) + c(i+2,i+2) + riri+2ri+3} 
• r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] 
0 0 
100 1 
0 0 
10 2 
0 0 
100 3 
0 0 
c(i,j), i <= j kay(i,j), i <= j
s = 2 
• c(1,3) = min{c(1,1) + c(2,3) + r1r2r4, 
c(1,2) + c(3,3) + r1r3r4} 
• r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] 
• c(1,3) = min{0 + 10 + 10, 100 + 0 + 100} 
0 0 
100 1 
20 1 
0 0 
10 2 
0 0 
100 3 
0 0 
c(i,j), i <= j kay(i,j), i <= j
s = 2 
• c(2,4) = min{c(2,2) + c(3,4) + r2r3r5, 
c(2,3) + c(4,4) + r2r4r5} 
• r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] 
• c(2,4) = min{0 + 100 + 100, 10 + 0 + 10} 
0 0 
100 1 
20 1 
20 3 
0 0 
10 2 
0 0 
100 3 
0 0 
c(i,j), i <= j kay(i,j), i <= j
s = 3 
• c(1,4) = min{c(1,1) + c(2,4) + r1r2r5, 
c(1,2) + c(3,4) + r1r3r5, c(1,3) + c(4,4) + r1r4r5} 
• r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] 
• c(1,4) = min{0+20+100, 100+100+1000, 20+0+100} 
0 0 
100 1 
20 120 1 
1 
20 3 
0 0 
10 2 
0 0 
100 3 
0 0 
c(i,j), i <= j kay(i,j), i <= j
Determine The Best Way To Compute M14 
• kay(1,4) = 1. 
• So the last multiplication is M14 = M11 x M24. 
• M11 involves a single matrix and no multiply. 
• Find best way to compute M24. 
0 0 
100 1 
20 120 1 
1 
20 3 
0 0 
10 2 
0 0 
100 3 
0 0 
c(i,j), i <= j kay(i,j), i <= j
Determine The Best Way To Compute M24 
• kay(2,4) = 3 . 
• So the last multiplication is M24 = M23 x M44. 
• M23 = M22 x M33. 
• M44 involves a single matrix and no multiply. 
0 0 
100 1 
20 120 1 
1 
20 3 
0 0 
10 2 
0 0 
100 3 
0 0 
c(i,j), i <= j kay(i,j), i <= j
The Best Way To Compute M14 
• The multiplications (in reverse order) are: 
 M14 = M11 x M24 
 M24 = M23 x M44 
 M23 = M22 x M33
Time Complexity 
1 2 3 4 
c(i,j), i <= j 
1 
2 
3 
4 
0 
100 
0 
20 
20 
10 
0 
120 
100 
0 
• O(q2) 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) + rirk+1ri+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(q3) time.
Time Complexity 
1 2 3 4 
kay(i,j), i <= j 
1 
2 
3 
4 
0 
1 
0 
1 
2 
0 
1 
3 
3 
0 
• The traceback takes O(1) time to determine 
each matrix product that is to be done. 
• q-1 products are to be done. 
• Traceback time is O(q).

Weitere ähnliche Inhalte

Was ist angesagt?

3f. Pedagogy of Mathematics (Part II) - Algebra (Ex 3.6)
3f. Pedagogy of Mathematics (Part II) - Algebra (Ex 3.6)3f. Pedagogy of Mathematics (Part II) - Algebra (Ex 3.6)
3f. Pedagogy of Mathematics (Part II) - Algebra (Ex 3.6)Dr. I. Uma Maheswari Maheswari
 
Worksheet 3 addition & subtraction
Worksheet 3 addition & subtractionWorksheet 3 addition & subtraction
Worksheet 3 addition & subtractionkrunamthip
 
Modular arithmetic
Modular arithmeticModular arithmetic
Modular arithmeticJanani S
 
Alg 2 : multiply ing & factoring
Alg 2 : multiply ing & factoringAlg 2 : multiply ing & factoring
Alg 2 : multiply ing & factoringpaksukur
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivationKumar
 
Circle generation algorithm
Circle generation algorithmCircle generation algorithm
Circle generation algorithmAnkit Garg
 
00000070 1 20130107-130231
00000070 1 20130107-13023100000070 1 20130107-130231
00000070 1 20130107-130231Niwat Namisa
 
Econometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions ManualEconometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions ManualLewisSimmonss
 
00000070 1 20130107-130231
00000070 1 20130107-13023100000070 1 20130107-130231
00000070 1 20130107-130231Niwat Namisa
 
Cubic Spline Interpolation
Cubic Spline InterpolationCubic Spline Interpolation
Cubic Spline InterpolationVARUN KUMAR
 
Expanding Binomial Brackets
Expanding Binomial BracketsExpanding Binomial Brackets
Expanding Binomial BracketsPassy World
 
Math22 Lecture1
Math22 Lecture1Math22 Lecture1
Math22 Lecture1hdsierra
 
Spm additional-mathematical-formulae-pdf
Spm additional-mathematical-formulae-pdfSpm additional-mathematical-formulae-pdf
Spm additional-mathematical-formulae-pdfMahadzir Ismail
 

Was ist angesagt? (19)

3f. Pedagogy of Mathematics (Part II) - Algebra (Ex 3.6)
3f. Pedagogy of Mathematics (Part II) - Algebra (Ex 3.6)3f. Pedagogy of Mathematics (Part II) - Algebra (Ex 3.6)
3f. Pedagogy of Mathematics (Part II) - Algebra (Ex 3.6)
 
Worksheet 3 addition & subtraction
Worksheet 3 addition & subtractionWorksheet 3 addition & subtraction
Worksheet 3 addition & subtraction
 
Modular arithmetic
Modular arithmeticModular arithmetic
Modular arithmetic
 
Class notes precalc
Class notes precalcClass notes precalc
Class notes precalc
 
Metric k center
Metric k centerMetric k center
Metric k center
 
Alg 2 : multiply ing & factoring
Alg 2 : multiply ing & factoringAlg 2 : multiply ing & factoring
Alg 2 : multiply ing & factoring
 
Ch 06
Ch 06Ch 06
Ch 06
 
Ch 03
Ch 03Ch 03
Ch 03
 
Bresenham derivation
Bresenham derivationBresenham derivation
Bresenham derivation
 
Circle generation algorithm
Circle generation algorithmCircle generation algorithm
Circle generation algorithm
 
Assignments 04
Assignments 04Assignments 04
Assignments 04
 
00000070 1 20130107-130231
00000070 1 20130107-13023100000070 1 20130107-130231
00000070 1 20130107-130231
 
Econometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions ManualEconometric Analysis 8th Edition Greene Solutions Manual
Econometric Analysis 8th Edition Greene Solutions Manual
 
00000070 1 20130107-130231
00000070 1 20130107-13023100000070 1 20130107-130231
00000070 1 20130107-130231
 
Cubic Spline Interpolation
Cubic Spline InterpolationCubic Spline Interpolation
Cubic Spline Interpolation
 
16083116
1608311616083116
16083116
 
Expanding Binomial Brackets
Expanding Binomial BracketsExpanding Binomial Brackets
Expanding Binomial Brackets
 
Math22 Lecture1
Math22 Lecture1Math22 Lecture1
Math22 Lecture1
 
Spm additional-mathematical-formulae-pdf
Spm additional-mathematical-formulae-pdfSpm additional-mathematical-formulae-pdf
Spm additional-mathematical-formulae-pdf
 

Ähnlich wie Matrix Chain Ordering in O(n^3) Time

Matrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmMatrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmRajKumar323561
 
Dynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain MultiplicationDynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain MultiplicationPecha Inc.
 
Dynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain MultiplicationDynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain MultiplicationKrishnakoumarC
 
Quantum factorization.pdf
Quantum factorization.pdfQuantum factorization.pdf
Quantum factorization.pdfssuser8b461f
 
Class-10-Mathematics-Chapter-1-CBSE-NCERT.ppsx
Class-10-Mathematics-Chapter-1-CBSE-NCERT.ppsxClass-10-Mathematics-Chapter-1-CBSE-NCERT.ppsx
Class-10-Mathematics-Chapter-1-CBSE-NCERT.ppsxSoftcare Solution
 
A study on number theory and its applications
A study on number theory and its applicationsA study on number theory and its applications
A study on number theory and its applicationsItishree Dash
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfjannatulferdousmaish
 
Longest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain MultiplicationLongest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain MultiplicationJaneAlamAdnan
 
Dynamic1
Dynamic1Dynamic1
Dynamic1MyAlome
 
DynamicProgramming.ppt
DynamicProgramming.pptDynamicProgramming.ppt
DynamicProgramming.pptDavidMaina47
 

Ähnlich wie Matrix Chain Ordering in O(n^3) Time (20)

Matrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithmMatrix chain multiplication in design analysis of algorithm
Matrix chain multiplication in design analysis of algorithm
 
Dynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain MultiplicationDynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain Multiplication
 
Dynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain MultiplicationDynamic Programming Matrix Chain Multiplication
Dynamic Programming Matrix Chain Multiplication
 
Quantum factorization.pdf
Quantum factorization.pdfQuantum factorization.pdf
Quantum factorization.pdf
 
Class-10-Mathematics-Chapter-1-CBSE-NCERT.ppsx
Class-10-Mathematics-Chapter-1-CBSE-NCERT.ppsxClass-10-Mathematics-Chapter-1-CBSE-NCERT.ppsx
Class-10-Mathematics-Chapter-1-CBSE-NCERT.ppsx
 
2.circle
2.circle2.circle
2.circle
 
A study on number theory and its applications
A study on number theory and its applicationsA study on number theory and its applications
A study on number theory and its applications
 
Computer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdfComputer algorithm(Dynamic Programming).pdf
Computer algorithm(Dynamic Programming).pdf
 
Longest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain MultiplicationLongest Common Subsequence & Matrix Chain Multiplication
Longest Common Subsequence & Matrix Chain Multiplication
 
2018 MUMS Fall Course - Sampling-based techniques for uncertainty propagation...
2018 MUMS Fall Course - Sampling-based techniques for uncertainty propagation...2018 MUMS Fall Course - Sampling-based techniques for uncertainty propagation...
2018 MUMS Fall Course - Sampling-based techniques for uncertainty propagation...
 
MFCS-17.ppt
MFCS-17.pptMFCS-17.ppt
MFCS-17.ppt
 
Laws of Exponent
Laws of ExponentLaws of Exponent
Laws of Exponent
 
Computer science-formulas
Computer science-formulasComputer science-formulas
Computer science-formulas
 
CLIM Fall 2017 Course: Statistics for Climate Research, Spatial Data: Models ...
CLIM Fall 2017 Course: Statistics for Climate Research, Spatial Data: Models ...CLIM Fall 2017 Course: Statistics for Climate Research, Spatial Data: Models ...
CLIM Fall 2017 Course: Statistics for Climate Research, Spatial Data: Models ...
 
Daa chapter 3
Daa chapter 3Daa chapter 3
Daa chapter 3
 
Computer graphics 2
Computer graphics 2Computer graphics 2
Computer graphics 2
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Dynamic1
Dynamic1Dynamic1
Dynamic1
 
DynamicProgramming.ppt
DynamicProgramming.pptDynamicProgramming.ppt
DynamicProgramming.ppt
 
CIRCLES.pptx
CIRCLES.pptxCIRCLES.pptx
CIRCLES.pptx
 

Mehr von Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec37
Lec37Lec37
Lec37
 
Lec36
Lec36Lec36
Lec36
 
Lec35
Lec35Lec35
Lec35
 
Lec34
Lec34Lec34
Lec34
 
Lec33
Lec33Lec33
Lec33
 
Lec32
Lec32Lec32
Lec32
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec28
Lec28Lec28
Lec28
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec25
Lec25Lec25
Lec25
 
Lec24
Lec24Lec24
Lec24
 
Lec23
Lec23Lec23
Lec23
 
Lec21
Lec21Lec21
Lec21
 
Lec20
Lec20Lec20
Lec20
 
Lec19
Lec19Lec19
Lec19
 

Matrix Chain Ordering in O(n^3) Time

  • 1. Matrix Multiplication Chains • Determine the best way to compute the matrix product M1x M2 x M3 x … x Mq. • Let the dimensions of Mi be rix ri+1. • q-1 matrix multiplications are to be done. • Decide the matrices involved in each of these multiplications.
  • 2. Decision Sequence • M1x M2 x M3 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.
  • 3. Problem State • M1x M2 x M3 x … x Mq • The matrices involved in each multiplication are a contiguous subset of the given q matrices. • The problem state is given by a set of pairs of the form (i, j), i <= j.  The pair (i,j) denotes a problem in which the matrix product Mix Mi+1 x … x Mj is to be computed.  The initial state is (1,q).  If the last matrix product is (M1x M2 x … x Mk) x (Mk+1x Mk+2 x … x Mq), the state becomes {(1,k), (k+1,q)}.
  • 4. 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 Mijis Mikx Mk+1,j for some k, i <= k < j. • Irrespective of what k is, a best computation of Mijin which the last product is Mikx Mk+1,j has the property that Mikand Mk+1,j are computed in the best possible way. • So the principle of optimality holds and dynamic programming may be applied.
  • 5. Recurrence Equations • Let c(i,j) be the cost of an optimal (best) way to compute Mij, 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 Mikx Mk+1,j. • c(i,i) = 0, 1 <= i <= q. (Mii= Mi) • c(i,i+1) = riri+1ri+2, 1 <= i < q. (Mii+1= Mix Mi+1) • kay(i,i+1) = i.
  • 6. c(i, i+s), 1 < s < q • The last multiplication in the best way to compute Mi,i+s is Mikx Mk+1,i+s for some k, i <= k < i+s. • If we knew k, we could claim: c(i,i+s) = c(i,k) + c(k+1,i+s) + rirk+1ri+s+1 • Since i <= k < i+s, we can claim c(i,i+s) = min{c(i,k) + c(k+1,i+s) + rirk+1ri+s+1}, where the min is taken over i <= k < i+s. • kay(i,i+s) is the k that yields above min.
  • 7. Recurrence Equations • c(i,i+s) = min i <= k < i+s{c(i,k) + c(k+1,i+s) + rirk+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.
  • 8. 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(q3) time. • Implement nonrecursively for best worst-case efficiency.
  • 9. Example • q = 4, (10 x 1) * (1 x 10) * (10 x 1) * (1 x 10) • r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] 1 2 3 4 1 2 3 4 c(i,j), i <= j kay(i,j), i <= j
  • 10. s = 0 c(i,i) and kay(i,i) , 1 <= i <= 4 are to be computed. 0 0 0 0 0 0 0 0 c(i,j), i <= j kay(i,j), i <= j
  • 11. s = 1 c(i,i+1) and kay(i,i+1) , 1 <= i <= 3 are to be computed. 0 0 0 0 0 0 0 0 c(i,j), i <= j kay(i,j), i <= j
  • 12. s = 1 • c(i,i+1) = riri+1ri+2, 1 <= i < q. (Mii+1= Mix Mi+1) • kay(i,i+1) = i. • r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] 0 0 100 1 0 0 10 2 0 0 100 3 0 0 c(i,j), i <= j kay(i,j), i <= j
  • 13. s = 2 • c(i,i+2) = min{c(i,i) + c(i+1,i+2) + riri+1ri+3, c(i,i+1) + c(i+2,i+2) + riri+2ri+3} • r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] 0 0 100 1 0 0 10 2 0 0 100 3 0 0 c(i,j), i <= j kay(i,j), i <= j
  • 14. s = 2 • c(1,3) = min{c(1,1) + c(2,3) + r1r2r4, c(1,2) + c(3,3) + r1r3r4} • r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] • c(1,3) = min{0 + 10 + 10, 100 + 0 + 100} 0 0 100 1 20 1 0 0 10 2 0 0 100 3 0 0 c(i,j), i <= j kay(i,j), i <= j
  • 15. s = 2 • c(2,4) = min{c(2,2) + c(3,4) + r2r3r5, c(2,3) + c(4,4) + r2r4r5} • r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] • c(2,4) = min{0 + 100 + 100, 10 + 0 + 10} 0 0 100 1 20 1 20 3 0 0 10 2 0 0 100 3 0 0 c(i,j), i <= j kay(i,j), i <= j
  • 16. s = 3 • c(1,4) = min{c(1,1) + c(2,4) + r1r2r5, c(1,2) + c(3,4) + r1r3r5, c(1,3) + c(4,4) + r1r4r5} • r = [r1,r2 ,r3,r4,r5] = [10, 1, 10, 1, 10] • c(1,4) = min{0+20+100, 100+100+1000, 20+0+100} 0 0 100 1 20 120 1 1 20 3 0 0 10 2 0 0 100 3 0 0 c(i,j), i <= j kay(i,j), i <= j
  • 17. Determine The Best Way To Compute M14 • kay(1,4) = 1. • So the last multiplication is M14 = M11 x M24. • M11 involves a single matrix and no multiply. • Find best way to compute M24. 0 0 100 1 20 120 1 1 20 3 0 0 10 2 0 0 100 3 0 0 c(i,j), i <= j kay(i,j), i <= j
  • 18. Determine The Best Way To Compute M24 • kay(2,4) = 3 . • So the last multiplication is M24 = M23 x M44. • M23 = M22 x M33. • M44 involves a single matrix and no multiply. 0 0 100 1 20 120 1 1 20 3 0 0 10 2 0 0 100 3 0 0 c(i,j), i <= j kay(i,j), i <= j
  • 19. The Best Way To Compute M14 • The multiplications (in reverse order) are:  M14 = M11 x M24  M24 = M23 x M44  M23 = M22 x M33
  • 20. Time Complexity 1 2 3 4 c(i,j), i <= j 1 2 3 4 0 100 0 20 20 10 0 120 100 0 • O(q2) 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) + rirk+1ri+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(q3) time.
  • 21. Time Complexity 1 2 3 4 kay(i,j), i <= j 1 2 3 4 0 1 0 1 2 0 1 3 3 0 • The traceback takes O(1) time to determine each matrix product that is to be done. • q-1 products are to be done. • Traceback time is O(q).