How can I give an efficient algorithm for computeing the transition function δ for the string-matching automaton in time O(m |Σ|), using π prefix function?
I want to compute the transition function in a finite automaton. Normal transition function has O(m^3|Σ|) complexity, where m = length of pattern P and Σ is the alphabet.
COMPUTE_TRANSITION_FUNCTION(P,Σ)
m = length(P);
for q = 0 through m do
for each character x in Σ
k = min(m+1, q+2); // +1 for x, +2 for subsequent repeat loop to decrement
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.
π is the prefix function defined in KMP algorithm
There is an O(m.|Σ|) algorithm and because the transaction function has O(m.|Σ|) possible input, there is no better algorithm due to the time complexity.
Assume we have computed π, and we want to calculate d(q, x). d(q, x) means in which state should we go, if we are currently in state q and the current character in the input is x. if the current character is P[q], we should go to state q + 1, because q+1 character is matched. so d(q, p[i]) = q + 1. Otherwise we have to go to a state with lower number. π[q] means the last state before q that P[0 .. π[q]] is a suffix of P[0 .. q]. so we copy the outputs of the state π[q] to the outputs of the state q except for the character p[i] which we have set previously.
I hope you understand it!