Documentation

CslibFrontier.Algorithms.Lean.EditDistance.LCS

Longest common subsequence #

The length of a longest common subsequence of two words, computed by the dynamic program of CLRS, Section 15.4 (the same table as Wagner and Fischer, with letter matches rewarded instead of operations charged). A subsequence is a sublist in the sense of List.Sublist: a word obtained by deleting letters, not necessarily consecutive.

Cslib.Algorithms.Lean.EditDistance.lcsLength x y is the table entry, and Cslib.Algorithms.Lean.EditDistance.isGreatest_lcsLength proves that it is the greatest length of a common subsequence; Cslib.Algorithms.Lean.EditDistance.lcs reads one off the table. The two problems are dual: under Costs.indel (1 per insertion or deletion, 2 per replacement by a different letter) the edit distance is |x| + |y| - 2 · lcsLength x y (Cslib.Algorithms.Lean.EditDistance.editDist_indel_add_two_mul). Replacements by a different letter are allowed, and optScript Costs.indel may return them, but splitting each into a deletion and an insertion keeps the cost, so this is also the least cost of a script without them (Cslib.Algorithms.Lean.EditDistance.isLeast_editDist_indel): the insertion–deletion distance.

Main definitions #

Main results #

Word-RAM running-time bounds for longest-common-subsequence length are proved in CslibFrontier.Algorithms.WordRAM.EditDistance.

References #

The longest-common-subsequence table, over pairs of suffixes.

The length of a longest common subsequence of x and y: the table entry.

theorem Cslib.Algorithms.Lean.EditDistance.lcsLength_cons_cons {α : Type u_1} [DecidableEq α] (a b : α) (x y : List α) :
lcsLength (a :: x) (b :: y) = if a = b then lcsLength x y + 1 else max (lcsLength x (b :: y)) (lcsLength (a :: x) y)

The recurrence of CLRS, Theorem 15.1.

Every common subsequence of x and y has length at most lcsLength x y.

A longest common subsequence of x and y, read off the table. Ties prefer dropping the first letter of x. This is a specification for proofs and decide: each choice consults the entries lcsLength x' y' directly instead of a stored table, so it is not the linear-time traceback, which is not formalized.

def Cslib.Algorithms.Lean.EditDistance.lcs.lcsAux {α : Type u_1} [DecidableEq α] (a : α) (x : List α) (f : List α → List α) :
List α → List α

The longest common subsequences of a :: x, given those of x.

theorem Cslib.Algorithms.Lean.EditDistance.lcs_cons_cons {α : Type u_1} [DecidableEq α] (a b : α) (x y : List α) :
lcs (a :: x) (b :: y) = if a = b then a :: lcs x y else if lcsLength (a :: x) y ≤ lcsLength x (b :: y) then lcs x (b :: y) else lcs (a :: x) y
theorem Cslib.Algorithms.Lean.EditDistance.lcs_spec {α : Type u_1} [DecidableEq α] (x y : List α) :
(lcs x y).Sublist x ∧ (lcs x y).Sublist y ∧ (lcs x y).length = lcsLength x y

lcs x y is a common subsequence of x and y of length lcsLength x y.

lcsLength x y is the greatest length of a common subsequence of x and y.

Insertion–deletion distance #

The letters a script keeps: the targets of its replacements of a letter by itself.

Under Costs.indel, a script costs |src| + |tgt| less twice the number of letters it keeps.

A script with no replacement by a different letter: each operation is an insertion, a deletion, or keeps a letter.

theorem Cslib.Algorithms.Lean.EditDistance.EditScript.isIndel_cons {α : Type u_1} {o : EditOp α} {s : EditScript α} :
IsIndel (o :: s) ↔ (∀ (a b : α), o = EditOp.sub a b → a = b) ∧ s.IsIndel

Split each replacement by a different letter into a deletion followed by an insertion.

Under Costs.indel, splitting replacements keeps the cost.

Deleting every letter of x and inserting every letter of y.

A common subsequence z of x and y gives a script keeping z.

Under Costs.indel, the edit distance is |x| + |y| - 2 · lcsLength x y. By isLeast_editDist_indel, it is also the least number of insertions and deletions in a script from x to y.

Under Costs.indel, replacements by a different letter gain nothing: editDist Costs.indel x y is the least cost of a script from x to y with no such replacement, that is, the least number of insertions and deletions turning x into y. The witness splits the replacements of optScript Costs.indel x y.