14

I came across this figure which shows that context-free and regular languages are (proper) subsets of efficient problems (supposedly $\mathrm{P}$). I perfectly understand that efficient problems are a subset of all decidable problems because we can solve them but it could take a very long time.

Why are all context-free and regular languages efficiently decidable? Does it mean solving them will not take a long time (I mean we know it without more context)?

enter image description here

Gigili
  • 2,213
  • 3
  • 22
  • 31

2 Answers2

17

Regular language membership can be decided in $\cal{O}(n)$ time by simulating the language's (minimal) DFA (which has been precomputed).

Context free language membership can be decided in $\cal{O}(n^3)$ by the CYK Algorithm.

There are decidable languages that are not in $\sf{P}$, such as those in $\sf{EXPTIME}\setminus \sf{P}$.

A.Schulz
  • 12,252
  • 1
  • 42
  • 64
Dave Clarke
  • 20,345
  • 4
  • 70
  • 114
1

Refinement/"fine print" on answer by DC: all CFLs in the form of Chomsky Normal Form can be parsed efficiently with the CYK algorithm and all CFLs can be converted to CNF. However, converting an arbitrary CFL to CNF may take exponential space in worst case depending on some algorithms. (I am not aware of an algorithm that guarantees P-time conversion here, is anyone else? One must consider all edge/worst cases such as nondeterministic CFLs or ambiguous ones.) Wikipedia states on the CNF section Order of transformations

Moreover, the worst-case bloat in grammar size[note 4] depends on the transformation order. Using |G| to denote the size of the original grammar G, the size blow-up in the worst case may range from $|G|^2$ to $2^{2 |G|}$, depending on the transformation algorithm used.[6]:7

Therefore it seems there may exist CFLs that are not efficiently parseable. Most programming languages are efficiently convertable to CNF (or perhaps mostly defined in CNF or near-CNF) therefore CFL parsing for "typical" languages is "practically" in P. There is probably some modern research into this worst case complexity (but did not find recent papers on it on cursory search). Eg this older (1973) research paper by Greibach also seems to indicate that worst case performance may not bounded by P. see eg.

vzn
  • 11,162
  • 1
  • 28
  • 52