ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SuffixApplications.h

Problems on strings solved by a suffix array or a suffix tree, beyond the book (those of chapter 10, Searching Strings by Substring, of Ferragina's book and their classical companions). More...

Files

file  ADS/string/SuffixApplications.h
 Problems on strings solved by a suffix array [see SuffixArray.h] or a suffix tree [see SuffixTree.h], beyond the book (those of chapter 10, Searching Strings by Substring, of Ferragina's book and their classical companions).

Classes

struct  ADS::CopyPhrase
 a phrase of the parsing of Ziv and Lempel: copy length symbols from distance places back, then append next, if any More...

Functions

std::pair< std::size_t, std::size_t > ADS::longestRepeatedSubstring (const SuffixArray &SA)
 the place and the length of a longest substring that occurs twice (0 and 0 if none)
std::optional< std::size_t > ADS::repeatedSubstring (const SuffixArray &SA, std::size_t L, std::size_t C)
 the place of a substring of length \( L \) that occurs at least \( C \) times, if any
std::uint64_t ADS::distinctSubstrings (const SuffixArray &SA)
 the number of distinct nonempty substrings, \( n ( n + 1 ) / 2 - \sum_r \mathit{LCP}[ r ] \)
std::string ADS::longestCommonSubstring (const std::vector< std::string > &S, std::size_t k)
 a longest substring common to at least \( k \) of the strings \( S \)
std::vector< std::pair< std::size_t, std::size_t > > ADS::maximalRepeats (const SuffixTree &ST)
 the maximal repeats, as the place of an occurrence and the length
bool ADS::proximity (const SuffixArray &SA, std::string_view P, std::string_view Q, std::size_t k)
 whether an occurrence of \( P \) and one of \( Q \) start at most \( k \) places apart
std::vector< std::size_t > ADS::kMismatches (std::string_view T, std::string_view P, std::size_t k)
 the places where \( P \) occurs in \( T \) with at most \( k \) mismatches
std::vector< CopyPhrase > ADS::lz77ByTree (std::string_view T)
 the parsing of Ziv and Lempel of \( T \), the copies from the farthest occurrence, by the suffix tree
std::string ADS::decodeCopyPhrases (const std::vector< CopyPhrase > &Z)
 the text of a parsing, each phrase copying its symbols one at a time, so that a copy may overlap the symbols it writes

Detailed description

Problems on strings solved by a suffix array or a suffix tree, beyond the book (those of chapter 10, Searching Strings by Substring, of Ferragina's book and their classical companions).

The definitions, the invariants and the costs of the area are in Theory of strings.

Function documentation

◆ decodeCopyPhrases()

std::string ADS::decodeCopyPhrases ( const std::vector< CopyPhrase > & Z)
inlinenodiscard

the text of a parsing, each phrase copying its symbols one at a time, so that a copy may overlap the symbols it writes

Precondition
each distance is at most the length decoded before it, and positive if its length is

◆ distinctSubstrings()

std::uint64_t ADS::distinctSubstrings ( const SuffixArray & SA)
inlinenodiscard

the number of distinct nonempty substrings, \( n ( n + 1 ) / 2 - \sum_r \mathit{LCP}[ r ] \)

◆ kMismatches()

std::vector< std::size_t > ADS::kMismatches ( std::string_view T,
std::string_view P,
std::size_t k )
inlinenodiscard

the places where \( P \) occurs in \( T \) with at most \( k \) mismatches

In pseudocode (after Landau and Vishkin, beyond the book), the extensions computed on the suffix array of \( T \# P \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{kMismatches}( T , P , k ) \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - p\ \textbf{do} \cr 3 & \qquad e \gets 0;\ j \gets 0 \cr 4 & \qquad \textbf{while}\ e \le k\ \textbf{and}\ j < p\ \textbf{do} \cr 5 & \qquad\quad j \gets j + \mathrm{lce}( T[ i + j .. ] , P[ j .. ] ) \cr 6 & \qquad\quad \textbf{if}\ j < p\ \textbf{then}\ e \gets e + 1;\ j \gets j + 1\quad \triangleright\ \text{a mismatch, skipped} \cr 7 & \qquad \textbf{if}\ e \le k\ \textbf{then}\ \text{report } i \end{array}} \]

◆ longestCommonSubstring()

std::string ADS::longestCommonSubstring ( const std::vector< std::string > & S,
std::size_t k )
inlinenodiscard

a longest substring common to at least \( k \) of the strings \( S \)

In pseudocode (beyond the book, as in Abouelhoda, Kurtz and Ohlebusch), \( \mathit{owner}( i ) \) the string to which the place \( i \) of the join belongs:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestCommonSubstring}( S , k ) \cr 2 & \quad \textbf{if}\ k = 1\ \textbf{then return}\ \text{a longest } \text{string of } S \cr 3 & \quad X \gets S_0 \, \$_0 \, S_1 \, \$_1 \cdots S_{m-1} \, \$_{m-1}\quad \triangleright\ \text{distinct separators, smaller than } \text{the bytes} \cr 4 & \quad A \gets \text{the suffix array of } X;\ \mathit{best} \gets 0;\ \mathit{lo} \gets 0 \cr 5 & \quad \textbf{for}\ \mathit{hi} \gets 0\ \textbf{to}\ |X| - 1 \ \textbf{do} \cr 6 & \qquad \text{add } \mathit{owner}( A[ \mathit{hi} ] ) \text{ to the } \text{counts of the window} \cr 7 & \qquad \textbf{while}\ \text{the window } [ \mathit{lo} , \mathit{hi} ] \text{ has suffixes of } k \text{ distinct strings} \ \textbf{do} \cr 8 & \qquad\quad \mathit{best} \gets \max\{ \mathit{best} , A.\mathrm{lce}( A[ \mathit{lo} ] , A[ \mathit{hi} ] ) \} \cr 9 & \qquad\quad \text{remove } \mathit{owner}( A[ \mathit{lo} ] ) \text{ from the counts};\ \mathit{lo} \gets \mathit{lo} + 1 \cr 10 & \quad \textbf{return}\ \text{the prefix of length } \mathit{best} \text{ of the suffix that gave it} \end{array}} \]

Precondition
\( 1 \le k \le |S| \)
Postcondition
a substring of at least \( k \) of the strings (an audit)

◆ longestRepeatedSubstring()

std::pair< std::size_t, std::size_t > ADS::longestRepeatedSubstring ( const SuffixArray & SA)
inlinenodiscard

the place and the length of a longest substring that occurs twice (0 and 0 if none)

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestRepeatedSubstring}( \mathit{SA} ) \cr 2 & \quad r \gets \text{the first rank with the largest } \mathit{LCP}[ r ] \cr 3 & \quad \textbf{return}\ ( \mathit{SA}[ r ] , \mathit{LCP}[ r ] ) \text{, or } ( 0 , 0 ) \text{ if } \mathit{LCP} \text{ is all 0} \end{array}} \]

