SlideShare a Scribd company logo
1 of 14
Download to read offline
KMP Pattern Search
QUICK OVERVIEW
Created by,
Arjun SK
arjunsk.com
What is Patter Searching ?
o Suppose you are reading a text document.
o You want to search for a word.
o You click CTRL + F and search for that word.
o The word processor scans the document and shows the position of
occurrence.
What exactly happens is that, word i.e. pattern is searched inside the
text document.
Implementation
Naïve Approach
The naïve approach is to check whether the pattern matches the string
at every possible position in the string.
P = Pattern (word) of length m
T = Text (document) of length n
Naive string matching algorithm
takes time O((n-m+1)m)
Basic Idea of KMP
a b c d a b c a a a b c b a b
a b c d a b c d
Text
Pattern
Text
Pattern
We can find the next position for comparison, by looking at the pattern.
a b c d a b c a a a b c b a b
a b c d a b c d
KMP (Knuth-Morris-Prattern String Matching Algorithm)
Why KMP?
Best known for linear time for exact pattern matching.
How is it implemented?
o We find patterns within the search pattern.
o When a pattern comparison partially fails, we can skip to next
occurrence of prefix pattern.
o In this way, we can skip trivial comparisons.
Pre-processing
Let’s say we’re matching the pattern “abababca” against the text
“bacbababaabcbab”.
Here’s our prefix match table : i.e. prefix-table[i]
index 0 1 2 3 4 5 6 7
char a b a b a b c a
value 0 0 1 2 3 4 0 1
Matching prefix i.e. a
Matching prefix i.e. ab
Matching prefix i.e. aba
Matching prefix i.e. abab
No matching prefix
Pre-processing - cont.
• partial_match_length = length of the matched pattern in a step.
• prefix-table = pre-processed prefix table
• If prefix-table[ partial_match_length ] > 1
we may skip ahead
partial_match_length - prefix-table[ partial_match_length – 1 ] characters.
// Used to skip, already compared prefix match in the pattern.
Searching
b a c b a b a b a a b c b a b
a b a b a b c a
Text
Pattern
b a c b a b a b a a b c b a b
a b a b a b c a
Text
Pattern
This is a partial match length of 1
The value at prefix-table[partial_match_length - 1] (or prefix-table[0]) is 0.
so we don’t get to skip ahead any.
b a c b a b a b a a b c b a b
a b a b a b c a
Text
Pattern
b a c b a b a b a a b c b a b
a b a b a b c a
Text
Pattern
b a c b a b a b a a b c b a b
a b a b a b c a
Text
Pattern
In naïve approach we shift right and compare again:
Step 2
b a c b a b a b a a b c b a b
a b a b a b c a
Text
Pattern
Step 1
b a c b a b a b a a b c b a b
a b a b a b c a
Text
Pattern
But in KMP approach, we can directly skip Step 1
b a c b a b a b a a b c b a b
a b a b a b c a
Text
Pattern
X X
b a c b a b a b a a b c b a b
a b a b a b c a
Text
Pattern
This is a partial match length of 5
The value at prefix-table[partial_match_length - 1] (or prefix-table[4]) is 3.
That means we get to skip ahead
partial_match_length – prefix-table[partial_match_length - 1] (or 5 - table[4] = 5 - 3 = 2) characters:
We skip comparing “b”. The next comparison starts at next “ab” i.e. the prefix match.
In KMP we can directly skip comparing “ab”
This is a partial match length of 3
The value at prefix-table[partial_match_length - 1] (or prefix-table[2]) is 1.
That means we get to skip ahead
partial_match_length – prefix-table[partial_match_length - 1] (or 3 - table[2] = 3 - 1 = 2) characters:
b a c b a b a b a a b c b a b
a b a b a b c a
Text
Pattern
b a c b a b a b a a b c b a b
a b a b a b c a
Text
Pattern X X
We skip comparing “b”. The next comparison starts at next “a” i.e. the prefix match.
Complexity
 O(m) - It is to compute the prefix function values.
 O(n) - It is to compare the pattern to the text.
 Total of O(n + m) run time.

More Related Content

What's hot

libpinyin
libpinyinlibpinyin
libpinyinPeng Wu
 
Handout Regular expression with examples and lecture
Handout Regular expression with examples  and lecture Handout Regular expression with examples  and lecture
Handout Regular expression with examples and lecture mariajan8
 
A26 5 polydivision
A26 5 polydivisionA26 5 polydivision
A26 5 polydivisionvhiggins1
 
Postfix Notation | Compiler design
Postfix Notation | Compiler designPostfix Notation | Compiler design
Postfix Notation | Compiler designShamsul Huda
 
Wei Yang - 2014 - Consistent Improvement in Translation Quality of Chinese–Ja...
Wei Yang - 2014 - Consistent Improvement in Translation Quality of Chinese–Ja...Wei Yang - 2014 - Consistent Improvement in Translation Quality of Chinese–Ja...
Wei Yang - 2014 - Consistent Improvement in Translation Quality of Chinese–Ja...Association for Computational Linguistics
 
