Matrix Chain Multiplication with Dynamic Programming: Optimal Parenthesization for {4,10,3,12,20,7}\{4,10,3,12,20,7\}

Matrix Chain Multiplication with Dynamic Programming: Optimal Parenthesization for {4,10,3,12,20,7}\{4,10,3,12,20,7\}

Verified Sources
May 25, 2026

We are given a matrix chain with dimension array p={4,10,3,12,20,7}p=\{4,10,3,12,20,7\}. This means the matrices are2:

  • A1:4×10A_1: 4 \times 10
  • A2:10×3A_2: 10 \times 3
  • A3:3×12A_3: 3 \times 12
  • A4:12×20A_4: 12 \times 20
  • A5:20×7A_5: 20 \times 7

The goal of matrix chain multiplication is not to change the final product, since matrix multiplication is associative, but to minimize the total number of scalar multiplications required.2

If a matrix of size a×ba \times b is multiplied by a matrix of size b×cb \times c, then the cost is:

abca \cdot b \cdot c

Dynamic programming is used because the problem has optimal substructure and overlapping subproblems.2

We define:

m[i,j]=minimum cost of multiplying AiAi+1Ajm[i,j] = \text{minimum cost of multiplying } A_iA_{i+1}\cdots A_j

with recurrence2:

m[i,j]=minik<j(m[i,k]+m[k+1,j]+pi1pkpj)m[i,j] = \min_{i \le k < j}\Big(m[i,k] + m[k+1,j] + p_{i-1}p_kp_j\Big)

Base case:

m[i,i]=0m[i,i] = 0

The dynamic programming table-filling algorithm runs in O(n3)O(n^3) time and uses O(n2)O(n^2) space, where nn is the number of matrices.2

Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity.
Matrix chain multiplication - Wikipedia - Overview of the optimization problem and why parenthesization matters.
Matrix Chain Multiplication - GeeksforGeeks - Summary of recurrence, DP table method, and O(n3)O(n^3) complexity.

Footnotes

  1. Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity. 2 3 4 5

  2. Matrix chain multiplication - Wikipedia - Overview of the problem, associativity, and Catalan-number growth of parenthesizations. 2 3

  3. Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds. 2 3 4 5

Matrix Chain Multiplication - Dynamic Programming

Key Observation

The dimensions determine the multiplication cost, not the values inside the matrices. Different valid parenthesizations can have very different costs.2

Footnotes

  1. Matrix chain multiplication - Wikipedia - Overview of the problem, associativity, and Catalan-number growth of parenthesizations.

  2. Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds.

Let us encode the matrices more formally using the dimension array p={4,10,3,12,20,7}p=\{4,10,3,12,20,7\}:

MatrixSize
A1A_14×104 \times 10
A2A_210×310 \times 3
A3A_33×123 \times 12
A4A_412×2012 \times 20
A5A_520×720 \times 7

Thus there are n=5n=5 matrices. A brute-force strategy would examine all full parenthesizations, whose count grows according to the Catalan number sequence and is exponential in effect for this problem size growth. Dynamic programming avoids recomputing the same subchains by filling tables diagonally by chain length.2

We maintain two tables:

  • m[i,j]m[i,j]: minimum multiplication cost for AiAjA_i \cdots A_j
  • s[i,j]s[i,j]: the best split index kk where the optimal split occurs

For this instance, we will compute all chain lengths from 22 to 55.

Footnotes

  1. Matrix chain multiplication - Wikipedia - Overview of the problem, associativity, and Catalan-number growth of parenthesizations.

  2. Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity. 2

  3. Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds.

