SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Downloaden Sie, um offline zu lesen
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Knuth-Morris-Pratt Algorithm
Kranthi Kumar Mandumula
December 18, 2011
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
outline
Definition
History
Components of KMP
Algorithm
Example
Run-Time Analysis
Advantages and Disadvantages
References
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Definition:
Best known for linear time for exact matching.
Compares from left to right.
Shifts more than one position.
Preprocessing approach of Pattern to avoid trivial
comparisions.
Avoids recomputing matches.
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
History:
This algorithm was conceived by Donald Knuth
and Vaughan Pratt and independently by James
H.Morris in 1977.
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
History:
Knuth, Morris and Pratt discovered first linear time
string-matching algorithm by analysis of the naive
algorithm.
It keeps the information that naive approach
wasted gathered during the scan of the text. By
avoiding this waste of information, it achieves a
running time of O(m + n).
The implementation of Knuth-Morris-Pratt
algorithm is efficient because it minimizes the total
number of comparisons of the pattern against the
input string.
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Components of KMP:
The prefix-function :
It preprocesses the pattern to find matches of
prefixes of the pattern with the pattern itself.
It is defined as the size of the largest prefix of
P[0..j − 1] that is also a suffix of P[1..j].
It also indicates how much of the last
comparison can be reused if it fails.
It enables avoiding backtracking on the string
‘S’.
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
m ← length[p]
a[1] ← 0
k ← 0
for q ← 2 to m do
while k > 0 and p[k + 1] p[q] do
k ← a[k]
end while
if p[k + 1] = p[q] then
k ← k + 1
end if
a[q] ← k
end for
return
Here a =
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Computation of Prefix-function with example:
Let us consider an example of how to compute
for the pattern ‘p’.
Pattern a b a b a c a
I n i t i a l l y : m = length [ p]= 7
[1]= 0
k=0
where m, [1], and k are the length of the pattern,
prefix function and initial potential value
respectively.
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Step 1: q = 2 , k = 0
[2]= 0
q 1 2 3 4 5 6 7
p a b a b a c a
0 0
Step 2: q = 3 , k = 0
[3]= 1
q 1 2 3 4 5 6 7
p a b a b a c a
0 0 1
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Step 3: q = 4 , k = 1
[4]= 2
q 1 2 3 4 5 6 7
p a b a b a c a
0 0 1 2
Step 4: q = 5 , k = 2
[5]= 3
q 1 2 3 4 5 6 7
p a b a b a c a
0 0 1 2 3
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Step 5: q = 6 , k = 3
[6]= 1
q 1 2 3 4 5 6 7
p a b a b a c a
0 0 1 2 3 1
Step 6: q = 7 , k = 1
[7]= 1
q 1 2 3 4 5 6 7
p a b a b a c a
0 0 1 2 3 1 1
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
After i t e r a t i n g 6 times , the p r e f i x function
computations i s complete :
q 1 2 3 4 5 6 7
p a b A b a c a
0 0 1 2 3 1 1
The running time of the prefix function is O(m).
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Algorithm
Step 1: I n i t i a l i z e the input variables :
n = Length of the Text .
m = Length of the Pattern .
= Prefix −function of pattern ( p ) .
q = Number of characters matched .
Step 2: Define the variable :
q=0 , the beginning of the match .
Step 3: Compare the f i r s t character of the pattern with f i r s t character of
t e x t .
I f match i s not found , s u b s t i t u t e the value of [ q ] to q .
I f match i s found , then increment the value of q by 1.
Step 4: Check whether a l l the pattern elements are matched with the t e x t
elements .
I f not , repeat the search process .
I f yes , p r i n t the number of s h i f t s taken by the pattern .
Step 5: look f o r the next match .
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
n ← length[S]
m ← length[p]
a ← Compute Prefix function
q ← 0
for i ← 1 to n do
while q > 0 and p[q + 1] S[i] do
q ← a[q]
if p[q + 1] = S[i] then
q ← q + 1
end if
if q == m then
q ← a[q]
end if
end while
end for
Here a =
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Example of KMP algorithm:
Now let us consider an example so that the algorithm
can be clearly understood.
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Let us execute the KMP algorithm to find whether ‘p’
occurs in ‘S’.
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
I n i t i a l l y : n = size of S = 15;
m = size of p=7
Step 1: i = 1 , q = 0
comparing p [ 1 ] with S[ 1 ]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
P[1] does not match with S[1]. ‘p’ will be shifted one position to the right.
Step 2: i = 2 , q = 0
comparing p [ 1 ] with S[ 2 ]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Step 3: i = 3 , q = 1
comparing p [ 2 ] with S[ 3 ] p [ 2 ] does not match with S[ 3 ]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Backtracking on p , comparing p [ 1 ] and S[ 3 ]
Step 4: i = 4 , q = 0
comparing p [ 1 ] with S[ 4 ] p [ 1 ] does not match with S[ 4 ]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Step 5: i = 5 , q = 0
comparing p [ 1 ] with S[ 5 ]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Step 6: i = 6 , q = 1
comparing p [ 2 ] with S[ 6 ] p [ 2 ] matches with S[ 6 ]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Step 7: i = 7 , q = 2
comparing p [ 3 ] with S[ 7 ] p [ 3 ] matches with S[ 7 ]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Step 8: i = 8 , q = 3
comparing p [ 4 ] with S[ 8 ] p [ 4 ] matches with S[ 8 ]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Step 9: i = 9 , q = 4
comparing p [ 5 ] with S[ 9 ] p [ 5 ] matches with S[ 9 ]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Step 10: i = 10 , q = 5
comparing p [ 6 ] with S[10] p [ 6 ] doesn ’ t matches with S[10]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Backtracking on p , comparing p [ 4 ] with S[10] because a f t e r mismatch q = [ 5 ] = 3
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Step 11: i = 11 , q = 4
comparing p [ 5 ] with S[11]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Step 12: i = 12 , q = 5
comparing p [ 6 ] with S[12] p [ 6 ] matches with S[12]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Step 13: i = 13 , q = 6
comparing p [ 7 ] with S[13] p [ 7 ] matches with S[13]
String b a c b a b a b a b a c a a b
Pattern a b a b a c a
pattern ‘p’ has been found to completely occur in
string ‘S’. The total number of shifts that took place for
the match to be found are: i − m = 13-7 = 6 shifts.
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Run-Time analysis:
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.
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Advantages and Disadvantages:
Advantages:
The running time of the KMP algorithm is
optimal (O(m + n)), which is very fast.
The algorithm never needs to move backwards
in the input text T. It makes the algorithm good for
processing very large files.
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Advantages and Disadvantages:
Disadvantages:
Doesn’t work so well as the size of the
alphabets increases. By which more chances of
mismatch occurs.
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
Knuth-Morris-Pratt
Algorithm
Kranthi Kumar
Mandumula
Graham A.Stephen, “String Searching Algorithms”,
year = 1994.
Donald Knuth, James H. Morris, Jr, Vaughan Pratt,
“Fast pattern matching in strings”, year = 1977.
Thomas H.Cormen; Charles E.Leiserson.,
Introduction to algorithms second edition , “The
Knuth-Morris-Pratt Algorithm”, year = 2001.
Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm

