東京大学 情報理工学系研究科 コンピュータ科学専攻 2022年8月実施 専門科目 問題1
Author
zephyr , 祭音Myyura
Description
Let Σ \Sigma Σ be the set { a , b } \{a, b\} { a , b } of letters. For a language L ⊆ Σ ∗ L \subseteq \Sigma^* L ⊆ Σ ∗ over Σ \Sigma Σ , we define Γ ( L ) \Gamma(L) Γ ( L ) as follows.
Γ ( L ) = { v ∈ Σ ∗ ∣ ∃ w ∈ Σ ∗ ⋅ ( ∣ v ∣ = ∣ w ∣ ∧ v w ∈ L ) } . \Gamma(L) = \{v \in \Sigma^* \mid \exists w \in \Sigma^* \cdot (|v| = |w| \land vw \in L)\}. Γ ( L ) = { v ∈ Σ ∗ ∣ ∃ w ∈ Σ ∗ ⋅ ( ∣ v ∣ = ∣ w ∣ ∧ v w ∈ L )} .
Here, ∣ x ∣ |x| ∣ x ∣ denotes the length of the string x x x . For example, if L 1 = { a a , b a , a b b , a b b b } L_1 = \{aa, ba, abb, abbb\} L 1 = { aa , ba , abb , abbb } , then Γ ( L 1 ) = { a , b , a b } \Gamma(L_1) = \{a, b, ab\} Γ ( L 1 ) = { a , b , ab } .
Answer the following questions.
(1) Let L 2 = { ( a b ) n ∣ n ≥ 0 } L_2 = \{(ab)^n \mid n \geq 0\} L 2 = {( ab ) n ∣ n ≥ 0 } . Express Γ ( L 2 ) \Gamma(L_2) Γ ( L 2 ) using a regular expression.
(2) Let L 3 = { a n b n a m b m ∣ n ≥ 0 , m ≥ 0 } L_3 = \{a^n b^n a^m b^m \mid n \geq 0, m \geq 0\} L 3 = { a n b n a m b m ∣ n ≥ 0 , m ≥ 0 } . Give a context-free grammar that generates Γ ( L 3 ) \Gamma(L_3) Γ ( L 3 ) .
(3) Let M = ( Q , Σ , δ , q 0 , F ) \mathcal{M} = (Q, \Sigma, \delta, q_0, F) M = ( Q , Σ , δ , q 0 , F ) be a deterministic finite automaton, and let L M L_{\mathcal{M}} L M be the language accepted by M \mathcal{M} M . Here, Q Q Q , Σ \Sigma Σ , δ \delta δ , q 0 q_0 q 0 , and F F F are the set of states, the transition function, the initial state, and the set of final states of M \mathcal{M} M , respectively. You may assume that the transition function δ : Q × Σ → Q \delta : Q \times \Sigma \to Q δ : Q × Σ → Q is total. Give a finite automaton that accepts Γ ( L M ) \Gamma(L_{\mathcal{M}}) Γ ( L M ) , with a brief explanation.
(4) If the proposition given below is true, then give how to construct a context-free grammar that generates Γ ( L ) \Gamma(L) Γ ( L ) , from a context-free grammar that generates L L L (you may use push-down automata instead of context-free grammars), with a brief explanation. Otherwise, give a counterexample, with a brief explanation.
Proposition: "For every context-free language L ⊆ Σ ∗ L \subseteq \Sigma^* L ⊆ Σ ∗ , Γ ( L ) \Gamma(L) Γ ( L ) is a context-free language."
设 Σ \Sigma Σ 为字母集 { a , b } \{a, b\} { a , b } 。对于 Σ \Sigma Σ 上的语言 L ⊆ Σ ∗ L \subseteq \Sigma^* L ⊆ Σ ∗ ,我们定义 Γ ( L ) \Gamma(L) Γ ( L ) 如下。
Γ ( L ) = { v ∈ Σ ∗ ∣ ∃ w ∈ Σ ∗ ⋅ ( ∣ v ∣ = ∣ w ∣ ∧ v w ∈ L ) } . \Gamma(L) = \{v \in \Sigma^* \mid \exists w \in \Sigma^* \cdot (|v| = |w| \land vw \in L)\}. Γ ( L ) = { v ∈ Σ ∗ ∣ ∃ w ∈ Σ ∗ ⋅ ( ∣ v ∣ = ∣ w ∣ ∧ v w ∈ L )} .
这里,∣ x ∣ |x| ∣ x ∣ 表示字符串 x x x 的长度。例如,如果 L 1 = { a a , b a , a b b , a b b b } L_1 = \{aa, ba, abb, abbb\} L 1 = { aa , ba , abb , abbb } ,那么 Γ ( L 1 ) = { a , b , a b } \Gamma(L_1) = \{a, b, ab\} Γ ( L 1 ) = { a , b , ab } 。
回答以下问题。
(1) 设 L 2 = { ( a b ) n ∣ n ≥ 0 } L_2 = \{(ab)^n \mid n \geq 0\} L 2 = {( ab ) n ∣ n ≥ 0 } 。用正则表达式表示 Γ ( L 2 ) \Gamma(L_2) Γ ( L 2 ) 。
(2) 设 L 3 = { a n b n a m b m ∣ n ≥ 0 , m ≥ 0 } L_3 = \{a^n b^n a^m b^m \mid n \geq 0, m \geq 0\} L 3 = { a n b n a m b m ∣ n ≥ 0 , m ≥ 0 } 。给出生成 Γ ( L 3 ) \Gamma(L_3) Γ ( L 3 ) 的上下文无关文法。
(3) 设 M = ( Q , Σ , δ , q 0 , F ) \mathcal{M} = (Q, \Sigma, \delta, q_0, F) M = ( Q , Σ , δ , q 0 , F ) 为一个确定性有限自动机,并且设 L M L_{\mathcal{M}} L M 为 M \mathcal{M} M 接受的语言。这里,Q Q Q , Σ \Sigma Σ , δ \delta δ , q 0 q_0 q 0 , 和 F F F 分别为 M \mathcal{M} M 的状态集合、字母表、转移函数、初始状态和终止状态集合。可以假设转移函数 δ : Q × Σ → Q \delta : Q \times \Sigma \to Q δ : Q × Σ → Q 是全定义的。给出一个接受 Γ ( L M ) \Gamma(L_{\mathcal{M}}) Γ ( L M ) 的有限自动机,并简要解释。
(4) 如果以下命题为真,请给出如何从生成 L L L 的上下文无关文法构造生成 Γ ( L ) \Gamma(L) Γ ( L ) 的上下文无关文法(可以使用下推自动机代替上下文无关文法),并简要解释。否则,请给出一个反例,并简要解释。
命题: " 对于每个上下文无关语言 L ⊆ Σ ∗ L \subseteq \Sigma^* L ⊆ Σ ∗ ,Γ ( L ) \Gamma(L) Γ ( L ) 是一个上下文无关语言。"
题目描述
令 Σ = { a , b } \Sigma=\{a,b\} Σ = { a , b } 。对语言 L ⊆ Σ ∗ L\subseteq\Sigma^* L ⊆ Σ ∗ ,定义
Γ ( L ) = { v ∈ Σ ∗ ∣ 存在 w ∈ Σ ∗ ,使 ∣ v ∣ = ∣ w ∣ 且 v w ∈ L } . \Gamma(L)
=\{v\in\Sigma^*\mid
\text{存在 }w\in\Sigma^*\text{,使 }|v|=|w|\text{ 且 }vw\in L\}. Γ ( L ) = { v ∈ Σ ∗ ∣ 存在 w ∈ Σ ∗ ,使 ∣ v ∣ = ∣ w ∣ 且 v w ∈ L } .
其中 ∣ x ∣ |x| ∣ x ∣ 是字符串长度。例如
L 1 = { a a , b a , a b b , a b b b } L_1=\{aa,ba,abb,abbb\} L 1 = { aa , ba , abb , abbb } 时,
Γ ( L 1 ) = { a , b , a b } \Gamma(L_1)=\{a,b,ab\} Γ ( L 1 ) = { a , b , ab } 。回答下列问题。
(1)令 L 2 = { ( a b ) n ∣ n ≥ 0 } L_2=\{(ab)^n\mid n\ge0\} L 2 = {( ab ) n ∣ n ≥ 0 } ,用正则表达式表示
Γ ( L 2 ) \Gamma(L_2) Γ ( L 2 ) 。
(2)令
L 3 = { a n b n a m b m ∣ n , m ≥ 0 } L_3=\{a^nb^na^mb^m\mid n,m\ge0\} L 3 = { a n b n a m b m ∣ n , m ≥ 0 } ,给出生成
Γ ( L 3 ) \Gamma(L_3) Γ ( L 3 ) 的上下文无关文法。
(3)设 DFA
M = ( Q , Σ , δ , q 0 , F ) \mathcal M=(Q,\Sigma,\delta,q_0,F) M = ( Q , Σ , δ , q 0 , F ) 识别语言 L M L_{\mathcal M} L M ,且
δ \delta δ 为全函数。构造识别 Γ ( L M ) \Gamma(L_{\mathcal M}) Γ ( L M ) 的有限自动机并简要说明。
(4)判断命题“对每个上下文无关语言 L ⊆ Σ ∗ L\subseteq\Sigma^* L ⊆ Σ ∗ ,
Γ ( L ) \Gamma(L) Γ ( L ) 仍为上下文无关语言”是否成立。若成立,说明如何由生成 L L L 的 CFG 构造生成
Γ ( L ) \Gamma(L) Γ ( L ) 的 CFG(也可改用 PDA);若不成立,给出反例并简要解释。
Kai
(1)
If ( a b ) n = v w (ab)^n=vw ( ab ) n = v w and ∣ v ∣ = ∣ w ∣ |v|=|w| ∣ v ∣ = ∣ w ∣ , then ∣ v ∣ = n |v|=n ∣ v ∣ = n . According as n n n is even or odd,
v = ( a b ) r or v = ( a b ) r a . v=(ab)^r\quad\text{or}\quad v=(ab)^r a. v = ( ab ) r or v = ( ab ) r a .
Hence
Γ ( L 2 ) = ( a b ) ∗ ( ε + a ) . \Gamma(L_2)=(ab)^*(\varepsilon+a). Γ ( L 2 ) = ( ab ) ∗ ( ε + a ) .
(2)
The midpoint of a n b n a m b m a^n b^n a^m b^m a n b n a m b m lies in the first b b b -block when m ≤ n m\le n m ≤ n , and in the second a a a -block when m > n m>n m > n . Thus
Γ ( L 3 ) = { a n b r ∣ n ≥ r ≥ 0 } ∪ { a n b n a r ∣ n , r ≥ 0 } . \Gamma(L_3)
=\{a^n b^r\mid n\ge r\ge0\}
\cup\{a^n b^n a^r\mid n,r\ge0\}. Γ ( L 3 ) = { a n b r ∣ n ≥ r ≥ 0 } ∪ { a n b n a r ∣ n , r ≥ 0 } .
It is generated by
S → A ∣ B C , A → a A ∣ a A b ∣ ε , B → a B b ∣ ε , C → a C ∣ ε . \begin{aligned}
S&\to A\mid BC,\\
A&\to aA\mid aAb\mid\varepsilon,\\
B&\to aBb\mid\varepsilon,\\
C&\to aC\mid\varepsilon.
\end{aligned} S A B C → A ∣ BC , → a A ∣ a A b ∣ ε , → a B b ∣ ε , → a C ∣ ε .
(3)
For R ⊆ Q R\subseteq Q R ⊆ Q , put
Pre ( R ) = { q ∈ Q ∣ ∃ c ∈ Σ : δ ( q , c ) ∈ R } . \operatorname{Pre}(R)=\{q\in Q\mid \exists c\in\Sigma:\delta(q,c)\in R\}. Pre ( R ) = { q ∈ Q ∣ ∃ c ∈ Σ : δ ( q , c ) ∈ R } .
Use the DFA with state set Q × 2 Q Q\times2^Q Q × 2 Q , initial state ( q 0 , F ) (q_0,F) ( q 0 , F ) , transition
( q , R ) → c ( δ ( q , c ) , Pre ( R ) ) , (q,R)\xrightarrow{c}(\delta(q,c),\operatorname{Pre}(R)), ( q , R ) c ( δ ( q , c ) , Pre ( R )) ,
and accepting states { ( q , R ) ∣ q ∈ R } \{(q,R)\mid q\in R\} {( q , R ) ∣ q ∈ R } . After reading v v v , its second component is
{ q ∈ Q ∣ ∃ w ∈ Σ ∣ v ∣ : δ ( q , w ) ∈ F } . \{q\in Q\mid \exists w\in\Sigma^{|v|}:\delta(q,w)\in F\}. { q ∈ Q ∣ ∃ w ∈ Σ ∣ v ∣ : δ ( q , w ) ∈ F } .
It therefore accepts exactly when some such w w w satisfies v w ∈ L M vw\in L_{\mathcal M} v w ∈ L M .
(4)
The proposition is false. Over Σ = { a , b } \Sigma=\{a,b\} Σ = { a , b } , regard
A = a a , B = a b , C = b a , D = b b A=aa,\qquad B=ab,\qquad C=ba,\qquad D=bb A = aa , B = ab , C = ba , D = bb
as fixed-length codewords, and let
L = { A n B n C i D 3 i ∣ n , i ≥ 1 } . L=\{A^nB^nC^iD^{3i}\mid n,i\ge1\}. L = { A n B n C i D 3 i ∣ n , i ≥ 1 } .
This is context-free. Intersecting its half-prefix language with the regular language
A + B + C + D A^+B^+C^+D A + B + C + D gives
Γ ( L ) ∩ A + B + C + D = { A n B n C n + 1 D ∣ n ≥ 1 } , \Gamma(L)\cap A^+B^+C^+D
=\{A^nB^nC^{n+1}D\mid n\ge1\}, Γ ( L ) ∩ A + B + C + D = { A n B n C n + 1 D ∣ n ≥ 1 } ,
because equality of the two half-lengths gives
2 n + i + 1 = n + 2 i 2n+i+1=n+2i 2 n + i + 1 = n + 2 i , hence i = n + 1 i=n+1 i = n + 1 . The language on the right is not context-free: fixed-length inverse decoding and removal of the final C , D C,D C , D would yield
{ A n B n C n ∣ n ≥ 1 } \{A^nB^nC^n\mid n\ge1\} { A n B n C n ∣ n ≥ 1 } . Since context-free languages are closed under intersection with regular languages, Γ ( L ) \Gamma(L) Γ ( L ) cannot be context-free.
Knowledge
RegularExpression ContextFreeGrammar DeterministicFiniteAutomaton ContextFreeLanguage
解题技巧和信息
Constructing Regular Expressions : Understanding the structure of the language is crucial to forming the correct regular expression.
Designing CFGs : Consider the form of prefixes when designing CFGs for Γ ( L ) \Gamma(L) Γ ( L ) .
DFA Construction : For automaton-based constructions, consider pairs of states to account for parallel processing of string halves.
Non-context-free Languages : Remember that some operations can lead to languages that are not context-free even if the original language is.
重点词汇
Prefix : 前缀
Context-free grammar (CFG) : 上下文无关文法
Deterministic finite automaton (DFA) : 确定性有限自动机
Regular expression : 正则表达式
参考资料
Hopcroft, J.E., Motwani, R., Ullman, J.D. (2006). Introduction to Automata Theory, Languages, and Computation . Addison-Wesley. Chapter 2, 5.
Sipser, M. (2012). Introduction to the Theory of Computation . Cengage Learning. Chapter 2, 4.