Damerau–Levenshtein distance

In information theory and computer science, the Damerau–Levenshtein distance (named after Frederick J. Damerau and Vladimir I. Levenshtein[1][2][3]) is a distance (string metric) between two strings, i.e., finite sequence of symbols, given by counting the minimum number of operations needed to transform one string into the other, where an operation is defined as an insertion, deletion, or substitution of a single character, or a transposition of two adjacent characters. In his seminal paper,[4] Damerau not only distinguished these four edit operations but also stated that they correspond to more than 80% of all human misspellings. Damerau's paper considered only misspellings that could be corrected with at most one edit operation.

The Damerau–Levenshtein distance differs from the classical Levenshtein distance by including transpositions among its allowable operations. The classical Levenshtein distance only allows insertion, deletion, and substitution operations.[5] Modifying this distance by including transpositions of adjacent symbols produces a different distance measure, known as the Damerau–Levenshtein distance.[2]

While the original motivation was to measure distance between human misspellings to improve applications such as spell checkers, Damerau–Levenshtein distance has also seen uses in biology to measure the variation between DNA.[6]

Presented here are two algorithms: the first,[7] simpler one, computes what is known as the optimal string alignment[citation needed] (sometimes called the restricted edit distance[citation needed]), while the second one[8] computes the Damerau–Levenshtein distance with adjacent transpositions. Adding transpositions adds significant complexity. The difference between the two algorithms consists in that the optimal string alignment algorithm computes the number of edit operations needed to make the strings equal under the condition that no substring is edited more than once, whereas the second one presents no such restriction.

Take for example the edit distance between CA and ABC. The Damerau–Levenshtein distance LD(CA,ABC) = 2 because CA → AC → ABC, but the optimal string alignment distance OSA(CA,ABC) = 3 because if the operation CA → AC is used, it is not possible to use AC → ABC because that would require the substring to be edited more than once, which is not allowed in OSA, and therefore the shortest sequence of operations is CA → A → AB → ABC. Note that for the optimal string alignment distance, the triangle inequality does not hold: OSA(CA,AC) + OSA(AC,ABC) < OSA(CA,ABC), and so it is not a true metric.

Firstly, let us consider a direct extension of the formula used to calculate Levenshtein distance. Below is pseudocode for a function OptimalStringAlignmentDistance that takes two strings, str1 of length lenStr1, and str2 of length lenStr2, and computes the optimal string alignment distance between them:

intOptimalStringAlignmentDistance(charstr1[1..lenStr1],charstr2[1..lenStr2])// d is a table with lenStr1+1 rows and lenStr2+1 columnsdeclareintd[0..lenStr1,0..lenStr2]// i and j are used to iterate over str1 and str2declareinti,j,cost// for loop is inclusive, need table 1 row/column larger than string lengthforifrom0tolenStr1d[i,0]:=iforjfrom1tolenStr2d[0,j]:=j// pseudo-code assumes string indices start at 1, not 0// if implemented, make sure to start comparing at 1st letter of stringsforifrom1tolenStr1forjfrom1tolenStr2ifstr1[i]=str2[j]thencost:=0elsecost:=1d[i,j]:=minimum(d[i-1,j]+1,// deletiond[i,j-1]+1,// insertiond[i-1,j-1]+cost// substitution)if(i>1andj>1andstr1[i]=str2[j-1]andstr1[i-1]=str2[j])thend[i,j]:=minimum(d[i,j],d[i-2,j-2]+cost// transposition)returnd[lenStr1,lenStr2]

Here is the second algorithm that computes the true Damerau–Levenshtein distance with adjacent transpositions (ActionScript 3.0); this function requires as an additional parameter the size of the alphabet (C), so that all entries of the arrays are in 0..(C−1):

staticpublicfunction damerauLevenshteinDistance(a:Array,b:Array,C:uint):uint{// "infinite" distance is just the max possible distancevarINF:uint=a.length+b.length;// make and initialize the character array indicesvarDA:Array=newArray(C);for(vark:uint=0;k<C;++k)DA[k]=0;// make the distance matrix H[-1..a.length][-1..b.length]varH:matrix=newmatrix(a.length+2,b.length+2);// initialize the left and top edges of HH[-1][-1]=INF;for(vari:uint=0;i<=a.length;++i){H[i][-1]=INF;H[i][0]=i;}for(varj:uint=0;j<=b.length;++j){H[-1][j]=INF;H[0][j]=j;}// fill in the distance matrix H// look at each character in afor(vari:uint=1;i<=a.length;++i){varDB:uint=0;// look at each character in bfor(varj:uint=1;j<=b.length;++j){vari1:uint=DA[b[j-1]];varj1:uint=DB;varcost:uint;if(a[i-1]==b[j-1]){cost=0;DB=j;}elsecost=1;H[i][j]=Math.min(H[i-1][j-1]+cost,// substitutionH[i][j-1]+1,// insertionH[i-1][j]+1,// deletionH[i1-1][j1-1]+(i-i1-1)+1+(j-j1-1));}DA[a[i-1]]=i;}returnH[a.length][b.length];}

Note: the algorithm given in the paper uses alphabet 1..C rather than the 0..C−1 used here; the paper indexes arrays: H[−1..|A|,−1..|B|] and DA[1..C]; here DA[0..C−1] is used; the paper seems to be missing the necessary line H[−1,−1] = INF

To devise a proper algorithm to calculate unrestricted Damerau–Levenshtein distance note that there always exists an optimal sequence of edit operations, where once-transposed letters are never modified afterwards. (This holds as long as the cost of a transposition, , is at least the average of the cost of an insertion and deletion, i.e., .[8]) Thus, we need to consider only two symmetric ways of modifying a substring more than once: (1) transpose letters and insert an arbitrary number of characters between them, or (2) delete a sequence of characters and transpose letters that become adjacent after deletion. The straightforward implementation of this idea gives an algorithm of cubic complexity: , where M and N are string lengths. Using the ideas of Lowrance and Wagner,[8] this naive algorithm can be improved to be in the worst case.

It is interesting that the bitap algorithm can be modified to process transposition. See the information retrieval section of[1] for an example of such an adaptation.

Damerau–Levenshtein distance plays an important role in natural language processing. In natural languages, strings are short and the number of errors (misspellings) rarely exceeds 2. In such circumstances, restricted and real edit distance differ very rarely. Oommen and Loke[2] even mitigated the limitation of the restricted edit distance by introducing generalized transpositions. Nevertheless, one must remember that the restricted edit distance usually does not satisfy the triangle inequality and, thus, cannot be used with metric trees.

Since DNA frequently undergoes insertions, deletions, substitutions, and transpositions, and each of these operations occurs on approximately the same timescale, the Damerau–Levenshtein distance is an appropriate metric of the variation between two strands of DNA. More common in DNA, protein, and other bioinformatics related alignment tasks is the use of closely related algorithms such as Needleman–Wunsch algorithm or Smith–Waterman algorithm.

The algorithm can be used with any set of words, like vendor names. Since entry is manual by nature there is a risk of entering a false vendor. A fraudster employee may enter one real vendor such as "Rich Heir Estate Services" versus a false vendor "Rich Hier State Services". The fraudster would then create a false bank account and have the company route checks to the real vendor and false vendor. The Damerau–Levenshtein algorithm will detect the transposed and dropped letter and bring attention of the items to a fraud examiner.