|
| | SuffixTree (void)=default |
| | the tree of no text, with no node
|
| | SuffixTree (std::string_view T, Build b=Build::mcCreight) |
| | the suffix tree of the bytes of \( T \), built as b says
|
| | SuffixTree (const Symbols &T, std::size_t K, Build b=Build::mcCreight) |
| | the suffix tree of an integer text, built as the one of the bytes above
|
| std::size_t | textSize (void) const |
| | the length \( n \) of \( T \), without the terminator
|
| std::size_t | root (void) const |
| | the root, the node 0
|
| const Node & | node (std::size_t u) const |
| | the node \( u \)
|
| std::size_t | leaf (std::size_t i) const |
| | the leaf of the suffix \( i \), the terminator being the suffix \( n \)
|
| std::size_t | symbol (std::size_t i) const |
| | the symbol at the place \( i \) of \( T \$ \) as stored: the symbols of \( T \) plus 1, and 0 for the terminator at \( n \)
|
| std::size_t | locus (std::string_view P) const |
| | the extended locus of \( P \), or npos if \( P \) does not occur
|
| std::size_t | locus (const Symbols &P) const |
| | the locus of locus() above, of an integer pattern
|
| std::size_t | count (std::string_view P) const |
| | the number of occurrences of \( P \), the leaves below its locus (the terminator left out)
|
| std::vector< std::size_t > | locate (std::string_view P) const |
| | the places of the occurrences of \( P \), in lexicographic order of their suffixes; the empty pattern at the \( n \) places of the suffixes
|
| std::size_t | lca (std::size_t u, std::size_t v) const |
| | the lowest common ancestor of \( u \) and \( v \), \( O( 1 ) \)
|
| std::size_t | lce (std::size_t i, std::size_t j) const |
| | the length of the longest common prefix of the suffixes \( i \) and \( j \) of \( T \)
|
| std::pair< std::vector< std::size_t >, std::vector< std::size_t > > | toSuffixArray (void) const |
| | \( \mathit{SA} \) and \( \mathit{LCP} \) of \( T \), by a visit in order
|
| std::vector< std::size_t > | preorder (void) const |
| | the nodes in preorder, the children by increasing first symbol
|
| std::size_t | size (void) const |
| | the number of nodes
|
| const_iterator | begin (void) const |
| | the root
|
| const_iterator | end (void) const |
| | one past the last node
|
| const_iterator | cbegin (void) const |
| | the root
|
| const_iterator | cend (void) const |
| | one past the last node
|
the suffix tree of \( T \$ \), its nodes in a vector, the root at 0
the suffix tree of the bytes of \( T \), built as b says
In pseudocode (the algorithm of McCreight, beyond the book), the three constructions; \( \mathrm{scan}( u , i ) \) walks down from \( u \) along the suffix \( i \), comparing every symbol, splits the edge where they differ and returns the node reached, and \( \mathrm{leaf}( u , i ) \) hangs the leaf of the suffix \( i \) below \( u \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{naive}( T ) \cr
2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n\ \textbf{do}
\ \mathrm{leaf}( \mathrm{scan}( \mathit{root} , i ) , i ) \cr
3 & \textbf{procedure}\ \mathrm{mcCreight}( T ) \cr
4 & \quad \mathrm{leaf}( \mathit{root} , 0 );\ h \gets \mathit{root}
\quad \triangleright\ \text{the node of } \mathit{head}_{i-1} \cr
5 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr
6 & \qquad \textbf{if}\ \mathit{link}( h ) = \textbf{nil}\ \textbf{then}
\cr
7 & \qquad\quad w \gets \text{the node of } s[ h ] \text{ without its }
\text{first symbol, rescanned from } \mathit{link}( \mathit{parent}(
h ) ) \cr
& \qquad\qquad \text{by the first symbol of each edge, splitting the }
\text{last one if needed};\ \mathit{link}( h ) \gets w \cr
8 & \qquad h \gets \mathrm{scan}( \mathit{link}( h ) , i );
\ \mathrm{leaf}( h , i ) \cr
9 & \textbf{procedure}\ \mathrm{fromSuffixArray}( T ) \cr
10 & \quad ( \mathit{SA} , \mathit{LCP} ) \gets \text{the arrays of }
\mathrm{suffixArraySais}() \text{ and } \mathrm{lcpArray}() \cr
11 & \quad x \gets \mathrm{leaf}( \mathit{root} , n )\quad
\triangleright\ \text{the terminator, the smallest suffix} \cr
12 & \quad \textbf{for}\ r \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr
13 & \qquad l \gets \mathit{LCP}[ r - 1 ] \text{, or 0 if } r = 0 \cr
14 & \qquad \textbf{while}\ \mathit{depth}( \mathit{parent}( x ) ) > l
\ \textbf{do}\ x \gets \mathit{parent}( x ) \cr
15 & \qquad u \gets \mathit{parent}( x ) \text{, or a new node at depth }
l \text{ on the edge to } x \text{ if } \mathit{depth}( u ) < l \cr
16 & \qquad x \gets \mathrm{leaf}( u , \mathit{SA}[ r ] )
\end{array}}
\]
| std::size_t ADS::SuffixTree::lca |
( |
std::size_t | u, |
|
|
std::size_t | v ) const |
|
nodiscard |
the lowest common ancestor of \( u \) and \( v \), \( O( 1 ) \)
In pseudocode (the reduction of Bender and Farach-Colton, beyond the book), \( E \) the Euler tour of the tree, \( D \) the depths of its nodes and \( F[ u ] \) the first place of \( u \) in it:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{lca}( u , v ) \cr
2 & \quad a \gets \min\{ F[ u ] , F[ v ] \};\ b \gets \max\{ F[ u ] ,
F[ v ] \} \cr
3 & \quad \textbf{return}\ E[ \arg\min_{a \le k \le b} D[ k ] ]\quad
\triangleright\ \text{a query of range minimum}
\end{array}}
\]
- Precondition
- \( u \) and \( v \) are less than size()
| std::size_t ADS::SuffixTree::lce |
( |
std::size_t | i, |
|
|
std::size_t | j ) const |
|
nodiscard |
the length of the longest common prefix of the suffixes \( i \) and \( j \) of \( T \)
In pseudocode (beyond the book), the lowest common ancestor of two leaves spelling the longest common prefix of their suffixes:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{lce}( i , j ) \cr
2 & \quad \textbf{if}\ i = n\ \textbf{or}\ j = n\ \textbf{then return}
\ 0 \cr
3 & \quad \textbf{if}\ i = j\ \textbf{then return}\ n - i \cr
4 & \quad \textbf{return}\ \mathit{depth}( \mathrm{lca}( \mathrm{leaf}( i
) , \mathrm{leaf}( j ) ) )
\end{array}}
\]
- Precondition
- \( i \le n \) and \( j \le n \)
| std::vector< std::size_t > ADS::SuffixTree::locate |
( |
std::string_view | P | ) |
const |
|
nodiscard |
the places of the occurrences of \( P \), in lexicographic order of their suffixes; the empty pattern at the \( n \) places of the suffixes
In pseudocode (beyond the book), the subtree of the locus having as many leaves as occurrences and fewer internal nodes:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{locate}( P ) \cr
2 & \quad u \gets \mathrm{locus}( P );\ \textbf{if}\ u = \textbf{nil}
\ \textbf{then return}\ (\,) \cr
3 & \quad \textbf{return}\ \text{the suffixes of the leaves below } u
\text{ but } n \text{, by a visit in preorder}
\end{array}}
\]
| std::size_t ADS::SuffixTree::locus |
( |
std::string_view | P | ) |
const |
|
nodiscard |
the extended locus of \( P \), or npos if \( P \) does not occur
In pseudocode (chapter 10, Searching Strings by Substring, of Ferragina's book, beyond the book), the extended locus being the highest node whose string has \( P \) as a prefix:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{locus}( P ) \cr
2 & \quad u \gets \mathit{root};\ k \gets 0 \cr
3 & \quad \textbf{while}\ k < p\ \textbf{do} \cr
4 & \qquad c \gets \text{the child of } u \text{ by } P[ k ];
\ \textbf{if}\ c = \textbf{nil}\ \textbf{then return}\ \textbf{nil}
\cr
5 & \qquad \textbf{foreach}\ \text{further symbol of the edge to } c
\text{, while } k < p\ \textbf{do} \cr
6 & \qquad\quad \textbf{if}\ \text{it differs from } P[ k ]
\ \textbf{then return}\ \textbf{nil} \cr
7 & \qquad u \gets c \cr
8 & \quad \textbf{return}\ u
\end{array}}
\]
| std::pair< std::vector< std::size_t >, std::vector< std::size_t > > ADS::SuffixTree::toSuffixArray |
( |
void | | ) |
const |
|
nodiscard |
\( \mathit{SA} \) and \( \mathit{LCP} \) of \( T \), by a visit in order
In pseudocode (beyond the book), the internal node met between two leaves of a visit in order spelling their lcp:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{toSuffixArray}() \cr
2 & \quad \textbf{foreach}\ \text{node } u \text{ in preorder, the }
\text{children by increasing first symbol}\ \textbf{do} \cr
3 & \qquad \textbf{foreach}\ \text{child } c \text{ of } u\ \textbf{do}
\cr
4 & \qquad\quad \textbf{if}\ c \text{ is not the first child}
\ \textbf{then}\ \text{append } \mathit{depth}( u ) \text{ to }
\mathit{LCP} \cr
5 & \qquad\quad \textbf{if}\ c \text{ is a leaf}\ \textbf{then}
\ \text{append its suffix to } \mathit{SA}\ \textbf{else}
\ \text{visit } c \cr
6 & \quad \text{drop the first entry of } \mathit{SA} \text{ and of }
\mathit{LCP}\quad \triangleright\ \text{the terminator} \cr
7 & \quad \textbf{return}\ ( \mathit{SA} , \mathit{LCP} )
\end{array}}
\]