How matrix-chain order minimizes scalar multiplications

Short answer: Matrix-chain multiplication chooses where to parenthesize a product of matrices so the number of scalar multiplications is minimized. Matrix i has dimensions p[i−1] by p[i]. The cost of multiplying a chain from i through j is the minimum, over the last multiplication’s split k, of the two subchain costs plus p[i−1] · p[k] · p[j]. The table fills by increasing chain length in O(n³) time. The split positions reconstruct one optimal parenthesization.


What the problem actually asks

Matrix multiplication is associative and not commutative. Different parenthesizations multiply the same matrices in the same order and do different amounts of arithmetic, because the intermediate dimensions differ. Trying every parenthesization is exponential. A subchain’s optimal cost does not depend on how the matrices outside it are grouped, so the subchain costs form a DP.

The split k is the last multiplication, not an arbitrary binary-tree shape stored separately. Every binary parenthesization has a root split, so the minimum over k covers them all.


What the algorithm guarantees

The cell for the full chain is the minimum number of scalar multiplications. Time is Θ(n³) and memory is Θ(n²), one cost and one split per subchain. The dimensions array has length n+1 for n matrices. An off-by-one in p is the usual bug and produces a cost for the wrong inner dimensions. The algorithm does not reorder matrices. A[i] must still appear immediately left of A[i+1].

Knuth’s optimization speeds some related DP tables, including optimal binary search trees, and does not change this recurrence’s O(n³) shape in the basic form. The chain DP is also the template for other interval DPs: burst balloons, minimum cost to cut a stick, and similar problems that split an interval.


The working steps

  1. Read dimensions so matrix i has p[i−1] rows and p[i] columns. Reject a chain whose adjacent dimensions disagree. Those matrices cannot be multiplied at all.
  2. Set the cost of every chain of length 1 to 0. A single matrix needs no multiplication.
  3. For length from 2 to n, for every start i, let j = i+length−1. Try every split k from i to j−1 and keep the minimum of cost(i, k) + cost(k+1, j) + p[i−1]·p[k]·p[j]. Store the winning k.
  4. The answer is cost(1, n), or cost(0, n−1), in whatever index convention matches p.
  5. Reconstruct by printing a parenthesization that recurses on the stored split.

The cubic bound is the number of (start, end, split) triples. It does not multiply the matrices. It only counts the scalar multiplications a later multiplication would perform. Multiplying in the chosen order is ordinary matrix arithmetic after the splits are known. Reordering the chain is illegal unless the problem explicitly allows it, which this one does not.


Approaches that look simpler and fail

Approach Why it fails when the added term uses the wrong dimensions
Charge p[i] · p[k] · p[j] for a split The left factor of the final multiply has p[i−1] rows, not p[i] rows. The off-by-one computes a different, incorrect cost.
Minimize over permutations of the matrices The product is ordered. Only parenthesization changes. A permutation is a different product.
Fill the table in order of the left endpoint only A long chain reads shorter chains. Filling by increasing length, or any order that computes smaller intervals first, is required.
Return the split and skip the two subchain costs The final multiply is only the root. The subchains have their own internal multiplications, already stored in the table.

A related interval DP with a different cost is the optimal binary search tree. The arithmetic after the order is chosen is plain matrix multiplication, including Strassen’s algorithm if the goal is to multiply two large matrices faster than the classical product.


A working example

This Python fragment chooses the split that minimizes scalar multiplications. Dimensions are 10×30, 30×5, 5×60. It prints 4500.

dims = [10, 30, 5, 60]
n = len(dims) - 1
dp = [[0] * (n + 1) for _ in range(n + 1)]
for length in range(2, n + 1):
    for i in range(1, n - length + 2):
        j = i + length - 1
        dp[i][j] = min(
            dp[i][k] + dp[k + 1][j] + dims[i - 1] * dims[k] * dims[j]
            for k in range(i, j)
        )
assert dp[1][n] == 4500
print(dp[1][n])

Where it is used

Matrix-chain order is the parenthesization a numerical library or a query planner uses before multiplying several matrices. The dynamic program counts scalar multiplications and does not multiply the matrices itself. Strassen changes the cost of each product, not the parenthesization table.


When you can drop this algorithm

Drop the DP when there are two matrices. There is nothing to parenthesize. Keep it when a chain of three or more compatible matrices must be multiplied and the parenthesization, not the matrices’ contents, is the decision.


Why this stays published

We publish this so a search for matrix-chain order returns the split cost p[i−1] · p[k] · p[j] and the O(n³) interval DP. Matrix-chain order minimizes scalar multiplications by a cubic interval DP whose split cost is the product of the three dimensions at the final multiply.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *