I am trying to implement the Wagner-Fischer algorithm
in java using the Wikipedia reference
Java Code:
public class StringDistance {
public static void main(String[] args) {
int i, j, m, n, temp, tracker;
int[][] d = new int[50][50];
String s = "kitten";
String t = "sitting";
char u[] = s.toCharArray();
char v[] = t.toCharArray();
m = u.length;
n = v.length;
for (i = 0; i <= m; i++) {
d[i][0] = i;
}
for (j = 0; j <= n; j++) {
d[0][j] = j;
}
for (j = 1; j <= m; j++) {
for (i = 1; i <= n; i++) {
if (u[i - 1] == v[j - 1]) {
tracker = 0;
} else {
tracker = 1;
}
temp = Math.min((d[i - 1][j] + 1), (d[i][j - 1] + 1));
d[i][j] = Math.min(temp, (d[i - 1][j - 1] + tracker));
}
}
System.out.println("The levenstien distance" + d[n][m]);
}
}
But the above code is working only for the strings with equal lengths. If i want to make this work for unequal strings.Please let me know how to overcome the issue.
I am getting the index out of bounds error :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at StringDistance.main(StringDistance.java:32)
Let's get rid of some of the local variables to make it clearer why this is happening:
for (j = 1; j <= u.length; j++) {
for (i = 1; i <= v.length; i++) {
if (u[i - 1] == v[j - 1]) {
tracker = 0;
} else {
tracker = 1;
}
You're using i - 1
(which is guaranteed to be in range for v
) as the index into u
and j - 1
(which is guaranteed to be in range for u
) as the index into v
.
So I suspect this expression:
u[i - 1] == v[j - 1]
should just be
u[j - 1] == v[i - 1]
I'd also strongly suggest only declaring variables at the point of first use, in minimal scope, and using 0-based indexing rather than 1-based. And the conditional operator helps too. So your loop would become:
for (int j = 0; j < u.length; j++) {
for (int i = 0; i < v.length; i++) {
int tracker = u[j] == v[i] ? 0 : 1;
int temp = Math.min(d[i][j + 1] + 1, d[i + 1][j] + 1);
d[i + 1][j + 1] = Math.min(temp, d[i][j] + tracker);
}
}
See more on this question at Stackoverflow