1. Gyanastra
  2. Algorithms
  3. Matrix Chain Multiplication Worked Table, Catalan Counting and Optimal BST
  4. Full worked table for p = ⟨10, 30, 5, 60⟩
Free lesson, no account needed 5 min read

Algorithms · Matrix Chain Multiplication Worked Table, Catalan Counting and Optimal BST

Full worked table for p = ⟨10, 30, 5, 60⟩

Here n=3n=3 matrices:

  • A1A_1: 10×3010\times30
  • A2A_2: 30×530\times5
  • A3A_3: 5×605\times60

So p0=10, p1=30, p2=5, p3=60p_0=10,\ p_1=30,\ p_2=5,\ p_3=60.

Length 1 (base case)

cellvalue
m[1][1]m[1][1]0
m[2][2]m[2][2]0
m[3][3]m[3][3]0

Length 2

m[1][2]m[1][2]: only k=1k=1. m[1][2]=m[1][1]+m[2][2]+p0p1p2=0+0+10⋅30⋅5m[1][2] = m[1][1] + m[2][2] + p_0 p_1 p_2 = 0 + 0 + 10\cdot30\cdot5 10⋅30=30010\cdot30 = 300; 300⋅5=1500300\cdot5 = 1500. So m[1][2]=1500m[1][2]=1500, s[1][2]=1s[1][2]=1.

m[2][3]m[2][3]: only k=2k=2. m[2][3]=m[2][2]+m[3][3]+p1p2p3=0+0+30⋅5⋅60m[2][3] = m[2][2] + m[3][3] + p_1 p_2 p_3 = 0 + 0 + 30\cdot5\cdot60 30⋅5=15030\cdot5 = 150; 150⋅60=9000150\cdot60 = 9000. So m[2][3]=9000m[2][3]=9000, s[2][3]=2s[2][3]=2.

Length 3

m[1][3]m[1][3]: two candidates, k=1k=1 and k=2k=2.

  • k=1k=1: m[1][1]+m[2][3]+p0p1p3=0+9000+10⋅30⋅60m[1][1] + m[2][3] + p_0 p_1 p_3 = 0 + 9000 + 10\cdot30\cdot60. 10⋅30=30010\cdot30=300; 300⋅60=18000300\cdot60=18000. Total =9000+18000=27000= 9000 + 18000 = 27000.
  • k=2k=2: m[1][2]+m[3][3]+p0p2p3=1500+0+10⋅5⋅60m[1][2] + m[3][3] + p_0 p_2 p_3 = 1500 + 0 + 10\cdot5\cdot60. 10⋅5=5010\cdot5=50; 50⋅60=300050\cdot60=3000. Total =1500+3000=4500= 1500 + 3000 = 4500.

Minimum is 45004500 at k=2k=2. So m[1][3]=4500m[1][3]=4500, s[1][3]=2s[1][3]=2.

The complete mm table

m[i][j]m[i][j]j=1j=1j=2j=2j=3j=3
i=1i=1015004500
i=2i=2—09000
i=3i=3——0

The complete ss table

s[i][j]s[i][j]j=1j=1j=2j=2j=3j=3
i=1i=1—12
i=2i=2——2
i=3i=3———

Reconstructing the parenthesization

Start at (1,3)(1,3): s[1][3]=2s[1][3]=2 means split as (A1A2)(A3)(A_1A_2)(A_3).

  • Left part (1,2)(1,2): s[1][2]=1s[1][2]=1 means (A1)(A2)(A_1)(A_2).
  • Right part (3,3)(3,3): single matrix.

So the optimal parenthesization is ((A1A2)A3)\big((A_1A_2)A_3\big).

Verification by direct multiplication:

  • A1A2A_1A_2: 10×3010\times30 times 30×530\times5 costs 10⋅30⋅5=150010\cdot30\cdot5 = 1500, result is 10×510\times5.
  • (A1A2)A3(A_1A_2)A_3: 10×510\times5 times 5×605\times60 costs 10⋅5⋅60=300010\cdot5\cdot60 = 3000, result is 10×6010\times60.
  • Total =1500+3000=4500= 1500 + 3000 = \mathbf{4500}. ✓

