The minimum edit distance between two strings is the minimum number of editing operations Insertion
Deletion
Substitution
needed to transform one into other.
Defining Min Edit Distance
For two Strings:
- X of length
n - Y of length
m
We define D(i,j)
- the edit distance between X[1..i] and Y[1..j], i.e. the first i characters of
X and the first j characters ofY . - the edit distance between X and
Y is thus D(n,m).
Computing Min Edit Distance (Levenshtein)
- Initialization
D(i,0)=i, D(0,j)=j Recurrence Relation:
for each i=i…M
for each j=1…N
D(i,j)=⎧⎩⎨⎪⎪⎪⎪⎪⎪D(i−1,j)+1D(i,j−1)+1D(i−1,j−1)+{20if X(i)≠Y(j)if X(i)=Y(j)Termination:
D(N,M) is the distance.