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

The engineered edit distance, longest common subsequence and approximate search, beyond the book. More...

Files

file  ADS/string/EditDistance.h
 The engineered edit distance, longest common subsequence and approximate search, beyond the book: the ones that the benchmark of the distances compares with edlib and rapidfuzz-cpp, while editDistance(), stringMatching() [see ApproximateMatching.h] and lcs() [see LongestCommonSubsequence.h] stay the dynamic programming of the textbooks, \( \Theta( m n ) \) time.

Functions

std::size_t ADS::levenshtein (std::string_view a, std::string_view b)
 the edit distance of Levenshtein of \( a \) and \( b \)
std::size_t ADS::levenshtein (std::string_view a, std::string_view b, std::size_t k)
 the edit distance of \( a \) and \( b \) if at most \( k \), \( k + 1 \) otherwise
std::size_t ADS::lcsLength (std::string_view a, std::string_view b)
 the length of a longest common subsequence of \( a \) and \( b \)
std::vector< std::pair< std::size_t, std::size_t > > ADS::approximateMatches (std::string_view T, std::string_view P, std::size_t k)
 the ends \( j \), from 1 to \( n \), of the substrings of \( T \) at edit distance at most \( k \) from \( P \), in increasing order, with their distances

Detailed description

The engineered edit distance, longest common subsequence and approximate search, beyond the book.

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

Function documentation

◆ approximateMatches()

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

the ends \( j \), from 1 to \( n \), of the substrings of \( T \) at edit distance at most \( k \) from \( P \), in increasing order, with their distances

In pseudocode (the search of Myers with the cut-off of Ukkonen, beyond the book), \( \mathit{score}[ b ] \) the cell at the bottom of the block \( b \) in the current column and \( y \) the last block computed:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{approximateMatches}( T , P , k ) \cr 2 & \quad P_v \gets 1 \ldots 1;\ M_v \gets 0;\ y \gets \min\{ B - 1 , \lfloor k / 64 \rfloor \} \cr 3 & \quad \textbf{for}\ j \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad h \gets 0\quad \triangleright\ \text{the top row, } D[ 0 ][ j ] = 0 \cr 5 & \qquad \textbf{for}\ b \gets 0\ \textbf{to}\ y\ \textbf{do} \ \text{move the block } b \text{ by } \mathit{Peq}[ T[ j - 1 ] ] \text{ and } h \text{, and update } \mathit{score}[ b ] \cr 6 & \qquad \textbf{if}\ y + 1 < B\ \textbf{and}\ \mathit{score}[ y ] \le k + 1\ \textbf{then}\ y \gets y + 1 \text{, the block entering all } +1 \cr 7 & \qquad \textbf{else while}\ y > 0\ \textbf{and}\ \mathit{score}[ y ] > k + 64\ \textbf{do}\ y \gets y - 1 \cr 8 & \qquad \textbf{if}\ y = B - 1\ \textbf{and}\ D[ m ][ j ] \le k \ \textbf{then}\ \text{report } ( j , D[ m ][ j ] ) \end{array}} \]

Postcondition
the \( j \) with \( \mathrm{matchingTable}( P , T )[ m ][ j ] \le k \) (an audit)

◆ lcsLength()

std::size_t ADS::lcsLength ( std::string_view a,
std::string_view b )
inlinenodiscard

the length of a longest common subsequence of \( a \) and \( b \)

In pseudocode (the algorithm of Allison and Dix in the form of Crochemore et al., beyond the book), \( a \) the shorter string, of \( m \) bytes, in blocks of 64 bits:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lcsLength}( a , b ) \cr 2 & \quad c \gets \text{the length of the common prefix and suffix,} \text{ stripped} \cr 3 & \quad V \gets 1^m \cr 4 & \quad \textbf{foreach}\ \text{byte } x \text{ of } b\ \textbf{do} \cr 5 & \qquad M \gets \mathit{Peq}[ x ];\ V \gets ( V + ( V \wedge M ) ) \vee ( V \wedge \neg M ) \cr 6 & \quad \textbf{return}\ c + \text{the number of 0 among the } m \text{ bits of } V \end{array}} \]

