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

The suffix array of a text \( T \) of \( n \) symbols, beyond the book. More...

Files

file  ADS/string/SuffixArray.h
 The suffix array of a text \( T \) of \( n \) symbols, beyond the book: \( \mathit{SA}[ r ] \) is the starting place of the \( r \)-th smallest suffix of \( T \) in the lexicographic order, a suffix that is a prefix of another being the smaller.

Classes

class  ADS::SuffixArray
 the suffix array of a text with its \( \mathit{LCP} \), \( \mathit{Llcp} \) and \( \mathit{Rlcp} \) arrays and the range minima of \( \mathit{LCP} \) More...

Typedefs

using ADS::Symbols = std::vector< std::size_t >
 a text on an integer alphabet

Functions

Symbols ADS::toSymbols (std::string_view T)
 the bytes of \( T \) as symbols in \( [ 0 , 256 ) \)
std::vector< std::size_t > ADS::suffixArraySorting (const Symbols &T)
 the suffix array of \( T \) by a sort of comparisons of its suffixes
std::vector< std::size_t > ADS::suffixArraySorting (std::string_view T)
 the array of suffixArraySorting() above, on the bytes of \( T \)
std::vector< std::size_t > ADS::suffixArrayDoubling (const Symbols &T)
 the suffix array of \( T \) by the prefix doubling of Manber and Myers
std::vector< std::size_t > ADS::suffixArrayDoubling (std::string_view T)
 the array of suffixArrayDoubling() above, on the bytes of \( T \)
std::vector< std::size_t > ADS::suffixArrayLarssonSadakane (const Symbols &T)
 the suffix array of \( T \) by the doubling of Larsson and Sadakane
std::vector< std::size_t > ADS::suffixArrayLarssonSadakane (std::string_view T)
 the array of suffixArrayLarssonSadakane() above, on the bytes of \( T \)
std::vector< std::size_t > ADS::suffixArraySkew (const Symbols &T, std::size_t K)
 the suffix array of \( T \) by the Skew algorithm of Kärkkäinen and Sanders
std::vector< std::size_t > ADS::suffixArraySkew (std::string_view T)
 the array of suffixArraySkew() above, on the bytes of \( T \)
std::vector< std::size_t > ADS::suffixArraySais (const Symbols &T, std::size_t K)
 the suffix array of \( T \) by the induced sorting of Nong, Zhang and Chan
std::vector< std::size_t > ADS::suffixArraySais (std::string_view T)
 the array of suffixArraySais() above, on the bytes of \( T \)
std::vector< std::size_t > ADS::suffixArrayScan (const Symbols &T, std::size_t m)
 the suffix array of \( T \) by the scan-based algorithm of Baeza-Yates, Gonnet and Snider, on blocks of \( m \) symbols
std::vector< std::size_t > ADS::suffixArrayScan (std::string_view T, std::size_t m)
 the array of suffixArrayScan() above, on the bytes of \( T \)
std::vector< std::size_t > ADS::inversePermutation (const std::vector< std::size_t > &SA)
 the inverse of a permutation: \( R[ \mathit{SA}[ r ] ] = r \)
std::vector< std::size_t > ADS::lcpArray (const Symbols &T, const std::vector< std::size_t > &SA)
 the LCP array of Kasai et al., \( n - 1 \) entries (none if \( n < 2 \))
std::vector< std::size_t > ADS::lcpArray (std::string_view T, const std::vector< std::size_t > &SA)
 the array of lcpArray() above, on the bytes of \( T \)

Detailed description

The suffix array of a text \( T \) of \( n \) symbols, beyond the book.

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

Typedef documentation

◆ Symbols

using ADS::Symbols = std::vector< std::size_t >

a text on an integer alphabet

Function documentation

◆ inversePermutation()

std::vector< std::size_t > ADS::inversePermutation ( const std::vector< std::size_t > & SA)
inlinenodiscard

the inverse of a permutation: \( R[ \mathit{SA}[ r ] ] = r \)

