CYK Algorithm
The CYK algorithm is a classic bottom-up dynamic programming method for determining whether a string belongs to a context-free language and constructing its parse trees in cubic time.
The CYK algorithm (named after its independent co-inventors John Cocke, Daniel Younger, and Tadao Kasami) is a bottom-up dynamic programming algorithm that decides membership and constructs parse trees for any context-free grammar (CFG) expressed in Chomsky Normal Form (CNF). Developed during the 1960s (Younger, 1967), the algorithm evaluates all contiguous substrings of length 1 through n in a triangular parse table, achieving a worst-case time complexity of O(nยณ ยท |G|) and space complexity of O(nยฒ ยท |G|), where n is the length of the input string and |G| is the size of the grammar.
Formal Grammar Prerequisites: Chomsky Normal Form
Before running the CYK algorithm, an arbitrary context-free grammar G = (V, ฮฃ, R, S) must be converted into Chomsky Normal Form (Chomsky, 1959), in which every production rule takes one of two restricted shapes:
- Binary non-terminal rules: A โ BC, where A, B, and C are non-terminal symbols in V (and B, C are not the start symbol S).
- Terminal rules: A โ a, where a is a single terminal symbol in ฮฃ (with S โ ฮต permitted only if the empty string belongs to the language).
Converting an arbitrary CFG to CNF eliminates ฮต-productions, unit rules (A โ B), and long right-hand sides by introducing auxiliary non-terminals, increasing grammar size by at most a polynomial factor while preserving the exact generated language.
Dynamic Programming Table and Recurrence
Bottom-Up Substring Indexing
Given an input string w = aโaโยทยทยทaโ of length n, the CYK algorithm maintains a two-dimensional triangular table P[i, l], where i โ {1, ..., n} denotes the starting position and l โ {1, ..., n โ i + 1} denotes the substring length. Each entry P[i, l] stores the set of non-terminals A โ V that can derive the substring w[i .. i + l โ 1].
Base Case and Inductive Step
- Length l = 1 (Base Case): For each position i from 1 to n, populate P[i, 1] with all non-terminals A such that A โ aแตข is a production rule in R.
- Lengths l = 2 to n (Inductive Step): For each starting index i from 1 to n โ l + 1 and each split point k from 1 to l โ 1, inspect pairs (B, C) such that B โ P[i, k] and C โ P[i + k, l โ k]. Whenever a binary rule A โ BC exists in R, insert A into P[i, l].
- Acceptance: The input string w belongs to the language L(G) if and only if the start symbol S appears in the top cell P[1, n].
By recording backpointers alongside each non-terminal added to P[i, l], the algorithm reconstructs one or all valid parse treesโor a compact shared packed parse forest (SPPF) for ambiguous grammarsโin polynomial space.
Complexity Analysis and Probabilistic Extensions
Worst-Case Bounds and Matrix Multiplication Reduction
Because the triple nested loop iterates over substring length l (n steps), starting index i (O(n) steps), and split position k (O(n) steps), standard CYK executes in ฮ(nยณ ยท |G|) time for general CNF grammars. In 1975, Leslie Valiant proved that general context-free recognition can be reduced to Boolean matrix multiplication, improving the theoretical asymptotic bound in n to O(nฯ), where ฯ < 2.372 is the matrix-multiplication exponent, though standard cubic CYK remains faster in practice for moderate string lengths due to low constant factors.
| Feature | CYK Algorithm | Earley Parser | LL / LR Parsers | ||
|---|---|---|---|---|---|
| Grammar Class | Arbitrary CFG (in CNF) | Arbitrary CFG (no CNF needed) | Deterministic subclasses (LL(k), LR(k)) | ||
| Strategy | Bottom-up dynamic programming | Top-down chart parsing with prediction | Predictive / shift-reduce deterministic | ||
| Worst-Case Time | O(nยณ ยท | G | ) | O(nยณ) general; O(nยฒ) unambiguous | O(n) linear time |
| Natural Language Use | Probabilistic CFG (PCFG) parsing | Ambiguous chart parsing | Compiler front-ends (rare in NLP) |
Probabilistic CYK in Computational Linguistics and Bioinformatics
In natural language processing, Probabilistic CYK extends each table entry P[i, l] to store the maximum-probability derivation (Viterbi-style maximization) or total inside probability for each non-terminal under a Probabilistic Context-Free Grammar (PCFG). In computational biology, the same cubic dynamic-programming structure underlies RNA secondary-structure prediction and stochastic context-free grammar (SCFG) covariance models, where base-paired stems correspond to nested context-free productions folded across nucleic-acid sequences to identify conserved non-coding RNAs and riboswitches across microbial, plant, and eukaryotic genomes worldwide in comparative genomics.
Worked Triangular Table Example
Consider a CNF grammar G with start symbol S and rules S โ AB | BC, A โ BA | a, B โ CC | b, C โ AB | a, tested against input string w = baaba (n = 5):
- Row l = 1 (Terminal Base Case): The five single-character cells are populated directly from terminal productions: P[1, 1] = {B}, P[2, 1] = {A, C}, P[3, 1] = {A, C}, P[4, 1] = {B}, and P[5, 1] = {A, C}.
- Row l = 2 (Pairs): For w[1..2] = ba, combining P[1, 1] = {B} and P[2, 1] = {A, C} matches A โ BA and S โ BC, giving P[1, 2] = {S, A}. Similarly, P[2, 2] = {B} (from CC), P[3, 2] = {S, C} (from AB and CB), and P[4, 2] = {S, A} (from BA and BC).
- Rows l = 3 to 5 (Bottom-Up Completion): Testing every split k โ {1, ..., l โ 1} populates P[1, 3] = โ , P[2, 3] = {S, B}, P[3, 3] = {B}, then P[1, 4] = {S, A}, P[2, 4] = {S, A, C}, and finally the apex cell P[1, 5] = {S, A, C}. Because S โ P[1, 5], w = baaba belongs to L(G), and following stored split indices k reconstructs every valid binary parse tree.
Frequently Asked Questions
Why does the CYK algorithm require Chomsky Normal Form?
Every inductive step splits a substring of length l into exactly two adjacent non-empty substrings (w[i..i+kโ1] and w[i+k..i+lโ1]), which corresponds directly to binary rules A โ BC. Eliminating ฮต-rules and unary chain rules A โ B ensures that every subproblem references strictly shorter substrings, preventing cyclic dependencies inside each table cell.
How can Earley's algorithm parse arbitrary CFGs in the same cubic worst-case time without CNF conversion?
Earley's parser maintains dotted production items along a chart and handles unary rules, ฮต-productions, and arbitrary right-hand-side lengths dynamically via Predictor, Scanner, and Completer operations, achieving O(nยณ) worst-case time (and O(nยฒ) on unambiguous grammars) without expanding the grammar into Chomsky Normal Form beforehand.
References
- Younger, D. H. (Feb 1967). Recognition and parsing of context-free languages in time nยณ. Information and Control, 10(2), 189โ208. https://doi.org/10.1016/S0019-9958(67)80007-X
- Chomsky, N. (Jun 1959). On certain formal properties of grammars. Information and Control, 2(2), 137โ167. https://doi.org/10.1016/S0019-9958(59)90362-6
- Valiant, L. G. (Apr 1975). General context-free recognition in less than cubic time. Journal of Computer and System Sciences, 10(2), 308โ315. https://doi.org/10.1016/S0022-0000(75)80046-8