Algorithm Search⌘K

Proofs: Landau-Vishkin algorithm

These are the human proofs of Landau-Vishkin algorithm, at full length.

The Landau-Vishkin algorithm computes the furthest rows of the diagonals full

This proof shows: correctness.

Indices are 0-based, ∣P∣=m|P| = m, ∣T∣=n|T| = n, k≥0k \ge 0, and dd is the Levenshtein distance. Let C[i][j]C[i][j], for 0≤i≤m0 \le i \le m and 0≤j≤n0 \le j \le n, be the Sellers table. By the correctness proof of Sellers' algorithm, 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]+[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] + [P[i-1] \ne T[j-1]]\bigr). \tag{R}

Diagonal dd, for −m≤d≤n-m \le d \le n, is the set of cells (i,i+d)(i, i + d) with sd≤i≤tds_d \le i \le t_d, where sd=max⁡(0,−d)s_d = \max(0, -d) and td=min⁡(m,n−d)t_d = \min(m, n - d). Its first cell has the value C[sd][sd+d]=sdC[s_d][s_d + d] = s_d by the boundary values.

Lemma 1 (columns). For 1≤i≤m1 \le i \le m and 0≤j≤n0 \le j \le n: C[i−1][j]−1≤C[i][j]≤C[i−1][j]+1C[i-1][j] - 1 \le C[i][j] \le C[i-1][j] + 1.

Proof. For j=0j = 0 the values are i−1i - 1 and ii. Let j≥1j \ge 1. The right inequality is a term of (R). For the left one, induct on jj. By (R), C[i][j]C[i][j] is one of three terms, and we show each term is at least C[i−1][j]−1C[i-1][j] - 1. The term C[i−1][j]+1C[i-1][j] + 1 is. The term C[i−1][j−1]+cC[i-1][j-1] + c, with c∈{0,1}c \in \{0, 1\}, is at least C[i−1][j]−1C[i-1][j] - 1, because (R) for row i−1i - 1 gives C[i−1][j]≤C[i−1][j−1]+1C[i-1][j] \le C[i-1][j-1] + 1 when i≥2i \ge 2, and C[0][j]=0≤C[0][j−1]+1C[0][j] = 0 \le C[0][j-1] + 1 when i=1i = 1. The term C[i][j−1]+1C[i][j-1] + 1 is at least C[i−1][j−1]−1+1=C[i−1][j−1]C[i-1][j-1] - 1 + 1 = C[i-1][j-1] by the induction hypothesis for column j−1j - 1, and C[i−1][j−1]≥C[i−1][j]−1C[i-1][j-1] \ge C[i-1][j] - 1 as just shown. □\square

Lemma 2 (rows). For 0≤i≤m0 \le i \le m and 1≤j≤n1 \le j \le n: C[i][j−1]−1≤C[i][j]≤C[i][j−1]+1C[i][j-1] - 1 \le C[i][j] \le C[i][j-1] + 1.

Proof. For i=0i = 0 both values are 0. Let i≥1i \ge 1. The right inequality is a term of (R). For the left one, induct on ii, with the claim for row i−1i - 1 known. The term C[i][j−1]+1C[i][j-1] + 1 is at least C[i][j−1]−1C[i][j-1] - 1. The term C[i−1][j]+1C[i-1][j] + 1 is at least C[i−1][j−1]−1+1=C[i−1][j−1]C[i-1][j-1] - 1 + 1 = C[i-1][j-1] by the hypothesis for row i−1i - 1, and C[i−1][j−1]≥C[i][j−1]−1C[i-1][j-1] \ge C[i][j-1] - 1 by Lemma 1. The term C[i−1][j−1]+cC[i-1][j-1] + c is at least C[i−1][j−1]≥C[i][j−1]−1C[i-1][j-1] \ge C[i][j-1] - 1 by Lemma 1. □\square

Lemma 3 (diagonals). For 1≤i≤m1 \le i \le m and 1≤j≤n1 \le j \le n: C[i−1][j−1]≤C[i][j]≤C[i−1][j−1]+1C[i-1][j-1] \le C[i][j] \le C[i-1][j-1] + 1, and C[i][j]=C[i−1][j−1]C[i][j] = C[i-1][j-1] when P[i−1]=T[j−1]P[i-1] = T[j-1].

