ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

String matching algorithms tutorial 2: Introduction to Rabin Karp algorithm with implementation.

prodevelopertutorial August 18, 2019

In this tutorial we shall understand how Rabin Karp algorithm will work. This is the 2ndkind of algorithm we study in pattern matching algorithm set.

Rabin karp algorithm uses a hash function and brute force approach to check if a pattern is present inside a string.

 

Below is the high level working of the algorithm.

  1. Get the hash of the pattern. Suppose the length of the pattern is ‘n’.
  2. Get the hash of the whole string, by dividing the string in to “n” characters. And get the hash of those individual sub strings.
  3. Check if any hash value from substring matches our pattern hash.
    1. If it matches, do a brute force check and check if all characters ae same as pattern.
    2. If it did not match, then calculate the hash for the next “n” pattern.

 

Below is the basic working of algorithm with example:

Consider the String “S” as abdcabcde

Pattern “P” “abc”

 

Now what we do is, we calculate the hash value of the string for the size that is equal to our pattern.

 

As out pattern is “abc”, the length is 3. Hence for our string “abdcabcde” we calculate the hash as:

 

hash(abd) => u

hash(bdc) => v

hash(dca) => w

hash(abc) => x

hash(bcd) => y

hash(cde) => x

 

Here we are sending an input to hash, it is giving an hashed output.

 

Now we shall calculate hash value for our search pattern:

 

Hash(abc) = x.

 

Now that we have calculated hash for the string and for pattern, we shall if there is any hash matches our pattern hash.

Yes, there is a match. When there is a match, we shall do brute force search of the sub-string and pattern. Then one of below 2 things can happen:

 

  1. If all the characters in the string are same, then it is called as valid hit.
  2. If the character are not same, it is called as spurious hit. Because the hash value matches, but strings did not match.

 

In our example, the hash value for our pattern is ‘x’.In the hashes calculated, we have 2 substrings where the hash value is ‘x’.

 

hash(abc) = x

hash(cde) = x.

 

Here        “abc” is a valid hit.

“cde” is a spurious hit.

 

To avoid these kind of situations, it is recommended to have the hash key of larger value.

 

Now we shall see on how to actually solve by using a hash function and followed by rolling hash function.

 

Consider the string s = abdcabcd

Pattern p = abc

 

Our hash function will be as below:

 

Hash_function = first_char + second_char*prime_number^1 + third_character * prime_number^2.

As out pattern is of length 3, hash function will also take input of 3 character.

 

Let our prime number be 3. Note that the prime number can be random. Larger the prime number, less chances of getting same output from hash function.

 

So for our pattern “abc”, we shall consider

a -> 1

b -> 2

c -> 3

d -> 4

.

.

.

.

z -> 26

 

So for our pattern abc, hash will be calculated as below:

  • 1 + 2*3 + 3*3*3
  • 1 + 6 + 27
  • 34

The output will be 34. New we shall calculate hash for our string and try to match with out pattern.

 

Now for the string “abd” => 1 + 2*3 +4*9

  • 1 + 6 + 36
  • 43

Now for the string “bdc” => 2 + 4*3 +3*9

  • 2 + 12 + 27
  • 41

 

In the above 2 steps, we are actually rolling over character by character, at any point we  would have already calculated value for 2 characters.

 

i.e

abd=> bdc

 

As we would have calculated the hash value of “bd” already, there is no need to calculate again. Here we need to calculate only for the new character i.e “c”.

 

This can be calculated by 3 step process.

 

Let x = old hash value – value of (old_char)

= x/prime_number

new Hash value = x + prime^n-1 * valueof(new_character)

 

i.e for “abd” we have 43.

 

Now for the string “bdc”

  • 43 – value of (a)
  • 43 -1 = 42
  • 42/3 = 14
  • 14 + 3*9 = 41

 

The value is same as we calculated earlier individually. This method is called as rolling hash method.

 

Now for the string “dca”

  • 41 – value of (b)
  • 41 -2 = 39
  • 39/3 = 13
  • 13 + 1*9 = 22

 

Now for the string “cab”

  • 22 – value of (d)
  • 22 – 4 = 18
  • 18/3 = 6
  • 6 + 2*9 = 24

 

Now for the string “abc”

  • 24 – value of (c)
  • 24 – 3 = 21
  • 21/3 = 7
  • 7 + 3*9 = 34

 

Now for the string “bcd”

  • 34 – value of (a)
  • 34 – 1 = 33
  • 33/3 = 11
  • 11 + 4*9 = 47

 

Now we have calculated all the hash value for the string, we now shall compare with the pattern hash.

 

When we find a match, we check if all the characters are same. If same we got out search pattern.

 

If the hash value is same but strings are different, we conclude that it is a spurious hit and check if there exist another hash value that is same as our pattern hash.

Further Reading:

AJ’s definitive guide for DS and Algorithms. Click here to study the complete list of algorithm and data structure tutorial. 85+ chapters to study from.

 

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

Leave a Reply Cancel Reply

You must be logged in to post a comment.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2023 ProDeveloperTutorial.com
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT