1. Gyanastra
  2. Algorithms
  3. Matrix Chain Multiplication
  4. Matrix Chain Multiplication: Cost of Multiplying Two Matrices
Free lesson, no account needed 5 min read 11 practice questions after it
Asked in past papers5 times
  • GATE CSE2018201620112004
  • UGC NET2014

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, P×Q×RP \times Q \times R, 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 AA has dimension m×nm \times n and BB has dimension n×pn \times p, the product matrix CC has dimension m×pm \times p.
  • 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 P×QP \times Q matrix with a Q×RQ \times R matrix costs P×Q×RP \times Q \times R scalar multiplications. This is the convention used throughout the Matrix Chain Multiplication problem.
  • Order matters: For three matrices AA, BB, CC, 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

  1. Check compatibility: The columns of the first matrix must equal the rows of the second matrix; otherwise multiplication is impossible.
  2. Determine result size: The result has (rows of the first matrix) × (columns of the second matrix) elements.
  3. Count multiplications for one element: To compute an element such as C11C_{11}, multiply the first row of AA element-by-element with the first column of BB and add the products.
    The number of multiplications per element equals the number of columns of AA (= number of rows of BB).
  4. 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: (m×n)×(n×p)→m×p(m \times n) \times (n \times p) \rightarrow m \times p
  • Total scalar multiplications for two matrices: P×Q×RP \times Q \times R, where the first matrix is P×QP \times Q and the second is Q×RQ \times R
    • PP = rows of first matrix
    • QQ = columns of first matrix = rows of second matrix
    • RR = columns of second matrix

Worked examples

Example: A (3×2)×B (2×3)A\ (3 \times 2) \times B\ (2 \times 3)

AA has 3 rows and 2 columns:

  • Row 1: A11,A12A_{11}, A_{12}
  • Row 2: A21,A22A_{21}, A_{22}
  • Row 3: A31,A32A_{31}, A_{32}

BB has 2 rows and 3 columns:

  • Row 1: B11,B12,B13B_{11}, B_{12}, B_{13}
  • Row 2: B21,B22,B23B_{21}, B_{22}, B_{23}
StepActionResult
1Check compatibility: columns of AA = 2, rows of BB = 2Compatible
2Determine result size: rows of AA × columns of BBCC is 3×33 \times 3, i.e. 9 elements
3Compute C11C_{11}: first row of AA times first column of BB, then add2 scalar multiplications
4Total cost: 9 elements × 2 multiplications each3×3×2=183 \times 3 \times 2 = 18 scalar multiplications

General case: A (P×R)×B (R×Q)A\ (P \times R) \times B\ (R \times Q)

  1. Result has P×QP \times Q elements.
  2. Each element needs RR multiplications.
  3. Total =P×Q×R= P \times Q \times R.

Revision summary

  • Two matrices multiply only if the inner dimensions match: columns of the first = rows of the second.
  • The product of (m×n)(m \times n) and (n×p)(n \times p) is m×pm \times p.
  • Cost is measured in scalar (element-to-element) multiplications, not matrix-to-matrix operations.
  • Memorise the convention: a P×QP \times Q matrix times a Q×RQ \times R matrix costs P×Q×RP \times Q \times R.
  • 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 O(n3)O(n^{3}) 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 O(n2)O(n^{2}) 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 O(n3)O(n^{3}), 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, O(nlog⁡n)O(n \log n)) and all-pairs shortest paths (Floyd-Warshall, O(n3)O(n^{3})).
    Asked inUGC NET 2014
Pages 1–2 of 2