Edit distance: the Wagner–Fischer algorithm #
The edit distance between two words under general letter costs, computed by the dynamic program of Wagner and Fischer (see also CLRS, Problem 15-5 and Kleinberg and Tardos, Section 6.6).
A cost function C : Costs α charges C.del a to delete a letter a, C.ins b to insert a
letter b, and C.sub a b to replace a by b (keeping a letter is the replacement sub a a,
of cost C.sub a a, usually 0). An edit script is a list of such operations; read left to
right, it consumes its source word and produces its target word. A script is the same thing as
an alignment of the two words, or a trace in the terminology of Wagner and Fischer: every letter
of the source is deleted or replaced exactly once, and every letter of the target is inserted or is
the result of exactly one replacement, in order.
Cslib.Algorithms.Lean.EditDistance.editDist C x y is the Wagner–Fischer table entry for x and
y, and Cslib.Algorithms.Lean.EditDistance.isLeast_editDist proves that it is the least cost of a
script from x to y. Cslib.Algorithms.Lean.EditDistance.optScript reads an optimal script off
the table.
Scope #
Costs are natural numbers and arbitrary: no triangle inequality or symmetry is assumed. The
theorem is about scripts (alignments, traces). Wagner and Fischer also consider sequences of edit
operations applied one after another at arbitrary positions, and show that under their cost
conditions the least cost of such a sequence equals the least cost of a trace.
CslibFrontier.Algorithms.Lean.EditDistance.Unit proves this equivalence for unit costs and derives
the Levenshtein metric laws. The weighted sequence equivalence remains open.
Mathlib used to have Levenshtein.Cost and levenshtein (Mathlib/Data/List/EditDistance), with
the same three cost fields over separate source and target alphabets and the same recurrence, but
no characterization as a least cost over scripts. That API was removed from Mathlib in
leanprover-community/mathlib4#29196 and is absent from the pinned Mathlib, so nothing here bridges
to it.
Main definitions #
Cslib.Algorithms.Lean.EditDistance.Costs: letter costs;Costs.unit(Levenshtein) andCosts.indel(1per insertion or deletion,2per replacement by a different letter).Cslib.Algorithms.Lean.EditDistance.EditOp,EditScript: edit operations and scripts, withEditScript.src,EditScript.tgtandEditScript.cost.Cslib.Algorithms.Lean.EditDistance.editDist: the Wagner–Fischer table entry.Cslib.Algorithms.Lean.EditDistance.optScript: an optimal script, by traceback.
Main results #
Cslib.Algorithms.Lean.EditDistance.editDist_le_cost: every script fromxtoycosts at leasteditDist C x y.Cslib.Algorithms.Lean.EditDistance.src_optScript,tgt_optScript,cost_optScript:optScript C x yis a script fromxtoyof costeditDist C x y.Cslib.Algorithms.Lean.EditDistance.isLeast_editDist:editDist C x yis the least cost of a script fromxtoy.Cslib.Algorithms.Lean.EditDistance.editDist_le_sum_del_ins,editDist_unit_le_add: upper bounds from deletingxand insertingy.
Word-RAM running-time bounds for Wagner–Fischer edit distance are proved in
CslibFrontier.Algorithms.WordRAM.EditDistance.
References #
Unit costs: every deletion, insertion and replacement by a different letter costs 1, and
keeping a letter is free. The resulting distance is the Levenshtein distance.
Insertion and deletion costs: 1 per insertion or deletion, and 2 for replacing a letter by
a different one, the cost of a deletion followed by an insertion; keeping a letter is free.
Replacements are not forbidden, and optScript Costs.indel may return them, but splitting each
into a deletion and an insertion keeps the cost, so the distance is the insertion–deletion distance
(Cslib.Algorithms.Lean.EditDistance.isLeast_editDist_indel).
The letters an operation consumes from the source word.
The letters an operation produces in the target word.
An edit script: a list of edit operations, read left to right.
The source word of a script: the letters it consumes, in order.
The target word of a script: the letters it produces, in order.
The cost of a script: the sum of the costs of its operations.
Under unit costs, the edit distance is at most |x| + |y|.
Every script from x to y costs at least editDist C x y.
The traceback for a nonempty source and target: given the optimal scripts d, u, l for
(x, y), (x, b :: y) and (a :: x, y), extend the cheapest of them. Ties prefer a replacement,
then a deletion.
An optimal script from x to y, read off the Wagner–Fischer table. This is a
specification for proofs and decide: each choice consults the entries editDist C x' y' directly
instead of a stored table, so it is not the linear-time traceback, which is not formalized.
The scripts from a :: x, given the scripts from x.
Wagner–Fischer. The table entry editDist C x y is the least cost of an edit script (an
alignment, or trace) from x to y.
Splitting an edit script at a chosen source boundary s.src = u ++ v produces two scripts
whose sources are u and v, whose targets concatenate to s.tgt, and whose costs sum to
s.cost C.