SlideShare a Scribd company logo
2
Most read
7
Most read
15
Most read
Unit IV- KMP String Matching
Algorithm
• The Knuth–Morris–Pratt string-searching algorithm (or KMP algorithm) searches for occurrences of
a "word" W within a main "text string" S when a mismatch occurs, the pattern P has sufficient
information to determine where the next potential match could begin thereby avoiding several
unnecessary matching bringing the time complexity to linear.
• Knuth-Morris and Pratt introduce a linear time algorithm for the string matching problem.
• It which checks the characters from left to right. When a pattern has a sub-pattern appears more than
one in the sub-pattern.
Knuth–Morris–Pratt
Some of the applications are Text editors in computing machines, Database queries,
Bioinformatics and Cheminformatics, two dimensional mesh, network intrusion
detections system, wide window pattern matching (large string matching), music
content retrievals, language syntax checker, ms word spell checker, matching DNA
sequences, digital libraries, search engines.
Applications
Components of KMPAlgorithm:
1. The Prefix Function (Π): The prefix function for this string is defined as an array π
of length n, where π[i] is the length of the longest proper prefix of the substring s[0…i]
which is also a suffix of this substring. A proper prefix of a string is a prefix that is not
equal to the string itself. By definition, π[0]=0.
2. The KMP Matcher: With string 'S,' pattern 'p' and prefix function 'Π' as inputs, find
the occurrence of 'p' in 'S' and returns the number of shifts of 'p' after which
occurrences are found.
The Prefix Function (Π)
Following pseudo code compute the prefix function, Π:
COMPUTE- PREFIX- FUNCTION (P)
1. m ←length [P] //'p' pattern to be matched
2. Π [1] ← 0
3. k ← 0
4. for q ← 2 to m
5. do while k > 0 and P [k + 1] ≠ P [q]
6. do k ← Π [k]
7. If P [k + 1] = P [q]
8. then k← k + 1
9. Π [q] ← k
10. Return Π
Example: Compute Π for the pattern 'p' below:
Initially: m = length [p] = 7
Π [1] = 0
k = 0
COMPUTE- PREFIX- FUNCTION
(P)
1. m ←length [P]
//'p' pattern to be
matched
2. Π [1] ← 0
3. k ← 0
4. for q ← 2 to m
5. do while k > 0 and P
[k + 1] ≠ P [q]
6. do k ← Π [k-1]
7. If P [k + 1] = P [q]
8. then k← k + 1
9. Π [q] ← k
10. Return Π
KMP String Matching Algorithm
KMP String Matching Algorithm
Running Time Analysis:
For calculating the prefix function, the for loop from step 4 to step 10
runs 'm' times. Step1 to Step3 take constant time. Hence the running time
of computing prefix function is O (m).
The KMP Matcher:
The KMP Matcher with the pattern 'p,' the string ‘T' and prefix function 'Π' as input, finds a
match of p in T.
Following pseudo code compute the matching component of KMP algorithm:
KMP-MATCHER (T, P)
1. n ← length [S]
2. m ← length [P]
3. Π← COMPUTE-PREFIX-FUNCTION (P)
4. q ← 0 // numbers of characters matched
5. for i ← 1 to n // scan S from left to right
6. do while q > 0 and P [q + 1] ≠ T [i]
7. do q ← Π [q] // next character does not match
8. If P [q + 1] = T [i]
9. then q ← q + 1 // next character matches
10. If q = m // is all of p matched?
11. then print "Pattern occurs with shift" i - m
12. q ← Π [q] // look for the next
match
Running Time Analysis:
The for loop beginning in step 5 runs 'n' times, i.e., as long as the length of the
string 'S.' Since step 1 to step 4 take constant times, the running time is
dominated by this for the loop. Thus running time of the matching function is O
(n).
KMP String Matching Algorithm
KMP-MATCHER (T, P)
1. n ← length [S]
2. m ← length [P]
3. Π← COMPUTE-PREFIX-FUNCTION (P)
4. q ← 0
5. for i ← 1 to n
6. do while q > 0 and P [q + 1] ≠
T [i]
7. do q ← Π [q]
8. If P [q + 1] = T [i]
9. then q ← q + 1
10. If q = m
11. then print "Pattern occurs
with shift" i - m
12. q ← Π [q]
KMP-MATCHER (T, P)
1. n ← length [S]
2. m ← length [P]
3. Π← COMPUTE-PREFIX-FUNCTION (P)
4. q ← 0
5. for i ← 1 to n
6. do while q > 0 and P [q + 1] ≠
T [i]
7. do q ← Π [q]
8. If P [q + 1] = T [i]
9. then q ← q + 1
10. If q = m
11. then print "Pattern occurs
with shift" i - m
12. q ← Π [q]
KMP-MATCHER (T, P)
1. n ← length [S]
2. m ← length [P]
3. Π← COMPUTE-PREFIX-FUNCTION (P)
4. q ← 0
5. for i ← 1 to n
6. do while q > 0 and P [q + 1] ≠
T [i]
7. do q ← Π [q]
8. If P [q + 1] = T [i]
9. then q ← q + 1
10. If q = m
11. then print "Pattern occurs
with shift" i - m
12. q ← Π [q]
KMP-MATCHER (T, P)
1. n ← length [S]
2. m ← length [P]
3. Π← COMPUTE-PREFIX-FUNCTION (P)
4. q ← 0
5. for i ← 1 to n
6. do while q > 0 and P [q + 1] ≠
T [i]
7. do q ← Π [q]
8. If P [q + 1] = T [i]
9. then q ← q + 1
10. If q = m
11. then print "Pattern occurs
with shift" i - m
12. q ← Π [q]
KMP-MATCHER (T, P)
1. n ← length [S]
2. m ← length [P]
3. Π← COMPUTE-PREFIX-FUNCTION (P)
4. q ← 0
5. for i ← 1 to n
6. do while q > 0 and P [q + 1] ≠
T [i]
7. do q ← Π [q]
8. If P [q + 1] = T [i]
9. then q ← q + 1
10. If q = m
11. then print "Pattern occurs
with shift" i - m
12. q ← Π [q]

