Sei sulla pagina 1di 43

Automata and Formal Languages

Exercises

Review of some Basic Notions

1. Prove by induction that for every positive integer n the number


3 16n + 6n + 1 is divisible by 5.
2. Prove by induction that for every n 1


n
X
1
n
1
1
1
1
=
+
+
+
.
k(k
+
4)
4
n
+
1
2(n
+
2)
3(n
+
3)
4(n
+
4)
k=1
3. The sequence (an )
n=0 satisfies the recurrence
an = 6an1 9an2 ,

n 2,

and the initial conditions a0 = 4, a1 = 9. Prove by induction


that an = (n 4)3n for each n.
4. Prove by induction that for every n 1
n
X

3
k=1

k>

3
n3n.
4

(Can you provide a non-inductive proof as well?)


5. Prove by induction that every n 24 can be expressed in the
form n = 5a + 7b for appropriately chosen non-negative integers a
and b.
6. A finite number of straight lines are drawn in the plane. Prove,
by induction on number of lines, that it is possible to color the
1

resulting map by two colors in such a way that any two neighboring
states will have different colors.
7. There are n lines in the plane in general position (i.e., no two of
them are parallel and no three meet at the same point). Prove that
the number of states in the resulting map is n(n+1)
+ 1.
2
8. There are n pairs of socks of different colors in a drawer. How
many need to be drawn to make sure that they will contain at least
one pair?
9. Let a1 , a2 , . . . , an be any integers. Show that there exist 1
i j n such that the sum ai + ai+1 + . . . + aj is divisible by n.
10. Prove that, for any finite set of (at least two) people, there are
at least two people in the set who know exactly the same number of
people from the set. (Assume that if A knows B then B knows A).
11. Prove that, in any group of six people or more, either there are
three each of whom knows both of the two others or there are three
each of whom knows none of the two others.
12. For every subset of {rreflexive, symmetric, transitive}, construct a relation with the properties in this partial set, but not with
the other properties.
13. For each of the following relations on a set A, determine
whether it is reflexive, symmetric, transitive:
(a) A the set of integers, aRb if b a is divisible by m (where m
is a fixed positive integer)
(b) A the set of integers, aRb if |b a| 5.
(c) A the set of reals, aRb if |b a| 0.001.

(d) A the set of vectors in Rn , aRb if every component of b a


is integer.
(e) A the set of vectors in R3 , aRb if a is longer than b.

(f) A the set of integers, aRb if a divides b.


(g) A the set of integers, aRb if a and b are relatively prime (i.e.,
have no common divisor greater than 1).
(h) A the set of integers, aRb if a and b have the same prime
factors.
(i) A the set of integers, aRb if |a b| is not a prime.
2

(j) A the set of n n matrices with rational entries, aRb if there


exists a matrix c A with det(c) = 1 such that b = ca.
(k) A the set of n n matrices with rational entries, aRb if there
exists an invertible matrix c A with b = c1 ac.
(l) A the set of Hebrew words, aRb if the number of letters of b
is not less than that of a.
(m) A the set of Hebrew words, aRb if the words a and b contain
at least one common letter.
(n) A the set of Hebrew words, aRb if the words a and b contain
at least one common letter at the same position.
14. Prove that for any partial ordering of a finite set there exists a
minimal element.
15. Describe the transitive closure for the following relations:
(a) A the set of integers, aRb if a + b is divisible by m (m 2 a
fixed integer).
(b) the relation from Question 13.b.
(c) A the set of all points in the plane, aRb if the distance between
a and b is at most 1.
(d) the relation from Question 13.g.
(e) A ={1,2,3,4,5}, R = { (1,5), (2,3), (4,1), (5,3)}.
(f) A the set of vertices of a finite undirected graph, aRb if a = b
or there is an edge from a to b.
(g) A the set of all permutations of {1, 2, . . . , n}, aRb if it is
possible to pass from a to b by exchanging two adjacent entries.
(h) A the set of integers, aRb if |a b| = m, where m is an
arbitrary fixed positive integer.
(i) A the set of integers, aRb if the greatest common divisor of a
and b is greater than 1.
16. There are n functions f1 , f2 , . . . , fn in a Java program. Define a
graph on the set of these functions as follows: there is an edge from
function fi to function fj if fi calls fj . For example, a function f is
directly recursive if there is an edge from f to itself, and recursive
if there is a path from f to f . Let M be the adjacency matrix of
the graph (Mij = 1 if there is an edge from fi to fj and Mij = 0
otherwise).
3

(a) Show that fi is recursive iff (M n )ii > 0 for some positive integer n.
(b) Show that the set of functions that have to be put in every
program that uses fi is: {fj : there exists n such that Mijn > 0}.
17. A path in a directed graph is Hamiltonian if it passes exactly
once through each vertex of the graph. (It is not necessary that there
exists an edge from last vertex of the path to the first). Prove by
induction that for any finite graph the following is true: If between
every pair of vertices there is an edge in at least one direction, then
the graph contains a Hamiltonian path.

Decidable and Undecidable Problems

18.
(a) Present a machine which, given a sequence of digits representing a non-negative integer in base 10, starting with the least
significant digit, determines whether this integer is even or odd.
(b) Same as the preceding part if the digits are given starting with
the most significant.
(c) Same as part (a) for the problem of divisibility by 125.
(d) Same as part (a) for the problem of divisibility by 3.
19.

The ring of Gaussian integers consists of the subset


Z[i] = {a + bi : a, b Z}

