2016-09-05 7 views
0

レーベンシュタイン距離は、反復的に2行をこのように使用して計算することができます:私は転置を考慮に入れないOptimal String alignment distanceに出くわした反復バージョンは、

https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_two_matrix_rows

。ウィキペディアは、通常のレーベンシュタインアルゴリズムの簡単な拡張機能を使用して計算することができることを言う:

if i > 1 and j > 1 and a[i-1] = b[j-2] and a[i-2] = b[j-1] then 
    d[i, j] := minimum(d[i, j], 
         d[i-2, j-2] + cost) // transposition 

は、しかし、私は、反復バージョンのコードに、そのページ上の擬似コードアルゴリズムの拡張ポートにできませんよ。どんな助けでも大歓迎です。

答えて

1

あなたは、この新しいバージョンを計算するために3つの行を必要とする、私は、コードを確認することはできませんが、私はそれについてかなり確信しています:

int DamerauLevenshteinDistance(string s, string t) 
{ 
// degenerate cases 
if (s == t) return 0; 
if (s.Length == 0) return t.Length; 
if (t.Length == 0) return s.Length; 

// create two work vectors of integer distances 
int[] v0 = new int[t.Length + 1]; 
int[] v1 = new int[t.Length + 1]; 
int[] v2 = new int[t.Length + 1]; 

// initialize v0 (the previous row of distances) 
// this row is A[0][i]: edit distance for an empty s 
// the distance is just the number of characters to delete from t 
for (int i = 0; i < v0.Length; i++) 
    v0[i] = i; 

    // compute v1 

    v1[0] = 0; 

    // use formula to fill in the rest of the row 
    for (int j = 0; j < t.Length; j++) 
    { 
     var cost = (s[0] == t[j]) ? 0 : 1; 
     v1[j + 1] = Minimum(v1[j] + 1, v0[j + 1] + 1, v0[j] + cost); 
    } 

if (s.Length == 1) { 
    return v1[t.Length]; 
} 

for (int i = 1; i < s.Length; i++) 
{ 
    // calculate v2 (current row distances) from the previous rows v0 and v1 

    // first element of v2 is A[i+1][0] 
    // edit distance is delete (i+1) chars from s to match empty t 
    v2[0] = i + 1; 

    // use formula to fill in the rest of the row 
    for (int j = 0; j < t.Length; j++) 
    { 
     var cost = (s[i] == t[j]) ? 0 : 1; 
     v2[j + 1] = Minimum(v2[j] + 1, v1[j + 1] + 1, v1[j] + cost); 
     if (j > 0 && s[i] = t[j-1] && s[i-1] = t[j]) 
      v2[j + 1] = Minimum(v2[j+1], 
        v0[j-1] + cost); 
    } 

    // copy v2 (current row) to v1 (previous row) and v1 to v0 for next iteration 
    for (int j = 0; j < v0.Length; j++) 
     v0[j] = v1[j]; 
     v1[j] = v2[j]; 
} 

return v2[t.Length]; 
} 

元のコードは、上記のウィキペディアの実装から来ています。