More Related Content

PPT
KMP Pattern Matching algorithm
PDF
PPTX
String matching algorithms(knuth morris-pratt)
PPTX
Longest Common Subsequence
PPTX
Lecture 17 Iterative Deepening a star algorithm
PPTX
Bfs and Dfs
PPTX
LINEAR BOUNDED AUTOMATA (LBA).pptx
PPT
String matching algorithm
KMP Pattern Matching algorithm
String matching algorithms(knuth morris-pratt)
Longest Common Subsequence
Lecture 17 Iterative Deepening a star algorithm
Bfs and Dfs
LINEAR BOUNDED AUTOMATA (LBA).pptx
String matching algorithm

What's hot (20)

PPT
PPTX
DFS and BFS
PPT
Pattern matching
PPTX
Simplification of cfg ppt
PPT
Depth First Search ( DFS )
PPTX
Breadth First Search & Depth First Search
PPTX
Sum of subset problem.pptx
PPTX
Asymptotic Notation
PPTX
Binary Heap Tree, Data Structure
PPT
Heap sort
PPTX
Rabin Karp Algorithm
PDF
Expression trees
PPTX
8 queens problem using back tracking
PPT
Heapsort ppt
PPT
B trees in Data Structure
PDF
Rabin karp string matcher
PPTX
Graph traversals in Data Structures
PPTX
Knuth morris pratt string matching algo
PPTX
Lecture 21 problem reduction search ao star search
DFS and BFS
Pattern matching
Simplification of cfg ppt
Depth First Search ( DFS )
Breadth First Search & Depth First Search
Sum of subset problem.pptx
Asymptotic Notation
Binary Heap Tree, Data Structure
Heap sort
Rabin Karp Algorithm
Expression trees
8 queens problem using back tracking
Heapsort ppt
B trees in Data Structure
Rabin karp string matcher
Graph traversals in Data Structures
Knuth morris pratt string matching algo
Lecture 21 problem reduction search ao star search
Ad

Similar to KMP String Matching Algorithm (20)

