 String searching algorithm

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 (also called patterns) are found within a larger string or text.
Let Σ be an alphabet (finite set). Formally, both the pattern and searched text are vectors of elements of Σ. The Σ may be a usual human alphabet (for example, the letters A through Z in the Latin alphabet). Other applications may use binary alphabet (Σ = {0,1}) or DNA alphabet (Σ = {A,C,G,T}) in bioinformatics.
In practice, how the string is encoded can affect the feasible string search algorithms. In particular if a variable width encoding is in use then it is slow (time proportional to N) to find the Nth character. This will significantly slow down many of the more advanced search algorithms. A possible solution 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.
Contents
Basic classification
The various algorithms can be classified by the number of patterns each uses.
Single pattern algorithms
Let m be the length of the pattern and let n be the length of the searchable text.
Algorithm Preprocessing time Matching time^{1} Naïve string search algorithm 0 (no preprocessing) Θ((nm+1) m) Rabin–Karp string search algorithm Θ(m) average Θ(n+m),
worst Θ((nm+1) m)Finitestate automaton based search Θ(m Σ) Θ(n) Knuth–Morris–Pratt algorithm Θ(m) Θ(n) Boyer–Moore string search algorithm Θ(m + Σ) Ω(n/m), O(n) Bitap algorithm (shiftor, shiftand, Baeza–Yates–Gonnet) Θ(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.^{[1]}
Algorithms using finite set of patterns
 Aho–Corasick string matching algorithm
 Commentz–Walter algorithm
 Rabin–Karp string search algorithm
Algorithms using infinite number of patterns
Naturally, the patterns can not be enumerated 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 Text not preprocessed Text preprocessed Patterns not preprocessed Elementary algorithms Index methods Patterns preprocessed Constructed search engines Signature methods Naïve string search
The simplest and least efficient 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) steps.
Finite state automaton based search
In this approach, we avoid backtracking by constructing a deterministic finite automaton (DFA) that recognizes strings containing the desired search string. These are expensive to construct—they are usually created using the powerset construction—but 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 needlelength 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 BaezaYates' approach.
Index methods
Faster search algorithms are based on preprocessing of 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 Θ(n) time, and all z occurrences of a pattern can be found in O(m + z) time (if the alphabet size is viewed as a constant).
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/nonmatch". These are sometimes called "fuzzy" searches.
References
 ^ Hume and Sunday (1991) [Fast String Searching] SOFTWARE—PRACTICE AND EXPERIENCE, VOL. 21(11), 1221–1248 (NOVEMBER 1991 )
 R. S. Boyer and J. S. Moore, A fast string searching algorithm, Carom. ACM 20, (10), 262–272(1977).
 Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGrawHill, 2001. ISBN 0262032937. Chapter 32: String Matching, pp.906–932.
External links
 Huge (maintained) list of pattern matching links
 StringSearch – highperformance pattern matching algorithms in Java – Implementations of many StringMatchingAlgorithms in Java (BNDM, BoyerMooreHorspool, BoyerMooreHorspoolRaita, ShiftOr)
 Exact String Matching Algorithms — Animation in Java, Detailed description and C implementation of many algorithms.
 BoyerMooreRaitaThomas
Categories: Algorithms on strings
 Search algorithms
Wikimedia Foundation. 2010.
Look at other dictionaries:
Fuzzy string searching — Approximate string search is the name that is used for a category of techniques for finding strings that approximately match some given pattern string. It may also be known as approximate or inexact matching. Approximate string searching has two… … Wikipedia
RabinKarp string search algorithm — The Rabin Karp algorithm is a string searching algorithm created by Michael O. Rabin and Richard M. Karp in 1987 that uses hashing to find a substring in a text. It is used for multiple pattern matching rather than single pattern matching. For… … Wikipedia
Boyer–Moore string search algorithm — The Boyer–Moore string search algorithm is a particularly efficient string searching algorithm, and it has been the standard benchmark for the practical string search literature. [Hume and Sunday (1991) [Fast String Searching] SOFTWARE PRACTICE… … Wikipedia
Aho–Corasick string matching algorithm — The Aho–Corasick string matching algorithm is a string searching algorithm invented by Alfred V. Aho and Margaret J. Corasick. It is a kind of dictionary matching algorithm that locates elements of a finite set of strings (the dictionary ) within … Wikipedia
String — WiktionaryparstringGenerally, string is a thin, flexible piece of rope or twine which is used to tie, bind, or hang other objects. String can be made from a variety of fibres.Examples of string use include: * String figures, designs formed by… … Wikipedia
StringMatchingAlgorithmus — Dieser Artikel wurde aufgrund von inhaltlichen Mängeln auf der Qualitätssicherungsseite der Redaktion Informatik eingetragen. Dies geschieht, um die Qualität der Artikel aus dem Themengebiet Informatik auf ein akzeptables Niveau zu bringen. Hilf… … Deutsch Wikipedia
String metric — String metrics (also known as similarity metrics) are a class of textual based metrics resulting in a similarity or dissimilarity (distance) score between two pairs of text strings for approximate matching or comparison and in fuzzy string… … Wikipedia
Knuth–Morris–Pratt algorithm — The Knuth–Morris–Pratt string searching algorithm (or KMP algorithm) searches for occurrences of a word W within a main text string S by employing the observation that when a mismatch occurs, the word itself embodies sufficient information to… … Wikipedia
Bitap algorithm — The bitap algorithm (also known as the shift or, shift and or Baeza Yates Gonnet algorithm) is a fuzzy string searching algorithm developed by Udi Manber and Sun Wu in 1991refManber91refManber92 based on work done by Ricardo Baeza Yates and… … Wikipedia
Search algorithm — In computer science, a search algorithm, broadly speaking, is an algorithm that takes a problem as input and returns a solution to the problem, usually after evaluating a number of possible solutions. Most of the algorithms studied by computer… … Wikipedia