Algorithm Search⌘K

Proofs: Wagner-Fischer algorithm

These are the human proofs of Wagner-Fischer algorithm, at full length.

The Wagner-Fischer table holds the Levenshtein distances of all prefix pairs full

This proof shows: correctness.

Indices are 0-based. Let xx have length mm and yy length nn. The distance d(u,v)d(u, v) is the least number of unit-cost insertions, deletions and substitutions that turn uu into vv. Let −- be a gap symbol that is not in Σ\Sigma, and let δ(a,b)=[a≠b]\delta(a, b) = [a \ne b] for a,b∈Σ∪{−}a, b \in \Sigma \cup \{-\}. This δ\delta is the discrete metric, so δ(a,b)≤δ(a,c)+δ(c,b)\delta(a, b) \le \delta(a, c) + \delta(c, b) for all three symbols.

An alignment of uu and vv is a sequence of columns (a1,b1),…,(aL,bL)(a_1, b_1), \ldots, (a_L, b_L) over Σ∪{−}\Sigma \cup \{-\}, with no column (−,−)(-, -), such that a1⋯aLa_1 \cdots a_L with the gaps removed is uu and b1⋯bLb_1 \cdots b_L with the gaps removed is vv. Its cost is ∑tδ(at,bt)\sum_t \delta(a_t, b_t). Let A(u,v)A(u, v) be the least cost of an alignment of uu and vv.

Lemma 1. After lines L1 to L6, D[i][j]=A(x[0…i),y[0…j))D[i][j] = A(x[0 \ldots i), y[0 \ldots j)) for all 0≤i≤m0 \le i \le m and 0≤j≤n0 \le j \le n.

Proof. By induction on i+ji + j, in the order of the loops, which compute D[i−1][j]D[i-1][j], D[i][j−1]D[i][j-1] and D[i−1][j−1]D[i-1][j-1] before D[i][j]D[i][j]. The only alignment of x[0…i)x[0 \ldots i) with the empty string has ii columns (a,−)(a, -), so A=iA = i, which line L1 stores. Line L2 is the same for jj. Let i,j≥1i, j \ge 1, and consider an alignment of x[0…i)x[0 \ldots i) and y[0…j)y[0 \ldots j). Its last column is one of three kinds: (x[i−1],y[j−1])(x[i-1], y[j-1]), (x[i−1],−)(x[i-1], -) or (−,y[j−1])(-, y[j-1]). If we remove the last column, we get an alignment of x[0…i−1)x[0 \ldots i-1) and y[0…j−1)y[0 \ldots j-1), or of x[0…i−1)x[0 \ldots i-1) and y[0…j)y[0 \ldots j), or of x[0…i)x[0 \ldots i) and y[0…j−1)y[0 \ldots j-1), and each alignment of these pairs extends by the matching column. So AA of the pair is the least of A(shorter pair)+δ(last column)A(\text{shorter pair}) + \delta(\text{last column}) over the three kinds. The three costs of the last columns are [x[i−1]≠y[j−1]][x[i-1] \ne y[j-1]], 1 and 1. By the induction hypothesis, this is the minimum of line L6, with cc from line L5. □\square

Lemma 2. d(u,v)≤A(u,v)d(u, v) \le A(u, v).

Proof. Take an alignment of cost A(u,v)A(u, v). Process its columns from the last one to the first one. A column (a,b)(a, b) with a=ba = b needs no operation. A column (a,b)(a, b) with a,b∈Σa, b \in \Sigma and a≠ba \ne b is a substitution of the character of uu in this column by bb. A column (a,−)(a, -) is a deletion of that character, and a column (−,b)(-, b) is an insertion of bb at this place. The columns to the left are not changed by an operation on a later column, so their characters keep their positions. At the end the string reads vv. The script has one operation for each column with δ=1\delta = 1, so its length is the cost of the alignment. □\square

Lemma 3. A(u,w)≤A(u,v)+A(v,w)A(u, w) \le A(u, v) + A(v, w) for all strings uu, vv, ww.

