📜  广义后缀树1

📅  最后修改于: 2021-04-17 09:13:00             🧑  作者: Mango

在较早的后缀树文章中,我们为一个字符串创建了后缀树,然后查询该树以进行子字符串检查,搜索所有模式,最长的重复子字符串并构建后缀数组(所有线性时间操作)。

还有许多其他问题,其中涉及多个字符串。
例如,在文本文件或字典中搜索模式,拼写检查器,电话簿,自动完成,最长的常见子字符串问题,最长回文子字符串等等。

对于此类操作,需要对所有涉及的字符串进行索引,以便更快地进行搜索和检索。一种方法是使用后缀特里或后缀树。我们将在这里讨论后缀树。
由一组字符串的后缀树称为通用后缀树。
我们将在这里讨论针对两个字符串构建通用后缀树的简单方法。
稍后,我们将讨论另一种为两个或更多字符串构建通用后缀树的方法。

在这里,我们将对已经讨论过的一个字符串使用后缀树实现,并对其进行一些修改以构建通用的后缀树。

让我们考虑我们要为其建立广义后缀树的两个字符串X和Y。为此,我们将创建一个新的字符串X#Y $,其中#和$都是终端符号(必须是唯一的)。然后,我们将为X#Y $构建后缀树,这将是X和Y的通用后缀树。相同的逻辑将适用于两个以上的字符串(即,使用唯一的终端符号连接所有字符串,然后为连接的字符串构建后缀树) 。

假设X = xabxa,而Y = babxba,则
X#Y $ = xabxa#babxba $
如果我们运行Ukkonen的后缀树构造–第6部分中针对字符串xabxa#babxba $实施的代码,则会得到以下输出:
输出:

画册视图:

我们可以使用该树来解决一些问题,但是我们可以通过删除路径标签上不需要的子字符串来对其进行一些改进。路径标签应该只包含来自一个输入字符串的子字符串,因此,如果路径标签中包含来自多个输入字符串的子字符串,我们可以仅保留与一个字符串相对应的起始部分,然后删除所有后面的部分。例如,对于路径标签#babxba $,a#babxba $和bxa#babxba $,我们可以删除babxba $(属于第二个输入字符串),然后新的路径标签分别为#,a#和bxa#。进行此更改后,上图将如下所示:

广义后缀树

下面的实现是在原始实现的基础上构建的。在这里,我们将删除路径标签上不需要的字符。如果路径标签中包含“#”字符,则我们将修剪该路径标签中“#”之后的所有字符。
注意:此实现仅为两个字符串X和Y构建了通用后缀树,这两个字符串串联为X#Y $

// A C program to implement Ukkonen's Suffix Tree Construction
// And then build generalized suffix tree
#include 
#include 
#include 
#define MAX_CHAR 256
   
struct SuffixTreeNode {
    struct SuffixTreeNode *children[MAX_CHAR];
   
    //pointer to other node via suffix link
    struct SuffixTreeNode *suffixLink;
   
    /*(start, end) interval specifies the edge, by which the
     node is connected to its parent node. Each edge will
     connect two nodes,  one parent and one child, and
     (start, end) interval of a given edge  will be stored
     in the child node. Lets say there are two nods A and B
     connected by an edge with indices (5, 8) then this
     indices (5, 8) will be stored in node B. */
    int start;
    int *end;
   
    /*for leaf nodes, it stores the index of suffix for
      the path  from root to leaf*/
    int suffixIndex;
};
   
typedef struct SuffixTreeNode Node;
   
char text[100]; //Input string
Node *root = NULL; //Pointer to root node
   
/*lastNewNode will point to newly created internal node,
  waiting for it's suffix link to be set, which might get
  a new suffix link (other than root) in next extension of
  same phase. lastNewNode will be set to NULL when last
  newly created internal node (if there is any) got it's
  suffix link reset to new internal node created in next
  extension of same phase. */
Node *lastNewNode = NULL;
Node *activeNode = NULL;
   
/*activeEdge is represeted as input string character
  index (not the character itself)*/
int activeEdge = -1;
int activeLength = 0;
   
// remainingSuffixCount tells how many suffixes yet to
// be added in tree
int remainingSuffixCount = 0;
int leafEnd = -1;
int *rootEnd = NULL;
int *splitEnd = NULL;
int size = -1; //Length of input string
   
