Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

20CS2018L – Design and Analysis of URK21CS7006

Algorithms Lab

Ex. No. 5
Dynamic programming technique to find optimal ordering of matrix
multiplication

Aim
To write a program for finding the optimal ordering of matrix multiplication.
Description
Matrix chain multiplication, also known as matrix chain order problem, is an optimization
problem that seeks to determine the most efficient way to multiply a chain of matrices. The
problem involves finding the optimal order of matrix multiplications in order to minimize the
number of scalar multiplications required to compute the product.

Given a sequence of matrices A1, A2, A3, ..., An, the problem is to find the optimal
parenthesization of the product of matrices A1 * A2 * A3 * ... * An such that the number of
scalar multiplications required to compute the product is minimized.

This problem is important because matrix multiplication is a fundamental operation in computer


science and engineering and it is used in many applications such as computer graphics, scientific
computing, linear algebra, and many others.

Matrix chain multiplication can be solved using dynamic programming by computing the
minimum number of scalar multiplications required to compute the product of submatrices and
using this information to compute the minimum number of multiplications required for the entire
chain of matrices.

The time complexity of the matrix chain multiplication problem is O(n^3), where n is the
number of matrices in the chain.

Algorithm
for l -> (2,n):
for I -> (1,n-l+1):

Ex.5 Dynamic programming technique to find optimal ordering of matrix multiplication 1


20CS2018L – Design and Analysis of URK21CS7006
Algorithms Lab

j = i+l-1
m[i][j] = sys.maxsize
for k ->(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]
Program
import sys
p = [5,4,3,7,2]
n = len(p)

def MatrixChainOrder(p):
global name
n = len(p)
m = [[0 for x in range(n)] for x in range(n)]
s = [[0 for x in range(n)] for x in range(n)]

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

Ex.5 Dynamic programming technique to find optimal ordering of matrix multiplication 2


20CS2018L – Design and Analysis of URK21CS7006
Algorithms Lab

q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]

if q < m[i][j]:
m[i][j]=q
s[i][j]=k
name = "A"
print(m[1][n-1])
return s

s = MatrixChainOrder(p)

def brackets(s,start,end):
global name
if(start==end):
print(name, end = "");
name = chr(ord(name) + 1)
return;

else:
k=s[start][end]
print('(',end="")
brackets(s,start,k)
brackets(s,k+1,end)
print(')',end="")

Ex.5 Dynamic programming technique to find optimal ordering of matrix multiplication 3


20CS2018L – Design and Analysis of URK21CS7006
Algorithms Lab

brackets(s,1,n-1)
Input & Output:

Result:
The program is successfully executed and the result is shown as output.

Ex.5 Dynamic programming technique to find optimal ordering of matrix multiplication 4

You might also like