3.7.13 · D4Algorithm Paradigms

Exercises — DP problems — matrix chain multiplication

1,973 words9 min readBack to topic

Level 1 — Recognition

L1.1 — Read the dimensions off the array

Given , list each matrix with its dimensions.

Recall Solution

The rule is " uses and ". Walk the array in overlapping pairs:

Why it works: consecutive matrices share a boundary dimension (, , ), which is exactly why they can be multiplied — the inner dimensions match automatically. Number of matrices length of minus one .

L1.2 — Cost of a single multiplication

You multiply a matrix by a matrix. How many scalar multiplications?

Recall Solution

Cost . Why: the result is entries, and each entry is a dot product of length (the shared middle dimension), so .


Level 2 — Application

L2.1 — A length-2 chain (only one way)

. Compute .

Recall Solution

Only two matrices, so only one split (): Why no min needed: with two matrices there is exactly one place to put the parentheses.

L2.2 — A length-3 chain, compare both splits

(the motivation example). Compute by trying both split points, and state the optimal parenthesization.

Recall Solution

Chain , indices . Sub-costs first:

Now the two outer splits:

  • :
  • :

at → parenthesization .

Why wins: splitting after makes the final combine tiny, because the big dimension has already been "eaten" inside the cheap step.


Level 3 — Analysis

L3.1 — Full table for

Compute (optimal cost) and the optimal parenthesization. Fill by increasing chain length.

Recall Solution

Matrices: , .

Length 2:

Length 3:

  • :
    • :
    • :
    • min ,
  • :
    • :
    • :
    • min ,

Length 4:

  • :
  • :
  • :

at , .

Reconstruct: . Then . Final: .

See the table-filling order below — chains grow along the diagonal, never row by row.

Figure — DP problems — matrix chain multiplication

L3.2 — Which cells does depend on?

Without recomputing, state exactly which table entries must already exist before can be computed, and explain the diagonal fill order.

Recall Solution

For the splits require the pairs:

  • : and
  • : and
  • : and

So it needs — all of them shorter chains (length ). This is why we fill by increasing length : every cell of length only ever reaches into cells of length , which are already done. See the arrows in the figure below.

Figure — DP problems — matrix chain multiplication

Level 4 — Synthesis

L4.1 — Count the parenthesizations (Catalan connection)

How many distinct full parenthesizations does a product of matrices have? Give the formula and the number.

Recall Solution

The number of parenthesizations of matrices is the Catalan number : For we need : Why Catalan: picking the outermost split point recursively creates a left group and a right group, and — exactly the Catalan recurrence. See Catalan Numbers. This exponential growth () is precisely why brute force is hopeless and DP () wins.

L4.2 — Memoized top-down version, and why it's the same complexity

Write pseudocode for the top-down memoized MCM and argue it is still time, space.

Recall Solution
memo = 2D array filled with -1
def solve(i, j):
    if i == j: return 0
    if memo[i][j] != -1: return memo[i][j]
    best = +infinity
    for k in i..j-1:
        cost = solve(i,k) + solve(k+1,j) + p[i-1]*p[k]*p[j]
        best = min(best, cost)
    memo[i][j] = best
    return best
# answer = solve(1, n)

Why same complexity: there are distinct states (each solved once thanks to memo), and each does an loop over time. Space is the memo table (plus recursion stack). This is the Memoization vs Tabulation equivalence: same states, same transitions, different visiting order. See Time Complexity Analysis.


Level 5 — Mastery

L5.1 — Same paradigm, different combine: Burst Balloons

In Burst Balloons you have balloons with values , padded to . Bursting balloon (last, inside interval ) earns (adjacent survivors). The recurrence is Explain in one paragraph how this is structurally identical to MCM, and name the one conceptual switch that makes it work.

Recall Solution

Both are interval DP: solve a sub-range by fixing one special element and recursing on the two pieces. In MCM you fix the last multiplication (split at ), and the two sides are independent because a completed block only exposes its outer dimensions. In Burst Balloons you fix the last balloon burst (not the first!) — because once is the last to pop inside , its neighbours are exactly the survivors and , making the two sub-intervals independent. The one conceptual switch: fix the LAST event, not the first, so the subproblems decouple. The combine term (MCM) becomes (balloons) — same "three boundary values multiplied" shape. This same "try every split/root" pattern also drives Optimal Binary Search Tree.

L5.2 — Design a variant: minimize the maximum single multiplication

Instead of minimizing the sum of scalar multiplications, suppose you want to minimize the largest single combine cost used in the whole plan (a "peak memory/latency" objective). Write the modified recurrence and compute the answer for .

Recall Solution

Replace "sum then min" with "for each split take the max of the three pieces, then min over ": Why max inside: the peak is the worst single operation along the way, so combining pieces takes the largest of {left's peak, right's peak, this combine}. Why min outside: we still get to choose the split, so we pick the one whose peak is smallest.

Compute for ():

  • :
    • :
    • :
    • min at .

So the minimum peak is with . Insight: the recurrence skeleton (interval DP, split at ) is untouched — only the aggregation operators changed (sum→max). Recognising that "the paradigm is the split, the objective is swappable" is the L5 mastery point.


Recall Quick self-test recap

Fill by increasing chain length? ::: Yes — , never row by row. Combine cost for split on ? ::: . Number of parenthesizations of matrices? ::: Catalan . How to switch MCM into "minimize peak cost"? ::: Replace inner sum with max, keep the outer min. Interval-DP golden rule? ::: Fix the LAST event so subproblems decouple.

Connections

  • Dynamic Programming — optimal substructure + overlapping subproblems
  • Catalan Numbers — counts the parenthesizations (L4.1)
  • Burst Balloons — same interval DP, fix the last burst (L5.1)
  • Optimal Binary Search Tree — "try every root" interval DP
  • Memoization vs Tabulation — L4.2 top-down equivalence
  • Time Complexity Analysis from three nested loops