Documentation

CslibFrontier.Algorithms.Lean.EditDistance.Grid

Dynamic programs over pairs of suffixes #

The common skeleton of the Wagner–Fischer edit-distance program Wagner and Fischer and the longest-common-subsequence program CLRS, Section 15.4. Both fill a table indexed by pairs of suffixes of two words x and y. The entry for two empty suffixes is a constant. An entry with one empty suffix depends on its neighbour along that border. Every other entry depends on three neighbours: the entry dropping the first letter of both suffixes, the entry dropping only the first letter of the suffix of x, and the entry dropping only the first letter of the suffix of y. (The textbook tables are indexed by prefixes; indexing by suffixes is the same program run on the reversed words, and it lets the recursion follow the list structure.)

A GridDP records the four rules. GridDP.value D x y is the entry for the whole words, computed row by row: GridDP.row D y x lists the entries for x against every suffix of y, longest first, and the row for a :: x is computed from the row for x alone (GridDP.stepRow). This is the usual two-row scheme, in which only the previous row is kept. Checked Word-RAM running-time bounds for the concrete edit-distance and longest-common-subsequence instances live in CslibFrontier.Algorithms.WordRAM.EditDistance.

Main definitions #

Main results #

References #

structure Cslib.Algorithms.Lean.EditDistance.GridDP (α : Type u_1) (β : Type u_2) :
Type (max u_1 u_2)

The rules of a dynamic program over pairs of suffixes of two words over α, with entries in β. The entry for (a :: x, b :: y) is consCons a b d u l, where d, u and l are the entries for (x, y), (x, b :: y) and (a :: x, y).

  • nilNil : β

    The entry for two empty words.

  • consNil : α → β → β

    The entry for (a :: x, []) from the entry for (x, []).

  • nilCons : α → β → β

    The entry for ([], b :: y) from the entry for ([], y).

  • consCons : α → α → β → β → β → β

    The entry for (a :: x, b :: y) from the entries for (x, y), (x, b :: y) and (a :: x, y).

def Cslib.Algorithms.Lean.EditDistance.GridDP.nilRow {α : Type u_1} {β : Type u_2} (D : GridDP α β) :
List α → List β

The row of the empty word: the entries for ([], y'), for every suffix y' of y, longest first.

def Cslib.Algorithms.Lean.EditDistance.GridDP.stepRow {α : Type u_1} {β : Type u_2} (D : GridDP α β) (a : α) :
List α → List β → List β

The row of a :: x computed from the row r of x: the entries for (a :: x, y'), for every suffix y' of y, longest first.

def Cslib.Algorithms.Lean.EditDistance.GridDP.row {α : Type u_1} {β : Type u_2} (D : GridDP α β) (y : List α) :
List α → List β

The row of x: the entries for (x, y'), for every suffix y' of y, longest first.

def Cslib.Algorithms.Lean.EditDistance.GridDP.value {α : Type u_1} {β : Type u_2} (D : GridDP α β) (x y : List α) :
β

The entry for the whole words x and y.

theorem Cslib.Algorithms.Lean.EditDistance.GridDP.tail_nilRow {α : Type u_1} {β : Type u_2} (D : GridDP α β) (b : α) (y : List α) :
(D.nilRow (b :: y)).tail = D.nilRow y
theorem Cslib.Algorithms.Lean.EditDistance.GridDP.tail_stepRow {α : Type u_1} {β : Type u_2} (D : GridDP α β) (a b : α) (y : List α) (r : List β) :
(D.stepRow a (b :: y) r).tail = D.stepRow a y r.tail
theorem Cslib.Algorithms.Lean.EditDistance.GridDP.tail_row {α : Type u_1} {β : Type u_2} (D : GridDP α β) (b : α) (y x : List α) :
(D.row (b :: y) x).tail = D.row y x

Dropping the head of the row of x against b :: y gives the row of x against y.

@[simp]
theorem Cslib.Algorithms.Lean.EditDistance.GridDP.length_row {α : Type u_1} {β : Type u_2} (D : GridDP α β) (y x : List α) :
(D.row y x).length = y.length + 1

A row contains one entry for each suffix, including the empty suffix.

theorem Cslib.Algorithms.Lean.EditDistance.GridDP.getElem_row {α : Type u_1} {β : Type u_2} (D : GridDP α β) (y x : List α) (k : ℕ) (hk : k < (D.row y x).length) :
(D.row y x)[k] = D.value x (List.drop k y)

Column k holds the value for the suffix starting at position k.

@[simp]
theorem Cslib.Algorithms.Lean.EditDistance.GridDP.value_cons_nil {α : Type u_1} {β : Type u_2} (D : GridDP α β) (a : α) (x : List α) :
D.value (a :: x) [] = D.consNil a (D.value x [])
theorem Cslib.Algorithms.Lean.EditDistance.GridDP.value_nil_cons {α : Type u_1} {β : Type u_2} (D : GridDP α β) (b : α) (y : List α) :
D.value [] (b :: y) = D.nilCons b (D.value [] y)
theorem Cslib.Algorithms.Lean.EditDistance.GridDP.value_cons_cons {α : Type u_1} {β : Type u_2} (D : GridDP α β) (a b : α) (x y : List α) :
D.value (a :: x) (b :: y) = D.consCons a b (D.value x y) (D.value x (b :: y)) (D.value (a :: x) y)