patternmatching
patternmatching
patternmatching
Deliverables
String Basics
Rabin-Karp Algorithm
1 3 5 11 10 9 8 7
r i t h m r i t h m r i t h m r i t h m
2 4 6
r i t h m r i t h m r i t h m
Ref: https://www.youtube.com/watch?v=4Xyhb72LCX4
11/15/2021 10:08 AM Copyright @ gdeepak.Com® 10
Rabin-Karp Algorithm
• It speeds up testing of equality of the pattern to the
substrings in the text by using a hash function.
• A hash function is a function which converts every string
into a numeric value, called its hash value; e.g.
hash("hello")=5. It exploits the fact that if two strings are
equal, their hash values are also equal.
• There are two problems: Different strings can also result in
the same hash value and there is extra cost of calculating
the hash for each group of strings.
• Worst case is O(mn)
3 1 4 1 5 9 2 6 5 3 5
3 1 4 1 5 9 2 6 5 3 5
3 1 4 1 5 9 2 6 5 3 5
3 1 4 1 5 9 2 6 5 3 5
3 1 4 1 5 9 2 6 5 3 5
a b a c a b
13
x 0 1 2 3 4 5
a b a c a b
P[x] a b a c a b
14 15 16 17 18 19
f(x) 0 0 1 0 1 2
a b a c a b
11/15/2021 10:08 AM Copyright @ gdeepak.Com® 27
KMP Video