Weitere ähnliche Inhalte

Was ist angesagt?

RABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHINGRABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHINGAbhishek Singh
 
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
 
Rabin Carp String Matching algorithm
Rabin Carp String Matching  algorithmRabin Carp String Matching  algorithm
Rabin Carp String Matching algorithmsabiya sabiya
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp AlgorithmSohail Ahmed
 
Pattern matching
Pattern matchingPattern matching
Pattern matchingshravs_188
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesFahim Ferdous
 
String Matching Algorithms-The Naive Algorithm
String Matching Algorithms-The Naive AlgorithmString Matching Algorithms-The Naive Algorithm
String Matching Algorithms-The Naive AlgorithmAdeel Rasheed
 
knowledge representation using rules
knowledge representation using rulesknowledge representation using rules
knowledge representation using rulesHarini Balamurugan
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithmMohd Arif
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardAnimesh Chaturvedi
 
Boyer moore algorithm
Boyer moore algorithmBoyer moore algorithm
Boyer moore algorithmAYESHA JAVED
 

Was ist angesagt? (20)

String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
RABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHINGRABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHING
 
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 Carp String Matching algorithm
Rabin Carp String Matching  algorithmRabin Carp String Matching  algorithm
Rabin Carp String Matching algorithm
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Rabin Karp ppt
Rabin Karp pptRabin Karp ppt
Rabin Karp ppt
 
Rabin Karp Algorithm
Rabin Karp AlgorithmRabin Karp Algorithm
Rabin Karp Algorithm
 
Pattern matching
Pattern matchingPattern matching
Pattern matching
 
Red black tree
Red black treeRed black tree
Red black tree
 
