Compute the prefix function π for the pattern ababbabbabbababbabb.
π={0,0,1,2,0,1,2,0,1,2,0,1,2,3,4,5,6,7,8}.
32.4-2
Give an upper bound on the size of π∗[q] as a function of q. Give an example to show that your bound is tight.
∣π∗[q]∣<q.
32.4-3
Explain how to determine the occurrences of pattern P in the text T by examining the π function for the string PT (the string of length m+n that is the concatenation of P and T).
{q∣π[q]=m and q≥2m}.
32.4-4
Use an aggregate analysis to show that the running time of KMP-MATCHER is Θ.
The number of q=q+1 is at most n.
32.4-5
Use a potential function to show that the running time of KMP-MATCHER is Θ(n).
Φ=p.
32.4-6
Show how to improve KMP-MATCHER by replacing the occurrence of π in line 7 (but not line 12) by π′, where π′ is defined recursively for q=1,2,…,m−1 by the equation
π′[q]=⎩⎨⎧0π′[π[q]]π[q] if π[q]=0, if π[q]=0 and P[π[q]+1]=P[q+1] if π[q]=0 and P[π[q]+1]=P[q+1].
Explain why the modified algorithm is correct, and explain in what sense this change constitutes an improvement.
If P[q+1]=T[i], then if P[π[q]+q]=P[q+1]=T[i], there is no need to compare P[π[q]+q] with T[i].
32.4-7
Give a linear-time algorithm to determine whether a text T is a cyclic rotation of another string T′. For example, arc and car are cyclic rotations of each other.
Find T′ in TT.
32.4-8 ⋆
Give an O(m∣Σ∣)-time algorithm for computing the transition function δ for the string-matching automaton corresponding to a given pattern P. (Hint: Prove that δ(q,a)=δ(π[q],a) if q=m or P[q+1]=a.)