String Naive and KMP
String Naive and KMP
aaaaaaaaab
aaab
Generating lps:
Pat= a b c d a b c
Prf = a, ab, abc, abcd
Suf=c, bc, abc, dabac
lps=abc
p1=a b c d a b e a b f
0 0 0 0 1 2 0 1 2 0
p2=a b c d e a b f a b c
0 0 0 0 0 1 2 0 1 2 3
p3=a a a a b a a c d
0 1 2 3 0 1 2 0 0
Str: a b a b c a b a b a b d
Pat: a b a b d
KMP (Knuth Morris Pratt) Pattern Searching: The Naive pattern-
searching algorithm doesn’t work well in cases where we see many
matching characters followed by a mismatching character.
1) txt[] = “AAAAAAAAAAAAAAAAAB”, pat[] = “AAAAB”
2) txt[] = “ABABABCABABABCABABABC”, pat[] = “ABABAC”
(A worst case for Naive).
The KMP matching algorithm uses degenerating property (pattern
having the same sub-patterns appearing more than once in the pattern) of
the pattern and improves the worst-case complexity to O(n+m). The
basic idea behind KMP’s algorithm is: whenever we detect a mismatch
(after some matches), we already know some of the characters in the text
of the next window. We take advantage of this information to avoid
matching the characters that we know will anyway match.