1. Gyanastra
  2. Algorithms
  3. Matrix Chain Multiplication
  4. Matrix Chain Multiplication: Parenthesization Order and Cost Comparison
Free lesson, no account needed 5 min read 12 practice questions after it
Asked in past papers5 times
  • GATE CSE201820162011
  • ISRO2020
  • UGC NET2014

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 A,B,C,…A, B, C, \dots 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 A,B,C,…A, B, C, \dots 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 (AB)C=A(BC)(AB)C = A(BC). The final product is the same regardless of order, but the number of scalar multiplications (the cost) differs.
  • Cost of one multiplication: multiplying a P×QP \times Q matrix by a Q×RQ \times R matrix costs P⋅Q⋅RP \cdot Q \cdot R scalar multiplications and produces a P×RP \times R 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

  1. 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.
  2. Choose a parenthesization, i.e. decide which pair of matrices is multiplied first.
  3. Compute the cost of each individual multiplication as P×Q×RP \times Q \times R and sum them to get the total cost of that parenthesization.
  4. 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:

  1. (AB)C(AB)C — multiply AA and BB first, then multiply the result with CC.
  2. A(BC)A(BC) — multiply BB and CC first, then multiply AA with the result.

Formulas

  • Cost of multiplying a P×QP \times Q matrix by a Q×RQ \times R matrix: P×Q×RP \times Q \times R scalar multiplications, where PP = rows of the first matrix, QQ = columns of the first matrix (= rows of the second matrix), RR = columns of the second matrix.
  • Total cost of a parenthesization: total cost=∑(cost of each individual matrix multiplication in that order)\text{total cost} = \sum \text{(cost of each individual matrix multiplication in that order)}.

Worked examples

Matrices: AA is 2×12 \times 1, BB is 1×21 \times 2, CC is 2×42 \times 4.

Order 1: (AB)C(AB)C

  • Step 1: multiply AA (2×12 \times 1) with BB (1×21 \times 2). The resulting matrix DD has dimensions 2×22 \times 2; scalar multiplications = 2×1×2=42 \times 1 \times 2 = 4.
  • Step 2: multiply DD (2×22 \times 2) with CC (2×42 \times 4). Scalar multiplications = 2×2×4=162 \times 2 \times 4 = 16; the resulting matrix EE has dimensions 2×42 \times 4.
  • Total cost = 4+16=204 + 16 = 20 multiplications.

Order 2: A(BC)A(BC)

  • Step 1: multiply BB (1×21 \times 2) with CC (2×42 \times 4). The resulting matrix DD has dimensions 1×41 \times 4; scalar multiplications = 1×2×4=81 \times 2 \times 4 = 8.
  • Step 2: multiply AA (2×12 \times 1) with DD (1×41 \times 4). The dimensions are compatible (1=11 = 1); scalar multiplications = 2×1×4=82 \times 1 \times 4 = 8.
  • Total cost = 8+8=168 + 8 = 16 multiplications.

Order A(BC)A(BC) saves 20−16=420 - 16 = 4 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

ParenthesizationStep 1 costStep 2 costTotal scalar multiplications
(AB)C(AB)C2×1×2=42 \times 1 \times 2 = 42×2×4=162 \times 2 \times 4 = 1620
A(BC)A(BC)1×2×4=81 \times 2 \times 4 = 82×1×4=82 \times 1 \times 4 = 816

Revision summary

  • Associativity guarantees the same final matrix for every parenthesization; only the cost changes.
  • Cost of one multiplication of a P×QP \times Q matrix with a Q×RQ \times R matrix is P×Q×RP \times Q \times R; the result is P×RP \times R.
  • For 3 matrices there are exactly 2 parenthesizations: (AB)C(AB)C and A(BC)A(BC).
  • In the example (AA: 2×12\times1, BB: 1×21\times2, CC: 2×42\times4): (AB)C(AB)C costs 20, A(BC)A(BC) 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] + pi−1p_{i-1}·pkp_{k}·pjp_{j}), with m[i][i] = 0, where the chain dimensions are p0p_{0} x p1p_{1}, p1p_{1} x p2p_{2}, ..., pn−1p_{n-1} x pnp_{n};
    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 O(n3)O(n^{3}) and space complexity is O(n2)O(n^{2}).
    Asked inGATE 2016
  • For a chain of n matrices the DP considers sum over lengths of (n-L+1)(L-1) splits, giving Θ(n3)\Theta(n^{3}) 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 AiA_{i}...AjA_{j} splits at some k, and the parenthesizations of AiA_{i}...AkA_{k} and Ak+1A_{k+1}...AjA_{j} 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
Pages 1–2 of 2