I'm trying to understand the Wagner–Fischer algorithm for finding distance between to strings. I'm looking through a pseudocode of it in the following link: http://en.wikipedia.org/wiki/Wagner%E2%80%93Fischer_algorithm
int EditDistance(char s[1..m], char t[1..n])
// For all i and j, d[i,j] will hold the Levenshtein distance between
// the first i characters of s and the first j characters of t.
// Note that d has (m+1) x(n+1) values.
let d be a 2-d array of int with dimensions [0..m, 0..n]
for i in [0..m]
d[i, 0] ← i // the distance of any first string to an empty second string
for j in [0..n]
d[0, j] ← j // the distance of any second string to an empty first string
for j in [1..n]
for i in [1..m]
if s[i] = t[j] then
d[i, j] ← d[i-1, j-1] // no operation required
else
d[i, j] ← minimum of
(
d[i-1, j] + 1, // a deletion
d[i, j-1] + 1, // an insertion
d[i-1, j-1] + 1 // a substitution
)
return d[m,n]
Actually I get the algorithm's point and understand it intuitively, by it's not obvious for me why are d[i-1, j] + 1, d[i, j-1] + 1 and d[i-1, j-1] + 1 considered as a deletion, insertion and substitution. It'd be great if someone explained the implementation nuances in a more detailed way.
Note the string which is imagined to be kept at columns is constant and we need to find the edit distance of the string kept at rows . See this for example
Here we are trying to compute Edit distance of AVERY ! So now, the substructure DP[i][j]=DP[i-1][j-1] (if G[j]==A[i])
NOTE: G stands for GARVEY and A for AVERY
because If we can solve Edit Distance of G[j-1] and A[i-1] in k operations we can solve G[j] and A[i] operations ( No operation needed for last character)
Other wise
DP[i][j]= minimum of following
DP[i-1][j]+1 (See we can only delete from the Row String (whose EDIT distance is to be calculated !) DP[i-1][j] represents the string G[1...j] and A[1...i-1]! See the character A[i] deleted??)
DP[i][j-1]+1 (See this is not a deletion!! What we do is add a character at i+1 th posiition ! And hence it is equal to G[j] (We can add any character))
DP[i-1][j-1]+1 (I guess this is easy now the i th and j th character weren't same so what we did was change the A[i] th character into G[j]th)
Feel free to ask doubts :)