SlideShare a Scribd company logo
5
Most read
7
Most read
8
Most read
String Matching
Using
The Rabin-Karp Algorithm
Dr. AMIT KUMAR @JUET
Outline
• Definition of the Rabin-Karp algorithm
• How Rabin-Karp works
• A Rabin-Karp example
• Complexity
• Real Life applications
Dr. AMIT KUMAR @JUET
String Matching Problem
• The idea of the string matching problem is that we
want to find all occurrences of the pattern P in the
given text T.
• We could use the brute force method for string
matching, which utilizes iteration over T. At each
letter, we compare the sequence against P until all
letters match of until the end of the alphabet is
reached.
• The worst case scenario can reach O(N*M)
Dr. AMIT KUMAR @JUET
Definition of Rabin-Karp
• A string search algorithm which compares a
string's hash values, rather than the strings
themselves. For efficiency, the hash value of
the next position in the text is easily
computed from the hash value of the current
position.
Dr. AMIT KUMAR @JUET
How Rabin-Karp works
• Let characters in both arrays T and P be digits in
radix-S notation. (S = (0,1,...,9)
• Let p be the value of the characters in P
• Choose a prime number q such that fits within a
computer word to speed computations.
• Compute (p mod q)
– The value of p mod q is what we will be using to find all
matches of the pattern P in T.
Dr. AMIT KUMAR @JUET
How Rabin-Karp works (continued)
• 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.
Dr. AMIT KUMAR @JUET
A Rabin-Karp example
• Given T = 31415926535 and P = 26
• We choose q = 11
• P mod q = 26 mod 11 = 4
13 14 95 62 35 5
13 14 95 62 35 5
14 mod 11 = 3 not equal to 4
31 mod 11 = 9 not equal to 4
13 14 95 62 35 5
41 mod 11 = 8 not equal to 4Dr. AMIT KUMAR @JUET
Rabin-Karp example continued
13 14 95 62 35 5
15 mod 11 = 4 equal to 4 -> spurious hit
13 14 95 62 35 5
59 mod 11 = 4 equal to 4 -> spurious hit
13 14 95 62 35 5
92 mod 11 = 4 equal to 4 -> spurious hit
13 14 95 62 35 5
26 mod 11 = 4 equal to 4 -> an exact match!!
13 14 95 62 35 5
65 mod 11 = 10 not equal to 4
Dr. AMIT KUMAR @JUET
Rabin-Karp example continued
As we can see, when a match is found, further testing is
done to insure that a match has indeed been found.
13 14 95 62 35 5
53 mod 11 = 9 not equal to 4
13 14 95 62 35 5
35 mod 11 = 2 not equal to 4
Dr. AMIT KUMAR @JUET
Rabin-Karp example (Alphabets)
• 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
Dr. AMIT KUMAR @JUET
Complexity
• The running time of the Rabin-Karp algorithm in the
worst-case scenario is O(n-m+1)m but it has a good
average-case running time.
• If a sufficiently large prime number is used for the
hash function, the hashed values of two different
patterns will usually be distinct.
• If the expected number of valid shifts is small O(1)
then the Rabin-Karp algorithm can be expected to
run in time O(n+m) plus the time to required to
process spurious hits.
Dr. AMIT KUMAR @JUET
Applications
• Bioinformatics
– Used in looking for similarities of two or more proteins;
i.e. high sequence similarity usually implies significant
structural or functional similarity.
Dr. AMIT KUMAR @JUET

More Related Content

PPTX
Rabin Karp ppt
PPTX
String Matching (Naive,Rabin-Karp,KMP)
PPTX
String matching algorithms
PPTX
String Matching Algorithms-The Naive Algorithm
PPTX
Knuth morris pratt string matching algo
PPT
String matching algorithm
PPT
Design and Analysis of Algorithms
PPTX
Computability and Complexity
Rabin Karp ppt
String Matching (Naive,Rabin-Karp,KMP)
String matching algorithms
String Matching Algorithms-The Naive Algorithm
Knuth morris pratt string matching algo
String matching algorithm
Design and Analysis of Algorithms
Computability and Complexity

What's hot (20)

PPTX
Rabin Karp Algorithm
PPTX
Rabin karp string matching algorithm
PPTX
RABIN KARP ALGORITHM STRING MATCHING
PDF
PPTX
Rabin Carp String Matching algorithm
PDF
P, NP, NP-Complete, and NP-Hard
PPT
Asymptotic notations
PPTX
Naive string matching
PPTX
Np hard
PPT
Binary Search
PDF
Algorithms Lecture 2: Analysis of Algorithms I
PPTX
String matching algorithms(knuth morris-pratt)
PDF
Binary Search - Design & Analysis of Algorithms
PDF
Operator precedence
PPTX
Input-Buffering
PPTX
heap Sort Algorithm
PPTX
Boyer moore algorithm
PPTX
String Matching Finite Automata & KMP Algorithm.
PPTX
Asymptotic Notation
PPT
Intermediate code generation (Compiler Design)
Rabin Karp Algorithm
Rabin karp string matching algorithm
RABIN KARP ALGORITHM STRING MATCHING
Rabin Carp String Matching algorithm
P, NP, NP-Complete, and NP-Hard
Asymptotic notations
Naive string matching
Np hard
Binary Search
Algorithms Lecture 2: Analysis of Algorithms I
String matching algorithms(knuth morris-pratt)
Binary Search - Design & Analysis of Algorithms
Operator precedence
Input-Buffering
heap Sort Algorithm
Boyer moore algorithm
String Matching Finite Automata & KMP Algorithm.
Asymptotic Notation
Intermediate code generation (Compiler Design)
Ad

Similar to Rabin karp string matcher (20)

PPT
Rabin-Karp (2).ppt
PPTX
Karp-Rabin algorithm-15-Jan-1997-rabin_karp_matching.pptx
PPTX
String_Matching_algorithm String_Matching_algorithm .pptx
PPTX
Rabin Karp - String Matching Algorithm
PPTX
String Matching Algorithms: Naive, KMP, Rabin-Karp
PDF
Examination of Document Similarity Using Rabin-Karp Algorithm
PDF
StringMatching-Rabikarp algorithmddd.pdf
PPTX
String matching Algorithm by Foysal
PPTX
String Matching algorithm String Matching algorithm String Matching algorithm
PPTX
IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION ALGORITHM
PDF
25 String Matching
PDF
Combination of Levenshtein Distance and Rabin-Karp to Improve the Accuracy of...
PPTX
Advance algorithms in master of technology
PDF
Pattern matching programs
PDF
K-Gram as a Determinant of Plagiarism Level in Rabin-Karp Algorithm
PPTX
10b- Rabin Karp String Matching Problem.pptx
PDF
Modified Rabin Karp
PPTX
Daa unit 5
PPT
String searching
DOC
4 report format
Rabin-Karp (2).ppt
Karp-Rabin algorithm-15-Jan-1997-rabin_karp_matching.pptx
String_Matching_algorithm String_Matching_algorithm .pptx
Rabin Karp - String Matching Algorithm
String Matching Algorithms: Naive, KMP, Rabin-Karp
Examination of Document Similarity Using Rabin-Karp Algorithm
StringMatching-Rabikarp algorithmddd.pdf
String matching Algorithm by Foysal
String Matching algorithm String Matching algorithm String Matching algorithm
IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION ALGORITHM
25 String Matching
Combination of Levenshtein Distance and Rabin-Karp to Improve the Accuracy of...
Advance algorithms in master of technology
Pattern matching programs
K-Gram as a Determinant of Plagiarism Level in Rabin-Karp Algorithm
10b- Rabin Karp String Matching Problem.pptx
Modified Rabin Karp
Daa unit 5
String searching
4 report format
Ad

More from Amit Kumar Rathi (20)

PDF
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
PDF
Fundamentals of Genetic Algorithms (Soft Computing)
PDF
Fuzzy Systems by using fuzzy set (Soft Computing)
PDF
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
PDF
Associative Memory using NN (Soft Computing)
PDF
Back Propagation Network (Soft Computing)
PDF
Fundamentals of Neural Network (Soft Computing)
PDF
Introduction to Soft Computing (intro to the building blocks of SC)
PDF
Topological sorting
PDF
String matching, naive,
PDF
Shortest path algorithms
PDF
Sccd and topological sorting
PDF
Red black trees
PDF
Recurrence and master theorem
PDF
Minimum spanning tree
PDF
Merge sort analysis
PDF
Loop invarient
PDF
Linear sort
PDF
Heap and heapsort
PDF
Greedy algorithm activity selection fractional
Hybrid Systems using Fuzzy, NN and GA (Soft Computing)
Fundamentals of Genetic Algorithms (Soft Computing)
Fuzzy Systems by using fuzzy set (Soft Computing)
Fuzzy Set Theory and Classical Set Theory (Soft Computing)
Associative Memory using NN (Soft Computing)
Back Propagation Network (Soft Computing)
Fundamentals of Neural Network (Soft Computing)
Introduction to Soft Computing (intro to the building blocks of SC)
Topological sorting
String matching, naive,
Shortest path algorithms
Sccd and topological sorting
Red black trees
Recurrence and master theorem
Minimum spanning tree
Merge sort analysis
Loop invarient
Linear sort
Heap and heapsort
Greedy algorithm activity selection fractional

Recently uploaded (20)

PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Sustainable Sites - Green Building Construction
PPTX
UNIT 4 Total Quality Management .pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
composite construction of structures.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPT
Project quality management in manufacturing
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Structs to JSON How Go Powers REST APIs.pdf
bas. eng. economics group 4 presentation 1.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
CH1 Production IntroductoryConcepts.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Sustainable Sites - Green Building Construction
UNIT 4 Total Quality Management .pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
composite construction of structures.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Project quality management in manufacturing
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx

Rabin karp string matcher

  • 1. String Matching Using The Rabin-Karp Algorithm Dr. AMIT KUMAR @JUET
  • 2. Outline • Definition of the Rabin-Karp algorithm • How Rabin-Karp works • A Rabin-Karp example • Complexity • Real Life applications Dr. AMIT KUMAR @JUET
  • 3. String Matching Problem • The idea of the string matching problem is that we want to find all occurrences of the pattern P in the given text T. • We could use the brute force method for string matching, which utilizes iteration over T. At each letter, we compare the sequence against P until all letters match of until the end of the alphabet is reached. • The worst case scenario can reach O(N*M) Dr. AMIT KUMAR @JUET
  • 4. Definition of Rabin-Karp • A string search algorithm which compares a string's hash values, rather than the strings themselves. For efficiency, the hash value of the next position in the text is easily computed from the hash value of the current position. Dr. AMIT KUMAR @JUET
  • 5. How Rabin-Karp works • Let characters in both arrays T and P be digits in radix-S notation. (S = (0,1,...,9) • Let p be the value of the characters in P • Choose a prime number q such that fits within a computer word to speed computations. • Compute (p mod q) – The value of p mod q is what we will be using to find all matches of the pattern P in T. Dr. AMIT KUMAR @JUET
  • 6. How Rabin-Karp works (continued) • 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. Dr. AMIT KUMAR @JUET
  • 7. A Rabin-Karp example • Given T = 31415926535 and P = 26 • We choose q = 11 • P mod q = 26 mod 11 = 4 13 14 95 62 35 5 13 14 95 62 35 5 14 mod 11 = 3 not equal to 4 31 mod 11 = 9 not equal to 4 13 14 95 62 35 5 41 mod 11 = 8 not equal to 4Dr. AMIT KUMAR @JUET
  • 8. Rabin-Karp example continued 13 14 95 62 35 5 15 mod 11 = 4 equal to 4 -> spurious hit 13 14 95 62 35 5 59 mod 11 = 4 equal to 4 -> spurious hit 13 14 95 62 35 5 92 mod 11 = 4 equal to 4 -> spurious hit 13 14 95 62 35 5 26 mod 11 = 4 equal to 4 -> an exact match!! 13 14 95 62 35 5 65 mod 11 = 10 not equal to 4 Dr. AMIT KUMAR @JUET
  • 9. Rabin-Karp example continued As we can see, when a match is found, further testing is done to insure that a match has indeed been found. 13 14 95 62 35 5 53 mod 11 = 9 not equal to 4 13 14 95 62 35 5 35 mod 11 = 2 not equal to 4 Dr. AMIT KUMAR @JUET
  • 10. Rabin-Karp example (Alphabets) • 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 Dr. AMIT KUMAR @JUET
  • 11. Complexity • The running time of the Rabin-Karp algorithm in the worst-case scenario is O(n-m+1)m but it has a good average-case running time. • If a sufficiently large prime number is used for the hash function, the hashed values of two different patterns will usually be distinct. • If the expected number of valid shifts is small O(1) then the Rabin-Karp algorithm can be expected to run in time O(n+m) plus the time to required to process spurious hits. Dr. AMIT KUMAR @JUET
  • 12. Applications • Bioinformatics – Used in looking for similarities of two or more proteins; i.e. high sequence similarity usually implies significant structural or functional similarity. Dr. AMIT KUMAR @JUET