Algorithm Search⌘K

Proofs: Myers bit-parallel algorithm

These are the human proofs of Myers bit-parallel algorithm, at full length.

Myers' bit vectors hold the vertical differences of the Sellers table full

This proof shows: correctness.

Indices are 0-based, ∣P∣=m≥1|P| = m \ge 1, ∣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−1][j−1]+δij,  C[i][j−1]+1,  C[i−1][j]+1),δij=[P[i−1]≠T[j−1]],C[i][j] = \min\bigl(C[i-1][j-1] + \delta_{ij},\; C[i][j-1] + 1,\; C[i-1][j] + 1\bigr), \qquad \delta_{ij} = [P[i-1] \ne T[j-1]],

and C[m][j]=min⁡0≤h≤jed⁡(P,T[h..j))C[m][j] = \min_{0 \le h \le j} \operatorname{ed}(P, T[h..j)), by the correctness proof of Sellers' algorithm. So the end positions to report are the jj with C[m][j]≤kC[m][j] \le k. For 1≤i≤m1 \le i \le m define the vertical difference Δvi(j)=C[i][j]−C[i−1][j]\Delta v_i(j) = C[i][j] - C[i-1][j], and for 0≤i≤m0 \le i \le m, j≥1j \ge 1, the horizontal difference Δhi(j)=C[i][j]−C[i][j−1]\Delta h_i(j) = C[i][j] - C[i][j-1]. Then Δh0(j)=0\Delta h_0(j) = 0.

The claim is: after column jj is processed (line L2 for j=0j = 0, line L10 for j≥1j \ge 1), bit i−1i - 1 of PvPv is set exactly when Δvi(j)=1\Delta v_i(j) = 1, bit i−1i - 1 of MvMv is set exactly when Δvi(j)=−1\Delta v_i(j) = -1, all Δvi(j)\Delta v_i(j) lie in {−1,0,1}\{-1, 0, 1\}, and score=C[m][j]score = C[m][j]. The claim gives the output, because line L11 reports jj exactly when C[m][j]≤kC[m][j] \le k.

Column 0. C[i][0]−C[i−1][0]=1C[i][0] - C[i-1][0] = 1 for all ii, so Pv=1mPv = 1^m, Mv=0Mv = 0, and score=m=C[m][0]score = m = C[m][0], as line L2 sets them.

From now on fix j≥1j \ge 1 and assume the claim for column j−1j - 1. Write vi=Δvi(j−1)v_i = \Delta v_i(j-1), hi=Δhi(j)h_i = \Delta h_i(j), Eqi=1−δijEq_i = 1 - \delta_{ij}, and di=C[i][j]−C[i−1][j−1]d_i = C[i][j] - C[i-1][j-1] for 1≤i≤m1 \le i \le m.

Lemma 1 (one cell). Let 1≤i≤m1 \le i \le m and hi−1∈{−1,0,1}h_{i-1} \in \{-1, 0, 1\}. Then

  1. di=min⁡(δij,vi+1,hi−1+1)∈{0,1}d_i = \min(\delta_{ij}, v_i + 1, h_{i-1} + 1) \in \{0, 1\}, and di=0d_i = 0 exactly when Eqi=1Eq_i = 1 or vi=−1v_i = -1 or hi−1=−1h_{i-1} = -1;
  2. hi=di−vih_i = d_i - v_i and Δvi(j)=di−hi−1\Delta v_i(j) = d_i - h_{i-1}, and both lie in {−1,0,1}\{-1, 0, 1\}.

Proof. C[i][j−1]=C[i−1][j−1]+viC[i][j-1] = C[i-1][j-1] + v_i and C[i−1][j]=C[i−1][j−1]+hi−1C[i-1][j] = C[i-1][j-1] + h_{i-1}. Subtract C[i−1][j−1]C[i-1][j-1] from the recurrence: di=min⁡(δij,vi+1,hi−1+1)d_i = \min(\delta_{ij}, v_i + 1, h_{i-1} + 1). Each term is at least 0, and δij≤1\delta_{ij} \le 1, so di∈{0,1}d_i \in \{0, 1\}, and di=0d_i = 0 exactly when one term is 0. Next, hi=C[i][j]−C[i][j−1]=di−vih_i = C[i][j] - C[i][j-1] = d_i - v_i and Δvi(j)=C[i][j]−C[i−1][j]=di−hi−1\Delta v_i(j) = C[i][j] - C[i-1][j] = d_i - h_{i-1}. Then hi≥−1h_i \ge -1 because di≥0d_i \ge 0 and vi≤1v_i \le 1, and hi≤1h_i \le 1 because di≤vi+1d_i \le v_i + 1. The same bounds with hi−1h_{i-1} in place of viv_i give Δvi(j)∈{−1,0,1}\Delta v_i(j) \in \{-1, 0, 1\}. □\square

