0% found this document useful (0 votes)
56 views61 pages

String Matching - RYS - Lect - 1 - 2 - 3 - Update

The document discusses various string matching algorithms such as the naive method, finite automata approach, Rabin Karp algorithm, and KMP algorithm. It provides examples to explain the working of the naive and KMP algorithms. The KMP algorithm improves upon the naive method by using a prefix function to determine how far to shift the pattern when a mismatch occurs, avoiding re-checking characters. This provides a linear time complexity of O(n+m) compared to quadratic time for the naive method. The Rabin-Karp algorithm uses hashing to quickly determine if a character sequence matches the pattern before doing a brute force comparison.

Uploaded by

yogini choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views61 pages

String Matching - RYS - Lect - 1 - 2 - 3 - Update

The document discusses various string matching algorithms such as the naive method, finite automata approach, Rabin Karp algorithm, and KMP algorithm. It provides examples to explain the working of the naive and KMP algorithms. The KMP algorithm improves upon the naive method by using a prefix function to determine how far to shift the pattern when a mismatch occurs, avoiding re-checking characters. This provides a linear time complexity of O(n+m) compared to quadratic time for the naive method. The Rabin-Karp algorithm uses hashing to quickly determine if a character sequence matches the pattern before doing a brute force comparison.

Uploaded by

yogini choudhary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

String Matching Algorithms

Table of Contents

• String Matching
– Naïve Method
– Finite Automata Approach
– Rabin Karp
– KMP
Pattern Matching
• Given a text string T[0..n-1] and a pattern
P[0..m-1], find all occurrences of the pattern
within the text.

• Example: T = ababcabdabcaabc and P = abc,


the occurrences are:
– first occurrence starts at T[3]
– second occurrence starts at T[9]
– third occurrence starts at T[13]
Let Σ denotes the set of alphabet .

• Given:

A string of alphabets T*1..n+ of size “n”


and a pattern P*1..m+ of size “m”
where, m<<<n.
• To Find:

Whether the pattern P occurs in text T or not. If it does, then


give the first occurrence of P in T.

The alphabets of both T and P are drawn from finite set Σ.


NAÏVE APPROACH

T: a b c a b d a a b c d e

P: a b d
Example ( Step – 1 )

T: a b c a b d a a b c d e

P: a b d

Mismatch after 3 Comparisons


Example ( Step – 2 )

T: a b c a b d a a b c d e

a b d
P:

Mismatch after 1 Comparison


Example ( Step – 3 )

T: a b c a b d a a b c d e

a b d
P:

Mismatch after 1 Comparison


Example ( Step – 4 )

T: a b c a b d a a b c d e

a b d
P:

Match found after 3 Comparisons

Thus, after 8 comparisons the


substring P is found in T.
Worst Case Running Time

T : a a a a a……..a a f of size say “n”

P : a a a f of size 4
Example ( Step – 1 )

T: a a a a . . . . . a a f

P: a a a f

Mismatch found after 4 comparisons


Example ( Step – 2 )

T: a a a a a , , , , a a f

P: a a a f

Mismatch found after 4 comparisons


Example

T: a a a a a . . . . a a a f

a a a f
P:

Match found after 4 comparisons


Worst Case Running Time

This will continue to happen until (n-4)th


alphabet in T is compared with the characters
in P and thus the no. of comparisons required
is (n-4)4 + 4.
Worst Case Running Time

• At every step, after ‘m’ comparisons a


mismatch will be found.

• These ‘m’ comparisons will be done for (n-m)


characters in T.

• Thus,the running time obtained is (n-m)m+m.


Finite Automata

#a

#a ∑
a
a a f
s1 s2 s3 f
s0

#a
Worst Case Running Time

• In finite automata, each character is scanned atmost


once. Thus in the worst case, the searching time is
O(n).

• Preprocessing time:- As for every character in ∑ an


edge has to be formed, thus the preprocessing time
is O(m*|∑|).

• Thus total running time is O(n) + O(m*|∑|).


Drawback:-

If the alphabet set ∑ is very large, then the


time required to construct the FA will be very
large.
BRUTE FORCE STRATEGY
• In this strategy whenever a mismatch was
found , the pattern was shifted right by 1
character.

• But this wasn’t an efficient strategy as it


required a large number of comparisons.
Hence a better algorithm was required.

