Matrix multiplication, Zusammenfassungen von Mathematik

Toggle sidebar
bg1
Lecture 12: Chain Matrix Multiplication
CLRS Section 15.2
Outline of this Lecture
Recalling matrix multiplication.
The chain matrix multiplication problem.
A dynamic programming algorithm for chain ma-
trix multiplication.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Unvollständige Textvorschau

Nur auf Docsity: Lade Matrix multiplication und mehr Zusammenfassungen als PDF für Mathematik herunter!

Lecture 12: Chain Matrix Multiplication CLRS Section 15. Outline of this Lecture

Recalling matrix multiplication.

The chain matrix multiplication problem.

A dynamic programming algorithm for chain ma- trix multiplication.

Recalling Matrix Multiplication

Matrix: An   matrix   ^  ^ is a two- dimensional array

 

which has rows and  columns.

Example: The following is a +  , matrix:   

, ,^1

Remarks on Matrix Multiplication

If   is defined,   may not be defined.

Quite possible that     .

Multiplication is recursively defined by                     (^)         5

Matrix multiplication is associative , e.g.,                      so parenthenization does not change result.

Direct Matrix multiplication  

Given a    matrix  and a    matrix  , the direct way of multiplying    is to compute each

^   ^    ^ ^ ^ 

for   ^  and    .

Complexity of Direct Matrix multiplication:

Note that has   entries and each entry takes    time to compute so the total procedure takes      time.

The Chain Matrix Multiplication Problem

Given dimensions   5 55  corresponding to matrix sequence  ,   , (^5) 55 ,   where   has dimension       , determine the “multiplication sequence” that minimizes the number of      scalar multiplications in computing (^) . That is, determine how to parenthisize the multiplications.              

            -                          

   

Exhaustive search:   + ^    .

Question: Any better approach? Yes – DP

Developing a Dynamic Programming Algorithm

Step 1: Determine the structure of an optimal solution (in this case, a parenthesization).

Decompose the problem into subproblems: For each pair     , determine the multiplication sequence for  ^      ^ ^   that minimizes the number of multiplications.

Clearly,  ^  is a       matrix.

Original Problem: determine sequence of multiplica- tion for  ^ .

Developing a Dynamic Programming Algorithm

Step 1 – Continued: Thus the problem of determin- ing the optimal sequence of multiplications is broken down into 2 questions:

How do we decide where to split the chain (what is )? (Search all possible values of )

How do we parenthesize the subchains   (^)  and     (^) ? (Problem has optimal substructure property that   (^)  and     (^)  must be optimal so we can ap- ply the same procedure recursively)

Developing a Dynamic Programming Algorithm

Step 1 – Continued:

Optimal Substructure Property: If final “optimal” so- lution of  ^  involves splitting into  ^  and    ^  at final step then parenthesization of  ^  and    ^  in final optimal solution must also be optimal for the subproblems “standing alone”:

If parenthisization of  ^  was not optimal we could replace it by a better parenthesization and get a cheaper final solution, leading to a contradiction.

Similarly, if parenthisization of    ^  was not op- timal we could replace it by a better parenthesization and get a cheaper final solution, also leading to a con- tradiction.

Developing a Dynamic Programming Algorithm

Step 2: Recursively define the value of an optimal solution.

^ ^ ^  

Proof: Any optimal sequence of multiplication for  ^  is equivalent to some choice of splitting    (^)     (^)      (^) 

for some   , where the sequences of multiplications for (^)  and     (^)  also are optimal. Hence    ^       ^  ^ ^         5

Developing a Dynamic Programming Algorithm

Step 2 – Continued: We know that, for some

   ^       ^  ^ ^         5 We don’t know what is, though But, there are only! possible values of so we can check them all and find the one which returns a smallest cost.

Therefore

^ ^ ^  

Dynamic Programming Design Warning!!

When designing a dynamic programming algorithm there are two parts:

  1. Finding an appropriate optimal substructure prop- erty and corresponding recurrence relation on ta- ble items. Example:

   ^    ^ 

 (^)       (^)     (^)        

  1. Filling in the table properly. This requires finding an ordering of the table el- ements so that when a table item is calculated using the recurrence relation, all the table values needed by the recurrence relation have already been calculated. In our example this means that by the time   " is calculated all of the values      and    were already calculated.

Example for the Bottom-Up Computation

Example: Given a chain of four matrices  ,   ,   and   , with   , ,   + ,   ^1 ,     and   ^0. Find   ^ + .

S0: Initialization (^)

             

   

             

          

           

4 3 2 1

1 2 3 4

m[i,j]

5 4 6 2 7 A1 A2 A3 A p0 p1 p2 p3^ p

0 0 0 0

j i

Example – Continued

Stp 2: Computing  # ^ By definition

 # ^    ^    $^       ^       

^5

             

   

             

          

           

4 3 2 1

1 2 3 4

m[i,j]

5 4 6 2 7 A1 A2 A3 A p0 p1 p2 p3^ p

0 0 0 0

j i 120 48

Example – Continued

Stp3: Computing  ^ #^ +  By definition

 ^ #^ +     ^    ^      ^ +         

(^)

             

   

             

          

           

4 3 2 1

1 2 3 4

m[i,j]

5 4 6 2 7 A1 A2 A3 A p0 p1 p2 p3^ p

0 0 0 0

j i 120 48 84