Skip to content

Commit

Permalink
edit suffix tree
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangYou0122 committed Jul 2, 2014
2 parents b7dcd00 + ddd9164 commit e0166f9
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 127 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CC=gcc
CPP=g++
AR=ar
RANLIB=ranlib
CFLAGS= -g -Wall -Wno-unused-function
CFLAGS= -g -Wall -Wno-unused-function -std=gnu++0x
SRCDIR = ./src
INCLUDEDIR = -I./include -I.
DEPS =
Expand Down Expand Up @@ -69,6 +69,7 @@ PROGRAMS = m_based_demo \
selection_sort_demo \
8queue_demo \
palindrome_demo \
suffix_array_demo \
suffix_tree_demo

all: $(PROGRAMS)
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@
Red-black tree
Interval tree
Prefix Tree(Trie)
*Suffix Tree(未实现)*
Suffix Tree
B-Tree
Suffix Array

Hash by multiplication
Hash table
Expand Down Expand Up @@ -96,4 +97,5 @@
wycg1984: for K-Means
xmuliang: for HeapSort, Kruskal MST
wyh267: for base64, LRU, bubble sort, selection sort
ZhangYou0122 : Push-Relabel algorithm
ZhangYou0122: Push-Relabel algorithm, Suffix Tree
UsingtcNower: Suffix Array
2 changes: 1 addition & 1 deletion include/prime.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace alg {
if (n%2 == 0) return false;

unsigned sqrtn = sqrt(n);
for (unsigned int i = 2; i <= sqrtn; ++i) {
for (unsigned int i = 3; i <= sqrtn; i+=2) {
if (n % i == 0) {
return false;
}
Expand Down
102 changes: 102 additions & 0 deletions include/suffix_array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*******************************************************************************
* ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* SUFFIX ARRAY
*
* Features:
* suffix array can sort all the suffixs in time complexity O(n*log^2(n)),
* and use memory in O(n). And suffix array can get two suffixs' longest
* common prefix(lcp) in O(log(n)) complexity.
*
* You can test it by running suffix_array_demo.cpp
* Want to get more detailed information about suffix array?
*
* Please google SUFF_AR_ENG.pdf
*
* AUTHOR: [email protected]
******************************************************************************/

#ifndef __SUFFIX_ARRAY_H__
#define __SUFFIX_ARRAY_H__

#include <algorithm>
#include <vector>
#include <string>
#include <math.h>

using namespace std;

namespace alg {
class SuffixArray {
private:
vector<vector<int> > bucket;
vector<int> suffix;
int N, L, K;
const string& str;
void suffix_sort();
void update_bucket();

bool less_than(int a, int b) {
if(K==0) return str[a]<str[b];
else {
if(bucket[K-1][a]==bucket[K-1][b]) return bucket[K-1][a+L/2]<bucket[K-1][b+L/2];
else return bucket[K-1][a]<bucket[K-1][b];
}
}

bool equal(int a, int b) {
return !less_than(a,b) && !less_than(b,a);
}

public:
explicit SuffixArray(const string& s) : N(s.size()), L(0), K(0), str(s) { suffix_sort();}
// return the sorted suffix
int operator [] (int i) { return suffix[i];}
// Given two suffixs of string, return the longest common prefix length
int lcp_length(int x, int y);
};

void SuffixArray::suffix_sort() {
// init suffix
suffix.resize(N);
for(int i=0;i<N;i++) suffix[i]=i;
// init bucket
bucket.resize(ceil(log2(N))+1);
for(size_t k=0;k<bucket.size();k++) bucket[k].resize(N+N);

for(L=1,K=0;(L>>1)<N;L<<=1,K++) {
sort(suffix.begin(), suffix.end(), bind(&SuffixArray::less_than, *this, placeholders::_1, placeholders::_2));
update_bucket();
}
}


void SuffixArray::update_bucket() {
int seq=0;
bucket[K][suffix[0]]=0;
for(int i=1;i<N;i++) {
if(!equal(suffix[i],suffix[i-1])) seq++;
bucket[K][suffix[i]]=seq;
}
fill(bucket[K].begin()+N, bucket[K].end(), -1);
}

int SuffixArray::lcp_length(int x, int y) {
if(x==y) return N-x;
int ret=0;
for(int k=K-1;k>=0 && x<N && y<N;k--) {
if(bucket[k][x]==bucket[k][y]) {
x += (1<<k);
y += (1<<k);
ret += (1<<k);
}
}
return ret;
}
}

#endif // __SUFFIX_ARRAY_H__
41 changes: 19 additions & 22 deletions include/suffix_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SuffixTree
{
public:
// active point is initialized as (root, None, 0), remainder initialized as 1
SuffixTree(string str):test_str(str), root(test_str), active_point(&root, NULL, 0), remainder(0), pos(0), base_pos(0), ls() {}
SuffixTree(string str):test_str(str), root(test_str), active_point(&root, 0, 0), remainder(0), pos(0), active_e(0), ls() {}
int construct(void);

// return -1 if no such sub exist, return the beginning postion of this substring in thr original string if it exist
Expand Down Expand Up @@ -229,10 +229,10 @@ class SuffixTree
class ActivePoint{
public:
Node* active_node;
Edge* active_edge;
char active_edge;
int active_length;

ActivePoint(Node* node, Edge* edge, int length):
ActivePoint(Node* node, char edge, int length):
active_node(node), active_edge(edge), active_length(length) { std::cout << "ActivePoint initialized" << std::endl; }
};

Expand All @@ -241,8 +241,11 @@ class SuffixTree

Node* get_active_node(void) { return active_point.active_node; }
void set_active_node(Node* node) { active_point.active_node = node; cout << "Active node set as " << node << endl; }
Edge* get_active_edge(void) { return active_point.active_edge; }
void set_active_edge(Edge* edge) { active_point.active_edge = edge; }
char get_active_edge(void)
{
return test_str[active_e];
}

int get_active_length(void) { return active_point.active_length; }
void set_active_length(int len) { active_point.active_length = len; }
void inc_active_len() { active_point.active_length++; }
Expand All @@ -252,7 +255,7 @@ class SuffixTree
int remainder;
// how many characters inserted?
unsigned int pos;
unsigned int base_pos; // the beginnig position of suffixes need to be inserted
unsigned int active_e; // the beginnig position of suffixes need to be inserted
char get_ele(int i) { return test_str[i]; }
// insert a char from pos to suffix tree
int insert();
Expand All @@ -261,36 +264,30 @@ class SuffixTree
int print_node(Node* node, int level);


Node* seperate_edge(Node * node, Edge* edge, int rule);
Node* seperate_edge(Node * node, Edge* edge);

// check if we can change active node
void check_active_node(void)
bool check_active_node(void)
{
Node* node = get_active_node();
Edge* edge = get_active_edge();
char a_char = get_active_edge();
Edge* edge = node->find_edge(a_char);

if (edge == NULL)
return;
return false;

unsigned int edge_size = edge->end - edge->begin + 1;
unsigned int length = get_active_length();

// update
if (edge_size == length) {
if (length >= edge_size) {
set_active_node(edge->endpoint);
set_active_edge(0);
set_active_length(0);
base_pos += edge_size;
}
else if (length > edge_size) {
set_active_length(length-edge_size);
set_active_node(edge->endpoint);
int new_length = get_active_length();
base_pos += edge_size;
Edge *new_active_edge = edge->endpoint->find_edge(get_ele(base_pos));
set_active_edge(new_active_edge);
check_active_node();
active_e += edge_size;

return true;
}
return false;
}

// this class indicate when shall we insert a suffix link
Expand Down
37 changes: 37 additions & 0 deletions src/suffix_array_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>
#include <string>
#include <math.h>

#include "suffix_array.h"

using namespace std;
using namespace alg;

void print(string::iterator b, string::iterator e) {
for(auto it=b;it!=e;++it) cout<<*it;
}

int main()
{
string str;
while(cin>>str) {
SuffixArray sa(str);
cout<<endl;
cout<<"sorted suffixs are:"<<endl;
for(size_t i=0;i<str.size();i++) {
print(str.begin()+sa[i], str.end());
cout<<endl;
}
cout<<endl;
cout<<"The length of the longest common prefix of two suffixs ";
int i=rand()%str.size();
int j=rand()%str.size();
print(str.begin()+i,str.end());
cout<<" and ";
print(str.begin()+j,str.end());
cout<<" is ";
cout<<sa.lcp_length(i,j)<<endl;
cout<<endl;
}
return 0;
}
Loading

0 comments on commit e0166f9

Please sign in to comment.