Repository navigation
Expand file tree
/
Copy pathcode.js
More file actions
62 lines (48 loc) · 1.52 KB
/
Copy pathcode.js
File metadata and controls
62 lines (48 loc) · 1.52 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
import { Array1DTracer, Array2DTracer, LogTracer } from '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));
const saTracer = new Array2DTracer('Suffix Array');
const wordTracer = new Array1DTracer('Given Word');
const logger = new LogTracer('Progress');
saTracer.set(suffixArray);
wordTracer.set(word).delay();
word += '$'; // special character
logger.print('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) {
wordTracer.select(i);
i++;
}
wordTracer.delay();
while (c < word.length - 1) {
wordTracer.deselect(c);
c++;
}
wordTracer.delay();
}
(function createSA(sa, word) {
for (let i = 0; i < word.length; i++) {
sa[i][1] = word.slice(i);
selectSuffix(word, i);
saTracer.patch(i, 1, sa[i][1]).delay();
saTracer.depatch(i, 1).delay();
}
}(suffixArray, word));
logger.print('Re-organizing Suffix Array in sorted order of suffixes using efficient sorting algorithm (O(N.log(N)))');
suffixArray.sort((a, b) => {
logger.print(`The condition a [1] (${a[1]}) > b [1] (${b[1]}) is ${a[1] > b[1]}`);
return a[1] > b[1];
});
for (let i = 0; i < word.length; i++) {
saTracer.patch(i, 0, suffixArray[i][0]);
saTracer.patch(i, 1, suffixArray[i][1]).delay();
saTracer.depatch(i, 0);
saTracer.depatch(i, 1);
}