Algorithm Search⌘K

Proofs: Wu-Manber algorithm

These are the human proofs of Wu-Manber algorithm, at full length.

The Wu-Manber vectors hold the active rows of the Sellers table full

This proof shows: correctness.

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

by the correctness proof of Sellers' algorithm. For 0≤e≤k0 \le e \le k and 0≤j≤n0 \le j \le n let

Se(j)={ i:1≤i≤m, C[i][j]≤e },Ve(j)=∑i∈Se(j)2i−1.S_e(j) = \{\, i : 1 \le i \le m,\ C[i][j] \le e \,\}, \qquad V_e(j) = \sum_{i \in S_e(j)} 2^{i-1}.

For a vector XX that represents a set S⊆{1,…,m}S \subseteq \{1, \ldots, m\} by bit i−1i - 1 for ii, the vector (X≪1) ∣ 1(X \ll 1) \,|\, 1, cut to mm bits, represents { i:1≤i≤m, i−1∈S∪{0} }\{\, i : 1 \le i \le m,\ i - 1 \in S \cup \{0\} \,\}.

Lemma 1. For 1≤i≤m1 \le i \le m, 1≤j≤n1 \le j \le n and 0≤e≤k0 \le e \le k, C[i][j]≤eC[i][j] \le e holds exactly when at least one of these holds:

  1. δij=0\delta_{ij} = 0 and C[i−1][j−1]≤eC[i-1][j-1] \le e;
  2. e≥1e \ge 1 and C[i−1][j−1]≤e−1C[i-1][j-1] \le e - 1;
  3. e≥1e \ge 1 and C[i][j−1]≤e−1C[i][j-1] \le e - 1;
  4. e≥1e \ge 1 and C[i−1][j]≤e−1C[i-1][j] \le e - 1.

Proof. By (R), C[i][j]≤eC[i][j] \le e exactly when one of its three terms is at most ee. The term C[i][j−1]+1≤eC[i][j-1] + 1 \le e is condition 3, and C[i−1][j]+1≤eC[i-1][j] + 1 \le e is condition 4; both need e≥1e \ge 1 since the values are not negative. The term C[i−1][j−1]+δij≤eC[i-1][j-1] + \delta_{ij} \le e holds exactly when δij=0\delta_{ij} = 0 and C[i−1][j−1]≤eC[i-1][j-1] \le e (condition 1), or δij=1\delta_{ij} = 1 and C[i−1][j−1]≤e−1C[i-1][j-1] \le e - 1. The latter implies condition 2, and condition 2 implies C[i−1][j−1]+δij≤eC[i-1][j-1] + \delta_{ij} \le e for both values of δij\delta_{ij}. □\square

Lemma 2. After line L2, R[e]=Ve(0)R[e] = V_e(0) for all ee. After the pass of the loop of line L4 for column jj, R[e]=Ve(j)R[e] = V_e(j) for all ee.

Proof. Column 0: C[i][0]=i≤eC[i][0] = i \le e exactly for 1≤i≤min⁡(e,m)1 \le i \le \min(e, m), which are the bits 0 to min⁡(e,m)−1\min(e, m) - 1 set by line L2.

Column j≥1j \ge 1: assume R[e]=Ve(j−1)R[e] = V_e(j-1) for all ee before the pass. Let b=B[T[j−1]]b = B[T[j-1]], which has bit i−1i - 1 set exactly when δij=0\delta_{ij} = 0. We show by induction on ee that after the update of R[e]R[e], R[e]=Ve(j)R[e] = V_e(j), and that at that moment old=Ve(j−1)old = V_e(j-1).

For e=0e = 0, line L5 saves old=V0(j−1)old = V_0(j-1) and sets R[0]R[0] to ((V0(j−1)≪1) ∣ 1) & b((V_0(j-1) \ll 1) \,|\, 1) \,\&\, b. Bit i−1i - 1 of it is set exactly when i−1∈S0(j−1)∪{0}i - 1 \in S_0(j-1) \cup \{0\}, that is C[i−1][j−1]≤0C[i-1][j-1] \le 0 (row 0 has the value 0), and δij=0\delta_{ij} = 0. By Lemma 1 with e=0e = 0, only condition 1 can hold, so this is C[i][j]≤0C[i][j] \le 0.

For e≥1e \ge 1, line L7 sets prev=Ve−1(j−1)prev = V_{e-1}(j-1) (the value of oldold from step e−1e - 1) and old=Ve(j−1)old = V_e(j-1), and R[e−1]=Ve−1(j)R[e-1] = V_{e-1}(j) by the induction. The new value is the OR, cut to mm bits, of four vectors. Bit i−1i - 1 of each is set exactly when:

  • ((old≪1) ∣ 1) & b((old \ll 1) \,|\, 1) \,\&\, b: C[i−1][j−1]≤eC[i-1][j-1] \le e and δij=0\delta_{ij} = 0, which is condition 1;
  • (prev≪1) ∣ 1(prev \ll 1) \,|\, 1: C[i−1][j−1]≤e−1C[i-1][j-1] \le e - 1, which is condition 2 (for i=1i = 1, C[0][j−1]=0≤e−1C[0][j-1] = 0 \le e - 1);
  • prevprev: C[i][j−1]≤e−1C[i][j-1] \le e - 1, which is condition 3;
  • R[e−1]≪1R[e-1] \ll 1, together with the 1 of the second vector: C[i−1][j]≤e−1C[i-1][j] \le e - 1, which is condition 4 (for i=1i = 1, C[0][j]=0≤e−1C[0][j] = 0 \le e - 1, and the 1 covers it).

By Lemma 1 the OR has bit i−1i - 1 set exactly when C[i][j]≤eC[i][j] \le e. So R[e]=Ve(j)R[e] = V_e(j). □\square

Theorem. WU-MANBER(P,T,k)(P, T, k) reports, in increasing order, exactly the positions j∈{0,…,n}j \in \{0, \ldots, n\} such that d(P,T[h…j))≤kd(P, T[h \ldots j)) \le k for some h≤jh \le j.

Proof. By the definition of the Sellers table, jj is such a position exactly when C[m][j]≤kC[m][j] \le k. For m=0m = 0 this holds for every jj, and lines L3 and L8 report every jj. For m≥1m \ge 1, C[m][j]≤kC[m][j] \le k holds exactly when m∈Sk(j)m \in S_k(j), that is when bit m−1m - 1 of Vk(j)V_k(j) is set. By Lemma 2 this is the test of line L3 for j=0j = 0 and of line L8 for j≥1j \ge 1. The columns are handled in increasing order. ■\blacksquare

Back to Wu-Manber algorithm