Proofs: Amir-Lewenstein-Porat algorithm
These are the human proofs of Amir-Lewenstein-Porat algorithm, at full length.
The Amir-Lewenstein-Porat algorithm reports exactly the k-mismatch occurrences full
This proof shows: correctness.
Indices are 0-based, , , , and is the number of with , for . The claim is that ALP-K-MISMATCH returns the set in increasing order. If there is no start position, and the algorithm returns nothing. Assume from now on.
Line L1. If , then for every , so every start position is an occurrence, which is what line L1 returns. Assume from now on. Then .
The parameters. Since and , we have . Each symbol of occurs at least times in , so in case A line L4 can choose distinct positions of each of distinct symbols of . Let be this set of marks. Then , and is one of the chosen symbols for each .
Lemma 1: the counts of case A
After line L5, is the number of marks with , for .
Proof. Line L5 adds 1 to once for each pair with , and . For a fixed , the map is a bijection from the marks with onto these pairs, since is determined by , and holds because and .
Lemma 2: the filter keeps every occurrence
If , then .
Proof. The marks with are some of the mismatched positions, so there are at most of them. By Lemma 1, .
Lemma 3: the kangaroo check
KANGAROO() succeeds if and only if .
Proof. Let , let be the mismatched positions of alignment , and let and . We claim that for , if the loop has not stopped, its iteration number starts with and the counter equal to . This holds for . In this iteration, for , and when . So the longest common prefix of and has length , and becomes . If , then , and the check succeeds. Otherwise the counter becomes ; the check fails if , and else becomes , which proves the claim for . So the check fails in iteration for the least with , which exists if and only if ; and when , it reaches iteration and succeeds.
Each step makes one extension query, and the check fails at the latest at the st mismatch, so it makes at most queries. A query at reads the suffix of from , so it is defined.
Case A is correct
Line L6 returns the with for which KANGAROO() succeeds, in increasing order. By Lemma 3, every returned has . Conversely, if , Lemma 2 shows that passes the filter, and Lemma 3 shows that the check succeeds, so is returned.
Case B is correct
We show that after lines L8 and L9, , the number of with . Split these by the symbol .
- If , line L8 adds the correlation for , which counts at exactly the with and . Each block of of length starting at holds the windows of the starts , and the blocks start at , so each start is computed in exactly one block. The transform computes the correlation exactly: with a number-theoretic transform modulo a prime each value is at most , and with a floating-point transform each value is rounded to the nearest integer.
- If is rare, line L9 adds 1 at for the pair , since is rare and , and for no other pair with this , by the bijection of Lemma 1.
A pair with is counted by neither line: the correlation for needs , and line L9 needs . So , and line L10 returns exactly the with , in increasing order.
Conclusion
Lines L3 to L10 run exactly one of the two cases, and each returns the set of occurrences. With line L1, ALP-K-MISMATCH is correct for all and all strings, including the empty pattern, which line L1 handles because .