string searching problemsdavid/algorithms/strings.pdf · string searching applications: very widely...
TRANSCRIPT
String searching problems
String searching: The problem - given the following data:
a SOURCE or TEXT string s = s1s2s3 . . . sn, and
a PATTERN string p = p1p2p3 . . . pm.
Ask: does p occur as a substring of s and, if so, where? May seekany occurrence, first occurrence, or all occurrences, etc.
For example, s could be the string “abracadabra” and p the string“cad”.
String searching
Applications: Very widely applicable, wherever strings occur,e.g. for text strings, in text editors, file operations eg grep, andthe major application of web search engines; non-text stringsinclude matching genetic sequences.
Question: The problem is a special case of more general ‘patternmatching’ problems - to what extent do string search algorithmsgeneralise, or pattern matching algorithms specialise?
String searching: Basic algorithm
A naıve algorithm:
Look for a match starting at beginning of source text s.
Compare characters of pattern p with those of s until either
1 find two characters that differ – this means that no match ispresent with the current starting point
2 reach end of p – this means we’ve found a match
3 reach end of s – this means that no match is present.
If (1) then slide the pattern along one character and start searchagain at the beginning of the pattern.
Repeat until reach end of p (success) or end of s (failure).
Naıve algorithm - complexity
Naıve string searching algorithm
If the pattern has length m and source text has length n, in worstcase need
Length.of .pattern×Number .of .possible.start.points = m×(n−m+1)
character comparisons (i.e. equality tests).
For example:
Pattern aaaabText aaaaaaaaaaaaab
takes 5× (14− 5 + 1) = 50 comparisons
Often n is much larger than m, in which case the algorithm isO(m × n).
Efficient exact string matching
The naıve algorithm will always find a match if one exists but oftendoes much more work than is necessary.
Positions: 0123456Pattern: abcabcdText: abcabcabcd....
Match fails at position 6.
The naıve algorithm shifts the pattern along by one and startschecking again at start of pattern. It throws away knowledge ofthe source text gained by checking and knowing that we havematched so far. We know that the text matches the first 6characters of the pattern, so, from the pattern alone, we knowwhat the text is and that moving by 1 cannot match.
Knuth-Morris-Pratt: Reference
The Knuth-Morris-Pratt algorithm (KMP)
This is the basis for the Knuth-Morris-Pratt algorithm:Donald E. Knuth, James H. Morris and Vaughan R. Pratt, FastPattern Matching in Strings, SIAM Journal on Computing, 6(2):323–350.
Knuth-Morris-Pratt: Development I
In fact, shifting the pattern by 2 cannot match, but by 3, it maypossibly match:
0123456Pattern: abcabcdText: abcabcabcd.....
0123456789
This works because, at the point of the match failure, the string‘abc’
is both a prefix of the pattern (abcabcd)
and a proper suffix of the pattern up to the mismatch (abcabc d).(by ‘proper’ we mean it is not the whole substring).
Knuth-Morris-Pratt: Development II
In general, for each k with 0 < k < pattern length, define fail(k)to be
The length of the longest prefix of the pattern that is asuffix of pattern[1..k].
If we define fail(0) to be 0, we have (for the above example):
k = 0 1 2 3 4 5 6pk = a b c a b c dfail(k) = 0 0 0 1 2 3 0
Knuth-Morris-Pratt: Search program
This pseudocode program finds the first occurrence of thepattern in the text:
KMP(pattern, text)i = 0; j = 0;while i < n do{ if pattern[j]=text[i]
then{ {if j=m-1
then return ‘‘found’’}; // successi = i+1; j = j+1; // continue along pattern
else if j>0 // fail casethen j = fail(j-1) // shift patternelse i = i+1;
return ‘‘not-found’’
Knuth-Morris-Pratt: Complexity
Using this algorithm, we search a text of length n for a pattern oflength m, having precomputed the fail array.
The precomputation of the fail array can be done by abootstrapping technique (see standard texts) in O(m) operations.
We can show that the searching requires only 2n tests of characterequality.
The overall algorithm thus has complexity O(m + n).
Comment
Note:
In this example, a failure at say character index 5 – the second ‘c’– means that although the first ‘ab’ aligns with the second ‘ab’, weknow that this alignment too must fail as the next text character isnot a ‘c’. This improvement is not usually incorporated into thealgorithm.
Knuth-Morris-Pratt: Conclusions
KMP is fast exactly where the naıve algorithm is slow – thatis, when the pattern and text contain repeated patterns ofcharacters.
KMP is particularly good when the alphabet is small, forexample bit patterns.
There is another algorithm which outperforms them both . . .
Another efficient exact string search
The Boyer-Moore Algorithm
This algorithm (R. M. Boyer and J. S. Moore, 1977) uses a changeof approach together with two techniques to improve the amountby which the pattern is shifted whenever a match fails.
Change of approach:
Try to match the pattern from right to left, rather than left to rightStill move the pattern across the source text from left to right.
Boyer-Moore: Example
For example, if the pattern is ‘wish’ and the source text is ‘dish offruit’.
dish of fruit|
wish
We would successfully match ’h’,’s’ and ’i’ before failing on ’d’ and‘w’.
At first sight doesn’t look like a great idea!
Boyer-Moore: Development I
Idea 1.
If a match fails, move the pattern along so that, if possible, amatch is made with the source text character we are looking at.
This is made clearer with an example. Consider the source text:‘here is a piece of text which we wish to search’, and the pattern:‘wish’:
here is a piece of text which we wish to search|
wish
The ‘h’ fails to match against ‘e’, and no ‘e’ appears in thepattern, so no match can contain the ‘e’.
So can move pattern along past the ‘e’, i.e. 4 places (the width ofthe pattern).
We are now at the position:
here is a piece of text which we wish to search|
wish
Again match fails, this time against the space character. Since nospace in the pattern can shift along 4 again
here is a piece of text which we wish to search|
wish
Match fails again, but this time against an ‘i’, so move patternalong so that ‘i’ in the source text matches rightmost ‘i’ in thepattern. (Why rightmost?)
We are now at position:
here is a piece of text which we wish to search|
wish
There is no ‘c’ in pattern, so shift 4. A couple more moves like thisgive us:
here is a piece of text which we wish to search|
wish
The ‘h’ matches, so try the next character down the pattern (‘s’).This fails, so move along to match the ‘w’ in the text against therightmost ‘w’ of the pattern
here is a piece of text which we wish to search|
wish
etc etc...
The complete search is shown below:
here is a piece of text which we wish to search| | | | | | || | | ||
wish | | | | | || | | ||wish | | | | || | | ||
wish | | | || | | ||wish | | || | | ||
wish | || | | ||wish || | | ||
wish | | ||wish | ||
wish ||wish|wish
The match start occurs at the 34th character, but we have onlyhad to make 15 comparisons. The previous methods would havemade at least 37 attempts at matching.
The B-M approach means that many text characters are notlooked at all. In general, the longer the pattern, the fewer thecomparisons.
Given the text character on which match fails, we need to knowhow far can shift pattern
If the character isn’t in the pattern, then can shift the widthof the pattern
If the character c is in the pattern, then can shift so thatrightmost occurrence of c matches the occurrence of c in thetext.
Set up an array containing these values, for each character in thecharacter set being used.
Boyer-Moore: Development II
Idea 2: The second technique used by B-M is essentially anadaptation of the fail array used in KMP, adapted to the right toleft pattern search.
Suppose we have a pattern batsandcats:
.......dats.......|
batsandcats
Mismatch occurs on text character ‘d’. The first technique wouldslide the pattern along until next ‘d’ in pattern matched text, i.e. 1character:
.......dats.......|
batsandcats
But we know that characters to right of current position are ‘ats’.
If the string ‘ats’ does not occur again in the pattern, can slide thepattern past it, otherwise we slide pattern along so that ‘ats’ intext matches next occurrence of ‘ats’ in the pattern
.......dats.......|
batsandcats
Boyer-Moore: Definition
In general, define MatchJump[k] to be the amount to incrementthe text position to begin the next pattern scan after a mismatchat character k of the pattern.
If m is the length of the pattern p, then, for k < m, let r belargest index so that:
pr . . . pr+m−k−1 matches pk+1 . . . pm
and pr−1 6= pk .
Define MatchJump[k] = m − r + 1.
If we can’t match the whole suffix pk+1 . . . pm, look for a q suchthat suffix of length q matches, then take
MatchJump[k] = m − k + m − q.
The Boyer-Moore string searching algorithm
The Boyer-Moore algorithm uses both these approaches and movesthe pattern along as much as it can: computing both shifts andtaking the maximum.
This combination produces a dramatic improvement over the naıvealgorithm and KMP, particularly for long patterns and text with alarge alphabet.
Applications: The Boyer-Moore algorithm is the algorithm ofchoice for searching in some text editors (e.g. in the emacs editor).
Other methods of exact and inexact string matching
There are many other approaches to string matching (see exercisesfor some of these). These include:
automata-based methods - building and running automata,
algorithms using bit-string operations,
methods using probabilities e.g. in English some letters occurless often than others (e.g ‘x’ and ‘z’), match these first?
techniques based on hashing . Exact methods using hashinginclude the Karp-Rabin algorithm. Inexact methods includeHarrison-hashing (‘Implementation of substring test’,M.C. Harrison. C.A.C.M. 14:12. 1971.) in which we maycompare patterns with webpages in one machine instruction!(c.f. Web Search Engines)