Proof. Let α\alpha be an optimal alignment of uu and vv and β\beta an optimal alignment of vv and ww. Each character of vv is in one column of α\alpha and in one column of β\beta. We build a sequence of triples (a,c,b)(a, c, b). For the tt-th character cc of vv, take the triple (a,c,b)(a, c, b), where (a,c)(a, c) is its column in α\alpha and (c,b)(c, b) is its column in β\beta. A column (a,−)(a, -) of α\alpha gives the triple (a,−,−)(a, -, -), and a column (−,b)(-, b) of β\beta gives the triple (−,−,b)(-, -, b). Order the triples by the order of vv: before the triple of the tt-th character of vv, and after the triple of the (t−1)(t-1)-th one, put the triples of the columns of α\alpha that lie between these two characters, in their order in α\alpha, then those of β\beta, in their order in β\beta. Then the first entries, read in order, spell uu with gaps, and the last entries spell ww with gaps. Remove the triples whose first and last entries are both −-. The pairs (first entry, last entry) of the other triples form an alignment γ\gamma of uu and ww. For each triple, δ(a,b)≤δ(a,c)+δ(c,b)\delta(a, b) \le \delta(a, c) + \delta(c, b). The term δ(a,c)\delta(a, c) is the cost of the column of α\alpha in the triple, or 0 for a triple (−,−,b)(-, -, b), and the same holds for β\beta. Each column of α\alpha and of β\beta is in exactly one triple. So the cost of γ\gamma is at most the cost of α\alpha plus the cost of β\beta. □\square

Lemma 4. A(u,v)≤d(u,v)A(u, v) \le d(u, v).

Proof. Let u=s0,s1,…,sL=vu = s_0, s_1, \ldots, s_L = v be a script of length L=d(u,v)L = d(u, v), where each sts_t comes from st−1s_{t-1} by one operation. For one operation, A(st−1,st)≤1A(s_{t-1}, s_t) \le 1: align the unchanged characters with themselves, and add one column (a,b)(a, b), (a,−)(a, -) or (−,b)(-, b) for the changed character. By Lemma 3, applied L−1L - 1 times, A(u,v)≤∑t=1LA(st−1,st)≤LA(u, v) \le \sum_{t=1}^{L} A(s_{t-1}, s_t) \le L. For L=0L = 0, u=vu = v and the alignment of each character with itself costs 0. □\square

Theorem. WAGNER-FISCHER(x,y)(x, y) returns d(x,y)d(x, y), and TRACEBACK returns an alignment of cost d(x,y)d(x, y).

Proof. By Lemmas 2 and 4, A=dA = d. By Lemma 1, line L7 returns D[m][n]=A(x,y)=d(x,y)D[m][n] = A(x, y) = d(x, y).

For TRACEBACK, we use this invariant at line L9: the columns in SS form an alignment of x[i…m)x[i \ldots m) and y[j…n)y[j \ldots n) of cost D[m][n]−D[i][j]D[m][n] - D[i][j]. It holds at line L8, where SS is empty and (i,j)=(m,n)(i, j) = (m, n). In a step, if line L10 applies, the column (x[i−1],y[j−1])(x[i-1], y[j-1]) costs D[i][j]−D[i−1][j−1]D[i][j] - D[i-1][j-1]. If line L11 applies, the column (x[i−1],−)(x[i-1], -) costs 1=D[i][j]−D[i−1][j]1 = D[i][j] - D[i-1][j]. If neither applies, then j>0j > 0 and D[i][j]=D[i][j−1]+1D[i][j] = D[i][j-1] + 1: when i=0i = 0, this is D[0][j]=jD[0][j] = j; when j=0j = 0, line L11 applies, because D[i][0]=D[i−1][0]+1D[i][0] = D[i-1][0] + 1; when i,j≥1i, j \ge 1, line L6 makes D[i][j]D[i][j] equal to one of its three terms, and the first two are excluded. So line L12 adds a column (−,y[j−1])(-, y[j-1]) of cost 1=D[i][j]−D[i][j−1]1 = D[i][j] - D[i][j-1]. In each case the invariant holds again, and i+ji + j drops by 1 or 2. So the loop stops after at most m+nm + n steps, with i=j=0i = j = 0, and SS is an alignment of xx and yy of cost D[m][n]−D[0][0]=d(x,y)D[m][n] - D[0][0] = d(x, y). ■\blacksquare

Back to Wagner-Fischer algorithm