Proofs: Valiant's algorithm
These are the human proofs of Valiant's algorithm, at full length.
Valiant's algorithm fills every cell of the recognition table with the right set of nonterminals, and it stops full
This proof shows: correctness, termination.
We use the notation of the context-free grammar page and the pseudocode of the page: VALIANT (lines L1 to L4), COMPUTE (lines L5 to L7) and COMPLETE (lines L8 to L16). The grammar is in Chomsky normal form: every production is with nonterminals and , or with a terminal , and the only other production allowed is , when is on no right side. The input is , and is the least power of 2 with .
The target values
For let be the set of nonterminals with , where are a padding symbol that is not in . For a set of positions let
and write for with the set of all positions: the pairs that can be split anywhere.
Lemma 1. (a) . (b) If , then . (c) when .
Proof. In Chomsky normal form, a nonterminal other than a start symbol with derives only nonempty strings, and is on no right side. So in a derivation , both and derive nonempty parts of , and . Hence a one-letter string comes only from a production , which is (a). For the first step must be some , and with and , both nonempty; with this is a split point with , which is (b). For (c), no derived string contains the padding symbol.
Line L1 sets the terminal cells to the value of Lemma 1(a) for and leaves them empty for , which also agrees with Lemma 1(a), since no production has the padding symbol. All other cells of and all cells of start empty.
The block products
For blocks of cells, is the matrix whose cell is the set of pairs with and for some in the shared index range. The implementation computes it with one Boolean product for each pair that is the right side of a production, which gives the same pairs.
A product added into adds to each cell of the block exactly the pairs of the splits , computed from the current . In every product of lines L13 to L15, and , so holds for all , and of the blocks. If the cells and that it reads already hold their final values , the product adds exactly .
Lemma 2: COMPLETE
Consider a call COMPLETE with a power of 2 and . Its rectangle is the set of cells with and . Suppose that, when the call starts,
- for all (the triangle of rows) and for all (the triangle of columns);
- for every cell of the rectangle (the splits in the gap between the two ranges are in);
- every cell of the rectangle with is still empty in .
Then, when the call returns, and for every cell of the rectangle, and no cell outside the rectangle has changed, in or in .
Proof. By induction on .
Base, . The rectangle is the single cell . Its splits with all lie in , so condition 2 says . If , the cell is a terminal cell, line L9 does nothing, and is right by line L1. Otherwise line L9 sets , empty by condition 3, to , which is by Lemma 1(b). Nothing else changes.
Step, . Let and , as in line L11. The rectangle splits into four quadrants of side :
- : rows , columns , the quadrant nearest the diagonal;
- : rows , columns ;
- : rows , columns ;
- : rows , columns , the farthest one.
We check the three conditions for each of the four recursive calls, in order. The triangles that the calls need lie inside the triangles of condition 1 or inside quadrants already finished, and condition 3 holds because only the call for a quadrant writes in that quadrant.
- Line L12, COMPLETE for . Its triangles and are parts of the triangles of condition 1. Its gap is , the same as ours, so condition 2 holds as it is. By induction, is final afterwards.
- Line L13, then COMPLETE for . The gap of is . The cells of have the splits by condition 2, and line L13 adds the splits , with for in the triangle of rows (final by condition 1) and in (final). So on . The triangles are , inside the triangle of rows, and . By induction, is final.
- Line L14, then COMPLETE for . The gap of is . Line L14 adds the splits , with in and for in the triangle of columns, all final. The triangles are and . By induction, is final.
- Line L15, then line L16, COMPLETE for . The gap of is . Line L15 adds , reading in the triangle of rows and in , and , reading in and in the triangle of columns. All are final. The triangles are and . By induction, is final.
Each cell of the rectangle is in exactly one quadrant, so it is final at the end, and its cell equals by the induction hypothesis. The products write only into quadrants of our rectangle, and the recursive calls change nothing outside their own quadrants, so nothing outside the rectangle changes.
Lemma 3: COMPUTE
Consider a call COMPUTE with a power of 2, and suppose that, when it starts, every cell with has and is empty in unless it is a terminal cell. Then, when it returns, for all , and no cell outside this triangle has changed.
Proof. By induction on . If , there is no cell and line L5 does nothing. Otherwise let . Line L6 fills the triangles and , by induction, without touching the rectangle of rows and columns . On that rectangle, is still empty, which is because the gap is empty, and the non-terminal cells are empty in . So line L7 meets the conditions of Lemma 2 and fills the rectangle. The two triangles and the rectangle make up the whole triangle.
Theorem
VALIANT stops and returns true exactly when . When it stops, for all , so the table holds every nonterminal that derives every substring of .
Proof. After line L1, the conditions of Lemma 3 hold for COMPUTE, so after line L2 every cell is final. If , then , and in Chomsky normal form only by the production , which line L3 tests. If , line L4 returns whether , that is, whether .
Termination: every recursive call has half the side of its caller, so the depth of the recursion is at most for COMPUTE plus for COMPLETE, and every call makes at most four recursive calls and a bounded number of block products.
Remark: the number of products
By induction on , a call COMPLETE of side makes block products of side for every power of 2 with : it makes 4 of side itself, and its four calls of side make of each smaller side. COMPUTE makes calls of COMPLETE of side for , so the number of block products of side is
Each block product costs one Boolean matrix product of side per pair , which gives the time bound of the page.