19
KMP : Knuth Morris Pratt Algorithm
T : …… tj .. …...tj+r-1 ….tj+k-r…...tj+k-2 tj+k-1 …
………………………………
P: p1 …… pr …… ……… pk-1 pk ……
p1 …… pr pk …
If tj+k ≠ pk
Shifting of the pattern is required. But instead of shifting right by 1
character, we look for longest prefix of p1 … pk-1 that matches the
suffix of tj … tj+k-1.

Since tj … tj+k-1 has already been matched with p1 … pk-1 , this


means we need to look for longest prefix of p1 … pk-1 that matches
with its own suffix.
tj t(j+1) t(j+2) t(j+3) t(j+4) … t(j+k-1) t(j+k)
p1 p2 p3 p4 p5 …. pk p(k+1) XXXX
p1 p2 ….. P(k-3) p(k-2)

20
KMP Contd..
• Let r be the length of the longest prefix of P that
matches with the matched part of P. Then the
pattern can be shifted by r positions instead of 1 and
tj+k-1 should be compared with pr+1.
• Claim 1: We have not missed any match i.e. the
pattern does not exist at any position from j to j+k-r-
1.
• Proof: Had it been, we would have a longer prefix
matching with its suffix.
Why LONGEST?

T:abcabcabcabcaf
mismatch found
P:abcabcabcaf

22
T:abcabcabcabcaf
mismatch found
P:abcabcabcaf

the longest prefix.


Correct alignment for the pattern will be by
shifting it 3 characters right.

23
T:abcabcabcabcaf

P: abcabcabcaf

Pattern found.

24
T:abcabcabcabcaf
mismatch
P: abcabcabcaf

Pattern not found.


By finding a smaller prefix and aligning the
pattern accordingly as shown, the pattern’s
occurrence in the text got missed (that is we
shifted by more positions than we should
have) 25
So it is known that we need to find the longest
prefix in the pattern that matches its suffix.
But HOW?

26
P : p1 ….………….…………… pk …………

Let the length of the longest prefix of p1 … pk-1 that


matches its suffix be ‘r.’

27
T : …… tj .. …...tj+r-1 ….tj+k-r…...tj+k-2 tj+k-1 …
………………………………
P: p1 …… pr …… ……… pk-1 pk ……
p1 …… pr pk …
If tj+k-1 ≠ pk
Let Fail[k] be a pointer which says that if a mismatch
occurs for pk then what is the character in P that
should come in place of pk by shifting P accordingly .

How to compute Fail[k]? Or pi[k]


28
Analysis of KMP
# of mismatch: For mismatch the pattern is shifted
by at least 1 position. The maximum number of
shifts is determined by the largest suffix.
T: ......a b c a b c a b c a b c d a f d........
mismatch
P: deb
mismatch
P: deb For every mismatch pattern is
..
.. shifted by atleast1postion.
 Total no. of shifts <= n-m
 Total no. of mismatches <=n-m+1
• We formalize the information that we precompute as follows.
Given a pattern P[1..m] , the prefix function for the pattern P
is the function π :,1,2,…m-→,0,1,…,m-1}such that
• π[q]=max{k:k<q and Pk Ͻ Pp}
• That is, π[q] is the length of the longest prefix of P that is a
proper suffix of Pq.
• Example: prefix function π for the pattern ababaca
• Prefix Algo KMP matcher
Example
• P=ababaca
Step 4: i = 4,j = 2,F[4] =3
Now, to understand the process let us go through an example. Assume
that T = b a c b a b a b a b a c a c a & P = a b a b a c a. Since we have
already filled the prefix table, let us use it and go to the matching
algorithm. Initially: n = size of T = 15; m = size of P = 7.
• Pattern P has been found to completely occur in string T. The total number
of shifts that took place for the match to be found are: i – m= 13 – 7 = 6
shifts.
• KMP performs the comparisons from left to right
• KMP algorithm needs a preprocessing (prefix function) which takes O(m)
space and time complexity
• Searching takes O(n + m) time complexity (does not depend on alphabet
size)
Analysis of KMP contd.
# of matches: For every match, pointer in the
text moves up by 1 position.
T: ......a b c a b c a b c a b c d a f d........
For every match pointer moves
P: abc bde
up by 1 position.
P: abcbde
P: a b c b. d e => # of matches <= length of text
.. <= n
..
The complexity of KMP is linear in nature.
O(m+n)
Rabin-Karp
• The Rabin-Karp string searching algorithm calculates a hash value
for the pattern, and for each M-character subsequence of text to
be compared.
• If the hash values are unequal, the algorithm will calculate the
hash value for next M-character sequence.
• If the hash values are equal, the algorithm will do a Brute Force
comparison between the pattern and the M-character sequence.
• In this way, there is only one comparison per text subsequence,
and Brute Force is only needed when hash values match.
• Perhaps an example will clarify some things...

