A homework assignment in the school required that I write a program for this task:

*In the problem of multiplication of the matrix chain, we are given a sequence of*

matrices `A (1), A (2), ..., A (n)`

. The objective is to calculate the product.

`A (1) ..., A (n)`

with the minimum number of scalar multiplications.

Therefore, we have to find an optimal parenthesis of the matrix.

product `A (1) ..., A (n)`

such that the cost of computing the product is

minimized.

Here is my solution for this task (in Python):

```
def matrix_product (p):
""
Return m and s.
subway[i][j] is the minimum number of scalar multiplications needed to calculate the
product of matrices A (i), A (i + 1), ..., A (j).
s[i][j] is the index of the matrix after which the product is divided into a
Optimal parenthesis of the parent product.
P[0... n] is a list such that the matrix A (i) has dimensions p[i - 1] x p[i].
""
length = len (p) # len (p) = number of matrices + 1
# m[i][j] is the minimum number of multiplications needed to calculate the
# product of matrices A (i), A (i + 1), ..., A (j)
# s[i][j] It is the matrix after which the product is divided into the minimum.
# number of multiplications needed
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):
""
Return the minimum number of scalar multiplications needed to calculate the
product of matrices A (start), A (start + 1), ..., A (end).
The minimum number of scalar multiplications needed to calculate the
product of matrices A (i), A (i + 1), ..., A (j) is stored in m[i][j].
The index of the matrix after which the previous product is divided into an optimal
the parenthesis is stored in s[i][j].
P[0... n] is a list such that the matrix A (i) has dimensions p[i - 1] x p[i].
""
yes m[start][end] > = 0:
return m[start][end]
if start == end:
q = 0
plus:
q = float (& # 39; inf & # 39;)
for k in range (start, end):
temp = matrix_product_helper (p, start, k, m, s)
+ matrix_product_helper (p, k + 1, final, m, s)
+ p[start - 1]*P[k]*P[end]
if q> temp:
q = temperature
s[start][end] = k
subway[start][end] = q
come back
def print_parenthesization (s, start, end):
""
Print the optimal parenthesis of the matrix product A (start) x
A (start + 1) x ... x A (end).
s[i][j] is the index of the matrix after which the product is divided into a
Optimal parenthesis of the parent product.
""
if start == end:
print (& # 39; A[{}]& # 39; .format (start), end = & # 39; & # 39;)
he came back
k = s[start][end]
print (& # 39 ;, & end = & # 39; & # 39;)
print_parenthesization (s, start, k)
print_parenthesization (s, k + 1, final)
print (& # 39;) & # 39 ;, end = & # 39; & # 39;)
n = int (entry (& # 39; Enter the number of arrays: & # 39;))
p = []
for i in the range (n):
temp = int (entry (& # 39; Enter the number of rows in the array {}: & # 39; .format (i + 1)))
p.append (temp)
temp = int (entry (& # 39; Enter the number of columns in the array {}: & # 39; .format (n)))
p.append (temp)
m, s = matrix_product (p)
print (& # 39; The number of scalar multiplications needed: & # 39 ;, m[1][n])
print (& # 39; Optimum parenthesis: & # 39 ;, end = & # 39; & # 39;)
print_parenthesization (s, 1, n)
```

Here is an example of output:

```
Enter the number of matrices: 3
Enter the number of rows in the matrix 1: 10
Enter the number of rows in the matrix 2: 100
Enter the number of rows in the matrix 3: 5
Enter the number of columns in the matrix 3: 50
The number of scalar multiplications needed: 7500.
Optimum parenthesis: ((A[1]A[2])A[3])
```

NOTE – The time needed to `print_parenthesization ()`

(for this example) it is `0: 00: 15.332220`

seconds.

Therefore, I would like to know if I could make this program shorter and more efficient.

Any help would be greatly appreciated.