Dynamic programming

O(n³)·O(n²) space
Whole table

Drag to pan; pinch or Ctrl+scroll to zoom, which stops at the whole table; arrow keys pan once the picture has focus. The headers stay put while you pan.

A1, 5 by 10, A1, 5 by 10, not yet filledA1, 5 by 10, A2, 10 by 3, not yet filledA1, 5 by 10, A3, 3 by 12, not yet filledA1, 5 by 10, A4, 12 by 5, not yet filledA1, 5 by 10, A5, 5 by 50, not yet filledA1, 5 by 10, A6, 50 by 6, not yet filledA2, 10 by 3, A1, 5 by 10, not part of this tableA2, 10 by 3, A2, 10 by 3, not yet filledA2, 10 by 3, A3, 3 by 12, not yet filledA2, 10 by 3, A4, 12 by 5, not yet filledA2, 10 by 3, A5, 5 by 50, not yet filledA2, 10 by 3, A6, 50 by 6, not yet filledA3, 3 by 12, A1, 5 by 10, not part of this tableA3, 3 by 12, A2, 10 by 3, not part of this tableA3, 3 by 12, A3, 3 by 12, not yet filledA3, 3 by 12, A4, 12 by 5, not yet filledA3, 3 by 12, A5, 5 by 50, not yet filledA3, 3 by 12, A6, 50 by 6, not yet filledA4, 12 by 5, A1, 5 by 10, not part of this tableA4, 12 by 5, A2, 10 by 3, not part of this tableA4, 12 by 5, A3, 3 by 12, not part of this tableA4, 12 by 5, A4, 12 by 5, not yet filledA4, 12 by 5, A5, 5 by 50, not yet filledA4, 12 by 5, A6, 50 by 6, not yet filledA5, 5 by 50, A1, 5 by 10, not part of this tableA5, 5 by 50, A2, 10 by 3, not part of this tableA5, 5 by 50, A3, 3 by 12, not part of this tableA5, 5 by 50, A4, 12 by 5, not part of this tableA5, 5 by 50, A5, 5 by 50, not yet filledA5, 5 by 50, A6, 50 by 6, not yet filledA6, 50 by 6, A1, 5 by 10, not part of this tableA6, 50 by 6, A2, 10 by 3, not part of this tableA6, 50 by 6, A3, 3 by 12, not part of this tableA6, 50 by 6, A4, 12 by 5, not part of this tableA6, 50 by 6, A5, 5 by 50, not part of this tableA6, 50 by 6, A6, 50 by 6, not yet filledA1, 5 by 10A15×10A2, 10 by 3A210×3A3, 3 by 12A33×12A4, 12 by 5A412×5A5, 5 by 50A55×50A6, 50 by 6A650×6A1, 5 by 10A15×10A2, 10 by 3A210×3A3, 3 by 12A33×12A4, 12 by 5A412×5A5, 5 by 50A55×50A6, 50 by 6A650×6→ to↓ from

The table has 6 rows and 6 columns, and nothing has been worked out yet. Only the upper triangle is part of the problem, and it fills outwards from the diagonal rather than row by row.

The table has 6 rows and 6 columns, and nothing has been worked out yet. Only the upper triangle is part of the problem, and it fills outwards from the diagonal rather than row by row.
Answer so far—

M(i, j) = min over k in [i, j) of M(i, k) + M(k+1, j) + pᵢ₋₁ · pₖ · pⱼ

  1. 1a single matrix costs nothing to multiply, so the whole diagonal is zero
  2. 2take the next diagonal: every subchain one longer than the last
  3. 3try each place this subchain could be split in two
  4. 4write the cheapest, and remember where it split
  5. 5walk back through the remembered splits to read off the brackets
  6. 6stop: the top-right cell is the cheapest cost, and the walk back is the order
Cells filled
0
Cells read
0
Values written
0

Complexity

BestO(n³)
AverageO(n³)
WorstO(n³)
Extra spaceO(n²)
n³ / 6 at n=36 predicts 36; this run took 0.

Matrix chain multiplication

Multiplying matrices is associative, so the answer does not depend on the order — but the cost does, often by orders of magnitude. The table holds the cheapest cost of multiplying each subchain. To fill a cell you try every place the subchain could be split in two, add the cost of the two halves and the cost of the single multiplication that joins them, and keep the smallest. Both halves are shorter than the whole, so every cell a split needs is on an earlier diagonal.

Its recurrence

M(i, j) = min over k in [i, j) of M(i, k) + M(k+1, j) + pᵢ₋₁ · pₖ · pⱼ

When it is the right choice

Any time an associative operation has a cost that depends on where the brackets go: matrix products in graphics and machine learning, join ordering in a query planner, and parsing problems with the same shape. It is also the standard example of interval dynamic programming, which is the family this fill order belongs to — the same shape solves optimal binary search trees and polygon triangulation.

How it works

Worth noticing

Cost

Best
O(n³)
Average
O(n³)
Worst
O(n³)
Extra space
O(n²)
Table
two dimensions
Accepts
4 to 9 dimensions, each from 1 to 99

The limits are pedagogical rather than technical: this tool keeps the whole table in every recorded step, so the shape of the table sets the size of the trace directly, and a run has to stay short enough to sit and watch. An input that would take more than three thousand steps on this algorithm is refused before a single step is generated, from a formula in the input sizes rather than by running it and finding out.

An unfilled cell is drawn differently from a cell holding a zero — no fill, a dashed outline, and no number at all — because early in a run that difference is most of the picture.

Worth comparing

All dynamic programming algorithms