Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE) PDF Download

Matrix Chain Multiplication

Given a sequence of matrices, find the most efficient way to multiply these matrices together. The problem is not actually to perform the multiplications, but merely to decide in which order to perform the multiplications.
We have many options to multiply a chain of matrices because matrix multiplication is associative. In other words, no matter how we parenthesize the product, the result will be the same. For example, if we had four matrices A, B, C, and D, we would have:
(ABC)D = (AB)(CD) = A(BCD) = ....
However, the order in which we parenthesize the product affects the number of simple arithmetic operations needed to compute the product, or the efficiency. For example, suppose A is a 10 × 30 matrix, B is a 30 × 5 matrix, and C is a 5 × 60 matrix. Then,  
(AB)C = (10 × 30 × 5) + (10 × 5 × 60) = 1500 + 3000 = 4500 operations
A(BC) = (30 × 5 × 60) + (10 × 30 × 60) = 9000 + 18000 = 27000 operations.
Clearly the first parenthesization requires less number of operations.
Given an array p[] which represents the chain of matrices such that the ith matrix Ai is of dimension p[i - 1] x p[i]. We need to write a function MatrixChainOrder() that should return the minimum number of multiplications needed to multiply the chain.

Input: p[] = {40, 20, 30, 10, 30}  
Output: 26000  
There are 4 matrices of dimensions 40 x 20, 20 x 30, 30 x 10 and 10 x 30.
Let the input 4 matrices be A, B, C and D.  The minimum number of  multiplications are obtained by putting parenthesis in following way (A(BC))D --> 20 * 30 * 10 + 40 * 20 * 10 + 40 * 10 * 30

Input: p[] = {10, 20, 30, 40, 30}
Output: 30000
There are 4 matrices of dimensions 10 x 20, 20 x 30, 30 x 40 and 40 x 30.
Let the input 4 matrices be A, B, C and D.  The minimum number of multiplications are obtained by putting parenthesis in following way
((AB)C)D --> 10 * 20 * 30 + 10 * 30 * 40 + 10 * 40 * 30

Input: p[] = {10, 20, 30}  
Output: 6000  
There are only two matrices of dimensions 10 x 20 and 20 x 30. So there is only one way to multiply the matrices, cost of which is 10 * 20 * 30

  1. Optimal Substructure: A simple solution is to place parenthesis at all possible places, calculate the cost for each placement and return the minimum value. In a chain of matrices of size n, we can place the first set of parenthesis in n-1 ways. For example, if the given chain is of 4 matrices. let the chain be ABCD, then there are 3 ways to place first set of parenthesis outer side: (A)(BCD), (AB)(CD) and (ABC)(D). So when we place a set of parenthesis, we divide the problem into subproblems of smaller size. Therefore, the problem has optimal substructure property and can be easily solved using recursion.
    Minimum number of multiplication needed to multiply a chain of size n = Minimum of all n-1 placements (these placements create subproblems of smaller size)
  2. Overlapping Subproblems Following is a recursive implementation that simply follows the above optimal substructure property.

Below is the implementation of the above idea

  • C++
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • C
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • Java
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • Python3
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • C#
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • PHP
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • Javascript
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)

Output: Minimum number of multiplications is 30
The time complexity of the above naive recursive approach is exponential. It should be noted that the above function computes the same subproblems again and again. See the following recursion tree for a matrix chain of size 4. The function MatrixChainOrder(p, 3, 4) is called two times. We can see that there are many subproblems being called more than once.
Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)

Since same subproblems are called again, this problem has Overlapping Subproblems property. So Matrix Chain Multiplication problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, recomputations of same subproblems can be avoided by constructing a temporary array m[][] in bottom up manner.

Dynamic Programming Solution 

Following is the implementation of the Matrix Chain Multiplication problem using Dynamic Programming (Tabulation vs Memoization)

1. Using Memoization

  • C++
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • Java
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • Python3
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • C#
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • Javascript
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)

Output: Minimum number of multiplications is 18

2. Using Tabulation 

  • C++
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • C
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • Java
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • Python
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • C#
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • PHP
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
  • Javascript
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)
    Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)

Output: Minimum number of multiplications is 18
Time Complexity: O(n3 )
Auxiliary Space: O(n2)

The document Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Algorithms.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
81 videos|80 docs|33 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Matrix Chain Multiplication - Algorithms - Computer Science Engineering (CSE)

1. What is matrix chain multiplication and why is it important in computer science engineering?
Ans. Matrix chain multiplication is the process of multiplying multiple matrices together in a specific order to minimize the number of scalar multiplications required. It is important in computer science engineering because it has various applications in fields such as computer graphics, algorithm design, and optimization problems.
2. How is matrix chain multiplication different from regular matrix multiplication?
Ans. Matrix chain multiplication involves finding the most efficient way to multiply a sequence of matrices, whereas regular matrix multiplication is the process of multiplying two matrices together. Matrix chain multiplication requires determining the optimal order of matrix multiplication to minimize the overall cost, while regular matrix multiplication does not consider the order.
3. What is the complexity of matrix chain multiplication?
Ans. The complexity of matrix chain multiplication is O(n^3), where n is the number of matrices in the sequence. This complexity arises from the dynamic programming approach used to solve the problem, which involves filling in a table of subproblem solutions.
4. How can matrix chain multiplication be solved using dynamic programming?
Ans. Matrix chain multiplication can be solved using dynamic programming by breaking down the problem into smaller subproblems and storing the solutions to these subproblems in a table. The table is then used to determine the optimal order of matrix multiplication. This approach avoids redundant calculations and improves efficiency.
5. Can matrix chain multiplication be parallelized for faster computation?
Ans. Yes, matrix chain multiplication can be parallelized to achieve faster computation. By dividing the matrices into smaller chunks and assigning them to multiple processors or threads, the computations can be performed concurrently. This parallelization technique can significantly reduce the overall computation time, especially for large matrices.
81 videos|80 docs|33 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

shortcuts and tricks

,

Free

,

pdf

,

MCQs

,

mock tests for examination

,

Previous Year Questions with Solutions

,

Extra Questions

,

ppt

,

Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)

,

Sample Paper

,

Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)

,

Semester Notes

,

study material

,

Viva Questions

,

Summary

,

Important questions

,

Objective type Questions

,

practice quizzes

,

past year papers

,

Matrix Chain Multiplication | Algorithms - Computer Science Engineering (CSE)

,

Exam

,

video lectures

;