Professional Documents
Culture Documents
=
+ =
+ =
1 m
1 i
0 1 - m i
1 m
1 i
1 i i i
1 m
1 i
i
c
) (c
c
If u
m-1
> u
0
, then amortized cost upper bounds real cost.
We have u
0
= 0 (initial value of k)
u
m-1
> 0 (final value of k).
We show
i
= O(1).
Jim Anderson Comp 750, Fall 2009 String Matching - 43
Time Complexity (Continued)
The value of
i
obviously depends on how many times statement
6 is executed.
Note that k > t[k]. Thus, each execution of statement 6 decreases
k by at least 1.
So, suppose that statements 5..6 iterate several times, decreasing
the value of k.
We have: number of iterations s k
old
k
new
. Thus,
i
s O(1) + 2(k
old
k
new
) + u
i
u
i-1
Hence,
i
= O(1). Total cost is therefore O(m).
for statements
other than 5 & 6
= k
new
= k
old
Jim Anderson Comp 750, Fall 2009 String Matching - 44
Rest of the Algorithm
KMP(T, P)
n := length[T];
m := length[P];
t := Compute-t(P);
q := 0;
for i := 1 to n do
while q > 0 and P[q+1] = T[i] do
q := t[q]
od;
if P[q+1] = T[i] then
q := q + 1
fi;
if q = m then
print pattern occurs with shift i m;
q := t[q]
fi
od
Time complexity
of loop is O(n)
(similar to the
analysis of
Compute-t).
Total time is
O(m + n).
Jim Anderson Comp 750, Fall 2009 String Matching - 45
Example
i 1 2 3 4 5
P[i] a b a b c
t[i] 0 0 1 2 0
P = a b a b c
1 2 3 4 5 6 7 8 9 10
T = a b b a b a b a b c
Start of 1
st
loop: q = 0, i = 1 [a]
2
nd
loop: q = 1, i = 2 [b]
3
rd
loop: q = 2, i = 3 [b]
4
th
loop: q = 0, i = 4 [a]
5
th
loop: q = 1, i = 5 [b]
6
th
loop: q = 2, i = 6 [a]
7
th
loop: q = 3, i = 7 [b]
mismatch
detected
8
th
loop: q = 4, i = 8 [a]
9
th
loop: q = 3, i = 9 [b]
10
th
loop: q = 4, i = 10 [c]
Termination: q = 5
mismatch
detected
match
detected
Please see the book for formal correctness proofs.
(Theyre very tedious.)