I am reading about string algorithms in Cormen’s book “Introduction to Algorithms”. For Transition which is shown below.
My question: why are we doing min(m+1, q+2) and why are we incrementing m by 1 and q by 2.
Following link has back ground to above question.
http://people.scs.carleton.ca/~maheshwa/courses/5703COMP/Fall2009/StringMatching.pdf
Kindly help here with a simple example.
Algorithm Compute-Transition-Function(P, Sigma)
m = length(P);
for q = 0 through m do
for each character x in Sigma
k = min(m+1, q+2);
repeat k = k-1 // work backwards from q+1
until Pk 'is-suffix-of' Pqx;
d(q, x) = k; // assign transition table
end for;
end for;
return d;
End algorithm.
m + 1because in the nextrepeatloopkis decreased first.q + 2because in therepeatyou start then withq + 1so have at least 1 char.The following code might have a boundary problem (q == m is missing),
but wants to make the indexing a bit clearer.