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

The Lyndon factorization and the least rotation of a string, beyond the book. More...

Files

file  ADS/string/Lyndon.h
 The Lyndon factorization and the least rotation of a string, beyond the book.

Functions

bool ADS::isLyndon (std::string_view S)
 whether \( S \) is a Lyndon word, i.e., nonempty and smaller than all its proper suffixes, in \( O( n^2 ) \) by the definition
std::vector< std::size_t > ADS::lyndonFactorization (std::string_view S)
 the starts of the factors of the Lyndon factorization of \( S \), by Duval
std::size_t ADS::leastRotation (std::string_view S)
 the start of the least rotation of \( S \), 0 if \( S \) is empty

Detailed description

The Lyndon factorization and the least rotation of a string, beyond the book.

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

Function documentation

◆ isLyndon()

bool ADS::isLyndon ( std::string_view S)
inlinenodiscard

whether \( S \) is a Lyndon word, i.e., nonempty and smaller than all its proper suffixes, in \( O( n^2 ) \) by the definition

◆ leastRotation()

std::size_t ADS::leastRotation ( std::string_view S)
inlinenodiscard

the start of the least rotation of \( S \), 0 if \( S \) is empty

In pseudocode (Duval's algorithm, beyond the book), the scan of lyndonFactorization() on \( S S \), stopped at the factor that covers the place \( n \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{leastRotation}( S ) \cr 2 & \quad D \gets S S;\ i \gets 0;\ r \gets 0 \cr 3 & \quad \textbf{while}\ i < n\ \textbf{do} \cr 4 & \qquad r \gets i \cr 5 & \qquad ( w , j ) \gets \text{the length of } w \text{ and the end of } \text{the scan of lines 4 to 7 above on } D \text{ from } i \cr 6 & \qquad \textbf{while}\ i + w \le j\ \textbf{do}\ i \gets i + w \cr 7 & \quad \textbf{return}\ r \end{array}} \]

Postcondition
no rotation is smaller (audit)

◆ lyndonFactorization()

std::vector< std::size_t > ADS::lyndonFactorization ( std::string_view S)
inlinenodiscard

the starts of the factors of the Lyndon factorization of \( S \), by Duval

In pseudocode (Duval's algorithm, beyond the book), \( k \) the place \( |w| \) before \( j \), back to \( i \) when a longer Lyndon word starts at \( i \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lyndonFactorization}( S ) \cr 2 & \quad i \gets 0;\ F \gets \text{an empty sequence} \cr 3 & \quad \textbf{while}\ i < n\ \textbf{do} \cr 4 & \qquad j \gets i + 1;\ k \gets i \cr 5 & \qquad \textbf{while}\ j < n\ \textbf{and}\ S[ k ] \le S[ j ] \ \textbf{do} \cr 6 & \qquad\quad \textbf{if}\ S[ k ] < S[ j ]\ \textbf{then}\ k \gets i \ \textbf{else}\ k \gets k + 1 \cr 7 & \qquad\quad j \gets j + 1 \cr 8 & \qquad \textbf{while}\ i + ( j - k ) \le j\ \textbf{do}\ \text{append } i \text{ to } F;\ i \gets i + ( j - k ) \cr 9 & \quad \textbf{return}\ F \end{array}} \]

Postcondition
Lyndon words, non-increasing, that give \( S \) back (audit)