Professional Documents
Culture Documents
Matrix Chain Multiplication-Final
Matrix Chain Multiplication-Final
Matrix Chain Multiplication-Final
Multiplication
Presented by:
M Ahsan Iqbal MSCS1973113
Syed Ahmed Ali MSCS2073147
Shoaib Ahmed MSCS2073131
Khawaja M. Moeed Ghori MSCS1973123
Agenda
• Now The problem is not actually to perform the multiplications, but merely to
decide in which order to perform the multiplications in most efficient manner.
• Matrix Chain Multiplication’s goal is to find most efficient way to multiply the
matrices.
Matrix Chain Mutiplication (MCM)
Recursive (Top-Down Approach)
How does this formula constructed, can be explained through below diagram:
To go from the recursive formulation above to a program is straightforward:
Matrix-chain(i, j)
IF i = j THEN return 0
m=∞
FOR k = i TO j − 1 DO
q = Matrix-chain(i, k) + Matrix-chain(k + 1, j) +pi−1 · pk · pj
IF q < m THEN m = q
OD
Return m
END Matrix-chain
Problem With Recursive Approach
• Wherever we see a recursive solution that has repeated calls for same
inputs, we can optimize it using Dynamic Programming.
• Goal is to store results of problems for reducing the efforts of re-
computing.
• It reduces time complexities.
Memoization
• A.B.C.D 1 2 3 4
• A= 13*5 || B= 5*89 || C= 89*3 || D= 3*34 1 0
2 0
• Step 1: M {1,1} - A
M {2,2} – B 3 0
M {3,3} – C
4 0
M {4,4} – D
Bottom-Up Approach by Example
1 2 3 4
• A.B.C.D
• A= 13*5 || B= 5*89 || C= 89*3 || D= 3*34 1 0 5789
2 0 1335
• Step 2:
3 0 9078
M{1,2} – A.B – 13*5.5*89 – 13*5*89 = 5789
M {2,3} – B.C – 5*89.89*3 – 5*89*3 = 1335 4 0
M {3,4} – C.D – 89*3.3*34 – 89*3*34 = 9078
Bottom-Up Approach by Example
• A.B.C.D
• A= 13*5 || B= 5*89 || C= 89*3 || D= 3*34
1 2 3 4
• Step 3: M {1,3}
1 0 5789 1530
1st Possibility – A.(B.C)
= 13*5.5*89.89*3
= M{1,1} + M{3,3} + 13*5*3
2 0 1335
= Cost of A | Cost of BC | Cost A.(B.C)
= 0 + 1335 + 195 3 0 9078
= 1530
2nd Possibility - (A.B).C 4 0
= M{ 1,2} + M{3,3} + 13*89*3
= 5789 + 0 + 3471
= 9256
We will select the minimum value from the possibilities. So, M {1,3} = 1530
Bottom-Up Approach by Example
• A.B.C.D
• A= 13*5 || B= 5*89 || C= 89*3 || D= 3*34
1 2 3 4
• Step 4: M {2,4}
1 0 5789 1530
1st Possibility – B.(C.D)
= 5*89.89*3.3*34
= M{3,2} + M{3,4} + 5*89*34
2 0 1335 1845
= 0 + 9078 + 15130
= 24,208 3 0 9078
2nd Possibility - (B.C).D
= M{ 3,3} + M{4,4} + 5*3*34 4 0
= 1335 + 0 + 510
= 1845
We will select the minimum value from the possibilities. So, M {2,4} = 1845
Bottom-Up Approach by Example
• A.B.C.D
• A= 13*5 || B= 5*89 || C= 89*3 || D= 3*34 1 2 3 4
• Step 5: M {1,4} – A.B.C.D
For this we have 03 sequence 1 0 5789 1530 2856
1 – A.(B.C.D) - { M {1,1} + M {2,4} + 13*5*34}
2 – (A.B) . (C.D) - M {1,2} + M {3,4} + 13*89*34 2 0 1335 1845
3 – (A.B.C).D - M {1,3} + M {4,4} + 13*3*34}
Min { M {1,1} + M {2,4} + 13*5*34}, M {1,2} + M {3,4} + 13*89*34, M {1,3} + M {4,4} +
13*3*34}} 3 0 9078
Min {0+1845+2210, 5789+9078+39338, 1530+0+1326}
Min { 4055, 54021, 2856} 4 0
We will select the minimum value from the possibilities. So, M {1,4} = 2856
This means, 2856 no. of multiplication required to multiply A.B.C.D
Formula For Bottom-Up Approach
• M [I,j]
• min
1 2 3 4
• A= 13*5 || B= 5*89 || C= 89*3 || D= 3*34
• Step 5: M {1,4} – A.B.C.D
1 0 5789 1530 2856
For this we have 03 sequence
1 – A.(B.C.D) - { M {1,1} + M {2,4} + 13*5*34}
2 – (A.B) . (C.D) - M {1,2} + M {3,4} + 13*89*34
2 0 1335 1845
3 – (A.B.C).D - M {1,3} + M {4,4} + 13*3*34}
Min { M {1,1} + M {2,4} + 13*5*34}, M {1,2} + M {3,4} + 13*89*34, M {1,3} + M {4,4} + 3 0 9078
13*3*34}}
Min {0+1845+2210, 5789+9078+39338, 1530+0+1326}
4 0
Min { 4055, 54021, 2856}
We will select the minimum value from the possibilities. So, M {1,4} = 2856
This means, 2856 no. of multiplication required to multiply A.B.C.D
Thank You