Myers' bit vectors hold the vertical differences of the Sellers table full
This proof shows: correctness.
Indices are 0-based, ∣P∣=m≥1, ∣T∣=n, k≥0. Let C[i][j] be the Sellers table, so C[0][j]=0, C[i][0]=i, and for i,j≥1
C[i][j]=min(C[i−1][j−1]+δij,C[i][j−1]+1,C[i−1][j]+1),δij=[P[i−1]=T[j−1]], and C[m][j]=min0≤h≤jed(P,T[h..j)), by the correctness proof of Sellers' algorithm. So the end positions to report are the j with C[m][j]≤k. For 1≤i≤m define the vertical difference Δvi(j)=C[i][j]−C[i−1][j], and for 0≤i≤m, j≥1, the horizontal difference Δhi(j)=C[i][j]−C[i][j−1]. Then Δh0(j)=0.
The claim is: after column j is processed (line L2 for j=0, line L10 for j≥1), bit i−1 of Pv is set exactly when Δvi(j)=1, bit i−1 of Mv is set exactly when Δvi(j)=−1, all Δvi(j) lie in {−1,0,1}, and score=C[m][j]. The claim gives the output, because line L11 reports j exactly when C[m][j]≤k.
Column 0. C[i][0]−C[i−1][0]=1 for all i, so Pv=1m, Mv=0, and score=m=C[m][0], as line L2 sets them.
From now on fix j≥1 and assume the claim for column j−1. Write vi=Δvi(j−1), hi=Δhi(j), Eqi=1−δij, and di=C[i][j]−C[i−1][j−1] for 1≤i≤m.
Lemma 1 (one cell). Let 1≤i≤m and hi−1∈{−1,0,1}. Then
- di=min(δij,vi+1,hi−1+1)∈{0,1}, and di=0 exactly when Eqi=1 or vi=−1 or hi−1=−1;
- hi=di−vi and Δvi(j)=di−hi−1, and both lie in {−1,0,1}.
Proof. C[i][j−1]=C[i−1][j−1]+vi and C[i−1][j]=C[i−1][j−1]+hi−1. Subtract C[i−1][j−1] from the recurrence: di=min(δij,vi+1,hi−1+1). Each term is at least 0, and δij≤1, so di∈{0,1}, and di=0 exactly when one term is 0. Next, hi=C[i][j]−C[i][j−1]=di−vi and Δvi(j)=C[i][j]−C[i−1][j]=di−hi−1. Then hi≥−1 because di≥0 and vi≤1, and hi≤1 because di≤vi+1. The same bounds with hi−1 in place of vi give Δvi(j)∈{−1,0,1}. □
Since h0=0, induction on i with Lemma 1 gives hi∈{−1,0,1} for all i, so Lemma 1 holds for all 1≤i≤m and all differences in column j lie in {−1,0,1}. Write Di=[di=0], Phi=[hi=1], Mhi=[hi=−1], Pvi=[vi=1], Mvi=[vi=−1], and Ph0=Mh0=0.
Lemma 2 (cell logic). For 1≤i≤m:
- Di=Eqi∨Mvi∨Mhi−1;
- Phi=Mvi∨¬(Di∨Pvi) and Mhi=Pvi∧Di;
- [Δvi(j)=1]=Mhi−1∨¬(Di∨Phi−1) and [Δvi(j)=−1]=Phi−1∧Di.
Proof. Part 1 is Lemma 1, part 1. For part 2, hi=di−vi with di∈{0,1}. By part 1, Mvi implies Di. So hi=1 holds when vi=−1 (then di=0), or when vi=0 and di=1, which is ¬Di∧¬Pvi given ¬Mvi. No other pair gives 1. And hi=−1 only for di=0, vi=1. Part 3 is the same argument for Δvi(j)=di−hi−1, with Mhi−1⇒Di from part 1. □
Lemma 3 (the carry chain). Let Xi=Eqi∨Mvi. For 1≤i≤m, Di=1 exactly when Xi=1, or there is an r<i with Eqr=1 and Pvq=1 for all r≤q≤i−1.
Proof. By Lemma 2, Mhi−1=Pvi−1∧Di−1 for i≥2, and Mh0=0. So D1=X1 and Di=Xi∨(Pvi−1∧Di−1). Induction on i gives: Di=1 exactly when there is an r≤i with Xr=1 and Pvq=1 for r≤q≤i−1. If such an r is less than i, then Pvr=1, so vr=1, so Mvr=0 and Xr=1 gives Eqr=1. □
Lemma 4 (addition). Let A and B be m-bit vectors with A∧¬B=0, and let S=(A+B)mod2m. For 0≤b<m, bit b of S⊕B is Ab⊕cb, where cb=1 exactly when there is an a<b with Aa=1 and Bq=1 for all a≤q≤b−1.
Proof. Binary addition gives Sb=Ab⊕Bb⊕cb, where cb is the carry into bit b: c0=0 and cb+1 is the majority of Ab, Bb, cb. Reduction mod 2m drops only the carry out of bit m−1, so it changes no bit below m. So (S⊕B)b=Ab⊕cb. If Bb=0 then Ab=0 and cb+1=0, and if Bb=1 then cb+1=Ab∨cb. So cb+1=Bb∧(Ab∨cb), and induction on b from c0=0 gives the stated form of cb. □
The step. Bit b=i−1 stands for row i. Line L6 computes Xh=(((Eq∧Pv)+Pv)⊕Pv)∨Eq. Apply Lemma 4 with A=Eq∧Pv and B=Pv. If Eqi=1, bit i−1 of Xh is 1. If Eqi=0, then Ai−1=0 and bit i−1 of Xh is ci−1, which is 1 exactly when some r<i has Eqr=Pvr=1 and Pvq=1 for r≤q≤i−1. By Lemma 3, D=Xh∨Mv bit by bit.
Line L7 sets Ph=Mv∨¬(Xh∨Pv). Where Mvi=1 both this and Mvi∨¬(Di∨Pvi) are 1, and where Mvi=0, Xhi=Di. So Ph holds Ph1,…,Phm by Lemma 2. Line L7 sets Mh=Pv∧Xh, and Pv∧Xh=Pv∧D because Pv∧Mv=0. So Mh holds Mh1,…,Mhm.
Line L8 reads hm from bit m−1 of Ph and Mh, at most one of which is set, so score becomes C[m][j−1]+hm=C[m][j].
After line L9, bit i−1 of Ph is Phi−1 and bit i−1 of Mh is Mhi−1 for 1≤i≤m, since the shift puts bit i−2 into bit i−1, fills bit 0 with 0=Ph0=Mh0, and the mask drops the old bit m−1. Line L10 sets Pv=Mh∨¬(Xv∨Ph) with Xv=Eq∨Mv. Here Di=Xvi∨Mhi−1 by Lemma 2, so where Mhi−1=1 both sides of part 3 are 1, and elsewhere Xvi=Di. So the new bit i−1 of Pv is [Δvi(j)=1]. Line L10 sets Mv=Ph∧Xv, and Phi−1∧Xvi=Phi−1∧Di because Phi−1 and Mhi−1 are never both 1. So the new bit i−1 of Mv is [Δvi(j)=−1]. This proves the claim for column j, and by induction for all columns.
The distance version. For ed(A,B) the first row is C[0][j]=j, so h0=1, Ph0=1 and Mh0=0. Lemmas 1 to 3 hold with the same proofs, because they use h0∈{−1,0,1} and Mh0=0 only. The only change is that the shift of Ph in line L9 fills bit 0 with 1. Then score=C[m][n]=ed(A,B). ■