Algorithm Search⌘K

Proofs: Cole-Hariharan algorithm

These are the human proofs of Cole-Hariharan algorithm, at full length.

The sparse Cole-Hariharan algorithm reports exactly the start positions of 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 start is a position s∈{0,…,n}s \in \{0, \ldots, n\} with ed(P,T[s…j))≤k\mathrm{ed}(P, T[s \ldots j)) \le k for some jj with s≤j≤ns \le j \le n. The claim is that COLE-HARIHARAN(PP, TT, kk), as given on the page and in the implementation, returns the sorted set of starts. Only the sparse case and the fallback of lines L2 and L3 are proved here; the mostly periodic procedure of the paper is not part of this program. The proof does not use that the breaks are aperiodic, only that they are disjoint substrings of PP; aperiodicity matters for the running time.

Reversal

Write XRX^R for the reverse of a string XX. Reversing an edit script of PP into XX gives one of the same cost of PRP^R into XRX^R, so ed(P,X)=ed(PR,XR)\mathrm{ed}(P, X) = \mathrm{ed}(P^R, X^R). The reverse of T[s…j)T[s \ldots j) is TR[n−j…n−s)T^R[n - j \ldots n - s). So ss is a start of PP in TT if and only if n−sn - s is an end of PRP^R in TRT^R, that is, a position j′j' with ed(PR,TR[h′…j′))≤k\mathrm{ed}(P^R, T^R[h' \ldots j')) \le k for some h′≤j′h' \le j'.

The Landau-Vishkin values

We use the correctness proof of the Landau-Vishkin algorithm, applied to PRP^R and TRT^R. It defines the Sellers table CC, the diagonals dd with −m≤d≤n-m \le d \le n, and the furthest row L[d][e]L[d][e] of diagonal dd whose value is at most ee, with L[d][e]=−∞L[d][e] = -\infty when there is none. It shows the following.

  • (F1) L[d][e]=−∞L[d][e] = -\infty when d<max⁡(−e,−m)d < \max(-e, -m).
  • (F2) If the three values L[d−1][e−1]L[d-1][e-1], L[d][e−1]L[d][e-1] and L[d+1][e−1]L[d+1][e-1] are given correctly, with −∞-\infty for diagonals outside [−m,n][-m, n] or for e−1=−1e - 1 = -1, then the step that takes their candidates, clips to min⁡(m,n−d)\min(m, n - d) and extends by a longest common extension yields L[d][e]L[d][e] (its Lemma 4, whose proof uses only these three values).
  • (F3) A position j′j' is an end if and only if L[j′−m][k]=mL[j' - m][k] = m (its theorem).

The program BAND-MATCHES(dlodlo, dhidhi) runs the same step for e=0,…,ke = 0, \ldots, k on the diagonals dd with max⁡(blo,−e)≤d≤bhi\max(blo, -e) \le d \le bhi, where blo=max⁡(dlo−k,−m)blo = \max(dlo - k, -m) and bhi=min⁡(dhi+k,n)bhi = \min(dhi + k, n), reads a neighbour outside [blo,bhi][blo, bhi] or below −(e−1)-(e - 1) as −∞-\infty (the value NONE), and returns the d∈[dlo,dhi]d \in [dlo, dhi] whose stored value at level kk is mm.

Lemma 1: the band

For 0≤e≤k0 \le e \le k, the stored value of every diagonal dd with max⁡(dlo−k+e,−e,−m)≤d≤min⁡(dhi+k−e,n)\max(dlo - k + e, -e, -m) \le d \le \min(dhi + k - e, n) is L[d][e]L[d][e]. Hence BAND-MATCHES(dlodlo, dhidhi) returns exactly the d∈[dlo,dhi]d \in [dlo, dhi] with L[d][k]=mL[d][k] = m.

Proof. Induct on ee. Fix dd in the stated range for ee; it lies in [blo,bhi][blo, bhi], so the program computes it. Consider a neighbour d′∈{d−1,d,d+1}d' \in \{d - 1, d, d + 1\} at level e−1e - 1 (for e=0e = 0 there is none, and the step uses only the first cell of the diagonal). We show that the program reads L[d′][e−1]L[d'][e - 1].

  • If −m≤d′≤n-m \le d' \le n and d′≥−(e−1)d' \ge -(e - 1), then dlo−k+(e−1)≤d′≤dhi+k−(e−1)dlo - k + (e - 1) \le d' \le dhi + k - (e - 1), because ∣d′−d∣≤1|d' - d| \le 1. So d′d' is in the range for e−1e - 1, and its stored value is L[d′][e−1]L[d'][e - 1] by the induction hypothesis.
  • If d′<−(e−1)d' < -(e - 1) or d′<−md' < -m, the true value is −∞-\infty by (F1) or because the diagonal has no cells. The program reads NONE: either d′<blod' < blo and the neighbour is outside the array, or the entry was never written at level e−1e - 1.
  • If d′>nd' > n, then d=nd = n and d′=n+1>bhid' = n + 1 > bhi, so the program reads NONE, and the diagonal has no cells.

By (F2), the stored value of dd is L[d][e]L[d][e].

For the second sentence, let d∈[dlo,dhi]d \in [dlo, dhi]. If −k≤d-k \le d and −m≤d≤n-m \le d \le n, then dd is in the range for e=ke = k, so its stored value at level kk is L[d][k]L[d][k], and the program returns dd exactly when L[d][k]=mL[d][k] = m. Otherwise L[d][k]≠mL[d][k] \ne m: for d<−kd < -k or d<−md < -m it is −∞-\infty, and for d>nd > n there is no cell. In that case the program does not return dd, since the entry is NONE or dd lies outside [blo,bhi][blo, bhi], where the program does not look. □\square

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. Its cost is the number of unpaired positions of PP and of AA plus the number of pairs with P[i]≠A[u]P[i] \ne A[u], and the least cost is ed(P,A)\mathrm{ed}(P, A), as shown in the proof of the edit distance hardness theorem (its Lemma 1). If ii is paired, write π(i)\pi(i) for its partner.

Let ℓ=k2\ell = k^2. A break is a set Ib=[ob,ob+ℓ)I_b = [o_b, o_b + \ell) of positions of PP, for b=0,…,2k−1b = 0, \ldots, 2k - 1, and the sets are pairwise disjoint. A break is clean in an alignment if every i∈Ibi \in I_b is paired with an equal symbol and π(i+1)=π(i)+1\pi(i + 1) = \pi(i) + 1 whenever i,i+1∈Ibi, i + 1 \in I_b.

Lemma 2: at least k clean breaks

In an alignment of cost cc, at most cc breaks are not clean. So if c≤kc \le k, at least kk of the 2k2k breaks are clean.

Proof. We give each break that is not clean a unit of cost, with distinct units for distinct breaks.

  • (a) If some i∈Ibi \in I_b is unpaired or paired with an unequal symbol, the unit is the unpaired symbol P[i]P[i] or the unequal pair of ii.
  • (b) Otherwise all i∈Ibi \in I_b are paired with equal symbols, and π(i+1)≠π(i)+1\pi(i + 1) \ne \pi(i) + 1 for some i,i+1∈Ibi, i + 1 \in I_b. By the order condition π(i+1)≥π(i)+2\pi(i + 1) \ge \pi(i) + 2, and the position u=π(i)+1u = \pi(i) + 1 of AA lies strictly between the partners of ii and i+1i + 1. A partner of uu would lie strictly between ii and i+1i + 1, so uu is unpaired. The unit is the unpaired symbol A[u]A[u].

Units of kind (a) involve a position of PP in IbI_b, and the breaks are disjoint, so they differ for different breaks. Units of kind (a) are symbols of PP or pairs, and units of kind (b) are unpaired symbols of AA, so the two kinds differ. Let b≠b′b \ne b' both get units of kind (b), from i,i+1∈Ibi, i + 1 \in I_b and i′,i′+1∈Ib′i', i' + 1 \in I_{b'}, with IbI_b before Ib′I_{b'}. Then i+1≤i′i + 1 \le i', and the unit of bb is below π(i+1)≤π(i′)\pi(i + 1) \le \pi(i'), while the unit of b′b' is above π(i′)\pi(i'). So all units are distinct, and there are at most cc of them. □\square

Lemma 3: a clean break votes for the start

Let A=T[s…j)A = T[s \ldots j) with an alignment of cost c≤kc \le k to PP, and let break bb be clean. Then q=s+π(ob)q = s + \pi(o_b) is an occurrence of P[ob…ob+ℓ)P[o_b \ldots o_b + \ell) in TT, and q−ob−k≤s≤q−ob+kq - o_b - k \le s \le q - o_b + k.

Proof. Cleanness gives π(ob+r)=π(ob)+r\pi(o_b + r) = \pi(o_b) + r and A[π(ob)+r]=P[ob+r]A[\pi(o_b) + r] = P[o_b + r] for 0≤r<ℓ0 \le r < \ell, so T[q+r]=P[ob+r]T[q + r] = P[o_b + r]. Let tt be the number of pairs whose position of PP is below obo_b; by the order condition these are also the pairs whose position of AA is below π(ob)\pi(o_b). Then ob=t+xo_b = t + x and π(ob)=t+y\pi(o_b) = t + y, where xx counts the unpaired positions of PP below obo_b and yy the unpaired positions of AA below π(ob)\pi(o_b). So q−s−ob=y−xq - s - o_b = y - x, and x,y≥0x, y \ge 0 with x+y≤c≤kx + y \le c \le k give ∣q−s−ob∣≤k|q - s - o_b| \le k. □\square

Lemma 4: the marks

If ss is a start and k≥1k \ge 1, then after line L5 the window w=⌊s/k⌋w = \lfloor s / k \rfloor has marks from at least kk distinct breaks, so line L6 keeps it.

Proof. Fix jj with ed(P,T[s…j))≤k\mathrm{ed}(P, T[s \ldots j)) \le k and an alignment of least cost. By Lemma 2 at least kk breaks are clean. For a clean break bb, Lemma 3 gives an occurrence qq of the string of bb, which the Aho-Corasick search of line L4 reports (the implementation looks up each break by its string, so equal strings at different offsets share the list of occurrences, and each break uses its own offset). Line L5 sets lo=max⁡(0,q−ob−k)lo = \max(0, q - o_b - k) and hi=min⁡(n,q−ob+k)hi = \min(n, q - o_b + k). Since 0≤s≤n0 \le s \le n, Lemma 3 gives lo≤s≤hilo \le s \le hi, so ⌊lo/k⌋≤w≤⌊hi/k⌋\lfloor lo/k \rfloor \le w \le \lfloor hi/k \rfloor, and the window ww gets a mark from bb. The stamp array counts break bb at most once per window, because line L5 handles the occurrences of one break before those of the next, and a window marked by bb is counted once for bb. So the count of ww is the number of distinct breaks that mark it, which is at least kk. □\square

Lemma 5: the check of a window

For a window ww, with s0=wks_0 = wk and s1=min⁡(wk+k−1,n)s_1 = \min(wk + k - 1, n), line L7 returns exactly the starts ss with s0≤s≤s1s_0 \le s \le s_1.

Proof. The call is BAND-MATCHES(n−m−s1n - m - s_1, n−m−s0n - m - s_0) on PRP^R and TRT^R, and each returned diagonal dd is mapped to s=n−(m+d)s = n - (m + d). The map d↦n−m−dd \mapsto n - m - d is a bijection from [n−m−s1,n−m−s0][n - m - s_1, n - m - s_0] onto [s0,s1][s_0, s_1]. By Lemma 1, dd is returned if and only if L[d][k]=mL[d][k] = m, which by (F3) holds if and only if m+d=n−sm + d = n - s is an end of PRP^R in TRT^R, which by the reversal holds if and only if ss is a start. □\square

The theorem

Lines L2 and L3. If k=0k = 0, or the scan of line L1 finds fewer than 2k2k breaks, the program returns the images of BAND-MATCHES(−m-m, n−mn - m). By Lemma 1, (F3) and the reversal, these are the s=n−m−ds = n - m - d for the ends m+dm + d of PRP^R, which are all the starts, since every end lies in [0,n][0, n] and so on a diagonal in [−m,n−m][-m, n - m]. They are sorted before they are returned.

Lines L4 to L8. Otherwise k≥1k \ge 1 and there are 2k2k disjoint breaks of length k2k^2. Every returned value is a start by Lemma 5. Every start ss lies in a window that line L6 keeps, by Lemma 4, and Lemma 5 then returns it. The windows are disjoint, so no start is returned twice, and line L8 sorts the result. So the program returns the sorted set of starts. ■\blacksquare

Back to Cole-Hariharan algorithm