◆ lcpArray() [1/2]

std::vector< std::size_t > ADS::lcpArray ( const Symbols & T,
const std::vector< std::size_t > & SA )
inlinenodiscard

the LCP array of Kasai et al., \( n - 1 \) entries (none if \( n < 2 \))

In pseudocode (the algorithm of Kasai et al., beyond the book), the lcp of the suffix \( i + 1 \) with its predecessor in \( \mathit{SA} \) being at least \( h - 1 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lcpArray}( T , \mathit{SA} ) \cr 2 & \quad R \gets \text{the inverse of } \mathit{SA};\ h \gets 0 \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad \textbf{if}\ R[ i ] = 0\ \textbf{then}\ h \gets 0; \ \textbf{continue} \cr 5 & \qquad k \gets \mathit{SA}[ R[ i ] - 1 ] \cr 6 & \qquad \textbf{while}\ i + h < n\ \textbf{and}\ k + h < n \ \textbf{and}\ T[ i + h ] = T[ k + h ]\ \textbf{do}\ h \gets h + 1 \cr 7 & \qquad \mathit{LCP}[ R[ i ] - 1 ] \gets h \cr 8 & \qquad \textbf{if}\ h > 0\ \textbf{then}\ h \gets h - 1 \cr 9 & \quad \textbf{return}\ \mathit{LCP} \end{array}} \]

Precondition
\( \mathit{SA} \) is a suffix array of \( T \) (an audit)
Postcondition
\( \mathit{LCP}[ r ] \) is the lcp of the suffixes \( \mathit{SA}[ r ] \) and \( \mathit{SA}[ r + 1 ] \) (an audit)

◆ lcpArray() [2/2]

std::vector< std::size_t > ADS::lcpArray ( std::string_view T,
const std::vector< std::size_t > & SA )
inlinenodiscard

the array of lcpArray() above, on the bytes of \( T \)

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lcpArray}( T , \mathit{SA} ) \cr 2 & \quad \textbf{return}\ \mathrm{lcpArray}( \mathrm{toSymbols}( T ) , \mathit{SA} ) \end{array}} \]

Precondition
\( \mathit{SA} \) is a suffix array of \( T \) (an audit)

◆ suffixArrayDoubling() [1/2]

std::vector< std::size_t > ADS::suffixArrayDoubling ( const Symbols & T)
inlinenodiscard

the suffix array of \( T \) by the prefix doubling of Manber and Myers

In pseudocode (the doubling of Manber and Myers, beyond the book), after the round of \( k \) the rank \( R[ i ] \) being that of the prefix of \( k \) symbols of the suffix \( i \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArrayDoubling}( T ) \cr 2 & \quad \mathit{SA} \gets \text{the places sorted by } T[ i ] \text{ by } \text{radix sort};\ R[ i ] \gets \text{the rank of } T[ i ] \cr 3 & \quad k \gets 1 \cr 4 & \quad \textbf{while}\ R[ \mathit{SA}[ n - 1 ] ] < n - 1\ \textbf{and} \ k < n\ \textbf{do} \cr 5 & \qquad \mathit{key}( i ) \gets ( R[ i ] , R[ i + k ] + 1 )\quad \triangleright\ \text{the second item 0 if } i + k \ge n \cr 6 & \qquad \text{sort } \mathit{SA} \text{ by } \mathit{key} \text{ by } \text{LSD radix sort} \cr 7 & \qquad R'[ \mathit{SA}[ 0 ] ] \gets 0 \cr 8 & \qquad \textbf{for}\ r \gets 1\ \textbf{to}\ n - 1\ \textbf{do} \ R'[ \mathit{SA}[ r ] ] \gets R'[ \mathit{SA}[ r - 1 ] ] + [ \mathit{key}( \mathit{SA}[ r ] ) \ne \mathit{key}( \mathit{SA}[ r - 1 ] ) ] \cr 9 & \qquad R \gets R';\ k \gets 2 k \cr 10 & \quad \textbf{return}\ \mathit{SA} \end{array}} \]

