+ All Categories
Home > Documents > String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely...

String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely...

Date post: 05-Aug-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
24
String searching problems String searching: The problem - given the following data: a SOURCE or TEXT string s = s 1 s 2 s 3 ... s n , and a PATTERN string p = p 1 p 2 p 3 ... p m . Ask: does p occur as a substring of s and, if so, where? May seek any occurrence, first occurrence, or all occurrences, etc. For example, s could be the string “abracadabra” and p the string “cad”.
Transcript
Page 1: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 2: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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?

Page 3: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 4: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 5: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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.

Page 6: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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.

Page 7: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 8: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 9: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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’’

Page 10: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 11: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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.

Page 12: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 13: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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.

Page 14: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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!

Page 15: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 16: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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?)

Page 17: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 18: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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.

Page 19: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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.

Page 20: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 21: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 22: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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.

Page 23: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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

Page 24: String searching problemsdavid/algorithms/strings.pdf · String searching Applications: Very widely applicable, wherever strings occur, e.g. for text strings, in text editors, le

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)


Recommended