◆ lz77ByTree()

std::vector< CopyPhrase > ADS::lz77ByTree ( std::string_view T)
inlinenodiscard

the parsing of Ziv and Lempel of \( T \), the copies from the farthest occurrence, by the suffix tree

In pseudocode (beyond the book), \( \mathit{least}( u ) \) the smallest suffix of a leaf below \( u \), computed by one visit:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lz77ByTree}( T ) \cr 2 & \quad i \gets 0 \cr 3 & \quad \textbf{while}\ i < n\ \textbf{do} \cr 4 & \qquad u \gets \mathit{root} \cr 5 & \qquad \textbf{loop} \cr 6 & \qquad\quad c \gets \text{the child of } u \text{ along the suffix } i;\ \textbf{if}\ \mathit{least}( c ) \ge i\ \textbf{then} \ \text{exit the loop} \cr 7 & \qquad\quad u \gets c \cr 8 & \qquad \ell \gets \mathit{depth}( u );\ d \gets i - \mathit{least}( u ) \text{ if } \ell > 0 \text{, else 0} \cr 9 & \qquad \text{append } ( d , \ell , T[ i + \ell ] ) \text{, without} \text{ the symbol if } i + \ell = n;\ i \gets i + \ell + 1 \end{array}} \]

Postcondition
decodeCopyPhrases() gives \( T \) back (an audit)

