Algorithm Search⌘K

Proofs: Sellers algorithm

These are the human proofs of Sellers algorithm, at full length.

Sellers' 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, and dd is the Levenshtein distance. For 0≤i≤m0 \le i \le m and 0≤j≤n0 \le j \le n let

E[i][j]=min⁡0≤h≤jd(P[0…i),T[h…j)).E[i][j] = \min_{0 \le h \le j} d(P[0 \ldots i), T[h \ldots j)).

The Wagner-Fischer proof shows that d(u,v)d(u, v) is the least cost A(u,v)A(u, v) of an alignment of uu and vv, where a column (a,b)(a, b) costs [a≠b][a \ne b] and the gap counts as a symbol. We use alignments below.

Lemma 1. E[0][j]=0E[0][j] = 0, E[i][0]=iE[i][0] = i, and for i,j≥1i, j \ge 1

E[i][j]=min⁡(E[i][j−1]+1,  E[i−1][j]+1,  E[i−1][j−1]+[P[i−1]≠T[j−1]]).E[i][j] = \min\bigl(E[i][j-1] + 1,\; E[i-1][j] + 1,\; E[i-1][j-1] + [P[i-1] \ne T[j-1]]\bigr).

Proof. For i=0i = 0, take h=jh = j: the empty strings have distance 0. For j=0j = 0, the only substring is empty, and d(P[0…i),ε)=id(P[0 \ldots i), \varepsilon) = i.

Let i,j≥1i, j \ge 1. Each term on the right is the cost of some alignment of P[0…i)P[0 \ldots i) with some T[h…j)T[h \ldots j), h≤jh \le j. For the first term, take an optimal pair (h,α)(h, \alpha) for E[i][j−1]E[i][j-1], with h≤j−1h \le j - 1, and add the column (−,T[j−1])(-, T[j-1]). This aligns P[0…i)P[0 \ldots i) with T[h…j)T[h \ldots j). For the second, take an optimal pair for E[i−1][j]E[i-1][j] and add the column (P[i−1],−)(P[i-1], -). For the third, take an optimal pair for E[i−1][j−1]E[i-1][j-1], with h≤j−1h \le j-1, and add (P[i−1],T[j−1])(P[i-1], T[j-1]). So E[i][j]E[i][j] is at most each term.

Conversely, let h≤jh \le j and let α\alpha be an optimal alignment of P[0…i)P[0 \ldots i) and T[h…j)T[h \ldots j). If h=jh = j, then α\alpha has the ii columns (P[t],−)(P[t], -) and costs ii, and E[i−1][j]+1≤(i−1)+1=iE[i-1][j] + 1 \le (i - 1) + 1 = i, because E[i−1][j]≤d(P[0…i−1),ε)=i−1E[i-1][j] \le d(P[0 \ldots i-1), \varepsilon) = i - 1. If h<jh < j, the last column of α\alpha is one of (−,T[j−1])(-, T[j-1]), (P[i−1],−)(P[i-1], -) and (P[i−1],T[j−1])(P[i-1], T[j-1]). Without it, α\alpha aligns P[0…i)P[0 \ldots i) with T[h…j−1)T[h \ldots j-1), or P[0…i−1)P[0 \ldots i-1) with T[h…j)T[h \ldots j), or P[0…i−1)P[0 \ldots i-1) with T[h…j−1)T[h \ldots j-1). In each case hh is allowed in the minimum of the smaller entry, so the cost of α\alpha is at least the matching term on the right. □\square

Lemma 2. At the test of line L9 for the column jj (and at line L2 for j=0j = 0), the array holds C[i]=E[i][j]C[i] = E[i][j] for all 0≤i≤m0 \le i \le m.

Proof. By induction on jj. Line L1 sets C[i]=i=E[i][0]C[i] = i = E[i][0]. Assume C[i]=E[i][j−1]C[i] = E[i][j-1] for all ii before the step for jj. Line L4 saves prev=C[0]=E[0][j−1]prev = C[0] = E[0][j-1] and sets C[0]=0=E[0][j]C[0] = 0 = E[0][j]. We claim that at the start of the step for row ii (line L5), C[0…i−1]C[0 \ldots i-1] holds column jj, C[i…m]C[i \ldots m] still holds column j−1j - 1, and prev=E[i−1][j−1]prev = E[i-1][j-1]. This holds for i=1i = 1. In the step, line L7 computes min⁡(C[i]+1,C[i−1]+1,prev+c)=min⁡(E[i][j−1]+1,E[i−1][j]+1,E[i−1][j−1]+c)\min(C[i] + 1, C[i-1] + 1, prev + c) = \min(E[i][j-1] + 1, E[i-1][j] + 1, E[i-1][j-1] + c), which is E[i][j]E[i][j] by Lemma 1. Line L8 sets prevprev to the old C[i]=E[i][j−1]C[i] = E[i][j-1] and stores E[i][j]E[i][j] in C[i]C[i]. So the claim holds for i+1i + 1. After row mm, the array is column jj. □\square

Theorem. SELLERS(P,T,k)(P, T, k) reports jj if and only if d(P,T[h…j))≤kd(P, T[h \ldots j)) \le k for some h≤jh \le j, and it reports each such jj once, in increasing order.

Proof. By Lemma 2, the test at line L2 or L9 for jj is E[m][j]≤kE[m][j] \le k, which is the condition by the definition of EE. The loop of line L3 visits each jj once, in increasing order. ■\blacksquare

Back to Sellers algorithm