SlideShare a Scribd company logo
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

PPTX
Boyer moore algorithm
PPTX
String Matching Algorithms-The Naive Algorithm
PPTX
Data Structure and Algorithms Merge Sort
PPT
String searching
PPTX
Rabin Carp String Matching algorithm
PPTX
HEAP SORT .pptx
PDF
String matching algorithms
PPTX
Naive string matching
Boyer moore algorithm
String Matching Algorithms-The Naive Algorithm
Data Structure and Algorithms Merge Sort
String searching
Rabin Carp String Matching algorithm
HEAP SORT .pptx
String matching algorithms
Naive string matching

What's hot (20)

PPTX
Data structures
PPTX
String matching Algorithm by Foysal
PDF
Introduction to NumPy
PPTX
Knuth morris pratt string matching algo
PPT
Minimum spanning tree
PPT
Group Ring.ppt
PPTX
prim's and kruskal's algorithm
PPTX
Array in c language
PDF
Quick sort algorithn
PPTX
My lecture infix-to-postfix
PPTX
Application of tries
PDF
pandas dataframe notes.pdf
PDF
Python Variable Types, List, Tuple, Dictionary
PDF
String matching, naive,
PPTX
Balanced Tree (AVL Tree & Red-Black Tree)
PDF
Quick sort-170316064200
PPTX
Linear and Binary search
PDF
PDF
Heap and heapsort
PDF
Arrays In Python | Python Array Operations | Edureka
Data structures
String matching Algorithm by Foysal
Introduction to NumPy
Knuth morris pratt string matching algo
Minimum spanning tree
Group Ring.ppt
prim's and kruskal's algorithm
Array in c language
Quick sort algorithn
My lecture infix-to-postfix
Application of tries
pandas dataframe notes.pdf
Python Variable Types, List, Tuple, Dictionary
String matching, naive,
Balanced Tree (AVL Tree & Red-Black Tree)
Quick sort-170316064200
Linear and Binary search
Heap and heapsort
Arrays In Python | Python Array Operations | Edureka
Ad

Similar to KMP Pattern Search (20)

PPTX
Kmp & bm copy
PPTX
Knucth Morris and pratt_presentation.pptx
PPTX
STRING MATCHING
PPT
KMP Pattern Matching algorithm
PPT
String matching algorithm
PPT
W9Presentation.ppt
PPTX
String-Matching algorithms KNuth-Morri-Pratt.pptx
PPT
lec17.ppt
PPT
PatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
PPTX
String matching algorithms(knuth morris-pratt)
PPT
String kmp
PPTX
Gp 27[string matching].pptx
PPT
PPTX
KMP String Matching Algorithm
PDF
Pattern matching programs
PPTX
IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION ALGORITHM
PPT
Knutt Morris Pratt Algorithm by Dr. Rose.ppt
PPT
String matching algorithms
PPTX
String Matching (Naive,Rabin-Karp,KMP)
PDF
module6_stringmatchingalgorithm_2022.pdf
Kmp & bm copy
Knucth Morris and pratt_presentation.pptx
STRING MATCHING
KMP Pattern Matching algorithm
String matching algorithm
W9Presentation.ppt
String-Matching algorithms KNuth-Morri-Pratt.pptx
lec17.ppt
PatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
String matching algorithms(knuth morris-pratt)
String kmp
Gp 27[string matching].pptx
KMP String Matching Algorithm
Pattern matching programs
IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION ALGORITHM
Knutt Morris Pratt Algorithm by Dr. Rose.ppt
String matching algorithms
String Matching (Naive,Rabin-Karp,KMP)
module6_stringmatchingalgorithm_2022.pdf
Ad

Recently uploaded (20)

PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Trump Administration's workforce development strategy
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Lesson notes of climatology university.
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
Computing-Curriculum for Schools in Ghana
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Weekly quiz Compilation Jan -July 25.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
What if we spent less time fighting change, and more time building what’s rig...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Trump Administration's workforce development strategy
LDMMIA Reiki Yoga Finals Review Spring Summer
Lesson notes of climatology university.
Practical Manual AGRO-233 Principles and Practices of Natural Farming
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
Computing-Curriculum for Schools in Ghana
Supply Chain Operations Speaking Notes -ICLT Program
Yogi Goddess Pres Conference Studio Updates
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Paper A Mock Exam 9_ Attempt review.pdf.
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Weekly quiz Compilation Jan -July 25.pdf

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.