◆ maximalRepeats()

std::vector< std::pair< std::size_t, std::size_t > > ADS::maximalRepeats ( const SuffixTree & ST)
inlinenodiscard

the maximal repeats, as the place of an occurrence and the length

In pseudocode (section 7.12 of Gusfield, beyond the book), \( \mathit{left}( u ) \) the symbol before all the occurrences below \( u \), or diverse:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{maximalRepeats}( \mathit{ST} ) \cr 2 & \quad \textbf{foreach}\ \text{node } u \text{, the children} \text{ before the parents}\ \textbf{do} \cr 3 & \qquad \textbf{if}\ u \text{ is the leaf of the suffix } i \ \textbf{then}\ \mathit{left}( u ) \gets T[ i - 1 ] \text{, or the } \text{terminator if } i = 0 \cr 4 & \qquad \textbf{else if}\ \text{its children have the same } \mathit{left}\ \textbf{then}\ \mathit{left}( u ) \gets \text{that } \text{symbol} \cr 5 & \qquad \textbf{else}\ \mathit{left}( u ) \gets \text{diverse} \cr 6 & \quad \textbf{return}\ \text{the internal nodes but the root with } \mathit{left}( u ) = \text{diverse, in preorder} \end{array}} \]

◆ proximity()

bool ADS::proximity ( const SuffixArray & SA,
std::string_view P,
std::string_view Q,
std::size_t k )
inlinenodiscard

whether an occurrence of \( P \) and one of \( Q \) start at most \( k \) places apart

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{proximity}( \mathit{SA} , P , Q , k ) \cr 2 & \quad O \gets \text{the occurrences of } P \text{ and of } Q \text{, each marked with its pattern, sorted by place} \cr 3 & \quad \textbf{return}\ \text{whether two adjacent items of } O \text{ of different patterns are at most } k \text{ apart} \end{array}} \]

◆ repeatedSubstring()

std::optional< std::size_t > ADS::repeatedSubstring ( const SuffixArray & SA,
std::size_t L,
std::size_t C )
inlinenodiscard

the place of a substring of length \( L \) that occurs at least \( C \) times, if any

In pseudocode (beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{repeatedSubstring}( \mathit{SA} , L , C ) \cr 2 & \quad \textbf{if}\ L = 0\ \textbf{then return}\ 0 \text{ if } C \le n + 1 \text{, and } \textbf{nil} \text{ otherwise} \cr 3 & \quad \textbf{if}\ L > n\ \textbf{then return}\ \textbf{nil} \cr 4 & \quad \textbf{if}\ C = 1\ \textbf{then return}\ 0 \cr 5 & \quad \mathit{run} \gets 0 \cr 6 & \quad \textbf{for}\ r \gets 0\ \textbf{to}\ n - 2\ \textbf{do} \cr 7 & \qquad \textbf{if}\ \mathit{LCP}[ r ] \ge L\ \textbf{then} \cr 8 & \qquad\quad \mathit{run} \gets \mathit{run} + 1;\ \textbf{if} \ \mathit{run} \ge C - 1\ \textbf{then return}\ \mathit{SA}[ r ] \cr 9 & \qquad \textbf{else}\ \mathit{run} \gets 0 \cr 10 & \quad \textbf{return}\ \textbf{nil} \end{array}} \]

Precondition
\( C \ge 1 \)