51
Rabin-Karp Example
• Hash value of “AAAAA” is 37
• Hash value of “AAAAH” is 100

52
Rabin-Karp Algorithm

pattern is M characters long


hash_p=hash value of pattern
hash_t=hash value of first M letters in body of text
do
if (hash_p == hash_t)
brute force comparison of pattern
and selected section of text
hash_t= hash value of next section of text, one character over
while (end of text or
brute force comparison == true)
53
Rabin-Karp

• Common Rabin-Karp questions:


“What is the hash function used to calculate values for
character sequences?”
“Isn’t it time consuming to hash very one of the M-character
sequences in the text body?”
“Is this going to be on the final?”

• To answer some of these questions, we’ll have to get


mathematical.

54
Example
• To find the pattern 26535 in the text 3 1 4 1 5
9 2 6 5 3 5 8 9 7 9 3 , we choose a table size Q
(997 in the example), compute the hash value
26535 % 997 = 613, and then look for a match
by computing hash val ues for each five-digit
substring in the text
Example-Method 1

• computing the hash function. With five-digit


values, we could just do all the necessary
calculations with int values, but what do we
do when M is 100 or 1,000?
Rabin-Karp Math
• Consider an M-character sequence as an M-digit number in base b,
where b is the number of letters in the alphabet. The text
subsequence t[i .. i+M-1] is mapped to the number

• Furthermore, given x(i) we can compute x(i+1) for the next subsequence t[i+1 .. i+M] in
constant time, as follows:

• In this way, we never explicitly compute a new value. We


simply adjust the existing value as we move over one
character. 57
• Key idea. The Rabin-Karp method is based on efficiently
computing the hash func tion for position i+1 in the text,
given its value for position i. It follows directly from a simple
mathematical formulation. Using the notation ti for
txt.charAt(i), the num ber corresponding to the M-character
substring of txt that starts at position i is

• and we can assume that we know the value of h(xi ) = xi mod


Q . Shifting one position right in the text corresponds to
replacing xi by
• We subtract off the leading digit, multiply by R, then add the
trailing digit. Now, the crucial point is that we do not have to
maintain the values of the numbers, just the values of their
remainders when divided by Q
Example:Method 2
Rabin-Karp Math Example

• Let’s say that our alphabet consists of 10 letters.


• our alphabet = a, b, c, d, e, f, g, h, i, j
• Let’s say that “a” corresponds to 1, “b” corresponds to 2 and so
on.
The hash value for string “cah” would be ...

3*100 + 1*10 + 8*1 = 318

61

Rabin-Karp Mods
If M is large, then the resulting value (~bM) will be enormous. For this
reason, we hash the value by taking it mod a prime number q.
• The mod function (% in Java) is particularly useful in this case due to several
of its inherent properties:
[(x mod q) + (y mod q)] mod q = (x+y) mod q
(x mod q) mod q = x mod q
• For these reasons:
h(i)=((t[i] bM-1 mod q) +(t[i+1] bM-2 mod q) + ...
+(t[i+M-1] mod q))mod q
h(i+1) =( h(i)  b mod q
Shift left one digit
-t[i]  bM mod q
Subtract leftmost digit
+t[i+M] mod q )
Add new rightmost digit
62
mod q
Rabin-Karp Complexity
• If a sufficiently large prime number is used for the hash function,
the hashed values of two different patterns will usually be distinct.
• If this is the case, searching takes O(N) time, where N is the
number of characters in the larger body of text.
• It is always possible to construct a scenario with a worst case
complexity of O(MN). This, however, is likely to happen only if the
prime number used for hashing is small.

63
Rabin Karp Algorithm
Example: P: 31415, T: 2359023141526739921
Finite Automata
Example
pattern P : ababaca, text T : abababacaba

Shift 9-7=2
Algorithm
Reference
• Algorithms by Kevin and et.al
• Introduction to Algorithms by Cormen and
et.al

You might also like