
Length = len (p ) # len(p) = number of matrices + 1 # m is the minimum number of multiplications needed to compute the # product of matrices A(i), A(i+1). P is a list such that matrix A(i) has dimensions p x p. Optimal parenthesization of the matrix product. S is the index of the matrix after which the product is split in an M is the minimum number of scalar multiplications needed to compute the It prints the optimal parenthesization of the matrix-chain product A(start) x … x A(end). It also takes two indexes start and end as arguments.ġ5. The function print_parenthesization takes as argument a 2D table s as generated above.ġ4. The above step is performed for each chain_length in starting with chain_length = 2.ġ3. The function then computes m for each value of i using the above formula.ġ1. The function first sets m = 0 for 1 <= i <= n where n is the number of matrices.ġ0. The last term is the cost of multiplying the two products formed by splitting the matrix-chain after matrix k.ĩ. This is done by finding a k such that m + m + p*p*p is minimized. The function finds the minimum computations needed to evaluate A(start) x … x A(end) and stores it in m.Ĩ. The index of the matrix after which the above product is split in an optimal parenthesization is stored in s.ħ.

The function stores the minimum number of scalar multiplications needed to compute the product A(i) x A(i + 1) x … x A(j) in m.Ħ. It creates two 2D tables m and s as a list of lists.ĥ. p is a list such that matrix A(i) has dimensions p x p.Ĥ. The function matrix_product takes a list p as argument.ģ. Two functions are defined, matrix_product and print_parenthesization.Ģ.

We select the minimum from above and fill it in the table.1.

The matrix multiplication is associative, thus we have various ways to multiply. Matrix chain multiplication in C++ is an interesting problem. We don’t need to find the multiplication result but the order of matrices in which they need to be multiplied. Given a sequence of matrixes, we have to decide the order of multiplication of matrices which would require the minimum cost.