Boyer more algorithm
Boyer more algorithmBoyer more algorithm
Boyer more algorithm
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Backtracking
Backtracking  Backtracking
Backtracking
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
String Matching Algorithms-The Naive Algorithm
String Matching Algorithms-The Naive AlgorithmString Matching Algorithms-The Naive Algorithm
String Matching Algorithms-The Naive Algorithm
 
knowledge representation using rules
knowledge representation using rulesknowledge representation using rules
knowledge representation using rules
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Dinive conquer algorithm
Dinive conquer algorithmDinive conquer algorithm
Dinive conquer algorithm
 
P, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-HardP, NP, NP-Complete, and NP-Hard
P, NP, NP-Complete, and NP-Hard
 
NP completeness
NP completenessNP completeness
NP completeness
 
Boyer moore algorithm
Boyer moore algorithmBoyer moore algorithm
Boyer moore algorithm
 

Andere mochten auch

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
 
Lesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum ValuesLesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum ValuesMatthew Leingang
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 
Maximums and minimum
Maximums and minimum Maximums and minimum
Maximums and minimum rubimedina01
 
Pattern matching in ds by m anoj vasava=mca
Pattern matching in ds by m anoj vasava=mcaPattern matching in ds by m anoj vasava=mca
Pattern matching in ds by m anoj vasava=mcaManoj_vasava
 
15 puzzle problem game on android
15 puzzle problem game on android15 puzzle problem game on android
15 puzzle problem game on androidAkhilesh Jain
 
Master method
Master method Master method
Master method Rajendran
 
Algoritma Pencarian String matching
Algoritma Pencarian String matching Algoritma Pencarian String matching
Algoritma Pencarian String matching Kukuh Setiawan
 
Naive String Matching Algorithm | Computer Science
Naive String Matching Algorithm | Computer ScienceNaive String Matching Algorithm | Computer Science
Naive String Matching Algorithm | Computer ScienceTransweb Global Inc
 
strassen matrix multiplication algorithm
strassen matrix multiplication algorithmstrassen matrix multiplication algorithm
strassen matrix multiplication algorithmevil eye
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1Kumar
 
Rabin Karp - String Matching Algorithm
Rabin Karp - String Matching AlgorithmRabin Karp - String Matching Algorithm
Rabin Karp - String Matching AlgorithmSyed Owais Ali Chishti
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 

Andere mochten auch (20)

String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.String Matching Finite Automata & KMP Algorithm.
String Matching Finite Automata & KMP Algorithm.
 
Lesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum ValuesLesson 19: Maximum and Minimum Values
Lesson 19: Maximum and Minimum Values
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Unit 3 daa
Unit 3 daaUnit 3 daa
Unit 3 daa
 
Maximums and minimum
Maximums and minimum Maximums and minimum
Maximums and minimum
 
Pattern matching in ds by m anoj vasava=mca
Pattern matching in ds by m anoj vasava=mcaPattern matching in ds by m anoj vasava=mca
Pattern matching in ds by m anoj vasava=mca
 
15 puzzle problem game on android
15 puzzle problem game on android15 puzzle problem game on android
15 puzzle problem game on android
 
Master method
Master method Master method
Master method
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Algoritmo de Rabin-Karp
Algoritmo de Rabin-KarpAlgoritmo de Rabin-Karp
Algoritmo de Rabin-Karp
 
Algoritma Pencarian String matching
Algoritma Pencarian String matching Algoritma Pencarian String matching
Algoritma Pencarian String matching
 
Naive String Matching Algorithm | Computer Science
Naive String Matching Algorithm | Computer ScienceNaive String Matching Algorithm | Computer Science
Naive String Matching Algorithm | Computer Science
 
strassen matrix multiplication algorithm
strassen matrix multiplication algorithmstrassen matrix multiplication algorithm
strassen matrix multiplication algorithm
 
String Match | Computer Science
String Match | Computer ScienceString Match | Computer Science
String Match | Computer Science
 
Greedy
GreedyGreedy
Greedy
 
Divide and conquer 1
Divide and conquer 1Divide and conquer 1
Divide and conquer 1
 
06. string matching
06. string matching06. string matching
06. string matching
 
Rabin Karp - String Matching Algorithm
Rabin Karp - String Matching AlgorithmRabin Karp - String Matching Algorithm
Rabin Karp - String Matching Algorithm
 
Mergesort
MergesortMergesort
Mergesort
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 

Ähnlich wie Kmp

String searching
String searching String searching
String searching thinkphp
 