of the complex plane, with the operations of addition and multiplication. An element z Z[i], distinct from 0, 1, i, may be either
prime or composite; it is composite if it may be written as a product
of two such numbers, and prime otherwise.
(a) Present an algorithm to decide whether a given z Z[i] is prime
or not.
(b) Show that every element z Z[i], distinct from 0, 1, i, is
a product of finitely many primes. Present an algorithm for
finding such a factorization.
20. Show that the 1-dimensional tiling problem, namely that of
tiling the line by finitely many types of unit intervals, is decidable.
4

21. Show that if a certain tiling system enables tiling some m n


rectangle, with m, n 2, in such a way that the rightmost column
is tiled the same as the leftmost column and the top row is tiled the
same as the bottom row, then there exists a legal tiling of the whole
plane.
22.

Consider the diophantine equation x2 + y 2 = a.

(a) Prove that it has no solutions if a is divisible by one of the


primes p = 3, 7, 11, but not by its square.
(b) Prove that the set of integers a for which the equation is solvable
is closed under multiplication.
23. Prove that there exist infinitely many positive integers a for
which the diophantine equation x2 + y 2 + z 2 = a has no solutions.
(Hint: Consider the equation modulo 8.)
24. Prove that for most integers a the diophantine equation
x4 + y 4 + z 4 = a has no solutions.

3
25.

Alphabets and Languages


Characterize those pairs of words x, y such that:

(a) xy = yx.
(b) xyx = yxy.
(c) xyx = yxy 2 .
(d) xy 2 x = yx2 y.
26.

Define words an and bn in {0, 1} for each n 0 as follows:


a0 = 0,
an = an1 bn1 ,

b0 = 1
bn = bn1 an1 ,

n1.

(a) Find the length of an and bn for each n.


(b) Show that, for each n, the word bn is obtained from an by
replacing each 0 by a 1 and vice versa.
(c) Show that both words an and bn are palindromes for each even
n. What happens for odd n?
(d) Show that, for each n, the word an does not contain the strings
000 and 111.
5

(e) Show that an is cube-free for each n (namely, an contains no


string of the form www, where w 6= ).
27.

Let L1 and L2 be languages.

(a) Show that if L1 L2 then L1 L2 .

(b) Conclude that for any L1 and L2 we have L1 L2 (L1 L2 ) .


(c) Show that it is possible to have L1 L2 6= (L1 L2 ) .

(d) Find languages L1 and L2 , neither of which contains the other,


such that L1 L2 = (L1 L2 ) .
28.

Let L1 , L2 , L3 be languages.

(a) Show that


(L1 L2 ) L3 = L1 L3 L2 L3 .
(b) Show that the following is not necessarily true:
(L1 L2 ) L3 = L1 L3 L2 L3 .
29. Give an example of infinite languages L1 , L2 satisfying L1
L2 = and L1 L2 = L2 L1 .
30. Denote by LC the complement language
of L . Is it


C
possible that LC =
6 (L ) ? that LC = (L )C ?

31.

Prove by induction that:


R
(a) wR = w for every word w.
n
(b) wR = (wn )R for every word w and n 0.

32.

Prove that:

(a) if L then (L ) = L .

(b) if L1 , L2 then (L1 L2 ) = (L1 L2 ) . Show that it is


impossible in general to replace the right hand side by (L 1 L2 ) .
Under what conditions can we do it?
(c) if L1 , L2 and L1 then (L1 L1 L2 ) = L1 (L1 L2 L1 ) .
33.

How many subwords does a word of length n have?

Regular Expressions

34. Replace the following regular expressions by simpler regular


expressions representing the same languages:
(a) (0 ((1(1 ))0)).

(b) (((0 )(1 )) ((0 1) )).

(c) ((((0 )1) ) (((01 10)0) )).

(d) (a ) (b ) (ab ) (a b ) .
(e) a bb a.

(f) (a b) (ba ) .

(g) (a b) a(a b) .

35. Let = {0, 1}. Show that the following languages over are
regular:
(a) The collection of all words containing an even number of 0s.
(b) The collection of all words containing an odd number of 0s.
(c) The collection of all words in which the number of 0s is divisible
by 3.
(d) The collection of all words containing at least three 0s.
(e) The collection of all words in which the block 01 appears exactly
once.
(f) The collection of all words in which the block 00 appears exactly
once.
(g) The collection of all words in which the block 000 appears exactly once.
(h) The collection of all words ending with 10.
(i) The collection of all words not ending with 10.
36. How many words of length n do the languages, corresponding
to the following regular expressions, contain? (In each case is the
set of letters appearing in the expression.)
(a) (01 ) .
(b) 0 1 .
(c) 0 1 2 .
7

(d) 0 1 0 .
(e) 0 1 2 3 4 5 6 .
(f) (0 10) .

(g) (0 11 22) .

(h) (01 02 12 000 111) .


(i) 0 1 (01) .

(j) (1 011 ) .

(k) (01 10) .


37. Show that, if L is a regular language, then so are the following
languages:
(a) LR .
(b) The language consisting of all suffixes of words in L.
(c) The language consisting of all words in L containing the letter
(where is an arbitrary fixed letter in ).
(d) The language consisting of all words in L not containing the
letter (where is an arbitrary fixed letter in ).
(e) The language consisting of all words of even length in L.
(f) The language obtained from L by taking only the words starting
with the letter and deleting it:
L = {w : w L} .
(g) The language obtained from L by deleting the last letter in
every non-empty word:
L0 = {w : w L} .
(h) The language obtained from L by doubling the last letter in
every non-empty word:
L0 = {w 2 : w L} .
(i) The language obtained from L by taking, for each word in L,
all words obtained from it by adding a single to it:
L0 = {w1 w2 : w1 w2 L} .

