In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
, string-searching algorithms, sometimes called string-matching algorithms, are an important class of
string algorithms
In computer programming, a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation). ...
that try to find a place where one or several
strings
String or strings may refer to:
*String (structure), a long flexible structure made from threads twisted together, which is used to tie, bind, or hang other objects
Arts, entertainment, and media Films
* ''Strings'' (1991 film), a Canadian anim ...
(also called patterns) 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
An alphabet is a standardized set of basic written graphemes (called letters) that represent the phonemes of certain spoken languages. Not all writing systems represent language in this way; in a syllabary, each character represents a syll ...
(
finite set) Σ. Σ 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
Bioinformatics () is an interdisciplinary field that develops methods and software tools for understanding biological data, in particular when the data sets are large and complex. As an interdisciplinary field of science, bioinformatics combi ...
.
In practice, the method of feasible string-search algorithm may be affected by the string encoding. In particular, if a
variable-width encoding
A variable-width encoding is a type of character encoding scheme in which codes of differing lengths are used to encode a character set (a repertoire of symbols) for representation, usually in a computer. Most common variable-width encodings are ...
is in use, then it may be slower to find the ''N''th 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.
Overview
The most basic case of string searching involves one (often very long) string, sometimes called the ''haystack'', and one (often very short) 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 (or more) 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.
Many symbol systems include characters that are synonymous (at least for some purposes):
*Latin-based alphabets distinguish lower-case from upper-case, but for many purposes string search is expected to ignore the distinction.
*Many languages include
ligature
Ligature may refer to:
* Ligature (medicine), a piece of suture used to shut off a blood vessel or other anatomical structure
** Ligature (orthodontic), used in dentistry
* Ligature (music), an element of musical notation used especially in the me ...
s, 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
Non-coding DNA (ncDNA) sequences are components of an organism's DNA that do not encode protein sequences. Some non-coding DNA is transcribed into functional non-coding RNA molecules (e.g. transfer RNA, microRNA, piRNA, ribosomal RNA, and regula ...
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.
Finally, for strings that represent natural language, aspects of the language itself become involved. For example, one might wish to find all occurrences of a "word" despite it having alternate spellings, prefixes or suffixes, etc.
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 ("u") 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 (MEM). Given two strings, MEMs are common substrings that cannot be extended left or right without causing a mismatch.
Examples of search algorithms
Naive string search
A simple and inefficient way to see where one string occurs inside another is to check at each index, one by one. First, we see if there's a copy of the needle starting at 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, 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, backtracking is avoided 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
In the theory of computation and automata theory, the powerset construction or subset construction is a standard method for converting a nondeterministic finite automaton (NFA) into a deterministic finite automaton (DFA) which recognizes the same f ...
—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
In computer science, approximate string matching (often colloquially referred to as fuzzy string searching) is the technique of finding strings that match a pattern approximately (rather than exactly). The problem of approximate string matching i ...
. The
bitap algorithm The bitap algorithm (also known as the shift-or, shift-and or Baeza-Yates–Gonnet algorithm) is an approximate string matching algorithm. The algorithm tells whether a given text contains a substring which is "approximately equal" to a given patter ...
is an application of Baeza–Yates' approach.
Index methods
Faster search algorithms preprocess the text. After building a
substring index
In computer science, a substring index is a data structure which gives substring search in a text or text collection in sublinear time. If you have a document S of length n, or a set of documents D=\ of total length n, you can locate all occurrenc ...
, for example a
suffix tree
In computer science, a suffix tree (also called PAT tree or, in an earlier form, position tree) is a compressed trie containing all the suffixes of the given text as their keys and positions in the text as their values. Suffix trees allow particu ...
or
suffix array
In computer science, a suffix array is a sorted array of all suffixes of a string. It is a data structure used in, among others, full-text indices, data-compression algorithms, and the field of bibliometrics.
Suffix arrays were introduced by as ...
, the occurrences of a pattern can be found quickly. As an example, a suffix tree can be built in
time, and all
occurrences of a pattern can be found in
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 Trigram search is a method of searching for text when the exact syntax or spelling of the target object is not precisely known or when queries may be regular expressions. It finds objects which match the maximum number of three consecutive character ...
, 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.
Classification of search algorithms
Classification by a number of patterns
The various
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
s can be classified by the number of patterns each uses.
Single-pattern algorithms
In the following compilation, ''m'' is the length of the pattern, ''n'' the length of the searchable text, and ''k'' = , Σ, is the size of the alphabet.
:1.Asymptotic times are expressed using
O, Ω, and Θ notation.
:2.Used to implement the ''memmem'' and
''strstr'' search functions in the
glibc
The GNU C Library, commonly known as glibc, is the GNU Project's implementation of the C standard library. Despite its name, it now also directly supports C++ (and, indirectly, other programming languages). It was started in the 1980s by ...
and
musl
musl is a C standard library intended for operating systems based on the Linux kernel, released under the MIT License. It was developed by Rich Felker with the goal to write a clean, efficient and standards-conformant libc implementation.
Ov ...
C standard libraries.
:3.Can be extended to handle
approximate string matching
In computer science, approximate string matching (often colloquially referred to as fuzzy string searching) is the technique of finding strings that match a pattern approximately (rather than exactly). The problem of approximate string matching i ...
and (potentially-infinite) sets of patterns represented as
regular languages
In theoretical computer science and formal language theory, a regular language (also called a rational language) is a formal language that can be defined by a regular expression, in the strict sense in theoretical computer science (as opposed to ...
.
The
Boyer–Moore string-search algorithm
In computer science, the Boyer–Moore string-search algorithm is an efficient string-searching algorithm that is the standard benchmark for practical string-search literature. It was developed by Robert S. Boyer and J Strother Moore in 1977.
T ...
has been the standard benchmark for the practical string-search literature.
Algorithms using a finite set of patterns
In the following compilation, ''M'' is the length of the longest pattern, ''m'' their total length, ''n'' the length of the searchable text, ''o'' the number of occurrences.
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
In theoretical computer science and formal language theory, a regular grammar is a grammar that is ''right-regular'' or ''left-regular''.
While their exact definition varies from textbook to textbook, they all require that
* all production rules ...
or
regular expression.
Classification by the use of preprocessing programs
Other classification approaches are possible. One of the most common uses preprocessing as main criteria.
Classification by matching strategies
Another one classifies the algorithms by their matching strategy:
* 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 (Naïve, Rabin–Karp)
See also
*
Sequence alignment
In bioinformatics, a sequence alignment is a way of arranging the sequences of DNA, RNA, or protein to identify regions of similarity that may be a consequence of functional, structural, or evolutionary relationships between the sequences. Alig ...
*
Graph matching
Graph matching is the problem of finding a similarity between graphs.Endika Bengoetxea"Inexact Graph Matching Using Estimation of Distribution Algorithms" Ph.
D., 2002Chapter 2:The graph matching problem(retrieved June 28, 2017)
Graphs are comm ...
*
Pattern matching
*
Compressed pattern matching
In computer science, compressed pattern matching (abbreviated as CPM) is the process of searching for patterns in compressed data with little or no decompression. Searching in a compressed string is faster than searching an uncompressed string and ...
*
Matching wildcards
In computer science, an algorithm for matching wildcards (also known as globbing) is useful in comparing text strings that may contain wildcard syntax. Common uses of these algorithms include command-line interfaces, e.g. the Bourne shell or Micro ...
*
Full-text search
References
*R. S. Boyer and J. S. Moore,
A fast string searching algorithm'' Carom. ACM 20, (10), 262–272(1977).
*
Thomas H. Cormen
Thomas H. Cormen is the co-author of ''Introduction to Algorithms'', along with Charles Leiserson, Ron Rivest, and Cliff Stein. In 2013, he published a new book titled '' Algorithms Unlocked''. He is a professor of computer science at Dartmout ...
,
Charles E. Leiserson,
Ronald L. Rivest, and
Clifford Stein. ''
Introduction to Algorithms'', Third Edition. MIT Press and McGraw-Hill, 2009. . Chapter 32: String Matching, pp. 985–1013.
External links
Huge 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)
StringsAndChars– Implementations of many String-Matching-Algorithms (for single and multiple patterns) in Java
— Animation in Java, Detailed description and C implementation of many algorithms.
(PDF) Improved Single and Multiple Approximate String MatchingKalign2: high-performance multiple alignment of protein and nucleotide sequences allowing external featuresNyoTengu – high-performance pattern matching algorithm in C– Implementations of Vector and Scalar String-Matching-Algorithms in C
{{strings