PPT
W9Presentation.ppt
PPTX
String-Matching algorithms KNuth-Morri-Pratt.pptx
PPT
lec17.ppt
PPT
String-Matching Algorithms Advance algorithm
PPT
String searching
PPT
Knutt Morris Pratt Algorithm by Dr. Rose.ppt
PPT
PPTX
String Matching (Naive,Rabin-Karp,KMP)
PDF
StringMatching-Rabikarp algorithmddd.pdf
PPTX
Gp 27[string matching].pptx
PPT
String matching algorithms
PDF
module6_stringmatchingalgorithm_2022.pdf
PPTX
String matching Algorithm by Foysal
PDF
Pattern matching programs
PPT
String kmp
PPTX
Kmp & bm copy
PDF
String matching algorithms
PPTX
STRING MATCHING
PPT
PatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
PDF
Modified Rabin Karp
W9Presentation.ppt
String-Matching algorithms KNuth-Morri-Pratt.pptx
lec17.ppt
String-Matching Algorithms Advance algorithm
String searching
Knutt Morris Pratt Algorithm by Dr. Rose.ppt
String Matching (Naive,Rabin-Karp,KMP)
StringMatching-Rabikarp algorithmddd.pdf
Gp 27[string matching].pptx
String matching algorithms
module6_stringmatchingalgorithm_2022.pdf
String matching Algorithm by Foysal
Pattern matching programs
String kmp
Kmp & bm copy
String matching algorithms
STRING MATCHING
PatternMatching2.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
Modified Rabin Karp
Ad

Recently uploaded (20)

PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Trump Administration's workforce development strategy
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Cell Types and Its function , kingdom of life
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
RMMM.pdf make it easy to upload and study
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Final Presentation General Medicine 03-08-2024.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Trump Administration's workforce development strategy
Final Presentation General Medicine 03-08-2024.pptx
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Cell Types and Its function , kingdom of life
01-Introduction-to-Information-Management.pdf
Pharma ospi slides which help in ospi learning
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O7-L3 Supply Chain Operations - ICLT Program
Anesthesia in Laparoscopic Surgery in India
RMMM.pdf make it easy to upload and study
A systematic review of self-coping strategies used by university students to ...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Microbial diseases, their pathogenesis and prophylaxis
Chinmaya Tiranga quiz Grand Finale.pdf