(j) The language obtained from L by taking, for each word in L,


all words obtained from it by adding any number of times the
letter :
[
L0 =
{} {1 }{} {2 }{} . . . {} {k }{} .
1 2 ...k L

38. The -height of a regular expression is inductively defined as


follows:
h() = 0.
h(a) = 0,

a .

h(( )) = h(()) = max(h(), h()).


h( ) = h() + 1.

For example, h((0 1 101) 01 0 1 ) = 2 .


For each of the following regular expressions, find a regular expression of smaller -height representing the same language:
(a) (0 1 01) .
(b) (012 1 0) .

(c) (01 ) .
(d) (00 1 1 0 102 1) .

(e) ((012) 01) .


(f) (0(1 2) ) .

(g) ((01) (10) ) .

(h) ( ) ( an arbitrary regular expression).

(i) ( ) (, arbitrary regular expressions).


39. Let , be regular expressions. Assume that the language corresponding to does not contain the word . Consider the following
equation:
x = x .

(Namely, a regular expression x for which both sides represent the


same language is sought). Find the solution x and show it is unique.

Deterministic Finite Automata

40. Consider the automaton M given in the figure, and:


a

a
a

b
b

(a) Find three words accepted by M .


(b) Write the configuration sequences corresponding to these words.
(c) Find three words not accepted by M .
41. Describe (in words or by a regular expression) the languages
accepted by the following deterministic finite automata:
(a)

a
b

a,b

a,b

a,b
a

10

(b)
a,b

a,b
a
a,b
b
a
b

(c)
a,b
a,b

a
a

(d)
a,b

a,b
a,b

(e)
a

b
a

a,b

11

(f)
a,b
b

a
a

a
b

(g)
a
a
b

a
a

a,b

(h)
b

b
a

a
a

(i)
a

a
b

b
b

a
b

12

42. Let = {a, b}. Construct DFAs accepting the following languages:
(a) All words of even length.
(b) All words of odd length.
(c) All words of length 2 modulo 5.
(d) All words in which the number of as is 2 modulo 5.
(e) All words in which the number of as is 2 modulo 3 and the
number of bs is 2 or 3 modulo 4.
(f) Any given finite language.
(g) Any given language whose complement is finite.
(h) All words not containing ba as a subword.
(i) All words not ending with aab.
(j) All words ending with neither ab nor aabb.
(k) All words containing abab as a subword.
(l) All words containing both ab and ba as subwords.
(m) All words containing neither a2 nor b2 as subwords.
(n) All words in which the third letter from the right is b.
(o) All words with at most one occurrence of a2 and one occurrence
of b2 .

Non-Deterministic Finite Automata

43. Describe (in words or by a regular expression) the languages


accepted by the following automata:
(a)

a
b

(b)

13

(c)

(d)

aba

b
bb

(e)
a
a

b
b
a

(f)
a

b
a

a
a

14

(g)
bb

a,aba
aaa
,ba

ab

(h)

ab
,a

3
a

a,b

a
a,ba

ab

b
,b

44.
(a1) Construct an NFA accepting the language consisting of all
words over {a, b} ending with aaba.
(a2) Construct a DFA accepting the same language.
(b) Do the same for the language (ab a3 aba) .
45. Construct NFAs accepting the following languages:
(a) (101 010) .

(b) 01(10 001) 101.


(c) (10 101)1 .

(d) (00 1) (01 0) .

(e) ((0 1 0 ) 1) .

(f) (01 110) (1101 010) .


46. The concept of an NFA may be generalized in such a way that,
instead of having a single initial state s, we shall have some set
15

S Q, each of whose elements may be the initial state. Show


that the family of languages accepted by all automata using this
extended definition is identical with that obtained by using only
conventional NFAs.
47.
(a) Show that, if we restrict our attention to automata satisfying
A = {s}, then the family of accepted languages is different than
that obtained by using all automata.
(b) What family of languages is obtained by automata in which A
consists of a single state?
(c) What family of languages is obtained by automata in which
s
/ A?
48. Construct an equivalent DFA for each of the following NFAs:
(a)
ab
;
bba
(b)
ab
;
, b

(c)

, a
;

a, b

16

(d)
b
a,b
a
3

3
ba

49. Let L be a language accepted by a finite automaton. Show


that the following languages are also accepted by finite automata:
(a) LR .
(b) The language consisting of all words in L whose length is l
modulo k for some 0 l < k.
(c) The language consisting of all words in L containing the letter
a at least twice and at most 4 times.
(d) The language consisting of all prefixes of words in L.
(e) The language consisting of all suffixes of words in L.
(f) The language consisting of all subwords of words in L.
(g) The set of subsequences of all words in L. (Here a word w2
is a subsequence of a word w1 if w1 = 1 2 . . . k for some
1 , 2 , . . . , k and w2 = i1 i2 . . . il , where 1 i1 < i2 <
. . . < il k.)
(h) All words in L which are not strict prefixes of words in L, that
is {w L : L ({w}( {})) = } .
(i) The right quotient L/L1 of L over some language L1 , where:
L/L1 = {w : w1 L1 , ww1 L}.

(j) L0 = {w1 2 : w1 L, w1 , }.

50. Given two automata M1 , M2 , with state sets Q1 , Q2 , respectively, construct an automaton M3 whose state set is Q1 Q2 such
that L(M3 ) = L(M1 ) L(M2 ).
51. A language L is absolute if there exists a positive integer l such
that, for any word w , belonging to L depends only on the l
last letters of w.
17

(a) Prove that every absolute language is accepted by a finite automaton.


(b) Show that the family of absolute languages over is closed
under finite unions, finite intersections and complementation,
but not under concatenation and Kleene-.
52. Let 1 , 2 be some alphabets. Let h : 1 2 be any
function. We can extend h to a function defined on all 1 by the
following inductive definition:
h() = ,
h(w) = h(w)h(),

w 1 , 1 ,

(where the extension of h is denoted by h as well). Notice that h is


well defined and
h(w1 w2 ) = h(w1 )h(w2 ),

w1 , w2 1 .

An h satisfying these properties is a homomorphism from 1 to 2 .


(a) Show that, if a language L 1 is accepted by a finite automaton, then the language h(L) = {h(w) : w L} 2 is also
accepted by a finite automaton.
(b) Show that, if a language L 2 is accepted by an FA, then
so is the language h1 (L) = {w 1 : h(w) L)} 1 .
(Hint: start from an FA accepting L and construct one with
the same state set. Given a letter 1 , act as in the original
automaton for the word h().)
(c) In section a (b, respectively), if it is given that L is not accepted by any FA, is it necessarily the case that h(L) (h1 (L),
respectively) is also not accepted by any FA?
53. Given a language L {a} , denote P (L) = {n : an L}.
Prove that L is accepted by an FA if and only if the sequence P (L) is
eventually periodic, that is, it satisfies the following condition: there
exist a non-negative integer n0 , a positive integer d and integers
0 d1 < d2 < < dl < d such that for n n0 we have n P (L)
if and only if n mod d = di for some 1 i l.
54. Given a language L , denote P (L) = {|w| : w L}.
(a) If L is known to be accepted by an FA, what can you say about
P (L) in view of exercises 52.a and 53?
(b) Show that the condition you found in the preceding part on
P (L) is not sufficient for L to be accepted by an FA.
18

