Example Worked Example: Longest Common Subsequence DP Grid
example formalized

Worked Example: Longest Common Subsequence DP Grid

Longest common subsequence in CostM (Visited (ℕ × ℕ)): the cost footprint is now a set of 2-D grid cells (i, j), one per (prefix of xs, prefix of ys) subproblem. The memoization footprint is contained in the full DP grid, $$ (\operatorname{lcs}\,xs\,ys).\mathrm{cost} \;\subseteq\; \{0,\dots,|xs|\} \times \{0,\dots,|ys|\}, $$ which is lcs_cost_subset; taking cardinalities gives the polynomial-space bound $$ (\operatorname{lcs}\,xs\,ys).\mathrm{cost}.\mathrm{toFinset}.\mathrm{card} \;\le\; (|xs|+1)\,(|ys|+1), $$ i.e. lcs_cost_card_le. This is the canonical polynomial-space DP example — the 2-D analogue of Worked Example: Memoized Fibonacci Footprint.

lcs uses the bare CostM.tick form (rather than the macro) at its footprint-recording points, since each tick records a specific grid cell Visited.singleton (i, j) rather than a unit cost.

Lean declarations

  • LCS.lcs : List α → List α → CostM (Visited (ℕ × ℕ)) ℕ.
  • LCS.lcs_cost_subset — footprint the DP grid (via a private range_prod_mono monotonicity lemma).
  • LCS.lcs_cost_card_le — the (|xs|+1)(|ys|+1) cell-count bound.

References

Also in