Example: marketing

Lecture 12: Chain Matrix Multiplication

Lecture12 Recallingmatrix Multiplication . Thechainmatrix multiplicationproblem. A dynamicprogrammingalgorithmforchainma-tr ix :An Matrix isa two-dimensionalarray ! " # # $ # ! " $ # .. % " & ! " ')(((* whichhas rowsand :Thefollowingis a+ , Matrix : - . ,1 ,,1/ !.! '(((*52 RecallingMatrixMultiplicationTheproduct of a Matrix anda Matrix is a Matrix givenby for and .Example:If ./01! ,,1'(* .01,,'(* then 43 43 ++.003 433')(*53 RemarksonMatrixMultiplication If is defined, maynotbedefined. Quitepossible that . Multiplicationis recursivelydefinedby 5 Matrix multiplicationisassociative, , Givena Matrix anda Matrix , thedirectway of multiplying is to computeeach for and .ComplexityofDirectMatrixmultiplication: Notethat has entriesandeachentry takes timetocomputesothetotalproceduretakes Givena Matrix , a Matrix anda Matrix , then canbecomputedintwo ways and :Thenumberof multiplicationsneededare: 5 When ,, +, 1and , then .3 ..5A bigdifference!)))))))

Recalling Matrix Multiplication The product of a matrix and a matrix is a matrix given by for and . Example: If 0 . / 1! , 1 ' (* then . 43

Tags:

  Matrix, Multiplication, Multiplication matrix

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of Lecture 12: Chain Matrix Multiplication

1 Lecture12 Recallingmatrix Multiplication . Thechainmatrix multiplicationproblem. A dynamicprogrammingalgorithmforchainma-tr ix :An Matrix isa two-dimensionalarray ! " # # $ # ! " $ # .. % " & ! " ')(((* whichhas rowsand :Thefollowingis a+ , Matrix : - . ,1 ,,1/ !.! '(((*52 RecallingMatrixMultiplicationTheproduct of a Matrix anda Matrix is a Matrix givenby for and .Example:If ./01! ,,1'(* .01,,'(* then 43 43 ++.003 433')(*53 RemarksonMatrixMultiplication If is defined, maynotbedefined. Quitepossible that . Multiplicationis recursivelydefinedby 5 Matrix multiplicationisassociative, , Givena Matrix anda Matrix , thedirectway of multiplying is to computeeach for and .ComplexityofDirectMatrixmultiplication: Notethat has entriesandeachentry takes timetocomputesothetotalproceduretakes Givena Matrix , a Matrix anda Matrix , then canbecomputedintwo ways and :Thenumberof multiplicationsneededare: 5 When ,, +, 1and , then .3 ..5A bigdifference!)))))))

2 Implication:Themultiplication sequence (parenthesization)is important!!6 TheChainMatrixMultiplicationProblemGiven dimensions 5 5 5 correspondingto Matrix sequence , ,5 5 5, where hasdimension ,determinethe multiplicationsequence thatminimizesthenumberofscalarmultiplica tionsin computing . Thatis, determinehow toparenthisizethemultiplications. - Exhaustivesearch: + .Question:Any betterapproach?Yes DP7 Developinga DynamicProgrammingAlgorithmStep1:Determi nethestructureof anoptimalsolution(inthiscase, a parenthesization).Decomposetheproblemint osubproblems:Foreachpair , determinethemultiplicationsequencefor thatminimizesthenumberof , is a :determinesequenceof multiplica-tionfor .8 Developinga DynamicProgrammingAlgorithmStep1:Determi nethestructureof anoptimalsolution(inthiscase, a parenthesization).High-LevelParenthesiza tionfor Forany optimalmultiplicationsequence, atthelaststepyouaremultiplyingtwo matrices and forsome . Thatis, 5 Example 5 Here.

3 9 Developinga DynamicProgrammingAlgorithmStep1 Continued:Thustheproblemofdetermin-ingth eoptimalsequenceofmultiplicationsis brokendowninto2 questions: How dowe decidewhereto splitthechain(whatis )?(Searchallpossible valuesof ) How dowe parenthesize thesubchains and ?(Problemhasoptimalsubstructurepropertyt hat and mustbeoptimalsowe canap-plythesameprocedurerecursively)10 Developinga DynamicProgrammingAlgorithmStep1 Continued:OptimalSubstructureProperty:If final optimal so-lutionof involvessplittinginto and at finalstepthenparenthesizationof and infinaloptimalsolutionmustalsobeoptimalf orthesubproblems standingalone :If parenthisizationof wasnotoptimalwe couldreplaceit by a betterparenthesizationandgeta cheaperfinalsolution,leadingto a , if parenthisizationof wasnotop-timalwe couldreplaceit by a betterparenthesizationandgeta cheaperfinalsolution,alsoleadingto a DynamicProgrammingAlgorithmStep2 problem,we willstorethesolutionsto thesubproblemsin , let " denotetheminimumnumberofmultiplicationsn eededtocompute.

