Algorithm Search⌘K

Proofs: Undecidability of CFG equivalence

These are the human proofs of Undecidability of CFG equivalence, at full length.

Proof that equivalence of context-free grammars is undecidable full

This proof shows: statement.

Let (x,y)(x, y) be an instance of Post's correspondence problem with lists x=(x1,…,xm)x = (x_1, \ldots, x_m) and y=(y1,…,ym)y = (y_1, \ldots, y_m) of nonempty strings over Σ\Sigma. Let I={1,…,m}I = \{1, \ldots, m\} be new symbols and Δ=Σ∪I\Delta = \Sigma \cup I. For a sequence s=(i1,…,ik)s = (i_1, \ldots, i_k) write xs=xi1⋯xikx_s = x_{i_1} \cdots x_{i_k} and s←=ik⋯i1\overleftarrow{s} = i_k \cdots i_1. Let

Lx={xs s←:s∈Ik, k≥1},L_x = \{ x_s\, \overleftarrow{s} : s \in I^k,\ k \ge 1 \},

and LyL_y likewise. By Lemma 2 of the proof for intersections, Lx∩Ly≠∅L_x \cap L_y \ne \emptyset if and only if the instance has a solution. We write L‾=Δ∗∖L\overline{L} = \Delta^* \setminus L.

The grammar for the complement. Let Gx‾G_{\overline{x}} have start symbol CC and the productions

C→Q∣D,D→xi D i∣Y∣Zi i(i∈I),C \to Q \mid D, \qquad D \to x_i\, D\, i \mid Y \mid Z_i\, i \quad (i \in I),

where QQ generates the regular language Δ∗∖Σ∗I+\Delta^* \setminus \Sigma^* I^+, YY generates Σ+\Sigma^+, and ZiZ_i generates the regular language (Σ∗∖xiΣ∗) I∗(\Sigma^* \setminus x_i \Sigma^*)\, I^*, each by a right-linear grammar built from a finite automaton. The construction is computable from xx.

Lemma 1. L(D)={xj1⋯xjr z jr⋯j1:r≥0, z∈Σ+∪⋃i∈I(Σ∗∖xiΣ∗)I∗ i}L(D) = \{ x_{j_1} \cdots x_{j_r}\, z\, j_r \cdots j_1 : r \ge 0,\ z \in \Sigma^+ \cup \bigcup_{i \in I} (\Sigma^* \setminus x_i \Sigma^*) I^*\, i \}.

Proof. By induction on the number of uses of D→xiDiD \to x_i D i: each wraps the current string in xi…ix_i \ldots i, and the last step uses D→YD \to Y or D→Zi iD \to Z_i\, i. □\square

Lemma 2. L(Gx‾)=Lx‾L(G_{\overline{x}}) = \overline{L_x}.

Proof. (⊆\subseteq) Strings from QQ are not in Σ∗I+⊇Lx\Sigma^* I^+ \supseteq L_x. Let w∈L(D)w \in L(D), written as in Lemma 1 with j=(j1,…,jr)j = (j_1, \ldots, j_r), and suppose w∈Lxw \in L_x, say w=xss←w = x_s \overleftarrow{s}. The longest suffix of ww in I∗I^* is s←\overleftarrow{s}.

  • If z∈Σ+z \in \Sigma^+, then w=xj z j←w = x_j\, z\, \overleftarrow{j}, and its longest suffix in I∗I^* is j←\overleftarrow{j}, because zz ends with a symbol of Σ\Sigma. So s=js = j and xs=xjzx_s = x_j z, which gives z=εz = \varepsilon, a contradiction. (If r=0r = 0, w=zw = z has no index symbol and is not in LxL_x.)
  • If z=u v iz = u\, v\, i with u∈Σ∗∖xiΣ∗u \in \Sigma^* \setminus x_i \Sigma^* and v∈I∗v \in I^*, then w=xj u v i j←w = x_j\, u\, v\, i\, \overleftarrow{j}, and its longest suffix in I∗I^* is v i j←v\, i\, \overleftarrow{j}. So s←=v i j←\overleftarrow{s} = v\, i\, \overleftarrow{j}, that is, s=(j1,…,jr,i,…)s = (j_1, \ldots, j_r, i, \ldots) with the reverse of vv at the end, and xs=xj xi xtx_s = x_j\, x_i\, x_{t} for the sequence tt read from vv. Comparing with w=xss←w = x_s \overleftarrow{s} gives u=xi xt∈xiΣ∗u = x_i\, x_t \in x_i \Sigma^*, a contradiction.

