Algorithm Search⌘K

Proofs: Computing the string-matching automaton transition function

These are the human proofs of Computing the string-matching automaton transition function, at full length.

The lagging-state construction computes the transition function full

This proof shows: correctness, termination.

Claim. For a pattern P[0…m−1]P[0 \ldots m-1] over {0,…,σ−1}\{0, \ldots, \sigma-1\}, COMPUTE-TRANSITION-FUNCTION stops and returns δ\delta with δ[q][c]=σP(P[0…q−1] c)\delta[q][c] = \sigma_P(P[0 \ldots q-1]\,c) for all 0≤q≤m0 \le q \le m and all cc.

Here σP(w)\sigma_P(w) is the length of the longest prefix of PP that is a suffix of ww. We use the recursion proved on the page of the string-matching automaton: if r=σP(w)r = \sigma_P(w), then σP(wc)=σP(P[0…r−1] c)\sigma_P(wc) = \sigma_P(P[0 \ldots r-1]\,c).

Termination. All loops are bounded: mm iterations of line L4 with σ\sigma iterations of line L5 each.

Row 0. σP(c)\sigma_P(c) is 1 if c=P[0]c = P[0] and 0 otherwise, which is what lines L1 and L2 set. For m=0m = 0, σP\sigma_P is always 0 and line L1 sets row 0 to zeros.

Invariant. At the start of the iteration for qq (1≤q≤m1 \le q \le m):

(I) rows 0,…,q−10, \ldots, q-1 are correct, and X=σP(P[1…q−1])X = \sigma_P(P[1 \ldots q-1]).

For q=1q = 1, row 0 is correct and X=0=σPX = 0 = \sigma_P of the empty string.

Lemma 1. X<qX < q, so row XX is correct by (I).

Proof. X≤∣P[1…q−1]∣=q−1X \le |P[1 \ldots q-1]| = q - 1. □\square

Lemma 2. Let 1≤q≤m1 \le q \le m and let cc be a character, with c≠P[q]c \ne P[q] if q<mq < m. Then σP(P[0…q−1] c)=σP(P[1…q−1] c)\sigma_P(P[0 \ldots q-1]\,c) = \sigma_P(P[1 \ldots q-1]\,c).

Proof. Write w=P[0…q−1] cw = P[0 \ldots q-1]\,c and w′=P[1…q−1] cw' = P[1 \ldots q-1]\,c, the string ww without its first character. Let r=σP(w)≤min⁡(m,q+1)r = \sigma_P(w) \le \min(m, q + 1). If r=q+1r = q + 1, then q<mq < m and P[0…q]=wP[0 \ldots q] = w, so P[q]=cP[q] = c, which is excluded. So r≤q=∣w′∣r \le q = |w'|, and a suffix of ww of length at most ∣w′∣|w'| is a suffix of w′w'. Hence σP(w)≤σP(w′)\sigma_P(w) \le \sigma_P(w'). The other inequality holds because every suffix of w′w' is a suffix of ww. □\square

Step. For each cc handled by lines L5 and L6 and not overwritten by line L8, Lemma 2, the recursion with r=Xr = X and Lemma 1 give

δ[q][c]=δ[X][c]=σP(P[0…X−1] c)=σP(P[1…q−1] c)=σP(P[0…q−1] c).\delta[q][c] = \delta[X][c] = \sigma_P(P[0 \ldots X-1]\,c) = \sigma_P(P[1 \ldots q-1]\,c) = \sigma_P(P[0 \ldots q-1]\,c).

If q<mq < m, line L8 sets δ[q][P[q]]=q+1\delta[q][P[q]] = q + 1, which is correct because P[0…q]P[0 \ldots q] is a suffix of itself and no prefix of PP is longer than q+1q + 1 characters while ending P[0…q]P[0 \ldots q]. Line L9 sets XX to δ[X][P[q]]=σP(P[0…X−1] P[q])\delta[X][P[q]] = \sigma_P(P[0 \ldots X-1]\,P[q]), which by the recursion equals σP(P[1…q])\sigma_P(P[1 \ldots q]). Row XX read here was already correct before the iteration and is not changed by it, since X<qX < q. So (I) holds for q+1q + 1. At the end, (I) for q=m+1q = m + 1 is the claim. ■\blacksquare

Back to Computing the string-matching automaton transition function