this is the pseudo code I've see in ItoA:
1 m = P.length
2 let pi[1...m] be a new array
3 pi[1] = 0
4 k=0
5 for q=2 to m
6 while k > 0 and P[k+1] != P[q]
7 k = pi[k]
8 if P[k+1] == P[q]
9 k = k+1
10 pi[q] = k
11 return pi
my doubt is why on line 6 we do k = pi[k]
instead of k--
which seems to me that should be the way of checking a preffix of length k
(because if P[k+1] != P[q]
it means that a preffix of lenght k+1
that's also suffix cannot be achieved) which can be also suffix, that is comparing with P[q]
, I also think that if we do it, the running time will stay the same.
The recursive call
k = pi[k]
searches for the smaller border ofp[1..q]
; because thepi[k]
, which is the largest border ofP[0..k]
, is smaller border ofp[1..q]
thanpi[q]
. It will search until it finds a border ofp[1..q]
where the next characterp[k+1]
is not equal toP[q+1]
.You can find more details here: http://www.inf.fh-flensburg.de/lang/algorithmen/pattern/kmpen.htm