Algorithm Search⌘K

Proofs: Two-way algorithm

These are the human proofs of Two-way algorithm, at full length.

The two-way algorithm outputs exactly the valid shifts full

This proof shows: correctness, termination.

Claim. For a text T[0…n−1]T[0 \ldots n-1] and a pattern P[0…m−1]P[0 \ldots m-1] over an ordered alphabet, TWO-WAY(T,P)(T, P) stops and outputs exactly the shifts ss, 0≤s≤n−m0 \le s \le n - m, with T[s…s+m−1]=PT[s \ldots s+m-1] = P, in increasing order.

For m=0m = 0, line L2 outputs every shift, so let m≥1m \ge 1. A shift s′s' is valid when 0≤s′≤n−m0 \le s' \le n - m and T[s′…s′+m−1]=PT[s' \ldots s'+m-1] = P. Write π=per(P)\pi = \mathrm{per}(P) for the smallest period of PP.

Definitions. For a cut cc with 0≤c≤m0 \le c \le m (between P[c−1]P[c-1] and P[c]P[c]), an integer r≥1r \ge 1 is a repetition length at cc when P[k]=P[k+r]P[k] = P[k + r] for every kk with c−r≤k<cc - r \le k < c, k≥0k \ge 0 and k+r<mk + r < m. This is the definition of a repetition ww of length rr (with ww and the left part suffixes of one another, ww and the right part prefixes of one another) written position by position. The local period at cc is the smallest repetition length; π\pi is always a repetition length, and the cut is critical when the local period equals π\pi.

Facts about the factorization (Crochemore and Perrin). Let (ℓ,p)(\ell, p) be the result of CRITICAL-FACTORIZATION. Then (F1) the cut c=ℓ+1c = \ell + 1 is critical; (F2) ℓ+1<π\ell + 1 < \pi; (F3) if P[0…ℓ]≠P[p…p+ℓ]P[0 \ldots \ell] \ne P[p \ldots p+\ell] then π>max⁡(ℓ+1,m−ℓ−1)\pi > \max(\ell + 1, m - \ell - 1). These are the critical factorization theorem for the cut before the later of the two maximal suffixes, and two lemmas of the same paper; the test test_critical_factorization checks all three on every word of length at most 7 over three letters. We also need:

(F4) if P[0…ℓ]=P[p…p+ℓ]P[0 \ldots \ell] = P[p \ldots p+\ell], then p=πp = \pi. Proof. pp is the period of the suffix P[ℓ+1…m−1]P[\ell+1 \ldots m-1], so P[k]=P[k+p]P[k] = P[k+p] for ℓ+1≤k<m−p\ell + 1 \le k < m - p, and the test gives it for 0≤k≤ℓ0 \le k \le \ell. So pp is a period of PP, p≥πp \ge \pi. Conversely π\pi is a period of the suffix, so p≤πp \le \pi. □\square

So in the branch of line L4, p=πp = \pi; in the branch of line L14, q=max⁡(ℓ+1,m−ℓ−1)+1≤πq = \max(\ell+1, m-\ell-1) + 1 \le \pi by (F3).

Lemma 1 (no occurrence close to a partial match). Let ℓ+1≤j≤m\ell + 1 \le j \le m, and suppose T[s+k]=P[k]T[s+k] = P[k] for ℓ+1≤k≤j−1\ell + 1 \le k \le j - 1. Let s+ds + d be valid with d≥1d \ge 1. (a) If j=mj = m, or if d≤j−ℓ−1d \le j - \ell - 1, then d≥πd \ge \pi. (b) If d≤j−ℓ−1d \le j - \ell - 1, then π\pi divides dd.

Proof. Let KK be the set of k≥0k \ge 0 with ℓ+1≤k+d≤j−1\ell + 1 \le k + d \le j - 1. For k∈Kk \in K, P[k]=T[s+d+k]P[k] = T[s+d+k] because s+ds + d is valid, and T[s+d+k]=P[k+d]T[s+d+k] = P[k+d] by the hypothesis; so P[k]=P[k+d]P[k] = P[k + d].

(a) The repetition condition for dd at c=ℓ+1c = \ell + 1 asks P[k]=P[k+d]P[k] = P[k+d] for k≥0k \ge 0 with ℓ+1−d≤k≤ℓ\ell + 1 - d \le k \le \ell and k+d≤m−1k + d \le m - 1. Such a kk has k+d≥ℓ+1k + d \ge \ell + 1, and k+d≤j−1k + d \le j - 1: if j=mj = m this is the condition k+d≤m−1k + d \le m - 1, and if d≤j−ℓ−1d \le j - \ell - 1 it follows from k≤ℓk \le \ell. So k∈Kk \in K and the condition holds. By (F1), d≥πd \ge \pi.