Answer True/False and explain for each part. (Short Answer) 1. Say that w...
Answer True/False and explain for each part. (Short Answer)     1. Say that w...Answer True/False and explain for each part. (Short Answer)     1. Say that w...
Answer True/False and explain for each part. (Short Answer) 1. Say that w...licservernoida
 
word level analysis
word level analysis word level analysis
word level analysis tjs1
 
ADAPTIVE AUTOMATA FOR GRAMMAR BASED TEXT COMPRESSION
ADAPTIVE AUTOMATA FOR GRAMMAR BASED TEXT COMPRESSIONADAPTIVE AUTOMATA FOR GRAMMAR BASED TEXT COMPRESSION
ADAPTIVE AUTOMATA FOR GRAMMAR BASED TEXT COMPRESSIONcsandit
 

What's hot (11)

String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
libpinyin
libpinyinlibpinyin
libpinyin
 
String Match | Computer Science
String Match | Computer ScienceString Match | Computer Science
String Match | Computer Science
 
Handout Regular expression with examples and lecture
Handout Regular expression with examples  and lecture Handout Regular expression with examples  and lecture
Handout Regular expression with examples and lecture
 
A26 5 polydivision
A26 5 polydivisionA26 5 polydivision
A26 5 polydivision
 
Postfix Notation | Compiler design
Postfix Notation | Compiler designPostfix Notation | Compiler design
Postfix Notation | Compiler design
 
Wei Yang - 2014 - Consistent Improvement in Translation Quality of Chinese–Ja...
Wei Yang - 2014 - Consistent Improvement in Translation Quality of Chinese–Ja...Wei Yang - 2014 - Consistent Improvement in Translation Quality of Chinese–Ja...
Wei Yang - 2014 - Consistent Improvement in Translation Quality of Chinese–Ja...
 
Answer True/False and explain for each part. (Short Answer) 1. Say that w...
Answer True/False and explain for each part. (Short Answer)     1. Say that w...Answer True/False and explain for each part. (Short Answer)     1. Say that w...
Answer True/False and explain for each part. (Short Answer) 1. Say that w...
 
word level analysis
word level analysis word level analysis
word level analysis
 
Incremental Evolving Grammar Fragments
Incremental Evolving Grammar FragmentsIncremental Evolving Grammar Fragments
Incremental Evolving Grammar Fragments
 
ADAPTIVE AUTOMATA FOR GRAMMAR BASED TEXT COMPRESSION
ADAPTIVE AUTOMATA FOR GRAMMAR BASED TEXT COMPRESSIONADAPTIVE AUTOMATA FOR GRAMMAR BASED TEXT COMPRESSION
ADAPTIVE AUTOMATA FOR GRAMMAR BASED TEXT COMPRESSION
 

Similar to KMP Pattern Search

Rabin-Karp (2).ppt
Rabin-Karp (2).pptRabin-Karp (2).ppt
Rabin-Karp (2).pptUmeshThoriya
 
String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.Malek Sumaiya
 
Pattern matching programs
Pattern matching programsPattern matching programs
Pattern matching programsakruthi k
 
An Application of Pattern matching for Motif Identification
An Application of Pattern matching for Motif IdentificationAn Application of Pattern matching for Motif Identification
An Application of Pattern matching for Motif IdentificationCSCJournals
 
module6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdfmodule6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdfShiwani Gupta
 
String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)Aditya pratap Singh
 
brown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algobrown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algoSadiaSharmin40
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaghu nath
 
Lecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdfLecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdfSaravana Kumar
 
String matching Algorithm by Foysal
String matching Algorithm by FoysalString matching Algorithm by Foysal
String matching Algorithm by FoysalFoysal Mahmud
 

Similar to KMP Pattern Search (20)

Rabin-Karp (2).ppt
Rabin-Karp (2).pptRabin-Karp (2).ppt
Rabin-Karp (2).ppt
 
String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.
 
Pattern matching programs
Pattern matching programsPattern matching programs
Pattern matching programs
 
An Application of Pattern matching for Motif Identification
An Application of Pattern matching for Motif IdentificationAn Application of Pattern matching for Motif Identification
An Application of Pattern matching for Motif Identification
 
module6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdfmodule6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdf
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)String Matching (Naive,Rabin-Karp,KMP)
String Matching (Naive,Rabin-Karp,KMP)
 
Rabin karp string matcher
Rabin karp string matcherRabin karp string matcher
Rabin karp string matcher
 
Boyer more algorithm
Boyer more algorithmBoyer more algorithm
Boyer more algorithm
 
lec17.ppt
lec17.pptlec17.ppt
lec17.ppt
 
brown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algobrown.ppt for identifying rabin karp algo
brown.ppt for identifying rabin karp algo
 