55. Write a program which determines whether some words are


accepted by a given NFA. The data regarding the NFA is given in
a text file named automaton.txt, as follows. The first line of the file
contains a positive integer, which is the number of states of the NFA.
If the number is n, then Q = {q1 , q2 , . . . , qn }. The alphabet is (a
subset of) {a, b, . . . , z}. The initial state is s = q1 . The second line
of the input file contains a few positive integers. If these integers
are i1 , i2 , . . . , ik , then A = {qi1 , qi2 , . . . , qik }. The set of rules is
given by all other lines of the file, from the third on, each rule in
a separate line. Each line consists of an integer i, a single space, a
word w over , another space and then another integer j. The data
above signifies that (qi , w, qj ) . If w = then the integer i is
succeeded by two spaces and then the integer j. The program asks
the user to supply words (from the keyboard), and for each such
word decides whether it is accepted by the NFA or not (and sends
a message accordingly). If it is accepted, then the program further
provides a sequence of configurations showing how to get from s to a
final state. Each configuration is given in a separate line composed
of an integer i (meaning that the automaton is in state qi ) and a
word w (still to be read) preceded by a single space.
56.
(a) Present a simple algorithm which, given a DFA, determines
whether the language it accepts is finite or infinite.
(b) Implement the algorithm in a computer program.
57. Write a program which, given an NFA, constructs an equivalent
DFA.
58. The well-known Towers of Hanoi problem gives rise to several
languages, as follows. Let n, the number of disks, be arbitrary and
fixed. Consider the 6-letter alphabet
= {(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)} .
Any letter (i, j) should be interpreted as the instruction
move the disk on top of peg i to the top of peg j. A word in
is thus a sequence of instructions. For example, if n = 2, the
word (1, 2)(2, 3)(1, 2)(3, 2) represents a sequence of instructions for
moving both disks from the initial position, in which both are placed
on peg 1, to the final position, in which both are placed on peg 2
(although this is done non-optimally). The word (1, 2)(1, 3)(2, 3)
is another word which represents a sequence of legal moves, but
does not lead at the end to the required final position. The word
(1, 2)(1, 2)(2, 3) represents an illegal sequence of moves, as at the
second step disk 2 is placed above disk 1.
19

(a) Let Llegal denote the language consisting of all words


representing sequences of instructions which, when starting
from the initial position (all disks on peg 1), correspond to
legal sequences of disk moves. (Thus, for n = 2, we have
(1, 2)(1, 3)(2, 3) Llegal , whereas (1, 2)(1, 2)(2, 3)
/ Llegal .) Explain why Llegal is accepted by a finite automaton. Write a
computer program which reads the number of disks n (where
n may be assumed to be bounded by some large constant) and
gives an automaton accepting Llegal . The format of the automaton is as described above.
(b) The same for the language Lgood Llegal , consisting of those
words representing sequences of instructions leading in a legal way from the initial position to the final position. (Thus,
for n = 2, we have (1, 2)(2, 3)(1, 2)(3, 2) Lgood , whereas
(1, 2)(1, 3)(2, 3)
/ Lgood .)
59. Use the idea of the proof that every regular language is accepted
by an FA to construct FAs accepting the languages corresponding
to the following expressions:
(a) a ba ab .
(b) (a b )c .

(c) a (b a3 b2 )b .

(d) a bb (a a2 ba)ba .
(e) ba (a bb)a b

