I am struggling to grasp the algorithm for building the KMP failure function. The bulk of what is making my understanding incomplete concerns the line length=PI[length-1]. There is the psuedo code for the algorithm below. Here are my questions:
1.) How do we know that in the event s[i] != s[length], the best possible option for our candidate prefix length is PI[length-1]?
2.) If the candidate PI[length-1] fails, and on the next iteration of the while loop, we must go to PI[PI[length-1]-1] how do we know that the best possible candidate length is not actually BETWEEN PI[length-1] and PI[PI[length-1]-1]?
3.) When s[i]!=s[length] how do we know there does not exist a suffix(that matches a prefix) that ends at s[i] and that begins at a point in the string PRIOR to i-length?
I think my confusion could best be cleared by short informal proofs.
function f(string s):
PI = an array of integers with size equal to length of s
length=0
i=1
while i < the length of the s:
if s[length] == s[i]:
length+=1
PI[i]=length
i+=1
else:
if length!=0:
length=PI[length-1]
else:
PI[i]=0
i+=1
return PI
Thank you!
