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
40 lines (36 loc) · 1.18 KB
/
Copy pathcode.js
File metadata and controls
40 lines (36 loc) · 1.18 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
tracer._pace (200);
tracer._print ('Initialized DP Table');
tracer._print ('Y-Axis (Top to Bottom): ' + str1);
tracer._print ('X-Axis (Left to Right): ' + str2);
var dist = (function editDistance (str1, str2, table) {
//display grid with words
tracer._print ('*** ' + str2.split ('').join (' '));
table.forEach (function (item, index) {
var character = (index === 0) ? '*' : str1 [index-1];
tracer._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);
table [i] [j] = table [i-1] [j-1];
tracer._notify (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);
table [i] [j] = Math.min (table [i-1] [j], table [i] [j-1], table [i-1] [j-1]) + 1;
tracer._notify (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);
tracer._print ('Minimum Edit Distance: ' + dist);