(⊇\supseteq) Let w∉Lxw \notin L_x. If w∉Σ∗I+w \notin \Sigma^* I^+, then w∈L(Q)w \in L(Q). Otherwise w=u s←w = u\, \overleftarrow{s} with u∈Σ∗u \in \Sigma^* and s=(i1,…,ik)s = (i_1, \ldots, i_k), k≥1k \ge 1, and u≠xsu \ne x_s. Let rr be the largest number with 0≤r≤k0 \le r \le k such that xi1⋯xirx_{i_1} \cdots x_{i_r} is a prefix of uu, and write u=xi1⋯xir u′u = x_{i_1} \cdots x_{i_r}\, u'.

  • If r=kr = k, then u′≠εu' \ne \varepsilon because u≠xsu \ne x_s, and w=xi1⋯xik u′ ik⋯i1w = x_{i_1} \cdots x_{i_k}\, u'\, i_k \cdots i_1 is derived by kk uses of D→xiDiD \to x_i D i and then D→YD \to Y with Y⇒∗u′Y \Rightarrow^* u'.
  • If r<kr < k, then u′u' does not start with xir+1x_{i_{r+1}}, by the choice of rr. So w=xi1⋯xir (u′ ik⋯ir+2) ir+1 ir⋯i1w = x_{i_1} \cdots x_{i_r}\, (u'\, i_k \cdots i_{r+2})\, i_{r+1}\, i_r \cdots i_1, and the part in parentheses is in (Σ∗∖xir+1Σ∗)I∗(\Sigma^* \setminus x_{i_{r+1}} \Sigma^*) I^*, the language of Zir+1Z_{i_{r+1}}. So ww is derived by rr uses of D→xiDiD \to x_i D i and then D→Zir+1 ir+1D \to Z_{i_{r+1}}\, i_{r+1}. □\square

Theorem. Universality, equivalence, inclusion, equality with a regular language and inclusion of a regular language in a context-free one are undecidable, and each is complete for the co-recursively-enumerable sets.

Proof. The reduction. Given (x,y)(x, y), compute Gx‾G_{\overline{x}} and Gy‾G_{\overline{y}} with disjoint nonterminals and let GG have the new start symbol SS with S→Cx∣CyS \to C_x \mid C_y. By Lemma 2,

L(G)=Lx‾∪Ly‾=Lx∩Ly‾,L(G) = \overline{L_x} \cup \overline{L_y} = \overline{L_x \cap L_y},

so L(G)=Δ∗L(G) = \Delta^* if and only if Lx∩Ly=∅L_x \cap L_y = \emptyset, if and only if the instance has no solution. This is a many-one reduction from the complement of PCP to universality. The complement of PCP is complete for the co-recursively-enumerable sets, since PCP is complete for the recursively enumerable sets. So universality is hard for that class.

The other problems. Let GΔG_\Delta be the grammar S→aS∣εS \to a S \mid \varepsilon (a∈Δa \in \Delta) and EΔE_\Delta a regular expression for Δ∗\Delta^*. Then L(G)=Δ∗L(G) = \Delta^* if and only if L(G)=L(GΔ)L(G) = L(G_\Delta), if and only if L(GΔ)⊆L(G)L(G_\Delta) \subseteq L(G), if and only if L(G)=L(EΔ)L(G) = L(E_\Delta), if and only if L(EΔ)⊆L(G)L(E_\Delta) \subseteq L(G). So universality reduces to each of the problems 2 to 5, and each is hard.

Membership in the class. For each problem, a negative answer has a finite witness: a string in one language and not in the other. Membership of a string in a context-free or regular language is decidable, so listing all strings of Δ∗\Delta^* and testing each finds a witness exactly when the answer is negative. So the complement of each problem is recursively enumerable.

Fixed alphabet. PCP remains undecidable for lists over Σ={a,b}\Sigma = \{a, b\}. Replace each index symbol ii by the code d cid\, c^i with new letters c,dc, d. Lemma 2 holds with I∗I^* and I+I^+ replaced by the regular sets {dci:i∈I}∗\{d c^i : i \in I\}^* and its nonempty version, and with "longest suffix in I∗I^*" replaced by the longest suffix in {c,d}∗\{c, d\}^*, which decodes uniquely because every code starts with dd. One also adds to QQ the strings whose suffix in {c,d}∗\{c, d\}^* is not a sequence of codes, which is regular. So universality is undecidable over {a,b,c,d}\{a, b, c, d\}, and equivalence is undecidable with G2G_2 fixed to the grammar for {a,b,c,d}∗\{a, b, c, d\}^*. ■\blacksquare

Back to Undecidability of CFG equivalence