Proofs: Karp-Rabin multiple-pattern matching
These are the human proofs of Karp-Rabin multiple-pattern matching, at full length.
KR-SET outputs exactly the occurrences of the patterns full
This proof shows: correctness, termination.
Claim. For every text , every list of patterns and every value of drawn at line L1, KR-SET stops and outputs each pair with and exactly once, and outputs no other pair.
Notation. For a string of length , , with values in . In the pass for a length with , write and for . For a distinct pattern , ; the sets partition .
Lemma 1 (the table). After line L4 in the pass for , the lists of together contain each distinct pattern of length exactly once, and is in the list if and only if and .
Proof. Line L4 visits each distinct pattern of length once and appends it to the list at its own hash value, and is empty before, by line L3.
Lemma 2 (rolling). For , .
Proof. Modulo ,
which is before reduction. The final reduction gives the representative in , which is .
Lemma 3 (invariant). In the pass for with , each time line L9 runs for the shift , .
Proof. By induction on . For , line L7 sets . If at line L9 and , then lines L9 and L10 do not change , and line L11 sets to the left side of Lemma 2 with , so when line L9 runs for .
Lemma 4 (the pass for one length). The pass for a length outputs each pair with and occurring at exactly once, and no other pair.
Proof. Case . Line L5 outputs for every in and every with , once each, and the pass ends. The empty pattern occurs at exactly these .
Case . No pattern of length fits in the text, so it has no occurrence, and line L6 ends the pass with no output.
Case . Each in is visited once by line L8. Fix . By Lemma 3, line L9 goes through the list , which by Lemma 1 holds each distinct pattern of length with once. Line L10 outputs for exactly when .
- Every occurrence is output. If occurs at , let . Then , so , so is in , the test of line L10 succeeds, and is output since .
- Only occurrences are output. A pair is output only with some in such that and , so .
- No pair twice. For fixed , the pair can come only from , which is in the list once, and appears once in .
Theorem. The claim holds.
Proof. Termination. Line L2 runs over finitely many lengths. In each pass, line L4 visits finitely many patterns, line L5 outputs a finite set, and lines L8 to L11 run times with finite lists at line L9. So the algorithm stops.
Output. Each pattern index belongs to the pass for , and only that pass outputs pairs with index , because line L5 and line L10 output only indices of patterns of the current length. By Lemma 4, that pass outputs each occurrence once and no non-occurrence. The value of is not used in this argument, so the output is the same for every base.
Remark (the error of the Monte Carlo variant). Without the comparison at line L10, the pass outputs for every . A false pair needs a distinct pattern of length with . If and the patterns do not depend on and is prime, the difference is a nonzero polynomial in of degree at most over , because the codes are below . It has at most roots, so this event has probability at most . The union bound over the at most windows and the distinct patterns of each length gives a total probability of at most .