Node *newNode(int start, int *end)
{
    Node *node =(Node*) malloc(sizeof(Node));
    int i;
    for (i = 0; i < MAX_CHAR; i++)
          node->children[i] = NULL;
   
    /*For root node, suffixLink will be set to NULL
    For internal nodes, suffixLink will be set to root
    by default in  current extension and may change in
    next extension*/
    node->suffixLink = root;
    node->start = start;
    node->end = end;
   
    /*suffixIndex will be set to -1 by default and
      actual suffix index will be set later for leaves
      at the end of all phases*/
    node->suffixIndex = -1;
    return node;
}
   
int edgeLength(Node *n) {
    if(n == root)
        return 0;
    return *(n->end) - (n->start) + 1;
}
   
int walkDown(Node *currNode)
{
    /*activePoint change for walk down (APCFWD) using
     Skip/Count Trick  (Trick 1). If activeLength is greater
     than current edge length, set next  internal node as
     activeNode and adjust activeEdge and activeLength
     accordingly to represent same activePoint*/
    if (activeLength >= edgeLength(currNode))
    {
        activeEdge += edgeLength(currNode);
        activeLength -= edgeLength(currNode);
        activeNode = currNode;
        return 1;
    }
    return 0;
}
   
void extendSuffixTree(int pos)
{
    /*Extension Rule 1, this takes care of extending all
    leaves created so far in tree*/
    leafEnd = pos;
   
    /*Increment remainingSuffixCount indicating that a
    new suffix added to the list of suffixes yet to be
    added in tree*/
    remainingSuffixCount++;
   
    /*set lastNewNode to NULL while starting a new phase,
     indicating there is no internal node waiting for
     it's suffix link reset in current phase*/
    lastNewNode = NULL;
   
    //Add all suffixes (yet to be added) one by one in tree
    while(remainingSuffixCount > 0) {
   
        if (activeLength == 0)
            activeEdge = pos; //APCFALZ
   
        // There is no outgoing edge starting with
        // activeEdge from activeNode
        if (activeNode->children] == NULL)
        {
            //Extension Rule 2 (A new leaf edge gets created)
            activeNode->children] =
                                          newNode(pos, &leafEnd);
   
            /*A new leaf edge is created in above line starting
             from  an existng node (the current activeNode), and
             if there is any internal node waiting for it's suffix
             link get reset, point the suffix link from that last
             internal node to current activeNode. Then set lastNewNode
             to NULL indicating no more node waiting for suffix link
             reset.*/
            if (lastNewNode != NULL)
            {
                lastNewNode->suffixLink = activeNode;
                lastNewNode = NULL;
            }
        }
        // There is an outgoing edge starting with activeEdge
        // from activeNode
        else
        {
            // Get the next node at the end of edge starting
            // with activeEdge
            Node *next = activeNode->children];
            if (walkDown(next))//Do walkdown
            {
                //Start from next node (the new activeNode)
                continue;
            }
            /*Extension Rule 3 (current character being processed
              is already on the edge)*/
            if (text[next->start + activeLength] == text[pos])
            {
                //If a newly created node waiting for it's 
                //suffix link to be set, then set suffix link 
                //of that waiting node to current active node
                if(lastNewNode != NULL && activeNode != root)
                {
                    lastNewNode->suffixLink = activeNode;
                    lastNewNode = NULL;
                }
  
                //APCFER3
                activeLength++;
                /*STOP all further processing in this phase
                and move on to next phase*/
                break;
            }
   
            /*We will be here when activePoint is in middle of
              the edge being traversed and current character
              being processed is not  on the edge (we fall off
              the tree). In this case, we add a new internal node
              and a new leaf edge going out of that new node. This
              is Extension Rule 2, where a new leaf edge and a new
            internal node get created*/
            splitEnd = (int*) malloc(sizeof(int));
            *splitEnd = next->start + activeLength - 1;
   
            //New internal node
            Node *split = newNode(next->start, splitEnd);
            activeNode->children] = split;
   
            //New leaf coming out of new internal node
            split->children] = newNode(pos, &leafEnd);
            next->start += activeLength;
            split->children] = next;
   
            /*We got a new internal node here. If there is any
              internal node created in last extensions of same
              phase which is still waiting for it's suffix link
              reset, do it now.*/
            if (lastNewNode != NULL)
            {
            /*suffixLink of lastNewNode points to current newly
              created internal node*/
                lastNewNode->suffixLink = split;
            }
   
            /*Make the current newly created internal node waiting
              for it's suffix link reset (which is pointing to root
              at present). If we come across any other internal node
              (existing or newly created) in next extension of same
              phase, when a new leaf edge gets added (i.e. when
              Extension Rule 2 applies is any of the next extension
              of same phase) at that point, suffixLink of this node
              will point to that internal node.*/
            lastNewNode = split;
        }
   
        /* One suffix got added in tree, decrement the count of
          suffixes yet to be added.*/
        remainingSuffixCount--;
        if (activeNode == root && activeLength > 0) //APCFER2C1
        {
            activeLength--;
            activeEdge = pos - remainingSuffixCount + 1;
        }
        else if (activeNode != root) //APCFER2C2
        {
            activeNode = activeNode->suffixLink;
        }
    }
}
   
