MIT 18.404 — Lecture 7

Decision Problems for Automata and Grammars

By the end of this lecture, we will have gone over all content necessary for PSET #2 and our quiz next week.

§ Encodings

Definition. Given any object zz (e.g., an automaton, a graph, a string), let ⟨z⟩\langle z \rangle denote an encoding of zz into {0,1}∗\{\str{0}, \str{1}\}^*. Furthermore, let ⟨z1,z2,…,zk⟩\langle z_1, z_2, \dots, z_k \rangle denote an encoding of (⟨z1⟩,⟨z2⟩,…,⟨zk⟩)(\langle z_1 \rangle, \langle z_2 \rangle, \dots, \langle z_k \rangle) into a single string.

Remark. Note that ⟨z1,z2⟩\langle z_1, z_2 \rangle should probably not just be the concatenation of ⟨z1⟩\langle z_1 \rangle and ⟨z2⟩\langle z_2 \rangle. The encoding must be such that one can read the binary string ⟨z1,z2⟩\langle z_1, z_2 \rangle and recognize that it must be an encoding of two objects, not just ⟨z3⟩\langle z_3 \rangle for some random z3z_3.

The details of the encoding are not important—their only purpose is to let us take anything as binary input to a TM.

§ Algorithms on Automata

Turing machines are really strong—they can even run algorithms that answer questions about algorithms!

Problem. Consider the language ADFA:={⟨B,w⟩∣B is a DFA that accepts w}A_{\mathrm{DFA}} := \{\langle B, w \rangle \mid B \text{ is a DFA that accepts } w\}. Show that ADFAA_{\mathrm{DFA}} is decidable.

Solution: Recall that it suffices to define a TM decider MM for which ADFA=L(M)A_{\mathrm{DFA}} = L(M). Here's what MM does on input ss.

  1. Check that ss is of the form ⟨B,w⟩\langle B, w \rangle for some DFA BB and some string ww.

  2. Simulate BB on ww. (This takes finite time, because BB is a DFA that takes a finite string ww as input.)

  3. In the end, return Accept if BB returns Accept, and return Reject otherwise.

And this works, obviously.   ■\blacksquare

We're really abstracting away the details here. But because of the Church-Turing thesis—an algorithm is a Turing machine—we should have confidence that working at such a high level of abstraction will not cause any issues.

Remark. In case you're not convinced, here's a sketch of how we might implement “simulate BB on ww” in a TM.

  1. Use three tapes to hold each of ⟨B,w⟩\langle B, w \rangle, the current state q∈Qq \in Q, and ww with its head at the current position.

  2. At each step, do the following:

    1. Read the symbol a∈Σa \in \Sigma at tape 3.

    2. Scan the transition table on tape 1 for the entry whose state matches tape 2 and whose symbol is aa.

    3. Write the target state onto tape 2 and advance the tape 3 head.

  3. When all of ww has been read, scan FF on tape 1 for the state on tape 2.

Perhaps the individual steps above are more believably implementable by a TM.

Problem. Consider the language ANFA:={⟨B,w⟩∣B is an NFA that accepts w}A_{\mathrm{NFA}} := \{\langle B, w \rangle \mid B \text{ is an NFA that accepts } w\}. Show that ANFAA_{\mathrm{NFA}} is decidable.

Solution: Just take a TM decider MM that, on input ss, does the following:

Check that ss takes form ⟨B,w⟩\langle B, w \rangle, then convert the NFA BB to a DFA B′B', then simulate B′B' on ww as in the previous problem.   ■\blacksquare

Problem. Consider the language EDFA:={⟨B⟩∣B is a DFA and L(B)=∅}E_{\mathrm{DFA}} := \{\langle B \rangle \mid B \text{ is a DFA and } L(B) = \emptyset\}. Show that EDFAE_{\mathrm{DFA}} is decidable.

Solution: This is a path-finding problem—it asks the following:

Question. Is there a path from the start state to any accept state?

So we can just return Accept if and only if BFS from the start state finds no accept state.   ■\blacksquare

