-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditDistance.java
158 lines (133 loc) · 4.24 KB
/
EditDistance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.lang.Math.*;
import java.time.*;
import java.util.HashMap;
/**
* @author: Alexander Gruschina
*
* Preparation for final exam on "Similarity Search VO"
* held by Prof. Nikolaus Augsten (Database Research Group)
* Dept. of CS, University of Salzburg, Austria
* Course website: https://dbresearch.uni-salzburg.at/teaching/2016ws/ssdb/
*
* Investigating approaches for computing Edit Distance of 2 strings
* - brute force (exponential)
* - dynamic programming O(m*n) time+space
* - dynamic programming with optimized space complexity O(m) (wlog m < n)
**/
public class EditDistance {
static boolean DEBUG = false;
static boolean DEBUG_DYN_ARRAY = false;
static int recursions = 0;
public static void main(String[] args) {
if (args.length != 2) {
System.out.println("Usage: java EditDistance <string1> <string2>");
return;
}
String x = args[0];
String y = args[1];
int norm = Math.max(x.length(),y.length());
System.out.println("Computing Edit Distance (Levenshtein) of: ");
System.out.println("x: '" + x + "'");
System.out.println("y: '" + y + "'");
System.out.println();
// brute force
System.out.println("[Brute force w/o caching...]");
long start = System.currentTimeMillis();
int ed_bf = ed_bf(x,y);
float time = System.currentTimeMillis() - start;
System.out.print("\tDistance: " + ed_bf);
System.out.print(" (normalized: " + ((float)ed_bf)/norm + ")\n");
System.out.print("\ttook " + time + " ms");
System.out.println(" (" + recursions + " recursions)");
System.out.println();
// dynamic programming
System.out.println("[Dynamic programming...]");
start = System.currentTimeMillis();
int ed_dyn = ed_dyn(x,y);
time = System.currentTimeMillis() - start;
System.out.print("\tDistance: " + ed_dyn);
System.out.print(" (normalized: " + ((float)ed_dyn)/norm + ")\n");
System.out.println("\ttook " + time + " ms");
System.out.println();
// dynamic programming with space optimization
System.out.println("[Dynamic programming with space optimization...]");
start = System.currentTimeMillis();
int ed_dyn_reduced_space = ed_dyn_reduced_space(x,y);
time = System.currentTimeMillis() - start;
System.out.print("\tDistance: " + ed_dyn_reduced_space);
System.out.print(" (normalized: " + ((float)ed_dyn_reduced_space)/norm + ")\n");
System.out.println("\ttook " + time + " ms");
System.out.println();
}
public static int ed_bf(String x, String y) {
recursions++;
int m = x.length();
int n = y.length();
int c = 0;
if (DEBUG) {
System.out.println("m: " + m + ", n: " + n);
System.out.println("x: " + x + ", y: " + y);
}
if (m == 0) return n;
if (n == 0) return m;
if (x.charAt(m - 1) == y.charAt(n - 1)) c = 0; else c = 1;
String x_ = (m-1) > 0 ? x.substring(0,m-1) : "";
String y_ = (n-1) > 0 ? y.substring(0,n-1) : "";
return Math.min(
Math.min(
ed_bf(x, y_) + 1,
ed_bf(x_, y) + 1),
ed_bf(x_,y_) + c
);
}
public static int ed_dyn(String x, String y) {
int m = x.length();
int n = y.length();
int c = 0;
int[][] array = new int[m+1][n+1];
for (int i = 0; i <= m; i++) array[i][0] = i;
for (int j = 1; j <= n; j++) array[0][j] = j;
for (int j = 1; j <= n; j++) {
for (int i = 1; i <= m; i++) {
if (x.charAt(i-1) == y.charAt(j-1)) c = 0; else c = 1;
array[i][j] = Math.min(
Math.min(
array[i][j-1] + 1,
array[i-1][j] + 1),
array[i-1][j-1] + c
);
}
}
if (DEBUG_DYN_ARRAY) {
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++) {
System.out.println(array[i][j]);
}
}
}
return array[m][n];
}
public static int ed_dyn_reduced_space(String x, String y) {
int m = x.length();
int n = y.length();
int c = 0;
int[] col0 = new int[m+1];
int[] col1 = new int[m+1];
for (int i = 0; i <= m; i++) col0[i] = i;
for (int j = 1; j <= n; j++) {
col1[0] = j;
for (int i = 1; i <= m; i++) {
if (x.charAt(i-1) == y.charAt(j-1)) c = 0; else c = 1;
col1[i] = Math.min(
Math.min(
col0[i] + 1,
col1[i-1] + 1),
col0[i-1] + c
);
}
col0 = col1;
col1 = new int[m+1];
}
return col0[m];
}
}