Docsity

Worked Solutions for Automata, Exams of Theory of Automata

Toggle sidebar
bg1
Worked Solutions 1 - enumerability
Richard McKinley
March 13, 2005
1 Introduction
These are selected model answers to exercises from lecture handout 2. Each
section corresponds to a slide from that handout.
2Page12
Exercise 2.1. If A,Band Careenumerablesets,thesetoftriples
A×B×C={(a, b, c):a∈A, b ∈Bc ∈C}
is enumerable.
Proof. The product of two enumerable sets is enumerable. Writing A×B×C
as (A×B)×C, we see that it is the cartesian product of two enumerable sets:
A×Band C. Since A and B are enumerable, A×Bis enumerable So we see that
A×B×Cis the product of two enumerable sets, and hence enumerable.
Exercise 2.2. If A1,...,A
kare enumerable sets, then A1×...×Akis enu-
merable.
Proof. By induction. The base case (k=1)istrivial.
Now suppose that, for any enumerable sets A1,...,A
n,thesetA1×...×An
is enumerable. Then, given another enumerable set An+1 ,A1×...×An×An+1 =
B×A,whereB=A1×...×Anis enumerable. B×Ais the cartesian product
of two enumerable sets, and so is enumerable.
3Page13
Exercise 3.2. If Bis enumerable and there is a total injective function A→B,
then Ais enumerable.
Proof. If Bis enumerable, there is a coding of B- an total injective function f
from Bto N. We know we have a total injective function g:A→B,andtheir
composition f◦gis a function A→N. This function is total and injective (you
should check this, and in an exam we would expect you to prove it) and so Ais
enumerable.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Worked Solutions for Automata and more Exams Theory of Automata in PDF only on Docsity!

Worked Solutions 1 - enumerability

Richard McKinley

March 13, 2005

1 Introduction

These are selected model answers to exercises from lecture handout 2. Each section corresponds to a slide from that handout.

2 Page 12

Exercise 2.1. If A, B and C are enumerable sets, the set of triples

A × B × C = {(a, b, c) : a ∈ A, b ∈ Bc ∈ C}

is enumerable.

Proof. The product of two enumerable sets is enumerable. Writing A × B × C as (A × B) × C, we see that it is the cartesian product of two enumerable sets: A×B and C. Since A and B are enumerable, A×B is enumerable So we see that A × B × C is the product of two enumerable sets, and hence enumerable.

Exercise 2.2. If A 1 ,... , Ak are enumerable sets, then A 1 ×... × Ak is enu- merable.

Proof. By induction. The base case (k = 1) is trivial. Now suppose that, for any enumerable sets A 1 ,... , An, the set A 1 ×... × An is enumerable. Then, given another enumerable set An+1, A 1 ×.. .×An ×An+1 = B × A, where B = A 1 ×... × An is enumerable. B × A is the cartesian product of two enumerable sets, and so is enumerable.

3 Page 13

Exercise 3.2. If B is enumerable and there is a total injective function A → B, then A is enumerable.

Proof. If B is enumerable, there is a coding of B - an total injective function f from B to N. We know we have a total injective function g : A → B, and their composition f ◦ g is a function A → N. This function is total and injective (you should check this, and in an exam we would expect you to prove it) and so A is enumerable.

4 Page 14

Exercise 4.1. The set Q+^ is enumerable.

Proof. Let the function f : N × N → Q+^ take (a, b) to a b. This function is surjective,( for example, each rational number r has a form m n where m and n share no factors, and then (m, n) is a pre image for r) and we know that N × N is enumerable, (via Cantor’s zig-zag or an encoding c((m, n)) = pn^ ·^ qm^ where p and q are prime) so Q+^ is enumerable.

Exercise 4.4. The set A∗^ of strings over an enumerable alphabet A is enumer- able.

Proof. A is enumerable, and so we have an encoding cA : A → N. We will give a total injective function A∗^ → N × N. This will be enough to show that A∗^ is enumerable (Why?). A is enumerable, and so we have an encoding cA : A → N. From a previous exercise, we know that for each n ∈ N there is an encoding cn : An^ → N.