Since h0=0h_0 = 0, induction on ii with Lemma 1 gives hi∈{−1,0,1}h_i \in \{-1, 0, 1\} for all ii, so Lemma 1 holds for all 1≤i≤m1 \le i \le m and all differences in column jj lie in {−1,0,1}\{-1, 0, 1\}. Write Di=[di=0]D_i = [d_i = 0], Phi=[hi=1]Ph_i = [h_i = 1], Mhi=[hi=−1]Mh_i = [h_i = -1], Pvi=[vi=1]Pv_i = [v_i = 1], Mvi=[vi=−1]Mv_i = [v_i = -1], and Ph0=Mh0=0Ph_0 = Mh_0 = 0.

Lemma 2 (cell logic). For 1≤i≤m1 \le i \le m:

  1. Di=Eqi∨Mvi∨Mhi−1D_i = Eq_i \lor Mv_i \lor Mh_{i-1};
  2. Phi=Mvi∨¬(Di∨Pvi)Ph_i = Mv_i \lor \lnot(D_i \lor Pv_i) and Mhi=Pvi∧DiMh_i = Pv_i \land D_i;
  3. [Δvi(j)=1]=Mhi−1∨¬(Di∨Phi−1)[\Delta v_i(j) = 1] = Mh_{i-1} \lor \lnot(D_i \lor Ph_{i-1}) and [Δvi(j)=−1]=Phi−1∧Di[\Delta v_i(j) = -1] = Ph_{i-1} \land D_i.

Proof. Part 1 is Lemma 1, part 1. For part 2, hi=di−vih_i = d_i - v_i with di∈{0,1}d_i \in \{0, 1\}. By part 1, MviMv_i implies DiD_i. So hi=1h_i = 1 holds when vi=−1v_i = -1 (then di=0d_i = 0), or when vi=0v_i = 0 and di=1d_i = 1, which is ¬Di∧¬Pvi\lnot D_i \land \lnot Pv_i given ¬Mvi\lnot Mv_i. No other pair gives 1. And hi=−1h_i = -1 only for di=0d_i = 0, vi=1v_i = 1. Part 3 is the same argument for Δvi(j)=di−hi−1\Delta v_i(j) = d_i - h_{i-1}, with Mhi−1⇒DiMh_{i-1} \Rightarrow D_i from part 1. □\square

Lemma 3 (the carry chain). Let Xi=Eqi∨MviX_i = Eq_i \lor Mv_i. For 1≤i≤m1 \le i \le m, Di=1D_i = 1 exactly when Xi=1X_i = 1, or there is an r<ir < i with Eqr=1Eq_r = 1 and Pvq=1Pv_q = 1 for all r≤q≤i−1r \le q \le i - 1.

Proof. By Lemma 2, Mhi−1=Pvi−1∧Di−1Mh_{i-1} = Pv_{i-1} \land D_{i-1} for i≥2i \ge 2, and Mh0=0Mh_0 = 0. So D1=X1D_1 = X_1 and Di=Xi∨(Pvi−1∧Di−1)D_i = X_i \lor (Pv_{i-1} \land D_{i-1}). Induction on ii gives: Di=1D_i = 1 exactly when there is an r≤ir \le i with Xr=1X_r = 1 and Pvq=1Pv_q = 1 for r≤q≤i−1r \le q \le i - 1. If such an rr is less than ii, then Pvr=1Pv_r = 1, so vr=1v_r = 1, so Mvr=0Mv_r = 0 and Xr=1X_r = 1 gives Eqr=1Eq_r = 1. □\square

Lemma 4 (addition). Let AA and BB be mm-bit vectors with A∧¬B=0A \land \lnot B = 0, and let S=(A+B) mod 2mS = (A + B) \bmod 2^m. For 0≤b<m0 \le b < m, bit bb of S⊕BS \oplus B is Ab⊕cbA_b \oplus c_b, where cb=1c_b = 1 exactly when there is an a<ba < b with Aa=1A_a = 1 and Bq=1B_q = 1 for all a≤q≤b−1a \le q \le b - 1.

Proof. Binary addition gives Sb=Ab⊕Bb⊕cbS_b = A_b \oplus B_b \oplus c_b, where cbc_b is the carry into bit bb: c0=0c_0 = 0 and cb+1c_{b+1} is the majority of AbA_b, BbB_b, cbc_b. Reduction mod 2m2^m drops only the carry out of bit m−1m - 1, so it changes no bit below mm. So (S⊕B)b=Ab⊕cb(S \oplus B)_b = A_b \oplus c_b. If Bb=0B_b = 0 then Ab=0A_b = 0 and cb+1=0c_{b+1} = 0, and if Bb=1B_b = 1 then cb+1=Ab∨cbc_{b+1} = A_b \lor c_b. So cb+1=Bb∧(Ab∨cb)c_{b+1} = B_b \land (A_b \lor c_b), and induction on bb from c0=0c_0 = 0 gives the stated form of cbc_b. □\square

The step. Bit b=i−1b = i - 1 stands for row ii. Line L6 computes Xh=(((Eq∧Pv)+Pv)⊕Pv)∨EqXh = (((Eq \land Pv) + Pv) \oplus Pv) \lor Eq. Apply Lemma 4 with A=Eq∧PvA = Eq \land Pv and B=PvB = Pv. If Eqi=1Eq_i = 1, bit i−1i - 1 of XhXh is 1. If Eqi=0Eq_i = 0, then Ai−1=0A_{i-1} = 0 and bit i−1i - 1 of XhXh is ci−1c_{i-1}, which is 1 exactly when some r<ir < i has Eqr=Pvr=1Eq_r = Pv_r = 1 and Pvq=1Pv_q = 1 for r≤q≤i−1r \le q \le i - 1. By Lemma 3, D=Xh∨MvD = Xh \lor Mv bit by bit.

Line L7 sets Ph=Mv∨¬(Xh∨Pv)Ph = Mv \lor \lnot(Xh \lor Pv). Where Mvi=1Mv_i = 1 both this and Mvi∨¬(Di∨Pvi)Mv_i \lor \lnot(D_i \lor Pv_i) are 1, and where Mvi=0Mv_i = 0, Xhi=DiXh_i = D_i. So PhPh holds Ph1,…,PhmPh_1, \ldots, Ph_m by Lemma 2. Line L7 sets Mh=Pv∧XhMh = Pv \land Xh, and Pv∧Xh=Pv∧DPv \land Xh = Pv \land D because Pv∧Mv=0Pv \land Mv = 0. So MhMh holds Mh1,…,MhmMh_1, \ldots, Mh_m.

Line L8 reads hmh_m from bit m−1m - 1 of PhPh and MhMh, at most one of which is set, so scorescore becomes C[m][j−1]+hm=C[m][j]C[m][j-1] + h_m = C[m][j].

After line L9, bit i−1i - 1 of PhPh is Phi−1Ph_{i-1} and bit i−1i - 1 of MhMh is Mhi−1Mh_{i-1} for 1≤i≤m1 \le i \le m, since the shift puts bit i−2i - 2 into bit i−1i - 1, fills bit 0 with 0=Ph0=Mh00 = Ph_0 = Mh_0, and the mask drops the old bit m−1m - 1. Line L10 sets Pv=Mh∨¬(Xv∨Ph)Pv = Mh \lor \lnot(Xv \lor Ph) with Xv=Eq∨MvXv = Eq \lor Mv. Here Di=Xvi∨Mhi−1D_i = Xv_i \lor Mh_{i-1} by Lemma 2, so where Mhi−1=1Mh_{i-1} = 1 both sides of part 3 are 1, and elsewhere Xvi=DiXv_i = D_i. So the new bit i−1i - 1 of PvPv is [Δvi(j)=1][\Delta v_i(j) = 1]. Line L10 sets Mv=Ph∧XvMv = Ph \land Xv, and Phi−1∧Xvi=Phi−1∧DiPh_{i-1} \land Xv_i = Ph_{i-1} \land D_i because Phi−1Ph_{i-1} and Mhi−1Mh_{i-1} are never both 1. So the new bit i−1i - 1 of MvMv is [Δvi(j)=−1][\Delta v_i(j) = -1]. This proves the claim for column jj, and by induction for all columns.

The distance version. For ed⁡(A,B)\operatorname{ed}(A, B) the first row is C[0][j]=jC[0][j] = j, so h0=1h_0 = 1, Ph0=1Ph_0 = 1 and Mh0=0Mh_0 = 0. Lemmas 1 to 3 hold with the same proofs, because they use h0∈{−1,0,1}h_0 \in \{-1, 0, 1\} and Mh0=0Mh_0 = 0 only. The only change is that the shift of PhPh in line L9 fills bit 0 with 1. Then score=C[m][n]=ed⁡(A,B)score = C[m][n] = \operatorname{ed}(A, B). ■\blacksquare

Back to Myers bit-parallel algorithm