4 Theoptimumcostcanbedescribedby thefollowingrecursive DynamicProgrammingAlgorithmStep2:Recursi velydefinethevalueofanoptimalsolution. Proof:Any optimalsequenceof multiplicationfor is equivalentto somechoiceof splitting forsome , wherethesequencesof multiplicationsfor and 513 Developinga DynamicProgrammingAlgorithmStep2 Continued:We know that,forsome 5We don tknow what is, thoughBut,thereareonly ! possiblevaluesof sowecancheck 14 Developinga DynamicProgrammingAlgorithmStep3:Compute thevalueof anoptimalsolutionin : . onlydefinedfor .Theimportantpointis thatwhenwe usetheequation tocalculate we musthave alreadyevaluated and Forbothcases, thecorrespondinglengthofthematrix-chaina rebothlessthan . Hence, thealgorithmshouldfillthetable in increasingorderof thelengthof , we calculatein theorder ! ! " # $ % & ' ( ) * * + , - & ) /.0 * 1 .. * 2 # & & 15 DynamicProgrammingDesignWarning!!Whendes igninga dynamicprogrammingalgorithmtherearetwo items.

5 Example: thetable tableitemiscalculatedusingtherecurrencer elation,allthetable valuesneededby therecurrencerelationhave ourexamplethismeansthatby thetime " is calculatedallofthevalues and :Givena chainoffourmatrices , , and , with ,, +, 1, and 0. Find + .S0:Initialization 43211234m[i,j]54627A1A2A3A4p0p1p2p3p4000 0ij17 Example ContinuedStp1:Computing Bydefinition & " $ 35 43211234m[i,j]54627A1A2A3A4p0p1p2p3p4000 0ij12018 Example ContinuedStp2:Computing # Bydefinition # $ $ +.5 43211234m[i,j]54627A1A2A3A4p0p1p2p3p4000 0ij1204819 Example ContinuedStp3:Computing # + Bydefinition # + + + + .+5 43211234m[i,j]54627A1A2A3A4p0p1p2p3p4000 0ij120488420 Example ContinuedStp4:Computing Bydefinition " $ # & # ..5 43211234m[i,j]54627A1A2A3A4p0p1p2p3p4000 0ij12048848821 Example ContinuedStp5:Computing # + Bydefinition # + $ + $ # & # + $ # + + 43+5 43211234m[i,j]54627A1A2A3A4p0p1p2p3p4000 0ij12048848810422 Example ContinuedSt6:Computing + Bydefinition + + # + # + + +.

6 5 43211234m[i,j]54627A1A2A3A4p0p1p2p3p4000 0ij120488488104158We aredone!23 Developinga DynamicProgrammingAlgorithmStep4:Constru ctanoptimalsolutionfromcomputedinformati on :Maintainanarray 55 % 55 , where " de-notes fortheoptimalsplittingin computing . Thearray 55 55 canbeusedre-cursivelyto toRecovertheMultiplicationSequence? .. DynamicProgrammingAlgorithmStep4:Constru ctanoptimalsolutionfromcomputedinformati on :Consider 1. Assumethatthearray 551 % 551 1 + 1 , Hencethefinalmultiplicationsequenceis 525 TheDynamicProgrammingAlgorithmMatrix-Cha in( ) for( to ) ;for( to ) for( to ) ; ;for( to ) ;if ( ) ; ; return and ;(Optimumin ) takeson . Spacecomplexity .26 ConstructinganOptimalSolution:Compute Theactualmultiplicationcodeusesthe " valuetodeterminehow to splitthecurrentsequence. Assumethatthematricesarestoredinanarray ofmatrices 55 , andthat is globalto thisrecursive pro-cedure. Theprocedurereturnsa ( ) if ( ) ; is now , where is ; is now return ;multiplymatrices and elsereturn ; To compute , callMult( % ).

7 27 ConstructinganOptimalSolution:Compute ExampleofConstructinganOptimalSolution:C ompute .Considertheexampleearlier, where 1. Assumethatthearray 551 551 Mult Mult Mult Mult Hencetheproductis computedasfollows 528


Related search queries