forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
41 lines (37 loc) · 1.54 KB
/
Copy pathcode.js
File metadata and controls
41 lines (37 loc) · 1.54 KB
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
logger._print('Initialized DP Table');
logger._print('Y-Axis (Top to Bottom): ' + str1);
logger._print('X-Axis (Left to Right): ' + str2);
var dist = (function editDistance(str1, str2, table) {
//display grid with words
logger._print('*** ' + str2.split('').join(' '));
table.forEach(function (item, index) {
var character = (index === 0) ? '*' : str1 [index - 1];
logger._print(character + '\t' + item);
});
//begin ED execution
for (var i = 1; i < str1.length + 1; i++) {
for (var j = 1; j < str2.length + 1; j++) {
if (str1[i - 1] === str2[j - 1]) {
tracer._select(i - 1, j - 1)._wait();
table[i][j] = table[i - 1][j - 1];
tracer._notify(i, j, table[i][j])._wait();
tracer._denotify(i, j);
tracer._deselect(i - 1, j - 1);
}
else {
tracer._select(i - 1, j);
tracer._select(i, j - 1);
tracer._select(i - 1, j - 1)._wait();
table[i][j] = Math.min(table [i - 1] [j], table [i] [j - 1], table [i - 1] [j - 1]) + 1;
tracer._notify(i, j, table[i][j])._wait();
tracer._denotify(i, j);
tracer._deselect(i - 1, j);
tracer._deselect(i, j - 1);
tracer._deselect(i - 1, j - 1);
}
}
}
tracer._select(str1.length, str2.length);
return table [str1.length] [str2.length];
})(str1, str2, table);
logger._print('Minimum Edit Distance: ' + dist);