|
ADS
Algorithms and Data Structures
|
The approximate string matching of section 13.1: an occurrence of the pattern \( P = p_0 \cdots p_{m-1} \) in the text \( T = t_0 \cdots t_{n-1} \) with \( k \) errors, each a substitution, an insertion (a character of \( P \) not in \( T \)) or a deletion (a character of \( T \) not in \( P \)). More...
#include <algorithm>#include "ADS/Contract.h"#include <cstddef>#include <string>#include <utility>#include <vector>#include "ADS/string/impl/ApproximateMatching.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Typedefs | |
| using | ADS::Table = std::vector< std::vector< int > > |
| the table \( \mathit{DP}[ i ][ j ] \) of the costs | |
Functions | |
| template<class S> | |
| Table | ADS::matchingTable (const S &P, const S &T, bool edit=false) |
| the table \( \mathit{DP} \) of \( P \) and \( T \), of \( m + 1 \) rows and \( n + 1 \) columns; with edit the first row is \( \mathit{DP}[ 0 ][ j ] = j \), that of the edit distance | |
| template<class S> | |
| std::pair< long, int > | ADS::stringMatching (const S &P, const S &T) |
| the end of an occurrence of \( P \) in \( T \) with the fewest errors, the first one, and their number; \( ( -1 , m ) \) if \( T \) is empty | |
| template<class S> | |
| std::size_t | ADS::occurrence (const Table &DP, const S &P, const S &T, std::size_t j) |
| the place where the occurrence of \( P \) starts in \( T \) that ends at \( T[ j - 1 ] \) with \( \mathit{DP}[ m ][ j ] \) errors, walking back on the table | |
| template<class S> | |
| int | ADS::stringMatchingSingleRow (const S &P, const S &T) |
| the least number of errors of an occurrence of \( P \) in \( T \), with two rows of the table only | |
| template<class S> | |
| int | ADS::editDistance (const S &P, const S &T) |
| the edit distance of \( P \) and \( T \), in \( O( \min( m , n ) ) \) space | |
The approximate string matching of section 13.1: an occurrence of the pattern \( P = p_0 \cdots p_{m-1} \) in the text \( T = t_0 \cdots t_{n-1} \) with \( k \) errors, each a substitution, an insertion (a character of \( P \) not in \( T \)) or a deletion (a character of \( T \) not in \( P \)).
The value \( \mathit{DP}[ i ][ j ] \), the least \( k \) of an occurrence of the prefix \( P( i ) \) of \( i \) characters ending at the end of \( T( j ) \), is
\[ \mathit{DP}[ i ][ j ] = \left\{ \begin{array}{ll} 0 & i = 0 \\ i & j = 0 \cr \min\{ \mathit{DP}[ i - 1 ][ j - 1 ] + \delta , \mathit{DP}[ i - 1 ][ j ] + 1 , \mathit{DP}[ i ][ j - 1 ] + 1 \} & \mbox{otherwise} \end{array} \right. \]
with \( \delta = 0 \) if \( p_{i-1} = t_{j-1} \) and \( \delta = 1 \) otherwise, in \( \Theta( m n ) \) time; the best occurrences end where the last row is smallest. occurrence() walks back from \( \mathit{DP}[ m ][ j ] \) to where the occurrence starts (exercise 13.1); stringMatchingSingleRow() keeps two rows only, \( O( n ) \) space (section 13.4). editDistance(), the distance of Levenshtein, has \( \mathit{DP}[ 0 ][ j ] = j \), since \( T( j ) \) has \( j \) characters more than the empty prefix of \( P \), and its result in \( \mathit{DP}[ m ][ n ] \).
The contracts [see Contract.h]: occurrence() needs the table of \( P \) and \( T \) and a column of it; the best cost of stringMatching() is between 0 and \( m \), the edit distance between \( | m - n | \) and \( \max( m , n ) \). The operations are defined in ApproximateMatching.tpp.