Professional Documents
Culture Documents
b) Observe that the language can be rewritten as: {(babbabbab)i : i ≥ 1}, which
can be represented by the regular expression (babbabbab)(babbabbab)∗. Hence
it is regular.
c) We claim that the language (denoted by L) is not regular.
Suppose, on the contrary, that it is regular. Therefore the pumping theorem
will apply for some integer n ≥ 1. Consider the string w = an bn+1 ∈ L.
The pumping theorem says that there exists a decomposition of w = xyz
such that (1) y 6= e, (2) |xy| ≤ n, and (3) xy i z ∈ L for each i ≥ 0. From
(2) we know that the only possiblity is that y consists only of a’s, and from
(1), y must have at least one a; thus, we let y = aq where q > 0. So, let
x = ap , y = aq and z = ar bn+1 where q > 0 and n = p + q + r. Consider
xy 2 z = ap a2q an bn+1 = an+q bn+1 . Since q ≥ 1 implies n + q ≥ n + 1, thus
xy 2 z ∈
/ L. This contradicts with (3) which implies xy 2 z ∈ L. Therefore L is
not regular.
Question 2
a) Suppose, on the contrary, that the language (denoted by L) is regular. There-
fore pumping theorem applies for some n ≥ 1. Consider the string w =
an bban ∈ L. By the theorem, it can be rewritten as w = xyz such that (1)
y 6= e, (2) |xy| ≤ n, and (3) xy i z ∈ L for each i ≥ 0. From (1) and (2),
y = aq , where q > 0. Let x = ap , y = aq and z = ar bban , where p + q + r = n.
Consider xz = an−q bban . Since q > 0 ⇒ −q < 0 ⇒ n − q < n. Thus, xz is
1
not a palindrome, thus xz ∈
/ L. This contradicts with (3). Therefore L is not
regular.
Minimum-state DFA
Question 1
a, b
a
b a, b
For Figure 1
a a, b
For Figure 2
Question 2
Let A = {x|x = 2n, n ∈ Z} and B = {x|x = 3n, n ∈ Z}
A ∩ B = {x|x = 6n, n ∈ Z}
A and B are countable infinite, A ∩ B is countable infinite.
2
Let R+ = {x|x ≥ 0, x ∈ R} and R− = {x|x ≤ 0, x ∈ R}
+
R ∩ R− = {0}
where R+ and R− are uncountable, but R+ ∩ R− is finite.
Let A = {{x}|x ∈ R}
2N ∩ A = {{n}|n ∈ N }
2N and A are uncountable but 2N ∩ A is countably infinite.
Question 3
Define f : N → Z,
if a is odd, f (a)=(a+1)/2
if a is even, f (a)=-a/2
Question 4
We can layout all the elements in Z × Z on a coordinate system and count them in
the manner as shown in the following figures:
Method I
11
00
0011
11
15 00
14
00 0
11
13 11
00
0011
12 00
111 0011
11
00
11
0011
11 00
11
004 0
3 11
00
001100
2
16 111 0010
11
0
1
0 1 0
0511
00
17 1 1 00011
11 00
0011
11 00
00
11
1 9
1
000
11
00511
00
18 1011 007 0
11 1
8 0
124
00
11 00
11 0
1
023
1
11
00
19 00
11 1
000
11
20 21 22
Method II
7
1
0
0
1
11
00
00
11
8 11
00
2 6
00
11 1
0
911
00
00 11
11 00
00
11
3 00
11
10 11
0 001 10
5
11
00 1
0
0
1
00
11
10
40
1 12
11 0
1
3
Question 5
a) The enumeration is similar to that for N × N .
1. In the first round, we visit the rational number 0
2. In the second round, we visit 11 and its negative counterpart − 11 .
3. In the third round, we visit 21 , then its counterpart − 21 , then 12 , then its
counterpart − 21 .
4. In the fourth round, we visit 13 , then its counterpart − 13 , then 31 , then
its counterpart − 13 . (Note that 22 and − 22 are NOT included in the enu-
meration since the numerator and denominator are not relatively prime)
5. In general, in the nth round, we first choose elements pq where p, q >
0, p + q = n, and p and q are relatively prime. Then, arrange these
elements in the ascending order of denominators. Next, for each element
in the candidate list, we add its negative counterpart in the list. Finally,
enumerate these elements in the order specified in the list.
b) We assume, on the contrary, that the set of real numbers in the interval [0, 1] is
countable. In other words, the elements (in binary form) 1 can be enumerated
as: {r0 , r1 , · · ·}. Consider a real number r in the interval [0, 1] constructed in
the following way:
Context-free Grammars
Question 1
(a) G = (V, Σ, R, S), where V, Σ, and R are as follows.
V = {a, b, S}
Σ = {a, b}
R = {S → aS,
S → aSb,
S → e}
V = {a, b, c, d, A, B, X, S}
Σ = {a, b, c, d}
R = {S → aSd,
S → A,
S → B,
A → aAc,
A → X,
B → bBd,
B → X,
X → bXc,
X → e}
1 A bit of notation: we define the ith bit of the number to be the (i + 1)th digit after the binary
point. For instance, the 0th and 1st bits of .10 are 1 and 0 respectively.
4
(c) G = (V, Σ, R, S), where V, Σ, and R are as follows.
V = {a, b, S}
Σ = {a, b}
R = {S → SS,
S → aSbb,
S → bbSa,
S → bSaSb,
S → e}
V = {a, b, A, B, S}
Σ = {a, b}
R = {S → e,
S → aB,
S → bA,
A → aS,
A → bAA,
B → bS,
B → aBB}
V = {a, b, S}
Σ = {a, b}
R = {S → aSa,
S → bSb,
S → a,
S → b,
S → e}
Question 2
Let E = {w ∈ {a, b}∗ |w has even length}. We need to show L(G) ⊆ E and E ⊆
L(G), from which it will follow that L(G) = E.
To show L(G) ⊆ E
Consider an arbitrary string w ∈ L(G).
Then there is a derivation of w from S in G.
Observe that all the rules in G increase the number of terminals by 0 or 2 in a
derivation step.
Thus w must be even length. Thus w ∈ E.
To show E ⊆ L(G)
We want to show that any string w ∈ E also belongs to L(G). We will prove this
by induction on the length of w.
Basis Step. For |w| = 0, the only even-length string is e. It is clear that
w ∈ L(G) since it can be generated by the rule S → e in G.
Induction Hypothesis. Suppose all strings ∈ E of length k or less also belong to
L(G), for some k ≥ 0.
Induction Step. Consider a string w ∈ E of length k + 1. If k + 1 is odd, then
there will be no such string w ∈ E and the claim is trivially true. So assume k + 1
is even. Let w = σ1 w′ σ2 , where σ1 , σ2 ∈ {a, b}. Since w′ has length k − 1 and k − 1
is even, w ∈ E. From the induction hypothesis, it follows that w′ ∈ L(G), that is
5
S ⇒∗ w′ . Next observe that for each choice of σ1 and σ2 , there is a corresponding
rule S → σ1 Sσ2 in the grammar, which implies that S ⇒ σ1 Sσ2 . Since S ⇒ σ1 Sσ2
and S ⇒∗ w′ , it follows that S ⇒∗ σ1 w′ σ2 ; that is, S ⇒∗ w. Thus w ∈ L(G). This
completes the proof by induction.
Question 3
(Solution from old edition of LP)
Let L be a regular language; then L is accepted by a DFA M = (K, Σ, δ, s, F ).
Then let G be the regular grammar (V, Σ, R, S), where
V = Σ∪K
S = s
R = {q → ap : δ(q, a) = p} ∪ {q → e : q ∈ F }.
(We assume, without loss of generality, that Σ and K are disjoint sets.) We need
to show L(G) = L(M ). We first make the following claim:
Claim:
For any σ1 , . . . , σn ∈ Σ and p0 , . . . , pn ∈ K,
if and only if
p0 ⇒G σ1 p1 ⇒G σ1 σ2 p2 ⇒G . . . ⇒G σ1 . . . σn pn ;
Proof:
It follows from the fact that δ(q, a) = p iff q → ap is a rule in G.
Question 4
The pushdown automaton is M = (K, Σ, Γ, ∆, s, F ) where:
K = {s, f }
Σ = {a, b}
Γ = {a}
F = {f }
∆ = { ((s, a, e), (s, a)),
((s, e, e), (f, e)),
((f, b, e), (f, e)),
((f, b, a), (f, e)),
((f, b, aa), (f, e)) }.
6
Question 5
The pushdown automaton is M = (K, Σ, Γ, ∆, s, F ) where:
K = {s, q, f }
Σ = {a, b, c}
Γ = {a}
F = {f }
∆ = { ((s, a, e), (s, a)),
((s, e, e), (q, e)),
((q, b, a), (q, e)),
((q, e, e), (f, e)),
((f, c, a), (f, e)) }.
Food for thought: what would be the consequence if we did not use the inter-
mediate state q (i.e. all occurrences of q’s are replaced by state f , and the element
((q, e, e), (f, e)) is deleted from ∆)?
Pushdown Automata
Question 1
The grammar is G = (V, Σ, R, S) where:
V = {S, a, b, (, ), ∪, ∗ , ∅}
Σ = {a, b, (, ), ∪, ∗ , ∅}
R = { S → ∅,
S → a,
S → b,
S → SS,
S → S ∪ S,
S → S∗,
S → (S) }.
Question 2
The pushdown automaton is M = (K, Σ, Γ, ∆, s, F ) where:
K = {s, q, f }
Σ = {a, b}
Γ = {c, m, n}
F = {f }
∆ = { ((s, e, e), (q, c)),
((q, a, c), (q, mmc)),
((q, a, m), (q, mmm)),
((q, b, m), (q, e)),
((q, b, c), (q, nc))
((q, b, n), (q, nn))
((q, a, nn), (q, e))
((q, a, nc), (q, mc))
((q, e, c), (f, e)) }.
7
Question 3
Before we show the proofs, two useful theorems are stated as follows:
Theorem 1 The context-free languages are closed under union, concatenation and
Kleene star.
– L1 = {am bn : m > n}
– L2 = {am bn : m < n}
– L1 = {am bn cp dq : n = q}
– L2 = {am bn cp dq : m ≤ p}
– L3 = {am bn cp dq : m + n = p + q}
8
Question 4
In fact, the proof can be made to work for NFA as well.
Theorem 3 The context-free languages are closed under union, concatenation and
Kleene star.
Question 2
Assume, on the contrary, that the language L1 is context-free. Let R1 be a regular
language represented by the regular expression a∗ b∗ c∗ . By Theorem 2, L′ = L1 ∩R1
should be context-free. However, L′ is just {an bn cn : n ≥ 0}, which is known to be
not context-free. Contradiction occurs. Therefore, L1 is not context-free.
Question 3
a) Note that L − R can be rewritten as L ∩ R. Since regular languages are closed
under complementation, R is thus regular. As a result, L ∩ R is context-free
by Theorem 2, and so is L − R.
9
b) R − L is not necessarily context-free.
(Note that R − L can be rewritten as R ∩ L. Since L is not necessarily
context-free, we cannot apply Theorem 2.).
Let R = a∗ b∗ c∗ , L = {ai bj ck : i 6= j or j 6= k}. R − L = {an bn cn : n ≥ 0}
which is not context-free.
10