Python Program for Matrix Chain Multiplication | DP-8
Last Updated :
20 Apr, 2023
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 40x20, 20x30, 30x10 and 10x30.
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 10x20, 20x30, 30x40 and 40x30.
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 10x20 and 20x30. So there
is only one way to multiply the matrices, cost of which is 10*20*30
Following is a recursive implementation that simply follows the above optimal substructure property.
Python3
import sys
def MatrixChainOrder(p, i, j):
if i = = j:
return 0
_min = sys.maxsize
for k in range (i, j):
count = (MatrixChainOrder(p, i, k)
+ MatrixChainOrder(p, k + 1 , j)
+ p[i - 1 ] * p[k] * p[j])
if count < _min:
_min = count;
return _min;
arr = [ 1 , 2 , 3 , 4 , 3 ];
n = len (arr);
print ( "Minimum number of multiplications is " ,
MatrixChainOrder(arr, 1 , n - 1 ));
|
Output
Minimum number of multiplications is 30
Time complexity: The time complexity of the algorithm is O(n^3) where n is the number of matrices in the chain. This is because we have three nested loops and each loop runs n times.
Space complexity: The space complexity of the algorithm is O(n^2) because we are using a two-dimensional array to store the intermediate results. The size of the array is n x n.
Dynamic Programming Solution
Python
import sys
def MatrixChainOrder(p, n):
m = [[ 0 for x in range (n)] for x in range (n)]
for i in range ( 1 , n):
m[i][i] = 0
for L in range ( 2 , n):
for i in range ( 1 , n - L + 1 ):
j = i + L - 1
m[i][j] = sys.maxsize
for k in range (i, j):
q = m[i][k] + m[k + 1 ][j] + p[i - 1 ] * p[k] * p[j]
if q < m[i][j]:
m[i][j] = q
return m[ 1 ][n - 1 ]
arr = [ 1 , 2 , 3 , 4 , 3 ]
size = len (arr)
print ( "Minimum number of multiplications is " +
str (MatrixChainOrder(arr, size)))
|
Output
Minimum number of multiplications is 30
Please refer complete article on Matrix Chain Multiplication | DP-8 for more details!
Share your thoughts in the comments
Please Login to comment...