PPT PRESENTATION
DSA
TOPIC : Knuth–Morris–Pratt algorithm
By : Pavan Kumar BM
1
INTRODUCTION
TO KMP
ALGORITHM .
INDEX
2
• CREATING LPS
TABLE
• DEFINITION OF
PREFIX AND SUFFIX
INDEX
3
• IMPLEMENTATION
OF THE ALGORITM
• THE PROGRAM IN
‘C’
INDEX
Knuth-Morris-Pratt String
Search
The Knuth-Morris-Pratt (KMP) algorithm is a powerful and efficient
string searching algorithm that plays a crucial role in pattern
matching. Developed by Donald Knuth, Vaughan Pratt, and James H.
Morris, the KMP algorithm addresses the challenge of efficiently
finding occurrences of a pattern within a larger text.
STRING
PATTREN
A B A B C A B A B
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B C A B A B
PATTREN
A B A B C A B A B
A pattern refers to a sequence of
characters that you want to find within a
larger body of text
STRING
PATTREN
A B A B C A B A B
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B C A B A B
Before diving into the KMP algorithm, we focus on
the Longest Proper Prefix which is also Suffix (LPS)
table—a key element enhancing efficiency.
LSP(Longest Proper Prefix which is also
Suffix)
So.
HOW TO CREATE
LSP(Longest Proper Prefix which is also
Suffix)
Before creating LSP let us understand the concepts of Prefix and
Suffix with an Example.
• A prefix of a string is any substring that includes the
beginning of the string
• it is a sequence of characters that appears at the start of
the string
PREFIX:
PREFIX:
STUDENT
Ex:
PREFIX:
STUDENT
Ex:
“S”
A prefix of a string STUDENT can be as follow’s :
PREFIX:
STUDENT
Ex:
“S”
A prefix of a string STUDENT can be as follow’s :
“ST”
PREFIX:
STUDENT
Ex:
“S”
A prefix of a string STUDENT can be as follow’s :
“ST”
“STU”
PREFIX:
STUDENT
Ex:
“S”
A prefix of a string STUDENT can be as follow’s :
“ST”
“STU”
“STUD”
PREFIX:
STUDENT
Ex:
“S”
A prefix of a string STUDENT can be as follow’s :
“ST”
“STU”
“STUD”
“STUDE”
PREFIX:
STUDENT
Ex:
“S”
A prefix of a string STUDENT can be as follow’s :
“ST”
“STU”
“STUD”
“STUDE”
“STUDEN”
PREFIX:
STUDENT
Ex:
“S”
A prefix of a string STUDENT can be as follow’s :
“ST”
“STU”
“STUD”
“STUDE”
“STUDEN”
“STUDENT”
SUFFIX:
• A suffix of a string is any substring that includes the end of the string.
• It is a sequence of characters that appears at the end of the string, possibly the
entire string itself.
SUFFIX:
SUFFIX:
STUDENT
Ex:
“T”
A Suffix of a string STUDENT can be as follow’s :
Suffix:
STUDENT
Ex:
“T”
A Suffix of a string STUDENT can be as follow’s :
“NT”
Suffix:
STUDENT
Ex:
“T”
A Suffix of a string STUDENT can be as follow’s :
“NT”
“ENT”
Suffix:
STUDENT
Ex:
“T”
A Suffix of a string STUDENT can be as follow’s :
“NT”
“ENT”
“DENT”
Suffix:
STUDENT
Ex:
“T”
A Suffix of a string STUDENT can be as follow’s :
“NT”
“ENT”
“DENT”
“UDENT”
Suffix:
STUDENT
Ex:
“T”
A Suffix of a string STUDENT can be as follow’s :
“NT”
“ENT”
“DENT”
“UDENT”
“TUDENT”
Suffix:
STUDENT
Ex:
“T”
A Suffix of a string STUDENT can be as follow’s :
“NT”
“ENT”
“DENT”
“UDENT”
“TUDENT”
“STUDENT”
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
STRING
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
STRING
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 2 3 4 5 6 7 8 9
TABLE
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0
INITIAL INDEX = 0 & J = 1
Ind=0 j=0
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0
IF B!=A & Ind=0 send j=2
Ind=0 j=0
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 0 0 0 0 0 0
A == A Set table[2] = 1
Set ind = 1 and j =3
Ind=0 j=2
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 0 0 0 0 0
B == B Set Table[3] = 2
Set Ind = 2 and j = 4
Ind=1 j=3
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 0 0 0 0
A == A Set Table[4] = 3
Set Ind = 3 and j = 5
Ind=2 j=4
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 0 0 0 0
A != B Set Ind > 0
Set Ind = Table[2] = 1
Ind=3 j=5
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 0 0 0 0
A != B Set Ind > 0
Set Ind = Table[1] = 0
Ind=1 j=5
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 0 0 0
A== A Set Table[5] = 1
Set Ins = 1 and j = 6
Ind=1 j=5
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 4 5 0 0
B== B Set Table[6] = 2
Set Ins = 2 and j = 7
Ind=2 j=6
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A B
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A== A Set Table[7] = 3
Set Ins = 3 and j = 8
Ind=2 j=7
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A C
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
C == B Set Table[8] = 0
Set Ins = 3 and j = 8
Ind=3 j=8
The Knuth-Morris-Pratt (KMP) algorithm
A B A B D A B A C D A B A B C A B A B
PATTREN
A B A B A A B A C
ARRAY OF STRING
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 1 2 3 4 5 6 7 8
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
C == B Set Table[8] = 0
Set Ins = 3 and j = 8
Ind=3 j=8
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
THE TABLE FOR THE PATTREN IS CREATED
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
Start string searching
Set i=o and j=0
I=0
J=o
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘A’ == ‘A’
Set I==1,J==1
I=0
J=o
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘B’ == ‘B’
Set I==2,J==2
I=1
J=1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘C’ != ‘A’ and J!=0
Set J=T[J-1]=0
I=2
J=2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘C’ != ‘A’
Set I=3
I=2
J=0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘A’ == ‘A’
Set I=4,J=1
I=3
J=0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘B’ == ‘B’
Set I=5,J=2
I=4
J=1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘A’ == ‘A’
Set I=6,J=3
I=5
J=2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘B’ == ‘B’
Set I=7,J=4
I=6
J=3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘ ’! = ‘A’ and J!=0.
Set J=T[J-1]=2
I=7
J=4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘ ’! = ‘A’ and J!=0.
Set J=T[J-1]=0
I=7
J=2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘ ’! = ‘A’
Set I=8
I=7
J=0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘A ’= = ‘A’
Set I=9,J=1
I=8
J=0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘B ’== ‘B’
Set I=10,J=2
I=9
J=1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘A ’== ‘A’
Set I=11,J=3
I=10
J=2
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘B’== ‘B’
Set I=12,J=4
I=11
J=3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘A’== ‘A’
Set I=13,J=5
I=12
J=4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :Pattern
‘B’!= ‘A’ and J!=0.
Set J=T[I-1]= 3
I=13
J=5
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :P
‘B’==‘B’
Set I=14,J=4
I=13
J=3
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :P
‘A’==‘A’
Set I=15,J=5
I=14
J=4
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :P
‘A’==‘A’
Set I=16,J=6
I=15
J=5
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :P
‘B’==‘B’
Set I=17,J=7
I=16
J=6
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
TABLE
0 1 2 3 4 5 6 7 8
0 0 1 2 3 1 2 3 0
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A :P
Found pattern at index (i-j)=10
I=17
J=7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 1 2 3 4 5 6 7
A B C A B A B A B A B A B A A B A
The Knuth-Morris-Pratt (KMP) algorithm
Array of string
A B A B A A B A
Pattern
Found pattern at index (i-j)=10
Time complexity of the KMP() is O(N+M)
Advantage’s of using KMP Algorithm
1.Linear Time Complexity:
•The KMP algorithm guarantees linear time
complexity for pattern matching, making it efficient
for large texts and patterns.
2.Optimal for Repeated Patterns
•KMP excels in scenarios where patterns have
repeated sections, as it efficiently skips unnecessar
comparisons using LPS
3. Memory Efficiency
While it requires additional memory for the LPS
table, the memory requirements of KMP are
generally lower compared to some other
algorithms.
Disadvantage’s of using KMP Algorithm
1.Preprocessing Overhead:
The preprocessing phase may introduce extra
computation time, particularly for long patterns,
2. Limited Applications:
KMP is specifically designed for exact string
matching and may not be the best choice for other
variations of the matching problem
3.Complexity of Implementation
Implementing the KMP algorithm can be more
complex compared to simpler algorithms, and
understanding the concept of the LPS table requires
additional comprehension.
Conclusion: In conclusion, the Knuth-Morris-Pratt
algorithm provides a reliable solution for pattern
matching tasks. The choice to employ KMP should be
informed by the specific characteristics of the problem,
balancing considerations related to memory usage and
preprocessing time. KMP stands as a testament to
algorithmic ingenuity, offering an elegant and effective
approach to exact string pattern matching.*
C code for the KMP algorithm:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void computeLPS(char *pattern, int M, int *lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
void KMPSearch(char *text, char *pattern) {
int M = strlen(pattern);
int N = strlen(text);
int *lps = (int *)malloc(sizeof(int) * M);
if (lps == NULL) {
fprintf(stderr, "Memory allocation failed.n");
exit(EXIT_FAILURE);
}
computeLPS(pattern, M, lps);
int i = 0;
int j = 0;
while (i < N) {
if (pattern[j] == text[i]) {
j++;
i++;
}
if (j == M) {
printf("Pattern found at index %dn", i - j);
j = lps[j - 1];
} else if (i < N && pattern[j] != text[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
}
free(lps);
}
int main() {
char text[] = "ABABDABACDABABCABAB";
char pattern[] = "ABABCABAB";
printf("Text: %sn", text);
printf("Pattern: %sn", pattern);
KMPSearch(text, pattern);
return 0;
}
C code for the KMP algorithm:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void computeLPS(char *pattern,
int M, int *lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
These lines include necessary header files for input/output operations (stdio.h),
dynamic memory allocation (stdlib.h), and string manipulation functions
(string.h).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void computeLPS(char *pattern,
int M, int *lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
void computeLPS(char *pattern,
int M, int *lps) {
int len = 0;
lps[0] = 0;
int i = 1;
This line defines a function named computeLPS that computes the Longest
Prefix which is also a Suffix (LPS) array for a given pattern. It takes three
parameters: pattern (the pattern string), M (length of the pattern), and lps (an
array to store the computed LPS values).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void computeLPS(char *pattern, int M,
int *lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
while (i < M) {
if (pattern[i] == pattern[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
This initializes a variable i to 1 and starts a while loop that iterates through the
pattern and checks if the current characters in the pattern at positions i and len are
equal. If they are, it increments len, sets lps[i] to the current value of len, and
increments i. If the characters are not equal, it goes to the else part.
while (i < N) {
if (pattern[j] == text[i]) {
j++;
i++;
}
if (j == M) {
printf("Pattern found at index %dn", i - j);
j = lps[j - 1];
} else if (i < N && pattern[j] != text[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
free(lps);
}
void KMPSearch(char *text, char *pattern) {
int M = strlen(pattern);
int N = strlen(text);
int *lps = (int *)malloc(sizeof(int) * M);
if (lps == NULL) {
fprintf(stderr, "Memory allocation failed.n");
exit(EXIT_FAILURE);
}
computeLPS(pattern, M, lps);
int i = 0;
int j = 0;
void KMPSearch(char *text, char *pattern)
{
int M = strlen(pattern);
int N = strlen(text);
int *lps = (int *)malloc(sizeof(int) * M);
n");
This line defines a function named KMPSearch that performs the Knuth-Morris-Pratt string matching
algorithm. It takes two parameters: text (the text in which the pattern is to be searched) and pattern
(the pattern to be searched). These lines calculate the length of the pattern and the text.
This dynamically allocates memory for the LPS array using malloc.
while (i < N) {
if (pattern[j] == text[i]) {
j++;
i++;
}
if (j == M) {
printf("Pattern found at index %dn", i - j);
j = lps[j - 1];
} else if (i < N && pattern[j] != text[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
free(lps);
}
void KMPSearch(char *text, char *pattern) {
int M = strlen(pattern);
int N = strlen(text);
int *lps = (int *)malloc(sizeof(int) * M);
if (lps == NULL) {
fprintf(stderr, "Memory allocation failed.n");
exit(EXIT_FAILURE);
}
computeLPS(pattern, M, lps);
int i = 0;
int j = 0;
if (lps == NULL) {
fprintf(stderr, "Memory allocation
failed.n");
exit(EXIT_FAILURE);
}
If the allocation fails, it prints an error message and exits the program.
while (i < N) {
if (pattern[j] == text[i]) {
j++;
i++;
}
if (j == M) {
printf("Pattern found at index %dn", i - j);
j = lps[j - 1];
} else if (i < N && pattern[j] != text[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
free(lps);
}
void KMPSearch(char *text, char *pattern) {
int M = strlen(pattern);
int N = strlen(text);
int *lps = (int *)malloc(sizeof(int) * M);
if (lps == NULL) {
fprintf(stderr, "Memory allocation failed.n");
exit(EXIT_FAILURE);
}
computeLPS(pattern, M, lps);
int i = 0;
int j = 0;
computeLPS(pattern, M, lps);
int i = 0;
int j = 0;
computeLPS(pattern, M, lps);: Computes the LPS array for the given pattern.
int i = 0, j = 0;: Initializes indices for text (i) and pattern (j) in the Knuth-Morris-Pratt algorithm.
while (i < N) {
if (pattern[j] == text[i]) {
j++;
i++;
}
if (j == M) {
printf("Pattern found at index %dn", i - j);
j = lps[j - 1];
} else if (i < N && pattern[j] != text[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
free(lps);
}
void KMPSearch(char *text, char *pattern) {
int M = strlen(pattern);
int N = strlen(text);
int *lps = (int *)malloc(sizeof(int) * M);
if (lps == NULL) {
fprintf(stderr, "Memory allocation failed.n");
exit(EXIT_FAILURE);
}
computeLPS(pattern, M, lps);
int i = 0;
int j = 0;
while (i < N) {
if (pattern[j] == text[i]) {
j++;
i++;
}
if (j == M) {
printf("Pattern found at index %dn", i -
j);
j = lps[j - 1];
} else if (i < N && pattern[j] != text[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
free(lps);
}
while (i < N) {: Loop through the text.
if (pattern[j] == text[i]) { j++; i++; }: Match characters,
advancing indices.
if (j == M) { printf("Pattern found at index %dn", i -
j); j = lps[j - 1]; }: Check if pattern is found, print, and
adjust pattern index.
else if (i < N && pattern[j] != text[i]) { j = (j != 0) ? lps[j
- 1] : 0; i += (j == 0); }: Handle non-matching
characters, adjust indices based on LPS array.
int main() {
char text[] = "ABABDABACDABABCABAB";
char pattern[] = "WrongPattern"; // Change to a pattern that is not in the text
printf("Text: %sn", text);
printf("Pattern: %sn", pattern);
KMPSearch(text, pattern);
return 0;
}
Thank Y u

Detailed Presentation on KMP Algorithm for Efficient Pattern Searching

  • 1.
    PPT PRESENTATION DSA TOPIC :Knuth–Morris–Pratt algorithm By : Pavan Kumar BM
  • 2.
  • 3.
    2 • CREATING LPS TABLE •DEFINITION OF PREFIX AND SUFFIX INDEX
  • 4.
    3 • IMPLEMENTATION OF THEALGORITM • THE PROGRAM IN ‘C’ INDEX
  • 5.
    Knuth-Morris-Pratt String Search The Knuth-Morris-Pratt(KMP) algorithm is a powerful and efficient string searching algorithm that plays a crucial role in pattern matching. Developed by Donald Knuth, Vaughan Pratt, and James H. Morris, the KMP algorithm addresses the challenge of efficiently finding occurrences of a pattern within a larger text.
  • 6.
    STRING PATTREN A B AB C A B A B A B A B D A B A C D A B A B C A B A B
  • 7.
    PATTREN A B AB C A B A B
  • 8.
    PATTREN A B AB C A B A B A pattern refers to a sequence of characters that you want to find within a larger body of text
  • 9.
    STRING PATTREN A B AB C A B A B A B A B D A B A C D A B A B C A B A B
  • 10.
    PATTREN A B AB C A B A B Before diving into the KMP algorithm, we focus on the Longest Proper Prefix which is also Suffix (LPS) table—a key element enhancing efficiency.
  • 11.
    LSP(Longest Proper Prefixwhich is also Suffix) So. HOW TO CREATE
  • 12.
    LSP(Longest Proper Prefixwhich is also Suffix) Before creating LSP let us understand the concepts of Prefix and Suffix with an Example. • A prefix of a string is any substring that includes the beginning of the string • it is a sequence of characters that appears at the start of the string PREFIX:
  • 13.
  • 14.
    PREFIX: STUDENT Ex: “S” A prefix ofa string STUDENT can be as follow’s :
  • 15.
    PREFIX: STUDENT Ex: “S” A prefix ofa string STUDENT can be as follow’s : “ST”
  • 16.
    PREFIX: STUDENT Ex: “S” A prefix ofa string STUDENT can be as follow’s : “ST” “STU”
  • 17.
    PREFIX: STUDENT Ex: “S” A prefix ofa string STUDENT can be as follow’s : “ST” “STU” “STUD”
  • 18.
    PREFIX: STUDENT Ex: “S” A prefix ofa string STUDENT can be as follow’s : “ST” “STU” “STUD” “STUDE”
  • 19.
    PREFIX: STUDENT Ex: “S” A prefix ofa string STUDENT can be as follow’s : “ST” “STU” “STUD” “STUDE” “STUDEN”
  • 20.
    PREFIX: STUDENT Ex: “S” A prefix ofa string STUDENT can be as follow’s : “ST” “STU” “STUD” “STUDE” “STUDEN” “STUDENT”
  • 21.
    SUFFIX: • A suffixof a string is any substring that includes the end of the string. • It is a sequence of characters that appears at the end of the string, possibly the entire string itself.
  • 22.
  • 23.
    SUFFIX: STUDENT Ex: “T” A Suffix ofa string STUDENT can be as follow’s :
  • 24.
    Suffix: STUDENT Ex: “T” A Suffix ofa string STUDENT can be as follow’s : “NT”
  • 25.
    Suffix: STUDENT Ex: “T” A Suffix ofa string STUDENT can be as follow’s : “NT” “ENT”
  • 26.
    Suffix: STUDENT Ex: “T” A Suffix ofa string STUDENT can be as follow’s : “NT” “ENT” “DENT”
  • 27.
    Suffix: STUDENT Ex: “T” A Suffix ofa string STUDENT can be as follow’s : “NT” “ENT” “DENT” “UDENT”
  • 28.
    Suffix: STUDENT Ex: “T” A Suffix ofa string STUDENT can be as follow’s : “NT” “ENT” “DENT” “UDENT” “TUDENT”
  • 29.
    Suffix: STUDENT Ex: “T” A Suffix ofa string STUDENT can be as follow’s : “NT” “ENT” “DENT” “UDENT” “TUDENT” “STUDENT”
  • 30.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B STRING
  • 31.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B STRING
  • 32.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 1 2 3 4 5 6 7 8 9 TABLE
  • 33.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 0
  • 34.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 0 INITIAL INDEX = 0 & J = 1 Ind=0 j=0
  • 35.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 0 IF B!=A & Ind=0 send j=2 Ind=0 j=0
  • 36.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 1 0 0 0 0 0 0 A == A Set table[2] = 1 Set ind = 1 and j =3 Ind=0 j=2
  • 37.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 1 2 0 0 0 0 0 B == B Set Table[3] = 2 Set Ind = 2 and j = 4 Ind=1 j=3
  • 38.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 1 2 3 0 0 0 0 A == A Set Table[4] = 3 Set Ind = 3 and j = 5 Ind=2 j=4
  • 39.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 1 2 3 0 0 0 0 A != B Set Ind > 0 Set Ind = Table[2] = 1 Ind=3 j=5
  • 40.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 1 2 3 0 0 0 0 A != B Set Ind > 0 Set Ind = Table[1] = 0 Ind=1 j=5
  • 41.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 1 2 3 4 0 0 0 A== A Set Table[5] = 1 Set Ins = 1 and j = 6 Ind=1 j=5
  • 42.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 1 2 3 4 5 0 0 B== B Set Table[6] = 2 Set Ins = 2 and j = 7 Ind=2 j=6
  • 43.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A B ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A== A Set Table[7] = 3 Set Ins = 3 and j = 8 Ind=2 j=7
  • 44.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A C ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 1 2 3 1 2 3 0 C == B Set Table[8] = 0 Set Ins = 3 and j = 8 Ind=3 j=8
  • 45.
    The Knuth-Morris-Pratt (KMP)algorithm A B A B D A B A C D A B A B C A B A B PATTREN A B A B A A B A C ARRAY OF STRING 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 1 2 3 4 5 6 7 8 TABLE 0 1 2 3 4 5 6 7 8 0 0 1 2 3 1 2 3 0 C == B Set Table[8] = 0 Set Ins = 3 and j = 8 Ind=3 j=8
  • 46.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 THE TABLE FOR THE PATTREN IS CREATED
  • 47.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0
  • 48.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 49.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern Start string searching Set i=o and j=0 I=0 J=o 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 50.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘A’ == ‘A’ Set I==1,J==1 I=0 J=o 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 51.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘B’ == ‘B’ Set I==2,J==2 I=1 J=1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 52.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘C’ != ‘A’ and J!=0 Set J=T[J-1]=0 I=2 J=2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 53.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘C’ != ‘A’ Set I=3 I=2 J=0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 54.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘A’ == ‘A’ Set I=4,J=1 I=3 J=0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 55.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘B’ == ‘B’ Set I=5,J=2 I=4 J=1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 56.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘A’ == ‘A’ Set I=6,J=3 I=5 J=2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 57.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘B’ == ‘B’ Set I=7,J=4 I=6 J=3 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 58.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘ ’! = ‘A’ and J!=0. Set J=T[J-1]=2 I=7 J=4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 59.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘ ’! = ‘A’ and J!=0. Set J=T[J-1]=0 I=7 J=2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 60.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘ ’! = ‘A’ Set I=8 I=7 J=0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 61.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘A ’= = ‘A’ Set I=9,J=1 I=8 J=0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 62.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘B ’== ‘B’ Set I=10,J=2 I=9 J=1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 63.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘A ’== ‘A’ Set I=11,J=3 I=10 J=2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 64.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘B’== ‘B’ Set I=12,J=4 I=11 J=3 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 65.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘A’== ‘A’ Set I=13,J=5 I=12 J=4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 66.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :Pattern ‘B’!= ‘A’ and J!=0. Set J=T[I-1]= 3 I=13 J=5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 67.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :P ‘B’==‘B’ Set I=14,J=4 I=13 J=3 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 68.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :P ‘A’==‘A’ Set I=15,J=5 I=14 J=4 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 69.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :P ‘A’==‘A’ Set I=16,J=6 I=15 J=5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 70.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :P ‘B’==‘B’ Set I=17,J=7 I=16 J=6 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 71.
    TABLE 0 1 23 4 5 6 7 8 0 0 1 2 3 1 2 3 0 A B C A B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A :P Found pattern at index (i-j)=10 I=17 J=7 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 0 1 2 3 4 5 6 7
  • 72.
    A B CA B A B A B A B A B A A B A The Knuth-Morris-Pratt (KMP) algorithm Array of string A B A B A A B A Pattern Found pattern at index (i-j)=10 Time complexity of the KMP() is O(N+M)
  • 73.
    Advantage’s of usingKMP Algorithm 1.Linear Time Complexity: •The KMP algorithm guarantees linear time complexity for pattern matching, making it efficient for large texts and patterns. 2.Optimal for Repeated Patterns •KMP excels in scenarios where patterns have repeated sections, as it efficiently skips unnecessar comparisons using LPS 3. Memory Efficiency While it requires additional memory for the LPS table, the memory requirements of KMP are generally lower compared to some other algorithms.
  • 74.
    Disadvantage’s of usingKMP Algorithm 1.Preprocessing Overhead: The preprocessing phase may introduce extra computation time, particularly for long patterns, 2. Limited Applications: KMP is specifically designed for exact string matching and may not be the best choice for other variations of the matching problem 3.Complexity of Implementation Implementing the KMP algorithm can be more complex compared to simpler algorithms, and understanding the concept of the LPS table requires additional comprehension. Conclusion: In conclusion, the Knuth-Morris-Pratt algorithm provides a reliable solution for pattern matching tasks. The choice to employ KMP should be informed by the specific characteristics of the problem, balancing considerations related to memory usage and preprocessing time. KMP stands as a testament to algorithmic ingenuity, offering an elegant and effective approach to exact string pattern matching.*
  • 75.
    C code forthe KMP algorithm: #include <stdio.h> #include <stdlib.h> #include <string.h> void computeLPS(char *pattern, int M, int *lps) { int len = 0; lps[0] = 0; int i = 1; while (i < M) { if (pattern[i] == pattern[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } void KMPSearch(char *text, char *pattern) { int M = strlen(pattern); int N = strlen(text); int *lps = (int *)malloc(sizeof(int) * M); if (lps == NULL) { fprintf(stderr, "Memory allocation failed.n"); exit(EXIT_FAILURE); } computeLPS(pattern, M, lps); int i = 0; int j = 0; while (i < N) { if (pattern[j] == text[i]) { j++; i++; } if (j == M) { printf("Pattern found at index %dn", i - j); j = lps[j - 1]; } else if (i < N && pattern[j] != text[i]) { if (j != 0) { j = lps[j - 1]; } else { i++; } } }
  • 76.
    } free(lps); } int main() { chartext[] = "ABABDABACDABABCABAB"; char pattern[] = "ABABCABAB"; printf("Text: %sn", text); printf("Pattern: %sn", pattern); KMPSearch(text, pattern); return 0; } C code for the KMP algorithm:
  • 77.
    #include <stdio.h> #include <stdlib.h> #include<string.h> void computeLPS(char *pattern, int M, int *lps) { int len = 0; lps[0] = 0; int i = 1; while (i < M) { if (pattern[i] == pattern[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } }
  • 78.
    #include <stdio.h> #include <stdlib.h> #include<string.h> These lines include necessary header files for input/output operations (stdio.h), dynamic memory allocation (stdlib.h), and string manipulation functions (string.h).
  • 79.
    #include <stdio.h> #include <stdlib.h> #include<string.h> void computeLPS(char *pattern, int M, int *lps) { int len = 0; lps[0] = 0; int i = 1; while (i < M) { if (pattern[i] == pattern[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } }
  • 80.
    void computeLPS(char *pattern, intM, int *lps) { int len = 0; lps[0] = 0; int i = 1; This line defines a function named computeLPS that computes the Longest Prefix which is also a Suffix (LPS) array for a given pattern. It takes three parameters: pattern (the pattern string), M (length of the pattern), and lps (an array to store the computed LPS values).
  • 81.
    #include <stdio.h> #include <stdlib.h> #include<string.h> void computeLPS(char *pattern, int M, int *lps) { int len = 0; lps[0] = 0; int i = 1; while (i < M) { if (pattern[i] == pattern[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } }
  • 82.
    while (i <M) { if (pattern[i] == pattern[len]) { len++; lps[i] = len; i++; } else { if (len != 0) { len = lps[len - 1]; } else { lps[i] = 0; i++; } } } } This initializes a variable i to 1 and starts a while loop that iterates through the pattern and checks if the current characters in the pattern at positions i and len are equal. If they are, it increments len, sets lps[i] to the current value of len, and increments i. If the characters are not equal, it goes to the else part.
  • 83.
    while (i <N) { if (pattern[j] == text[i]) { j++; i++; } if (j == M) { printf("Pattern found at index %dn", i - j); j = lps[j - 1]; } else if (i < N && pattern[j] != text[i]) { if (j != 0) { j = lps[j - 1]; } else { i++; } } } free(lps); } void KMPSearch(char *text, char *pattern) { int M = strlen(pattern); int N = strlen(text); int *lps = (int *)malloc(sizeof(int) * M); if (lps == NULL) { fprintf(stderr, "Memory allocation failed.n"); exit(EXIT_FAILURE); } computeLPS(pattern, M, lps); int i = 0; int j = 0;
  • 84.
    void KMPSearch(char *text,char *pattern) { int M = strlen(pattern); int N = strlen(text); int *lps = (int *)malloc(sizeof(int) * M); n"); This line defines a function named KMPSearch that performs the Knuth-Morris-Pratt string matching algorithm. It takes two parameters: text (the text in which the pattern is to be searched) and pattern (the pattern to be searched). These lines calculate the length of the pattern and the text. This dynamically allocates memory for the LPS array using malloc.
  • 85.
    while (i <N) { if (pattern[j] == text[i]) { j++; i++; } if (j == M) { printf("Pattern found at index %dn", i - j); j = lps[j - 1]; } else if (i < N && pattern[j] != text[i]) { if (j != 0) { j = lps[j - 1]; } else { i++; } } } free(lps); } void KMPSearch(char *text, char *pattern) { int M = strlen(pattern); int N = strlen(text); int *lps = (int *)malloc(sizeof(int) * M); if (lps == NULL) { fprintf(stderr, "Memory allocation failed.n"); exit(EXIT_FAILURE); } computeLPS(pattern, M, lps); int i = 0; int j = 0;
  • 86.
    if (lps ==NULL) { fprintf(stderr, "Memory allocation failed.n"); exit(EXIT_FAILURE); } If the allocation fails, it prints an error message and exits the program.
  • 87.
    while (i <N) { if (pattern[j] == text[i]) { j++; i++; } if (j == M) { printf("Pattern found at index %dn", i - j); j = lps[j - 1]; } else if (i < N && pattern[j] != text[i]) { if (j != 0) { j = lps[j - 1]; } else { i++; } } } free(lps); } void KMPSearch(char *text, char *pattern) { int M = strlen(pattern); int N = strlen(text); int *lps = (int *)malloc(sizeof(int) * M); if (lps == NULL) { fprintf(stderr, "Memory allocation failed.n"); exit(EXIT_FAILURE); } computeLPS(pattern, M, lps); int i = 0; int j = 0;
  • 88.
    computeLPS(pattern, M, lps); inti = 0; int j = 0; computeLPS(pattern, M, lps);: Computes the LPS array for the given pattern. int i = 0, j = 0;: Initializes indices for text (i) and pattern (j) in the Knuth-Morris-Pratt algorithm.
  • 89.
    while (i <N) { if (pattern[j] == text[i]) { j++; i++; } if (j == M) { printf("Pattern found at index %dn", i - j); j = lps[j - 1]; } else if (i < N && pattern[j] != text[i]) { if (j != 0) { j = lps[j - 1]; } else { i++; } } } free(lps); } void KMPSearch(char *text, char *pattern) { int M = strlen(pattern); int N = strlen(text); int *lps = (int *)malloc(sizeof(int) * M); if (lps == NULL) { fprintf(stderr, "Memory allocation failed.n"); exit(EXIT_FAILURE); } computeLPS(pattern, M, lps); int i = 0; int j = 0;
  • 90.
    while (i <N) { if (pattern[j] == text[i]) { j++; i++; } if (j == M) { printf("Pattern found at index %dn", i - j); j = lps[j - 1]; } else if (i < N && pattern[j] != text[i]) { if (j != 0) { j = lps[j - 1]; } else { i++; } } } free(lps); } while (i < N) {: Loop through the text. if (pattern[j] == text[i]) { j++; i++; }: Match characters, advancing indices. if (j == M) { printf("Pattern found at index %dn", i - j); j = lps[j - 1]; }: Check if pattern is found, print, and adjust pattern index. else if (i < N && pattern[j] != text[i]) { j = (j != 0) ? lps[j - 1] : 0; i += (j == 0); }: Handle non-matching characters, adjust indices based on LPS array.
  • 91.
    int main() { chartext[] = "ABABDABACDABABCABAB"; char pattern[] = "WrongPattern"; // Change to a pattern that is not in the text printf("Text: %sn", text); printf("Pattern: %sn", pattern); KMPSearch(text, pattern); return 0; }
  • 92.