Wagner–Fischer algorithm
In computer science, the Wagner–Fischer algorithm is a dynamic programming algorithm that computes the edit distance between two strings of characters.
History
The Wagner–Fischer algorithm has a history of multiple invention. Navarro lists the following inventors of it, with date of publication, and acknowledges that the list is incomplete:- Vintsyuk, 1968
- Needleman and Wunsch, 1970
- Sankoff, 1972
- Sellers, 1974
- Wagner and Fischer, 1974
- Lowrance and Wagner, 1975
- P. Pletyuhin, 1996
Calculating distance
The Wagner–Fischer algorithm computes edit distance based on the observation that if we reserve a matrix to hold the edit distances between all prefixes of the first string and all prefixes of the second, then we can compute the values in the matrix by flood filling the matrix, and thus find the distance between the two full strings as the last value computed.A straightforward implementation, as pseudocode for a function Distance that takes two strings, s of length m, and t of length n, and returns the Levenshtein distance between them, looks as follows. The input strings are one-indexed, while the matrix d is zero-indexed, and
is a closed range.function Distance:
// for all i and j, d will hold the distance between
// the first i characters of s and the first j characters of t
// note that d has * values
declare int d
set each element in d to zero
// source prefixes can be transformed into empty string by
// dropping all characters
for i from 1 to m:
d := i
// target prefixes can be reached from empty source prefix
// by inserting every character
for j from 1 to n:
d := j
for j from 1 to n:
for i from 1 to m:
if s = t:
substitutionCost := 0
else:
substitutionCost := 1
d := minimum // substitution
return d
Two examples of the resulting matrix :
The invariant maintained throughout the algorithm is that we can transform the initial segment s into t using a minimum of d operations. At the end, the bottom-right element of the array contains the answer.Proof of correctnessAs mentioned earlier, the invariant is that we can transform the initial segments into t using a minimum of d operations. This invariant holds since:
d is in fact minimal; this is more difficult to show, and involves an argument by contradiction in which we assume d is smaller than the minimum of the three, and use this to show one of the three is not minimal.Possible modificationsPossible modifications to this algorithm include:
Seller's variant for string searchBy initializing the first row of the matrix with zeros, we obtain a variant of the Wagner–Fischer algorithm that can be used for fuzzy string search of a string in a text. This modification gives the end-position of matching substrings of the text. To determine the start-position of the matching substrings, the number of insertions and deletions can be stored separately and used to compute the start-position from the end-position.The resulting algorithm is by no means efficient, but was at the time of its publication one of the first algorithms that performed approximate search. |