String-searching algorithm
In computer science, string-searching algorithms, sometimes called string-matching algorithms, are an important class of string algorithms that try to find a place where one or several strings are found within a larger string or text.
A basic example of string searching is when the pattern and the searched text are arrays of elements of an alphabet Σ. Σ may be a human language alphabet, for example, the letters A through Z and other applications may use a binary alphabet or a DNA alphabet in bioinformatics.
In practice, the method of feasible string-search algorithm may be affected by the string encoding. In particular, if a variable-width encoding is in use, then it may be slower to find the Nth character, perhaps requiring time proportional to N. This may significantly slow some search algorithms. One of many possible solutions is to search for the sequence of code units instead, but doing so may produce false matches unless the encoding is specifically designed to avoid it.
Kinds of searching
The most basic case of string searching involves one string, sometimes called the haystack, and one string, sometimes called the needle. The goal is to find one or more occurrences of the needle within the haystack. For example, one might search for to within:Some books are to be tasted, others to be swallowed, and some few to be chewed and digested.
One might request the first occurrence of "to", which is the fourth word; or all occurrences, of which there are 3; or the last, which is the fifth word from the end.
Very commonly, however, various constraints are added. For example, one might want to match the "needle" only where it consists of one complete words—perhaps defined as not having other letters immediately adjacent on either side. In that case a search for "hew" or "low" should fail for the example sentence above, even though those literal strings do occur.
Another common example involves "normalization". For many purposes, a search for a phrase such as "to be" should succeed even in places where there is something else intervening between the "to" and the "be":
- More than one space
- Other "whitespace" characters such as tabs, non-breaking spaces, line-breaks, etc.
- Less commonly, a hyphen or soft hyphen
- In structured texts, tags or even arbitrarily large but "parenthetical" things such as footnotes, list-numbers or other markers, embedded images, and so on.
- Latin-based alphabets distinguish lower-case from upper-case, but for many purposes string search is expected to ignore the distinction.
- Many languages include ligatures, where one composite character is equivalent to two or more other characters.
- Many writing systems involve diacritical marks such as accents or vowel points, which may vary in their usage, or be of varying importance in matching.
- DNA sequences can involve non-coding segments which may be ignored for some purposes, or polymorphisms that lead to no change in the encoded proteins, which may not count as a true difference for some other purposes.
- Some languages have rules where a different character or form of character must be used at the start, middle, or end of words.
Another more complex type of search is regular expression searching, where the user constructs a pattern of characters or other symbols, and any match to the pattern should fulfill the search. For example, to catch both the American English word "color" and the British equivalent "colour", instead of searching for two different literal strings, one might use a regular expression such as:
colou?r
where the "?" conventionally makes the preceding character optional.
This article mainly discusses algorithms for the simpler kinds of string searching.
A similar problem introduced in the field of bioinformatics and genomics is the maximal exact matching. Given two strings, MEMs are common substrings that cannot be extended left or right without causing a mismatch.
Basic classification of search algorithms
The various algorithms can be classified by the number of patterns each uses.Single-pattern algorithms
Let m be the length of the pattern, n be the length of the searchable text and k = |Σ| be the size of the alphabet.Algorithm | Preprocessing time | Matching time | Space |
Naive string-search algorithm | none | Θ | none |
Rabin–Karp algorithm | Θ | average Θ, worst Θ | O |
Knuth–Morris–Pratt algorithm | Θ | Θ | Θ |
Boyer–Moore string-search algorithm | Θ | best Ω, worst O | Θ |
Bitap algorithm | Θ | O | |
Two-way string-matching algorithm | Θ | O | O |
BNDM | O | O | |
BOM | O | O | |
FM-index | O | O | O |
The Boyer–Moore string-search algorithm has been the standard benchmark for the practical string-search literature.
Algorithms using a finite set of patterns
- Aho–Corasick string matching algorithm
- Commentz-Walter algorithm
- Set-BOM
- Rabin–Karp string search algorithm
Algorithms using an infinite number of patterns
Other classification
Other classification approaches are possible. One of the most common uses preprocessing as main criteria.Text not preprocessed | Text preprocessed | |
Patterns not preprocessed | Elementary algorithms | Index methods |
Patterns preprocessed | Constructed search engines | Signature methods : |
Another one classifies the algorithms by their matching strategy:
- Match the prefix first
- Match the suffix first
- Match the best factor first
- Other strategy
Naïve string search