Matrix Chain Multiplication-Final

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Matrix Chain

Multiplication
Presented by:
M Ahsan Iqbal MSCS1973113
Syed Ahmed Ali MSCS2073147
Shoaib Ahmed MSCS2073131
Khawaja M. Moeed Ghori MSCS1973123
Agenda

• About Matrix Multiplication (MM)


• About Matrix Chain Multiplication MCM
• Dynamic Programming
• Recursive Approach
• Problem With Recursive
• Dynamic Programming
• Memoization
• Bottom-Up Approach
Matrix Multiplication (MM)

• Matrix refers to the one- or two-dimensional array of elements.


• Each element of array holds unique index.
• Process of multiplying two matrices is called Matrix Multiplication.
• The number of columns of first matrix must be equal to the number of
rows of the second matrix.
• The product will be new matrix.
Matrix Chain Mutiplication (MCM)

• For understanding Matrix Chain Multiplication, let’s try to explain it through


below example.

• 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)

• MCM problem will always be presented either


in from of array or string or any iterable data
type.
• Recursive approach required to partition the
given dataset in between the range of i & j.
• I & j will be near endpoints and it requires to
traverse the string/array and solve for each
partition.
• The number of characters between i & j are
break at k, giving us multiple sub-problem,
which further be solved in recursive manner.
The recursive formula for the above flow will be

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

• If you notice, our recursive algorithm to the


matrix chain problem makes exponentially
many calls to find solutions of the same
subproblems.
• The algorithm revisits the same subproblem
again and again. In other words, the
subproblems overlap ( Shaded green areas).
Dynamic Programming

• 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 technique for improving performance of recursive algorithm through


dynamic programing which we have discussed above is called
memoization.
• It uses Cache Matrix/Table to store paths.
• When ever the same combination of matrices repeats during process of
recursive algorithm it returns the sub problem from the cache table.
Bottom-Up Approach

• The optimal cost can also be achieved by implementing a tabular, bottom-


up approach.
• First, we shall create a 2-dimensional array for storing m[i,j].
• When ever the same combination of matrices repeats during process of
recursive algorithm it returns the sub problem from the cache table.
Bottom-Up Approach

• First-of-all, all the entries in the main


diagonal are set to 0.
• Next, we compute all the entries in the
diagonal just above it, which we call
diagonal 1.
• Then, we compute all the entries of the
next diagonal which is 2, and so on.
• When we reach the last diagonal and
compute its entry, we get the final answer.
Bottom-Up Approach by Example

• 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

You might also like