开发者

substring searching in a string using suffix tree..?

开发者 https://www.devze.com 2023-03-11 00:30 出处:网络
I have read that : 开发者_高级运维Searching for a substring, pat[1..m], in txt[1..n], can be solved in O(m) time (after the suffix tree for txt has been built in O(n) time).

I have read that :

开发者_高级运维Searching for a substring, pat[1..m], in txt[1..n], can be solved in O(m) time (after the suffix tree for txt has been built in O(n) time).

but at each point, we will have to choose which branch to take, so like in n-ary tree, at each node, we will have to compare with all max n pointers in that node to decide which branch to take. Will this not bring n factor in complexity of this algorithm, somehow in picture

Then how above it says that substring can be found in O(m)?

What am I missing here?


Then how above it says that substring can be found in O(m)?

By omission. You are correct that the runtime of searching in suffix trees is more complex than merely O(m).

However, it can indeed be sped up to O(m) if we trade off space requirements: we need to get the search at each node down to O(1) and we can do this by using an appropriate data structure (e.g. an array) which gives us the appropriate sub-tree for each letter in constant time.

For instance, assume that you’re using C++ for the implementation and your character (char) can contain 256 different values. Then the implementation of a node could look as follows:

struct node {
    char current_character;
    node* children[256];
};

Now, current_character refers to the character of the branch leading to the current node, and children is an array of child nodes. During the search, assume that you are currently at node u, and the next character in the input text is c. Then you will choose the next node as follows:

node* next = u->children[c];
if (next == 0) {
    // Child node does not exist => nothing found.
}
else {
    u = next;
    // Continue search with next …
}

Of course, this is only viable for very small alphabets (e.g. DNA for genomic sequences). In most common cases, the worst-case runtime of a suffix tree is indeed higher than O(m).


If pointers to childs are in array indexed by the letter, only constant time is needed for each pattern letter

node = tree root
FOR i in 1..m
   node = child[pat[i]]

so the complexity is O(m).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号