Postcondition
the length of lcs( a , b ) (an audit)

◆ levenshtein() [1/2]

std::size_t ADS::levenshtein ( std::string_view a,
std::string_view b )
inlinenodiscard

the edit distance of Levenshtein of \( a \) and \( b \)

In pseudocode (the diagonal transitions of Landau and Vishkin and the bit vectors of Myers in the band of Ukkonen, beyond the book), \( a \) the shorter string after the common prefix and suffix are stripped:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{levenshtein}( a , b ) \cr 2 & \quad \text{strip the common prefix and suffix};\ m \gets |a|;\ n \gets |b|\quad \triangleright\ m \le n \cr 3 & \quad \textbf{if}\ m = 0\ \textbf{then return}\ n \cr 4 & \quad \textbf{if}\ m \le 64\ \textbf{then return}\ \text{the distance } \text{by one word of Myers per column} \cr 5 & \quad \textbf{if}\ m > 128\ \textbf{then} \cr 6 & \qquad \textbf{for}\ h \gets 0 , 1 , \ldots \text{ up to } n / 48 + 128\ \textbf{do} \cr 7 & \qquad\quad \textbf{foreach}\ \text{diagonal } k\ \textbf{do} \ F_h( k ) \gets \mathit{slide}_k( \max\{ F_{h-1}( k ) + 1 , F_{h-1}( k + 1 ) + 1 , F_{h-1}( k - 1 ) \} ) \cr 8 & \qquad\quad \textbf{if}\ F_h( n - m ) = m\ \textbf{then return}\ h \cr 9 & \qquad\quad \textbf{if}\ h \in \{ 32 , 64 , \ldots \} \text{ and the } \text{guess } h m / r \text{ is too large}\ \textbf{then} \ \text{give up} \cr 10 & \quad t \gets \max\{ n - m , 64 , \text{the guess and a quarter} \} \cr 11 & \quad \textbf{loop} \cr 12 & \qquad \textbf{if}\ \text{the band of } t \text{ spans half the } \text{column or a quarter of the table is spent}\ \textbf{then} \cr 13 & \qquad\quad \textbf{return}\ \text{the distance by all the blocks, } \text{four columns at a time} \cr 14 & \qquad r \gets \text{the distance by the blocks in the band } i - j \in [ -t , t - ( n - m ) ] \cr 15 & \qquad \textbf{if}\ r \le t\ \textbf{then return}\ r \cr 16 & \qquad t \gets 2 t \end{array}} \]

Postcondition
editDistance( a , b ) (an audit)

◆ levenshtein() [2/2]

std::size_t ADS::levenshtein ( std::string_view a,
std::string_view b,
std::size_t k )
inlinenodiscard

the edit distance of \( a \) and \( b \) if at most \( k \), \( k + 1 \) otherwise

In pseudocode (beyond the book), the steps of levenshtein() above with \( k \) in place of the bounds it guesses:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{levenshtein}( a , b , k ) \cr 2 & \quad \text{strip the common prefix and suffix};\ m \gets |a|;\ n \gets |b|\quad \triangleright\ m \le n \cr 3 & \quad \textbf{if}\ n - m > k\ \textbf{then return}\ k + 1 \cr 4 & \quad \textbf{if}\ m = 0\ \textbf{then return}\ n \cr 5 & \quad \textbf{if}\ m \le 64\ \textbf{then return}\ \min\{ \text{the } \text{distance by one word} , k + 1 \} \cr 6 & \quad \textbf{if}\ m > 128 \text{ and } k \le n / 48 + 128 \ \textbf{then return}\ \min\{ \text{the diagonals up to } k , k + 1 \} \cr 7 & \quad \textbf{return}\ \text{the distance by the blocks in the band } \text{of } k \text{ (all the blocks if it is wide), or } k + 1 \end{array}} \]

Postcondition
\( \min\{ \mathrm{editDistance}( a , b ) , k + 1 \} \) (an audit)