Home › GATE CSE › computerscience › Theory of Computation › The CYK algorithm parses a CFG in CNF. Its time …
The CYK algorithm parses a CFG in CNF. Its time complexity for a string of length $n$ is
A$O(n)$
B$O(n \log n)$
C$O(n^3)$
D$O(2^n)$
Answer & Solution
Correct answer: C. $O(n^3)$
1. CYK (Cocke-Younger-Kasami) parses by DYNAMIC PROGRAMMING.
2. Build a 2D table $T[i][j]$ where $T[i][j]$ = set of non-terminals that can derive the substring from position $i$ to $j$.
3. Fill the table bottom-up:
- For length-1 substrings: $T[i][i] = \{A : A \to s_i \in R\}$.
- For longer substrings $[i,j]$: try every split $k$ in $i \leq k < j$ and every rule $A \to BC$ with $B \in T[i][k]$ and $C \in T[k+1][j]$.
4. Complexity:
- $O(n^2)$ substrings (cells in the table)
- For each cell, $O(n)$ split points
- For each split, $O(|R|)$ rules
5. Total: $O(n^3 \cdot |R|)$. With $|R|$ treated as constant: $O(n^3)$.
6. Faster algorithms exist for specific grammar classes (LL parsing is linear for LL(k) grammars). $O(n^3)$ is the general CFG parsing complexity.
_Source: Jeff Erickson, "Models of Computation", §5.9 (CYK Parsing Algorithm)._
Related questions
Which class of grammars generates ALL recursively enumerable languages?What is a LEFTMOST DERIVATION?Which of the following languages is CONTEXT-FREE?Consider the grammar $S \to (S) \mid SS \mid \varepsilon$. The language generated isEvery regular language is also context-free. The PROOF construction isA LEFT-RECURSIVE rule in a CFG has the form $A \to A\alpha$ for some non-empty $\alpha$. WContext-free languages are CLOSED under which of the following operations?Is the language $L = \{a^n b^n c^n \mid n \geq 0\}$ context-free?