Knuth’s Optimization & Monge Arrays
Quadrangle inequalities, totally monotone matrices and SMAWK.
In 1971 Knuth noticed that the root of an optimal binary search tree on a range of keys never moves left when a key is added on the right, nor right when a key is added on the left, and that this turns an DP into . In 1980 Frances Yao found the reason, an inequality between the costs of overlapping intervals. In 1987 Aggarwal, Klawe, Moran, Shor and Wilber built on the same inequality an algorithm that finds the minimum of every row of an matrix while looking at only of its entries.
What you’ll learn
- Define Monge arrays and total monotonicity, prove the local 2 × 2 test and the monotonicity of row minima, and recognize Monge costs.
- State and prove the Knuth–Yao theorem: the quadrangle inequality and monotonicity of the weights pass to the DP, which makes the optimal splits monotone and the whole table .
- Explain the SMAWK algorithm, prove its correctness and its bound, and use it to compute DP layers in linear time.
- Connect Monge structure to transportation problems, planar shortest paths and fast (min, +) products, and state what remains open.
Monge arrays and total monotonicity
The local test. If the inequality holds for all adjacent rows and columns, it holds for all , : sum the adjacent inequalities over the rectangle, and the inner terms cancel. Checking an matrix therefore takes tests, not .
Row minima move right. Let be the leftmost minimum of row . If but , then (as is the leftmost minimum of row ), and the Monge inequality forces row to prefer strictly, contradicting the choice of :
| Matrix or cost | Monge? | Why |
|---|---|---|
| for a convex , such as or | yes | the adjacent test is |
| for sorted and | yes | the cross term gives |
| interval sums | yes, with equality | the four terms cancel exactly |
| , or | no: inverse Monge | the inequality holds reversed, so row maxima move right instead |
| sums of Monge arrays; adding | yes | the defining inequality is linear in , and contributes zero |
Which of these matrices (rows , columns ) is Monge?
Show the answer
B.
with convex, so its adjacent second differences are at most 0. The other three fail: and have the cross term with the wrong sign, and is inverse Monge.
Knuth’s optimization, proved
Use Yao's form of an interval DP: , and for
with the largest achieving the minimum. Merging piles into one, paying the size of each merged pile, has this form with : the last merge joins and . Knuth's optimal binary search trees (1971) have the same shape, with the root excluded from both subtrees.
The cases or are equalities. Take the main case , and let and ; suppose (the other case is symmetric). Then is a legal split for and for :
The quadrangle inequality of trades for . For the terms, and , so the induction hypothesis applies:
Regroup: the right side is exactly , with split , plus , with split :
The remaining case asks for . With , split at and use the induction hypothesis together with the monotonicity ; the case is symmetric. This is the only place where monotonicity is needed.
Write for the cost of splitting at . For , part (1) on gives ; add the same and terms to both sides:
Take and any . Since , the inequality gives : no split left of beats it for , so the largest optimal split cannot move left. The bound follows the same way from part (1) on .
def merge_piles(a): # D[i][j]: least cost to merge piles a[i..j] (inclusive)
n = len(a)
P = [0]
for v in a:
P.append(P[-1] + v)
D = [[0] * n for _ in range(n)]
K = [[i] * n for i in range(n)] # K[i][i] = i seeds the windows
for length in range(1, n):
for i in range(n - length):
j = i + length
best, arg = None, None
for k in range(max(i + 1, K[i][j - 1]), min(j, K[i + 1][j]) + 1):
v = D[i][k - 1] + D[k][j] # last merge joins a[i..k-1] and a[k..j]
if best is None or v <= best:
best, arg = v, k # ties: keep the largest split
D[i][j], K[i][j] = best + P[j + 1] - P[i], arg
return D[0][n - 1]For piles it returns 34: merge (4), then (8), and (7), then the last merge (15). On 400 random piles it evaluates about splits, roughly , where the plain DP evaluates about , roughly . Tested against the plain DP on 1,000 random inputs.
SMAWK: every row minimum in linear time
Now take an totally monotone matrix given by an oracle that returns any entry in , and ask for the leftmost minimum of every row. Scanning costs . Monotone row minima alone allow divide and conquer on the rows: find the middle row's minimum, then search the upper half left of it and the lower half right of it, , which is divide-and-conquer optimization in matrix language. The SMAWK algorithm (Aggarwal, Klawe, Moran, Shor & Wilber, 1987) uses total monotonicity to reach :
- Reduce. Scan the columns left to right with a stack. Let the top column sit at stack position ; compare it with the new column in row . If is strictly smaller there, pop the top and compare again; otherwise push , unless the stack already holds columns, in which case is dropped. At most columns survive.
- Recurse on every other row, with the surviving columns.
- Interpolate. Each remaining row's minimum lies between the minima of the rows just above and below it; scan only that range.
Invariant: the column at stack position is no candidate for rows (some column to its left is at least as small there). It holds when the column is pushed at position : it did not beat the column below it in row , so by total monotonicity it beats it in no earlier row either.
Popping is safe. If the new column is strictly smaller than the top in row , total monotonicity makes it strictly smaller in every later row. So is dominated in rows by , and in rows by the invariant: it is nobody's leftmost minimum. A column dropped from a full stack is likewise dominated in every row.
Cost. Each column is pushed at most once and popped at most once, so Reduce takes comparisons and leaves at most columns. Interpolation over all the remaining rows scans each surviving column at most twice plus one step per row. With the cost on rows and columns:
def smawk(rows, cols, A): # leftmost minimum column of each row; A totally monotone
if not rows:
return {}
stack = [] # Reduce: at most len(rows) columns survive
for c in cols:
while stack and A(rows[len(stack) - 1], c) < A(rows[len(stack) - 1], stack[-1]):
stack.pop() # c beats the top column from this row downward
if len(stack) < len(rows):
stack.append(c)
ans = smawk(rows[1::2], stack, A) # recurse on every other row
j = 0
for i in range(0, len(rows), 2): # each remaining row: scan between its neighbors' answers
r = rows[i]
stop = ans[rows[i + 1]] if i + 1 < len(rows) else stack[-1]
best = stack[j]
while stack[j] != stop:
j += 1
if A(r, stack[j]) < A(r, best):
best = stack[j]
ans[r] = best
return ansCall smawk(list(range(n)), list(range(m)), A). On random Monge matrices it evaluates about entries per row and column, and it agrees with a brute-force scan on 500 random matrices. It is also optimal: a matrix whose rows are all equal is totally monotone, so finding one row's minimum may already require all entries, and answers must be written.
DP layers in linear time. A layer is the row-minimum problem for the matrix , with for . If satisfies the quadrangle inequality, adding the column term keeps the finite part Monge, and the infinite entries form a staircase that keeps the whole matrix totally monotone. SMAWK then computes each layer in , so layers cost , one logarithm better than divide and conquer.
The self-referential form is harder, because row needs entries that depend on earlier answers. For quadrangle-inequality costs, Larmore and Schieber (1991) adapted SMAWK to this online setting in time.
Geometry. The original application was to convex polygons: for the vertices of a convex -gon, the farthest other vertex of every vertex can be found in total, because the distance matrix, suitably cut, is totally monotone for maxima.
Monge structure across algorithms
Transportation. Monge's 1781 memoir on moving earth from cuts to fills gave the property its name. Hoffman (1963) proved that the greedy northwest-corner rule, which ships as much as possible from the first source to the first destination and moves on, solves every transportation problem with a given cost matrix exactly when that matrix is Monge.
Knuth–Yao as total monotonicity. Bein, Golin, Larmore and Zhang (2009) showed that the Knuth–Yao speedup is itself a consequence of total monotonicity of suitably defined matrices, unifying it with SMAWK-style row minima.
Planar graphs. In a planar graph, the distances between vertices on the boundary of one face form (pieces of) Monge matrices, because shortest paths between interleaved boundary pairs must cross. Fakcharoenphol and Rao (2006) built on this a near-linear algorithm for shortest paths with negative weights in planar graphs, and Monge searches remain a core tool of planar-graph algorithms.
Fast distance products. The (min, +) product of two Monge matrices is Monge and can be computed in time with SMAWK, one row at a time. For unit-Monge matrices, which arise from permutations, Tiskin computed the product in time; it powers fast semi-local comparisons of strings, such as the longest common subsequence of one string against every substring of another.
Key takeaways
- A matrix is Monge if for , ; adjacent tests suffice, and Monge arrays are totally monotone, so their leftmost row minima move right.
- Knuth–Yao: if satisfies the quadrangle inequality and is monotone, the DP inherits the inequality and its largest optimal splits satisfy , so interval DP costs .
- SMAWK finds all row minima of an totally monotone matrix in evaluations by reducing columns with a stack, recursing on half the rows and interpolating; it computes a quadrangle-inequality DP layer in .
- Monge structure reaches far beyond DP: greedy transportation (Hoffman), planar shortest paths, and fast distance products for Monge and unit-Monge matrices.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
To verify that an matrix is Monge, which tests suffice?
- All choices of two rows and two columns
- The submatrices of two adjacent rows and two adjacent columns
- That every row's minimum lies to the right of the previous row's
- The diagonal entries
Show the answer
B. The submatrices of two adjacent rows and two adjacent columns
Summing the adjacent inequalities over any rectangle telescopes to the inequality for its four corners, so the local tests imply all the others.
Which of these is not a hypothesis of the Knuth–Yao theorem for ?
- satisfies the quadrangle inequality
- whenever
- depends only on the length
- The splits are compared by the DP's own values
Show the answer
C. depends only on the length
The theorem needs the quadrangle inequality and monotonicity of ; interval sums of arbitrary non-negative piles satisfy both without depending only on length. The comparison of splits by is simply the DP itself.
How many entry evaluations does SMAWK need to find all leftmost row minima of an totally monotone matrix?
Show the answer
D.
Reduce keeps at most of the columns in comparisons, the recursion handles half the rows with at most columns, and interpolation costs : .
A DP has layers , with satisfying the quadrangle inequality. What is the best total time among the methods of this lesson and the previous ones?
- , since layers depend on each other
- , the limit of divide and conquer
- , with SMAWK on each layer
- , independent of
Show the answer
C. , with SMAWK on each layer
Each layer is the row-minimum problem of a totally monotone matrix (a Monge part plus a staircase of infinities), which SMAWK solves in ; layers cost .
Further reading
- D. E. Knuth, "Optimum binary search trees," Acta Informatica 1 (1971).
- F. F. Yao, "Efficient dynamic programming using quadrangle inequalities," Proceedings of the 12th ACM Symposium on Theory of Computing (1980).
- A. Aggarwal, M. M. Klawe, S. Moran, P. Shor & R. Wilber, "Geometric applications of a matrix-searching algorithm," Algorithmica 2 (1987), 195–208.
- R. E. Burkard, B. Klinz & R. Rudolf, "Perspectives of Monge properties in optimization," Discrete Applied Mathematics 70 (1996).
- W. Bein, M. J. Golin, L. L. Larmore & Y. Zhang, "The Knuth–Yao quadrangle-inequality speedup is a consequence of total monotonicity," ACM Transactions on Algorithms 6 (2009).