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 #
Cslib.Algorithms.Lean.EditDistance.lcsLength: the table entry.Cslib.Algorithms.Lean.EditDistance.lcs: a longest common subsequence, by traceback.Cslib.Algorithms.Lean.EditDistance.EditScript.kept: the letters a script keeps.Cslib.Algorithms.Lean.EditDistance.EditScript.IsIndel: a script with no replacement by a different letter;EditScript.splitSubsturns a script into one.
Main results #
Cslib.Algorithms.Lean.EditDistance.length_le_lcsLength: every common subsequence has length at mostlcsLength x y(lcsLength_le_leftandlcsLength_le_rightboundlcsLength x yby|x|and|y|).Cslib.Algorithms.Lean.EditDistance.lcs_spec:lcs x yis a common subsequence of lengthlcsLength x y.Cslib.Algorithms.Lean.EditDistance.isGreatest_lcsLength:lcsLength x yis the greatest length of a common subsequence.Cslib.Algorithms.Lean.EditDistance.editDist_indel_add_two_mul:editDist Costs.indel x y + 2 · lcsLength x y = |x| + |y|.Cslib.Algorithms.Lean.EditDistance.isLeast_editDist_indel:editDist Costs.indel x yis the least cost of a script fromxtoywith no replacement by a different letter.
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.
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.
The longest common subsequences of a :: x, given those of x.
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.
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.
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.