Algorithms · Matrix Chain Multiplication
Matrix Chain Multiplication: Parenthesization Order and Cost Comparison
Overview
Matrix Chain Multiplication is the problem of multiplying a chain of matrices and choosing the parenthesization (multiplication order) that minimizes the total number of scalar multiplications.
Because matrix multiplication is associative, every order yields the same final matrix, but the cost differs, so the order must be chosen carefully.
Key concepts
- Matrix Chain Multiplication: multiplying a chain of matrices and finding the order (parenthesization) of multiplication that minimizes the total number of scalar multiplications.
- Parenthesization: the order in which the matrices are multiplied — which pair is multiplied first, then combined with the next matrix.
- Associativity: matrix multiplication satisfies . The final product is the same regardless of order, but the number of scalar multiplications (the cost) differs.
- Cost of one multiplication: multiplying a matrix by a matrix costs scalar multiplications and produces a matrix.
- Total cost of a parenthesization: the sum of the costs of each individual matrix multiplication in that order.
- Optimal substructure: to apply dynamic programming, the first requirement is to identify the optimal substructure (splitting the problem into smaller optimization problems) and then write the recursive equations.
How it works
- A chain of matrices is given with their dimensions; a multiplication is only possible when the columns of the first matrix equal the rows of the second.
- Choose a parenthesization, i.e. decide which pair of matrices is multiplied first.
- Compute the cost of each individual multiplication as and sum them to get the total cost of that parenthesization.
- Compare the total costs of all parenthesizations and keep the one with the smallest number of scalar multiplications.
For three matrices there are exactly two parenthesizations:
- — multiply and first, then multiply the result with .
- — multiply and first, then multiply with the result.
Formulas
- Cost of multiplying a matrix by a matrix: scalar multiplications, where = rows of the first matrix, = columns of the first matrix (= rows of the second matrix), = columns of the second matrix.
- Total cost of a parenthesization: .
Worked examples
Matrices: is , is , is .
Order 1:
- Step 1: multiply () with (). The resulting matrix has dimensions ; scalar multiplications = .
- Step 2: multiply () with (). Scalar multiplications = ; the resulting matrix has dimensions .
- Total cost = multiplications.
Order 2:
- Step 1: multiply () with (). The resulting matrix has dimensions ; scalar multiplications = .
- Step 2: multiply () with (). The dimensions are compatible (); scalar multiplications = .
- Total cost = multiplications.
Order saves multiplications. With small dimensions the saving looks small, but with large dimensions (e.g. 200, 100, etc.) the saving becomes substantial and is not negligible.
Comparison
| Parenthesization | Step 1 cost | Step 2 cost | Total scalar multiplications |
|---|---|---|---|
| 20 | |||
| 16 |
Revision summary
- Associativity guarantees the same final matrix for every parenthesization; only the cost changes.
- Cost of one multiplication of a matrix with a matrix is ; the result is .
- For 3 matrices there are exactly 2 parenthesizations: and .
- In the example (: , : , : ): costs 20, costs 16 — the parenthesization order has a significant impact on cost.
- The goal is to keep the number of multiplications as small as possible; with larger dimensions the savings become substantial.
- Dynamic programming applies only after identifying the optimal substructure and writing the recursive equations.
Previous-year exam insights
- The number of distinct parenthesizations (ways to fully parenthesize a chain of n matrices) is the Catalan number C(n-1) = (1/n)·binom(2n-2, n-1);
for n=3 it is 2, n=4 it is 5.
Asked inGATE 2011 - DP recurrence: m[i][j] = min over i≤k<j of (m[i][k] + m[k+1][j] + ··), with m[i][i] = 0, where the chain dimensions are x , x , ..., x ;
the answer is m[1][n].
Asked inGATE 2016GATE 2011 - The DP fills the table by increasing chain length (diagonal order), so each subproblem m[i][j] is computed before it is needed; time complexity is and space complexity is .
Asked inGATE 2016 - For a chain of n matrices the DP considers sum over lengths of (n-L+1)(L-1) splits, giving subproblem-split pairs; a brute-force enumeration of all parenthesizations is exponential (Catalan), which is why DP is needed.
- Matrix chain multiplication exhibits optimal substructure: an optimal parenthesization of ... splits at some k, and the parenthesizations of ... and ... within it must themselves be optimal.
Asked inGATE 2011 - Cost accounting trap: when combining two intermediate results of sizes p x q and q x r the cost is p·q·r, and the resulting matrix is p x r — use the current intermediate dimensions, not the original matrix dimensions, at each step.
Asked inGATE 2011GATE 2016