Documentation

CslibFrontier.Algorithms.Lean.IntervalDP.Clients

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:

  1. Matrix-chain multiplication ([CLRS09], Section 15.2): n matrices A₁, …, Aₙ where Aₜ has dimensions dims (t - 1) × dims t, with split weight matrixChainWeight dims i k j = dims i * dims k * dims j. isLeast_matrixChainCost proves that intervalOptCost (matrixChainWeight dims) i j is the minimum scalar-multiplication cost over all full parenthesizations of A_{i + 1} ⋯ A_j.
  2. Optimal polygon triangulation: a convex (j - i + 1)-gon on vertices i, …, j, where a split tree encodes its dual triangulation into j - i - 1 triangles (triangles, length_triangles) and j - i - 2 pairwise non-crossing internal chords (diagonals, length_diagonals, diagonals_nonCrossing). isLeast_triangulationCost proves optimality for any triangle weight w : 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 #

Main results #

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].

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 #

def Cslib.Algorithms.Lean.IntervalDP.splitTreeCost {β : Type u_1} [AddCommMonoid β] (w : ℕ → ℕ → ℕ → β) :
ℕ → ℕ → BinaryTree ℕ → β

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.

theorem Cslib.Algorithms.Lean.IntervalDP.splitTreeCost_eq_treeCost {β : Type u_1} [AddCommMonoid β] (w : ℕ → ℕ → β) {i j : ℕ} {t : BinaryTree ℕ} (h : IsSplitTree i j t) :
splitTreeCost (fun (a x b : ℕ) => w a (b - 1)) i j t = OptimalBST.treeCost w i (j - 1) t

For a k-independent interval weight, splitTreeCost agrees with OptimalBST.treeCost after shifting the right endpoint by one.

def Cslib.Algorithms.Lean.IntervalDP.intervalOptCostAux {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) :
ℕ → ℕ → ℕ → β

Fuel-bounded evaluator for the k-dependent interval recurrence.

def Cslib.Algorithms.Lean.IntervalDP.intervalOptCost {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) (i j : ℕ) :
β

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.

def Cslib.Algorithms.Lean.IntervalDP.intervalSplitCost {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) (i j k : ℕ) :
β

The total cost of splitting [i, j] at k using optimal subproblems on [i, k] and [k, j].

theorem Cslib.Algorithms.Lean.IntervalDP.intervalOptCost_of_le {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) {i j : ℕ} (h : j ≤ i + 1) :
theorem Cslib.Algorithms.Lean.IntervalDP.intervalOptCost_eq {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) {i j : ℕ} (h : i + 1 < j) :

The recurrence equation for intervalOptCost.

def Cslib.Algorithms.Lean.IntervalDP.intervalOptSplit {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) (i j : ℕ) :

The largest optimal split point of [i, j] (and 0 when j ≤ i + 1).