Problem. Consider the language EQDFA:={⟨A,B⟩∣A and B are DFAs with L(A)=L(B)}EQ_{\mathrm{DFA}} := \{ \langle A, B \rangle \mid A \text{ and } B \text{ are DFAs with } L(A) = L(B) \}. Show that EQDFAEQ_{\mathrm{DFA}} is decidable.

Solution: This one is less obvious. Recall from our “closure property constructions” from Lecture 1 that

L(C):=(L(A)∩L(B)‾)∪(L(A)‾∩L(B))L(C) := \left( L(A) \cap \overline{L(B)} \right) \cup \left( \overline{L(A)} \cap L(B) \right)

for some DFA CC, where CC is constructible from the DFAs AA and BB.

Then we just consider a TM that constructs the DFA CC, then runs the algorithm used for EDFAE_{\mathrm{DFA}}.   ■\blacksquare

§ Algorithms on Grammars and Turing Machines

Answering questions about grammars is a little harder. Here's a useful fact that we'll assume without proof.

Theorem. (Chomsky Normal Form) Any CFG can be expressed as a CFG in which all rules look like A→BCA \rightarrow BC or A→aA \rightarrow a. Furthermore, there is a TM-executable algorithm for converting any CFG into such a form.

Problem. Consider the language ACFG:={⟨G,w⟩∣G is a CFG that generates w}A_{\mathrm{CFG}} := \{\langle G, w \rangle \mid G \text{ is a CFG that generates } w\}. Show that ACFGA_{\mathrm{CFG}} is decidable.

Solution: First check that input ss takes form ⟨G,w⟩\langle G, w \rangle. Then convert GG into Chomsky Normal Form.

The point is that any derivation of ww must apply a rule exactly 2∣w∣−12|w| - 1 times. So it is now feasible to search through all derivations of length 2∣w∣−12|w| - 1 and Accept if and only if any derivation yields ww.   ■\blacksquare

Here's an interesting corollary to the above point.

Theorem. (CFLs are Decidable) Every context-free language is decidable.

Proof: We must show that if AA is a CFL generated by a CFG GG, then there is a TM MGM_G that decides AA. Well, just take MGM_G to be the TM that, on input ww, searches through the length-(2∣w∣−1)(2|w| - 1) derivations using the Chomsky Normal Form of GG.   ■\blacksquare

Problem. Consider the language ECFG:={⟨G⟩∣G is a CFG and L(G)=∅}E_{\mathrm{CFG}} := \{\langle G \rangle \mid G \text{ is a CFG and } L(G) = \emptyset\}. Show that ECFGE_{\mathrm{CFG}} is decidable.
For example, the CFG GG with only one rule S→aSS \rightarrow aS should be in ECFGE_{\mathrm{CFG}}.

Solution: Construct the CFG G′G' that results from replacing every terminal in GG with ϵ\epsilon. (So S→0S1S \rightarrow \str{0}S\str{1} becomes S→ϵSϵS \rightarrow \epsilon S \epsilon, say.) Then just run the algorithm used for ACFGA_{\mathrm{CFG}} to check whether G′G' generates ϵ\epsilon, and Accept if and only if it does not.   ■\blacksquare

… except, the algorithm for ACFGA_{\mathrm{CFG}} now must search through all derivations of length 2∣ϵ∣−1=−12|\epsilon| - 1 = -1. What does that mean?!

The reason for this confusion is that Chomsky Normal Form permits one more kind of rule: S→ϵS \to \epsilon, where SS is the start variable. So all the algorithm ACFGA_{\mathrm{CFG}} is doing is determining whether S→ϵS \rightarrow \epsilon is in the Chomsky Normal Form of G′G'.

And that's really unsatisfying—all the nontrivial work is offloaded to the conversion of G′G' into Chomsky Normal Form. But we don't have time to present a better proof; maybe next lecture.   ■\blacksquare

It turns out that, unlike with DFAs, the following languages are not decidable.

But if we replace “decidable” with “Turing-recognizable”, we find…

Problem. Show that ATMA_{\mathrm{TM}} is Turing-recognizable.

Solution: After checking that input ss takes form ⟨M,w⟩\langle M, w \rangle, just simulate MM on ww. In particular,

This just works.   ■\blacksquare