forked from algorithm-visualizer/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
89 lines (75 loc) · 1.93 KB
/
Copy pathcode.js
File metadata and controls
89 lines (75 loc) · 1.93 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
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
// import visualization libraries {
const { Tracer, Array1DTracer, Array2DTracer, LogTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
// }
let word = 'virgo';
const suffixArray = (function skeleton(word) {
const arr = [];
for (let i = 1; i <= word.length + 1; i++) {
arr.push([i, '-']);
}
return arr;
}(word));
// define tracer variables {
const saTracer = new Array2DTracer('Suffix Array');
const wordTracer = new Array1DTracer('Given Word');
const logger = new LogTracer('Progress');
Layout.setRoot(new VerticalLayout([saTracer, wordTracer, logger]));
saTracer.set(suffixArray);
wordTracer.set(word);
Tracer.delay();
// }
word += '$'; // special character
// logger {
logger.println('Appended \'$\' at the end of word as terminating (special) character. Beginning filling of suffixes');
// }
function selectSuffix(word, i) {
let c = i;
while (i < word.length - 1) {
// visualize {
wordTracer.select(i);
// }
i++;
}
// visualize {
Tracer.delay();
// }
while (c < word.length - 1) {
// visualize {
wordTracer.deselect(c);
// }
c++;
}
// visualize {
Tracer.delay();
// }
}
(function createSA(sa, word) {
for (let i = 0; i < word.length; i++) {
sa[i][1] = word.slice(i);
selectSuffix(word, i);
// visualize {
saTracer.patch(i, 1, sa[i][1]);
Tracer.delay();
saTracer.depatch(i, 1);
Tracer.delay();
// }
}
}(suffixArray, word));
// logger {
logger.println('Re-organizing Suffix Array in sorted order of suffixes using efficient sorting algorithm (O(N.log(N)))');
// }
suffixArray.sort((a, b) => {
// logger {
logger.println(`The condition a [1] (${a[1]}) > b [1] (${b[1]}) is ${a[1] > b[1]}`);
// }
return a[1] > b[1];
});
// visualize {
for (let i = 0; i < word.length; i++) {
saTracer.patch(i, 0, suffixArray[i][0]);
saTracer.patch(i, 1, suffixArray[i][1]);
Tracer.delay();
saTracer.depatch(i, 0);
saTracer.depatch(i, 1);
}
// }