0% found this document useful (0 votes)
29 views2 pages

Matrix Chain Multiplication Algorithm

Uploaded by

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

Matrix Chain Multiplication Algorithm

Uploaded by

13 JAYALAKSHMI K
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Matrix Chain Multiplication Algorithm

• Matrix chain multiplication is an algorithm that is applied to determine the lowest cost way
for multiplying matrices.
• The actual multiplication is done using the standard way of multiplying the matrices, that is it
follows the basic rule that the number of rows in one matrix must be equal to the number of
columns in another matrix.
• Hence, multiple scalar multiplications must be done to achieve the product.
• Matrix chain multiplication algorithm is only applied to find the minimum cost way to
multiply a sequence of matrices. Therefore, the input taken by the algorithm is the sequence
of matrices while the output achieved is the lowest cost parenthesization.

Algorithm
• Count the number of parenthesization. Find the number of ways in which the input matrices
can be multiplied using the formulae −

P(n)= {∑ n-1 P(k)P(n-k) if n = 1

k=1 if n ≥ 2

(or)

P(n)= {2(n-1)Cn-1 if n ≥ 2
-------------- if n = 1
n

• 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]=

{min{c[i,k] + c[k+1,j] + di-1 dkdj


i≤k<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.
Pseudocode
Pseudocode to find the lowest cost of all the possible parenthesizations −

MATRIX-CHAIN-MULTIPLICATION(p)
n = [Link] ─ 1
let m[1…n, 1…n] and s[1…n ─ 1, 2…n] be new matrices
for i = 1 to n
m[i, i] = 0
for l = 2 to n // l is the chain length
for i = 1 to n - l + 1
j = i + l - 1
m[i, j] = ∞
for k = i to j - 1
q = m[i, k] + m[k + 1, j] + pi-1pkpj
if q < m[i, j]
m[i, j] = q
s[i, j] = k
return m and s

Output
PRINT-OPTIMAL-OUTPUT(s, i, j )
if i == j
print “A”i
else print “(”
PRINT-OPTIMAL-OUTPUT(s, i, s[i, j])
PRINT-OPTIMAL-OUTPUT(s, s[i, j] + 1, j)
print “)”

Common questions

Powered by AI

Scalar multiplications refer to the individual multiplication operations required to multiply matrices element-wise during matrix multiplication. In the matrix chain multiplication algorithm, planning revolves around minimizing these scalar multiplications because they directly affect the computational cost. The goal is to find a sequence of matrix multiplications that achieves the desired result with the fewest scalar operations, which is reflected in determining the optimal parenthesization with minimum multiplication cost .

The matrix chain multiplication algorithm determines the optimal parenthesization by first calculating the cost of multiplying each possible sequence of matrices. It uses dynamic programming to break the sequence into two parts, A[i,k] and A[k+1,j], and calculates the minimal cost for all possible k values between i and j. The optimal substructure ensures that these parts are divided to minimize the number of scalar multiplications. The algorithm maintains a cost table (m[i,j]) and a k-values table (s[i,j]) to track the minimum cost and optimal parenthesization. The process continues until the optimal parenthesization providing the lowest multiplication cost is found .

In the algorithm, the matrix m[i,j] stores the minimum scalar multiplication cost required to multiply the matrices from Ai to Aj. The matrix s[i,j] stores the index k at which the optimal division occurs for these matrices. The values in these matrices help reconstruct the sequence of multiplications corresponding to the minimum cost solution, guiding the optimal parenthesization .

Printing the optimal parenthesization output is significant because it demonstrates the specific order in which matrices should be multiplied to achieve the minimum cost. This information is crucial for implementing the matrix multiplication efficiently as it determines the grouping and sequence of operations required to minimize computational effort .

The formula used to calculate the number of possible parenthesizations is P(n) = ∑ P(k)P(n-k) for n ≥ 2, or alternatively, P(n) = 2(n-1)Cn-1 for n ≥ 2. This formula signifies the number of ways matrices can be parenthesized for multiplication, providing insight into the complexity and the number of options available to find the optimal multiplication order .

Dynamic programming plays a crucial role by storing the results of subproblems to avoid redundant computations. It utilizes the property of overlapping subproblems by calculating the minimum cost of multiplying sub-chains only once and using these results to build up solutions to larger problems. This significantly reduces the computational complexity compared to a naïve recursive approach, which would involve recalculating the costs for the same subproblems multiple times .

The time complexity of the matrix chain multiplication algorithm is O(n^3), where n is the number of matrices to be multiplied. This complexity arises from the triple nested loops in the pseudocode: one for chain length, one for the starting matrix of a chain, and one for division indices within the chain. Factors influencing this complexity include the size and number of input matrices, as each pair of subproblems must be calculated to ensure every possible sequence and partitioning of matrices is considered for the minimum cost .

The initial value of m[i,j] is set to infinity to ensure that any calculated multiplication cost during the algorithm will be lower, facilitating the correct identification of minimum multiplication costs as the algorithm processes. This practice eliminates any initial bias or default value that might erroneously be interpreted as a valid minimum cost .

The optimal substructure property is important because it allows the problem to be broken down into simpler subproblems, each of which provides optimal solutions. In matrix chain multiplication, dividing the sequence of matrices into subchains and finding the minimum multiplication cost for these subchains directly contributes to finding the optimal cost for multiplying the entire sequence. This property is essential for applying dynamic programming since it ensures that solving the smaller subproblems leads to the overall best solution .

The pseudocode structure involves initializing matrices m and s for storing costs and k-values. The chain length l is increased from 2 to n. For each possible subchain determined by l, variables i and j denote the starting and ending matrices, and m[i, j] is initially set to infinity. For each k, representing a partition between matrices, q computes the total cost using the formula q = m[i, k] + m[k + 1, j] + pi-1pkpj. If q is less than the current value m[i, j], update m[i, j] with q and store k in s[i, j]. This proceeds iteratively until the minimum cost is found .

You might also like