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 1. Knuth Morris Pratt String matching algorithm and implementation

prodevelopertutorial August 18, 2019

 

Problem Statement: You are given a string “s” and a pattern ‘p’. You need to find if the pattern is present in the string “s”.

Usually we can solve this by brute force approach. i.e comparing one letter after another, till we find the sub pattern or we reach end of the string.

 

This can be visualized as below:

 

Introduction to KMP algorithm and implementation

 

The above approach is not efficient. Hence we choose an efficient way to solve this problem.

 

There are total of 3 pattern matching algorithms. KMP is the first algorithm in them.

 

In this tutorial we shall see how to solve using KMP algorithm.

 

KMP algorithm is bit complex/difficult to understand, when compared to next 2 algorithms. I have made sure that the explanation is simple to understand and follow.

 

 

KMP algorithm has 2 parts:

  1. Partial Match table
  2. String Matching

High level working of the algorithm:

 

By some mechanism [we shall look at it next] we create a partial match table. This table will help us to skip the number of characters when there is a mismatch. Thus eliminating, checking of all the characters one by one.

 

  1. Creating a Partial Match Table

Partial match table is the length of characters of longest proper prefix and proper suffix.

Now what is proper prefix and proper suffix?

We shall have a look at it below:

 

Proper Prefix:

It is combination of all the characters except the last character. Prefix will be taken from left to right order.

 

For example:

 

For the string “ABCD”,

Proper prefix will be:

A

AB

ABC

 

We cannot take “D” making it “ABCD”, essentially making it as original string.

 

Proper Suffix:

 

It is the combination of all the characters except the first character. Suffix will be taken from right to left order.

 

For example:

 

For the string “ABCD”

Proper suffix will be:

D

CD

BCD

 

Similarly, we cannot take “A” making it “ABCD”, essentially making it as original string.

 

Now that we have understood what is proper prefix and proper suffix, we shall build a Partial Match Table [PMT].

 

PMT will be created for the “pattern” not for the “string”.

 

 

Consider the pattern “ABABAB”.

 

As there are 6 elements, the length of our PMT will be 6.

Introduction to KMP algorithm and implementation

 

Now let’s fill a[0], for a[0] we concentrate on “a”.

 

As there is only 1 element, proper prefix = 0, proper suffix = 0. Hence a[0] = 0.

Introduction to KMP algorithm and implementation

 

Now let’s fill a[1], for a[1] we concentrate on “ab”.

Here proper prefix = a, proper suffix = b. As there are no matching characters, a[1] = 0.

Introduction to KMP algorithm and implementation

 

Now let’s fill a[2], for a[2] we concentrate on “aba”.

Here proper prefix = a, proper suffix = a. As there is 1 match, length is 1 characters, a[2] = 1

Introduction to KMP algorithm and implementation

 

Now let’s fill a[3], for a[3] we concentrate on “abab”.

Here proper prefix = a, ab, aba, proper suffix = b, ab, bab. As there is 1 match, and the length is 2 characters, a[3] = 2.

 

Introduction to KMP algorithm and implementation

 

Now let’s fill a[4], for a[4] we concentrate on “ababa”.

Here proper prefix = a, ab, aba, abab proper suffix = b, ba, aba, baba. As there is 1 match, and the length is 3 characters, a[4] = 3.

 

Introduction to KMP algorithm and implementation

 

Now let’s fill a[5], for a[5] we concentrate on “ababab”.

Here proper prefix = a, ab, aba, abab, ababa proper suffix = b, ab, bab, abab, babab. As there is 1 match, and the length is 4 characters, a[5] = 4.

 

Hence our final PMT will be as below:

Introduction to KMP algorithm and implementation

 

Let’s come to 2ndpart of the algorithm. Searching pattern in the string.

 

To search the string, we follow below steps:

 

Step 1: Take 2 variables i and j

i = string [str[0]]

j = pattern[0]

 

Step 2: Compare str[i] with pattern[j+1] <- important

  1. If match is found, increment the index of both I and j.
  2. If there is a mismatch, move j to the location as per PMT.
  3. If j = 0, then increment i index.

 

Now we shall test the above steps with help of an example:

 

String = ababcacabababacad

 

Here if you observe, we are starting the index of string from 1 and also pattern index is 1.

 

We shall see step by step working of algorithm.

Initial table will be as below:

 

Introduction to KMP algorithm and implementation

 

As per the algorithm, match str[i] with pattern [j+1].

 

i.e str[1] with pattern[0+1] as shown below:

It is a match. Hence increment “i” and “j”.

Introduction to KMP algorithm and implementation

 

Now str[2] pattern[2] it is a match, move forward.

Now str[3] pattern[3] it is a match, move forward.

Now str[4] pattern[4] it is a match, move forward.

Introduction to KMP algorithm and implementation

 

Now str[5] pattern[5] it is NOT a match.

Introduction to KMP algorithm and implementation

 

Now we shall look at PMT[5], it is 4. Hence move j to 4th location again there is a mismatch.

Introduction to KMP algorithm and implementation

 

Now again see PMT at index 4, at index 4, the value is 2. Hence move “j” to 2 index, again there is a mismatch.

 

Introduction to KMP algorithm and implementation

 

Now we check the value at index 2 in PMT. It is 0.

Hence move “j” to 0 and move “i” by 1 as belwo:

Introduction to KMP algorithm and implementation

 

Again str[6] matches with pattern[1]

Str[7] does not match with pattern[2]

 

Introduction to KMP algorithm and implementation

 

Now check the value at index 2 of PMT, it is 0.

 

Here again move “j = 0” and i to next value.

 

Introduction to KMP algorithm and implementation

 

Now check again:

 

Str[8] == pattern[1]

Str[9] == pattern[2]

Str[10] == pattern[3]

Str[11] == pattern[4]

Str[12] == pattern[5]

Str[13] == pattern[6]

 

Hence we got our substring.

 

Introduction to KMP algorithm and implementation

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