Algorithm Search⌘K

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 G=(V,Σ,P,S)G = (V, \Sigma, P, S) is in Chomsky normal form: each production is A→BCA \to BC with B,C∈VB, C \in V, or A→aA \to a with a∈Σa \in \Sigma, or S→εS \to \varepsilon with SS on no right side. The input is w=a1⋯anw = a_1 \cdots a_n. For 1≤i≤j≤n1 \le i \le j \le n write wi,j=ai⋯ajw_{i,j} = a_i \cdots a_j and D[i,j]={A∈V:A⇒∗wi,j}D[i, j] = \{A \in V : A \Rightarrow^* w_{i,j}\}.

Lemma 1 (one symbol). A∈D[i,i]A \in D[i, i] if and only if A→ai∈PA \to a_i \in P.

Proof. If A→ai∈PA \to a_i \in P, then A⇒aiA \Rightarrow a_i. Conversely, let A⇒maiA \Rightarrow^m a_i with m≥1m \ge 1 (the case m=0m = 0 is impossible since A≠aiA \ne a_i). The first step uses a production of AA. It is not A→εA \to \varepsilon: that is allowed only for A=SA = S, and then the derivation ends at ε≠ai\varepsilon \neq a_i. It is not A→BCA \to BC: in Chomsky normal form no nonterminal other than SS derives ε\varepsilon, and SS is on no right side, so each of BB and CC derives only nonempty strings, and BCBC derives only strings of length at least 2. So the first step is A→aA \to a for a terminal aa, the derivation ends there, and a=aia = a_i. □\square

Lemma 2 (longer substrings). For i<ji < j, A∈D[i,j]A \in D[i, j] if and only if there are kk with i≤k<ji \le k < j and a production A→BC∈PA \to BC \in P with B∈D[i,k]B \in D[i, k] and C∈D[k+1,j]C \in D[k + 1, j].

Proof. If such kk, BB and CC exist, then A⇒BC⇒∗wi,k C⇒∗wi,k wk+1,j=wi,jA \Rightarrow BC \Rightarrow^* w_{i,k}\, C \Rightarrow^* w_{i,k}\, w_{k+1,j} = w_{i,j}. Conversely, let A⇒∗wi,jA \Rightarrow^* w_{i,j} with j−i+1≥2j - i + 1 \ge 2. As in Lemma 1, the first step is not A→εA \to \varepsilon and not A→aA \to a, since those derive strings of length 0 and 1. So it is some A→BCA \to BC, and BC⇒∗wi,jBC \Rightarrow^* w_{i,j}. By the splitting lemma (see the derivation page), wi,j=xyw_{i,j} = xy with B⇒∗xB \Rightarrow^* x and C⇒∗yC \Rightarrow^* y. Both xx and yy are nonempty, because BB and CC are not SS (as SS is on no right side) and so derive no empty string. Let k=i+∣x∣−1k = i + |x| - 1; then i≤k<ji \le k < j, x=wi,kx = w_{i,k} and y=wk+1,jy = w_{k+1,j}. □\square

Claim. When line L11 is reached, T[i,j]=D[i,j]T[i, j] = D[i, j] for all 1≤i≤j≤n1 \le i \le j \le n, and CYK returns true if and only if w∈L(G)w \in L(G).

Proof. We show by induction on ℓ=j−i+1\ell = j - i + 1 that after the iteration of line L4 for length ℓ\ell (or after line L3 for ℓ=1\ell = 1), every cell T[i,j]T[i, j] with j−i+1=ℓj - i + 1 = \ell equals D[i,j]D[i, j], and that no later line changes it.

Base, ℓ=1\ell = 1. Line L3 sets T[i,i]={A:A→ai∈P}T[i, i] = \{A : A \to a_i \in P\}, which is D[i,i]D[i, i] by Lemma 1. Later lines write only cells with j>ij > i.

Step, ℓ≥2\ell \ge 2. Assume the claim for all lengths less than ℓ\ell. Fix ii and j=i+ℓ−1j = i + \ell - 1. Line L6 sets T[i,j]=∅T[i, j] = \emptyset, and only the iterations for this (i,j)(i, j) write to T[i,j]T[i, j], since each later iteration has another pair (i,j)(i, j). Lines L7 to L10 add AA exactly when some kk with i≤k≤j−1i \le k \le j - 1 and some A→BCA \to BC have B∈T[i,k]B \in T[i, k] and C∈T[k+1,j]C \in T[k + 1, j]. The cells T[i,k]T[i, k] and T[k+1,j]T[k + 1, j] have lengths k−i+1k - i + 1 and j−kj - k, both between 1 and ℓ−1\ell - 1, so by the induction hypothesis they equal D[i,k]D[i, k] and D[k+1,j]D[k + 1, j]. By Lemma 2, the set of added nonterminals is exactly D[i,j]D[i, j].

For n≥1n \ge 1 the claim gives T[1,n]=D[1,n]T[1, n] = D[1, n], and line L11 returns true if and only if S⇒∗wS \Rightarrow^* w. For n=0n = 0, line L1 returns true if and only if S→ε∈PS \to \varepsilon \in P. In Chomsky normal form this is the only way to derive ε\varepsilon: a derivation that starts with S→aS \to a or S→BCS \to BC yields a string of length at least 1, as shown in Lemma 1. □\square

The parse tree. Line L10 sets back[i,j,A]=(k,B,C)back[i, j, A] = (k, B, C) only when A→BC∈PA \to BC \in P, B∈T[i,k]B \in T[i, k] and C∈T[k+1,j]C \in T[k + 1, j], and for i=ji = j the leaf comes from A→ai∈PA \to a_i \in P. Define the tree of (i,j,A)(i, j, A) with A∈T[i,j]A \in T[i, j] as the leaf node AA over aia_i if i=ji = j, and otherwise as the node AA with the trees of (i,k,B)(i, k, B) and (k+1,j,C)(k + 1, j, C) as children. By induction on j−ij - i, this is well defined (both children satisfy B∈T[i,k]B \in T[i, k] and C∈T[k+1,j]C \in T[k + 1, j], with shorter spans), it uses only productions of PP, and its leaves spell wi,jw_{i,j}. So the tree of (1,n,S)(1, n, S) is a parse tree of ww. Each internal node with two children splits its span into two nonempty parts, so the tree has nn nodes of the form A→aA \to a and n−1n - 1 binary nodes, and its construction takes O(n)O(n) time.

Termination. All loops are bounded for loops over finite ranges and the finite set PP, so CYK stops after (n+13)\binom{n+1}{3} runs of line L8. ■\blacksquare

Back to CYK algorithm