Dynamic Programming Solution: Fill the Cost Table

  1. 1
    Step 1

    Set all diagonal entries to zero because multiplying one matrix requires no multiplication: m[i,i]=0m[i,i]=0 for 1i51 \le i \le 5.2

    Footnotes

    1. Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity.

    2. Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds.

  2. 2
    Step 2

    For each adjacent pair, compute one direct multiplication cost using pi1pipi+1p_{i-1}p_ip_{i+1}. This gives: m[1,2]=4103=120m[1,2]=4\cdot10\cdot3=120, m[2,3]=10312=360m[2,3]=10\cdot3\cdot12=360, m[3,4]=31220=720m[3,4]=3\cdot12\cdot20=720, and m[4,5]=12207=1680m[4,5]=12\cdot20\cdot7=1680.

  3. 3
    Step 3

    For each subchain of 3 matrices, try both possible split points and keep the minimum. For example, for m[1,3]m[1,3], compare splitting after A1A_1 versus after A2A_2.

  4. 4
    Step 4

    For each subchain of 4 matrices, try all 3 split points. Use already-computed smaller subproblems from the table, which is the core dynamic programming idea.

    Footnotes

    1. Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity.

  5. 5
    Step 5

    For m[1,5]m[1,5], try splits at k=1,2,3,4k=1,2,3,4 and choose the smallest total. The corresponding split locations recorded in s[i,j]s[i,j] let us reconstruct the optimal parenthesization.

  6. 6
    Step 6

    Starting from s[1,5]s[1,5], recursively split the chain into left and right optimal subchains until single matrices remain. This prints the optimal parenthesization.

    Footnotes

    1. Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity.

1. Base cases

Since a single matrix does not need multiplication:

m[1,1]=m[2,2]=m[3,3]=m[4,4]=m[5,5]=0m[1,1]=m[2,2]=m[3,3]=m[4,4]=m[5,5]=0

2. Chains of length 22

For two matrices there is only one way to multiply.

m[1,2]=4103=120m[1,2] = 4\cdot10\cdot3 = 120 m[2,3]=10312=360m[2,3] = 10\cdot3\cdot12 = 360 m[3,4]=31220=720m[3,4] = 3\cdot12\cdot20 = 720 m[4,5]=12207=1680m[4,5] = 12\cdot20\cdot7 = 1680

3. Chains of length 33

For m[1,3]m[1,3]:

  • Split at k=1k=1: 0+360+41012=8400 + 360 + 4\cdot10\cdot12 = 840
  • Split at k=2k=2: 120+0+4312=264120 + 0 + 4\cdot3\cdot12 = 264

So,

m[1,3]=264,s[1,3]=2m[1,3]=264,\quad s[1,3]=2

For m[2,4]m[2,4]:

  • Split at k=2k=2: 0+720+10320=13200 + 720 + 10\cdot3\cdot20 = 1320
  • Split at k=3k=3: 360+0+101220=2760360 + 0 + 10\cdot12\cdot20 = 2760

So,

m[2,4]=1320,s[2,4]=2m[2,4]=1320,\quad s[2,4]=2

For m[3,5]m[3,5]:

  • Split at k=3k=3: 0+1680+3127=19320 + 1680 + 3\cdot12\cdot7 = 1932
  • Split at k=4k=4: 720+0+3207=1140720 + 0 + 3\cdot20\cdot7 = 1140

So,

m[3,5]=1140,s[3,5]=4m[3,5]=1140,\quad s[3,5]=4

All of these computations directly apply the standard recurrence for matrix-chain dynamic programming.2

Footnotes

  1. Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity.

  2. Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds.

How to Read the Recurrence

The term m[i,k]+m[k+1,j]m[i,k] + m[k+1,j] is the best cost of solving the two subchains, and pi1pkpjp_{i-1}p_kp_j is the cost of multiplying the two resulting matrices together.

Footnotes

  1. Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity.

4. Chains of length 44

For m[1,4]m[1,4], try k=1,2,3k=1,2,3:

  • k=1k=1: 0+1320+41020=21200 + 1320 + 4\cdot10\cdot20 = 2120
  • k=2k=2: 120+720+4320=1080120 + 720 + 4\cdot3\cdot20 = 1080
  • k=3k=3: 264+0+41220=1224264 + 0 + 4\cdot12\cdot20 = 1224

Therefore,

m[1,4]=1080,s[1,4]=2m[1,4]=1080,\quad s[1,4]=2

For m[2,5]m[2,5], try k=2,3,4k=2,3,4:

  • k=2k=2: 0+1140+1037=13500 + 1140 + 10\cdot3\cdot7 = 1350
  • k=3k=3: 360+1680+10127=2880360 + 1680 + 10\cdot12\cdot7 = 2880
  • k=4k=4: 1320+0+10207=27201320 + 0 + 10\cdot20\cdot7 = 2720

Therefore,

m[2,5]=1350,s[2,5]=2m[2,5]=1350,\quad s[2,5]=2