cn((a 1 ,... , an)) = cN×···×N(cA(a 1 ),... cA(an))

Regarding each string s of length n as an n-tuple, we have a function

cA∗^ : A∗^ → N × N,

cA∗^ (a 1 a 2 a 3... am) = (m, cm((a 1 ,... , am)) This is injective and total, so we are done. (You should verify yourself that this function is injective and total, and in an exam you should prove these claims).

5 Page 25

Exercise 5.2. Suppose that we have a programming language, such that every program describes a function N → N. Show that there must be functions N → N that are described by no program.

Proof. Any program can be thought of as a finite string from a finite alphabet of letters. We can therefore infer that the set of programs is enumerable. By contrast, the set of all functions N → N is non-enumerable (see lectures). So there is no surjective map from programs to functions (if there were, then the set of functions would be enumerable). In particular, the map that takes programs to the function they compute is not surjective, and hence there are functions for which there is no program.

An alternative solution is given below:

start

q 0 q 1 q 2

q 0 : String has no trailing zeroes. q 1 : String has one trailing zero. q 2 : String has two trailing zeroes.

  1. Here is a machine which accepts the given language.

start

q 0 q 1 q 2

q 0 : String contains no 00 , and the last symbol was a one q 1 : String contains no 00 , and the last symbol was a zero q 2 : String contains a 00

0

start q 0 q 1

1

0

1

(^0) q 2 q 3

0

1 1

q 0 : String contains no 0 11, and the last symbol was a one q 1 : String contains no 0 11, and the last symbol was a zero q 2 : String contains no 0 11, and the string so far ends 01 q 3 : String contains 011

Exercise 1.2. Give a DFA accepting the following language over the alphabet { 0 , 1 }: The set of all strings whose tenth symbol from the right is a 1.

Proof. Main Idea: The states of the machine track the last 1 0 symbols read by the machine.

Q = {x 1 x 2... xn | 0 ≤ n ≤ 10 , and xi ∈ { 0 , 1 } for all i ∈ { 1 ,... n}}

q 0 = ε (the empty string) Σ = { 0 , 1 } F (the accepting states) = { 1 x 2... x 10 | xi ∈ { 0 , 1 } for all i ∈ { 2 ,... 10 }}

δ(x 1 x 2... xn, y) =

{ x 1 x 2... xny if n < 10 x 2...^ xny^ if^ n^ = 1^0

Exercise 1.3. Consider the DFA with the following transition table:

0 1 → A A B ∗B B A

(1) Informally describe the language accepted by this DFA; (2)prove by induction on the length of an input string that your description is correct.

Proof. You should draw the transition graph before reading this answer; it will help with your understanding. We claim that the language of the DFA is {w | w contains an odd number of 1’s}. (Now you know about regular expressions, write this as a regular expression). The proof is by induction, but the induction hypothesis is stronger than you might expect: Claim:

ˆδ(A, w) =

{ A if w contains an even number of 1’s B if w contains an odd number of 1’s Base case w = ε δ^ ˆ(A, ε) = A, as ε has an even number of 1’s Inductive Step: We show the case for a string which ends in 1, and leave the case of a string ending in 0 to the reader as an exercise.

δ^ ˆ(A, w) = δˆ(A, v1) = δ(ˆδ(A, v1), 1) =

{ δ(A, 1) if v contains an even number of 1’s δ(B, 1) if v contains an odd number of 1’s

.

.

.

.

.

.

.

.

0 − 9

0 − 9

0 0 −^9

0 − 9

1

1

9

9

start

zero acc

one

nine

Exercise 1.

This example is similar to the last one. It helps to consider, for instance, a machine that will accept strings consisting of any number of the digits 1 − 9 and then a 0. Once you have done this the whole machine is made by duplicating the same structure for the digits 1 − 9.

.

.

.

.

.

.

.

.

start

ac0 ac

ac

no

no

no

If you consider the triangle of states { start,no0,ac0 } we can see that start will accept the empty string, ac will accept either the string 0 or a string of the symbols 1 − 9 (during which we stay in state no0 ) followed by a single 0.

Exercise 1.

The final exercise in the set is a little different from the others but no more difficult. The key is to consider the smallest strings that should be accepted and then to generalise the idea.

Exercise set 2

Convert the following NFA to a DFA:

0 1 → p {p, q} {p} q {r} {r} r {s} {} ∗s {s} {s}. It can help to draw the transition graph of the NFA and this is good practise: 0,

0,

0,

0

0 p q

s r

To convert the NFA to a DFA we use the powerset construction demonstrated in lectures and in an exam I would give a short explanation of this method. In what follows we are ignoring the unreachable states. We note that the start state of the DFA is defined to be the set containing just the start state of the NFA: {p}. To draw the transition graph of the DFA we start with this state and consider what set of states the NFA will enter from state p given input 0 or 1. We get the following:

{p}

1

0

{p,q}

We now have a new state {p, q} and we continue by assuming that the NFA is in the set of states {p, q} and considering what set of states it will enter on the inputs 0 and 1. This leads to:

{p}

1

0

{p,q} {p,r}

{p,q,r}

0

1

p q s

r t

0,

0

0 0

0

0

1

1

Again the DFA is found by the powerset construction. The machine is:

{p}

{p,q}

{p,t}

{p,q,r,s}

0

0

0

0

1

1

(^1 )

Describe informally the language accepted by this NFA. Firstly, the machines will only accept strings that end in 00 or in 01. One way of working this out is by considering what input strings can lead to each of the states in the DFA:

  • {p}: The string is the empty string, 1 or ends in 11.
  • {p, q}: The string ends in 10.
  • {p, q, r, s}: The string ends in 00.
  • {p, t}: The string ends in 01.

Only the final two states are accepting states. We now need to show that the machines accept all strings that end in 00 or 01. We can see this by looking at the NFA, it is clear that we will be in a set of states that contains the state p if we input any string of 0 ’s and 1 ’s. From the state p on input 00 or 01 we would enter a set of states containing an accepting state.

Worked Solutions 4: Regular expressions

Emma Jones

April 24, 2005

These are selected worked solutions to the exercises from lecture handout 5. Each section corresponds to a slide from that handout.

Exercise set 1

Write regular expressions for the following languages:

  1. The set of strings over alphabet {a, b, c} with at least one a and at least one b_._
  2. The set of strings of 0’s and 1’s whose tenth symbol from the right end is 1.
  3. The set of strings of 0’s and 1’s with at most on pair of consecutive 1’s. The first two are quite easy and you should be able to write them staight down. The third exercise is harder:

Exercise 1.

It can be far easier to write down a regular expression for a language if you first design either an NFA or DFA that accepts it. In this case we will use an NFA (why?). In the NFA there are four states corresponding to:

  • q: There have been no pairs of consecutive 1 ’s and the last symbol was a 0.
  • r: There have been no pairs of consecutive 1 ’s and the last symbol was a 1.
  • s: There have been one pair of consecutive 1 ’s and the last symbol was a 1.
  • t: There have been one pair of consecutive 1 ’s and the last symbol was a 0.

q (^) r s t

Exercise 2.

Once you have designed the machine to accept this language the process is exactly the same as above. The NFA is:

q r s t

Exercise 2.

This question is a little harder. It is easier if we work with the DFA this time:

q

r

t s

u

This time we have only one accepting state, state q. We need to consider the regular expres- sion denoting what is accepted after each loop:

  • After 0 loops: 1 ∗
  • After 1 loop: 1 ∗ 01 ∗ 01 ∗ 01 ∗ 01 ∗ 01 ∗
  • After 2 loops: 1 ∗( 01 ∗ 01 ∗ 01 ∗ 01 ∗ 01 ∗)^2
  • · · ·
  • After arbitarily many loops: 1 ∗( 01 ∗ 01 ∗ 01 ∗ 01 ∗ 01 ∗)∗

To see the first for instance, the only strings accepted by state q after no loops are strings of any number of 1 ’s (from the loop on state q). For the others we consider what input strings will take us from state q back to state q in a loop via the other states. Finally we note that we can loop round an arbitary number of times and hence get the regular expression 1 ∗( 01 ∗ 01 ∗ 01 ∗ 01 ∗ 01 ∗)∗.

Exercise set 3

For any alphabet Σ , which are the subsets S of Σ∗^ such that the set S∗^ is finite? Solution: {}, {} Reasoning: In an exam you should note the definition of the Kleene Star. From the definition, if S contains a non empty word w then S∗^ contains arbitarily long words w, w^2 , w^3 , w^4 ,.. .. Hence S cannot contain a non empty word. This leaves only two possible sets: {} and {}. We must then show that in both cases the set S∗^ is finite, you can do this by finding the set S∗^ using the definition of the Kleene star.

ε

ε

ε

ε

1

Finally we put the machine accepting 0 and the machine accepting 1 ∗^ together using the method of contructing a machine to accept E · E′ (where E and E′ are expressions) given on page 16 of lecture notes 6:

0 ε ε

ε

ε

ε

1

Exercise 1.

The machine is found by a similar method to above. You may need to use other methods from slides 12-18 from lecture notes 6. The machine is:

Exercise 1.

The machine is:

0 0

0

1

ε ε ε ε ε

ε

ε

ε

ε

ε

Exercise set 2

Consider the following  -NFA.

 a b c → p ∅ {p} {q} {r} q {p} {q} {r} ∅ ∗r {q} {r} ∅ {p}

  1. Compute the  -closure of each state.
  2. Give all the strings of length three or less accepted by this automaton.
  3. Convert the automaton to a DFA.

It might help you to draw the -NFA. To calculate the -closure of a state we consider what states we can get to from it following any number of  transitions (including no  transitions):

  • cl(p) = {p}
  • cl(q) = {p, q}
  • cl(r) = {p, q, r}

The easiest way to ensure that you do not miss any strings of length three is to list all of them and then test each one. Whichever way you tackle producing this list you should keep in mind strings such as cba, this is accepted since input c will take us to a set containing state r, from state r we make an  transition to a set containing state q, then input b takes us back to a set containing state r and finally on input a we remain in a set containing state r the accepting state. You should also recall that we may allow a sequence of any number of  transitions so strings such as bbc are also accepted. To convert the -NFA to a DFA we use the modified powerset construction. We ignore unreachable states. This process is very similar to the powerset construction however we need

Worked Solutions 6: Context-free grammars

Emma Jones

May 3, 2005

These are selected worked solutions to the exercises from lecture handout 7. Each section corresponds to a slide from that handout.

Exercise set 1

Consider the grammar

S → A 1 B A → 0 A |  B → 0 B | 1 B | .

Give parse trees for the strings

  1. 00101
  2. 1001
  3. 00011

In an exam I would briefly explain what a parse tree is e.g. slide 18. Then I would note that A can produce only 0 ’s. Since we must start with the start symbol S the first production used will always be S → A 1 B. For each of the strings we need to decide which 1 in the string is the 1 in A 1 B. Since A can only produce 0 ’s we can see that it must always be the left most 1. Once we know this it should be a simple matter to produce the trees. They are:

1. S

A

0 A

0 A

1 B

0 B

1 B

2. S

A

1 B

0 B

0 B

1 B

3. S

A

0 A

0 A

0 A

1 B

1 B

Exercise set 2

For the CFG G defined by the productions

S → aS | Sb | a | b,

prove by induction on the size of the parse tree that the no string in language L(G) has ba as a substring. We argue by induction on the size of the parse tree. The base cases should be all of the smallest possible parse trees. Proof:

  • Base cases: The two smallest parse trees are:

S

a

S

b

Neither the string a nor the string b has the string ba as a substring.

  • Inductive step: There are two cases: 1. The parse tree is of the form

S

a S where^ S^ is a parse tree for

w ∈ Σ∗. Since the parse tree for w is smaller than

S

a S the induction hypothesis

implies that w does not contain ba hence it is not possible for aw to contain the substring ba.

  1. The argument is similar but we are considering parse trees of the form

S

S b

If you complete the last part of the inductive step you should see that we have completed the proof since no other forms of parse trees are possible in this grammar.