|
ADS
Algorithms and Data Structures
|
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. More...
#include "ADS/Contract.h"#include <cstddef>#include <cstdint>#include <string_view>#include <utility>#include <vector>#include "ADS/string/impl/EditDistance.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
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 | |
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.
For a pattern \( P \) of \( m \) bytes (the column) and a text \( T \) of \( n \) bytes, the table of the distances is
\[ D[ i ][ j ] = \min\{ D[ i - 1 ][ j - 1 ] + [ P[ i - 1 ] \ne T[ j - 1 ] ] , D[ i - 1 ][ j ] + 1 , D[ i ][ j - 1 ] + 1 \} , \]
with \( D[ i ][ 0 ] = i \) and \( D[ 0 ][ j ] = j \) for the distance (0 for the search), whose adjacent cells differ by \( -1 \), 0 or \( +1 \). The bit vectors of Myers keep a column as the vertical differences \( D[ i ][ j ] - D[ i - 1 ][ j ] \), \( +1 \) in \( P_v \) and \( -1 \) in \( M_v \), 64 rows a word (a block), and \( \mathit{Eq} = \mathit{Peq}[ T[ j - 1 ] ] \), the places of \( P \) that hold the byte, moves a block to the next column with the horizontal difference \( h_{in} \) at its top, in the formulation of Hyyrö:
\[ \begin{array}{l} X_v = \mathit{Eq} \vee M_v , \quad \mathit{Eq}' = \mathit{Eq} \vee [ h_{in} < 0 ] , \quad X_h = ( ( ( \mathit{Eq}' \wedge P_v ) + P_v ) \oplus P_v ) \vee \mathit{Eq}' , \cr P_h = M_v \vee \neg( X_h \vee P_v ) , \quad M_h = P_v \wedge X_h , \quad h_{out} = P_h[ 63 ] - M_h[ 63 ] , \cr P_h \leftarrow ( P_h \ll 1 ) \vee [ h_{in} > 0 ] , \quad M_h \leftarrow ( M_h \ll 1 ) \vee [ h_{in} < 0 ] , \quad P_v \leftarrow M_h \vee \neg( X_v \vee P_h ) , \quad M_v \leftarrow P_h \wedge X_v , \end{array} \]
a dozen operations on words with no branch, so that a column costs \( O( \lceil m / 64 \rceil ) \) of them.
\[ F_h( k ) = \mathit{slide}_k\bigl( \max\{ F_{h-1}( k ) + 1 , F_{h-1}( k + 1 ) + 1 , F_{h-1}( k - 1 ) \} \bigr) , \]
\( \mathit{slide}_k( i ) \) the row reached from \( i \) along the matches of the diagonal (compared 8 bytes at a time), and the distance is the least \( d \) with \( F_d( n - m ) = m \), \( O( n + d^2 ) \) expected time on random strings and \( O( d ) \) space. They are used for \( d \) up to \( n / 48 + 128 \) (if \( m > 128 \)), where they cost about as much as the blocks, and give up once the rows \( r \) reached by \( h = 32 , 64 , 128 , \ldots \) edits suggest a distance \( h m / r \) above twice as much. Then the blocks, restricted to the diagonal band of Ukkonen of the cells that a path of cost at most \( k \) can cross, \( i - j \in [ -k , k - ( n - m ) ] \), and in it, as in edlib, to the blocks that may still lead to the end within \( k \): a cell \( ( i , j ) \) costs at least \( D[ i ][ j ] + | ( m - i ) - ( n - j ) | \) to the end, and a cell of a block at least the bottom of the block minus its distance from it, so that the first and the last block are dropped when the least of these bounds exceeds \( k \); the block below the last one enters, all \( +1 \), when the bottom of the last is at most \( k + 1 \), since the cells at most \( k \) grow by at most one row per column. The cells outside the band are taken as larger, which overestimates only cells that no path of cost \( k \) crosses. The bound \( k \) starts from the guess of the diagonals and a quarter more, and doubles until the distance is at most \( k \), \( O( n d / 64 ) \) for distance \( d \) in the usual cases. Once the bands have computed a quarter of the \( n \lceil m / 64 \rceil \) blocks of the table, or span half of the column, all the blocks are computed instead, four columns at a time: the block \( b \) of the column \( j + q \) needs only the block \( b - 1 \) of the same column and the block \( b \) of the column before, so that the blocks \( i - q \) of the columns \( j + q \), \( q < 4 \), are independent, the four lanes of the vectors of Highway with ADS_WITH_HIGHWAY (the bands of 8 blocks or more go the same way), and four chains of operations that overlap in the processor without. levenshtein( a , b , k ) does the same with \( k \) fixed (the diagonals up to \( k \), or the band of \( k \)) and gives \( k + 1 \) for any larger distance.\[ V \leftarrow ( V + ( V \wedge M ) ) \vee ( V \wedge \neg M ) , \]
the addition carried between the blocks, and the length is the number of 0 among the \( m \) bits of \( V \), \( O( n \lceil m / 64 \rceil ) \) after the common prefix and suffix are stripped.The bits \( \mathit{Peq} \) take \( 256 \lceil m / 64 \rceil \) words (in the object up to 4 blocks, with only the bytes of the two strings cleared beyond). The contracts [see Contract.h]: the results equal those of the dynamic programming, editDistance(), lcs() and the last row of matchingTable() (audits, \( O( m n ) \) time). The functions are defined in EditDistance.tpp.