Proofs: CYK algorithm
These are the human proofs of CYK algorithm, at full length.
CYK computes exactly the nonterminals that derive each substring full
This proof shows: correctness.
We use the pseudocode of the page. The grammar is in Chomsky normal form: each production is with , or with , or with on no right side. The input is . For write and .
Lemma 1 (one symbol). if and only if .
Proof. If , then . Conversely, let with (the case is impossible since ). The first step uses a production of . It is not : that is allowed only for , and then the derivation ends at . It is not : in Chomsky normal form no nonterminal other than derives , and is on no right side, so each of and derives only nonempty strings, and derives only strings of length at least 2. So the first step is for a terminal , the derivation ends there, and .
Lemma 2 (longer substrings). For , if and only if there are with and a production with and .
Proof. If such , and exist, then . Conversely, let with . As in Lemma 1, the first step is not and not , since those derive strings of length 0 and 1. So it is some , and . By the splitting lemma (see the derivation page), with and . Both and are nonempty, because and are not (as is on no right side) and so derive no empty string. Let ; then , and .
Claim. When line L11 is reached, for all , and CYK returns true if and only if .
Proof. We show by induction on that after the iteration of line L4 for length (or after line L3 for ), every cell with equals , and that no later line changes it.
Base, . Line L3 sets , which is by Lemma 1. Later lines write only cells with .
Step, . Assume the claim for all lengths less than . Fix and . Line L6 sets , and only the iterations for this write to , since each later iteration has another pair . Lines L7 to L10 add exactly when some with and some have and . The cells and have lengths and , both between 1 and , so by the induction hypothesis they equal and . By Lemma 2, the set of added nonterminals is exactly .
For the claim gives , and line L11 returns true if and only if . For , line L1 returns true if and only if . In Chomsky normal form this is the only way to derive : a derivation that starts with or yields a string of length at least 1, as shown in Lemma 1.
The parse tree. Line L10 sets only when , and , and for the leaf comes from . Define the tree of with as the leaf node over if , and otherwise as the node with the trees of and as children. By induction on , this is well defined (both children satisfy and , with shorter spans), it uses only productions of , and its leaves spell . So the tree of is a parse tree of . Each internal node with two children splits its span into two nonempty parts, so the tree has nodes of the form and binary nodes, and its construction takes time.
Termination. All loops are bounded for loops over finite ranges and the finite set , so CYK stops after runs of line L8.