Given matrices with dimensions , , …, , find the parenthesisation minimising the number of scalar multiplications.

Multiplying a by a matrix costs . Matrix multiplication is associative, so the result never changes — only the cost.

Why the order matters

For dimensions , , :

  • :
  • :

A 10× difference from parenthesisation alone.

The recurrence

Here is the cost of multiplying the matrices spanning dimension indices to .

long long matrixChain(vector<long long>& p) {
    int n = p.size() - 1;                            // number of matrices
    vector<vector<long long>> dp(n + 1, vector<long long>(n + 1, 0));
    for (int len = 2; len <= n; len++)               // by increasing length
        for (int i = 0; i + len <= n; i++) {
            int j = i + len;
            dp[i][j] = LLONG_MAX;
            for (int k = i + 1; k < j; k++)
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + p[i]*p[k]*p[j]);
        }
    return dp[0][n];
}

time, space. Iterating by length is essential — see Interval DP.

Reconstructing the parenthesisation

vector<vector<int>> split;                          // split[i][j] = best k
string build(int i, int j) {
    if (j == i + 1) return "A" + to_string(i + 1);
    return "(" + build(i, split[i][j]) + " x " + build(split[i][j], j) + ")";
}

Can it be optimised?

Knuth optimization does not apply

The cost does not satisfy the quadrangle inequality in general, so Knuth optimization is invalid here and matrix chain stays for the general DP. This is a common misconception — Knuth’s own result was for optimal binary search trees, whose cost function is Monge.

There is a specialised algorithm (Hu-Shing, 1982) that exploits the geometric structure of the problem, but it is intricate and essentially never needed: in contest versions of this problem is small.

The interval-DP family it belongs to

ProblemCost of merging and
Matrix chain
Merging stonessum of the range
Optimal BSTsum of the frequencies in the range
Polygon triangulationarea or perimeter of the triangle
Huffman (free order)greedy,
Hu-Tucker (fixed order)

Note the last two rows: when the leaves may be reordered, greedy Huffman solves it in ; when the order is fixed, you need Hu-Tucker or an Knuth-optimised DP. Matrix chain has a fixed order and a non-Monge cost, which is why it is the hard case.

Polygon triangulation — the same DP

Triangulating a convex polygon to minimise the total triangle weight is exactly this recurrence with being the weight of triangle . The dimension array corresponds to the polygon’s vertices, and each parenthesisation corresponds to a triangulation — a pleasing bijection, and the reason the number of parenthesisations is the Catalan number .

Where it actually appears

  • Optimising a chain of matrix multiplications in a linear algebra pipeline
  • Query planning in databases (join order optimisation is the same shape)
  • Tensor contraction ordering in machine learning frameworks
  • As the standard teaching example of interval DP

See also: Interval DP · Knuth Optimization · Catalan Numbers