theorem Cslib.Algorithms.Lean.IntervalDP.intervalOptSplit_spec {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) {i j : ℕ} (h : i + 1 < j) :
theorem Cslib.Algorithms.Lean.IntervalDP.intervalOptSplit_mem {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) {i j : ℕ} (h : i + 1 < j) :
theorem Cslib.Algorithms.Lean.IntervalDP.le_intervalOptSplit {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) {i j k : ℕ} (hk : k ∈ Finset.Ioo i j) (hopt : ∀ k' ∈ Finset.Ioo i j, intervalSplitCost w i j k ≤ intervalSplitCost w i j k') :
theorem Cslib.Algorithms.Lean.IntervalDP.intervalOptSplit_of_le {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) {i j : ℕ} (h : 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.

theorem Cslib.Algorithms.Lean.IntervalDP.isSplitTree_intervalOptTree {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) {i j : ℕ} (hij : i < j) :
theorem Cslib.Algorithms.Lean.IntervalDP.splitTreeCost_intervalOptTree {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) {i j : ℕ} (hij : i < j) :
theorem Cslib.Algorithms.Lean.IntervalDP.intervalOptCost_le {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) {i j k : ℕ} (hk : k ∈ Finset.Ioo i j) :

The recurrence is at most the cost of every split tree on [i, j].

theorem Cslib.Algorithms.Lean.IntervalDP.isLeast_intervalOptCost {β : Type u_1} [AddCommMonoid β] [LinearOrder β] [IsOrderedAddMonoid β] (w : ℕ → ℕ → ℕ → β) {i j : ℕ} (hij : i < j) :
IsLeast {c : β | ∃ (t : BinaryTree ℕ), IsSplitTree i j t ∧ splitTreeCost w i j t = c} (intervalOptCost w 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.

theorem Cslib.Algorithms.Lean.IntervalDP.intervalOptCost_eq_optimalBST {β : Type u_1} [AddCommMonoid β] [LinearOrder β] [IsOrderedAddMonoid β] (w : ℕ → ℕ → β) (i j : ℕ) :
intervalOptCost (fun (a x b : ℕ) => w a (b - 1)) i j = OptimalBST.optCost w i (j - 1)

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 #

def Cslib.Algorithms.Lean.IntervalDP.intervalCand {β : Type u_1} [AddCommMonoid β] (w : ℕ → ℕ → ℕ → β) (T : OptimalBST.Table β) (i j k : ℕ) :
β

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].

theorem Cslib.Algorithms.Lean.IntervalDP.intervalDP_eq {β : Type u_1} [AddCommMonoid β] [LinearOrder β] (w : ℕ → ℕ → ℕ → β) (n : ℕ) :
intervalDP w n = { cost := intervalOptCost w 0 n, tree := intervalOptTree w 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) #

Scalar-multiplication cost of multiplying a dims i × dims k matrix by a dims k × dims j matrix.

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 #

A triangle (a, b, c) in a triangulation of a convex polygon with ordered vertices a < b < c.

  • a : ℕ

    First vertex index.

  • b : ℕ

    Apex (middle) vertex index.

  • c : ℕ

    Last vertex index.

theorem Cslib.Algorithms.Lean.IntervalDP.Triangle.ext {x y : Triangle} (a : x.a = y.a) (b : x.b = y.b) (c : x.c = y.c) :
x = y

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⟩.

An internal chord (u, v) (u + 1 < v) of a convex polygon triangulation.

  • u : ℕ

    Smaller endpoint index.

  • v : ℕ

    Larger endpoint index.

theorem Cslib.Algorithms.Lean.IntervalDP.Chord.ext {x y : Chord} (u : x.u = y.u) (v : x.v = y.v) :
x = y

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.

theorem Cslib.Algorithms.Lean.IntervalDP.mem_triangles_bounds {i j : ℕ} {t : BinaryTree ℕ} (h : IsSplitTree i j t) {T : Triangle} (hT : T ∈ triangles i j t) :
i ≤ T.a ∧ T.a < T.b ∧ T.b < T.c ∧ T.c ≤ j

Every triangle of a split tree on [i, j] has strictly ordered vertices inside [i, j].

A convex (j - i + 1)-gon triangulation has j - i - 2 internal diagonals.

theorem Cslib.Algorithms.Lean.IntervalDP.mem_diagonals_bounds {i j : ℕ} {t : BinaryTree ℕ} (h : IsSplitTree i j t) {e : Chord} (he : e ∈ diagonals i j t) :
i ≤ e.u ∧ e.u + 1 < e.v ∧ e.v ≤ j

Every internal diagonal of a split tree on [i, j] connects non-adjacent vertices inside [i, j].

theorem Cslib.Algorithms.Lean.IntervalDP.diagonals_nonCrossing {i j : ℕ} {t : BinaryTree ℕ} (h : IsSplitTree i j t) (e₁ : Chord) :
e₁ ∈ diagonals i j t → ∀ e₂ ∈ diagonals i j t, ¬ChordsCross e₁ e₂

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.

theorem Cslib.Algorithms.Lean.IntervalDP.triangulationCost_eq_splitTreeCost {β : Type u_1} [AddCommMonoid β] (w : Triangle → β) (i j : ℕ) (t : BinaryTree ℕ) :
triangulationCost w i j t = splitTreeCost (fun (a b c : ℕ) => w { a := a, b := b, c := c }) i j t
theorem Cslib.Algorithms.Lean.IntervalDP.isLeast_triangulationCost {β : Type u_1} [AddCommMonoid β] [LinearOrder β] [IsOrderedAddMonoid β] (w : Triangle → β) {i j : ℕ} (hij : i < j) :
IsLeast {c : β | ∃ (t : BinaryTree ℕ), IsSplitTree i j t ∧ triangulationCost w i j t = c} (intervalOptCost (fun (a b c : ℕ) => w { a := a, b := b, c := c }) i j)

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.