Algorithm Search⌘K

Proofs: Chang-Lawler algorithm

These are the human proofs of Chang-Lawler algorithm, at full length.

The Chang-Lawler SET algorithm reports exactly the approximate matches full

This proof shows: correctness.

Indices are 0-based, ∣P∣=m|P| = m, ∣T∣=n|T| = n, k≥0k \ge 0, and ed\mathrm{ed} is the Levenshtein distance with unit costs. A match is a substring A=T[h…j)A = T[h \ldots j) with ed(P,A)≤k\mathrm{ed}(P, A) \le k, and the claim is that CHANG-LAWLER-SET returns the sorted set of the end positions jj of all matches. We use the correctness of Sellers' algorithm: SELLERS(PP, XX, kk) returns the end positions of the matches in a string XX.

Alignments

An alignment of PP and AA is a set of pairs (i,u)(i, u), with ii a position of PP and uu a position of AA, in which no position occurs twice and i<i′i < i' if and only if u<u′u < u' for any two pairs (i,u)(i, u) and (i′,u′)(i', u'). Its cost is the number of unpaired positions of PP and of AA plus the number of pairs (i,u)(i, u) with P[i]≠A[u]P[i] \ne A[u]. The least cost of an alignment is ed(P,A)\mathrm{ed}(P, A): an edit script gives an alignment of no larger cost by pairing each surviving symbol with its final position, and an alignment gives a script with one operation per unit of cost. The proof of the edit distance hardness theorem gives this argument in full (its Lemma 1).

The following fact follows from the order condition. If (i,u)(i, u) and (i′,u′)(i', u') are pairs with u<u′u < u', then every position of PP strictly between ii and i′i' is unpaired or paired with a position of AA strictly between uu and u′u'.

Lemma 1: few whole pieces

If ed(P,A)≤k\mathrm{ed}(P, A) \le k, then there are no k+1k + 1 pairwise disjoint nonempty intervals Q0,…,QkQ_0, \ldots, Q_k of positions of AA such that no A[Qr]A[Q_r] is a substring of PP.

Proof. Suppose that such intervals exist. Fix an alignment of PP and AA of cost ed(P,A)≤k\mathrm{ed}(P, A) \le k. We give, for each rr, a unit of cost that belongs to QrQ_r, such that the k+1k + 1 units are distinct. This shows a cost of at least k+1k + 1, a contradiction.

Let Qr=[x,y)Q_r = [x, y). Suppose first that every u∈Qru \in Q_r is paired, with partner π(u)\pi(u) and P[π(u)]=A[u]P[\pi(u)] = A[u], and that π(u+1)=π(u)+1\pi(u + 1) = \pi(u) + 1 for x≤u<y−1x \le u < y - 1. Then A[x…y)=P[π(x)…π(x)+y−x)A[x \ldots y) = P[\pi(x) \ldots \pi(x) + y - x), a substring of PP, which is false. So one of the following holds, and we choose the unit as stated.

  • Some u∈Qru \in Q_r is unpaired, or paired with an unequal symbol. The unit is the unpaired symbol A[u]A[u], or the unequal pair of uu.
  • All u∈Qru \in Q_r are paired with equal symbols, and π(u+1)≠π(u)+1\pi(u + 1) \ne \pi(u) + 1 for some uu with x≤u<y−1x \le u < y - 1. By the order condition, π(u+1)>π(u)\pi(u + 1) > \pi(u), so π(u+1)≥π(u)+2\pi(u + 1) \ge \pi(u) + 2, and the position i=π(u)+1i = \pi(u) + 1 of PP lies strictly between the partners of uu and u+1u + 1. A partner of ii would lie strictly between uu and u+1u + 1, so ii is unpaired. The unit is the unpaired symbol P[i]P[i].

Units of the first kind involve a position of AA in QrQ_r, so units of the first kind for different rr are distinct, because the intervals are disjoint. A unit of the first kind is a symbol of AA or a pair, and a unit of the second kind is a symbol of PP, so they are distinct. Two units of the second kind, for r≠sr \ne s, come from consecutive positions u,u+1∈Qru, u + 1 \in Q_r and v,v+1∈Qsv, v + 1 \in Q_s with u≠vu \ne v. Say u<vu < v; then u+1≤vu + 1 \le v, so π(u+1)≤π(v)\pi(u + 1) \le \pi(v), and the unit of QrQ_r is below π(u+1)\pi(u + 1) while the unit of QsQ_s is above π(v)\pi(v). So all k+1k + 1 units are distinct. □\square

