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)._
Keep practising Theory of Computation
Browse all Theory of Computation questions for GATE CSE, or explore more Computer Science topics.
Related questions
Which class of grammars generates ALL recursively enumerable languages?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 isContext-free languages are RECOGNISED by which type of machine?A CFG is AMBIGUOUS ifConsider the CFG with rule $S \to aSb \mid \varepsilon$. The language generated isWhich of these is an example of an IRREGULAR (non-regular) language?