Dynamic Programming
Topic- Matric Chain Multiplication(mcm)
Matrix Chain Multiplication
Matrix chain multiplication is an algorithmic technique used to determine the
most efficient way to multiply a chain of matrices together.
It is important because:-
*Optimization-reduce the no of operation and speed up matrix multiplication
*solving efficiency-large no of matrix can be multiply in optimal order and
reduce the processing time.
*Dp concept- that helps in learning the optimal structure(break inro smaller
subproblems) and overlapping subproblems concepts(solves the problems
multiple times).
*Without dp it can be solve using recursive approach in this approach it will
recompute the same subproblems multiple times making its inefficient and the
time complexity will near about exponential as it will a very large no.
*With using dp it can be solve in two ways
-Mnemonization(top-down)
-Tabulation(bottom-up)
And its time complexity will be O(N^3).
Problem solving Using Examples
Problem statement-Given ‘N’ matrices of varying dimensions, which are
multiplication compatible, find an order to multiply them such that the number
of unit operations is minimized
Before we start the MCM we need to understand the rules for matrix
multiplication
Two matrices A1 and A2 of dimensions [p x q] and [r x s] can only be multiplied
if and only if q=r.
The total number of multiplications required to multiply A1 and A2 are: p*q*s
Question- suppose take an array(size n+1) of 5 elements {10,20,30,40,50} find
out the minimum no of operation to multiply these 5 matrices.
Some rules to solve this MCM are
1)First start with the entire block/array and mark the i and j. so, to find out the f(i,j):-
2)Try all the partitions:-
There are 3 possible way for partition. For loop start from i and ends at j-1(1to3) and
the 2 partition will be f(i,j) and f(k+1,j).
3) run the best possible answer of the two partitions:
For base condition
If (i==j){
Return 0;}
It means i==j that in single matrix and no operation is req to multiply so its return 0.
Now , take a small example of 2 matrix to solve it out.
So, 2 possible way are there to partition i.e. is at K=1 and that is made as f(1,1) and
f(2,2) by f(i,k) and f(k+1,j) so, for base condition these will return 0. The partition
f(i,k) gives us a resultant matrix of dimensions [i -1 x k] and the partition f(k+1,j)
gives us a resultant matrix of dimensions [k,j]. Therefore, we need to count the
number of operations for our two partitions (k=1) as shown:
Now let's see the java code for this MCM:-
F(I,j)-min cost to multiply from ai to ak
F(k+1,j) “ “ “ (ak+1) to aj
arr[i-1] * arr[k] * arr[j] is the cost of
multiplying two resulting matrices:
Ai to Ak gives a matrix of size arr[i-1] × arr[k]
A(k+1) to Aj gives a matrix of size arr[k] ×
arr[j]
Applications of Matrix Chain Multiplication (MCM)
•Database Query Optimization
Determines the best order for table joins to minimize processing time.
➤
•Scientific & ML Computations
Optimizes matrix multiplication order in libraries like TensorFlow.
➤
•Computer Graphics
Speeds up combining multiple transformation matrices (e.g., scaling, rotation).
➤
•Signal Processing
Efficiently applies a series of linear system transformations.
➤
•Dynamic Programming Learning
Classic problem to teach recursion and DP techniques.
➤
"Thank you! This was a brief
overview of Matrix Chain
Multiplication."

Matrix Chain Multiplication.pptx file pp

  • 1.
    Dynamic Programming Topic- MatricChain Multiplication(mcm)
  • 2.
    Matrix Chain Multiplication Matrixchain multiplication is an algorithmic technique used to determine the most efficient way to multiply a chain of matrices together. It is important because:- *Optimization-reduce the no of operation and speed up matrix multiplication *solving efficiency-large no of matrix can be multiply in optimal order and reduce the processing time. *Dp concept- that helps in learning the optimal structure(break inro smaller subproblems) and overlapping subproblems concepts(solves the problems multiple times).
  • 3.
    *Without dp itcan be solve using recursive approach in this approach it will recompute the same subproblems multiple times making its inefficient and the time complexity will near about exponential as it will a very large no. *With using dp it can be solve in two ways -Mnemonization(top-down) -Tabulation(bottom-up) And its time complexity will be O(N^3).
  • 4.
    Problem solving UsingExamples Problem statement-Given ‘N’ matrices of varying dimensions, which are multiplication compatible, find an order to multiply them such that the number of unit operations is minimized Before we start the MCM we need to understand the rules for matrix multiplication Two matrices A1 and A2 of dimensions [p x q] and [r x s] can only be multiplied if and only if q=r.
  • 5.
    The total numberof multiplications required to multiply A1 and A2 are: p*q*s Question- suppose take an array(size n+1) of 5 elements {10,20,30,40,50} find out the minimum no of operation to multiply these 5 matrices.
  • 6.
    Some rules tosolve this MCM are 1)First start with the entire block/array and mark the i and j. so, to find out the f(i,j):- 2)Try all the partitions:-
  • 7.
    There are 3possible way for partition. For loop start from i and ends at j-1(1to3) and the 2 partition will be f(i,j) and f(k+1,j). 3) run the best possible answer of the two partitions: For base condition If (i==j){ Return 0;} It means i==j that in single matrix and no operation is req to multiply so its return 0. Now , take a small example of 2 matrix to solve it out.
  • 8.
    So, 2 possibleway are there to partition i.e. is at K=1 and that is made as f(1,1) and f(2,2) by f(i,k) and f(k+1,j) so, for base condition these will return 0. The partition f(i,k) gives us a resultant matrix of dimensions [i -1 x k] and the partition f(k+1,j) gives us a resultant matrix of dimensions [k,j]. Therefore, we need to count the number of operations for our two partitions (k=1) as shown: Now let's see the java code for this MCM:- F(I,j)-min cost to multiply from ai to ak F(k+1,j) “ “ “ (ak+1) to aj arr[i-1] * arr[k] * arr[j] is the cost of multiplying two resulting matrices: Ai to Ak gives a matrix of size arr[i-1] × arr[k] A(k+1) to Aj gives a matrix of size arr[k] × arr[j]
  • 10.
    Applications of MatrixChain Multiplication (MCM) •Database Query Optimization Determines the best order for table joins to minimize processing time. ➤ •Scientific & ML Computations Optimizes matrix multiplication order in libraries like TensorFlow. ➤ •Computer Graphics Speeds up combining multiple transformation matrices (e.g., scaling, rotation). ➤ •Signal Processing Efficiently applies a series of linear system transformations. ➤ •Dynamic Programming Learning Classic problem to teach recursion and DP techniques. ➤
  • 11.
    "Thank you! Thiswas a brief overview of Matrix Chain Multiplication."