KMP String Matching Algorithm

  • 1. Unit IV- KMP String Matching Algorithm
  • 2. • The Knuth–Morris–Pratt string-searching algorithm (or KMP algorithm) searches for occurrences of a "word" W within a main "text string" S when a mismatch occurs, the pattern P has sufficient information to determine where the next potential match could begin thereby avoiding several unnecessary matching bringing the time complexity to linear. • Knuth-Morris and Pratt introduce a linear time algorithm for the string matching problem. • It which checks the characters from left to right. When a pattern has a sub-pattern appears more than one in the sub-pattern. Knuth–Morris–Pratt
  • 3. Some of the applications are Text editors in computing machines, Database queries, Bioinformatics and Cheminformatics, two dimensional mesh, network intrusion detections system, wide window pattern matching (large string matching), music content retrievals, language syntax checker, ms word spell checker, matching DNA sequences, digital libraries, search engines. Applications
  • 4. Components of KMPAlgorithm: 1. The Prefix Function (Π): The prefix function for this string is defined as an array π of length n, where π[i] is the length of the longest proper prefix of the substring s[0…i] which is also a suffix of this substring. A proper prefix of a string is a prefix that is not equal to the string itself. By definition, π[0]=0. 2. The KMP Matcher: With string 'S,' pattern 'p' and prefix function 'Π' as inputs, find the occurrence of 'p' in 'S' and returns the number of shifts of 'p' after which occurrences are found.
  • 5. The Prefix Function (Π) Following pseudo code compute the prefix function, Π: COMPUTE- PREFIX- FUNCTION (P) 1. m ←length [P] //'p' pattern to be matched 2. Π [1] ← 0 3. k ← 0 4. for q ← 2 to m 5. do while k > 0 and P [k + 1] ≠ P [q] 6. do k ← Π [k] 7. If P [k + 1] = P [q] 8. then k← k + 1 9. Π [q] ← k 10. Return Π
  • 6. Example: Compute Π for the pattern 'p' below: Initially: m = length [p] = 7 Π [1] = 0 k = 0
  • 7. COMPUTE- PREFIX- FUNCTION (P) 1. m ←length [P] //'p' pattern to be matched 2. Π [1] ← 0 3. k ← 0 4. for q ← 2 to m 5. do while k > 0 and P [k + 1] ≠ P [q] 6. do k ← Π [k-1] 7. If P [k + 1] = P [q] 8. then k← k + 1 9. Π [q] ← k 10. Return Π
  • 10. Running Time Analysis: For calculating the prefix function, the for loop from step 4 to step 10 runs 'm' times. Step1 to Step3 take constant time. Hence the running time of computing prefix function is O (m).
  • 11. The KMP Matcher: The KMP Matcher with the pattern 'p,' the string ‘T' and prefix function 'Π' as input, finds a match of p in T. Following pseudo code compute the matching component of KMP algorithm: KMP-MATCHER (T, P) 1. n ← length [S] 2. m ← length [P] 3. Π← COMPUTE-PREFIX-FUNCTION (P) 4. q ← 0 // numbers of characters matched 5. for i ← 1 to n // scan S from left to right 6. do while q > 0 and P [q + 1] ≠ T [i] 7. do q ← Π [q] // next character does not match 8. If P [q + 1] = T [i] 9. then q ← q + 1 // next character matches 10. If q = m // is all of p matched? 11. then print "Pattern occurs with shift" i - m 12. q ← Π [q] // look for the next match
  • 12. Running Time Analysis: The for loop beginning in step 5 runs 'n' times, i.e., as long as the length of the string 'S.' Since step 1 to step 4 take constant times, the running time is dominated by this for the loop. Thus running time of the matching function is O (n).
  • 14. KMP-MATCHER (T, P) 1. n ← length [S] 2. m ← length [P] 3. Π← COMPUTE-PREFIX-FUNCTION (P) 4. q ← 0 5. for i ← 1 to n 6. do while q > 0 and P [q + 1] ≠ T [i] 7. do q ← Π [q] 8. If P [q + 1] = T [i] 9. then q ← q + 1 10. If q = m 11. then print "Pattern occurs with shift" i - m 12. q ← Π [q]
  • 15. KMP-MATCHER (T, P) 1. n ← length [S] 2. m ← length [P] 3. Π← COMPUTE-PREFIX-FUNCTION (P) 4. q ← 0 5. for i ← 1 to n 6. do while q > 0 and P [q + 1] ≠ T [i] 7. do q ← Π [q] 8. If P [q + 1] = T [i] 9. then q ← q + 1 10. If q = m 11. then print "Pattern occurs with shift" i - m 12. q ← Π [q]
  • 16. KMP-MATCHER (T, P) 1. n ← length [S] 2. m ← length [P] 3. Π← COMPUTE-PREFIX-FUNCTION (P) 4. q ← 0 5. for i ← 1 to n 6. do while q > 0 and P [q + 1] ≠ T [i] 7. do q ← Π [q] 8. If P [q + 1] = T [i] 9. then q ← q + 1 10. If q = m 11. then print "Pattern occurs with shift" i - m 12. q ← Π [q]
  • 17. KMP-MATCHER (T, P) 1. n ← length [S] 2. m ← length [P] 3. Π← COMPUTE-PREFIX-FUNCTION (P) 4. q ← 0 5. for i ← 1 to n 6. do while q > 0 and P [q + 1] ≠ T [i] 7. do q ← Π [q] 8. If P [q + 1] = T [i] 9. then q ← q + 1 10. If q = m 11. then print "Pattern occurs with shift" i - m 12. q ← Π [q]
  • 18. KMP-MATCHER (T, P) 1. n ← length [S] 2. m ← length [P] 3. Π← COMPUTE-PREFIX-FUNCTION (P) 4. q ← 0 5. for i ← 1 to n 6. do while q > 0 and P [q + 1] ≠ T [i] 7. do q ← Π [q] 8. If P [q + 1] = T [i] 9. then q ← q + 1 10. If q = m 11. then print "Pattern occurs with shift" i - m 12. q ← Π [q]