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)
00000100 00001100
00000101 00001101 The cost of each INCREMENT operation is linear in number
00001110 of bits flipped
00000110
00001111 In the worst case 01111111 is incremented to 10000000 for
00000111 k = 8.
00010000
So, for n operations, we have O(n*k)
Consider a sequence of n INCREMENT
operations
A[0] flips every time • So total number of flips =
A[1] flips n/2 times
A[2] flips n/4 times