Algorithm Search⌘K

Proofs: Ukkonen's cutoff algorithm

These are the human proofs of Ukkonen's cutoff algorithm, at full length.

Ukkonen's cut-off algorithm reports exactly the ends of the approximate matches full

This proof shows: correctness.

Indices are 0-based, ∣P∣=m|P| = m, ∣T∣=n|T| = n, k≥0k \ge 0. Let C[i][j]C[i][j] be the Sellers table, so C[0][j]=0C[0][j] = 0, C[i][0]=iC[i][0] = i, and for i,j≥1i, j \ge 1

C[i][j]=min⁡(C[i][j−1]+1,  C[i−1][j]+1,  C[i−1][j−1]+δij),δij=[P[i−1]≠T[j−1]],(R)C[i][j] = \min\bigl(C[i][j-1] + 1,\; C[i-1][j] + 1,\; C[i-1][j-1] + \delta_{ij}\bigr), \qquad \delta_{ij} = [P[i-1] \ne T[j-1]], \tag{R}

by the correctness proof of Sellers' algorithm. Let ℓj=max⁡{i:C[i][j]≤k}\ell_j = \max\{i : C[i][j] \le k\}, which exists since C[0][j]=0C[0][j] = 0.

Lemma 1. For j≥1j \ge 1: ℓj≤ℓj−1+1\ell_j \le \ell_{j-1} + 1.

Proof. Lemma 3 of the Landau-Vishkin proof gives C[i−1][j−1]≤C[i][j]C[i-1][j-1] \le C[i][j] for i,j≥1i, j \ge 1. If ℓj≥1\ell_j \ge 1, then C[ℓj−1][j−1]≤C[ℓj][j]≤kC[\ell_j - 1][j-1] \le C[\ell_j][j] \le k, so ℓj−1≥ℓj−1\ell_{j-1} \ge \ell_j - 1. If ℓj=0\ell_j = 0 the claim is trivial. □\square

A stored number ss is valid for a true value vv when s=vs = v if v≤kv \le k, and s>ks > k if v>kv > k.

Lemma 2. Let a,b,ca, b, c be valid for A,B,DA, B, D, and δ∈{0,1}\delta \in \{0, 1\}. Then min⁡(a+1,b+1,c+δ)\min(a + 1, b + 1, c + \delta) is valid for V=min⁡(A+1,B+1,D+δ)V = \min(A + 1, B + 1, D + \delta).

Proof. Each stored term is at least min⁡(true term,k+1)\min(\text{true term}, k + 1): a stored input equals the true one, or both are above kk, and then the stored term is above kk. If V≤kV \le k, some true term equals V≤kV \le k, so its input is at most kk and stored exactly, and the stored term equals VV. All other stored terms are at least min⁡(true term,k+1)≥V\min(\text{true term}, k + 1) \ge V. So the minimum is VV. If V>kV > k, every true term is above kk, so every stored term is above kk, and so is the minimum. □\square

Lemma 3 (invariant). After line L3, and after each pass of the loop of line L4 for column jj, the following hold. (a) lact=ℓjlact = \ell_j. (b) For 0≤i≤min⁡(ℓj+1,m)0 \le i \le \min(\ell_j + 1, m), the stored C[i]C[i] is valid for C[i][j]C[i][j].

Proof. Column 0: line L1 stores the true values C[i][0]=iC[i][0] = i, so (b) holds for all rows. The last row with i≤ki \le k is min⁡(k,m)\min(k, m), so line L2 gives (a).

Column j≥1j \ge 1, assuming (a) and (b) for column j−1j - 1. Line L5 sets top=min⁡(ℓj−1+1,m)top = \min(\ell_{j-1} + 1, m). Line L6 keeps C[0]=0C[0] = 0, which is the true value. By induction on i=1,…,topi = 1, \ldots, top, line L8 computes a value valid for C[i][j]C[i][j]. Its inputs are the stored C[i]C[i], still of column j−1j - 1 and valid by (b) since i≤topi \le top; the new C[i−1]C[i-1], valid by the induction on ii; and prevprev, the old C[i−1]C[i-1] of column j−1j - 1, valid by (b). Lemma 2 applied to (R) gives validity.

So rows 0 to toptop hold valid values for column jj. By Lemma 1, ℓj≤top\ell_j \le top. Every row ii with ℓj<i≤top\ell_j < i \le top has a true value above kk and so a stored value above kk, and row ℓj\ell_j has a stored value equal to its true value, at most kk. Hence the loop of line L9, which starts at toptop and stops at the first stored value at most kk, stops at ℓj\ell_j. This gives (a).

For (b): rows 0 to ℓj\ell_j are valid. If ℓj<m\ell_j < m, row ℓj+1\ell_j + 1 has a true value above kk by the definition of ℓj\ell_j, and line L11 stores k+1k + 1, which is valid. □\square

Theorem. UKKONEN-CUTOFF(P,T,k)(P, T, k) reports, in increasing order, exactly the positions j∈{0,…,n}j \in \{0, \ldots, n\} such that d(P,T[h…j))≤kd(P, T[h \ldots j)) \le k for some h≤jh \le j.

Proof. By the definition of the Sellers table, jj is such a position exactly when C[m][j]≤kC[m][j] \le k, that is, when ℓj=m\ell_j = m. By Lemma 3(a), line L3 for j=0j = 0 and line L10 for j≥1j \ge 1 report jj exactly when ℓj=m\ell_j = m. The columns are handled in increasing order of jj. ■\blacksquare

Back to Ukkonen's cutoff algorithm