| String Searching |
Article Index for String |
Website Links For String |
Information AboutString Searching |
| CATEGORIES ABOUT STRING SEARCHING ALGORITHM | |
| algorithms on strings | |
| search algorithms | |
|
Let Σ be an Alphabet (finite set). Formally, both the pattern and searched text are concatenations of elements of Σ. The Σ may be a usual human alphabet (for example, the letters A through Z in English). 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 unless the encoding is specifically designed to avoid it doing so may produce false matches. BASIC CLASSIFICATION The various Algorithm s 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. 1Asymptotic times are expressed using O, Ω, And Θ Notation Algorithms using finite set of patterns 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. 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 few characters 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 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 Expression s. Stubs KMP computes a Deterministic Finite Automaton that recognizes inputs with the string to search for as a suffix, so it doesn't need to back up. 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 and Gonnet uses bits in a word to keep 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 are based on preprocessing of the text. After building an Substring Index , for example a Suffix Tree or Suffix Array , the occurrences of a pattern can be found quickly. As an example can a suffix tree be built in time, and all occurrences of a pattern can be found in 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/non-match". These are sometimes called "fuzzy" searches. EXTERNAL LINKS
REFERENCES
SEE ALSO |
|
|