Precondition
\( |T| < 2^{32} \)
Postcondition
a suffix array of \( T \) (an audit)

◆ suffixArrayDoubling() [2/2]

std::vector< std::size_t > ADS::suffixArrayDoubling ( std::string_view T)
inlinenodiscard

the array of suffixArrayDoubling() above, on the bytes of \( T \)

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArrayDoubling}( T ) \cr 2 & \quad \textbf{return}\ \mathrm{suffixArrayDoubling}( \mathrm{toSymbols}( T ) ) \end{array}} \]

Precondition
\( |T| < 2^{32} \)

◆ suffixArrayLarssonSadakane() [1/2]

std::vector< std::size_t > ADS::suffixArrayLarssonSadakane ( const Symbols & T)
inlinenodiscard

the suffix array of \( T \) by the doubling of Larsson and Sadakane

In pseudocode (qsufsort of Larsson and Sadakane, beyond the book), a group being a run of \( I \) whose suffixes share their first \( h \) symbols and \( V[ i ] \) the last place of the group of the suffix \( i \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArrayLarssonSadakane}( T ) \cr 2 & \quad I \gets \text{the places sorted stably by } T[ i ];\ \text{the } \text{groups of equal } T[ i ] \cr 3 & \quad h \gets 1 \cr 4 & \quad \textbf{while}\ \text{some group has two suffixes or more} \ \textbf{do} \cr 5 & \qquad \textbf{foreach}\ \text{group } I[ a .. b ] \text{ of two } \text{suffixes or more}\ \textbf{do} \cr 6 & \qquad\quad \text{sort } I[ a .. b ] \text{ by } V[ I[ k ] + h ]\quad \triangleright\ -1 \text{ past the end of } T \cr 7 & \qquad\quad \text{split } I[ a .. b ] \text{ into the runs of equal } \text{keys, and set } V \text{ of each suffix to the end of its run} \cr 8 & \qquad \text{join the runs of groups of one suffix, to be skipped} \cr 9 & \qquad h \gets 2 h \cr 10 & \quad \textbf{return}\ I \end{array}} \]

Postcondition
a suffix array of \( T \) (an audit)

◆ suffixArrayLarssonSadakane() [2/2]

std::vector< std::size_t > ADS::suffixArrayLarssonSadakane ( std::string_view T)
inlinenodiscard

the array of suffixArrayLarssonSadakane() above, on the bytes of \( T \)

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArrayLarssonSadakane}( T ) \cr 2 & \quad \textbf{return}\ \mathrm{suffixArrayLarssonSadakane}( \mathrm{toSymbols}( T ) ) \end{array}} \]

◆ suffixArraySais() [1/2]

std::vector< std::size_t > ADS::suffixArraySais ( const Symbols & T,
std::size_t K )
inlinenodiscard

the suffix array of \( T \) by the induced sorting of Nong, Zhang and Chan