W9Presentation.ppt
W9Presentation.pptW9Presentation.ppt
W9Presentation.pptAlinaMishra7
 
Gp 27[string matching].pptx
Gp 27[string matching].pptxGp 27[string matching].pptx
Gp 27[string matching].pptxSumitYadav641839
 
module6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdfmodule6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdfShiwani Gupta
 
String-Matching Algorithms Advance algorithm
String-Matching  Algorithms Advance algorithmString-Matching  Algorithms Advance algorithm
String-Matching Algorithms Advance algorithmssuseraf60311
 
StringMatching-Rabikarp algorithmddd.pdf
StringMatching-Rabikarp algorithmddd.pdfStringMatching-Rabikarp algorithmddd.pdf
StringMatching-Rabikarp algorithmddd.pdfbhagabatijenadukura
 
String matching algorithms-pattern matching.
String matching algorithms-pattern matching.String matching algorithms-pattern matching.
String matching algorithms-pattern matching.Swapan Shakhari
 
General Cryptography :WHY JOHNNY THE DEVELOPER CAN’T WORK WITH PUBLIC KEY CER...
General Cryptography :WHY JOHNNY THE DEVELOPER CAN’T WORK WITH PUBLIC KEY CER...General Cryptography :WHY JOHNNY THE DEVELOPER CAN’T WORK WITH PUBLIC KEY CER...
General Cryptography :WHY JOHNNY THE DEVELOPER CAN’T WORK WITH PUBLIC KEY CER...Priyanka Aash
 

Ähnlich wie Kmp (17)

KMP String Matching Algorithm
KMP String Matching AlgorithmKMP String Matching Algorithm
KMP String Matching Algorithm
 
String searching
String searching String searching
String searching
 
W9Presentation.ppt
W9Presentation.pptW9Presentation.ppt
W9Presentation.ppt
 
Gp 27[string matching].pptx
Gp 27[string matching].pptxGp 27[string matching].pptx
Gp 27[string matching].pptx
 
module6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdfmodule6_stringmatchingalgorithm_2022.pdf
module6_stringmatchingalgorithm_2022.pdf
 
String-Matching Algorithms Advance algorithm
String-Matching  Algorithms Advance algorithmString-Matching  Algorithms Advance algorithm
String-Matching Algorithms Advance algorithm
 
K-BestMatch
K-BestMatchK-BestMatch
K-BestMatch
 
StringMatching-Rabikarp algorithmddd.pdf
StringMatching-Rabikarp algorithmddd.pdfStringMatching-Rabikarp algorithmddd.pdf
StringMatching-Rabikarp algorithmddd.pdf
 
String matching algorithms-pattern matching.
String matching algorithms-pattern matching.String matching algorithms-pattern matching.
String matching algorithms-pattern matching.
 
Daa unit 5
Daa unit 5Daa unit 5
Daa unit 5
 
Boyer more algorithm
Boyer more algorithmBoyer more algorithm
Boyer more algorithm
 
General Cryptography :WHY JOHNNY THE DEVELOPER CAN’T WORK WITH PUBLIC KEY CER...
General Cryptography :WHY JOHNNY THE DEVELOPER CAN’T WORK WITH PUBLIC KEY CER...General Cryptography :WHY JOHNNY THE DEVELOPER CAN’T WORK WITH PUBLIC KEY CER...
General Cryptography :WHY JOHNNY THE DEVELOPER CAN’T WORK WITH PUBLIC KEY CER...
 
Chap09alg
Chap09algChap09alg
Chap09alg
 
Chap09alg
Chap09algChap09alg
Chap09alg
 
IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION ALGORITHM
IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION  ALGORITHM  IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION  ALGORITHM
IMPLEMENTATION OF DIFFERENT PATTERN RECOGNITION ALGORITHM
 
4267
42674267
4267
 
4267
42674267
4267
 

Mehr von akruthi k

Unit i-introduction
Unit i-introductionUnit i-introduction
Unit i-introductionakruthi k
 
Pattern matching programs
Pattern matching programsPattern matching programs
Pattern matching programsakruthi k
 
Physical layer overview
Physical layer overviewPhysical layer overview
Physical layer overviewakruthi k
 
802.11 mgt-opern
802.11 mgt-opern802.11 mgt-opern
802.11 mgt-opernakruthi k
 
Wired equivalent privacy (wep)
Wired equivalent privacy (wep)Wired equivalent privacy (wep)
Wired equivalent privacy (wep)akruthi k
 

Mehr von akruthi k (10)

