String-searching algorithm
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[1] | Space |
---|---|---|---|
Naïve string-search algorithm | none | Θ(nm) | none |
Rabin–Karp algorithm | Θ(m) | average Θ(n + m), worst Θ((n−m)m) | O(1) |
Knuth–Morris–Pratt algorithm | Θ(m) | Θ(n) | Θ(m) |
Boyer–Moore string-search algorithm | Θ(m + k) | best Ω(n/m), worst O(mn) | Θ(k) |
Bitap algorithm (shift-or, shift-and, Baeza–Yates–Gonnet; fuzzy; agrep) | Θ(m + k) | O(mn) | |
Two-way string-matching algorithm (glibc memmem/strstr)[3] | Θ(m) | O(n+m) | O(1) |
BNDM (Backward Non-Deterministic DAWG Matching) (fuzzy + regex; nrgrep)[4] | O(m) | O(n) | |
BOM (Backward Oracle Matching)[5] | O(m) | O(mn) |
1.^ Asymptotic times are expressed using O, Ω, and Θ notation.
The Boyer–Moore string-search algorithm has been the standard benchmark for the practical string-search literature.[6]
Algorithms using a finite set of patterns
- Aho–Corasick string matching algorithm (extension of Knuth-Morris-Pratt)
- Commentz-Walter algorithm (extension of Boyer-Moore)
- Set-BOM (extension of Backward Oracle Matching)
- Rabin–Karp string search algorithm
Algorithms using an infinite number of patterns
Naturally, the patterns can not be enumerated finitely in this case. They are represented usually by a regular grammar or regular expression.
Other classification
Other classification approaches are possible. One of the most common uses preprocessing as main criteria.
Classes of string searching algorithms[7]
Text not preprocessed | Text preprocessed | |
---|---|---|
Patterns not preprocessed | Elementary algorithms | Index methods |
Patterns preprocessed | Constructed search engines | Signature methods :[8] |
Another one classifies the algorithms by their matching strategy:[9]
- Match the prefix first (Knuth-Morris-Pratt, Shift-And, Aho-Corasick)
- Match the suffix first (Boyer-Moore and variants, Commentz-Walter)
- Match the best factor first (BNDM, BOM, Set-BOM)
- Other strategy (Naive, Rabin-Karp)
Naïve string search
A simple and inefficient way to see where one string occurs inside another is to check each place it could be, one by one, to see if it's there. So first we see if there's a copy of the needle in the first character of the haystack; if not, we look to see if there's a copy of the needle starting at the second character of the haystack; if not, we look starting at the third character, and so forth. In the normal case, we only have to look at one or two characters for each wrong position to see that it is a wrong position, so in the average case, this takes O(n + m) steps, where n is the length of the haystack and m is the length of the needle; but in the worst case, searching for a string like "aaaab" in a string like "aaaaaaaaab", it takes O(nm)
Finite-state-automaton-based search
In this approach, we avoid backtracking by constructing a deterministic finite automaton (DFA) that recognizes stored search string. These are expensive to construct—they are usually created using the powerset construction—but are very quick to use. For example, the DFA shown to the right recognizes the word "MOMMY". This approach is frequently generalized in practice to search for arbitrary regular expressions.
Stubs
Knuth–Morris–Pratt computes a DFA that recognizes inputs with the string to search for as a suffix, Boyer–Moore starts searching from the end of the needle, so it can usually jump ahead a whole needle-length at each step. Baeza–Yates keeps track of whether the previous j characters were a prefix of the search string, and is therefore adaptable to fuzzy string searching. The bitap algorithm is an application of Baeza–Yates' approach.
Index methods
Faster search algorithms preprocess the text. After building a substring index, for example a suffix tree or suffix array, the occurrences of a pattern can be found quickly. As an example, a suffix tree can be built in $ \Theta (n) $ time, and all $ z $ occurrences of a pattern can be found in $ O(m) $ time under the assumption that the alphabet has a constant size and all inner nodes in the suffix tree know what leaves are underneath them. The latter can be accomplished by running a DFS algorithm from the root of the suffix tree.
Other variants
Some search methods, for instance trigram search, are intended to find a "closeness" score between the search string and the text rather than a "match/non-match". These are sometimes called "fuzzy" searches.