In pseudocode (SA-IS of Nong, Zhang and Chan, beyond the book), on \( s \), the text followed by a symbol smaller than all, the suffix \( i \) of type S if it is smaller than the suffix \( i + 1 \) and L otherwise, and LMS if it is of type S and the suffix \( i - 1 \) of type L:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{sais}( s , K ) \cr 2 & \quad t[ n - 1 ] \gets \text{S};\ \textbf{for}\ i \gets n - 2 \ \textbf{downto}\ 0\ \textbf{do}\ t[ i ] \gets \text{S if } s[ i ] < s[ i + 1 ] \text{ or } \cr & \qquad ( s[ i ] = s[ i + 1 ] \text{ and } t[ i + 1 ] = \text{S),} \text{ L otherwise} \cr 3 & \quad \mathrm{induce}( \text{the LMS places in the order of the text} ) \quad \triangleright\ \text{sorts the LMS-substrings} \cr 4 & \quad s_1 \gets \text{the names of the LMS-substrings, in the order } \text{of the text, equal ones named alike} \cr 5 & \quad \textbf{if}\ \text{two names are equal}\ \textbf{then} \ \mathit{SA}_1 \gets \mathrm{sais}( s_1 , \text{the number of names} ) \cr 6 & \quad \textbf{else}\ \mathit{SA}_1 \gets \text{the inverse of } s_1 \cr 7 & \quad \mathrm{induce}( \text{the LMS places in the order of } \mathit{SA}_1 ) \cr 8 & \quad \textbf{return}\ \mathit{SA} \cr 9 & \textbf{procedure}\ \mathrm{induce}( L ) \cr 10 & \quad \mathit{SA} \gets \text{empty};\ \textbf{foreach}\ i \in L \text{, from the last}\ \textbf{do}\ \text{put } i \text{ at the } \text{tail of the bucket of } s[ i ] \cr 11 & \quad \textbf{for}\ r \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 12 & \qquad \textbf{if}\ j = \mathit{SA}[ r ] > 0\ \textbf{and}\ t[ j - 1 ] = \text{L}\ \textbf{then}\ \text{put } j - 1 \text{ at the head of } \text{the bucket of } s[ j - 1 ] \cr 13 & \quad \textbf{for}\ r \gets n - 1\ \textbf{downto}\ 0\ \textbf{do} \cr 14 & \qquad \textbf{if}\ j = \mathit{SA}[ r ] > 0\ \textbf{and}\ t[ j - 1 ] = \text{S}\ \textbf{then}\ \text{put } j - 1 \text{ at the tail of } \text{the bucket of } s[ j - 1 ] \end{array}} \]

Precondition
every symbol of \( T \) is less than \( K \)
Postcondition
a suffix array of \( T \) (an audit)

◆ suffixArraySais() [2/2]

std::vector< std::size_t > ADS::suffixArraySais ( std::string_view T)
inlinenodiscard

the array of suffixArraySais() above, on the bytes of \( T \)

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArraySais}( T ) \cr 2 & \quad \textbf{return}\ \mathrm{suffixArraySais}( \mathrm{toSymbols}( T ) , 256 ) \end{array}} \]

◆ suffixArrayScan() [1/2]

std::vector< std::size_t > ADS::suffixArrayScan ( const Symbols & T,
std::size_t m )
inlinenodiscard

the suffix array of \( T \) by the scan-based algorithm of Baeza-Yates, Gonnet and Snider, on blocks of \( m \) symbols

In pseudocode (the algorithm of Baeza-Yates, Gonnet and Snider, beyond the book), \( \mathit{SA} \) the suffix array of the suffixes that start before the block:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArrayScan}( T , m ) \cr 2 & \quad \mathit{SA} \gets (\,) \cr 3 & \quad \textbf{foreach}\ s \in \{ 0 , m , 2 m , \ldots \} \text{ with } s < n\ \textbf{do} \cr 4 & \qquad B \gets \text{the suffixes that start in } T[ s .. s + m - 1 ] \text{, sorted by comparisons} \cr 5 & \qquad C[ 0 .. |B| ] \gets 0 \cr 6 & \qquad \textbf{for}\ j \gets 0\ \textbf{to}\ s - 1\ \textbf{do} \ C[ \text{the number of suffixes of } B \text{ not greater than } j ] \gets C[ \ldots ] + 1 \cr 7 & \qquad \mathit{SA} \gets \text{for } q = 0 , \ldots , |B| \text{, the } \text{next } C[ q ] \text{ entries of } \mathit{SA} \text{ followed} \cr & \qquad\quad \text{by } B[ q ] \text{ if } q < |B| \cr 8 & \quad \textbf{return}\ \mathit{SA} \end{array}} \]

Precondition
\( m > 0 \)
Postcondition
a suffix array of \( T \) (an audit)

◆ suffixArrayScan() [2/2]

std::vector< std::size_t > ADS::suffixArrayScan ( std::string_view T,
std::size_t m )
inlinenodiscard