Proof. The right inequality is the third term of (R) with c≤1c \le 1. For the left one, each term of (R) is at least C[i−1][j−1]C[i-1][j-1]: the first because C[i][j−1]+1≥C[i−1][j−1]C[i][j-1] + 1 \ge C[i-1][j-1] by Lemma 1, the second because C[i−1][j]+1≥C[i−1][j−1]C[i-1][j] + 1 \ge C[i-1][j-1] by Lemma 2, and the third because c≥0c \ge 0. When P[i−1]=T[j−1]P[i-1] = T[j-1], the third term is C[i−1][j−1]C[i-1][j-1], so C[i][j]≤C[i−1][j−1]C[i][j] \le C[i-1][j-1], and equality follows. □\square

By Lemma 3 the values along a diagonal never decrease. So for e≥0e \ge 0 the set {i:sd≤i≤td, C[i][i+d]≤e}\{i : s_d \le i \le t_d,\ C[i][i + d] \le e\} is an initial run sd,…,Ls_d, \ldots, L of the diagonal, or empty. Define L[d][e]L[d][e] as its largest element, and L[d][e]=−∞L[d][e] = -\infty when it is empty. Since the first cell has the value sds_d, the set is empty exactly when e<sde < s_d.

Lemma 4. Line L7 sets L[d][e]L[d][e] to the true value, for every ee with 0≤e≤k0 \le e \le k and every dd with max⁡(−e,−m)≤d≤n\max(-e, -m) \le d \le n. For d<max⁡(−e,−m)d < \max(-e, -m) the true value is −∞-\infty.

Proof. The last claim holds because d<−ed < -e gives sd=−d>es_d = -d > e, and diagonals d<−md < -m have no cells. The terms of line L5 that refer to such diagonals, or to e−1=−1e - 1 = -1, are −∞-\infty in the program and in the truth. Induct on ee, and assume that the stored values of row e−1e - 1 are true (no assumption for e=0e = 0). Fix dd with max⁡(−e,−m)≤d≤n\max(-e, -m) \le d \le n, so sd≤es_d \le e and sd≤tds_d \le t_d. Let λ\lambda be the true L[d][e]L[d][e]; it exists since sd≤es_d \le e. Let r0r_0 be the value of rr after line L6.

Step 1: the cell (r0,r0+d)(r_0, r_0 + d) lies on diagonal dd and has a value at most ee. Line L6 gives r0≤tdr_0 \le t_d, and line L4 gives r0≥sdr_0 \ge s_d, since sd≤tds_d \le t_d. Because the values on the diagonal never decrease, it suffices to find a cell of diagonal dd with a row at least r0r_0 and a value at most ee. Let r0′r_0' be the value of rr after line L5, so r0=min⁡(r0′,td)r_0 = \min(r_0', t_d). One term gives r0′r_0'.

  • The term sds_d: the first cell has the value sd≤es_d \le e.
  • The term L[d][e−1]+1=i+1L[d][e-1] + 1 = i + 1, with C[i][i+d]≤e−1C[i][i + d] \le e - 1. If i<tdi < t_d, the cell (i+1,i+1+d)(i + 1, i + 1 + d) has a value at most ee by Lemma 3. If i=tdi = t_d, the cell (td,td+d)(t_d, t_d + d) has a value at most e−1e - 1, and r0=tdr_0 = t_d.
  • The term L[d−1][e−1]=iL[d-1][e-1] = i, with C[i][i+d−1]≤e−1C[i][i + d - 1] \le e - 1. If i+d≤ni + d \le n, the cell (i,i+d)(i, i + d) lies on diagonal dd, since i≤mi \le m, and its value is at most ee by Lemma 2. Else i+d−1=ni + d - 1 = n, so i≥1i \ge 1 and i−1=n−d=tdi - 1 = n - d = t_d (as i−1<mi - 1 < m). The cell (i−1,n)(i - 1, n) has a value at most ee by Lemma 1, and r0=td=i−1r_0 = t_d = i - 1.
  • The term L[d+1][e−1]+1=i+1L[d+1][e-1] + 1 = i + 1, with C[i][i+d+1]≤e−1C[i][i + d + 1] \le e - 1. If i<mi < m, the cell (i+1,i+1+d)(i + 1, i + 1 + d) lies on diagonal dd and has a value at most ee by Lemma 1. Else i=mi = m, and the cell (m,m+d)(m, m + d) lies on diagonal dd with a value at most ee by Lemma 2, and r0=td=mr_0 = t_d = m.