Unit i-introduction
Unit i-introductionUnit i-introduction
Unit i-introduction
 
Pattern matching programs
Pattern matching programsPattern matching programs
Pattern matching programs
 
Boyer moore
Boyer mooreBoyer moore
Boyer moore
 
Physical layer overview
Physical layer overviewPhysical layer overview
Physical layer overview
 
Fhss
FhssFhss
Fhss
 
Dsss phy
Dsss phyDsss phy
Dsss phy
 
802.11 mgt-opern
802.11 mgt-opern802.11 mgt-opern
802.11 mgt-opern
 
802.11i
802.11i802.11i
802.11i
 
802.1x
802.1x802.1x
802.1x
 
Wired equivalent privacy (wep)
Wired equivalent privacy (wep)Wired equivalent privacy (wep)
Wired equivalent privacy (wep)
 

Kürzlich hochgeladen

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 

Kürzlich hochgeladen (20)

Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 

Kmp

  • 1. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula December 18, 2011 Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 2. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula outline Definition History Components of KMP Algorithm Example Run-Time Analysis Advantages and Disadvantages References Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 3. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Definition: Best known for linear time for exact matching. Compares from left to right. Shifts more than one position. Preprocessing approach of Pattern to avoid trivial comparisions. Avoids recomputing matches. Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 4. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula History: This algorithm was conceived by Donald Knuth and Vaughan Pratt and independently by James H.Morris in 1977. Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 5. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula History: Knuth, Morris and Pratt discovered first linear time string-matching algorithm by analysis of the naive algorithm. It keeps the information that naive approach wasted gathered during the scan of the text. By avoiding this waste of information, it achieves a running time of O(m + n). The implementation of Knuth-Morris-Pratt algorithm is efficient because it minimizes the total number of comparisons of the pattern against the input string. Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 6. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Components of KMP: The prefix-function : It preprocesses the pattern to find matches of prefixes of the pattern with the pattern itself. It is defined as the size of the largest prefix of P[0..j − 1] that is also a suffix of P[1..j]. It also indicates how much of the last comparison can be reused if it fails. It enables avoiding backtracking on the string ‘S’. Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 7. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula m ← length[p] a[1] ← 0 k ← 0 for q ← 2 to m do while k > 0 and p[k + 1] p[q] do k ← a[k] end while if p[k + 1] = p[q] then k ← k + 1 end if a[q] ← k end for return Here a = Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 8. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Computation of Prefix-function with example: Let us consider an example of how to compute for the pattern ‘p’. Pattern a b a b a c a I n i t i a l l y : m = length [ p]= 7 [1]= 0 k=0 where m, [1], and k are the length of the pattern, prefix function and initial potential value respectively. Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 9. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Step 1: q = 2 , k = 0 [2]= 0 q 1 2 3 4 5 6 7 p a b a b a c a 0 0 Step 2: q = 3 , k = 0 [3]= 1 q 1 2 3 4 5 6 7 p a b a b a c a 0 0 1 Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 10. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Step 3: q = 4 , k = 1 [4]= 2 q 1 2 3 4 5 6 7 p a b a b a c a 0 0 1 2 Step 4: q = 5 , k = 2 [5]= 3 q 1 2 3 4 5 6 7 p a b a b a c a 0 0 1 2 3 Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 11. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Step 5: q = 6 , k = 3 [6]= 1 q 1 2 3 4 5 6 7 p a b a b a c a 0 0 1 2 3 1 Step 6: q = 7 , k = 1 [7]= 1 q 1 2 3 4 5 6 7 p a b a b a c a 0 0 1 2 3 1 1 Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 12. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula After i t e r a t i n g 6 times , the p r e f i x function computations i s complete : q 1 2 3 4 5 6 7 p a b A b a c a 0 0 1 2 3 1 1 The running time of the prefix function is O(m). Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 13. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Algorithm Step 1: I n i t i a l i z e the input variables : n = Length of the Text . m = Length of the Pattern . = Prefix −function of pattern ( p ) . q = Number of characters matched . Step 2: Define the variable : q=0 , the beginning of the match . Step 3: Compare the f i r s t character of the pattern with f i r s t character of t e x t . I f match i s not found , s u b s t i t u t e the value of [ q ] to q . I f match i s found , then increment the value of q by 1. Step 4: Check whether a l l the pattern elements are matched with the t e x t elements . I f not , repeat the search process . I f yes , p r i n t the number of s h i f t s taken by the pattern . Step 5: look f o r the next match . Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 14. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula n ← length[S] m ← length[p] a ← Compute Prefix function q ← 0 for i ← 1 to n do while q > 0 and p[q + 1] S[i] do q ← a[q] if p[q + 1] = S[i] then q ← q + 1 end if if q == m then q ← a[q] end if end while end for Here a = Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 15. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Example of KMP algorithm: Now let us consider an example so that the algorithm can be clearly understood. String b a c b a b a b a b a c a a b Pattern a b a b a c a Let us execute the KMP algorithm to find whether ‘p’ occurs in ‘S’. Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 16. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula I n i t i a l l y : n = size of S = 15; m = size of p=7 Step 1: i = 1 , q = 0 comparing p [ 1 ] with S[ 1 ] String b a c b a b a b a b a c a a b Pattern a b a b a c a P[1] does not match with S[1]. ‘p’ will be shifted one position to the right. Step 2: i = 2 , q = 0 comparing p [ 1 ] with S[ 2 ] String b a c b a b a b a b a c a a b Pattern a b a b a c a Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 17. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Step 3: i = 3 , q = 1 comparing p [ 2 ] with S[ 3 ] p [ 2 ] does not match with S[ 3 ] String b a c b a b a b a b a c a a b Pattern a b a b a c a Backtracking on p , comparing p [ 1 ] and S[ 3 ] Step 4: i = 4 , q = 0 comparing p [ 1 ] with S[ 4 ] p [ 1 ] does not match with S[ 4 ] String b a c b a b a b a b a c a a b Pattern a b a b a c a Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 18. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Step 5: i = 5 , q = 0 comparing p [ 1 ] with S[ 5 ] String b a c b a b a b a b a c a a b Pattern a b a b a c a Step 6: i = 6 , q = 1 comparing p [ 2 ] with S[ 6 ] p [ 2 ] matches with S[ 6 ] String b a c b a b a b a b a c a a b Pattern a b a b a c a Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 19. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Step 7: i = 7 , q = 2 comparing p [ 3 ] with S[ 7 ] p [ 3 ] matches with S[ 7 ] String b a c b a b a b a b a c a a b Pattern a b a b a c a Step 8: i = 8 , q = 3 comparing p [ 4 ] with S[ 8 ] p [ 4 ] matches with S[ 8 ] String b a c b a b a b a b a c a a b Pattern a b a b a c a Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 20. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Step 9: i = 9 , q = 4 comparing p [ 5 ] with S[ 9 ] p [ 5 ] matches with S[ 9 ] String b a c b a b a b a b a c a a b Pattern a b a b a c a Step 10: i = 10 , q = 5 comparing p [ 6 ] with S[10] p [ 6 ] doesn ’ t matches with S[10] String b a c b a b a b a b a c a a b Pattern a b a b a c a Backtracking on p , comparing p [ 4 ] with S[10] because a f t e r mismatch q = [ 5 ] = 3 Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 21. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Step 11: i = 11 , q = 4 comparing p [ 5 ] with S[11] String b a c b a b a b a b a c a a b Pattern a b a b a c a Step 12: i = 12 , q = 5 comparing p [ 6 ] with S[12] p [ 6 ] matches with S[12] String b a c b a b a b a b a c a a b Pattern a b a b a c a Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 22. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Step 13: i = 13 , q = 6 comparing p [ 7 ] with S[13] p [ 7 ] matches with S[13] String b a c b a b a b a b a c a a b Pattern a b a b a c a pattern ‘p’ has been found to completely occur in string ‘S’. The total number of shifts that took place for the match to be found are: i − m = 13-7 = 6 shifts. Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 23. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Run-Time analysis: 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. Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 24. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Advantages and Disadvantages: Advantages: The running time of the KMP algorithm is optimal (O(m + n)), which is very fast. The algorithm never needs to move backwards in the input text T. It makes the algorithm good for processing very large files. Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 25. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Advantages and Disadvantages: Disadvantages: Doesn’t work so well as the size of the alphabets increases. By which more chances of mismatch occurs. Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm
  • 26. Knuth-Morris-Pratt Algorithm Kranthi Kumar Mandumula Graham A.Stephen, “String Searching Algorithms”, year = 1994. Donald Knuth, James H. Morris, Jr, Vaughan Pratt, “Fast pattern matching in strings”, year = 1977. Thomas H.Cormen; Charles E.Leiserson., Introduction to algorithms second edition , “The Knuth-Morris-Pratt Algorithm”, year = 2001. Kranthi Kumar Mandumula Knuth-Morris-Pratt Algorithm