1. Gyanastra
  2. Algorithms
  3. Matrix Chain Multiplication Worked Table, Catalan Counting and Optimal BST
  4. The problem, from zero
Free lesson, no account needed 4 min read
Asked in past papers3 times
  • GATE CSE2016
  • UGC NET20172016

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

The problem, from zero

This chapter is the "exam drill" companion to the Matrix Chain Multiplication chapter. We take the same DP, but we (1) fill the table cell by cell with every arithmetic step shown, (2) count how many parenthesizations exist (Catalan numbers), (3) state the complexity bounds precisely, and (4) extend the same "interval DP" idea to the Optimal Binary Search Tree (OBST) problem, which is the standard follow-up question in GATE.


We must multiply a chain of matrices A1A2⋯AnA_1 A_2 \cdots A_n. Matrix multiplication is associative, so the result is the same for every parenthesization, but the number of scalar multiplications differs enormously.

Dimensions. Matrix AiA_i has dimension pi−1×pip_{i-1} \times p_i. So the dimension array has n+1n+1 entries p0,p1,…,pnp_0, p_1, \dots, p_n.

Cost of one product. Multiplying a a×ba\times b matrix by a b×cb\times c matrix costs a⋅b⋅ca\cdot b\cdot c scalar multiplications (and produces an a×ca\times c matrix).

Goal. Choose the parenthesization minimizing total scalar multiplications.


The recurrence (with every symbol defined)

Let m[i][j]m[i][j] = minimum number of scalar multiplications needed to compute the product AiAi+1⋯AjA_i A_{i+1}\cdots A_j (for 1≤i≤j≤n1 \le i \le j \le n).

m[i][i]=0for all im[i][i] = 0 \quad\text{for all } i

m[i][j]=min⁡i≤k<j{ m[i][k]+m[k+1][j]+pi−1 pk pj },i<jm[i][j] = \min_{i \le k < j}\Big\{\, m[i][k] + m[k+1][j] + p_{i-1}\,p_k\,p_j \,\Big\}, \quad i<j

Symbols:

  • i,ji,j — first and last matrix index in the subchain (1-based).
  • kk — the split point: we compute Ai..AkA_i..A_k and Ak+1..AjA_{k+1}..A_j, then multiply the two results.
  • m[i][k],m[k+1][j]m[i][k], m[k+1][j] — optimal costs of the two subchains.
  • pi−1pkpjp_{i-1}p_k p_j — cost of the final multiplication: left result is pi−1×pkp_{i-1}\times p_k, right result is pk×pjp_k\times p_j, product costs pi−1⋅pk⋅pjp_{i-1}\cdot p_k\cdot p_j.

Let s[i][j]s[i][j] = the kk that achieves the minimum (used to reconstruct the parenthesization).

Order of filling: by increasing chain length L=j−i+1L = j-i+1, from L=2L=2 up to L=nL=n. (Length 1 is the base case.)


Pages 1–2 of 2