ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::SuffixTree Class Reference

the suffix tree of \( T \$ \), its nodes in a vector, the root at 0 More...

#include <ADS/string/SuffixTree.h>

Classes

struct  Node
 a node \( u \); the edge into it is labeled by the symbols of \( T \) from pos plus the depth of the parent to pos + depth (the terminator at \( n \)) More...

Public types

enum class  Build { naive , mcCreight , suffixArray }
 the construction More...
using value_type = Node
 a node
using size_type = std::size_t
 the type of the names
using difference_type = std::ptrdiff_t
 a difference of names
using const_reference = const Node &
 a node
using reference = const_reference
 a node
using const_iterator = std::vector< Node >::const_iterator
 a constant iterator on the nodes
using iterator = const_iterator
 on the nodes

Public member functions

 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

Static public attributes

static constexpr std::size_t npos = std::numeric_limits< std::size_t >::max()
 no node, no suffix

Detailed description

the suffix tree of \( T \$ \), its nodes in a vector, the root at 0

Member typedef documentation

◆ const_iterator

using ADS::SuffixTree::const_iterator = std::vector< Node >::const_iterator

a constant iterator on the nodes

◆ const_reference

a node

◆ difference_type

using ADS::SuffixTree::difference_type = std::ptrdiff_t

a difference of names

◆ iterator

◆ reference

◆ size_type

using ADS::SuffixTree::size_type = std::size_t

the type of the names

◆ value_type

Member enumeration documentation

◆ Build

enum class ADS::SuffixTree::Build
strong

the construction

Enumerator
naive 

each suffix by a walk down from the root

mcCreight 

the suffix links of McCreight

suffixArray 

the leaves in the order of SA, climbing by LCP

Constructor and destructor documentation

◆ SuffixTree() [1/3]

ADS::SuffixTree::SuffixTree ( void )
default

the tree of no text, with no node

◆ SuffixTree() [2/3]

ADS::SuffixTree::SuffixTree ( std::string_view T,
Build b = Build::mcCreight )
explicit

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}} \]

◆ SuffixTree() [3/3]

ADS::SuffixTree::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

Precondition
every symbol of \( T \) is less than \( K \)

Member function documentation

◆ begin()

const_iterator ADS::SuffixTree::begin ( void ) const
inlinenodiscard

the root

◆ cbegin()

const_iterator ADS::SuffixTree::cbegin ( void ) const
inlinenodiscard

the root

◆ cend()

const_iterator ADS::SuffixTree::cend ( void ) const
inlinenodiscard

one past the last node

◆ count()

std::size_t ADS::SuffixTree::count ( std::string_view P) const
nodiscard

the number of occurrences of \( P \), the leaves below its locus (the terminator left out)

◆ end()

const_iterator ADS::SuffixTree::end ( void ) const
inlinenodiscard

one past the last node

◆ lca()

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()

◆ lce()

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 \)

◆ leaf()

std::size_t ADS::SuffixTree::leaf ( std::size_t i) const
nodiscard

the leaf of the suffix \( i \), the terminator being the suffix \( n \)

Precondition
\( i \le n \)

◆ locate()

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}} \]

◆ locus() [1/2]

std::size_t ADS::SuffixTree::locus ( const Symbols & P) const
nodiscard

the locus of locus() above, of an integer pattern

◆ locus() [2/2]

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}} \]

◆ node()

const Node & ADS::SuffixTree::node ( std::size_t u) const
nodiscard

the node \( u \)

Precondition
\( u \) is less than size()

◆ preorder()

std::vector< std::size_t > ADS::SuffixTree::preorder ( void ) const
nodiscard

the nodes in preorder, the children by increasing first symbol

◆ root()

std::size_t ADS::SuffixTree::root ( void ) const
inlinenodiscard

the root, the node 0

◆ size()

std::size_t ADS::SuffixTree::size ( void ) const
inlinenodiscard

the number of nodes

◆ symbol()

std::size_t ADS::SuffixTree::symbol ( std::size_t i) const
nodiscard

the symbol at the place \( i \) of \( T \$ \) as stored: the symbols of \( T \) plus 1, and 0 for the terminator at \( n \)

Precondition
\( i \le n \)

◆ textSize()

std::size_t ADS::SuffixTree::textSize ( void ) const
nodiscard

the length \( n \) of \( T \), without the terminator

◆ toSuffixArray()

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}} \]

Member data documentation

◆ npos

std::size_t ADS::SuffixTree::npos = std::numeric_limits< std::size_t >::max()
staticconstexpr

no node, no suffix


The documentation for this class was generated from the following file: