The Wu-Manber vectors hold the active rows of the Sellers table full
This proof shows: correctness.
Indices are 0-based, ∣P∣=m, ∣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][j−1]+1,C[i−1][j]+1,C[i−1][j−1]+δij),δij=[P[i−1]=T[j−1]],(R) by the correctness proof of Sellers' algorithm. For 0≤e≤k and 0≤j≤n let
Se(j)={i:1≤i≤m, C[i][j]≤e},Ve(j)=i∈Se(j)∑2i−1. For a vector X that represents a set S⊆{1,…,m} by bit i−1 for i, the vector (X≪1)∣1, cut to m bits, represents {i:1≤i≤m, i−1∈S∪{0}}.
Lemma 1. For 1≤i≤m, 1≤j≤n and 0≤e≤k, C[i][j]≤e holds exactly when at least one of these holds:
- δij=0 and C[i−1][j−1]≤e;
- e≥1 and C[i−1][j−1]≤e−1;
- e≥1 and C[i][j−1]≤e−1;
- e≥1 and C[i−1][j]≤e−1.
Proof. By (R), C[i][j]≤e exactly when one of its three terms is at most e. The term C[i][j−1]+1≤e is condition 3, and C[i−1][j]+1≤e is condition 4; both need e≥1 since the values are not negative. The term C[i−1][j−1]+δij≤e holds exactly when δij=0 and C[i−1][j−1]≤e (condition 1), or δij=1 and C[i−1][j−1]≤e−1. The latter implies condition 2, and condition 2 implies C[i−1][j−1]+δij≤e for both values of δij. □
Lemma 2. After line L2, R[e]=Ve(0) for all e. After the pass of the loop of line L4 for column j, R[e]=Ve(j) for all e.
Proof. Column 0: C[i][0]=i≤e exactly for 1≤i≤min(e,m), which are the bits 0 to min(e,m)−1 set by line L2.
Column j≥1: assume R[e]=Ve(j−1) for all e before the pass. Let b=B[T[j−1]], which has bit i−1 set exactly when δij=0. We show by induction on e that after the update of R[e], R[e]=Ve(j), and that at that moment old=Ve(j−1).
For e=0, line L5 saves old=V0(j−1) and sets R[0] to ((V0(j−1)≪1)∣1)&b. Bit i−1 of it is set exactly when i−1∈S0(j−1)∪{0}, that is C[i−1][j−1]≤0 (row 0 has the value 0), and δij=0. By Lemma 1 with e=0, only condition 1 can hold, so this is C[i][j]≤0.
For e≥1, line L7 sets prev=Ve−1(j−1) (the value of old from step e−1) and old=Ve(j−1), and R[e−1]=Ve−1(j) by the induction. The new value is the OR, cut to m bits, of four vectors. Bit i−1 of each is set exactly when:
- ((old≪1)∣1)&b: C[i−1][j−1]≤e and δij=0, which is condition 1;
- (prev≪1)∣1: C[i−1][j−1]≤e−1, which is condition 2 (for i=1, C[0][j−1]=0≤e−1);
- prev: C[i][j−1]≤e−1, which is condition 3;
- R[e−1]≪1, together with the 1 of the second vector: C[i−1][j]≤e−1, which is condition 4 (for i=1, C[0][j]=0≤e−1, and the 1 covers it).
By Lemma 1 the OR has bit i−1 set exactly when C[i][j]≤e. So R[e]=Ve(j). □
Theorem. WU-MANBER(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. By the definition of the Sellers table, j is such a position exactly when C[m][j]≤k. For m=0 this holds for every j, and lines L3 and L8 report every j. For m≥1, C[m][j]≤k holds exactly when m∈Sk(j), that is when bit m−1 of Vk(j) is set. By Lemma 2 this is the test of line L3 for j=0 and of line L8 for j≥1. The columns are handled in increasing order. ■