Professional Documents
Culture Documents
Lecture 11 Knut H Morris Pratt
Lecture 11 Knut H Morris Pratt
Algorithm
Mansi A. Radke
What does it do?
• It finds the occurrences of a given input string s1 in the text/string s2
and at what offsets. i.e. it checks if s1 is a substring of s2 and at what
offset.
• Brute force algorithm (goes back and forth) and takes time
complexity of the order of (m*n)
ababaca
0012301
abacab
001012
ababaca
Lps array is
ababaca
0012301
Text is
bacbababaabcbab
Note:
If number of symbols is 5 O(m + n)
If number of symbols is 50 O(m + n)
If number of symbols is 500 O(m + n)
KMP O(m+n)
Knuth Morris Pratt Algorithm for string
matching
• Linear algorithm
• Complexity O(m+n)
• Never backtracks on the input text string T
• Makes use of prefix function
• The compute prefix function calculates the overlap of pattern string
with itself.
• The KMP matcher calculates the overlap between the text string and
the pattern string.
• 100 minutes?
• What when the tea pot is full? It can serve like 20 people!
• Accounting method
• Potential method
Aggregate analysis
• We determine an upper bound on the total cost of sequence of n
operations. The average cost per operation is then T(n)/n
• If you start with an empty stack, and you call MULTIPOP n times, it will
empty the stack in the first call and rest of the calls will be O(1)
• If pop, multipop and push all are combined, total cost would be O(n)