the array of suffixArrayScan() above, on the bytes of \( T \)

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArrayScan}( T , m ) \cr 2 & \quad \textbf{return}\ \mathrm{suffixArrayScan}( \mathrm{toSymbols}( T ) , m ) \end{array}} \]

Precondition
\( m > 0 \)

◆ suffixArraySkew() [1/2]

std::vector< std::size_t > ADS::suffixArraySkew ( const Symbols & T,
std::size_t K )
inlinenodiscard

the suffix array of \( T \) by the Skew algorithm of Kärkkäinen and Sanders

In pseudocode (the Skew algorithm of Kärkkäinen and Sanders, beyond the book), \( T \) followed by three symbols smaller than all and \( \mathrm{rank}( i ) \) the rank of the suffix \( i \) among the sample:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{skew}( T , K ) \cr 2 & \quad S_{12} \gets \text{the places } i \text{ with } i \bmod 3 \ne 0 \text{, sorted by } T[ i .. i + 2 ] \text{ by three passes of} \cr & \qquad \text{radix sort} \cr 3 & \quad N \gets \text{the ranks of the triples, the places } i \bmod 3 = 1 \text{ first and } i \bmod 3 = 2 \text{ after} \cr 4 & \quad \textbf{if}\ \text{two triples are equal}\ \textbf{then} \ S_{12} \gets \text{the places of } \mathrm{skew}( N , \max N ) \cr 5 & \quad S_0 \gets \text{the places } i \bmod 3 = 0 \text{, sorted by } ( T[ i ] , \mathrm{rank}( i + 1 ) ) \text{ by radix sort} \cr 6 & \quad \textbf{return}\ \text{the merge of } S_0 \text{ and } S_{12} \text{, comparing } i \in S_0 \text{ with } j \in S_{12} \text{ by} \cr & \qquad ( T[ i ] , \mathrm{rank}( i + 1 ) ) \text{ if } j \bmod 3 = 1 \text{, by } ( T[ i ] , T[ i + 1 ] , \mathrm{rank}( i + 2 ) ) \text{ if } j \bmod 3 = 2 \end{array}} \]

Precondition
every symbol of \( T \) is less than \( K \)
Postcondition
a suffix array of \( T \) (an audit)

◆ suffixArraySkew() [2/2]

std::vector< std::size_t > ADS::suffixArraySkew ( std::string_view T)
inlinenodiscard

the array of suffixArraySkew() above, on the bytes of \( T \)

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArraySkew}( T ) \cr 2 & \quad \textbf{return}\ \mathrm{suffixArraySkew}( \mathrm{toSymbols}( T ) , 256 ) \end{array}} \]

◆ suffixArraySorting() [1/2]

std::vector< std::size_t > ADS::suffixArraySorting ( const Symbols & T)
inlinenodiscard

the suffix array of \( T \) by a sort of comparisons of its suffixes

In pseudocode (chapter 10, Searching Strings by Substring, of Ferragina's book, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArraySorting}( T ) \cr 2 & \quad \mathit{SA} \gets ( 0 , 1 , \ldots , n - 1 ) \cr 3 & \quad \text{sort } \mathit{SA} \text{ by comparing the suffixes } T[ \mathit{SA}[ r ] .. n - 1 ] \text{ symbol by symbol} \cr 4 & \quad \textbf{return}\ \mathit{SA} \end{array}} \]

Postcondition
a suffix array of \( T \) (an audit)

◆ suffixArraySorting() [2/2]

std::vector< std::size_t > ADS::suffixArraySorting ( std::string_view T)
inlinenodiscard

the array of suffixArraySorting() above, on the bytes of \( T \)

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{suffixArraySorting}( T ) \cr 2 & \quad \textbf{return}\ \mathrm{suffixArraySorting}( \mathrm{toSymbols}( T ) ) \end{array}} \]

◆ toSymbols()

Symbols ADS::toSymbols ( std::string_view T)
inlinenodiscard

the bytes of \( T \) as symbols in \( [ 0 , 256 ) \)