void print(int i, int j)
{
    int k;
    for (k=i; k<=j && text[k] != '#'; k++)
        printf("%c", text[k]);
    if(k<=j)
        printf("#");
}
   
//Print the suffix tree as well along with setting suffix index
//So tree will be printed in DFS manner
//Each edge along with it's suffix index will be printed
void setSuffixIndexByDFS(Node *n, int labelHeight)
{
    if (n == NULL)  return;
   
    if (n->start != -1) //A non-root node
    {
        //Print the label on edge from parent to current node
        print(n->start, *(n->end));
    }
    int leaf = 1;
    int i;
    for (i = 0; i < MAX_CHAR; i++)
    {
        if (n->children[i] != NULL)
        {
            if (leaf == 1 && n->start != -1)
                printf(" [%d]\n", n->suffixIndex);
   
            //Current node is not a leaf as it has outgoing
            //edges from it.
            leaf = 0;
            setSuffixIndexByDFS(n->children[i], labelHeight +
                                  edgeLength(n->children[i]));
        }
    }
    if (leaf == 1)
    {
        for(i= n->start; i<= *(n->end); i++)
        {
            if(text[i] == '#') //Trim unwanted characters
            {
                n->end = (int*) malloc(sizeof(int));
                *(n->end) = i;
            }
        }
        n->suffixIndex = size - labelHeight;
        printf(" [%d]\n", n->suffixIndex);
    }
}
   
void freeSuffixTreeByPostOrder(Node *n)
{
    if (n == NULL)
        return;
    int i;
    for (i = 0; i < MAX_CHAR; i++)
    {
        if (n->children[i] != NULL)
        {
            freeSuffixTreeByPostOrder(n->children[i]);
        }
    }
    if (n->suffixIndex == -1)
        free(n->end);
    free(n);
}
   
/*Build the suffix tree and print the edge labels along with
suffixIndex. suffixIndex for leaf edges will be >= 0 and
for non-leaf edges will be -1*/
void buildSuffixTree()
{
    size = strlen(text);
    int i;
    rootEnd = (int*) malloc(sizeof(int));
    *rootEnd = - 1;
   
    /*Root is a special node with start and end indices as -1,
    as it has no parent from where an edge comes to root*/
    root = newNode(-1, rootEnd);
   
    activeNode = root; //First activeNode will be root
    for (i=0; i

输出:(您可以看到下面的输出对应于上面显示的第二张图)

# [5]
$ [12]
a [-1]
# [4]
$ [11]
bx [-1]
a# [1]
ba$ [7]
b [-1]
a [-1]
$ [10]
bxba$ [6]
x [-1]
a# [2]
ba$ [8]
x [-1]
a [-1]
# [3]
bxa# [0]
ba$ [9]

如果两个字符串的大小分别为M和N,则此实现将占用O(M + N)的时间和空间。
如果输入字符串尚未串联,则总共需要2(M + N)个空间,M + N个空间用于存储广义后缀树,另一个M + N个空间用于存储串联的字符串。

跟进:
将以上实现扩展为两个以上的字符串(即,使用唯一的终端符号连接所有字符串,然后为连接的字符串构建后缀树)

这种方法的一个问题是每个输入字符串都需要唯一的终端符号。这仅适用于很少的字符串,但是如果输入字符串过多,我们可能找不到那么多唯一的终端符号。
我们将很快讨论构建通用后缀树的另一种方法,该方法将只需要一个唯一的终端符号,即可解决上述问题,并可用于为任意数量的输入字符串构建通用后缀树。

我们已经发布了更多有关后缀树应用程序的文章:

  • 后缀树应用程序1 –子字符串检查
  • 后缀树应用程序2 –搜索所有模式
  • 后缀树应用程序3 –最长重复子串
  • 后缀树应用程序4 –构建线性时间后缀数组
  • 后缀树应用程序5 –最长公共子串
  • 后缀树应用6 –最长回文子串