3.7.13 · D2Algorithm Paradigms

Visual walkthrough — DP problems — matrix chain multiplication

1,574 words7 min readBack to topic

We will use one running chain the whole way: so the dimension array (the list of numbers that describes all the shapes) is


Step 1 — What a matrix "shape" is, and what one multiplication costs

WHAT. A matrix is just a grid of numbers. Its shape is written rows × columns. Matrix is : 35 rows, 15 columns. In the dimension array, has shape — it borrows the number on its left () for rows and the number on its right () for columns.

WHY only shapes. To count effort we never need the actual numbers inside — the effort depends only on how big the grids are. So we throw away the contents and keep the shape list .

PICTURE. To multiply an grid by a grid, the two inner numbers must match (both ). You produce an result, and each of its cells is a sum of products.

Figure — DP problems — matrix chain multiplication

Step 2 — Two orders, wildly different bills

WHAT. Take just the first three matrices and glue them two-at-a-time. Two possible orders exist. The final grid is identical (matrix multiplication is associative), but the running total of scalar multiplications is not.

WHY it matters. If both orders cost the same we would have no problem to solve. They don't — sometimes by a factor of 10 or more — so choosing the order is worth real work.

PICTURE. Watch the intermediate grid each order builds. A fat intermediate grid is what makes an order expensive.

Figure — DP problems — matrix chain multiplication

Using (a sharper illustration):


Step 3 — The one idea: look at the LAST glue

WHAT. Forget how the whole product is built. Ask only: which multiplication happens last? Whatever the parenthesization, there is exactly one outermost multiply, and it joins a left block to a right block.

WHY this question. It cuts a hard problem into two smaller, independent problems. If the last glue sits after matrix , then:

  • everything to the left, , has already collapsed into one grid,
  • everything to the right, , has already collapsed into one grid,
  • and their internal costs don't affect each other. This is optimal substructure.

PICTURE. The split point is a single cut in the chain. The two halves are their own miniature MCM problems.

Figure — DP problems — matrix chain multiplication

Step 4 — What the last glue costs

WHAT. After the two halves collapse, we know their shapes exactly from :

  • Left block produces a grid.
  • Right block produces a grid.

WHY these shapes. The left block starts at (rows ) and ends at (columns ). The right block starts at (rows ) and ends at (columns ). The two inner numbers are both — they match automatically, which is why consecutive blocks can always be glued.

PICTURE. Three boundary numbers survive: , , . By Step 1's rule the final glue costs their product.

Figure — DP problems — matrix chain multiplication

Step 5 — Assembling the recurrence

WHAT. Name the thing we want: let be the minimum number of scalar multiplications to fully collapse . The total cost for a chosen split is: cost of the left half + cost of the right half + the final glue.

WHY a min over . We do not know the best cut in advance (Step 2 proved greedy guessing fails). So we try every legal cut and keep the cheapest — that is the only honest choice.

PICTURE. Each candidate is one branch. We evaluate them all and pick the shortest bar.

Figure — DP problems — matrix chain multiplication

Step 6 — The degenerate & edge cases (never leave a gap)

WHAT. Every corner the reader could hit:

  1. One matrix (): the min has no valid (the range is empty). We must not call min on an empty set → this is precisely why the base case exists as a separate branch.
  2. Two matrices (): exactly one legal . No real choice, cost is just — matches Step 1's single-multiply cost, as it should.
  3. A dimension equal to 1 (e.g. a row or column vector): the formula still works; a in the product just makes that glue cheap. Nothing special-cased.
  4. Empty chain (, array length 1): no matrices, cost — guard before the loops.

WHY show these. A recurrence that crashes on the smallest input is not finished. The base case is not decoration; it is the floor the whole recursion stands on.

PICTURE. The recursion tree bottoms out only when every leaf is a single matrix (cost 0).

Figure — DP problems — matrix chain multiplication

Step 7 — Why fill by chain LENGTH, not by rows

WHAT. To compute we need and — both are shorter chains. So we must have all short chains before any long one.

WHY not row-by-row. Reading a table left-to-right, top-to-bottom would ask for (length 4) while (length 3) is still blank. Fill by increasing length instead: (the zeros), then , up to .

PICTURE. The DP table filling as diagonals sweeping outward from the main diagonal.

Figure — DP problems — matrix chain multiplication

Working our chain :

Recall Where does 9375 come from?

Best split of the full chain is ::: , and inside gives , so the answer is at cost 9375.


The one-picture summary

Everything above compressed into a single frame: the chain, one last cut at , two collapsed halves, three surviving boundary numbers feeding the glue cost, and the min over all cuts feeding the DP table filled by length.

Figure — DP problems — matrix chain multiplication
Recall Feynman retelling — the whole walkthrough in plain words

You have a row of paper sheets to stack-glue, two at a time. Gluing an sheet onto a sheet costs effort (Step 1). The final sheet looks the same no matter the gluing order, but the effort can differ 10× because fat middle sheets are expensive (Step 2). The trick: think about the very last glue — it always splits the row into a left piece and a right piece that were built independently (Step 3). Once split, the shapes of the two pieces are fixed by the boundary numbers, so the last glue costs (Step 4). We don't know the best place to cut, so we try every cut and keep the cheapest — that's the min over (Step 5). A single sheet costs nothing, which is the floor of the whole idea (Step 6). And because a long piece is built from shorter ones, we solve short pieces first, sweeping the table by length (Step 7). That's the entire algorithm — one honest "try all last cuts" repeated cleverly.


Connections

  • Dynamic Programming — the optimal-substructure engine behind Step 5
  • Catalan Numbers — how many parenthesizations exist (), i.e. why brute force loses
  • Optimal Binary Search Tree — same "try every split" interval-DP shape
  • Burst Balloons — interval DP where you instead fix the last balloon
  • Memoization vs Tabulation — top-down vs the length-sweep here
  • Time Complexity Analysis — the from loops over