(f) ab(((ab) b3 ) a) .

(g) ((a b)2 ) ((a b)3 ) .

(h) a((a2 b) (ac) (b c) )b2 .

(i) b((b2 ab ab) )a .

(j) ((ab) (bc) )ab.

(k) ((a b) ( c) ) .

(l) a (ab ba )b .

60. Use the idea of the proof that every language accepted by
an FA is regular to find regular expressions corresponding to the
languages accepted by the following automata:

20

(a)
a, b
;
a, b

(b)

a
a, b
;

a, b
b

(c)
a
b

b
b

b
a

(d)

b
b

b
a

21

(e)
a, b
a
;

a
a

(f)
a, b
a
a

a, b
b

(g)
a

a
;

a, b

b
a

(h)
a

b
a, b

a b

;
b

61. Show directly (without using automata) that, if a language L


is regular, then so is the language LR = {wR : w L}.
62. Show that, if L is regular, then so is {w : w L, w R L}.
22

63. Given a language L define: L1 = {vw : v L, w 6 L}.


(a) Show that if L is regular then so is L1 .
(b) Show that the converse is not necessarily true.
64. Suppose we have an eventually periodic infinite sequence of
letters from :
1 , 2 , . . . , n0 , n0 +1 , . . . , n0 +p1 , n0 , n0 +1 , . . . , n0 +p1 , . . . .
Put: L = {w : w = m m+1 . . . n , m, n 1} (where w =
for m > n). Show that L is regular.
65. Show that, if a regular language L is infinite, then there exist
regular infinite languages L1 , L2 such that L = L1 L2 and L1 L2 =
.

Pumping Lemma

66. Show that the following languages are not regular:


(a) {an bn+1 : n 0}.
(b) {an b2n : n 0}.
(c) {am ban bam+n : m, n 0}.
(d) {an1 bn2 cn3 dn1 +n2 +n3 : n1 , n2 , n3 0}.

(e) All words in {a, b} in which the number of as is distinct from


the number of bs.
(f) {am bn cn : m, n 0}.

67. Prove the following strengthened version of the pumping lemma:


Let M be a finite automaton and let x be any word in L(M ) with
|x| |Q| (where |Q| denotes the number of elements of Q). Then
there exist words u, v, w such that uvw = x, |uv| |Q|, v 6=
and uv n w L for every n 0.
68. Use the preceding exercise to show that the following languages
are not regular:
(a) {w2 : w },

(b) {wk : w },

(|| 2).
(|| 2, k 2).
23

(c) {wwR : w },

(|| 2).

(d) {ww : w {a, b} }, where w is the word obtained from w by


replacing each a with b and vice versa.
69. Prove the following strengthened version of the pumping lemma:
Given a regular language L there exists a constant s such that, if
z1 , z2 , z3 are any words with z1 z2 z3 L and |z2 | = s, then there
exist words u, v, w with z2 = uvw and v 6= such that z1 uv n wz3 L
for every n 0.
70. Use the preceding exercise to show that the following languages
are not regular:
(a) {am bn : m, n 0, gcd(m, n) > 1}. (Recall that gcd(m, n) is
the greatest common divisor of m and n.)
(b) {am bn : m, n 0, gcd(m, n) = 1}.
71. Which of the following languages are regular and which are
not:
(a) {an

3 +2n2

: n 0}.

(b) {1 2n 3 : 1 , 2 , 3 , 1 6= 2 , 1 6= 3 , 2 6= 3 , n 2}.

(c) {wwR w : w {a, b} }.

(d) All words in {a, b} in every proper prefix of which the difference
between the number of as and the number of bs does not
exceed 100 in its absolute value. (Thus, for the word itself the
difference in question may be 101.)
(e) {am bn : m, n 0, m n 2m}.
(f) All words over the Hebrew alphabet starting with a final letter
and ending with a non-final letter.

(g) All words over the Hebrew alphabet in which no letter occurs
more than three times.
72. Given a set of non-negative integers, the set of their expansions
in base 10 forms a partial language of {0, 1, . . . , 9} . For each of the
following sets show that the corresponding language is regular or
not (as indicated):
(a) All even numbers (regular).
(b) All numbers divisible by 10 (regular).
(c) All numbers congruent to 333 modulo 625 (regular).
24

(d) All numbers divisible by 3 (regular. Hint: You may use the
fact that an integer is divisible by 3 if and only if the sum of
its digits is such.)
(e) All numbers divisible by 11 (regular. Hint: A number is divisible
by 11 if and only if the alternating sum of its digits is such. For
example: 379181737 is divisible by 11 since 3 7 + 9 1 + 8
1 + 7 3 + 7 = 2 11.)
(f) All numbers congruent to r modulo d, where 0 r < d (regular).
(g) All powers of 10 (regular).
(h) All powers of 2 (not regular. Hint: Use the pumping lemma
and the fact that no power of 10 but 100 is a power of 2.)
(i) All perfect squares (not regular. Hint: Start with special perfect
squares.)
(j) {an : n 0} where a0 = 3 and an = a2n1 for n 1 (not
regular).
(k) {2m 5n : m, n 0, |m n| 10} (regular).
73. With definitions as in the preceding exercise, determine whether
the following sets are regular:
(a) {10m + 10n + 1 : m > n 1}.
(b) {102n + 10n + 1 : n 1}.
(c) {27 10m + 19 10n + 53 : m > n + 3, n > 2}.
(d) {27 10m 19 10n 53 : m max(2, n)}.
2

