Purpose: Find the row minima of an totally monotone matrix in — without ever reading most of the matrix. Named for its five authors: Aggarwal, Klawe, Moran, Shor, Wilber.
Totally monotone matrices
A matrix is totally monotone (for row minima) if for all and :
In words: once a later column becomes at least as good in some row, it stays at least as good in every lower row. This implies the argmin positions are non-decreasing down the rows:
Any matrix satisfying the quadrangle inequality (the concave/convex Monge condition)
is totally monotone. Monge matrices arise constantly in DP: costs of the form with a concave or convex structure, distances in trees, and many “cost of merging a range” functions.
Algorithm
Two mutually recursive procedures:
- REDUCE. If , discard columns that cannot contain any row minimum. Use a stack: compare the top column against the current one at row ; total monotonicity lets each comparison either pop a column permanently or advance. Result: columns remain, in comparisons.
- SMAWK. Recurse on the even-indexed rows only. That gives the argmins for half the rows. The odd rows’ argmins are then sandwiched between their neighbours’ argmins, so a single linear scan fills them in.
Complexity
- Time: matrix accesses — optimal, since you must at least read one entry per row
- Space:
- Requirement: entries must be computable in on demand (the matrix is never materialised)
The optimization ladder for DP
SMAWK is the strongest member of a family:
| Recurrence | Condition | Technique | Time |
|---|---|---|---|
| none | brute force | ||
| same | convex/concave Monge | D&C DP | |
| same | Monge, offline | SMAWK | |
| same | Monge, online (values depend on earlier answers) | LARSCH / “SMAWK online” | |
| linear in | CHT | or | |
| quadrangle inequality | Knuth |
The online problem
Plain SMAWK requires the matrix entries to be available on demand — but in a 1D/1D DP, depends on , which is itself being computed. The LARSCH algorithm (Larmore-Schieber) handles this online case in . In a contest, D&C DP at is nearly always sufficient and far easier.
Applications
- Least-weight subsequence / line breaking — the classic Knuth-Plass text justification problem
- Concave least-weight subsequence — optimal paragraph and page breaking
- All-pairs shortest paths in Monge graphs, RNA secondary structure prediction
- Largest empty rectangle, optimal binary search trees with Monge costs
- Row minima of a distance matrix in a convex polygon
Variants / Use Cases
- Divide and Conquer DP — the version you should write
- Knuth optimization — the interval-DP cousin
- Yao optimization — the quadrangle inequality conditions in general
- Dynamic Programming — the branch overview