Lemma 2: the pieces of a dead block

If line L5 calls block tt dead, then the intervals [pr,pr+1)[p_r, p_{r+1}) for r=0,…,kr = 0, \ldots, k are disjoint, lie inside the block [tb,(t+1)b)[tb, (t+1)b), and T[pr…pr+1)T[p_r \ldots p_{r+1}) is not a substring of PP for each rr.

Proof. A dead block is one where line L4 makes all k+1k + 1 jumps, with pr<(t+1)bp_r < (t+1)b before each jump, and ends with pk+1≤(t+1)bp_{k+1} \le (t+1)b. The jumps increase pp by at least 1, so tb=p0<p1<⋯<pk+1≤(t+1)btb = p_0 < p_1 < \cdots < p_{k+1} \le (t+1)b, and the intervals are disjoint, nonempty and inside the block. For each rr, pr+ms(pr)=pr+1−1<(t+1)b≤np_r + ms(p_r) = p_{r+1} - 1 < (t+1)b \le n, so the symbol T[pr+ms(pr)]T[p_r + ms(p_r)] exists, and by the maximality of ms(pr)ms(p_r) the string T[pr…pr+ms(pr)]=T[pr…pr+1)T[p_r \ldots p_r + ms(p_r)] = T[p_r \ldots p_{r+1}) is not a substring of PP. □\square

Lemma 3: every match covers a live block

Let m−k≥2m - k \ge 2 and b=⌊(m−k)/2⌋b = \lfloor (m - k)/2 \rfloor. Every match A=T[h…j)A = T[h \ldots j) contains a block [tb,(t+1)b)[tb, (t+1)b) with 0≤t<⌊n/b⌋0 \le t < \lfloor n/b \rfloor, and every block that AA contains is live.

Proof. Each operation changes the length by at most 1, so ∣A∣≥m−k≥2b|A| \ge m - k \ge 2b. Let t=⌈h/b⌉t = \lceil h / b \rceil. Then h≤tb<h+bh \le tb < h + b, so (t+1)b<h+2b≤j≤n(t+1)b < h + 2b \le j \le n. So AA contains the block [tb,(t+1)b)[tb, (t+1)b), and (t+1)b≤n(t+1)b \le n gives t<⌊n/b⌋t < \lfloor n / b \rfloor. If a block contained in AA were dead, Lemma 2 would give k+1k + 1 disjoint substrings of AA that are not substrings of PP, against Lemma 1 applied to AA with its positions shifted by hh. □\square

The theorem

Line L1. If m−k<2m - k < 2, the algorithm returns SELLERS(PP, TT, kk), which is correct.

Soundness. Let m−k≥2m - k \ge 2. Each window [lo,hi)[lo, hi) of line L8 satisfies 0≤lo≤hi≤n0 \le lo \le hi \le n, and an end position j′j' reported by SELLERS(PP, T[lo…hi)T[lo \ldots hi), kk) is the end of a match T[lo+h′…lo+j′)T[lo + h' \ldots lo + j') of TT. So every returned value lo+j′lo + j' is the end of a match.

Completeness. Let A=T[h…j)A = T[h \ldots j) be a match. By Lemma 3 it contains a live block tt. Since ∣A∣≤m+k|A| \le m + k, h≥j−m−k≥(t+1)b−m−kh \ge j - m - k \ge (t+1)b - m - k and j≤h+m+k≤tb+m+kj \le h + m + k \le tb + m + k, and 0≤h≤j≤n0 \le h \le j \le n. So AA lies inside the window W=[max⁡(0,(t+1)b−m−k),min⁡(n,tb+m+k))W = [\max(0, (t+1)b - m - k), \min(n, tb + m + k)) of line L6. The left and right ends of these windows do not decrease as tt grows, so line L7 merges each window into an interval that contains it, and the final windows contain every window of a live block. Let [lo,hi)[lo, hi) be the final window that contains WW. Then T[h…j)T[h \ldots j) is the substring [h−lo,j−lo)[h - lo, j - lo) of T[lo…hi)T[lo \ldots hi), so SELLERS reports j−loj - lo, and the algorithm returns jj.

Line L9 sorts the end positions and removes repeats, so the output is the sorted set of the ends of all matches. ■\blacksquare

Back to Chang-Lawler algorithm