(e) {3n : n 0}.


(f) {n! : n 0}.
(g) The set {an : n 0}, where

n = 0,
0,
an1 + 1,
n 1, an1 has an odd number of digits,
an =

an1 + 2,
n 1, an1 has an even number of digits.

(g) The set of all Fibonacci numbers {Fn : n 0}, where F1 =


F2 = 1 and Fn+2 = Fn + Fn+1 . (You may use the fact that no
power of 1+2 5 is an integer.)
(h) All perfect cubes.

25

74. Given a word w {a, b} , denote Ra,b (w) = #a (w)/#b (w),


where # (w) is the number of occurrences of in w. (Thus, Ra,b ()
is undefined, and we may take Ra,b (w) to be infinite for w {b}+ .)
A non-negative real number r (or ) is a limit ratio for a language
L {a, b} if there exists a sequence (wn )
n=0 of distinct words in
L such that Ra,b (wn ) r. Denote by Ra,b (L) the set of all limit
n

ratios for L. For example, Ra,b ({a, b} ) = [0, ] and Ra,b (L) = [, ]
if L = {w {a, b} : Ra,b (w) } (for any 0 ).
(a) Show that, if F is any finite set of (non-negative) rationals,
then there exists a regular language L with Ra,b (L) = F .

(b) The same for any interval with rational endpoints.


(c) Prove that, if L is a regular language such that Ra,b (L) contains
an irrational, then Ra,b (L) contains a sequence of rationals converging to it. (Hint: You need to pump large parts, perhaps
not consecutive, of given words.)

Context-Free Grammars

75. Find L(G) for each of the following grammars:


(a) S | abSba.
(b) S SS | a | b.
(c) S aS | b.

(d) S ba2 b3 | a2 bSb.

(e) S SS | aS | ab.
(f) S | aaSb | bbSa.
(g) S bA | abA ,
A | S.
(h) S aAb ,
A | S | Sb.
(i) S aAb | bAa ,
A | S | aSa.
(j) S aA | bS | b ,
A aB | bA ,
B aS | bB | a.
26

(k) S aA | bB ,
A bB ,
B | S.
(l) S AB ,
A aS | bB | ,
B bS | aA | .
(m) For any alphabet = {1 , 2 , . . . , m }:
S ,

S i Sj , 1 i, j m.

(Thus, R contains m2 + 1 elements.)


76. Show that the following grammars accept the indicated languages:
(a) S aS | aSbS | .
L(G) all words in which the number of as in any prefix is at
least as large as the number of bs in that prefix.
(b) S | aSb | bSa | SS.
L(G) all words in which the number of as is equal to the
number of bs.
(c) S aB | bA ,
A a | aS | bAA ,
B b | bS | aBB.
L(G) all words of positive length with the same number of
as and bs.
(d) S SS | aSb | c

L(G) all non-empty words in {a, b, c} satisfying:


i) The number of as is equal to the number of bs.
ii) In each prefix, the number of as is no less than the number
of bs.
iii) ab is not a subword.

(e) S SS | aSb | aSbc | .

L(G) all words in {a, b, c} satisfying:


i) The number of as is equal to the number of bs.
27

ii) In each prefix, the number of as is no less than the number


of bs.
iii) Every c is preceded by a b.
77. Recall that the grammar
S S  | aSb | SS
accepts the language consisting of all words in {a, b} satisfying:
i) The number of as is equal to the number of bs.
ii) In each prefix, the number of as is no less than the number of
bs.
Write a computer program which, given a word w in {a, b} determines whether it satisfies these two properties, and if so provides
a sequence of productions leading from S to w.
78. Construct context-free grammars accepting the following languages:
(a) {am+3 b2m+1 : m 0} {a3m+1 b2m : m 0}.

(b) {am bn cn dm e3 : m, n 0}.


(c) {am bn : 0 m n}.

(d) {ai bj ck dl : i, j, k, l 0, i 6= l, j > k}.

(e) {am bm+n an : m, n 0}.

(f) {ai bj ck dl : i + j = k + l}.


(g) {am bn : m n 3m}.

(h) {am bn : m = 3n n = 3m}.

(i) {wwR : w } ( an arbitrary alphabet).

(j) {w : wR = w} ( an arbitrary alphabet).

m m
(k)
m=2 L ((a b) (b a) ).

m
m
(l)
m=0 L ((a b) c(b a) ).

2m
(m)
a(a c)m ).
m=0 L (a b

(n) {al bm cn : l, m 1, n = |l m|}.

(o) All words in {a, b} starting with an a and containing an even


number of as.
(p) All words of length 2 modulo 5 over any alphabet .

(q) All words in {a, b, c} in which a and b appear exactly once


each.
28

(r) All words in {a, b} not containing the word aaa as a subword.
(s) {w : wR 6= w} ( an arbitrary alphabet).

(t) All words in {a, b, c, d} starting and ending with an a such that
between any two consecutive as there is an even number of bs.
(u) {avbw : v, w {a, b} , |v| = |w|}.
(v) {wcwR : w {a, b} }.

(w) All words in {a, b, c, d} satisfying the following conditions:


i) All as appear before the bs and the cs.
ii) All ds appear after the bs and the cs.
iii) The number of as is equal to the number of ds and is
positive.
iv) The number of cs is even and positive.
79. Consider the following grammars:
1. S a | aS | bSS | SbS | SSb.
2. S bSa | aSb | SS | ba | ab | .
3. S b | bS | aSS | SaS | SSa.
(a) Prove that the language accepted by the first grammar consists
of all words over {a, b} containing more as than bs, by the
second all words containing the same number of as and bs,
and by the third all words containing less as than bs.
(b) Write a program which reads words over {a, b}, determines to
which of the three languages each word belongs, and then shows
step by step a sequence of productions which leads from S to
the required word.