(b) Let et=d−tπe_t = d - t\pi for t=0,1,…t = 0, 1, \ldots while et≥0e_t \ge 0. We show by induction on tt that P[k]=P[k+et]P[k] = P[k + e_t] for every k∈Kk \in K. For t=0t = 0 this is shown above. For the step, let k∈Kk \in K and et+1≥0e_{t+1} \ge 0: then k+et+1≥0k + e_{t+1} \ge 0 and k+et≤k+d≤j−1≤m−1k + e_t \le k + d \le j - 1 \le m - 1, so the period π\pi gives P[k+et+1]=P[k+et]=P[k]P[k + e_{t+1}] = P[k + e_t] = P[k]. Now if et≥1e_t \ge 1, the argument of (a) with ete_t in place of dd applies: for k≥0k \ge 0 with ℓ+1−et≤k≤ℓ\ell + 1 - e_t \le k \le \ell, we have k+d≥k+et≥ℓ+1k + d \ge k + e_t \ge \ell + 1 and k+d≤ℓ+d≤j−1k + d \le \ell + d \le j - 1, so k∈Kk \in K and P[k]=P[k+et]P[k] = P[k+e_t]. So ete_t is a repetition length at the critical cut and et≥πe_t \ge \pi, which means et+1≥0e_{t+1} \ge 0. The sequence decreases by π\pi and stays nonnegative, and it can only stop at a value ete_t that is not ≥1\ge 1, so it reaches 0: π\pi divides dd. □\square

Lemma 2 (right-part mismatch). If the scan of line L8 or L17 stops at i<mi < m with T[s+i]≠P[i]T[s+i] \ne P[i], and T[s+k]=P[k]T[s+k] = P[k] for ℓ+1≤k≤i−1\ell + 1 \le k \le i - 1, then no shift s+ds + d with 1≤d<i−ℓ1 \le d < i - \ell is valid.

Proof. Let s+ds + d be valid with 1≤d≤i−ℓ−11 \le d \le i - \ell - 1. Lemma 1(b) with j=ij = i gives π∣d\pi \mid d. Then i−d≥ℓ+1≥0i - d \ge \ell + 1 \ge 0, so T[s+i]=P[i−d]=P[i]T[s+i] = P[i-d] = P[i] by the period π\pi, a contradiction. □\square

Lemma 3 (full right part). If T[s+k]=P[k]T[s+k] = P[k] for ℓ+1≤k≤m−1\ell + 1 \le k \le m - 1, then no shift s+ds + d with 1≤d<π1 \le d < \pi is valid.

Proof. Lemma 1(a) with j=mj = m. □\square

The memory. In the branch of line L4, the invariant is: at the start of each window ss, T[s+k]=P[k]T[s+k] = P[k] for 0≤k≤mem0 \le k \le \mathit{mem}. It holds with mem=−1\mathit{mem} = -1 at the start and after line L13. After line L12, the previous window ss matched at least the positions i+1,…,m−1i + 1, \ldots, m - 1 where i≤ℓi \le \ell is where the left scan stopped (or i≤memi \le \mathit{mem} and the window matched fully). The new window is s+πs + \pi and mem=m−π−1\mathit{mem} = m - \pi - 1; for 0≤k≤m−π−10 \le k \le m - \pi - 1, the position k+πk + \pi satisfies k+π≥π>ℓ≥ik + \pi \ge \pi > \ell \ge i, so T[s+π+k]=P[k+π]=P[k]T[s + \pi + k] = P[k + \pi] = P[k], using the period π\pi.

With this invariant, the scans of lines L8 and L10 skip only positions known to match, so in both branches: the right scan establishes T[s+k]=P[k]T[s+k] = P[k] for ℓ+1≤k≤i−1\ell + 1 \le k \le i - 1 and either i=mi = m or a mismatch at ii; and after a full right scan, the left scan ends with all of PP matched exactly when the window is valid (line L11 or L18 outputs ss exactly then).

Proof of the claim. At each window, ss is output exactly when it is valid. The shift is i−ℓ≥1i - \ell \ge 1 after a right mismatch, safe by Lemma 2; after a full right scan it is p=πp = \pi or q≤πq \le \pi, safe by Lemma 3, and at least 1. So ss increases at every window, no valid shift is skipped, and the output is in increasing order. Each scan moves ii monotonically within [−1,m][-1, m], so each window takes finite time, and there are at most n−m+1n - m + 1 windows. The procedure MAXIMAL-SUFFIX stops: line L25 strictly increases msms, which stays below mm, so it runs fewer than m+1m + 1 times; between two runs of line L25, each step of line L23 or L24 increases j+kj + k by exactly 1, and j+k<mj + k < m in the loop. ■\blacksquare

Back to Two-way algorithm