Interval dynamic programming: matrix-chain multiplication and polygon triangulation #
This module generalizes the k-independent interval recurrence of
CslibFrontier.Algorithms.Lean.OptimalBST.Basic to split-dependent weights w : ℕ → ℕ → ℕ → β:
c(i, j) = 0 for j ≤ i + 1,
c(i, j) = min_{i < k < j} (w i k j + c(i, k) + c(k, j)) for i + 1 < j.
A split tree t : BinaryTree ℕ satisfying IsSplitTree i j t decomposes the interval [i, j]
(i < j) into atomic unit segments [a, a + 1] (leaves) by splitting each [a, b] at some
a < k < b. Shifting the right endpoint by one identifies IsSplitTree i j with
OptimalBST.IsBST i (j - 1) (isSplitTree_iff_isBST), and intervalOptCost specializes to
OptimalBST.optCost when w is independent of k (intervalOptCost_eq_optimalBST).
Reusing the table and scan infrastructure of CslibFrontier.Algorithms.Lean.OptimalBST.Knuth,
intervalDP computes intervalOptCost w 0 n and an attaining split tree intervalOptTree w 0 n
bottom-up by increasing interval length (intervalDP_eq).
Two classical clients instantiate the recurrence:
- Matrix-chain multiplication ([CLRS09], Section 15.2):
nmatricesA₁, …, AₙwhereAₜhas dimensionsdims (t - 1) × dims t, with split weightmatrixChainWeight dims i k j = dims i * dims k * dims j.isLeast_matrixChainCostproves thatintervalOptCost (matrixChainWeight dims) i jis the minimum scalar-multiplication cost over all full parenthesizations ofA_{i + 1} ⋯ A_j. - Optimal polygon triangulation: a convex
(j - i + 1)-gon on verticesi, …, j, where a split tree encodes its dual triangulation intoj - i - 1triangles (triangles,length_triangles) andj - i - 2pairwise non-crossing internal chords (diagonals,length_diagonals,diagonals_nonCrossing).isLeast_triangulationCostproves optimality for any triangle weightw : Triangle → β.
Unlike Knuth's weight for optimal binary search trees, general matrix-chain multiplication fails
both the quadrangle inequality (OptimalBST.QuadrangleIneq) on its optimal cost table and
monotonicity of optimal split points under every tie-breaking rule
(exists_matrixChain_not_quadrangleIneq_and_not_split_mono, witnessed by [1, 1, 2, 5, 1]).
Main definitions #
Cslib.Algorithms.Lean.IntervalDP.IsSplitTree: full binary split trees of[i, j](i < j).Cslib.Algorithms.Lean.IntervalDP.splitTreeCost: split-weighted cost of a split tree.Cslib.Algorithms.Lean.IntervalDP.intervalOptCost,intervalOptSplit,intervalOptTree: thek-dependent interval recurrence, largest optimal split, and attaining tree.Cslib.Algorithms.Lean.IntervalDP.intervalDP: bottom-up cubic dynamic program returning the optimal cost and tree.Cslib.Algorithms.Lean.IntervalDP.matrixChainWeight: scalar-multiplication split weightdims i * dims k * dims j.Cslib.Algorithms.Lean.IntervalDP.Triangle,triangles,diagonals,ChordsCross,triangulationCost: triangle decomposition, non-crossing internal chords, and polygon triangulation cost.
Main results #
Cslib.Algorithms.Lean.IntervalDP.isSplitTree_iff_isBST,Cslib.Algorithms.Lean.IntervalDP.intervalOptCost_eq_optimalBST: specialization toCslib.Algorithms.Lean.OptimalBST.Cslib.Algorithms.Lean.IntervalDP.isLeast_intervalOptCost,Cslib.Algorithms.Lean.IntervalDP.intervalDP_eq: optimality of the recurrence and correctness of the bottom-up dynamic program.Cslib.Algorithms.Lean.IntervalDP.isLeast_matrixChainCost: optimality for matrix-chain multiplication.Cslib.Algorithms.Lean.IntervalDP.length_triangles,Cslib.Algorithms.Lean.IntervalDP.length_diagonals,Cslib.Algorithms.Lean.IntervalDP.diagonals_nonCrossing,Cslib.Algorithms.Lean.IntervalDP.isLeast_triangulationCost: geometric validity and optimality for convex polygon triangulation.Cslib.Algorithms.Lean.IntervalDP.exists_matrixChain_not_quadrangleIneq_and_not_split_mono: checked counterexample where matrix-chain optimal cost failsQuadrangleIneqand every optimal split violates split monotonicity.
References #
Full binary split trees on [i, j] #
IsSplitTree i j t: t is a full binary split tree of the interval [i, j] (i < j),
whose leaves are unit segments [a, a + 1] and whose internal nodes split [a, b] at k
(a < k < b) into [a, k] and [k, b].
- leaf (i : ℕ) : IsSplitTree i (i + 1) BinaryTree.nil
- split {i j k : ℕ} {l r : BinaryTree ℕ} (hik : i < k) (hkj : k < j) : IsSplitTree i k l → IsSplitTree k j r → IsSplitTree i j (BinaryTree.node k l r)
Shifting the right endpoint by one identifies split trees on [i, j] with binary search trees
on the keys i + 1, …, j - 1.
Split-dependent interval recurrence #
Total split cost of a split tree t on [i, j]: leaves cost 0, and a node splitting
[a, b] at k charges w a k b plus the costs of its left and right subtrees.
For a k-independent interval weight, splitTreeCost agrees with OptimalBST.treeCost after
shifting the right endpoint by one.
Fuel-bounded evaluator for the k-dependent interval recurrence.
The optimal cost c(i, j) of the k-dependent interval recurrence
c(i, j) = min_{i < k < j} (w i k j + c(i, k) + c(k, j)), with c(i, j) = 0 for j ≤ i + 1.
The total cost of splitting [i, j] at k using optimal subproblems on [i, k] and
[k, j].
The recurrence equation for intervalOptCost.
The largest optimal split point of [i, j] (and 0 when j ≤ i + 1).
Build a split tree from a split-point table R with explicit fuel.
An optimal split tree on [i, j], read off the largest optimal splits.
The recurrence is at most the cost of every split tree on [i, j].
Optimality of the k-dependent interval recurrence: for i < j, intervalOptCost w i j
is the least cost over all split trees on [i, j], attained by intervalOptTree w i j.
Specialization to OptimalBST.optCost: for a k-independent interval weight,
intervalOptCost recovers OptimalBST.optCost with the right endpoint shifted by one.
Bottom-up cubic dynamic program #
Candidate split cost for [i, j] at k, read from a dynamic-programming table T.
Fill the diagonal of intervals [i, i + d] (d ≥ 2, i + d ≤ n), reading only T.
The bottom-up table after filling diagonals up to length d.
Bottom-up cubic dynamic program computing the optimal cost and an attaining split tree on
[0, n].
Correctness of the bottom-up interval DP: intervalDP w n computes intervalOptCost w 0 n
and intervalOptTree w 0 n.
Client 1: Matrix-chain multiplication (CLRS Section 15.2) #
Optimality of matrix-chain multiplication ([CLRS09], Section 15.2): for a chain of
matrices A_{i + 1}, …, A_j where Aₜ has dimensions dims (t - 1) × dims t, the interval
recurrence intervalOptCost (matrixChainWeight dims) i j is the least scalar-multiplication cost
over all full parenthesizations (IsSplitTree i j).
Client 2: Optimal polygon triangulation #
The list of triangles in the triangulation represented by split tree t on vertices
i, …, j: each internal split of [a, b] at k produces triangle ⟨a, k, b⟩.
Two chords (u₁, v₁) and (u₂, v₂) cross if their endpoints alternate strictly in cyclic
order.
The internal diagonals (chords strictly separating non-adjacent vertices) of the triangulation
represented by split tree t on vertices i, …, j.
A convex (j - i + 1)-gon triangulates into exactly j - i - 1 triangles.
A convex (j - i + 1)-gon triangulation has j - i - 2 internal diagonals.
Non-crossing property: no two internal diagonals of a split-tree triangulation cross.
Total weight of a polygon triangulation: the sum of triangle weights over triangles i j t.
Optimality of polygon triangulation: for any triangle weight w : Triangle → β,
intervalOptCost (fun a b c => w ⟨a, b, c⟩) i j is the least triangulation weight over all
split-tree triangulations of the convex (j - i + 1)-gon on vertices i, …, j.
Failure of quadrangle inequality and split monotonicity for matrix-chain multiplication #
Failure of Yao's quadrangle inequality and optimal-split monotonicity for matrix-chain
multiplication: there exists a dimension chain whose optimal cost table violates
OptimalBST.QuadrangleIneq and whose unique optimal splits satisfy K(0, 3) = 2 > 1 = K(0, 4),
so optimal split points are non-monotone under every tie-breaking rule.