Regular Grammars

80. Construct DFAs accepting the same languages as the following


grammars:
(a) S aA | b2 B | ca2 b,
A bA | ab | aB | cac,
B ba | A | ba2 B.
29

(b) S | A | ba2 A | ab2 B,


A bS | ba,
B a2 S.
(c) S aS | bS | bA,
A bB,
B bC,
C aC | bC | .
81. Construct regular grammars accepting the same languages as
the following DFAs:
(a)
a
a

b
b

c
a, b
(b)
b
a
b
a

b
b

a
a

30

(c)
b
a
a

b
a

82. A context-free grammar is left-regular if R N (N {}) .


Prove that a language is regular if and only if it is accepted by a
left-regular context-free grammar.
83. Show that, if L is a context-free language, then so is LR .
84. Consider the following condition for a context-free grammar:
R N ( (N {}) N ) .

Is a language accepted by a grammar satisfying this condition necessarily regular?


85. Construct regular grammars accepting the same languages as
the following automata:
(a)
ba

a
a
b

ba

b
a

31

(b)

a a

a
a
b

c
b

a
(c)
b, c
a, b, c
c
a

10

Pushdown Automata

86. Describe the languages accepted by the following pushdown


automata:
(a)
a, |a
b, a| c, a|
c, a|
, a|

32

(b)
a, |a
, |

, |

b, a|
, |

, |
a, |a2

(c)
a, |a

a, a|
a2 b, |

b, |a

b, a|

(d)

a, |a3
2

a, a |
a, |a4

a, a2 |

a, a4 |

(e)
a, |a2

a, |a

a, |a
c, a|
ab, a|

b, a2 |

33

(f)
c, a2 |
a, |a
b, |a
b, |a3
(g)
b, a|
a, |a b, a|
c, a|

c, |

c, |
c, a|

87. Construct pushdown automata accepting the same languages


as the following grammars:
(a) S | SS | AS | SB,
A aB | a2 ,
B b2 S | baA | b2 .
(b) S aS | ASB | ScS,
A cAc | bBb | ba,
B Sb | AcA | BB.
88. Construct context-free grammars accepting the same languages
as the following pushdown automata:
(a)
a, |a
b, |a
c, a|

34

(b)
a, |a
, |

b, a|

89. Write a program receiving as its input a pushdown automaton


and producing a grammar accepting the same language. The automaton is defined in a text file named PDA as follows: In the first
line a positive integer giving the number of states of the automaton. The states are 1, 2, . . . . In the second line a sequence of
arbitrary length of characters; this is the alphabet . In the third
line , given similarly. Fourth line the initial state number.
Fifth line a sequence of numbers; these are the accepting states.
Sixth line on the elements of . These are given one per line, in
the order p, w, , q, , where p, q are positive integers (states), w is
a word over , and , are words over . These five components
are separated by single spaces from each other. The empty word is
denoted by 0 (assume that and do not include the characters
0 and space). The output of the program consists first of a file
called simple, containing a simple automaton equivalent to the original one. This file has in principle the same format as the file PDA.
The program also generates a file named grammar, where the grammar is described: In the first line appears the title non-terminals,
and thereafter in each line one non-terminal character of the grammar is written. Each non-terminal appears in the form < p, A, q >,
where p, q are numbers (signifying states) and A is either a letter
or 0 (the empty word). In the next line we have the heading rules,
and all the following lines are devoted to stating the rules, each rule
being on a separate line.
90. Write a program receiving as its input a context-free grammar
and producing a PDA accepting the same language. Use representations similar to those of the preceding exercise.

11

Closure Properties; Non-Context-Freeness Proofs

91. Use the property of closure under union to show that the following languages are context free:
(a) {am bn : m 6= n}.

(b) {a, b} {an bn : n 1}.


35

(c) {ai bj ck dl : j = l i k i + j = k + l}.


(d) {w {a, b} : wR = w}.

92. Prove that the following languages are not context-free:


(a) {ap : p a prime }.
n

(b) {a2 : n 0}.

(c) {w {a, b, c} : |w|a = |w|b = |w|c } (where |w| denotes the


number of occurrences in w of the letter ).
2

(d) {an bn : n 0}.

(e) {www : w },

(|| 2).

(f) {am bm cn : 0 m n 2m}.


(g) {an ban ban : n 0}.

93. Are the following languages context-free?


(a) {am bm an bn : m, n 0}.

(b) {wwR w : w {a, b} }.

(c) {An Hc1 c2 . . . cn :


n N, ci {a, b}, 1 i n}
{0, 1, H, a, b} , where An is the binary expansion of n.

(d) {x1 y1 x2 y2 . . . xm ym : xi {a, b}, yi {


a, b}, m 1, yi = xi ,
1 i m}.

94. Let h : 1 2 be a homomorphism (see exercise 52). Prove


that:
(a) If L 1 is context-free then so is h(L) 2 .

(b) If L 2 is context-free then so is h1 (L) 1 .

95. Prove that the pumping lemma for context-free languages


