Skip to content

32.4 The Knuth-Morris-Pratt algorithm

32.4-1

Compute the prefix function π\pi for the pattern ababbabbabbababbabb\text{ababbabbabbababbabb}.

π={0,0,1,2,0,1,2,0,1,2,0,1,2,3,4,5,6,7,8}.\pi = \{ 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]\pi^*[q] as a function of qq. Give an example to show that your bound is tight.

π[q]<q|\pi^*[q]| < q.

32.4-3

Explain how to determine the occurrences of pattern PP in the text TT by examining the π\pi function for the string PTPT (the string of length m+nm + n that is the concatenation of PP and TT).

{qπ[q]=m and q2m}\{ q \mid \pi[q] = m \text{ and } q \ge 2m \}.

32.4-4

Use an aggregate analysis to show that the running time of KMP-MATCHER\text{KMP-MATCHER} is Θ\Theta.

The number of q=q+1q = q + 1 is at most nn.

32.4-5

Use a potential function to show that the running time of KMP-MATCHER\text{KMP-MATCHER} is Θ(n)\Theta(n).

Φ=p.\Phi = p.

32.4-6

Show how to improve KMP-MATCHER\text{KMP-MATCHER} by replacing the occurrence of π\pi in line 7 (but not line 12) by π\pi', where π\pi' is defined recursively for q=1,2,,m1q = 1, 2, \ldots, m - 1 by the equation

π[q]={0 if π[q]=0,π[π[q]] if π[q]0 and P[π[q]+1]=P[q+1]π[q] if π[q]0 and P[π[q]+1]P[q+1]. \pi'[q] = \begin{cases} 0 & \text{ if } \pi[q] = 0, \\ \pi'[\pi[q]] & \text{ if } \pi[q] \ne 0 \text{ and } P[\pi[q] + 1] = P[q + 1] \\ \pi[q] & \text{ if } \pi[q] \ne 0 \text{ and } P[\pi[q] + 1] \ne P[q + 1]. \end{cases}

Explain why the modified algorithm is correct, and explain in what sense this change constitutes an improvement.

If P[q+1]T[i]P[q + 1] \ne T[i], then if P[π[q]+q]=P[q+1]T[i]P[\pi[q] + q] = P[q + 1] \ne T[i], there is no need to compare P[π[q]+q]P[\pi[q] + q] with T[i]T[i].

32.4-7

Give a linear-time algorithm to determine whether a text TT is a cyclic rotation of another string TT'. For example, arc\text{arc} and car\text{car} are cyclic rotations of each other.

Find TT' in TTTT.

32.4-8 \star

Give an O(mΣ)O(m|\Sigma|)-time algorithm for computing the transition function δ\delta for the string-matching automaton corresponding to a given pattern PP. (Hint: Prove that δ(q,a)=δ(π[q],a)\delta(q, a) = \delta(\pi[q], a) if q=mq = m or P[q+1]aP[q + 1] \ne a.)

Compute the prefix function mm times.