The Landau-Vishkin algorithm computes the furthest rows of the diagonals full
This proof shows: correctness.
Indices are 0-based, ∣P∣=m, ∣T∣=n, k≥0, and d is the Levenshtein distance. Let C[i][j], for 0≤i≤m and 0≤j≤n, be the Sellers table. By the correctness proof of Sellers' algorithm, C[0][j]=0, C[i][0]=i, and for i,j≥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) Diagonal d, for −m≤d≤n, is the set of cells (i,i+d) with sd≤i≤td, where sd=max(0,−d) and td=min(m,n−d). Its first cell has the value C[sd][sd+d]=sd by the boundary values.
Lemma 1 (columns). For 1≤i≤m and 0≤j≤n: C[i−1][j]−1≤C[i][j]≤C[i−1][j]+1.
Proof. For j=0 the values are i−1 and i. Let j≥1. The right inequality is a term of (R). For the left one, induct on j. By (R), C[i][j] is one of three terms, and we show each term is at least C[i−1][j]−1. The term C[i−1][j]+1 is. The term C[i−1][j−1]+c, with c∈{0,1}, is at least C[i−1][j]−1, because (R) for row i−1 gives C[i−1][j]≤C[i−1][j−1]+1 when i≥2, and C[0][j]=0≤C[0][j−1]+1 when i=1. The term C[i][j−1]+1 is at least C[i−1][j−1]−1+1=C[i−1][j−1] by the induction hypothesis for column j−1, and C[i−1][j−1]≥C[i−1][j]−1 as just shown. □
Lemma 2 (rows). For 0≤i≤m and 1≤j≤n: C[i][j−1]−1≤C[i][j]≤C[i][j−1]+1.
Proof. For i=0 both values are 0. Let i≥1. The right inequality is a term of (R). For the left one, induct on i, with the claim for row i−1 known. The term C[i][j−1]+1 is at least C[i][j−1]−1. The term C[i−1][j]+1 is at least C[i−1][j−1]−1+1=C[i−1][j−1] by the hypothesis for row i−1, and C[i−1][j−1]≥C[i][j−1]−1 by Lemma 1. The term C[i−1][j−1]+c is at least C[i−1][j−1]≥C[i][j−1]−1 by Lemma 1. □
Lemma 3 (diagonals). For 1≤i≤m and 1≤j≤n: C[i−1][j−1]≤C[i][j]≤C[i−1][j−1]+1, and C[i][j]=C[i−1][j−1] when P[i−1]=T[j−1].
Proof. The right inequality is the third term of (R) with c≤1. For the left one, each term of (R) is at least C[i−1][j−1]: the first because C[i][j−1]+1≥C[i−1][j−1] by Lemma 1, the second because C[i−1][j]+1≥C[i−1][j−1] by Lemma 2, and the third because c≥0. When P[i−1]=T[j−1], the third term is C[i−1][j−1], so C[i][j]≤C[i−1][j−1], and equality follows. □
By Lemma 3 the values along a diagonal never decrease. So for e≥0 the set {i:sd≤i≤td, C[i][i+d]≤e} is an initial run sd,…,L of the diagonal, or empty. Define L[d][e] as its largest element, and L[d][e]=−∞ when it is empty. Since the first cell has the value sd, the set is empty exactly when e<sd.
Lemma 4. Line L7 sets L[d][e] to the true value, for every e with 0≤e≤k and every d with max(−e,−m)≤d≤n. For d<max(−e,−m) the true value is −∞.
Proof. The last claim holds because d<−e gives sd=−d>e, and diagonals d<−m have no cells. The terms of line L5 that refer to such diagonals, or to e−1=−1, are −∞ in the program and in the truth. Induct on e, and assume that the stored values of row e−1 are true (no assumption for e=0). Fix d with max(−e,−m)≤d≤n, so sd≤e and sd≤td. Let λ be the true L[d][e]; it exists since sd≤e. Let r0 be the value of r after line L6.
Step 1: the cell (r0,r0+d) lies on diagonal d and has a value at most e. Line L6 gives r0≤td, and line L4 gives r0≥sd, since sd≤td. Because the values on the diagonal never decrease, it suffices to find a cell of diagonal d with a row at least r0 and a value at most e. Let r0′ be the value of r after line L5, so r0=min(r0′,td). One term gives r0′.
- The term sd: the first cell has the value sd≤e.
- The term L[d][e−1]+1=i+1, with C[i][i+d]≤e−1. If i<td, the cell (i+1,i+1+d) has a value at most e by Lemma 3. If i=td, the cell (td,td+d) has a value at most e−1, and r0=td.
- The term L[d−1][e−1]=i, with C[i][i+d−1]≤e−1. If i+d≤n, the cell (i,i+d) lies on diagonal d, since i≤m, and its value is at most e by Lemma 2. Else i+d−1=n, so i≥1 and i−1=n−d=td (as i−1<m). The cell (i−1,n) has a value at most e by Lemma 1, and r0=td=i−1.
- The term L[d+1][e−1]+1=i+1, with C[i][i+d+1]≤e−1. If i<m, the cell (i+1,i+1+d) lies on diagonal d and has a value at most e by Lemma 1. Else i=m, and the cell (m,m+d) lies on diagonal d with a value at most e by Lemma 2, and r0=td=m.
Step 2: L[d][e]≤λ. Line L7 adds q=LCE(r0,r0+d). For 0≤u<q we have P[r0+u]=T[r0+d+u], so by Lemma 3 the cells (r0+u+1,r0+d+u+1) all have the value of the cell (r0,r0+d), which is at most e by Step 1. They lie on diagonal d because the LCE stops at the end of P or T. So the row r0+q is in the run, and r0+q≤λ.
Step 3: L[d][e]≥λ. Let μ be the least row with sd≤μ≤λ such that P[u]=T[u+d] for all u with μ≤u<λ. It is enough to show r0≥μ: then μ≤r0≤λ by Step 2, all characters from row r0 to row λ are equal, and the LCE reaches at least λ.
If μ=sd, line L4 gives r0′≥sd, and td≥λ≥μ gives r0≥μ. Else μ>sd, so μ≥1 and μ+d≥1, and P[μ−1]=T[μ+d−1] by the choice of μ. The value v=C[μ][μ+d] is at most e, because μ≤λ. By (R) with a mismatch, v is one plus one of C[μ−1][μ+d−1], C[μ][μ+d−1] and C[μ−1][μ+d], so v≥1, e≥1, and that cell has a value at most e−1.
- If C[μ−1][μ+d−1]≤e−1, the cell is on diagonal d, so the true L[d][e−1]≥μ−1, and the first term of line L5 is at least μ.
- If C[μ][μ+d−1]≤e−1, the cell is on diagonal d−1, so L[d−1][e−1]≥μ.
- If C[μ−1][μ+d]≤e−1, the cell is on diagonal d+1, so L[d+1][e−1]+1≥μ.
By the induction hypothesis the stored values of row e−1 are the true ones, so r0′≥μ, and r0=min(r0′,td)≥μ since td≥λ≥μ. □
Theorem. LANDAU-VISHKIN(P,T,k) reports, in increasing order, exactly the positions j∈{0,…,n} such that d(P,T[h…j))≤k for some h≤j.
Proof. Such a j is a match exactly when C[m][j]≤k, by the definition of the Sellers table. The cell (m,j) lies on diagonal d=j−m, with −m≤d≤n−m, and it is the last cell of that diagonal, since td=m there. Because the values on the diagonal never decrease, C[m][m+d]≤k holds exactly when L[d][k]=m. For d<−k we have L[d][k]=−∞=m. By Lemma 4 with e=k, the stored values are the true ones, so line L8 reports exactly the matches. It reports them in increasing order of d, which is increasing order of j=m+d. ■