0% found this document useful (0 votes)
20 views6 pages

Knuth Morris Pratt Algorithms - Notes

The document explains the Knuth-Morris-Pratt (KMP) algorithm, focusing on the prefix function for a pattern, which identifies the longest prefix that is also a proper suffix. It provides an example of computing the prefix function for the pattern 'ababababca' and discusses the time complexity of the prefix function and KMP matcher. Additionally, it includes examination questions related to string matching algorithms, including KMP and Naïve string matcher.

Uploaded by

Sseiepr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Knuth Morris Pratt Algorithms - Notes

The document explains the Knuth-Morris-Pratt (KMP) algorithm, focusing on the prefix function for a pattern, which identifies the longest prefix that is also a proper suffix. It provides an example of computing the prefix function for the pattern 'ababababca' and discusses the time complexity of the prefix function and KMP matcher. Additionally, it includes examination questions related to string matching algorithms, including KMP and Naïve string matcher.

Uploaded by

Sseiepr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Knuth-Morris-Pratt(KMP) algorithm

Prefix function for a pattern


Given a pattern P[1..m], the prefix function for the
pattern P is the function π : {1,2,3,…..,m} 
{0,1,2,……,m-1} such that
π(q) = max{ k ! K <q and Pk ⊐ Pq}
π(q) is the length of the longest prefix of P that is a
proper suffix of Pq.
Knuth-Morris-Pratt(KMP) algorithm

Example: Compute the prefix function of the


pattern
P = ababababca
Solution
: 1 2 3 4 5 6 7 8 9 10
i a b a b a b a b c a
P(i) 0 0 1 2 3 4 5 6 0 1
π(i)
Knuth-Morris-Pratt(KMP) algorithm
Knuth-Morris-Pratt(KMP) algorithm
Knuth-Morris-Pratt(KMP) algorithm
Time complexity
Running time of compute-prefix-function is θ(m).
The matching time of KMP-Matcher is θ(n).

Question: Consider text and pattern as following:-


T = bacbababaabcbab
P = aba
Find all valid shifts using KMP algo.

Question: Compute the prefix function for the pattern


ababbabbabbababbabb.
AKTU Examination Questions
1. Write an algorithm for Naïve string matcher.
2. Write KMP algorithm for string matching. Perform the KMP
algorithm to search the occurrences of the pattern abaab in the
text string abbabaabaabab.
3. Write Rabin Karp string matching algorithm. Working modulo
q=11, how many spurious hits does the Rabin karp matcher in
the text T= 3141592653589793, when looking for the pattern
P=26.
4. Explain and Write the Knuth-Morris-Pratt algorithm for pattern
matching also write its time complexity.
5. Describe in detail Knuth-Morris-Pratt string matching
algorithm. Compute the prefix function 𝜋 for the pattern
ababbabbabbababbabb when the alphabet is Σ = {a,b}.
6. Compute the prefix function π for the pattern P= a b a c a b using
KNUTHMORRIS –PR

You might also like