-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneralized_suffix_tree.cpp
645 lines (558 loc) · 15.9 KB
/
generalized_suffix_tree.cpp
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <queue>
#include <algorithm>
namespace suffix_index {
typedef typename std::iterator_traits<typename std::vector<char>::iterator>::difference_type string_index;
class node;
class substring {
protected:
long index; // the index of the parent string
string_index left; // the left bound of the string
string_index right; // the right bound of the string
string_index * end; // the pointer of the end when the right side is still changing
bool changing_right;
public:
substring(long index, string_index left, string_index * end) :
index(index), left(left), end(end), right(0), changing_right(true) {
}
substring(long index, string_index left, string_index right):index(index), left(left), right(right),changing_right(false), end(nullptr){
}
substring():index(0), left(0), right(0), end(nullptr),changing_right(true){
}
long getIndex() const {
return index;
}
void setIndex(long index) {
this->index = index;
}
char getChar(const std::vector<std::vector<char> > indices, long offset) const {
return indices[getIndex()][getLeft()+offset];
}
bool isOffsetValid(long offset) const{
return getLeft() + offset <= getRight();
}
string_index getLeft() const {
return left;
}
void setLeft(string_index left) {
this->left = left;
}
string_index getRight() const {
if (changing_right){
return *end;
}else{
return right;
}
}
void setRight(string_index right) {
this->right = right;
}
void stop_changing(){
this->changing_right = false;
this->right = *this->end;
}
bool is_changing() const {
return this->changing_right;
}
string_index* getEnd() const {
return end;
}
};
struct substring_cmp {
bool operator()(const substring& lhs, const substring& rhs) const {
return lhs.getLeft() < rhs.getLeft();
}
};
class link {
private:
//std::set<substring, substring_cmp> labels; //if there is no threshold on approximation, we could keep only one label, which is more efficient when there is split
substring label;
node * target;
public:
link(substring substr, node * target): target(target), label(substr){
}
link(): target(nullptr){
}
substring & getLabel() {
return label;
}
void setLabel(const substring &label) {
this->label = label;
}
node* getTarget() {
return target;
}
void setTarget(node *target) {
this->target = target;
}
};
class node {
private:
std::unordered_map<char, link> children;
std::unordered_set<int> attribtues;
node * suffix_link;
public:
~node(){
// to avoid recursive deletion
}
node():suffix_link(nullptr){
}
const std::unordered_set<int>& getAttribtues() const {
return attribtues;
}
void setAttribtues(const std::unordered_set<int> attribtues) {
this->attribtues = attribtues;
}
void addAttribtue(const int & attribute) {
this->attribtues.insert(attribute);
}
std::unordered_map<char, link>& getChildren() {
return children;
}
bool isLeaf() const {
return children.empty();
}
link * getLink(char c) {
if (children.find(c)!= children.end()){
return & children[c];
}else{
return nullptr;
}
}
void setChildren(const std::unordered_map<char, link> &children) {
this->children = children;
}
void addChild(char ch, const link & l){
this->children[ch] = l;
}
node* getSuffixLink() {
return suffix_link;
}
void setSuffixLink(node * suffixLink) {
suffix_link = suffixLink;
}
};
class leaf: public node{
};
class parameters{
public:
string_index remaining;
node * active_node;
string_index active_edge;
string_index active_length;
string_index end;
long str_index;
parameters(long str_index): remaining(0), active_node(nullptr), active_edge(-1), active_length(0), end(-1), str_index(str_index){
}
string_index getActiveEdge() const {
return active_edge;
}
void setActiveEdge(string_index activeEdge) {
active_edge = activeEdge;
}
string_index getActiveLength() const {
return active_length;
}
void setActiveLength(string_index activeLength) {
active_length = activeLength;
}
const node* getActiveNode() const {
return active_node;
}
void setActiveNode(node * activeNode) {
active_node = activeNode;
}
string_index getEnd() const {
return end;
}
void setEnd(string_index end) {
this->end = end;
}
string_index getRemaining() const {
return remaining;
}
void setRemaining(string_index remaining) {
this->remaining = remaining;
}
};
class end_of_path_exception: public std::exception{
};
class index_build_exception: public std::exception{
};
class suffix_tree {
private:
std::vector<std::vector<char> > indices;
std::vector<parameters*> param_indices;
int approximate;
parameters* param;
node root;
void index_string(const std::vector<char> & str, int value){
this->indices.push_back(str);
long str_index = this->indices.size() - 1;
param = new parameters(str_index);
this->param_indices.push_back(param);
param->setActiveNode(&root);
for (long i=0; i < str.size(); ++i){
index_step(i, value);
}
/*
if (param->remaining != 0){
throw index_build_exception();
}
*/
}
node * select_node(long i){
std::unordered_map<char, link> & children =
param->active_node->getChildren();
std::unordered_map<char, link>::iterator it = children.find(indices[param->str_index][i]);
if (it==children.end())
return nullptr;
else
return it->second.getTarget();
}
node * select_node(){
std::unordered_map<char, link> & children =
param->active_node->getChildren();
std::unordered_map<char, link>::iterator it = children.find(indices[param->str_index][param->active_edge]);
return it->second.getTarget();
}
link & select_link(){
std::unordered_map<char, link> & children =
param->active_node->getChildren();
std::unordered_map<char, link>::iterator it = children.find(indices[param->str_index][param->active_edge]);
return it->second;
}
char next_char(long i){
std::unordered_map<char, link> & children =
param->active_node->getChildren();
std::unordered_map<char, link>::iterator it = children.find(indices[param->str_index][param->active_edge]);
const substring & label = it->second.getLabel();
const std::vector<char> & str = indices[label.getIndex()];
if((label.getRight()-label.getLeft()) >= param->active_length){
return str[label.getLeft() + param->active_length];
}
if((label.getRight()-label.getLeft()) + 1== param->active_length){
if(it->second.getTarget()->getLink(indices[param->str_index][i]) != nullptr){
return indices[param->str_index][i];
}
}else{
param->active_node = it->second.getTarget();
param->active_length = param->active_length - (label.getRight()-label.getLeft()) -1;
param->active_edge = param->active_edge + (label.getRight()-label.getLeft()) + 1;
return next_char(i);
}
throw end_of_path_exception();
}
void index_step(long i, int value){
node * last_created_internal_node = nullptr;
++param->end;
++param->remaining;
while (param->remaining > 0){
if(param->active_length == 0){
if(select_node(i) != nullptr){
param->active_edge = i;
++param->active_length;
node * n = select_node(i);
if (n->isLeaf())
n->addAttribtue(value);
break;
}else{
node * l = new node();
l->addAttribtue(value);
param->active_node->addChild(indices[param->str_index][i], link(
substring(param->str_index, i, ¶m->end),
l
));
--param->remaining;
}
} else {
try{
char ch = next_char(i);
if(ch==indices[param->str_index][i]){
if(last_created_internal_node != nullptr){
last_created_internal_node->setSuffixLink(select_node());
}
/*update node value when it is used to find longest common substring*/
/*otherwise it would more efficient to keep only walk_down and break*/
/*lcs becomes to find a path on which every values are contained on every nodes*/
link & l = select_link();
const substring & label = l.getLabel();
if (param->active_length == (label.getRight()-label.getLeft())){
l.getTarget()->addAttribtue(value);
if(param->active_node != &root){
param->active_node = param->active_node->getSuffixLink();
}else{
param->active_edge = param->active_edge + 1;
--param->active_length;
}
--param->remaining;
}else{
walk_down(i);
break;
}
}else{
link & l = select_link();
// add a new node to split
node * old_node = l.getTarget();
node * new_node = new node();
new_node->setAttribtues(old_node->getAttribtues());
node * new_leaf = new node();
new_leaf->addAttribtue(value);
new_node->addAttribtue(value);
link l_old = link();
link l_leaf = link();
l_old.setTarget(old_node);
substring & label = l.getLabel();
if(label.is_changing()){
l_old.setLabel(substring(
label.getIndex(),
label.getLeft() + param->active_length, label.getEnd()
));
label.stop_changing();
label.setRight(label.getLeft() + param->active_length-1);
} else {
long old_right = label.getRight();
label.setRight(label.getLeft()+param->active_length-1);
l_old.setLabel(substring(
label.getIndex(),
label.getLeft() + param->active_length,
old_right
));
}
l.setTarget(new_node);
l_old.setTarget(old_node);
const substring & first_label = l_old.getLabel();
new_node->addChild(indices[first_label.getIndex()][first_label.getLeft()], l_old);
l_leaf.setLabel(substring(param->str_index,
i, ¶m->end));
l_leaf.setTarget(new_leaf);
new_node->addChild(indices[param->str_index][i], l_leaf);
if(last_created_internal_node != nullptr){
last_created_internal_node->setSuffixLink(new_node);
}
last_created_internal_node = new_node;
new_node->setSuffixLink(&root);
if(param->active_node != &root){
param->active_node = param->active_node->getSuffixLink();
}else{
param->active_edge = param->active_edge + 1;
--param->active_length;
}
--param->remaining;
}
} catch (const end_of_path_exception &e) {
node * n = select_node();
node * new_node = new node();
new_node->addAttribtue(value);
n->addChild(indices[param->str_index][i],
link(substring(param->str_index,
i,¶m->end),
new_node));
if (last_created_internal_node != nullptr){
last_created_internal_node->setSuffixLink(n);
}
last_created_internal_node = n;
if (param->active_node != &root){
param->active_node = param->active_node->getSuffixLink();
}else{
++param->active_edge;
--param->active_length;
}
--param->remaining;
}
}
}
}
void walk_down(long i){
link & l = select_link();
const substring & label = l.getLabel();
if(param->active_length > (label.getRight()-label.getLeft())){
param->active_node = l.getTarget();
param->active_length = param->active_length - (label.getRight()-label.getLeft());
param->active_edge = i;
}else{
++param->active_length;
}
}
public:
static const char unique_char = '$';
~suffix_tree() {
// to avoid recursive deletion
std::queue<node*> del_q;
del_q.push(&root);
while (!del_q.empty()) {
node *current = del_q.front();del_q.pop();
for (auto it : current->getChildren()) {
del_q.push(it.second.getTarget());
}
if (&root != current) {
delete current;
}
}
for (parameters * p: param_indices){
delete p;
}
}
suffix_tree(int approximate) :
approximate(approximate), param(nullptr){
}
suffix_tree() :
suffix_tree(-1) {
}
void add_string(const std::string &word, int value) {
std::vector<char> char_list(word.begin(), word.end());
add_string(char_list, value);
}
void add_string(const std::vector<char> &word, int value) {
this->index_string(word, value);
}
std::unordered_set<int> search_string(const std::string word) {
std::vector<char> char_list(word.begin(), word.end());
return search_string(char_list);
}
std::unordered_set<int> search_string(const std::vector<char> text) {
std::unordered_set<int> ans;
node * current = &root;
for (size_t i=0; i < text.size(); ++i){
if (current != &this->root){
if(approximate<0){
ans.insert(current->getAttribtues().begin(),
current->getAttribtues().end());
}
}
link * l = current->getLink(text[i]);
if (l==nullptr){
break;
}else{
const substring & link_label = l->getLabel();
long offset = 0;
while(link_label.isOffsetValid(offset) && i < text.size() &&link_label.getChar(this->indices, offset) == text[i]){
++offset;
++i;
}
node * target = l->getTarget();
if (i >= text.size()){
if(approximate<0){
ans.insert(target->getAttribtues().begin(),
target->getAttribtues().end());
}else{
if(target->isLeaf()){
if ((link_label.getRight()-link_label.getLeft()-offset+1) <= approximate){
ans.insert(target->getAttribtues().begin(),
target->getAttribtues().end());
}
}
}
}else{
--i;
current = target;
}
}
}
return ans;
}
bool is_suffix(const std::string word) {
std::vector<char> char_list(word.begin(), word.end());
return is_suffix(char_list);
}
bool is_suffix(const std::vector<char> text) {
node * current = &root;
for (size_t i=0; i < text.size(); ++i){
link * l = current->getLink(text[i]);
if (l==nullptr){
break;
}else{
const substring & link_label = l->getLabel();
long offset = 0;
while(link_label.isOffsetValid(offset) && i < text.size() &&link_label.getChar(this->indices, offset) == text[i]){
++offset;
++i;
}
node * target = l->getTarget();
if (i >= text.size()){
if(target->isLeaf()){
if ((link_label.getRight()-link_label.getLeft()-offset+1) == 0){
return true;
}
}
}else{
--i;
current = target;
}
}
}
return false;
}
bool is_substring(const std::string word) {
std::vector<char> char_list(word.begin(), word.end());
return is_substring(char_list);
}
bool is_substring(const std::vector<char> text){
node * current = &root;
for (size_t i=0; i < text.size(); ++i){
link * l = current->getLink(text[i]);
if (l==nullptr){
break;
}else{
const substring & link_label = l->getLabel();
long offset = 0;
while(link_label.isOffsetValid(offset) && i < text.size() &&link_label.getChar(this->indices, offset) == text[i]){
++offset;
++i;
}
node * target = l->getTarget();
if (i >= text.size()){
return true;
}else{
--i;
current = target;
}
}
}
return false;
}
};
const char suffix_tree::unique_char;
}
int main(int argc, char **argv) {
std::cout << "Hello World" << std::endl;
suffix_index::suffix_tree st(0);
st.add_string("Hello", 1);
st.add_string("Hurry", 2);
st.add_string("mississippi", 4);
st.add_string("Huhao", 3);
st.add_string("Puhao", 5);
st.add_string("Hfhao", 6);
st.add_string("Puhax", 7);
for (int v: st.search_string("o")){
std::cout << v << std::endl;
}
std::cout << std::endl;
for (int v: st.search_string("hao")){
std::cout << v << std::endl;
}
std::cout << std::endl;
for (int v: st.search_string("Puhax")){
std::cout << v << std::endl;
}
for (int v: st.search_string("Hurry")){
std::cout << v << std::endl;
}
for (int v: st.search_string("mississippi")){
std::cout << v << std::endl;
}
std::cout << st.is_suffix("o") << std::endl;
std::cout << st.is_suffix("fhao") << std::endl;
std::cout << st.is_suffix("fha") << std::endl;
std::cout << st.is_suffix("Puhax") << std::endl;
std::cout << st.is_suffix("Hurry") << std::endl;
std::cout << st.is_suffix("mississippi") << std::endl;
std::cout << st.is_substring("pma") << std::endl;
std::cout << st.is_substring("fha") << std::endl;
return 0;
}