0% found this document useful (0 votes)
10 views3 pages

Ada 4

The document describes an algorithm and implementation for matrix chain multiplication using dynamic programming. It involves counting the number of ways matrices can be multiplied, devising an optimal substructure by dividing matrices into optimal parts, and using a formula and cost tables to find the lowest cost parenthesization. The program implements this using helper functions to calculate cost and parenthesization tables in O(N3) time complexity, where N is the number of matrices. It takes user input for matrix dimensions and outputs the number of scalar multiplications and optimal parenthesization.

Uploaded by

vipul jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Ada 4

The document describes an algorithm and implementation for matrix chain multiplication using dynamic programming. It involves counting the number of ways matrices can be multiplied, devising an optimal substructure by dividing matrices into optimal parts, and using a formula and cost tables to find the lowest cost parenthesization. The program implements this using helper functions to calculate cost and parenthesization tables in O(N3) time complexity, where N is the number of matrices. It takes user input for matrix dimensions and outputs the number of scalar multiplications and optimal parenthesization.

Uploaded by

vipul jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical-5

Aim:- Algorithm and Implementation of Matrix Chain


Multiplication(using dynamic programming).
Algorithm:-
 Count the number of parenthesizations. Find the number of ways in which the
input matrices can be multiplied using the formulae −
P(n)={1 if n=1

∑n-1k=1P(k)P(n−k) if n≥2
 Once the parenthesization is done, the optimal substructure must be devised as
the first step of dynamic programming approach so the final product achieved
is optimal. In matrix chain multiplication, the optimal substructure is found by
dividing the sequence of matrices A[i….j] into two parts A[i,k] and A[k+1,j]. It
must be ensured that the parts are divided in such a way that optimal solution is
achieved.
 Using the formula

C[i, j]={0 if i=j

Min i≤k<j{C[i, k]+C[k+1,j]+di−1dkdj if i<j


 find the lowest cost parenthesization of the sequence of matrices by
constructing cost tables and corresponding k values table.
 Once the lowest cost is found, print the corresponding parenthesization as the
output.

Program:-
def matrix_product(p):
length = len(p) # len(p) = number of matrices + 1
m = [[-1]*length for _ in range(length)]
s = [[-1]*length for _ in range(length)]
 
matrix_product_helper(p, 1, length - 1, m, s)
 
return m, s
 
 
def matrix_product_helper(p, start, end, m, s):
if m[start][end] >= 0:
return m[start][end]
 
if start == end:
q = 0
else:
q = float('inf')
for k in range(start, end):
temp = matrix_product_helper(p, start, k, m, s) \
+ matrix_product_helper(p, k + 1, end, m, s) \
+ p[start - 1]*p[k]*p[end]
if q > temp:
q = temp
s[start][end] = k
 
m[start][end] = q
return q
 
 
def print_parenthesization(s, start, end):
if start == end:
print('A[{}]'.format(start), end='')
return
 
k = s[start][end]
 
print('(', end='')
print_parenthesization(s, start, k)
print_parenthesization(s, k + 1, end)
print(')', end='')
 
 
n = int(input('Enter number of matrices: '))
p = []
for i in range(n):
temp = int(input('Enter number of rows in matrix {}: '.format(i + 1)))
p.append(temp)
temp = int(input('Enter number of columns in matrix {}: '.format(n)))
p.append(temp)
 
m, s = matrix_product(p)
print('The number of scalar multiplications needed:', m[1][n])
print('Optimal parenthesization: ', end='')
print_parenthesization(s, 1, n)

Output:-
Enter number of matrices: 3
Enter number of rows in matrix 1: 10
Enter number of rows in matrix 2: 100
Enter number of rows in matrix 3: 5
Enter number of columns in matrix 3: 50
The number of scalar multiplications needed: 7500
Optimal parenthesization: ((A[1]A[2])A[3])

Time Complexity:-
O(N3), Where ‘N’ is the size of the array.

You might also like