自动人形会梦见 NP 完全问题吗?(习题)

埃癸斯 (Aegis) 虽然是高性能的反暗影压制兵装 (Anti-Shadow Suppression Weapon),但她在逻辑推理方面并未得到特殊强化。在辰巳人工岛的月光馆学园插班入学后,埃癸斯常常感觉自己跟不上课程进度。

埃癸斯尤其不擅的学科是计算理论 (Theory of Computation);她认为,作为人工智慧的自己,底层是程式的逻辑;既然如此,自己无法完成计算理论作业的试题,和任何机器都解决不了停机问题是相同的原理。

作为特别课外活动部 (S.E.E.S, Specialized Extracurricular Execution Squad) 的 Leader,你察觉到埃癸斯只是在找借口。为了课外活动部的未来,你必须对她展开特别辅导。


Tutorial 1

Q1. Prove \((A^*)^+=(A^+)^*=A^*\)

Note that \(\epsilon \in A^*\). Thus, \((A^*)^+=(A^*)^*\).

Also, if \(B\subseteq C\), then \(B^*\subseteq C^*\). Thus, we immediately have \(A^*\subseteq (A^+)^* \subseteq (A^*)^*=(A^*)^+\).

Hence, it suffices to show that \((A^*)^*\subseteq A^*\). Suppose \(w\in (A^*)^*\)

Let \(w_1,w_2,...,w_k\) be such that \(w=w_1w_2...w_k\) and each \(w_i\in A^*\). For each \(i\), let \(w_{i,1},w_{i,2},...,w_{i,r_i}\) be such that \(w_i=w_{i,1}w_{i,2}...w_{i,r_i}\) and each \(w_{i,j}\in A\).

Thus, we have that \(w=w_{1,1}w_{1,2}...w_{1,r_1}w_{2,1}...w_{2,r_2}...w_{k,1}...w_{k,r_k}\), where each \(w_{i,j}\in A\). Thus, \(w\in A^*\).


Tutorial 2

Q1. For a DFA \(A=(Q,\Sigma,\delta,q_0,F)\), let \(\hat\delta\) be as defined in class. Show that \(\hat\delta(q,xy)=\hat\delta(\hat\delta(q,x),y)\), for all strings \(x,y\) over \(\Sigma^*\), and all states \(q\in Q\).

By induction on \(|y|\). Clearly, for \(y=\epsilon\), the statement holds.

Suppose the statement holds for \(y=w\). Then for \(y=wa\), with \(a\in \Sigma\), we have, \[ \begin{aligned} \hat\delta(q,xwa)&=\delta(\hat\delta(q,xw), a) \\ &= \delta(\hat\delta(\hat\delta(q, x), w), a) \\ &= \hat\delta(\hat\delta(q,x), wa) \\ &= \hat\delta(\hat\delta(q,x),y) \end{aligned} \] Q2. Prove \(L((R+S)^*)=L((R^*S^*)^*)\), for any regular expression \(R\) and \(S\).

Showing \(\subseteq\):

\(L(R)\subseteq L(R^*S^*)\) and \(L(S)\subseteq L(R^*S^*)\), therefore \(L(R+S)\subseteq L(R^*S^*)\).

Thus \(L((R+S)^*)\subseteq L((R^*S^*)^*)\).

Showing \(\supseteq\):

\(L((R^*S^*)^*)\subseteq L(((R+S)^*(R+S)^*)^*)\subseteq L(((R+S)^*)^*)=L((R+S)^*)\).

Therefore \(L(R+S)^*=L((R^*S^*)^*)\).

Misc. Notice \(q\in \text{Eclose}(q)\). Practice DFA to RegEx.


Tutorial 3

Q1. True or False. If \(L\) is a regular language, then \(L^R=\{x^R|x\in L\}\) is also a regular language.

First method. Given a regular expression \(S\), we show how to construct \(S^R\) such that \(L(S)^R=L(S^R)\).

Base case. \(a^R=a, \epsilon^R=\epsilon, \emptyset^R=\emptyset\).

Induction. Suppose the statement holds for \(A,B\), then for,

  • prove \(L((A+B)^R)=L(A+B)^R\) using property \((A+B)^R=A^R+B^R\)
  • prove \(L((A\cdot B)^R)=L(A\cdot B)^R\) using property \((A\cdot B)^R=B^R\cdot A^R\)
  • prove \(L((A^*)^R)=L(A^*)^R\) using property \((A^*)^R=(A^R)^*\)