Compare with the other order (A1)(A2A3)(A_1)(A_2A_3):

  • A2A3A_2A_3: 30×530\times5 times 5×605\times60 costs 30⋅5⋅60=900030\cdot5\cdot60 = 9000, result 30×6030\times60.
  • A1(A2A3)A_1(A_2A_3): 10×3010\times30 times 30×6030\times60 costs 10⋅30⋅60=1800010\cdot30\cdot60 = 18000.
  • Total =9000+18000=27000= 9000 + 18000 = 27000.

So the optimal cost is 4500, and the optimal parenthesization is ((A1A2)A3)((A_1A_2)A_3).


Counting parenthesizations: the Catalan number

The number of distinct ways to fully parenthesize a product of nn matrices is the (n−1)(n-1)-th Catalan number:

Cn−1=1n(2n−2 n−1 )=1n⋅(2n−2)!(n−1)! (n−1)!C_{n-1} = \frac{1}{n}\binom{2n-2}{\,n-1\,} = \frac{1}{n}\cdot\frac{(2n-2)!}{(n-1)!\,(n-1)!}

Equivalently, writing CkC_k for the kk-th Catalan number (with C0=1C_0=1):

Ck=1k+1(2kk),C0=1, C1=1, C2=2, C3=5, C4=14, C5=42,…C_k = \frac{1}{k+1}\binom{2k}{k}, \qquad C_0=1,\ C_1=1,\ C_2=2,\ C_3=5,\ C_4=14,\ C_5=42,\dots

The recurrence is C0=1C_0=1 and Ck=∑i=0k−1Ci Ck−1−iC_k = \sum_{i=0}^{k-1} C_i\,C_{k-1-i} for k≥1k\ge1 (e.g. C3=C0C2+C1C1+C2C0=2+1+2=5C_3=C_0C_2+C_1C_1+C_2C_0=2+1+2=5).

Check the values:

  • n=3n=3 matrices: Cn−1=C2=13(42)=13⋅6=2C_{n-1}=C_2 = \frac{1}{3}\binom{4}{2} = \frac{1}{3}\cdot 6 = 2. ✓ (We saw exactly two: ((A1A2)A3)((A_1A_2)A_3) and (A1(A2A3))(A_1(A_2A_3)).)
  • n=4n=4 matrices: C3=14(63)=14⋅20=5C_3 = \frac{1}{4}\binom{6}{3} = \frac{1}{4}\cdot 20 = 5. ✓
  • n=5n=5 matrices: C4=15(84)=15⋅70=14C_4 = \frac{1}{5}\binom{8}{4} = \frac{1}{5}\cdot 70 = 14. ✓
nn (matrices)Cn−1C_{n-1}value
1C0C_01
2C1C_11
3C2C_22
4C3C_35
5C4C_414
6C5C_542

Why this matters: the brute-force algorithm that tries every parenthesization is Ω(Cn−1)\Omega(C_{n-1}), which grows like 4n/n3/24^n/n^{3/2} — exponential. The DP is polynomial, which is the whole point.


Complexity of the DP

  • Number of distinct subproblems: pairs (i,j)(i,j) with 1≤i≤j≤n1\le i\le j\le n, i.e. (n2)+n=n(n+1)2=O(n2)\binom{n}{2}+n = \frac{n(n+1)}{2} = O(n^2).
  • Work per subproblem: the min over kk from ii to j−1j-1, at most n−1n-1 choices, so O(n)O(n).
  • Total time: O(n2)⋅O(n)=O(n3)O(n^2)\cdot O(n) = O(n^3).
  • Space: the mm and ss tables are each n×nn\times n, so O(n2)O(n^2).
QuantityBound
SubproblemsO(n2)O(n^2)
Time per subproblemO(n)O(n)
Total timeO(n3)O(n^3)
SpaceO(n2)O(n^2)
Brute force (all parenthesizations)Ω(Cn−1)\Omega(C_{n-1}), exponential

Pages 1–2 of 2