5. Full chain of length 55

Now compute m[1,5]m[1,5] by trying all splits k=1,2,3,4k=1,2,3,4:

  • k=1k=1: 0+1350+4107=16300 + 1350 + 4\cdot10\cdot7 = 1630
  • k=2k=2: 120+1140+437=1344120 + 1140 + 4\cdot3\cdot7 = 1344
  • k=3k=3: 264+1680+4127=2280264 + 1680 + 4\cdot12\cdot7 = 2280
  • k=4k=4: 1080+0+4207=16401080 + 0 + 4\cdot20\cdot7 = 1640

Hence the optimal cost is:

m[1,5]=1344m[1,5]=1344

and the best top-level split is:

s[1,5]=2s[1,5]=2

This means the chain is optimally split as:

(A1A2)(A3A4A5)(A_1A_2)(A_3A_4A_5)

Then from s[3,5]=4s[3,5]=4, the right part is split as:

(A3A4)A5(A_3A_4)A_5

So the full optimal parenthesization is:

((A1A2)((A3A4)A5))\boxed{((A_1A_2)((A_3A_4)A_5))}

or equivalently

(A1A2)((A3A4)A5)\boxed{(A_1A_2)\big((A_3A_4)A_5\big)}

with minimum scalar multiplications:

1344\boxed{1344}

Minimum Cost by Subchain Length

Optimal dynamic programming costs obtained for representative subchains

Dynamic Programming Tables

Cost table m[i,j]m[i,j]

i\ji \backslash j12345
1012026410801344
2-036013201350
3--07201140
4---01680
5----0

Split table s[i,j]s[i,j]

i\ji \backslash j12345
1-1222
2--222
3---34
4----4
5-----

The entries in the split table show where each optimal subproblem divides. This is the basis for backtracking the final parenthesization after the cost table is complete.2

Footnotes

  1. Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity.

  2. Matrix-Chain Multiplication PDF, Columbia University - Lecture notes showing worked matrix-chain examples and parenthesization logic.

1(A1A2)((A3A4)A5)

Detailed Reasoning and Complexity

Common Mistake

Do not multiply dimensions directly from the original matrices after each step without updating the intermediate result sizes. Each parenthesization creates new matrix dimensions that affect later costs.

Footnotes

  1. Matrix-Chain Multiplication PDF, Columbia University - Lecture notes showing worked matrix-chain examples and parenthesization logic.

Final Answer

The most efficient parenthesization of the matrix chain

A1A2A3A4A5A_1A_2A_3A_4A_5

for dimensions

{4,10,3,12,20,7}\{4,10,3,12,20,7\}

is:

(A1A2)((A3A4)A5)\boxed{(A_1A_2)\big((A_3A_4)A_5\big)}

or fully parenthesized:

((A1A2)((A3A4)A5))\boxed{((A_1A_2)((A_3A_4)A_5))}

Total scalar multiplications

  1. A1A2A_1A_2:

    4103=1204\cdot10\cdot3=120
  2. A3A4A_3A_4:

    31220=7203\cdot12\cdot20=720
  3. (A3A4)A5(A_3A_4)A_5:
    intermediate size is 3×203\times20, so

    3207=4203\cdot20\cdot7=420
  4. (A1A2)((A3A4)A5)(A_1A_2)\big((A_3A_4)A_5\big):
    intermediate sizes are 4×34\times3 and 3×73\times7, so

    437=844\cdot3\cdot7=84

Therefore,

120+720+420+84=1344\boxed{120+720+420+84=1344}

Complexity of the dynamic programming method

For a chain of nn matrices:

  • Time complexity: O(n3)\boxed{O(n^3)}
  • Space complexity: O(n2)\boxed{O(n^2)}

These are the standard complexity bounds for bottom-up matrix-chain multiplication dynamic programming.2

Footnotes

  1. Introduction to Algorithms, Dynamic Programming PDF - CLRS treatment of matrix-chain multiplication recurrence and complexity.

  2. Matrix Chain Multiplication - GeeksforGeeks - Practical explanation of the recurrence and complexity bounds.

Knowledge Check

Question 1 of 4
Q1Single choice

What is the optimal parenthesization for the matrix chain with dimensions {4,10,3,12,20,7}\{4,10,3,12,20,7\}?