DD2372 Automata and Languages – Problems from previous exams – (based on Kozen’s book) Dilian Gurov Royal Institute of Technology – KTH e–mail:
[email protected]
1
Fini Finite te Auto Automa mata ta and and Regul Regular ar Lan Langua guage gess 1.1
Simple Simple Constr Construct uction ionss
1. Give a deterministic finite automaton over over the alphabet {a, b} which accepts all strings containing no more than two consecutive consecutive occurrences occurrences of the same input letter. letter. (For (For example, abba should be accepted but not abaaab.) abaaab.) 2. Consider Consider the language over over the alphabet {a, b} consisting of all strings that do not contain two or more consecutive a’s and do not end with b. (a) Constr Construct uct a deterministic finite automaton (DFA) that that accept acceptss this this languag language. e. Draw Draw its automaton (DFA) graph. (b) Sugges Suggestt a regular expression that generates this language. Solution: For example, the regular expression (a ( a + )(bb )(bb∗ a)∗ . 3. Conver Convertt the nondeterminist nondeterministic ic automaton automaton given given belo b elow w to an equivalen equivalentt determinis deterministic tic one using the subset construction. Omit inaccessible states. Draw the graph of the resulting DFA.
→ →
a {q 2 } ∅ {q 2 , q 3 }
q 1 q 2 F q 3 F
b ∅ {q 1 , q 3 } {q 1 }
Solution: (presented as a table)
→ {q 1 , q 2 } {q 1 , q 3 } {q 2 , q 3 } {q 1 } {q 2 } ∅
F F F F
a {q 2 } {q 2 , q 3 } {q 2 , q 3 } {q 2 } ∅ ∅
b {q 1 , q 3 } {q 1 } {q 1 , q 3 } ∅ {q 1 , q 3 } ∅
4. Conver Convertt the nondeterminist nondeterministic ic automaton automaton given given belo b elow w to an equivalen equivalentt determinis deterministic tic one using the subset construction. Omit inaccessible states. Draw the graph of the resulting DFA.
→ →
q 1 q 2 q 3 q 4
a q 1 , q 2 − q 4 − 1
b q 3 q 4 − q 1 , q 4
F F
Solution: (given as a table)
→
{q 1 , q 3 } {q 1 , q 2 , q 4 } {q 1 , q 3 , q 4 } {q 1 , q 2 } {q 3 , q 4 } {q 1 , q 4 } {q 4 } {q 3 } {}
a {q 1 , q 2 , q 4 } {q 1 , q 2 } {q 1 , q 2 , q 4 } {q 1 , q 2 } {q 4 } {q 1 , q 2 } {} {q 4 } {}
b {q 3 } {q 1 , q 3 , q 4 } {q 1 , q 3 , q 4 } {q 3 , q 4 } {q 1 , q 4 } {q 1 , q 3 , q 4 } {q 1 , q 4 } {} {}
F F F F F
5. Conver Convertt the nondeterminist nondeterministic ic automaton automaton given given belo b elow w to an equivalen equivalentt determinis deterministic tic one using the subset construction construction (textbook (textbook Lecture Lecture 6). Omit inaccessibl inaccessiblee states. Draw Draw the graph of the resulting DFA. DFA.
→ →
q 0 q 1 F q 2 F
a {q 2 } {q 0 , q 2 } ∅
b ∅ ∅ {q 1 , q 2 }
Solution: (as a table)
→
{q 0 , q 1 } {q 0 , q 2 } {q 1 , q 2 } {q 2 } ∅
F F F F
a {q 0 , q 2 } {q 2 } {q 0 , q 2 } ∅ ∅
b ∅ {q 1 , q 2 } {q 1 , q 2 } {q 1 , q 2 } ∅
6. Conver Convertt the nondeterminist nondeterministic ic automaton automaton given given belo b elow w to an equivalen equivalentt determinis deterministic tic one using the subset constructi construction, on, textbook Lecture 6. Omit inaccessibl inaccessiblee states. Draw the graph of the resulting DFA. DFA.
→
q 0 q 1 q 2 F
a {q 1 } {q 0 , q 1 } ∅
b {q 2 } {q 0 } {q 1 , q 2 }
Solution: Easy: has 8 states, and hence no inaccessible ones!
7. Give Give a (as simple as possibl possible) e) nondet nondeterm ermini inisti sticc finite finite automat automaton on for the language language defined defined by ∗ ∗ the regular regular expression expression ab + a(ba) ba) . Apply Apply the subset subset construc constructio tion n to obtain obtain an equiv equivale alent nt determinis deterministic tic automaton. Draw the graph of this DFA, DFA, omitting omitting inaccessib inaccessible le states. states. 8. Give Give a DFA DFA for the language defined by the regular expression expression a∗ a, and another one for a(ba) ba)∗ . The union of the two DFAs defines a nondeterministic FA for the language a∗ a + a(ba) ba)∗ . (a) Apply the subset constructi construction on to this NFA NFA to produce a DFA DFA for this language. language. Omit the inaccessible states. Draw the graph of the resulting DFA. (b) Is this DFA DFA minimal? If not, which states are equivalen equivalent? t?
1.2
Closur Closure e Properti Properties es of Regul Regular ar Langu Language agess
9. Show that regular regular languages languages are closed under doubling: doubling: If language L is regular, then so is the def def two = and language L2 = {two x | x ∈ L}, where string doubling is defined inductively by two def two two xa = (two two x) · aa. aa. Solution: Here is a standard solution using finite automata; alternative solutions exist using
regular expressions or homomorphisms. Let L be regular. Then there is an NFA N = (Q ( QN , Σ, ∆N , S N N ) = L. Define N , F N N ) such that L(N ) a another NFA, N 2 , as follow follows: s: star startt with with N and replace every edge q −→ q with two edges a a q −→ q and q −→ q by inserting (for each edge!) a new state q . We can formalize this idea by taking as states of N 2 the states of N plus the edges of N , represented ted for example example N , the latter represen as the set of triples {(q N ) | q N N , a , qN N ∈ QN , q N ∈ ∆N (q N N , a)}. So, we can define N 2 as follows: def
) | q ∈ Q , q ∈ ∆ (q , a)} • QN 2 = QN ∪ {(q N N , a , qN N N N N N N N
• ∆N 2 is given by the two defining equations: def ) | q ∈ ∆ (q , a)} and ∆N 2 (q N N , a) = QN ∪ {(q N N , a , qN N N N N def ∆N 2 ((q ((q N ), a) = if a = b then {q N } else ∅ N , b , qN def
• S N N2 = S N N def
• F N N2 = F N N It is straightforward to show that, for the so constructed NFA, L(N 2 ) = L2 , thus implying that L2 is regular. Hence, regular languages are closed under doubling. 10. Let A ⊆ Σ∗ be a language. We define its prefix closure as: pref A = {x ∈ Σ∗ | ∃y ∈ Σ∗ . x · y ∈ A}
Prove Prove that that regula regularr languag languages es are closed under prefixi prefixing: ng: if langua language ge A is regular, then so is pref A. Solution: Let A ⊆ Σ∗ be regular regular.. Then Then there there must must be a DFA DFA M A = (Q, Σ,δ,s,F ) such that
L(M A ) = A. Now, based on M A , define another automaton M = (Q, ( Q, Σ,δ,s,F ) so that:
ˆ(q, y) ∈ F F = q ∈ Q | ∃y ∈ Σ . δ
∗
We will show that this automaton accepts the language pref A, and hence that pref A is regular. M ) = pref A Claim. L(M ) Proof.
x ∈ L(M ) M ) ⇔ ⇔ ⇔ ⇔ ⇔
ˆ(s, x) ∈ F δ ˆ(δ ˆ(s, x), y) ∈ F ∃y ∈ Σ∗ . δ ˆ(s, x · y) ∈ F ∃y ∈ Σ∗ . δ ∃ y ∈ Σ∗ . x · y ∈ A x ∈ pref A
{def. L(M ) M )} {def. F } {HW 1.3, page 301} {L(M A ) = A} {def. pref A}
11. Show Show that the class of regula regularr languag languages es is closed under the following unary operation on languages: def min A = {x ∈ A | no proper prefix of x is in A} (a) Define a construction construction on finite automata that has the correspondin corresponding g effect on the accepted accepted language. ( Q, Σ,δ,s,F ) Solution: An important result about a deterministic finite automaton M = (Q, ˆ ˆ ˆ is that δ (s, x · y) = δ (δ (s, x), y ). So, if if both x and y are accepted by M , M , the unique path ˆ ˆ from s to δ (s, x · y) ∈ F has to pass state δ (s, x) ∈ F . F . Then, to elimiminate elimiminate all suffixes suffixes of words in L(M ), M ), one has to eliminate all paths starting (and ending) in accept states of M . M .
This This is easily easily achiev achieved ed by removin removingg all outgoing outgoing edges edges from all accept accept states states.. Note Note that that this results in a nondeterminstic finite automaton! So, we define: def
N = (Q, Σ, ∆, {s} , F ) F ) def F } ∆(q, ∆(q, a) = {δ (q, a) | q ∈ (b) Prove Prove the construction construction correct. ˆ {q } , x) equals Solution: (Sketch) The important result that is needed here is that ∆( ˆ(q, x) exactly when the unique path from q to ˆ(q, x) does not pass through an accepting δ q to δ state (since we removed all their outgoing edges), and is the empty set otherwise. Formally:
ˆ {q } , x) = ∆(
ˆ(q, x) δ
∅
ˆ(q, y) ∈ F if for no proper prefix y of x, δ otherwise
After proving this Lemma, it is straightforward to prove that x ∈ L(N ) N ) ⇔ x ∈ min L(M ). M ). 1.3
DFA DFA State State Minim Minimiza izatio tion n
12. For the determinist deterministic ic automaton given below, apply the minimization minimization algorithm of Lecture Lecture 14 to compute the equvalence classes of the collapsing relation ≈ defin defined ed in Lect Lectur uree 13. 13. Show Show clearly clearly the computation computation steps. List the equivalence equivalence classes, classes, and apply the quotient construction construction to derive a minimized automaton. Draw its graph.
→
q 0 q 1 q 2 q 3 q 4
a q 0 q 2 q 2 q 2 q 0
b q 1 q 3 q 3 q 4 q 1
F
Solution: (incomplete) After applying the minimization algorithm we obtain that q 0 ≈ q 4 and
q 1 ≈ q 2 . Thus we obtain the following minimized automaton (given as a table).
→
{q 0 , q 4 } {q 1 , q 2 } {q 3 }
a {q 0 , q 4 } {q 1 , q 2 } {q 1 , q 2 }
b {q 1 , q 2 } {q 3 } {q 0 , q 4 }
F
13. For the determinist deterministic ic automaton given below, apply the minimization minimization algorithm of Lecture Lecture 14 to compute the equvalence classes of the collapsing relation ≈ defin defined ed in Lect Lectur uree 13. 13. Show Show clearly clearly the computation computation steps. List the equivalence equivalence classes, classes, and apply the quotient construction construction to derive a minimized automaton. Draw its graph.
→
q 1 q 2 F q 3 q 4 F q 5 q 6 q 7 q 8
a q 3 q 3 q 8 q 5 q 6 q 7 q 6 q 5
b q 8 q 1 q 2 q 6 q 2 q 8 q 4 q 8
With the minimi minimizat zation ion algo algorit rithm hm we establi establish sh that that q 1 ≈ q 6 ≈ q 8 , q 2 ≈ q 4 and Solution: With q 3 ≈ q 5 ≈ q 7 . The resulting quotient automaton, presented as a table, is:
→ {q 1 , q 6 , q 8 } {q 3 , q 5 , q 7 } {q 2 , q 4 }
F
a {q 3 , q 5 , q 7 } {q 1 , q 6 , q 8 } {q 3 , q 5 , q 7 }
b {q 1 , q 6 , q 8 } {q 2 , q 4 } {q 1 , q 6 , q 8 }
14. For the deterministic deterministic automaton given given below, apply the minimization minimization algorithm algorithm of Lecture Lecture 14 of the textbook to compute the equvalence classes of the collapsing relation ≈ defined in Lecture 13.
→
a q 1 q 2 q 5 q 4 q 5 q 5
q 0 F q 1 q 2 F q 3 q 4 F q 5
b q 3 q 3 q 2 q 1 q 4 q 5
(a) Show Show clearly the computation steps (use tables). tables). (b) List the computed equivalence equivalence classes. classes. (c) Apply the quotient quotient construction construction of Lecture Lecture 13 to derive the minimized minimized automaton. automaton. Draw Draw its graph. Solution: As a table:
→
a {q 1 , q 3 } {q 2 , q 4 } {q 5 } {q 5 }
{q 0 } F {q 1 , q 3 } {q 2 , q 4 } F {q 5 }
b {q 1 , q 3 } {q 1 , q 3 } {q 2 , q 4 } {q 5 }
15. For the determinis deterministic tic automaton given below, apply the minimization minimization algorithm of textbook Lecture 14 to compute the equvalence classes of the collapsing relation ≈ defined in textbook Lecture 13.
→
q 0 q 1 q 2 F q 3 q 4 F
a q 2 q 0 q 4 q 0 q 4
b q 0 q 2 q 3 q 4 q 1
(a) Show Show clearly the computation steps (use tables). tables). (b) List the computed equivalence equivalence classes. classes. Solution: These are {q 0 }, {q 1 , q 3 } and {q 2 , q 4 }. (c) Apply the quotient quotient construction of textbook Lecture 13 to derive derive the minimized minimized automaton. Draw its graph. Solution: (as a table)
→
{q 0 } {q 1 , q 3 } {q 2 , q 4 }
F
a {q 2 , q 4 } {q 0 } {q 2 , q 4 }
b {q 0 } {q 2 , q 4 } {q 1 , q 3 }
16. For the determ determini inisti sticc automat automaton on given given below, below, apply apply the minimization algorithm of textbook Lecture 14 to compute the equvalence classes of the collapsing relation ≈ defined in textbook Lecture 13.
→
q 0 q 1 q 2 q 3 q 4 q 5 q 6 q 7
F F
F
F
a q 1 q 2 q 2 q 4 q 2 q 2 q 7 q 5
b q 3 q 3 q 5 q 6 q 6 q 5 q 3 q 6
(a) Show Show clearly the computation steps (use tables). tables). (b) List the computed equivalence equivalence classes. classes. Solution: The equivalence classes are: {q 0 }, {q 1 , q 4 , q 7 }, {q 2 , q 5 } and {q 3 , q 6 }. (c) Apply Apply the the quotient construction construction of textbook Lecture 13 to derive the minimized automaton. Draw its graph. Solution: (as a table)
→
1.4
{q 0 } F {q 1 , q 4 , q 7 } F {q 2 , q 5 } {q 3 , q 6 }
a {q 1 , q 4 , q 7 } {q 2 , q 5 } {q 2 , q 5 } {q 1 , q 4 , q 7 }
b {q 3 , q 6 } {q 3 , q 6 } {q 2 , q 5 } {q 3 , q 6 }
Myhil Myhill–N l–Nerod erode e Relati Relations ons and and Theore Theorem m
17. Consider Consider the language language L defined by the regular expression (a ( a∗ + ba) ba)∗ . Describe the equivalence classes of {a, b}∗ w.r.t. the Myhill–Nerode relation ≡L defined by: (cf. equation (16.1) on page 97) def
x1 ≡L x2 ⇐⇒ ∀y ∈ Σ∗ .(x1 · y ∈ L ⇔ x2 · y ∈ L) Present Present these equivalence equivalence classes through regular expressions. expressions. Use ≡L to construct a minimal automaton M ≡L (cf. page 91) for the language L, and draw the graph of the automaton. Solution: The states of the quotient automaton are:
Σ∗/≡L = {L((a ((a + ba) ba)∗ ), L((a ((a + ba) ba)∗ b), L((a ((a + ba) ba)∗ bb( bb(a + b)∗ )} 18. Consider Consider the regular regular language A over alphabet Σ = {a, b} defined through the regular expression ∗ (ab + b) . Recall the Myhill–Nerode Theorem , textbook Lecture 16, with the equivalence relation ≡A on strings over Σ defined by: (cf. equation (16.1) on page 97) x1 ≡A x2 ⇐⇒ ∀y ∈ Σ∗ .(x1 · y ∈ A ⇔ x2 · y ∈ A) def
(a) Show the equivalence equivalence classes of Σ∗ w.r.t. equivalence ≡A , represented as regular expressions. Solution: There There are three equiv equivale alence nce classes. classes. They They can be repres represen ented ted by the regula regularr ∗ ∗ ∗ ∗ expressions: (ab (ab + b) , (ab + b) a and (ab (ab + b) aa( aa(a + b) . (b) For every pair of (different (different)) equivalence equivalence classes classes A1 and A2, give the shortest distinguishing ∗ A for any x1 ∈ A1 experiment , by means of a string y ∈ Σ such that x1 · y ∈ A ⇔ x2 · y ∈ and x2 ∈ A2 . Solution: (ab + b)∗ is distinguished from (ab ( ab + b)∗ a by , (ab + b)∗ is distinguished from (ab + b)∗aa( aa(a + b)∗ by , and (ab (ab + b)∗ a is distinguished from (ab ( ab + b)∗ aa( aa(a + b)∗ by b.
19. Recall Recall the Myhill–Nerode Myhill–Nerode Theorem, textbook Lecture 16, with the equivalence equivalence relation relation ≡A on strings strings defined in equation equation (16.1). The latter gives rise to the following following technique technique for proving proving that the minimal DFA for a given regular language A over Σ has at least k states: Identify k strings x1 , . . . , xk over Σ, for which you can show that: A x j whenever i = j. xi ≡ Then ≡A has at least k equivalence equivalence classes, and hence the desired desired result. result. Now, let m be an arbitrary but fixed even number. Consider the language B over {a, b} consisting of all strings of length at least m which have an equal number of a of a’s and b’s in the last m positions. Use the technique described above to show that the minimal DFA for language B has at least m 2 2 states. Hint: Consider the strings of length m 2. Solution: Consider the set X of strings of length m y. 2 over {a, b}. Let x, y ∈ X so that x =
y . Then Let x , y be the shortest suffices of x and y respectively, such that |x | = |y | and x = a( obviously a( a(x ) = a(y ). Now let z = ar bs , where r = m a(x ) and s = m − (|x | + r). Then 2 − a( m m x · z ∈ B while y · z ∈ Sincee X has 2 2 elements, ≡B has at least 2 2 B , and hence x ≡ B y . Sinc m equivalence equivalence classes, and therefore therefore the minimal minimal DFA DFA for language language B has at least 2 2 states.
20. Consider Consider the language language family An = {x ∈ {a, b}∗ | for every prefix y of x, 0 ≤ a( a(y ) − b( b(y) ≤ n} def
Prove formally that for every n, the minimal DFA accepting An has exactly exactly n + 2 states. states. Solution: We know that the minimal DFA accepting An is unique up to isomorphism. isomorphism. Then,
one can prove the above result by exhibiting a DFA for An that has exactly n + 2 states, states, and and is minimal, in the sense that no two of its states are equivalent. For a given n, define the DFA def
M n = ({q 0 , . . . , qn +1 } , {a, b} , δ , q0 , {q 0 , . . . , qn }) def
def
def
def
where δ (q i , a) = q i+1 for 0 ≤ i ≤ n, δ (q i+1 , b) = q i for 0 ≤ i < n, δ (q 0 , b) = q n+1 , δ (q n+1 , a) = def q n+1, and δ (q n+1 , b) = q n+1 . M n has n + 2 states, and it is easy to see that M n accepts An .
q j with witness We now show that M n is minimal. minimal. For 0 ≤ i < j ≤ n, we have q i ≈ witness bi+1 (since ˆ(q i , bi+1 ) = q n+1 ∈ ˆ(q j , bi+1 ) = q j −(i+1) ∈ F ). F while δ q n+1 δ F ). And for 0 ≤ i ≤ n, we have q i ≈ with the obvious witness . 1.5
Provin Proving g Non–reg Non–regula ulari rity ty of a Langu Language age
21. Apply Apply the Pumpin Pumpingg Lem Lemma ma – in contra– contra–posi positiv tivee form, form, as a gam gamee with with the Demon Demon – to show that the following language: A = {an | n is a power of 2 } is not regular. Solution: One possible solution is:
(D) Demon pick pickss k . k
k+1
k
(W) We pick x = , y = a2 and z = a2 . Then xyz = a2
and |y | > k .
k
. (D) Demon pick pickss u, v and w so that uvw = y = a2 and v = (W) We pick i = 2. k+1
Then xuvi wz = a2 +l for some 0 < l ≤ 2k . Sinc Sincee l < 2k+1 we have 2k+1 < 2k+1 + l < 2k+2 , A. We have a winning strategy, and A is therefore not regular. and hence xuv i wz ∈
2
Pushdo Pushdown wn Automa Automata ta and Contex Context-F t-Free ree Langu Language agess 2.1
Combin Combined ed Proble Problems ms
1. Consider Consider the language: language: def
+
L = x · y ∈ {a, b} | y = rev x
where rev x denotes the reverse string of x (cf. HW 2.2, page 302). (a) Use the Pumping Pumping Lemma to prove prove that L is not regular. Solution: As a game with the Demon (cf. Lecture 11):
• Demon picks k > 0. • We pick for example x = , y = ak , z = bbak , and we have xyz = ak bbak ∈ L and |y | ≥ k. • Demon picks uvw = y = ak , v = . • We pick for example i = 0. A. We have a winning Then xuvi wz = uwz = a j bbak for some j < k , and hence xuv i wz ∈ strategy, and L is therefore not regular. (b) Give Give a context–fr context–free ee grammar G for L. Solution: S → aa | bb | aSa | bSb (c) Prove Prove your your grammar correct correct (cf. Lecture Lecture 20): that is, prove prove L = L(G). Solution: We have to prove: ∗ ∀x ∈ {a, b}+ . (S →G x ⇔ x ∈ L) We show the two directions of the equivalence separately. (⇒) By induction on the length of the derivation of x. 0 {a, b}+ . Basis Holds vacuously, since S →G x is false: x = S is impossible since S ∈ n Induction Induction Step Assume Assume x ∈ L for all x such that S →G x (induction hypothesis). n+1
1
n
Let S → G x. Then Then,, we must must have have S →G γ and γ →G x for some γ ∈ {a,b,S }+ . But then γ can only be aa or bb or aSa or bSb. bSb . The first first two cases cases imply n = 0 and x = γ , and then obviously x ∈ L. In the case γ = aSa, aSa , it must be that x = ax a and n S →G x for some x . From the induction induction hypothesis, hypothesis, we have have x ∈ L. But x = ax a, and therefore also x ∈ L. The case γ = bSb is similar. similar. (⇐) By induction on |x|, which is even and positive. 1 Basis |x| = 2, then x ∈ L implies implies that x is either aa or bb. bb. In both cases S →G x and ∗ therefore S →G x. ∗ Induction Step Assume S →G x for all x ∈ L such that |x | = n (induction hypothesis). Let |x| = n + 2, and and let x ∈ L. It must be that either x = ax a or x = bx b for some x ∗ such that x ∈ L and |x | = n. From the induction induction hypothesis, hypothesis, S →G x . Then, in the 1 ∗ ∗ case x = ax a we also have aSa →G ax a = x, and since S →G aSa, aSa , then S →G x. The case x = bx b is similar. (d) Give Give an NPDA for L. Solution: One possibility is to put G in GNF: S → aA | bB | aSA | bSB A→a B→b and then construct the NPDA canonically (cf. Lecture 24): def
Q = {q } def Σ = {a, b} def Γ = {S,A,B }
a
q, S → q,SA a q, S → q, A b
def
δ =
q, S → q,SB b
q, S → q, B a q, A → q, b
q, B → q, def s = q def ⊥ = S 2. Consider Consider the language: language:
k l m
A= a ba
|m=k+l
(a) Use the closure properties properties of regular languages languages to show that A is not regular. ∗ ∗ Solution: The language L(b a ) is regular, but A ∩ L(b∗ a∗ ) = {bn an | n ≥ 0}, as we already know, is not regular. Since regular languages are closed under intersection, A is not regular. (b) Give Give a context–fr context–free ee grammar G generating A. Solution: One p ossibilit ossibility y is: S → aSa | B B → | bBa +
(c) Prove Prove your grammar correct; correct; that is, prove prove S →G x ⇔ x ∈ A. Solution: The proof is standard, and is made easy by the fact that we already know that + B →G x ⇔ x ∈ {bn an | n ≥ 0}. (d) Construct Construct an NPDA NPDA accepting accepting A − {} on empty stack. Explain your choice of productions. Solution: One possibility is to build an NPDA with three states, having the following productions: a
q 0 , ⊥ → q 0 , C a
q 0 , ⊥ → q 2 , C
a
q 1 , C → q 2 , ε
a
q 2 , C → q 2 , ε
b
q 1 , C → q 1 , C C
b
q 0 , ⊥ → q 1 , C a q 0 , C → q 0 , C C a q 0 , C → q 2 , C C b
q 0 , C → q 1 , C C The first state counts the initial a’s, the second state counts the b’s which follow, and the third state checks checks for the sum. In addition, the first state can nondetermin nondeterministica istically lly decide that no b’s are going to come and that exactly half of the a’s have been read. 3. Consider Consider the language: language:
A = {x ∈ {a, b}∗ | a( a(x) < b( b (x)}
(a) Give Give a context–fr context–free ee grammar G for A. Explain your choice of productions. Solution: There There are many many possibl possiblee soluti solutions ons.. One way way of looking looking at the strings strings of the language is to devide these into the ones which have exactly one occurrence of b more than occurrences of a, and those those that have have more. A string string is in the first first group group exactly exactly when it can be represented as a string of the shape e1 · b · e2 , where e1 and e2 are strings with an equal number of a’s and b’s. Thus, Thus, e1 and e2 can be produced by the grammar E → | aEb | bEa | EE . EE . A string is in the second group exactly when it is the concatenation of two strings of A. So we arrive at the following grammar: S → EbE | SS E → | aEb | bEa | EE
(b) Construct Construct an NPDA accepting accepting A on empty stack. Explain its workings. Solution: Ag Agai ain, n, ther theree is a num number of good solu soluti tions ons.. One One eleg elegan antt solu soluti tion on usin usingg εtransitions (proposed by one of the students at the exam) is based on the observation that if we use the ”standard” productions for comparing occurrences: a
b
q, ⊥ → q, A⊥
q, ⊥ → q, B ⊥
a
b
q, A → q,AA
q, B → q,BB
a
b
q, B → q, ε
q, A → q, ε
then a string is in A exactly when after reading it the stack contains only B ’s (on top of ⊥). Note that there must be at least one such B . So, we can obtain the desired behaviour by adding two more productions: b
q, ⊥ → q, B ε q, B → q, ε 4. Consider Consider the language: language: A = {am bn | m ≤ n} (a) Use the Pumping Lemma for regular languages languages (as a game with a Deamon) Deamon) to prove prove that A is not regular. (b) Refer to the closure closure properties of context–free context–free languages languages to argue that A is context–free. That is, represent A as the result of some operation(s) on context–free languages (which we already know to be context–free) under which CFLs are closed. def
def
Solution: A = B · C for B = {am bm | m ≥ 0} and C = {bn | n ≥ 0}, both of which we
know to be context–free, and we know CFLs to be closed under language concatenation. (c) Give Give a context–fr context–free ee grammar G generating A. Solution: One possibility is the grammar S → | aSb | Sb. Sb . (d) Prove Prove your grammar correct. correct. You are allowed allowed to reuse results results proved proved in class. (e) Construct Construct an NPDA NPDA accepting accepting A − {} on empty stack. Explain your choice of productions. Solution: One solution is a NPDA with one control state q and productions: productions: a
q, S → q,SA a q, S → q, A b
q, A → q, A b
q, A → q, The automaton uses the first production for reading all initial a’s but the last, then guesses the last a and uses the second production to get rid of the S at the top of the stack. The stack now has as many A’s as the a’s read so far. far. The automaton automaton guesses guesses the number number of b’s in excess of a’s in the string, and uses the third production that many times. The stack now has as many A’s as there are remaining (unread) b’s. The automaton uses production four to empty the stack upon reading the whole string. 5. Consider Consider the language: language:
k l m
A= a ba
|l =k+m
(a) Refer to the closure properties properties of regular languages languages to argue that A is not regular. Solution: The regular lanuguages are closed under intersection, but A ∩ L(a∗ b∗ ) equals {an bn | n ≥ 0} which is not regular, hence A cannot be regular. (b) Refer to the closure properties properties of context–fr context–free ee languages to argue that A is context–free. Solution: The context–free languages are closed under language concatenation, and since A can be represented as the concatenation of the context–free languages ak bk | k ≥ 0 and {bm am | m ≥ 0}, A must be context–free.
(c) Give Give a context–fr context–free ee grammar G generating A. Solution: From the previous observation, and the construction on grammars we used to show this closure property, we directly obtain the grammar: S → AB A → | aAb B → | bBa (d) Construct Construct an NPDA NPDA accepting accepting A − {} on empty stack. Explain your choice of productions. 6. Consider Consider the language: language: L = {x ∈ {a,b,c}∗ | a( a(x) + b( b(x) = c( c(x)} def
(a) Show Show that L is not regular. (b) Give Give a context–fr context–free ee grammar for L. Prove your grammar correct. (c) Construct Construct an NPDA accepting accepting L (on empty stack). 7. Consider Consider the language: language:
n} L = {am bn | m = (a) Give Give a simple argument argument for L being context–free. (Hint: you could use the closure properties of CFLs.) (b) Construct Construct an NPDA (possibly (possibly with –transitions) accepting L on empty stack. Explain its workings. 8. Consider Consider the context–fr context–free ee grammar: S → | aS | Sb (a) Which Which language does this grammar generate? generate? Solution: It generates the language L(a∗ b∗ ). (b) Prove Prove your answer answer correct. + Solution: The proof of S →G x ⇔ x ∈ L(a∗ b∗ ) is standard, as discussed in class. 9. Consider Consider the following following language: def
n} L = {am bn | m = over the alphabet Σ = {a, b}. (a) Refer Refer to the closure properties of context–free languages to show that L is context–free. def def def Solution: We have L = A · C ∪ C · B for languages A = L(aa∗ ), B = L(bb∗ ), and C = {an bn | n ≥ 0}, all of which which are context–fr context–free. ee. Since CFLs are closed under concatenation concatenation and union, L must must also be context–fr context–free. ee. (b) Guided Guided by your answer, answer, give a context–fr context–free ee grammar G generating L. Solution: Using the constructions used for proving the corresponding closure properties, we easily obtain the grammar: S S A S B S C C
→ → → →
S A S C C | S C C S B a | aS A b | bS B | aS C C b
(c) Constr Construct uct a deterministic pushdown automaton automaton (DPDA) that accepts L on final states states . (Recall that DPDAs rewrite ⊥ only to strings of shape γ ⊥, so they never halt because of an empty stack.) Draw its graph and explain its workings. Solution: (Sketch) It is not difficult to come up with a DPDA for this language having 6 contro controll states states.. The key key idea idea is to push push onto the stack stack a letter letter A for the first a of the input word, but push a different letter B for all following a’s. This This allows allows to detect detect the b “matching” “matching” the first a, and to move to a non-final control state.
(d) Recall Recall the Chomsky–Sch¨ utzenberger Theorem (textbook Supplementary Lecture G). Show how this theorem applies to the above language L, by identifying:
• a suitable natural number n, • a regular language R over the alphabet Σn of the n–th balanced parentheses language PARENn , and • a homomorphism h : Σn → Σ∗ , so that you can argue that L = h(PARENn ∩ R) holds. Solution: Again guided by the decomposition in (a), one can take n = 3, regular language R = L([1 [1 ∗ ]1 ∗ [2 ∗ ]2 ∗ + [ 2 ∗ ]2 ∗ [3 ∗ ]3 ∗ [3 ) and homomorphism h defined by h([1 ) = h([2 ) = a, h(]2 ) = h(]3 ) = b, and h(]1 ) = h([3 ) = . 2.2 2.2
Chom Chomsk sky– y–Sc Sch¨ h¨ utzenberger Theorem utzenberger
10. Recall Recall the Chomsky–Sc Chomsky–Sch¨ h¨ utzenberger Theorem, textbook Supplementary Lecture G. Show how utzenberger this Theorem applies to the context–free language PAL of even–length palindromes over {a, b}, by identifying identifying a suitable suitable number number n, a regular language R, and a homomorphism (renaming) h. ∗ ∗ Solution: PAL is equal to the language h(PARENn ∩ R) for n = 2, R = L(([1 +[2 ) (]1 +]2 ) )
and h renaming [1 and ]1 to a and [2 and ]2 to b. 11. Recall Recall the Chomsky–Sch¨ Chomsky–Sch¨ utzenberger Theorem (textbook Supplementary Lecture G). Show how utzenberger this theorem applies to the context–free language A = {x ∈ {a, b}∗ | a( a(x) = b( b(x)} def
over the alphabet Σ = {a, b}, by identifying:
• a suitable natural number n, regular language language R over the alphabet Σn of the n–th balanced parentheses language, and • a regular
• a homomorphism h : Σn → Σ∗ , for which you argue that A = h(PARENn ∩ R) holds. Solution: Recalling that A is generated by the grammar S → | aSb | bSa | SS it is easy to
see that A = h(PARENn ∩ R) holds for n = 2, R = (Σ n )∗ and h defined by h([1 ) = a, h(]1 ) = b, h([2 ) = b and h(]2 ) = a. 2.3
Provin Proving g Non–con Non–contex text–f t–free reenes nesss of a Language Language
12. Apply Apply the Pumpin Pumpingg Lem Lemma ma for context context–fr –free ee languag languages es (as a game with the Demon) Demon) to show show that the language: A = an bn a j | j ≤ n is not context–free.
Solution:
– Demon picks picks an arbitrary arbitrary k ≥ 0. + We pick pick z = ak bk ak which is in A. – Demon Demon picks picks u,v,w,x,y such that z = uvwxy, uvwxy, |vx | > 0, |vwx | ≤ k . + If vwx vw x = al bm for some l, m ≥ 0, we pick i = 0. Otherwise we pick i = 2. Since |vwx | ≤ k , vwx has either the shape al bm or bm al for some l, m such that l + m ≤ k . In the first case xv 0 wx0 y must be of the shape a p bq ak for some p, q such that p + q < 2k, and thus is not in A. In the second case, xv 2 wx2 y will either not be in L(a∗ b∗ a∗ ) at all, and thus not in A, or else xv 2 wx2 y must be of the shape ak b p aq for some p, q such that p + q > 2k, and thus not in A. Hence, we win the game in all cases, which shows that A is not context–free.
13. Consider Consider the language: language:
l m n
A= ab a |l