can be strengthened as follows: Given a context-free grammar G,
there exist constants K = K(G), k = k(G), such that if w
L(G), |w| K, then there exist words u, v, x, y, z , with
|vxy| k, vy 6= and w = uvxyz, such that uv n xy n z L(G) for
every n 0.
96. Let L be a context-free language and R a regular language. Is
the language LR necessarily context-free? What about RL?
97. Are the following languages context-free? (You may use exercises 94 95.)
36

(a) {ww : w {a, b} }.


(b) {baba2 ba3 . . . ban1 ban b : n 1}.
l m 5n

(c) {a2 3

: l, m, n 0}.

(d) {apq : p, q primes}.


(e) {al bm cn : l 6= m l 6= n m 6= n}.
(f) {al bm cn : l = m l = n m = n}.
(g) {1a10, 10a11, 11a100, 100a101, . . . , An aAn+1 , : n 1}, where
An is the binary expansion of n.
98. Prove that the family of context-free languages is closed under
union, concatenation and Kleene- by starting in each case from
PDAs accepting the given languages and constructing a PDA accepting the language in question.

12

Turing Machines

99. For each of the following Turing machines, determine for which
inputs they eventually halt, for which they hang and for which they
never halt. In all cases = {B}.
(a) = {a, b}.
a|a, R
b|a, L
(b) = {a, b}.
a|b, R
b|a, L a|a, R
b|a, R

37

(c) = {a, b}.


B|a, a|a, R
b|b, L a|B, L
b|B, R

(d) = {a, b, c}.


c|a, L

a|b, R
c|a, L

b|c, R

b|B,

(e) = {a, b, c}.


a|a, R

b|B, L
B|a,
c|a, L

(f) = {a, b}.


a|b, R
a|B, R
b|b, R
b|B, R

B|b, L
a|a, R
B|a, L
b|a, R

100. Find the languages accepted by the following Turing machines:

38

(a)
a|b, R
b|B, R

b|b, L

B|a, L

B|b, R

(b)
a|b, R

a|a, L

a|a, L

b|a, L

a|b, L

B|B, L
b|a, R
(c)
a|a, R b|b, R
b|b, R
B|B,
a|b, R

c|b, L

b|b, L
(d)
a|b, R
b|b, R

b|a, R
B|B, L

a|b, L
b|b,

(e)
a|b, R
b|a, R
B|B,

39

(f)

|, R
a|B, R

B|B, R

B|B, L

|, L

b|B, L

B|B,
B|B,

(g)
a|b, R
B|B,
b|a, L

101. Construct Turing machines computing the following functions


(in each case = {a, b}):

|w| 2,
w,
(a) f (w) =

w3 , |w| 3, w = 1 2 . . . n .
(b) f (w) = 1 2 4 5 7 8 . . . ,

w = 1 2 . . . n .

(c) f (w) = w R .
(d) f replaces each occurrence of ab by a c (for example, f (baabaabb) =
bacacb).

wc, w contains the block abba,


(e) f (w) =

wd, otherwise.

(f) f leaves the letters


letters in the even
1 6 3 4 5 2 7 ).

wgood,
(g) f (w) =

wbad,

in the odd places but reverses the order of


places (for example, f (1 2 3 4 5 6 7 ) =
w L(a b ),
w 6 L(a b ) .

40


wgreater, |w|a > |w|b ,

wless,
|w|a < |w|b ,
(h) f (w) =

wequal,
|w|a = |w|b .

102. Explain how we can, given any homomorphism (see exercise 52) from to , construct a Turing machine computing
the homomorphism. Exemplify for the homomorphism defined by
h(a) = cacb, h(b) = bab.
103. Construct Turing machines accepting the following languages:
(a) a b2 a.
(b) a b a b .
(c) {an bn : n 0}.
(d) {an bn cn : n 0}.
(e) {an b2n cn+1 : n 0}.
(f) {am bm an bn : m, n 0, m 6= n}.
(g) {al bm an : m = l + n}.

(h) {w {a, b} : |w|a |w|b + 1}.

(i) The language consisting of all words over {a, b} in which the
number of as is equal to the number of bs and the number of
as in each prefix is not smaller than the number of bs in the
same prefix.
(j) {ap : p is prime}.
n

(k) {a2 : n 0}.


104. Determine, for every positive integer k, which functions of k
integer variables are computed by the following Turing machines:
(a)
0|1, R
B|0,
1|1, R
(b)
41

B|0,

|, R
0|?, R

1|1, R

B|1, L
?|1, R
|, L

105. Construct Turing machines computing the following functions:


n Z+ .

(a) f (n) = min{n, 2},


(b) f (n) = n2 ,

n Z+ .

(c) f (n) = [log2 (n + 1)],


(d) f (m, n) = mn,

n Z+ .

m, n Z+ .

(e) f (m, n) = (m + 1)n ,

m, n Z+ .

106. Consider the following grammar:


S AA,
A AAA | Aa | aA | b .
(a) Show that the language accepted by this grammar is composed
of all words in {a, b} with an even number of bs.
(b) Show that this grammar is ambiguous.
(c) Construct an equivalent unambiguous grammar.
107. Construct unambiguous grammars for the following languages:
(a) All words in {a, b, c} in which the letters a and b occur exactly
once.
(b) All words in {a, b} in which the string ab2 occurs at least three
times.
(c) All words in {a, b, c} satisfying the following conditions:
i) Any two consecutive as are separated by at least one b.
ii) Any two consecutive bs are separated by at least one a.
42

108. Same as the preceding question for the languages:


(a) {w : wR = w}.

(b) {w : wR 6= w}.

43

Potrebbero piacerti anche