Kmp & bm copy
Kmp & bm   copyKmp & bm   copy
Kmp & bm copy
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Python - Lecture 7
Python - Lecture 7Python - Lecture 7
Python - Lecture 7
 
STRING MATCHING
STRING MATCHINGSTRING MATCHING
STRING MATCHING
 
Lecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdfLecture 18 - Regular Expressions.pdf
Lecture 18 - Regular Expressions.pdf
 
Daa unit 5
Daa unit 5Daa unit 5
Daa unit 5
 
String matching Algorithm by Foysal
String matching Algorithm by FoysalString matching Algorithm by Foysal
String matching Algorithm by Foysal
 
Thai Word Embedding with Tensorflow
Thai Word Embedding with Tensorflow Thai Word Embedding with Tensorflow
Thai Word Embedding with Tensorflow
 
Grammar Based Pre-Processing for PPM
Grammar Based Pre-Processing for PPM Grammar Based Pre-Processing for PPM
Grammar Based Pre-Processing for PPM
 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 

Recently uploaded (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 

KMP Pattern Search

  • 1. KMP Pattern Search QUICK OVERVIEW Created by, Arjun SK arjunsk.com
  • 2. What is Patter Searching ? o Suppose you are reading a text document. o You want to search for a word. o You click CTRL + F and search for that word. o The word processor scans the document and shows the position of occurrence. What exactly happens is that, word i.e. pattern is searched inside the text document.
  • 4. Naïve Approach The naïve approach is to check whether the pattern matches the string at every possible position in the string. P = Pattern (word) of length m T = Text (document) of length n Naive string matching algorithm takes time O((n-m+1)m)
  • 5. Basic Idea of KMP a b c d a b c a a a b c b a b a b c d a b c d Text Pattern Text Pattern We can find the next position for comparison, by looking at the pattern. a b c d a b c a a a b c b a b a b c d a b c d
  • 6. KMP (Knuth-Morris-Prattern String Matching Algorithm) Why KMP? Best known for linear time for exact pattern matching. How is it implemented? o We find patterns within the search pattern. o When a pattern comparison partially fails, we can skip to next occurrence of prefix pattern. o In this way, we can skip trivial comparisons.
  • 7. Pre-processing Let’s say we’re matching the pattern “abababca” against the text “bacbababaabcbab”. Here’s our prefix match table : i.e. prefix-table[i] index 0 1 2 3 4 5 6 7 char a b a b a b c a value 0 0 1 2 3 4 0 1 Matching prefix i.e. a Matching prefix i.e. ab Matching prefix i.e. aba Matching prefix i.e. abab No matching prefix
  • 8. Pre-processing - cont. • partial_match_length = length of the matched pattern in a step. • prefix-table = pre-processed prefix table • If prefix-table[ partial_match_length ] > 1 we may skip ahead partial_match_length - prefix-table[ partial_match_length – 1 ] characters. // Used to skip, already compared prefix match in the pattern.
  • 9. Searching b a c b a b a b a a b c b a b a b a b a b c a Text Pattern b a c b a b a b a a b c b a b a b a b a b c a Text Pattern This is a partial match length of 1 The value at prefix-table[partial_match_length - 1] (or prefix-table[0]) is 0. so we don’t get to skip ahead any.
  • 10. b a c b a b a b a a b c b a b a b a b a b c a Text Pattern b a c b a b a b a a b c b a b a b a b a b c a Text Pattern
  • 11. b a c b a b a b a a b c b a b a b a b a b c a Text Pattern In naïve approach we shift right and compare again: Step 2 b a c b a b a b a a b c b a b a b a b a b c a Text Pattern Step 1 b a c b a b a b a a b c b a b a b a b a b c a Text Pattern
  • 12. But in KMP approach, we can directly skip Step 1 b a c b a b a b a a b c b a b a b a b a b c a Text Pattern X X b a c b a b a b a a b c b a b a b a b a b c a Text Pattern This is a partial match length of 5 The value at prefix-table[partial_match_length - 1] (or prefix-table[4]) is 3. That means we get to skip ahead partial_match_length – prefix-table[partial_match_length - 1] (or 5 - table[4] = 5 - 3 = 2) characters: We skip comparing “b”. The next comparison starts at next “ab” i.e. the prefix match.
  • 13. In KMP we can directly skip comparing “ab” This is a partial match length of 3 The value at prefix-table[partial_match_length - 1] (or prefix-table[2]) is 1. That means we get to skip ahead partial_match_length – prefix-table[partial_match_length - 1] (or 3 - table[2] = 3 - 1 = 2) characters: b a c b a b a b a a b c b a b a b a b a b c a Text Pattern b a c b a b a b a a b c b a b a b a b a b c a Text Pattern X X We skip comparing “b”. The next comparison starts at next “a” i.e. the prefix match.
  • 14. Complexity  O(m) - It is to compute the prefix function values.  O(n) - It is to compare the pattern to the text.  Total of O(n + m) run time.