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, , , , and is the Levenshtein distance with unit costs. A match is a substring with , and the claim is that CHANG-LAWLER-SET returns the sorted set of the end positions of all matches. We use the correctness of Sellers' algorithm: SELLERS(, , ) returns the end positions of the matches in a string .
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 and . Its cost is the number of unpaired positions of and of plus the number of pairs with . The least cost of an alignment is : 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 and are pairs with , then every position of strictly between and is unpaired or paired with a position of strictly between and .
Lemma 1: few whole pieces
If , then there are no pairwise disjoint nonempty intervals of positions of such that no is a substring of .
Proof. Suppose that such intervals exist. Fix an alignment of and of cost . We give, for each , a unit of cost that belongs to , such that the units are distinct. This shows a cost of at least , a contradiction.
Let . Suppose first that every is paired, with partner and , and that for . Then , a substring of , which is false. So one of the following holds, and we choose the unit as stated.
- Some is unpaired, or paired with an unequal symbol. The unit is the unpaired symbol , or the unequal pair of .
- All are paired with equal symbols, and for some with . By the order condition, , so , 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 the first kind involve a position of in , so units of the first kind for different are distinct, because the intervals are disjoint. A unit of the first kind is a symbol of or a pair, and a unit of the second kind is a symbol of , so they are distinct. Two units of the second kind, for , come from consecutive positions and with . Say ; then , so , and the unit of is below while the unit of is above . So all units are distinct.
Lemma 2: the pieces of a dead block
If line L5 calls block dead, then the intervals for are disjoint, lie inside the block , and is not a substring of for each .
Proof. A dead block is one where line L4 makes all jumps, with before each jump, and ends with . The jumps increase by at least 1, so , and the intervals are disjoint, nonempty and inside the block. For each , , so the symbol exists, and by the maximality of the string is not a substring of .
Lemma 3: every match covers a live block
Let and . Every match contains a block with , and every block that contains is live.
Proof. Each operation changes the length by at most 1, so . Let . Then , so . So contains the block , and gives . If a block contained in were dead, Lemma 2 would give disjoint substrings of that are not substrings of , against Lemma 1 applied to with its positions shifted by .
The theorem
Line L1. If , the algorithm returns SELLERS(, , ), which is correct.
Soundness. Let . Each window of line L8 satisfies , and an end position reported by SELLERS(, , ) is the end of a match of . So every returned value is the end of a match.
Completeness. Let be a match. By Lemma 3 it contains a live block . Since , and , and . So lies inside the window of line L6. The left and right ends of these windows do not decrease as 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 be the final window that contains . Then is the substring of , so SELLERS reports , and the algorithm returns .
Line L9 sorts the end positions and removes repeats, so the output is the sorted set of the ends of all matches.