World Library  
Flag as Inappropriate
Email this Article

String searching algorithm

Article Id: WHEBN0000028648
Reproduction Date:

Title: String searching algorithm  
Author: World Heritage Encyclopedia
Language: English
Subject: Apostolico–Giancarlo algorithm, Hash function, Commentz-Walter algorithm, Compressed pattern matching, Lee distance
Collection:
Publisher: World Heritage Encyclopedia
Publication
Date:
 

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 (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.

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 time1
Naïve string search algorithm 0 (no preprocessing) Θ((n−m) m)
Rabin–Karp string search algorithm Θ(m) average Θ(n+m),
worst Θ((n−m) m)
Finite-state automaton based search Θ(m |Σ|) Θ(n)
Knuth–Morris–Pratt algorithm Θ(m) Θ(n)
Boyer–Moore string search algorithm Θ(m + |Σ|) Ω(n/m), O(nm)
Bitap algorithm (shift-or, shift-and, Baeza–Yates–Gonnet) Θ(m + |Σ|) O(mn)

1Asymptotic 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 a finite set of patterns

Algorithms using an 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[2]
Text not preprocessed Text preprocessed
Patterns not preprocessed Elementary algorithms Index methods
Patterns preprocessed Constructed search engines Signature methods

Naïve string search

A simple but 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 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 \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 knows what leafs 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.

See also

Academic conferences on text searching

References

  1. ^ Hume; Sunday (1991). "Fast String Searching". Software: Practice and Experience 21 (11): 1221–1248.  
  2. ^ Melichar, Borivoj, Jan Holub, and J. Polcar. Text Searching Algorithms. Volume I: Forward String Matching. Vol. 1. 2 vols., 2005. http://stringology.org/athens/TextSearchingAlgorithms/.

External links

  • Huge (maintained) list of pattern matching links Last updated:12/27/2008 20:18:38
  • StringSearch – high-performance pattern matching algorithms in Java – Implementations of many String-Matching-Algorithms in Java (BNDM, Boyer-Moore-Horspool, Boyer-Moore-Horspool-Raita, Shift-Or)
  • Exact String Matching Algorithms — Animation in Java, Detailed description and C implementation of many algorithms.
  • Boyer-Moore-Raita-Thomas
  • (PDF) Improved Single and Multiple Approximate String Matching
  • Kalign2: high-performance multiple alignment of protein and nucleotide sequences allowing external features
  • C implementation of Suffix Tree based Pattern Searching
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and USA.gov, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for USA.gov and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
 
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
 
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.
 


Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.