Solved Problems On Automato
Solved Problems On Automato
= {, 0, 1, 00, . . .},
a
= {, a, aa, . . .},
(foo)
= {, foo, foofoo, . . .}
A
+
Plus operator on the set A {0, 1}
+
= {0, 1, 00, 01, 10, . . .}
w
rev
Reverse of the string w (foobar)
rev
= raboof w
R
L The complement language to L (L), L
C
2
A
The powerset of A 2
{0,1}
=
_
, {0}, {1}, {0, 1}
_
|A| Number of elements in A (sometimes
denoted cardinality)
|{1, 2, 3}| = 3, |automation| = 7
|w|a The number of as in the string w |abracadabra|a = 5 na(w), #a(w)
prex (w) The set of strings x such that w = xz prex (abc) = {, a, ab, abc}
pprex(w) The set of proper prexes to w prex (abc) = {a, ab}
prex(L) The set of strings w such that w is a pre-
x in some string in L
prex (
) =
) =
=
0
1
2
3
(iv) 2
(v) 2
110
(iii)
+
4
= 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111,
1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111
The set is nite.
(iii) Each set in the innite and countable series is nite and may therefore be counted by mapping it
to the natural numbers. Thereby, the full set can be counted. The set is innite and countable.
(iv) 2
[(S x)
2
[ = [S
2
T[
= [S
2
[ +[T[ since S
2
and T are disjoint (x , S x , 2
S
)
= [S
2
[ +[t x [ t S
2
[
= [S
2
[ +[S
2
[
= 2 [S
2
[
= 2 2
|S|
(from the inductive hypothesis)
= 2
|S|+1
= 2
|S{x}|
since x , S
Summary: The claim holds for S = , and if the claim holds for some set S then it also holds for
the set S x where x , S. Thus, it holds in all cases and the proof is concluded.
Solution 1.13. (o76)
rev
= for the empty string . a
rev
= a for a string with containing only a single character. A non-
empty string x can be partitioned into two substrings x = ay where a contains one or zero characters.
We can then dene (ay)
rev
= y
rev
a where [a[ 1.
Now we will show that (xy)
rev
= y
rev
x
rev
. First assume that x is the empty string. If so, the claim
is true, since (y)
rev
= y
rev
= y
rev
. Now assume that x is a string a containing only one character. If
so, the claim is true as well, since (ay)
rev
= y
rev
a by denition. These are our base steps.
10
Now we assume that the claim (uv)
rev
= v
rev
u
rev
holds for two strings u, v. This is our inductive
hypothesis. Now we want to show that (auv)
rev
= v
rev
u
rev
a.
(auv)
rev
= (uv)
rev
a by denition. Furthermore, (uv)
rev
= v
rev
u
rev
by inductive hypothesis, which
yields (uv)
rev
a = v
rev
u
rev
a, which is what we wanted to show.
Since we have shown that the claim holds for a (two) base cases, and that it holds for any string
which may be constructed by concatenating a character to the beginning of the rst string, the claim
holds for all strings.
Solution 1.14. (o12)
[AB[ = [A[ [B[ for the empty string A = and an arbitrary set B. The claim also holds for A =
and an arbitrary B. Now assume that the claim holds for two sets Aand B (the induction hypothesis).
We want to show that [A x B[ = [A x[ [B[ for an arbitrary element x / A (if x A the
claim holds trivially, since x A A x = A).
We start with the left hand side. The cross product implies that AxB = ABxB.
Since AB and x B are disjoint (since x / A), it holds that [AB x B[ = [AB[ +
[x B[ = [AB[ +[B[.
For the right hand side it holds that [A x[ = [A[ + 1. Thus, we have [A x[ [B[ =
([A[+1)[B[ = [A[[B[+[B[ = [AB[+[B. But then it also holds that [AxB = [Ax[[B[,
which concludes the proof.
Solution 1.15. (o13)
Claim: 2
0
+ 2
1
+ 2
2
+ + 2
n
= 2
n+1
1 for all integers n 0.
Verication:
2
0
= 1 = 2
1
1 = 1
2
0
+ 2
1
= 3 = 2
2
1 = 3
2
0
+ 2
1
+ 2
2
= 7 = 2
3
1 = 7
Base step: We choose n = 0 as base case, since this is the least number for which the claim should
hold. According to the verication above, the claim holds for the base step.
Induction step: We assume that the claim holds for a certain case, and try to prove that it holds also
for the following case.
Thus, assume that 2
0
+ 2
1
+ 2
2
+ + 2
k
= 2
k+1
1 for some number k. We want to show
that this implies that 2
0
+ 2
1
+ 2
2
+ + 2
k
+ 2
k+1
= 2
k+2
1.
If 2
0
+ 2
1
+ 2
2
+ + 2
k
= 2
k+1
1 for some number k, then the following holds.
2
0
+ 2
1
+ 2
2
+ + 2
k
+ 2
k+1
= (2
k+1
1) + 2
k+1
(from the inductive hypothesis)
= 2
k+1
+ 2
k+1
1
= 2 2
k+1
1
= 2
k+2
1
Summary: The claim holds for the base case k = 0, and if it holds for a number k then it also holds
for the following number k + 1. Thus it holds for all cases, which concludes the proof.
11
2 Regular expressions
Exercise 2.1. (o23)
Construct regular expressions representing languages, over the alphabet a, b, c, in which for every
string w it holds that:
(i) The number of as in w is even.
(ii) There are 4i + 1 bs in w. (i 0)
(iii) [w[ = 3i. (i 0)
Exercise 2.2. (o24)
Aregular expression is ambiguous when there exists a string which can be constructed in two different
ways from the regular expression. Which of the following regular expressions are ambiguous?
(i) a
_
(ab)
cd
_
a(ababcb
(ii) aab
(ab)
ab
bba
(iii) aaba
aaaba aabba
a
Exercise 2.3. (o40)
Consider the regular expression
_
a(cd)
b
_
.
(i) Find a string over a, b, c, d
4
which matches the expression.
(ii) Find a string over a, b, c, d
4
which does not match the expression.
Exercise 2.4. (o41)
Find a regular expression for the language consisting of alternating zeroes and ones.
Exercise 2.5. (o42)
Find a regular expression for the language L over = a, b consisting of strings which contain
exactly two or exactly three bs.
Exercise 2.6. (o43)
Find a regular expression over the language L over the alphabet = a, b consisting of strings
where the number of bs can be evenly divided by 3.
Exercise 2.7. (o44)
Describe which languages the following regular expressions represents, using common english.
(i) (0 1)
01
(ii) 1
01
(iii) (11)
(iv) (0
10
10
(v) (0 1)
01(0 1)
12
(vi) 1
(vii) (10 0)
(1 10)
(viii) 0
(1 000
_
a(a b c)
(a b c)
b
_
c
(i) is L(E) = a, b, c
?
(ii) is L(EE) = a, b, c
?
TEST ASSIGNMENT 1. (LESSON ) (o45)
Find a regular expression which represents the set of strings over a, b which contains the two
substrings aa and bb.
13
Solutions
Solution 2.1. (o23)
(i) (b c)
_
_
a(b c)
_
2
_
(ii) (a c)
b(a c)
_
_
b(a c)
_
4
_
(iii) (
3
)
cd
_
or a(ababcb
.
(ii) Ambiguous. The string abb can be constructed either by ab
or a
bba
.
(iii) Unambiguous.
Solution 2.3. (o40)
(i) abab matches.
(ii) abcd doesnt match, since every c must be preceded by an a in order for the string to match.
Solution 2.4. (o41)
Alternative 1:
1. (01)
ba
ba
ba
ba
ba
ba
ba
ba
ba
ba
ba
.
Solution 2.6. (o43)
1. a
ba
ba
ba
ba
ba
ba
_
_
aa(a b)
bb
_
_
bb(a b)
aa
_
_
(a b)
15
3 Finite Automata
Exercise 3.1. (o16)
Let M be the following DFA.
start 1
4
b
2
a
b
3
a
b
a
a
b
(i) Write down four string accepted by M and the sequence of congurations that shows this.
(ii) Write down four strings not accepted by M.
Exercise 3.2. (o18)
Let M be the following DFA.
start 1
2
a
5
b
a 3
a
b
4
b
a
b
(i) Write down four strings accepted by M and the sequence of congurations that shows this.
(ii) Write down four strings not accepted by M.
Exercise 3.3. (o15)
Construct a DFA which accepts the following language:
L = w [ w
b
(ii) 1(1 0)
0 0
(iii) 0
10
16
(iv) (0 1)
1(0 1)
(v) 0
(1 )0
(vii) a(a b)
a b(a b)
b a b
Exercise 3.5. (o37)
Formally dene the automata you constructed in Exercise 3.4.
Exercise 3.6. (o17)
Which languages are accepted by the following automata:
(i)
start 1
2
a
4
b
3
a,b
a,b
a,b
(ii)
start 1
b
2
a
3
a,b
a,b
4
a
b
(iii)
start 1
0
2
1
1
3
0
0
4
1
0
1
(iv)
start 1
0
2
1
1
3
0
0,1
(v)
start 1
a
2
b
a
b
17
(vi)
start 1
a
2
a
b
b
Exercise 3.7. (o20)
Find DFAs which accepts the following languages:
(i) Strings over a, b ending in aa.
(ii) String over a, b containing three consecutive as (that is, contains the substring aaa).
(iii) Strings over a, b not containing the substring aaa.
(iv) All strings over a, b where each string of length 5 contains at least two as.
(v) The set of strings over a, b where each pair of as immediately succeeds a pair of bs.
(vi) The strings , 001 and 000101.
(vii) Strings over 0, 1 beginning with a 1 and which, if interpreted as a binary number, is a multiple
of 5. As an example, the string 1010 should be accepted since 1010
2
= 10
10
, which is a
multiple of 5. On the other hand, the string 1110 should not be accepted, since 1110
2
= 14
10
which is not a multiple of 5.
(viii) w a, b
[ [w[
a
= 2n, [w[
b
= 2m, m, n N
(ix) w a, b
[ [w[
a
= 3n, n N and w doesnt contain the substring aba.
Exercise 3.8. (o25)
Construct nite automata which accepts the languages dened by the following regular expressions.
(i) a
ba
ab
(ii) b
_
(aab
a
4
)b
_
a
(iii) ab
_
_
(ba)
bbb
_
a
_
b
Exercise 3.9. (o29)
Construct a DFA which is equivalent to the following regular expression:
_
00 (1 01)(11 0)
10
_
20
(ii)
q0 q1
q2
b
b
q3
b
a
b
a
a,
Exercise 3.15. (o65)
Convert the following NFA to a DFA.
start 0
a,b
1
a
2
a
3
a
a,b
Exercise 3.16. (o39)
Minimise the DFA below by set partitioning or double complement.
a,b
A
B
a
D
b
a
C
b
b E a
b
a
b
a
_
(ac)
b a
Hint: The DFA has 8 states.
22
Solutions
Solution 3.1. (o16)
(i) 1. a: 1, a#), 2, #)
2. bba: 1, bba#), 4, ba#), 4, a#), 3, #)
3. baa: 1, baa#), 4, aa#), 3, a#), 2, #)
4. baba: 1, baba#), 4, aba#), 3, ba#), 1, a#), 2, #)
(ii) 1.
2. bbbb
3. aaaa
4. abab
Solution 3.2. (o18)
(i) 1. aa: 1, aa#), 2, a#), 3, #)
2. bb: 1, bb#), 5, b#), 4, #)
3. aaab: 1, aaab#), 2, aab#), 3, #ab), 5, #b), 4, #)
4. bbba: 1, bbba#), 5, bba#), 4, #ba), 2, #a), 3, #)
(ii) 1.
2. a
3. aaab
4. bbb
losningo15
0
1
1
0
0
2
1
1
3 0
0
4
1
0,1
Note that if the input string w exists in the language L, we eventually will nd the substring 0101
in w. We dene the states corresponding to how much of the substring 0101 has been observed:
State 0 is the initial state. In this state no part of the substring 0101 has been observed.
In state 1, the rst zero of 0101 has been observed. If a zero turns up, we continue to search for
the next one in the substring 0101.
In state 2, the substring 01 of 0101 has been observed. If the next character is a one, we return
to the initial state since 011 is not a substring of 0101.
In state 3, the string 010 has been observed. If we add another zero, we return to state 1 this
because the new zero may be the start of a new substring 0101.
In state 4 we have observed the full substring 0101. Thus, it doesnt matter what other characters
there are in the string, since we already have concluded that the string exists in the language.
Consequently, state 4 is an accepting state.
23
Solution 3.4. (o36)
(i)
1 2
a
a,b
3
b
(ii)
1
2
1
3
0
1,0
0
(iii)
1
0
2
1
0
(iv)
1
0,1
2
1
0,1
(v)
1
0
2
0
1,
(vi)
1
2
0,1
3
0,1
0,1
0,1
(vii)
1
2
a
3
a
b
4
b
a,b
a
b
a,b
Solution 3.5. (o37)
(i) M = (1, 2, 3, a, b, , 1, 3),
a b
1 2
2 2 2, 3
3
24
(ii) M = (1, 2, 3, 0, 1, , 1, 3),
0 1
1 3 2
2 2, 3 2
3
(iii) M = (1, 2, 0, 1, , 1, 1, 2),
0 1
1 1 2
2 2
(iv) M = (1, 2, 0, 1, , 1, 1, 2),
0 1
1 1 1, 2
2 2 2
(v) M = (1, 2, 0, 1, , 1, 1, 2),
0 1
1 1 2 2
2 2
(vi) M = (1, 2, 3, a, b, , 1, 3),
a b
1 2, 3 2, 3
2 2, 3 2, 3
3
(vii) M = (1, 2, 3, 4, a, b, , 1, 3),
a b
1 2, 3 3, 4
2 2, 3 2
3
4 4 3, 4
Solution 3.6. (o17)
(i) The strings with an even number of characters and length of at least 2.
(ii) The strings which contains at least one a, followed by an odd number of characters.
(iii) The strings that ends in 101.NOTE: or?
(iv) The strings on the form 0
n
1
m
, n, m 0.
(v) The strings that ends in a.
(vi) The strings that doesnt start with a b.
Solution 3.7. (o20)
(i)
0
b
1
a
b
2
a
b
(ii)
0
b
1
a
b
2
a
b
3
a
a,b
25
(iii)
0
b
1
a
b
2
a
b
3
a
a,b
(iv)
5
10
a,b
0
1
b
6
a
2
b
7
a
3
b
8
a
4
b
9
a
a,b
b
a
b
a
b
a
b
a a,b
(v)
0
b
1
a
b
2
a
3
a
4
b
a,b
b
a
Note: state 0 should be an accepting state here.
(vi)
0
1 0
s
1
3
0,1
7
0,1
2
0
1
1
4
0
5 1
0
6
0
1
1
0
26
(vii)
0
s
0
1
0,1
1
0
0
1
1
0
1
0
0
1
0
1
2
3
4
(viii)
0
1 a
3
b
a
2
b
b
a
b
a
(ix)
0
b
1
a
5
a
2
b
6
a
b
4
b
3
a
a
b
a,b
Solution 3.8. (o25)
(i) DFA:
0
a
1
b
2
b
3
a
a,b
a
4
b
a
b
(ii) NFA:
0 1
b
aaaab
2
aa
3
a
b
b
27
(iii) NFA:
0 1
ab
a
bbbb
2
b
4
b
3
a
b
b
Solution 3.9. (o29)
Construct an NFA given a GFA.
1.
1
00
(1 01)(11 0)
10
2.
1
3
2
0
10
0
(1 01)(11 0)
3.
1
3
2
0
4
1
0
0
(1 01)
(11 0)
4.
1
3
1
5
0
2
0
0
6
1
4
1
1
1
0
0
28
Convert the NFA to a DFA (join the obviously equivalent states 5, 2 and 4, 6).
1
3
1
5 0
0
4
1
0
1
0
1
Solution 3.10. (o19)
We construct a GFA from the nite automation.
1. Eliminate states 2 and 6, new transitions (1, 3), (1, 4), (1, 5), (1, 4).
start 1
3
4
b,c
5
b
a,c
a,b,c
a,b
c
a
+
b
a
+
c
a
+
b
a
+
c
2. Eliminate state 4. Since there are no paths from state 4 (dead state) we can remove it entirely.
start 1
3
5
b
c
a
+
b
a
+
c
3. Create a new accepting state, number 7, and make new epsilon-transitions from the previous
accepting states to 7. We do this to be able to eliminate state 3 and 5.
29
start 1
3
5
b
7
c
a
+
b
a
+
c
a
(ii) b
a(a b)
(iii) (a baa)
b(a )
Solution 3.12. (o27)
Construct GFAs to create regular expressions, see Salling page 48 and forward.
(i)
_
b
+
a(b ab
a)
_
_
ab
b
+
a( ab
)
_
(ii)
_
ab aa(aa)
ab
_
_
a aa(aa)
( a)
_
(iii) a(aa ab)
(iv) b
a(b
+
ab
a)
2. Remove state 3.
start 1 0
0 11 10(0 10)
11
10(0 10)
11
_
10(0 10)
.
30
Solution 3.14. (o38)
We solve the assignments by the method from Salling, page 41.
(i)
R
a,b
{1,2}
b
{1,2,3}
a
{2,3}
a
b
b
a
Step by step:
1. Initial state: 1, 2 {1,2}
a
b
2.
1, 2
a
1, 2, 3
1, 2
b
(dead state)
R
a,b
{1,2}
b
{1,2,3}
a
a
b
3.
1, 2, 3
a
1, 2, 3
1, 2, 3
b
2, 3
R
a,b
{1,2}
b
{1,2,3}
a
a
{2,3}
b
a
b
31
4.
2, 3
a
1, 2
2, 3
b
2, 3
R
a,b
{1,2}
b
{1,2,3}
a
{2,3}
a
b
b
a
(ii)
a,b
{q1}
a
{q1,q3}
b
{q0,q1}
a
{q1,q2,q3}
b
b
{q0,q1,q3}
a
b
a
b
a
33
Double reversing: We use the double reversing algorithm from Salling, page 62.
First we reverse the automation and obtain an NFA M
rev
.
a,b
B
a
A
D
b
b
E
b
C
b
a
a
a
b
a
to obtain an NFA (M
)
rev
.
1
b
2
b
3
b
a
a
a
From (M
)
rev
we create a DFA M
as before.
1. {1,2}
a
b
2.
{1,2}
{1}
a
{1,2,3}
b
a
b
a
b
3.
{1,2}
{1}
a
{1,2,3} b
b
{}
a
a
b
4.
{1,2}
{1}
a
{1,2,3}
b
b
{}
a
a,b
We are done! Note that this DFA is the same as the one we obtained earlier, except that
the states have different names.
35
Solution 3.17. (o80)
We solve the problem by subset construction, Salling page 60.
Level 0 1, 2, 3, 4, 5, 6, 7, 8 Start
Level 1 2, 4, 6, 8 1, 3, 5, 7
The states 2,4,6 and 8 are accepting and the
states 1,3,5 and 7 are not.
Level 2 2, 6, 4, 8 1, 3 5, 7
The states 1 and 3 transfers to rejecting
states by the string b, but the states 5 and
7 transfers into accepting states.
Level 3 2, 6 4, 8 1, 3 5, 7
The states 2 and 6 transfers into acceptingt
states by the string ab, but the states 4 and 8
transfers into rejecting states by the string.
Level 4 2, 6 4, 8 1, 3 5, 7 No further distinguishing can be done.
The minimal DFA is shown below.
{4,8}
{1,3}
a
{5,7}
b
{2,6}
a
b
b
a
a,b
Solution 3.18. (o64)
Interpret the automata as one NFA and convert this to a DFA using subset construction.
{4}
a,b
{0,5}
b
{2,5,6}
a
{3,7}
{3,6}
a
{3,5}
b
b
{4,5,6}
a
a
{4,7}
b
{3,5,6}
b
a
{4,6}
a
{4,5}
b
b
a
b a
b
a
a
b
36
Step by step:
1. Initial state: 0, 5
2. 0, 5
a
2, 5, 6
3. 0, 5
b
4
4. 2, 5, 6
a
4, 5, 6
5. 2, 5, 6
b
3, 7
6. 4
a
4
7. 4
b
4
8. 4, 5, 6
a
4, 5, 6
9. 4, 5, 6
b
4, 7
10. 3, 7
a
3, 6
11. 3, 7
b
3, 5
12. 4, 7
a
4, 6
13. 4, 7
b
4, 5
14. 3, 6
a
3, 6
15. 3, 6
b
3, 7
16. 3, 5
a
3, 5, 6
17. 3, 5
b
3, 7
18. 4, 6
a
4, 6
19. 4, 6
b
4, 7
20. 4, 5
a
4, 5, 6
21. 4, 5
b
4, 7
22. 3, 5, 6
a
3, 5, 6
23. 3, 5, 6
b
3, 7
We rename the states according to below.
C
a,b
A
b
B
a
E
G
a
H
b
b
D
a
a
F
b
K
b
a
I
a
J
b
b
a
b
a
b
a
a
b
Minimisation:
Level 0 A, B, C, D, E, F, G, H, I, J, K
Level 1 A, B, C, D, I, J E, F, G, H, K Accepting or not
Level 2 A, C B, D, I, J E, G, H, K F foo
Level 3 A C B, D, I, J E, G, H, K F bar
Level 4 A C B, D, I, J E, G, H, K F No further distinguishing possible.
The new DFA:
{A}
{C}
b
{B,D,I,J}
a
a,b
a
{E,G,H,K}
b
a,b
SOLUTION TEST ASSIGNMENT 2. (o28)
TBA.
37
SOLUTION TEST ASSIGNMENT 3. (o11)
{1,2}
1
{1,2,3}
0
1
{1,2,3,4}
0
1
0
Step by step:
1. Initial state: 1, 2
2. 1, 2
0
1, 2, 3
3. 1, 2
1
1, 2
4. 1, 2, 3
0
1, 2, 3, 4
5. 1, 2, 3
1
1, 2
6. 1, 2, 3, 4
0
1, 2, 3, 4
7. 1, 2, 3, 4
1
1, 2
SOLUTION TEST ASSIGNMENT 4. (o72)
TBA.
38
4 Regular Grammar
Exercise 4.1. (o46)
Try to construct a regular grammar which generates the language a
n
b
n
[ n N. Explain why you
dont succeed!
Exercise 4.2. (o48)
Create regular grammar for the following languages. Use S as the rst non-terminal. When you have
created the grammars, construct the corresponding NFAs.
(i) w [ w 0, 1
10
0
Exercise 4.3. (o49)
Create NFAs for the following languages and construct the corresponding regular grammars using
these. Use S as the rst non-terminal.
(i) ( 1)(1 0)
01
(ii)
(0)
(iii) w [ w 0, 1
w doesnt end in 01
Exercise 4.4. (o47)
Create a regular grammar for the language of string over a, b which contains the substrings aa and
bb.
Exercise 4.5. (o50)
Construct regular grammars for the following languages.
(i) (aabb
aab
a)
(ba
b)
(ii) a
n
b
m
[ n +m = 2k + 1 n, m, k N
Exercise 4.6. (o77)
Construct a regular grammar which produces the language over the alphabet = a, b consisting
of all strings with at most 3 as. Show how the string babbaab is produced.
Exercise 4.7. (o78)
Construct a regular grammar producing the following language over the alphabet = a, b:
L = w [ ([w[
a
[w[
b
1 (mod 3)
Then show how the string abaaaaba is produced.
39
Solutions
Solution 4.1. (o46)
It is logical to start by S a . . . as production rule. From the beginning we have to be able to
generate bs to obtain the same number of as and bs. However, no other terminals may appear in a
regular production rule. The context-sensitive grammar S aSb [ , on the other hand, is generating
the language.
Solution 4.2. (o48)
(i)
S 0S [ 1S [ 1T [ 0V
T 1U
V 0U
U 0U [ 1U [
S
0,1
T
1
V
0
U
1
0,1
0
(ii)
S T [ 1U
T 1T [
U 0U [ V
V 1V [ 0
S
T
U
1
1
0
V
1
0
01
= (1 0)
01
S
0,1
T
0
1
S 0S [ 1S [ 0
T 1T [
(ii)
(0)
= 1
S
1
S 1S [
(iii)
S
1
T
0
0
U
1
1
0
S 0T [ 1S [
T 0T [ 1U [
U 0T [ 1S
40
Solution 4.4. (o47)
S S
1
[ S
2
S
1
aS
1
[ bS
1
[ aA
A aB
B aB [ bB [ bC
C bD
D aD [ bD [
S
2
aS
2
[ bS
2
[ bE
E bF
F aF [ bF [ aG
G bH
H aH [ bH [
Solution 4.5. (o50)
(i)
S S
1
[ S
2
S
1
aabA
1
[
A
1
bA
1
[ B
1
B
1
aaC
1
C
1
bC
1
[ D
1
D
1
aS
1
S
2
bA
2
A
2
aA
2
[ B
2
B
2
b
(ii) Construct strings with odd length consisting of some number of as followed by some number
of bs.
S aA
odd
[ bB
odd
A
odd
aA
even
[ bB
even
[
A
even
aA
odd
[ bB
odd
B
odd
bB
even
[
B
even
bB
odd
Solution 4.6. (o77)
S bS [ aS [
A bA [ aB [
B bB [ aC [
C bC [
Production of the string babbaab:
S bS baS babA babbA babbaB babbaaC babbaabC babbaab
41
Solution 4.7. (o78)
S aA [ bB
A aB [ bS [
B aS [ bA
The string abaaaaba is produced as follows.
S aA abS abaA abaaB abaaaS abaaaaA
abaaaabS abaaaabaA abaaaaba
42
5 Non-Regularity
Exercise 5.1. (o79)
Show that if the languages L
1
and L
2
are regular, then the following language are regular. You can
use any proof technique you like, and use any proof from the course book and/or the lecture material.
(i) L
1
L
2
(ii) L
1
(iii) L
1
L
2
(iv) L
1
, dvs.
L
1
(v) L
1
L
2
(vi) L
1
L
2
(vii) w [ w L
1
if and only if w / L
2
(viii) prex(L
1
) (see Notation)
(ix) sux(L
1
) (see Notation)
(x) substring(L
1
) where substring(L) = w [ x, y such that xwy L
Exercise 5.2. (o68)
Which of the following languages are regular?
(i) a
n
b
m
[ n > m n < m
(ii) a
n
b
m
[ n m n m
(iii) a
n
b
m
[ n > m n < m
(iv) a
n
b
m
[ n m n m
Exercise 5.3. (o34)
Prove that if L is regular, then L
rev
is regular.
Exercise 5.4. (o70)
What can be said about the language L = w a, b
(ii) w [ w 0, 1
[w[
0
,= [w[
1
(iii) w [ w a, b
[w[
a
= 2[w[
b
(iv) w
rev
[ w L(a
n
b
m
)
(v) xwx
rev
y [ x, w, y a, b
+
(vi) a
n
b
n+m
c
m
[ m 2
Exercise 5.7. (o32)
Prove that the following languages are non-regular:
(i) 0
,
1
n
0
m+n
[ m, n 1
(ii) ww [ w (0 1)
+
(iii) (01)
n
(10)
n
[ n 0
(iv) The set of strings over 0, 1 with the equally many zeroes as ones.
(v) w (0 1)
[ w = w
rev
(vi) xx
rev
w [ x, w (0 1)
+
(vii) 0
n
[ n is prime
(viii) 0
n
[ n is composite
(ix) 0
m
1
n
[ m ,= n
(x) 0
m
1
n
[ gcd(m, n) = 1, where the function gcd(x, y) is the greatest common divisor.
Exercise 5.8. (o81)
Prove that the language L = a
n
b
n
, n N is non-regular using the pumping lemma.
Exercise 5.9. (o62)
Prove that the language L = w 0, 1
[ u : www = uu is non-regular.
Exercise 5.10. (o67)
Let L be a regular language. What can be said about the following language?
L
1
= w [ ww
rev
ww
rev
L
Exercise 5.11. (o73)
x is a root to w if n 0 such that w = x
n
. We dene the root to a language L as rot(L) = x [
n 0 : x
n
L. Prove that for each language L
it holds that L
= root(L).
44
TEST ASSIGNMENT 5. (LESSON ) (o14)
Determine if the following languages are regular or not.
(i) L
a
= a
i
b
j
c
k
[ i +j = k
(ii) L
b
= a
i
b
j
c
k
[ 1 i j k min(81, j
2
)
45
Solutions
Solution 5.1. (o79)
(i) Regular languages are by denition closed under concatenation.
(ii) Regular languages are by denition closed under the Kleene star operation.
(iii) Regular languages are by denition closed under union.
(iv) If L
1
is regular, it is possible to construct a DFAfor the language. Assume that M
1
= (Q, , , s, F)
is such a DFA. Then it is possible to construct a new DFA M
1
= (Q, , , s, QF) which re-
jects a string w if and only if M
1
would accept w. L(M
1
) =
L(M
1
) =
L
1
= L
1
.
Since it exists a DFA accepting the language L
1
, then L
1
is regular if L
1
is regular. Regular
languages are closed under complement.
(v) DeMorgans law implies that L
1
L
2
=
_
L
1
L
2
_
. Since regular languages are closed under
complement and union, they are also closed under intersection.
(vi) Note that L
1
L
2
= L
1
L
2
. Since regular languages are closed under intersection and
complement, they are also close under set difference.
(vii) Note that w [ w L
1
om och endast om w / L
2
= (L
1
L
2
) (L
2
L
1
). Since regular
languages are closed under set difference and union, this set is regular.
(viii) Assume that M
1
is a NFA accepting L
1
. Construct a new NFAM
1
equivalent to M
1
except that
we add a new state q
f
, which is the only nal state in M
1
. We also add -transitions to q
f
from
all states from with a nal state is reachable in M
1
. If wz L
1
, then w is consumed by the
regular transitions in M
1
, followed by one of the -transitions to q
f
to simulate the consumption
of z, without actually consuming something. Thus, L(M
1
) = prex(L(M
1
)) = prex(L
1
),
that is, if L
1
is regular, then prefix(L
1
) is also regular.
(ix) Assume that M
1
is a NFA accepting L
1
. Construct a new NFA M
1
equivalent to M
1
except
that we add a new initial state q
0
. In addition, we add -transitions from q
0
to all states where
an initial state is reachable. If xw L
1
, then the consumption of x is simulated by a transition
from the rst set of -transitions and w is consumed by the regular transitions in M
1
. Thus,
L(M
1
) = sux(L(M
1
)) = sux(L
1
), and so it holds that if L
1
is regular, then sux(L
1
) is
also regular.
(x) Assume that M
1
is an NFA accepting L
1
. Construct a new NFA M
1
equivalent to M
1
except
that we add a new initial state q
0
and a new nal state q
f
, which is the only nal state in M
1
.
In addition, we add -transitions q
0
to all states from which an initial state can be reached, and
-transitions q
f
from all states from which a nal state can be reached in M
1
. If xwy L
1
,
the consumption of x is simulated by a transition from the rst set of -transitions, w is then
consumed by the regular transitions of M
1
and the consumption of y is simulated bya transition
fromthe second set of -transitions. Thus, L(M
1
) = delstrang(L(M
1
)) = delstrang(L
1
), and
hence it holds that if L
1
is regular, then delstrang(L
1
) is also regular.
Solution 5.2. (o68)
Not regular. It is the same language as a
n
b
m
[ n ,= m which is the complement to a
n
b
m
[
n = m which is not regular (which you should have discovered earlier). Since regularity
is closed under complementation, the considered language can not be regular (since it would
imply that a
n
b
m
[ n = m is regular).
Regular. The language can be described by the regular expression a
.
Regular. This language contains no language, and is thus regular.
46
Solution 5.3. (o34)
Since L is regular, there exists a DFA accepting this language. If we reverse all transitions in this
automation, and turn the initial state into an accepting state and all accepting states to initial states,
the result is that we run the original automation backwards, and the new automation will accept the
language L
rev
. Since there exists an nite automation accepting L
rev
, the language is regular.
Solution 5.4. (o70)
It is not the empty set since, for instance, w = a and w = aba satises abw = wba. If we look a bit
closer at this condition we notice that w has to start with ab since this is the prex on the left hand
side. In the same way, the strings has to end in ba since that is the postx on the right hand side.
However, we can make an exception for the string a which may act both as initial and nal a and
borrows, so to speak, its bs from the prex and postx strings in the condition. Thus, the string a
satises the condition aba = aba. We now try to nd some larger examples. The strings are supposed
to start with ab, so we had to add a b to our minimal string a. In addition, we had the requirement
that the strings should end in ba, but we can let the nal b be the same as the initial one to obtain
the string aba. If we keep on adding characters to this string, the requirement that they should end in
ba yields the following strings ababa, abababa, ababababa, . . .. As can be seen, there is a regularity
in the strings which can be summarised as: the strings begins with an a followed by zero or more
bas. This can be formulated as the regular expression a(ba)
.
The intersection of two non-regular languages may also result in a regular language, for instance
the non-regular languages a
n
b
n
and b
n
a
n
, which intersection only contains the empty string, which
is regular.
Concatenation of two non-regular languages will however always result in a non-regular language.
If we assume the contrary, that is, that it is possible to create a regular language by concatenation of
two non-regular languages, there would be a nite automation accepting this language. Concatenation
of languages corresponds to concatenation of two automata accepting respective language. Thus, it
would be possible to split this nite automation into two separate automata accepting each of the
original languages, which is a contradiction since these languages are non-regular which means there
cannot exist nite automata accepting them.
Solution 5.6. (o82)
Non-regular. Assume that L = a
n
b
2n
is a regular language. Then there exists a DFA
M accepting L. Create a new DFA M
) = a
n
b
n
which has to be regular, since there is a DFA accepting
this language. This is a contradiction since we know that a
n
b
n
is non-regular. Thus, L is also
non-regular.
Non-regular. Assume that L = w [ w 0, 1
[w[
0
,= [w[
1
is regular. Since regular
languages are closed under complementation and intersection, (L)L(0
1
)
has to be regular.
However, (L) L(0
) = 0
n
1
n
, which is not regular, and thus neither is L.
Non-regular. Assume that L = w [ w a, b
[w[
a
= 2[w[
b
is regular. We know fromthe
closure properties of regular languages that L L(a
) =
a
n
b
2n
, which is not regular. Hence, L is cannot be regular.
47
Regular. The regular expression b
= a
n
b
n
,
which is non-regular, thus L cannot be regular.
Solution 5.7. (o32)
01
n
0
n+m
[ m, n 1 = 01
n
0
n
0
m
. Since we know that 1
n
0
n
and 0
m
are non-
regular and that concatenation of non-regular languages are non-regular, the language cannot
be regular.
The language is non-regular. Since each pair of the innitely many strings over (0 1)
+
are
distinguished by the language, that is, each pair x, y (0 1)
+
) where x ,= y is distinguished
when xx belongs to the language and yx doesnt.
The fact that this language is non-regular might be simpler to understand if we let a = 01 and
b = 10 resulting in the language a
n
b
n
[ n 0 which is known to be non-regular.
Call this language L and assume that it is regular. Then L(0
) =
0
n
1
n
, which is non-regular. Thus, L cannot be regular.
This language distinguishes an innite number of strings and it therefore non-regular. Every
pair of the innitely many strings x, y (0 1)
+
where x ,= y are distinguished when xx
rev
belongs to the language but not yx
rev
.
The language has to distinguish innitely many strings. The proof is similar to the solution
above. For each of the innitely many strings x, y (01)
+
where x ,= y, there exists a string
z = x
rev
such that xzw belongs to the language, but not yzw. Hence innitely many strings are
distinguished, and the language is not regular.
Also here it is useful to use argumentation about distinguishing strings. However, we need to
make slightly stricter requirements on the strings since it otherwise would not be obvious that
the language distinguishes between them. Take two strings x = 0
n
and y = 0
n+1
. The number
of such strings are innite. Since there exists an innite number of prime numbers, there exists
a string z = 0
m
such that n + m is prime, but not (n + 1) + m (except when n = m = 1
where both 2 and 3 are prime). Then xz belongs to the language, but not yz. The language then
distinguishes an innite number of strings, thus making it non-regular.
Composite numbers are numbers which are not prime. Thus, this language is the complemen-
tary language to the language or prime strings in the previous exercise, which was non-regular.
Considering this and the closure properties of complementation, we can derive that this lan-
guage cannot be regular.
This language is the complement language to 0
n
1
n
which is non-regular. By the closure
properties for complementation we know that also this language is non-regular.
Solution 5.8. (o81)
We start by taking a look at the pumping lemma.
The pumping lemma states properties that must hold for all strings in a regular language (with
an innite number of strings). The lemma states that all strings in such a language, which has a
48
length exceeding a certain number, contains a non-empty substring which may both be pumped up
(repeated) an arbitrary number of times or pumped out (removed) such that the resulting string still
is in the language. In addition it holds that such a substring exists in each substring of a string in the
language, if the substring has a length equal to or exceeding the number stated above.
In other words, it holds that all strings w L such that [w[ N can be re-written as w = xyz
where [y[ ,= 0, and for all i N it holds that xy
i
z L. In addition, it holds that all substrings of w
where [w[ N has such a pumping block y.
To prove that a language is non-regular using the pumping lemma, it is easiest to try to construct
a counter example. Thus, we start by assuming that the language L = a
n
b
n
is regular, and try to
achieve a contradiction. We will do this by observing an individual string in L and show that it is not
possible to pump it using the pumping lemma.
Thus, according to the pumping lemma it holds that each string in L having a length greater tha
or equal to an unknown number N should have a pumping block y which can be pumped. Since
we have no idea what N would be, we have to be careful and select a string which is guaranteed to
have a length which is equal to or greater than N.
The only way we can do this is to choose a string which is parameterised in N. For instance, we
can consider the string w = a
N
b
N
, for which it naturally holds that [w[ N.
The next step is to nd out exact where in the string w = xyz the pumping block y is. Here we
could use the cact that a pumping block has to exist in all substrings of w where [w[ N.
Consider the substring a
N
, for which it holds that [a
N
[ N. Thus, a
N
must contain a y such that
w = xy
i
z, i N. So we have w = a
Nk
a
k
b
N
where x = a
Nk
, y = a
k
and z = b
N
. We attempt to
pump out the pumping block y once, and obtain the string a = a
Nk
b
N
where k > 0. This string is
supposed to belong to the language L according to the pumping lemma (under the assumption that L
is regular). But since we the number of as and the number of bs are not the same, the pumped string
cannot be in the language! We have found a contradiction, and have to reject our hypothesis that L
would be regular. Hence, L is non-regular.
Solution 5.9. (o62)
We use the pumping lemma also in this proof. The string w = 10
N
110
n
1 belongs to the language,
since www = uu with u = 10
N
110
N
110
N
1. If the starting string of zeroes is pumped, which
according to the pumping lemma should be possible since the length of this string is greater than or
equal to N, the string is no longer in the language. That is, if w = 10
2N
110
N
1 then www ,= uu.
Thus, the language is non-regular.
Solution 5.10. (o67)
The strings in the language consist of a concatenated pair of the substring ww
rev
. Thus, the strings are
the same backwards and forwards, i.e., they are palindromes. Palindrome languages are not regular,
and as we saw earlier, it is not possible to obtain a regular language by concatenating two non-regular
languages.
Solution 5.11. (o73)
We have that root(L) = x [ n 0 : x
n
L. Assume that L and let n = 0. This results in
root(L) = x [ x
0
L and since we no longer have any requirements on which strings x which
should belong to root(L) we have that root(L) =
=
root(L).
Now assume that
, it has
to hold that n 0 :
n
L. No matter how many times we repeat , the result is again , i.e., n
0 :
n
= . Thus, it must hold that L. Consequently, we have shown that L
= root(L)
and this together with the paragraph above yields that L
= root(L).
49
SOLUTION TEST ASSIGNMENT 5. (o14)
L
a
is non-regular. Assume that L
a
is regular for contradiction. Then there should be a number
N such that if w L
a
and [w[ N, then w contains a non-empty substring y which can be
pumped such that w stays in the language. Such a string exists, as a matter of fact, in every
substring of w with a length of at least N characters. Consider the string a
N
b
N
c
2N
which
belongs to the language since N +N = 2N. Let the beginning sequence of as be the pumping
string y, which satises the condition in the pumping lemma since [y[ N. If we pump up
y once, the string no longer belongs to the language, since 2N + N ,= 2N. Thus, we have
achieved a contradiction, and L
a
cannot be regular.
L
b
is regular. Each string in L
b
may consist of maximum 81 of each of the characters a, b and
c. This makes the number of strings in L
b
nite, thus regular.
50