Merge pull request #131 from duaraghav8/gh-pages · sghgithub/AlgorithmVisualizer@75a6233 · GitHub
Skip to content

Commit 75a6233

Browse files
author
Raghav Dua
committed
Merge pull request algorithm-visualizer#131 from duaraghav8/gh-pages
Suffix Array Construction (naive)
2 parents ea7b15b + 081a9e0 commit 75a6233

4 files changed

Lines changed: 79 additions & 1 deletion

File tree

algorithm/category.json

Lines changed: 2 additions & 1 deletion
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
word += '$'; //special character
2+
logger._print ('Appended \'$\' at the end of word as terminating (special) character. Beginning filling of suffixes');
3+
4+
function selectSuffix (word, i) {
5+
var c = i;
6+
7+
while (i < word.length-1) {
8+
wordTracer._select (i);
9+
i++;
10+
}
11+
wordTracer._wait ();
12+
13+
while (c < word.length-1) {
14+
wordTracer._deselect (c);
15+
c++;
16+
}
17+
wordTracer._wait ();
18+
}
19+
20+
(function createSA (sa, word) {
21+
for (var i = 0; i < word.length; i++) {
22+
sa [i] [1] = word.slice (i);
23+
24+
selectSuffix (word, i);
25+
saTracer._notify (i, 1, sa [i] [1])._wait ();
26+
saTracer._denotify (i, 1)._wait ();
27+
}
28+
}) (suffixArray, word);
29+
30+
logger._print ('Re-organizing Suffix Array in sorted order of suffixes using efficient sorting algorithm (O(N.log(N)))');
31+
suffixArray.sort (function (a, b) {
32+
logger._print ('The condition a [1] (' + a [1] + ') > b [1] (' + b [1] + ') is ' + (a [1] > b [1]));
33+
return a [1] > b [1];
34+
});
35+
36+
for (var i = 0; i < word.length; i++) {
37+
saTracer._notify (i, 0, suffixArray [i] [0]);
38+
saTracer._notify (i, 1, suffixArray [i] [1])._wait ();
39+
40+
saTracer._denotify (i, 0);
41+
saTracer._denotify (i, 1);
42+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var word = 'virgo';
2+
var suffixArray = (function skeleton (word) {
3+
var arr = [];
4+
5+
for (var i = 1; i <= word.length+1; i++) {
6+
arr.push ([i, '-']);
7+
}
8+
9+
return arr;
10+
}) (word);
11+
12+
var saTracer = new Array2DTracer ('Suffix Array'),
13+
wordTracer = new Array1DTracer ('Given Word'),
14+
logger = new LogTracer ('Progress');
15+
16+
saTracer._setData (suffixArray);
17+
wordTracer._setData (word);
Lines changed: 18 additions & 0 deletions

0 commit comments

Comments
 (0)