Algorithms · Matrix Chain Multiplication
Matrix Chain Multiplication: Cost of Multiplying Two Matrices
Overview
Matrix chain multiplication starts from the cost of multiplying two matrices.
Two matrices can be multiplied only when their inner dimensions match, and the cost of that product is counted in scalar (element-to-element) multiplications.
This cost convention, , is the basis for the whole Matrix Chain Multiplication problem, where the order of multiplying a chain of matrices changes the total cost.
Key concepts
- Compatibility condition: Two matrices can be multiplied only if the number of columns in the first matrix equals the number of rows in the second matrix.
- Size of the result: If has dimension and has dimension , the product matrix has dimension .
- Scalar multiplication: The cost of matrix multiplication is counted in scalar multiplications — element-to-element multiplications, not matrix-to-matrix operations.
- Cost convention: Multiplying a matrix with a matrix costs scalar multiplications. This is the convention used throughout the Matrix Chain Multiplication problem.
- Order matters: For three matrices , , , all must be compatible, and the order in which pairs are multiplied affects the total cost. This motivates the chain multiplication problem.
How it works
- Check compatibility: The columns of the first matrix must equal the rows of the second matrix; otherwise multiplication is impossible.
- Determine result size: The result has (rows of the first matrix) × (columns of the second matrix) elements.
- Count multiplications for one element: To compute an element such as , multiply the first row of element-by-element with the first column of and add the products.
The number of multiplications per element equals the number of columns of (= number of rows of ). - Total cost: Multiply the number of elements in the result by the multiplications needed per element.
Formulas
- Compatibility condition: columns of first matrix = rows of second matrix
- Result dimension:
- Total scalar multiplications for two matrices: , where the first matrix is and the second is
- = rows of first matrix
- = columns of first matrix = rows of second matrix
- = columns of second matrix
Worked examples
Example:
has 3 rows and 2 columns:
- Row 1:
- Row 2:
- Row 3:
has 2 rows and 3 columns:
- Row 1:
- Row 2:
| Step | Action | Result |
|---|---|---|
| 1 | Check compatibility: columns of = 2, rows of = 2 | Compatible |
| 2 | Determine result size: rows of × columns of | is , i.e. 9 elements |
| 3 | Compute : first row of times first column of , then add | 2 scalar multiplications |
| 4 | Total cost: 9 elements × 2 multiplications each | scalar multiplications |
General case:
- Result has elements.
- Each element needs multiplications.
- Total .
Revision summary
- Two matrices multiply only if the inner dimensions match: columns of the first = rows of the second.
- The product of and is .
- Cost is measured in scalar (element-to-element) multiplications, not matrix-to-matrix operations.
- Memorise the convention: a matrix times a matrix costs .
- For three or more matrices, compatibility must hold across the whole chain, and the multiplication order matters for total cost.
Previous-year exam insights
- Matrix Chain Multiplication is solved by dynamic programming in time using a table m[i][j] = minimum cost to multiply matrices i..j, with recurrence m[i][j] = min over i≤k<j of (m[i][k] + m[k+1][j] + d(i-1)*d(k)*d(j)) for a chain with dimension array d[0..n];
the table has entries.
Asked inUGC NET 2014 - The total cost of any parenthesization of a chain is the sum of the costs of the individual pairwise products; different parenthesizations give different totals, so the minimum must be found by trying all splits (Catalan number of parenthesizations, exponential if done naively).
Asked inGATE 2018GATE 2011 - For a chain of n matrices, the number of distinct ways to fully parenthesize the product is the (n-1)th Catalan number C(n-1) = (1/n) * binom(2n-2, n-1).
Asked inGATE 2018 - The asymptotic time complexity of the standard matrix multiplication of two n x n matrices (basic triple-loop method) is , independent of whether the arrays are stored in row-major or column-major order; storage order only affects constant factors/cache behaviour, not the asymptotic complexity.
Asked inGATE 2004 - In a valid chain A1 A2 ... An with dimensions d0 x d1, d1 x d2, ..., d(n-1) x dn, the final product has dimension d0 x dn, and every intermediate product Ai...Aj has dimension d(i-1) x dj.
Asked inGATE 2016GATE 2011 - Matrix chain multiplication is a classic example of dynamic programming (optimal substructure + overlapping subproblems), distinct from greedy algorithms; it is often contrasted with Huffman coding (greedy, ) and all-pairs shortest paths (Floyd-Warshall, ).
Asked inUGC NET 2014