0% found this document useful (0 votes)
48 views

Example II Matrix Chain Multiplication Problem

The document describes the matrix chain multiplication problem and provides an example to minimize the cost of multiplying a sequence of matrices. It gives the sizes of 5 matrices as 4x10, 10x3, 3x12, 12x20, 20x7. It then calculates the cost m[i,j] of multiplying the matrices from i to j in all combinations, and fills a table with the minimum costs.

Uploaded by

abc
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Example II Matrix Chain Multiplication Problem

The document describes the matrix chain multiplication problem and provides an example to minimize the cost of multiplying a sequence of matrices. It gives the sizes of 5 matrices as 4x10, 10x3, 3x12, 12x20, 20x7. It then calculates the cost m[i,j] of multiplying the matrices from i to j in all combinations, and fills a table with the minimum costs.

Uploaded by

abc
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Example II :

Matrix Chain
Multiplication
Problem
Prachi Joshi
Example: We are given the sequence {4, 10,
3, 12, 20, and 7}. The matrices have size 4 x
10, 10 x 3, 3 x 12, 12 x 20, 20 x 7.

1 2 3 4 5
1
2
3
4
5
 

4*10 10*3 3*12 12*20 20*7

1 2 3 4 5
1 0
2 0
3 0
4 0
5 0
For just A1 Cost is 0. m[1,1]=0
For just A2 Cost is 0 m[2,2]=0
For just A3 Cost is 0 m[3,3]=0
For just A4 Cost is 0 m[4,4]=0
For just A5 Cost is 0 m[5,5]=0
 

4*10 10*3 3*12 12*20 20*7

Find m[1,2] = A1 . A2
= (4*10) (10*3)
= 4*10*3 1 2 3 4 5
= 120 1 0 120
2 0
3 0
4 0
5 0
 

4*10 10*3 3*12 12*20 20*7

Find m[2,3] = A2 . A3
= (10*3) (3*12)
= 10*3*12 1 2 3 4 5
= 360 1 0 120
2 0 360
3 0
4 0
5 0
 

4*10 10*3 3*12 12*20 20*7

Find m[3,4] = A3 . A4
= (3*12) (12*20)
= 3*12*20 1 2 3 4
= 720 1 0 120
2 0 360
3 0 720
4 0
5
 

4*10 10*3 3*12 12*20 20*7

Find m[4,5] = A4 . A5
= (12*20) (20*7)
= 12*20*7 1 2 3 4 5
= 1680 1 0 120
2 0 360
3 0 720
4 0 1680
5 0
Find m[1,3] = Two ways, take the minimum 
4*10 10*3 3*12 12*20 20*7
Way 1
A1 . (A2 . A3)
4*10 10*3 3*12
=m[1,1]+m[2,3]+4*10*12
=0+360+480
=840 1 2 3 4 5
1 0 120 264
Way 2 2 0 360
(A1 . A2) . A3
3 0 720
4*10 10*3 3*12
=m[1,2]+m[3,3]+4*3*12 4 0 1680
=120+0+144 5 0
=264
Find m[2,4] = Two ways, take the minimum 
4*10 10*3 3*12 12*20 20*7
Way 1
A2 . (A3 . A4)
10*3 3*12 12*20
=m[2,2]+m[3,4]+10*3*20
=0+720+600
=1320 1 2 3 4 5
1 0 120 264
Way 2 2 0 360 1320
(A2 . A3) . A4
3 0 720
10*3 3*12 12*20
=m[2,3]+m[4,4]+10*12*20 4 0 1680
=360+0+2400 5 0
=2760
Find m[3,5] = Two ways, take the minimum 
4*10 10*3 3*12 12*20 20*7
Way 1
A3 . (A4 . A5)
3*12 12*20 20*7
=m[3,3]+m[4,5]+3*12*7
=0+1680+252
=1932 1 2 3 4 5
1 0 120 264
Way 2 2 0 360 1320
(A3 . A4) . A5
3 0 720 1140
3*12 12*20 20*7
=m[3,4]+m[5,5]+3*20*7 4 0 1680
=720+0+420 5 0
=1140
 

4*10 10*3 3*12 12*20 20*7


Find m[1,4] Use the following formula

m[1,4]=min{m[1,1]+m[2,4]+4*10*20, m[1,2]+m[3,4]+4*3*2,
m[1,3]+m[4,4]+4*12*20}

=min{0+1320+800,120+720+240,264+0+960}
1 2 3 4 5
=min{2120,1080,1224} 1 0 120 264 1080
2 0 360 1320
=1080 3 0 720 1140
4 0 1680
5 0
 

4*10 10*3 3*12 12*20 20*7


Find m[2,5] Use the following formula

m[2,5]=min{m[2,2]+m[3,5]+10*3*7, m[2,3]+m[4,5]+10*12*7,
m[2,4]+m[5,5]+10*20*7}

=min{0+1932+210,360+1680+840,1320+0+1400}
1 2 3 4 5
=min{1350,2880,2720}
1 0 120 264 1080
=1350 2 0 360 1320 1350
3 0 720 1140
4 0 1680
5 0
 

4*10 10*3 3*12 12*20 20*7


Find m[1,5] Use the following formula

m[1,5]=min{m[1,1]+m[2,5]+4*10*7, m[1,2]+m[3,5]+4*3*7,
m[1,3]+m[4,5]+4*12*7,m[1,4]+m[5,5]+4*20*7}

=min{0+1350+280,120+1140+84,264+1680+336,1080+0+560}
1 2 3 4 5
=min{1630,1344,2280,1640}
1 0 120 264 1080 1344
=1344 2 0 360 1320 1350
3 0 720 1140
4 0 1680
5 0
1 2 3 4 5
1 0 120 264 1080 1344
2 0 360 1320 1350
3 0 720 1140
4 0 1680
5 0

You might also like