Proofs: Bit-parallel LCS algorithm
These are the human proofs of Bit-parallel LCS algorithm, at full length.
The bit-parallel update computes the steps of each LCS column full
This proof shows: correctness.
We use the pseudocode of the page. Let for , . Bit of a word is called row . For a column let for , and let be the -bit word whose row is . A row with is a step of column .
Lemma 1 (unit differences). For all : and . So is well defined.
Proof. Adding one item to one of the two sequences cannot lower the LCS length, and it raises it by at most 1, since deleting that item from a common subsequence of the longer prefixes leaves a common subsequence that is shorter by at most 1.
Lemma 2 (block rule). Fix , write , and . Let be rows with , no step of column at rows , and a step at row . Let be the first row in with , if there is one. Then in rows , column has exactly one step, at row if exists and at row otherwise, and .
The same holds for a final run: if column has no step at rows and , then column has exactly one step in these rows, at the first row with , if there is one, and no step otherwise.
Proof. Let . By the assumptions, for and . The LCS recurrence in the form of the LCS table is if , and otherwise. By Lemma 1, .
Rows before (or all rows if does not exist or ): they are not matches, so , and from we get . No step.
If exists and : , a step. For , , so no step. At : ; if , ; otherwise . So no step at , and .
If : , a step at , and .
If does not exist: , a step at , and .
For a final run, the same argument without the row gives the claim.
Lemma 3 (blocks cover the column). Let column have steps at rows . Split the rows into the blocks and the final run (empty if ). Then Lemma 2 applies to each of them, and so it describes all steps of column .
Proof. The first block starts at with . Each block ends with by Lemma 2, which is the condition for the next block or the final run.
Lemma 4 (the word operations). Let , , , and . Then .
Proof. In , a block of Lemma 3 is a run of 1 bits at rows and a 0 bit at row ; the final run is 1 bits at rows . Since , has no bit at any row .
No carry between blocks. Consider the part of formed by the bits of one block . Its value in is (bits to ), and in it is a subset of the same bits, so the sum is less than . Its highest possible bit is , which is row . So the block, with its carry entering from below equal to 0, produces no carry into row . By induction from the lowest block upward, the carry into each block is 0, and can be computed block by block.
Inside a block . The block of has 1 bits at and 0 at .
- If has no bit in the block, that is, no row of is a match, then equals in the block: 1 at and 0 at . is 0 at , since is. So is 0 at row and 1 at rows . By Lemma 2, is or does not exist, and in both cases the only step is at . This matches.
- If has a bit in the block, let be its lowest row; it is the first match in , since is 1 at all of . Adding the bit at row to the 1 bits at rows makes them 0 and sets row . The other bits of , at rows in , then fall on 0 bits and are set with no carry. So is 1 at rows , 0 at , equal to at rows , and 1 at . The word is 1 exactly at the rows of that are not matches. The OR is 1 at all rows of except , where both terms are 0 (, and the sum is 0 there). By Lemma 2 the only step is at . This matches.
Final run . If has no bit there, is all 1 in the run, and by Lemma 2 there is no step. Otherwise, with the lowest bit of , the carry from row runs through rows and leaves the word, where the mod drops it; the rest is as in a block, without row . So is 1 at all rows of the run except , as Lemma 2 says.
Each row of therefore equals the row of .
Theorem. BIT-PARALLEL-LCS returns .
Proof. Line L1 builds the masks. Column 0 of the table is all 0, so it has no step, and , which is line L2. By Lemma 4 and induction on , after round of line L3, . After round , the number of 0 bits of is the number of steps of column , which is . Line L6 returns minus the number of 1 bits, which is this number.
Cost. Each round does a constant number of operations on -bit words, each of which is machine-word operations, with the carry of the addition passed from each machine word to the next. So the loop takes time, and line L1 takes .