Step 2: L[d][e]≤λL[d][e] \le \lambda. Line L7 adds q=LCE(r0,r0+d)q = \mathrm{LCE}(r_0, r_0 + d). For 0≤u<q0 \le u < q we have P[r0+u]=T[r0+d+u]P[r_0 + u] = T[r_0 + d + u], so by Lemma 3 the cells (r0+u+1,r0+d+u+1)(r_0 + u + 1, r_0 + d + u + 1) all have the value of the cell (r0,r0+d)(r_0, r_0 + d), which is at most ee by Step 1. They lie on diagonal dd because the LCE stops at the end of PP or TT. So the row r0+qr_0 + q is in the run, and r0+q≤λr_0 + q \le \lambda.

Step 3: L[d][e]≥λL[d][e] \ge \lambda. Let μ\mu be the least row with sd≤μ≤λs_d \le \mu \le \lambda such that P[u]=T[u+d]P[u] = T[u + d] for all uu with μ≤u<λ\mu \le u < \lambda. It is enough to show r0≥μr_0 \ge \mu: then μ≤r0≤λ\mu \le r_0 \le \lambda by Step 2, all characters from row r0r_0 to row λ\lambda are equal, and the LCE reaches at least λ\lambda.

If μ=sd\mu = s_d, line L4 gives r0′≥sdr_0' \ge s_d, and td≥λ≥μt_d \ge \lambda \ge \mu gives r0≥μr_0 \ge \mu. Else μ>sd\mu > s_d, so μ≥1\mu \ge 1 and μ+d≥1\mu + d \ge 1, and P[μ−1]≠T[μ+d−1]P[\mu - 1] \ne T[\mu + d - 1] by the choice of μ\mu. The value v=C[μ][μ+d]v = C[\mu][\mu + d] is at most ee, because μ≤λ\mu \le \lambda. By (R) with a mismatch, vv is one plus one of C[μ−1][μ+d−1]C[\mu-1][\mu+d-1], C[μ][μ+d−1]C[\mu][\mu+d-1] and C[μ−1][μ+d]C[\mu-1][\mu+d], so v≥1v \ge 1, e≥1e \ge 1, and that cell has a value at most e−1e - 1.

  • If C[μ−1][μ+d−1]≤e−1C[\mu-1][\mu+d-1] \le e - 1, the cell is on diagonal dd, so the true L[d][e−1]≥μ−1L[d][e-1] \ge \mu - 1, and the first term of line L5 is at least μ\mu.
  • If C[μ][μ+d−1]≤e−1C[\mu][\mu+d-1] \le e - 1, the cell is on diagonal d−1d - 1, so L[d−1][e−1]≥μL[d-1][e-1] \ge \mu.
  • If C[μ−1][μ+d]≤e−1C[\mu-1][\mu+d] \le e - 1, the cell is on diagonal d+1d + 1, so L[d+1][e−1]+1≥μL[d+1][e-1] + 1 \ge \mu.

By the induction hypothesis the stored values of row e−1e - 1 are the true ones, so r0′≥μr_0' \ge \mu, and r0=min⁡(r0′,td)≥μr_0 = \min(r_0', t_d) \ge \mu since td≥λ≥μt_d \ge \lambda \ge \mu. □\square

Theorem. LANDAU-VISHKIN(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. Such a jj is a match exactly when C[m][j]≤kC[m][j] \le k, by the definition of the Sellers table. The cell (m,j)(m, j) lies on diagonal d=j−md = j - m, with −m≤d≤n−m-m \le d \le n - m, and it is the last cell of that diagonal, since td=mt_d = m there. Because the values on the diagonal never decrease, C[m][m+d]≤kC[m][m + d] \le k holds exactly when L[d][k]=mL[d][k] = m. For d<−kd < -k we have L[d][k]=−∞≠mL[d][k] = -\infty \ne m. By Lemma 4 with e=ke = k, the stored values are the true ones, so line L8 reports exactly the matches. It reports them in increasing order of dd, which is increasing order of j=m+dj = m + d. ■\blacksquare

Back to Landau-Vishkin algorithm