Second method. Suppose \(A=(Q,\Sigma,\delta,q_0,F)\) is a DFA for \(L\). Construct an \(\epsilon\)-NFA for \(L^R\) as follows.

\(A^N=(Q\cup \{q_0'\}, \Sigma,\delta_N,q_0',\{q_0\})\), where

  • \(\delta_N(q_0',\epsilon)=F\)
  • for \(q\in Q,a\in \Sigma\), \(\delta_N(q,a)=\{q':\delta(q',a)=q\}\). Other transitions sets are \(\emptyset\).

By induction on \(|w|\) we show that, for \(q,q' \in Q\), \(\hat\delta(q,w)=q'\) iff \(q\in \delta_N(q',w^R)\). Thus \(\hat\delta(q_0,w)\in F\) iff \(q_0\in\cup_{q\in F}\hat{\delta_N}(q,w^R)\). Thus, \(A_N\) accepts \(L^R\).

Q2. True or False. If \(L_1\) is regular and \(L_2\subseteq L_1\), then \(L_2\) is regular.

Take \(L_1=\Sigma^*\), and \(L_2\) to be some non-regular subset of \(\Sigma^*\).

Acceptance is two-way: every string accepted by a DFA is in the language, and every string rejected by a DFA is not in the language.

Q3. (hard) For any language \(L\), define \(\text{HALF}(L)=\{w|(\exists u)[wu\in L \text{ and } |w|=|u|]\}\). Show that if \(L\) is regular, then \(\text{HALF}(L)\) is regular.

Let \(A=(\delta_A,Q,q_0,F)\) be a DFA for \(L\) with some alphabet \(\Sigma\).

Then define \(B\) as follows:

  • The states \(Q_B\) of \(B\) are of the form \([q,S]\) where \(q\in Q\) and \(S\subseteq Q\).
  • The initial state of \(B\) is \([q_0,F]\).
  • \(\delta_B([q,S],a)=[\delta_A(q,a),T]\) where \(T=\{p\in Q:\exists b\in \Sigma:\exists p'\in S:\delta_A(p,b)=p'\}\).
  • The accepting states of \(B\) are \(F_B=\{[q,S]:q\in S\}\).

Then we have the following invariant by construction: all reachable states \([q,S]\) after some input are such that \(q\) is the state that \(A\) would be in after reading that input, and the states in \(S\) are all those states such that there is a path from that state to an accepting state (in \(A\)) that has the same length as the input that was read.

So when we are in a state \([q,S]\) wIth \(q\in S\) after reading some input \(w_1\), we know that \(q\) is the state of \(A\) after reading \(w_1\) and as \(q\in S\) there is some input \(w_2\) with \(|w_1|=|w_2|\) that induces a path from \(q\) to an accepting state, which means that \(w_1w_2\in L\), and so \(w_1\in \text{HALF}(L)\).

Source: StackExchange - Automata | Prove that if L is regular than half(L) is regular too


Tutorial 4

Q1. Give a CFG for language \(L=\{w:\#(a\in w)=\#(b\in w)\}\), \(\Sigma=\{a,b\}\). \[ S\to aSb|bSa|SS|\epsilon \] Q2. Give an unambiguous CFG for the above language.

design three minimal states:

  • \(T\): balance state, where \(\#a=\#b\).
  • \(A\): imbalance state, where \(\#a=\#b+1\).
  • \(B\): imbalance state, where \(\#b=\#a +1\).

To eliminate ambiguity, we need to prevent partial derivations from achieving the state prematurely, that is, no non-empty proper prefix has the same property of the whole string. \[ \begin{aligned} &S\to \epsilon|TS \\ &T\to aB|bA \\ &A\to a|bAA \\ &B\to b|aBB \end{aligned} \] \(T\) only achieves \(\#a=\#b\) if if is fully expanded, so are \(A\), \(B\).

The recursive productions correct the initial imbalance without allowing it to balance out prematurely

参见上图,按照上述的递归结构,由位置 \((x,y)\) 开始,找到之后的第一个 \((x_T,y)\),把 \([x,x_T]\) 划分为 \(T\)。同理,找到之后的第一个 \((x_A,y+1)\),把 \([x,x_A]\) 划分为 \(A\);找到之后的第一个 \((x_B,y-1)\),把 \([x,x_B]\) 划分为 \(B\)


-----------------------------------そして、次の曲が始まるのです。-----------------------------------