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

The approximate string matching of section 13.1. More...

Files

file  ADS/string/ApproximateMatching.h
 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 \)).

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

Detailed description

The approximate string matching of section 13.1.

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

Typedef documentation

◆ Table

using ADS::Table = std::vector< std::vector< int > >

the table \( \mathit{DP}[ i ][ j ] \) of the costs

Function documentation

◆ editDistance()

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

In pseudocode (section 13.4), the rows on the shorter string:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{editDistance}( P , T ) \cr 2 & \quad \textbf{if}\ m < n\ \textbf{then return} \ \mathrm{editDistance}( T , P )\quad \triangleright \ \text{the row on the shorter string} \cr 3 & \quad D \gets ( 0 , 1 , \ldots , n ) \cr 4 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ m\ \textbf{do} \cr 5 & \qquad \mathit{prev} \gets D;\ D[ 0 ] \gets i \cr 6 & \qquad D[ j ] \gets \min\{ D[ j - 1 ] + 1 , \mathit{prev}[ j ] + 1 , \mathit{prev}[ j - 1 ] + \delta_{ij} \} \text{ for } j = 1 , \ldots , n \cr 7 & \quad \textbf{return}\ D[ n ] \end{array}} \]

Postcondition
the distance is in \( [ | m - n | , \max( m , n ) ] \)

◆ matchingTable()

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

In pseudocode (section 13.1):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{matchingTable}( P , T , \mathit{edit} )\quad \triangleright\ \delta_{ij} = [ p_{i-1} \ne t_{j-1} ] \cr 2 & \quad \mathit{DP}[ i ][ 0 ] \gets i \text{ for every } i; \ \mathit{DP}[ 0 ][ j ] \gets j \text{ if } \mathit{edit} \text{, else } 0\quad \triangleright \ \text{an occurrence may start anywhere} \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ m\ \textbf{do} \cr 4 & \qquad \textbf{for}\ j \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 5 & \qquad\quad \mathit{DP}[ i ][ j ] \gets \min\{ \mathit{DP}[ i ][ j - 1 ] + 1 , \mathit{DP}[ i - 1 ][ j ] + 1 , \mathit{DP}[ i - 1 ][ j - 1 ] + \delta_{ij} \} \cr 6 & \quad \textbf{return}\ \mathit{DP} \end{array}} \]

◆ occurrence()

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

In pseudocode (exercise 13.1), each step undoing the term of the minimum that gave the value of the cell:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{occurrence}( \mathit{DP} , P , T , j )\quad \triangleright\ \text{the start of the occurrence ending at } j \cr 2 & \quad i \gets m \cr 3 & \quad \textbf{while}\ i > 0\ \textbf{do}\quad \triangleright \ \text{back along a choice that gives } \mathit{DP}[ i ][ j ] \cr 4 & \qquad \textbf{if}\ j > 0 \text{ and } \mathit{DP}[ i ][ j ] = \mathit{DP}[ i - 1 ][ j - 1 ] + \delta_{ij}\ \textbf{then}\ ( i , j ) \gets ( i - 1 , j - 1 ) \cr 5 & \qquad \textbf{else if}\ \mathit{DP}[ i ][ j ] = \mathit{DP}[ i - 1 ][ j ] + 1\ \textbf{then}\ i \gets i - 1\ \textbf{else}\ j \gets j - 1 \cr 6 & \quad \textbf{return}\ j \end{array}} \]

Precondition
\( \mathit{DP} \) is matchingTable( P , T ) and \( j \le |T| \)

◆ stringMatching()

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

In pseudocode (section 13.1), the least value of the last row, the column 0 left out:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{stringMatching}( P , T ) \cr 2 & \quad \mathit{DP} \gets \mathrm{matchingTable}( P , T , \textbf{false} ) \cr 3 & \quad \mathit{pos} \gets \text{the first } j \ge 1 \text{ of the least } \mathit{DP}[ m ][ j ]\quad \triangleright \ \text{the end of a best occurrence} \cr 4 & \quad \textbf{return}\ ( \mathit{pos} - 1 , \mathit{DP}[ m ][ \mathit{pos} ] ) \end{array}} \]

Postcondition
the cost is in \( [ 0 , m ] \)

◆ stringMatchingSingleRow()

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

In pseudocode (section 13.4), \( \mathit{prev} \) the row \( i - 1 \) and \( D \) the row \( i \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{stringMatchingSingleRow}( P , T )\quad \triangleright\ D \text{ the row } i \text{ of the table} \cr 2 & \quad D \gets n + 1 \text{ zeros} \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ m\ \textbf{do} \cr 4 & \qquad \mathit{prev} \gets D;\ D[ 0 ] \gets i \cr 5 & \qquad D[ j ] \gets \min\{ D[ j - 1 ] + 1 , \mathit{prev}[ j ] + 1 , \mathit{prev}[ j - 1 ] + \delta_{ij} \} \text{ for } j = 1 , \ldots , n \cr 6 & \quad \textbf{return}\ \min\{ D[ 1 ] , \ldots , D[ n ] \} \text{, or } m \text{ if } n = 0 \end{array}} \]