ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
StringMatching.h File Reference

The exact string matching, beyond the book: the occurrences of a pattern \( P \) of \( m \) characters in a text \( T \) of \( n \) characters, i.e., the places \( s \in [ 0 , n - m ] \) with \( T[ s .. s + m - 1 ] = P \). More...

#include <cstddef>
#include <cstdint>
#include "ADS/Contract.h"
#include <string_view>
#include <vector>
#include "ADS/string/impl/StringMatching.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

std::vector< std::size_t > ADS::naiveMatching (std::string_view T, std::string_view P)
 the places of all the occurrences of \( P \) in \( T \), by trying every shift
std::vector< long > ADS::failure (std::string_view P)
 the failure function: \( F[ q ] \) is the length of the longest proper border of \( P[ 0 .. q - 1 ] \), and \( F[ 0 ] = -1 \)
std::vector< std::size_t > ADS::kmp (std::string_view T, std::string_view P, long *comparisons=nullptr)
 the places of all the occurrences of \( P \) in \( T \), by Knuth-Morris-Pratt; the comparisons of characters go to *comparisons, if given
std::vector< long > ADS::strongFailure (std::string_view P)
 the strong failure function: \( G[ q ] = F[ q ] \) unless \( P[ F[ q ] ] = P[ q ] \), in which case \( G[ q ] = G[ F[ q ] ] \); \( G[ m ] = F[ m ] \)
std::vector< std::size_t > ADS::kmpStrong (std::string_view T, std::string_view P, long *comparisons=nullptr)
 the occurrences of kmp() above, with the strong failure function on a mismatch
std::vector< std::size_t > ADS::boyerMoore (std::string_view T, std::string_view P, long *comparisons=nullptr)
 the places of all the occurrences of \( P \) in \( T \), by Boyer-Moore with the bad character and the strong good suffix rules
std::vector< std::size_t > ADS::horspool (std::string_view T, std::string_view P, long *comparisons=nullptr)
 the places of all the occurrences of \( P \) in \( T \), by Horspool
std::vector< std::size_t > ADS::shiftOr (std::string_view T, std::string_view P)
 the places of all the occurrences of \( P \) in \( T \), by Shift-Or
std::vector< std::size_t > ADS::zFunction (std::string_view S)
 the Z-function: \( Z[ i ] \) is the length of the longest common prefix of \( S \) and \( S[ i .. ] \), and \( Z[ 0 ] = |S| \)
std::vector< std::size_t > ADS::zMatching (std::string_view T, std::string_view P)
 the places of all the occurrences of \( P \) in \( T \), by the Z-function of \( P \) and the Z-boxes of \( T \)
std::vector< std::size_t > ADS::twoWay (std::string_view T, std::string_view P)
 the places of all the occurrences of \( P \) in \( T \), by the Two-Way algorithm of Crochemore and Perrin

Detailed description

The exact string matching, beyond the book: the occurrences of a pattern \( P \) of \( m \) characters in a text \( T \) of \( n \) characters, i.e., the places \( s \in [ 0 , n - m ] \) with \( T[ s .. s + m - 1 ] = P \).

These are the algorithms of the textbooks, which count their comparisons of characters where asked; the engineered search of ADS is StringSearcher [see StringSearch.h].

  • naiveMatching() tries each shift of \( P \) along \( T \), \( O( n m ) \);
  • kmp(), the algorithm of Knuth, Morris and Pratt, first computes the failure function of \( P \), \( F[ q ] \) the length of the longest proper border of \( P[ 0 .. q - 1 ] \) (a border is both a prefix and a suffix), and then scans \( T \) once, shifting \( P \) by its failure on a mismatch, so that no character of \( T \) is compared again after a match: at most \( 2 n \) comparisons, \( \Theta( n + m ) \) time. strongFailure() is the failure function improved by Knuth, \( G[ q ] = G[ F[ q ] ] \) if \( P[ F[ q ] ] = P[ q ] \) and \( G[ q ] = F[ q ] \) otherwise, since a mismatch on \( P[ q ] \) would repeat on the character after the border; kmpStrong() is kmp() with it, which never compares a character of \( T \) twice against the same character of \( P \);
  • boyerMoore() compares \( P \) from right to left and shifts by the larger of the bad character rule (the last place of the character of \( T \) in \( P \)) and the strong good suffix rule (the next place of the suffix matched, preceded by another character, or the longest prefix of \( P \) that is its suffix), both prepared in \( O( m + \sigma ) \); \( O( n / m ) \) comparisons at best, \( O( n m ) \) in the worst case for all the occurrences (\( O( n ) \) for the first one);
  • horspool() shifts by the bad character of the last character of the window alone, \( \mathit{shift}[ c ] = m - 1 - \max\{ i < m - 1 : P[ i ] = c \} \) (\( m \) if none), the simplest and often the fastest of the family on large alphabets, \( O( n m ) \) in the worst case;
  • shiftOr() (\( m \le 64 \)) keeps in a word \( D \) the prefixes of \( P \) that end at the current character, a clear bit \( i \) when \( P[ 0 .. i ] \) does, and updates it as \( D \gets ( D \ll 1 ) \vee B[ T[ j ] ] \), with the bit \( i \) of \( B[ c ] \) clear if \( P[ i ] = c \): \( \Theta( n ) \) operations on words;
  • zFunction() gives \( Z[ i ] = \mathrm{lcp}( S , S[ i .. ] ) \) for every \( i \) in \( \Theta( n ) \), from the Z-box \( [ l , r ) \) that reaches farthest: inside it \( Z[ i ] \ge \min( Z[ i - l ] , r - i ) \), and only the characters past \( r \) are compared; zMatching() extends the boxes of \( T \) against \( P \) in the same way, \( \Theta( n + m ) \);
  • twoWay() is the Two-Way algorithm of Crochemore and Perrin [see StringSearch.h]: the critical factorization \( P = u v \) by two maximal suffixes, \( v \) compared from left to right and \( u \) from right to left, at most \( 2 n \) comparisons in \( O( 1 ) \) extra space.

The empty pattern occurs at every place \( 0 , \ldots , n \).

The contracts [see Contract.h]: the failure function gives proper borders, the Z-function values that fit the string, and every search the occurrences of naiveMatching() (checked at the level of the audits). The operations are defined in StringMatching.tpp.

See also
D. E. Knuth, J. H. Morris, V. R. Pratt, Fast pattern matching in strings, SIAM Journal on Computing 6(2), 323-350, 1977, doi:10.1137/0206024
R. S. Boyer, J. S. Moore, A fast string searching algorithm, Communications of the ACM 20(10), 762-772, 1977, doi:10.1145/359842.359859
R. N. Horspool, Practical fast searching in strings, Software: Practice and Experience 10(6), 501-506, 1980, doi:10.1002/spe.4380100608
R. A. Baeza-Yates, G. H. Gonnet, A new approach to text searching, Communications of the ACM 35(10), 74-82, 1992, doi:10.1145/135239.135243
D. Gusfield, Algorithms on Strings, Trees and Sequences, Cambridge University Press, 1997, section 1.4, doi:10.1017/CBO9780511574931
M. Crochemore, D. Perrin, Two-way string-matching, Journal of the ACM 38(3), 650-674, 1991, doi:10.1145/116825.116845
C. Charras, T. Lecroq, Handbook of Exact String Matching Algorithms, King's College Publications, 2004, ISBN 0-9543006-4-5
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa