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, , , , and is the Levenshtein distance with unit costs. A start is a position with for some with . The claim is that COLE-HARIHARAN(, , ), 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 ; aperiodicity matters for the running time.
Reversal
Write for the reverse of a string . Reversing an edit script of into gives one of the same cost of into , so . The reverse of is . So is a start of in if and only if is an end of in , that is, a position with for some .
The Landau-Vishkin values
We use the correctness proof of the Landau-Vishkin algorithm, applied to and . It defines the Sellers table , the diagonals with , and the furthest row of diagonal whose value is at most , with when there is none. It shows the following.
- (F1) when .
- (F2) If the three values , and are given correctly, with for diagonals outside or for , then the step that takes their candidates, clips to and extends by a longest common extension yields (its Lemma 4, whose proof uses only these three values).
- (F3) A position is an end if and only if (its theorem).
The program BAND-MATCHES(, ) runs the same step for on the diagonals with , where and , reads a neighbour outside or below as (the value NONE), and returns the whose stored value at level is .
Lemma 1: the band
For , the stored value of every diagonal with is . Hence BAND-MATCHES(, ) returns exactly the with .
Proof. Induct on . Fix in the stated range for ; it lies in , so the program computes it. Consider a neighbour at level (for there is none, and the step uses only the first cell of the diagonal). We show that the program reads .
- If and , then , because . So is in the range for , and its stored value is by the induction hypothesis.
- If or , the true value is by (F1) or because the diagonal has no cells. The program reads NONE: either and the neighbour is outside the array, or the entry was never written at level .
- If , then and , so the program reads NONE, and the diagonal has no cells.
By (F2), the stored value of is .
For the second sentence, let . If and , then is in the range for , so its stored value at level is , and the program returns exactly when . Otherwise : for or it is , and for there is no cell. In that case the program does not return , since the entry is NONE or lies outside , where the program does not look.
Alignments
An alignment of and is a set of pairs , with a position of and a position of , in which no position occurs twice and if and only if for any two pairs. Its cost is the number of unpaired positions of and of plus the number of pairs with , and the least cost is , as shown in the proof of the edit distance hardness theorem (its Lemma 1). If is paired, write for its partner.
Let . A break is a set of positions of , for , and the sets are pairwise disjoint. A break is clean in an alignment if every is paired with an equal symbol and whenever .
Lemma 2: at least k clean breaks
In an alignment of cost , at most breaks are not clean. So if , at least of the 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 is unpaired or paired with an unequal symbol, the unit is the unpaired symbol or the unequal pair of .
- (b) Otherwise all are paired with equal symbols, and for some . By the order condition , and the position of lies strictly between the partners of and . A partner of would lie strictly between and , so is unpaired. The unit is the unpaired symbol .
Units of kind (a) involve a position of in , and the breaks are disjoint, so they differ for different breaks. Units of kind (a) are symbols of or pairs, and units of kind (b) are unpaired symbols of , so the two kinds differ. Let both get units of kind (b), from and , with before . Then , and the unit of is below , while the unit of is above . So all units are distinct, and there are at most of them.
Lemma 3: a clean break votes for the start
Let with an alignment of cost to , and let break be clean. Then is an occurrence of in , and .
Proof. Cleanness gives and for , so . Let be the number of pairs whose position of is below ; by the order condition these are also the pairs whose position of is below . Then and , where counts the unpaired positions of below and the unpaired positions of below . So , and with give .
Lemma 4: the marks
If is a start and , then after line L5 the window has marks from at least distinct breaks, so line L6 keeps it.
Proof. Fix with and an alignment of least cost. By Lemma 2 at least breaks are clean. For a clean break , Lemma 3 gives an occurrence of the string of , 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 and . Since , Lemma 3 gives , so , and the window gets a mark from . The stamp array counts break at most once per window, because line L5 handles the occurrences of one break before those of the next, and a window marked by is counted once for . So the count of is the number of distinct breaks that mark it, which is at least .
Lemma 5: the check of a window
For a window , with and , line L7 returns exactly the starts with .
Proof. The call is BAND-MATCHES(, ) on and , and each returned diagonal is mapped to . The map is a bijection from onto . By Lemma 1, is returned if and only if , which by (F3) holds if and only if is an end of in , which by the reversal holds if and only if is a start.
The theorem
Lines L2 and L3. If , or the scan of line L1 finds fewer than breaks, the program returns the images of BAND-MATCHES(, ). By Lemma 1, (F3) and the reversal, these are the for the ends of , which are all the starts, since every end lies in and so on a diagonal in . They are sorted before they are returned.
Lines L4 to L8. Otherwise and there are disjoint breaks of length . Every returned value is a start by Lemma 5. Every start 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.