Edit distance finds the number of insertion, deletion or substitutions required to one string to another. I want to to also include swaps in this algorithm. For example "apple" and "appel" should give a edit distance of 1.
Asked
Active
Viewed 4,790 times
2 Answers
9
The edit distance that you are defining is called the Damerau–Levenshtein distance. You can find possible implementations on the Wikipedia page.

a3nm
- 8,717
- 6
- 31
- 39
-1
See the algorithm here.
http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Dynamic/Edit/
You can give different costs for swap, add, deletions.
m[i,j] = min(m[i-1,j-1]
+ if s1[i]=s2[j] then 0 else cost_swap fi,
m[i-1, j] + cost_insert,
m[i, j-1] + cost_delete ), i=1..|s1|, j=1..|s2|

Rajendran T
- 1,513
- 10
- 15
-
1What you have answered is substitution not swaps. In my example given above in the second string swapping "el" gives "le" and thus matches the first string – Raja Roy Jan 09 '12 at 03:49