You are on page 1of 2

While finding the edit distance between strings 𝑥 = 𝑥# … 𝑥% and 𝑦 = 𝑦# … 𝑦' multiple optimal

solutions are possible. In the following example, 𝑥 = TREE and 𝑦 = TOR, and four optimal
solutions are possible, each having cost 3. These are shown below:

T R E E T R E E T R E E T _ R E E
T O R _ T O _ R T _ O R T O R _ _

You would need to maintain a matrix N to store the number of optimal solutions in addition to
the matrix E which stores the optimal edit distances.

(a) Define the entry N[𝑖, 𝑗] of the matrix N. (simply a one line definition written in English).

E[𝑖, 𝑗] = 𝑡ℎ𝑒 𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝑒𝑑𝑖𝑡 𝑑𝑖𝑠𝑡𝑎𝑛𝑐𝑒 𝑏𝑒𝑡𝑤𝑒𝑒𝑛 𝑥# … 𝑥? 𝑎𝑛𝑑 𝑦# … 𝑦@

N[𝑖, 𝑗] = number of optimal edit distance solutions between 𝑥# … 𝑥? 𝑎𝑛𝑑 𝑦# … 𝑦@

(b) Write a recurrence to compute the entry N[𝑖, 𝑗] of the matrix N. Explain in a few lines how it
works. Also give base cases.

Base cases,
N[𝑖, 0] = 1, 0 ≤ 𝑖 ≤ 𝑛
N[0, 𝑗] = 1, 0 ≤ 𝑗 ≤ 𝑛

//recurrence
N[𝑖, 𝑗] = 𝑁[𝑖 − 1, 𝑗 − 1] ∗ 𝛼# + 𝑁[𝑖 − 1, 𝑗] ∗ 𝛼I + 𝑁[𝑖, 𝑗 − 1] ∗ 𝛼J

Considering, E[i, j], the optimal value, has been computed using the standard recurrence.
𝛼# =1, if the score of replacement/match 𝑥? , 𝑦@ is equal to E[i, j], else 𝛼# =0
𝛼I =1, if the score of delete 𝑥? is equal to E[i, j], else 𝛼I =0
𝛼J =1, if the score of insert 𝑦@ is equal to E[i, j], else 𝛼J =0

Explanation
We accumulate the number of optimal options at each position. In the end, N[n, m]
contains the number of ways to optimally align x and y.

(c) Write pseudo-code for a bottom-up iterative program to compute both matrices E and N.

editDistanceWithNumOptimalSols(x, y, E, N, m, n)
//base cases
for(int i=0; i<n; i++){
E[i][0]=i;
N[i][0]=1;
}

//base cases
for(int j=0; j<m; j++){
E[0][j]=j;
N[0][j]=1;
}

for(int i=1; i<n; i++){


for(int j=1; j<m; j++){
int matchrep= E[i-1][j-1]+((x[i]==y[j])?0:1);
int ins = E[i][j-1]+1;
int del = E[i-1][j]+1;
E[i][j] = min(matchrep, min(ins, del));

N[i][j]=0;
if(matchrep==E[i][j])// 𝛼#
N[i][j] += N[i-1][j-1];

if(del==E[i][j])// 𝛼I
N[i][j] += N[i-1][j];

if(ins==E[i][j])// 𝛼J
N[i][j] += N[i][j-1];

}
}

You might also like