Are all context-free and regular languages efficiently decidable?

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)?

• Out of curiosity, where did you find this figure? It may help to have context to explain, as “efficient” is not a formal notion and different people might use it to mean different things. – Gilles 'SO- stop being evil' Mar 13 '12 at 16:10
• If "efficient" means "$\in \mathrm{P}$" (as is common), "efficient" does not mean "not a very long time" as polynomials yield huge values, too. Note a basic result in complexity is that there are infinite sequences of problems, each properly easier than the next. This holds both inside and outside of $\mathrm{P}$. – Raphael Mar 13 '12 at 16:33
• @Raphael: In this context, efficient is a class of languages which are decidable in polynomial time. I used "it could took a very long time" for decidable problems as opposed to undecidable ones which we cannot find solutions in any finite amount of time for. – Gigili Mar 13 '12 at 20:29
• the correct technical way to say this is that determining if w∈L where w is a word and L is a language is in P. ie/aka "language recognition" – vzn Sep 29 '15 at 17:45

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}$.

• You may want to mention the matrix multiplication algorithm for context-free gramamrs that has a better running time, and that this algorithm works very efficiently (linearly) on just about any practical context-free grammar: sciencedirect.com/science/article/pii/030439759190180A – Alex ten Brink Mar 13 '12 at 17:04
• @AlextenBrink I don't think this question asks for finer granularity than "polynomial or not". – Raphael Mar 13 '12 at 18:22
• Note that you don't need the minimal automaton, as long as it's deterministic. Running time will still be $O(n)$, since at each step there's a unique possible "next move". – Janoma Mar 13 '12 at 19:53
• In fact, for regular languages, you do not even explicitly need deterministic automata. You simulate all computations in parallel by keeping track of all states in that way mimicking the powerset construction. – Hendrik Jan Nov 3 '12 at 23:44
• @Dave: linear in the length of the input string, for a fixed regular language, like the other complexities given here. – Hendrik Jan Nov 4 '12 at 10:36

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.