cos2601workshop fin 2013

32
Discussion Class: COS2601

Upload: maddy79

Post on 20-Apr-2017

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: COS2601Workshop FIN 2013

Discussion Class: COS2601

Page 2: COS2601Workshop FIN 2013

Question 1

(a)Let S = {a, bb, bab, abaab}. For each of the following strings, state whether or not it is a word in S*. Justify your answer.

(i)abbabaabab

(ii) abaabbabbbaabb

Page 3: COS2601Workshop FIN 2013

Solution:

(i) No, no concatenations of a, bb, bab and abaab yield abbabaabab.

Try to generate the word: abbabaabab

but ab not possible.

(ii) Yes, concatenations of a, bb, bab and abaab yield abaabbabbbaabb.

Page 4: COS2601Workshop FIN 2013

Question 1(b) Give an example of a set S such that S* only contains all possible

strings of combinations of a’s and b’s that have length divisible by three.

(c) Give an example of two sets S and T of strings such thatS* = T* but S T and T S.

Solution:

(b) S = {aaa, aab, aba, abb, baa, bab, bba, bbb}.

All words in S*: Multiples of three; All possible combinations of a’s & b’s.

(c) Two possible answers:

S = {, a} and T = { a, aa } or

S = {a, b, ab} and T = {a, b, ba}.

Page 5: COS2601Workshop FIN 2013

Solution:

Question 2Consider the alphabet {a, b} and the language ODDAB where ODDAB consists of all words of ODD length which contain the substring ab. Provide(i) an appropriate universal set,(ii) the generator(s),(iii) a function on the universal set, and then(iv) use these concepts to write down a recursive

definition of the language ODDAB.

(i) {a, b}* suitable - contains all the words in ODDAB.

(ii) aba, aab, abb, bab (odd, contains ab) (iii) CONCAT

Page 6: COS2601Workshop FIN 2013

2(iv) Recursive def. ODDAB:

ODDAB smallest subset of {a,b}* such that

aba, aab, abb, bab ODDAB and if w ODDAB, then also

CONCAT(w, aa), CONCAT(aa, w), CONCAT(w, ab),

CONCAT(ab, w), CONCAT(w, bb), CONCAT(bb, w),

CONCAT(ba, w), CONCAT(w, ba)  ODDAB.

Alternative recursive def.:Rule 1: aba, aab, abb, bab ODDAB.

Rule 2: If w ODDAB, then also CONCAT(w,aa), CONCAT(aa,w), CONCAT(w,ab), CONCAT(ab,w), CONCAT(ba,w), CONCAT(w,ba), CONCAT(bb,w), CONCAT(bb,w)  ODDAB.

Rule 3: Only words produced by rules 1 and 2 are in ODDAB.

Page 7: COS2601Workshop FIN 2013

Another example:A recursive definition for language EVENnotAA over {a, b} must be compiled where EVENnotAA consists of all words of even length and not containing the substring aa.Provide(i) an appropriate universal set,(ii)the generator(s),(iii) a function on the universal set, and then(iv) use these concepts to write down a recursive definition of the language EVENnotAA.

Solution:

(i) {a, b}* suitable-contains all words in EVENnotAA.

(ii) Λ, ab, ba and bb

(iii) CONCAT

Page 8: COS2601Workshop FIN 2013

(iv) Recursive def. for EVENnotAA :

EVENnotAA is the smallest subset of {a,b}* such that Λ, ab, ba and bb EVENnotAA and

if w EVENnotAA, then also CONCAT(w, ba), CONCAT(ab, w),CONCAT(w,bb), CONCAT(bb,w) EVENnotAA, and

if w EVENnotAA and w does not end on an a, then CONCAT(w, ab) EVENnotAA, and

if w EVENnotAA and w does not begin with an a, then CONCAT(ba, w) EVENnotAA.

Page 9: COS2601Workshop FIN 2013

Question 3(i) Give a recursive definition of the set P of all positive integers greater

than or equal to 5,

(ii) formulate the appropriate induction principle, and

(iii) then apply the induction principle to prove that

2n – 3 2n-2 for all n 5.Solution:

(i) P is the smallest subset of Z such that5P and if nP, then also n+1P.

(ii) If a subset A of P is such that 5A and if nA, then also n+1A, then A = P.

Page 10: COS2601Workshop FIN 2013

Question 3(iii) Apply induction principle: Prove 2n–3 2n-2 for all integers n5Solution:

Define the set A: A P: A = {nP | 2n - 3 2n - 2}

Is n = 5A?2(5) - 3 25 - 2

i.e. 7 8Thus, 5A is true.

Assume kA, hence we assume 2k – 3 2k – 2

Required to prove k+1A, i.e. 2(k+1) –3 2(k+1) – 2

End goal to prove A = P.

Page 11: COS2601Workshop FIN 2013

Question 3(iii) cont. Apply the induction principle: Prove 2n – 3 2n-2 for all integers n 5.

Solution cont.:Assume kA, hence we assume 2k – 3 2k – 2 (1)

Required to prove k+1A, i.e. 2(k+1) – 3 2(k+1) – 2

LHS:

2(k+1)–3 = 2k + 2 – 3 = (2k – 3) + 2 2k – 2 + 2 from (1)

2·(2k – 2) smallest value for 2k – 2, k=5 is 25–2 = 8 and 28 = 16 > 10 = 25–2 + 2 = 21 + k – 2

= 2 (k +1) – 2

Thus k+1A

Hence, A = P thus 2n - 3 2n - 2 for all n 5.

Page 12: COS2601Workshop FIN 2013

Question 4(a) Does the regular expression

[b*+ (abb*)* + (aabb*)*]* bbb [b* + (abb*)* + (aabb*)*]*define the language of all words in which the substring bbb appears at least once, but aaa does not appear at all? If not, give a counterexample.

Solution:

No, the regular expression does not define the language.

All words must contain bbb.

No word contains aaa.

However, not all words can be generated – somecounterexamples are:

babbbaabbbbbba.

Page 13: COS2601Workshop FIN 2013

Question 4(b)Give a regular expression generating the language consisting of all words containing exactly one occurrence of the substring aa and no occurrence of the substring bb.

Solution:

Regular expression:Must generate words that start (and end) with either an a or a b. All occurrences of the letter b (except if it is the last letter of the word) must be followed by an a.

Furthermore, only one a may be followed by another a - in order to ensure only one occurrence of the substring aa.

A possible regular expression is:(b + )(ab)*aa(ba)*(b + )

Page 14: COS2601Workshop FIN 2013

Question 5Build an FA (finite automaton) that accepts the language of all words that satisfies both of the following conditions:

NO word contains the substring bba, and ALL words end with a double letter, thus all words end with

either aa or bb.Note: Only one FA must be build.

Solution: We begin to draw the FA as follows:

- 1 2

3

+ 5 6

a

a, bb

a

b

b

+ 4a

a

Page 15: COS2601Workshop FIN 2013

Question 5NO word contains the substring bba, and ALL words end with a double letter, either aa or bb.

Solution:

- 1 2

3

+ 5 6

a

a, bb

a

b

b

+ 4a

a

ab

b

Page 16: COS2601Workshop FIN 2013

Question 6By using Kleene’s theorem, find a regular expression that generates the language accepted by the following TG (transition graph):

-ba

b

+

+

a

b

a

aa

bb

a

Page 17: COS2601Workshop FIN 2013

Question 6 cont.By using Kleene’s theorem, find a regular expression.

Solution:Step 1 - Create a unique start state and a unique final state:

1

4

3

2

a

a

a

b

b

b b

b a

a a

- + Λ

Λ

Λ

Page 18: COS2601Workshop FIN 2013

Question 6 cont. Solution:Step 2 - Eliminate state 3:

1

4

2

a

a

a

b

bb

- + Λ

Λ

Λ

bab*aa

1

4

3

2

a

a

a

b

b

b b

b a

a a

- + Λ

Λ

Λ

Page 19: COS2601Workshop FIN 2013

Question 6 cont. Solution:Step 3 - Eliminate state 4:

1

4

2

a

a

a

b

bb

- + Λ

Λ

Λ

bab*aa

1

2

a

a

- + Λ

Λ

( b + b a b * a a ) ( b b ) * a

( b + b a b * a a ) ( b b ) *

Page 20: COS2601Workshop FIN 2013

Question 6 cont. Solution:Step 4 - Eliminate state 2:

1

2

a

a

- + Λ

Λ

( b + b a b * a a ) ( b b ) * a

( b + b a b * a a ) ( b b ) *

1 - + Λ

[ a + ( b + b a b * a a ) ( b b ) * a ] a *

( b + b a b * a a ) ( b b ) *

Page 21: COS2601Workshop FIN 2013

Regular expression:[a + (b + bab*aa)(bb)*a]a* + (b + bab*aa)(bb)*

1 - + Λ

[ a + ( b + b a b * a a ) ( b b ) * a ] a * + ( b + b a b * a a ) ( b b ) *

Question 6 cont. Solution:Step 5 - Eliminate state 1:

1 - + Λ

[ a + ( b + b a b * a a ) ( b b ) * a ] a *

( b + b a b * a a ) ( b b ) *

Page 22: COS2601Workshop FIN 2013

Question 7 Consider the following FA with a corresponding regular expression r1:

Build an FA for the regular expression r1* by applying Kleene’s theorem.

-x1 +x3

x2

a, b

b

ba

a

Page 23: COS2601Workshop FIN 2013

Question 7 cont FA for a regular expression r1*Solution: First compile a transition table

-x1 +x3

x2

a, b

b

ba

a

New state

Read a

Read b

±z1 = x1 z3 z2

z2 = x1 z3 z2z3 = x2 z3 z4

+z4 = x1 or x3

z5 z4

+z5 = x2 or x3 or x1

z5 z4

Page 24: COS2601Workshop FIN 2013

Question 7 cont. Solution: FA for a regular expression r1*

New state Read an a Read a b±z1 = x1 z3 z2

z2 = x1 z3 z2

z3 = x2 z3 z4

+z4 = x1 or x3 z5 z4

+z5 = x2 or x3 or x1 z5 z4

± z1 z2

+z4

z3

+z5

a

bb

a

b

a

a

a b

b

Page 25: COS2601Workshop FIN 2013

Question 8(a)Convert the following Mealy machine to a Moore machine:

q 0

q 2

q 1

b / 1

b / 1 b / 0

a / 0

a / 0

a / 1

q01/0

q2/0

q1/1

q02/1

b

b

b

b

a a

a

a

Solution:

Page 26: COS2601Workshop FIN 2013

Solution:

Question 8(b)Consider the following FA:

Find an NFA (non-deterministic FA) with four states that accepts the same language.

-

+

a a

a

a

a

b

b b

b

b

- + a a a ,b

b

a ,b

Page 27: COS2601Workshop FIN 2013

Assume L = {anbnam, where n, m{0,1,2,3,...}} is regular.

Then there exists an FA with, say k states, that accepts L.Let w = akbkap be a word in L.

According to the pumping lemma, w may be written asw = xyz such that length(x) + length(y) ≤ k AND length(y) > 0

i.e. both x and y can only be a’s.

Pumping lemma: xyyz must also be an element of L. Not the case: xyyz has more a’s (in first group of a’s) than b’s.

This a contradiction. Our assumption that L is regular is incorrect and we conclude that L is not regular.

Question 9 Use the Pumping Lemma with length to prove that the following language is nonregular:

L = {anbnam, for some n = 0,1,2,3,… and m = 0,1,2,3,…}.Solution:

Page 28: COS2601Workshop FIN 2013

Extra example: Use the Pumping Lemma with length to prove that the following language is nonregular:

L = { abn + 1 c5an n = 1, 2, 3,... }.

Assume L = {abn + 1 c5an n = 1, 2, 3,... } is regular.

Then there exists an FA with, say k states, that accepts L.Let w = abk + 1 c5ak be a word in L.

According to the pumping lemma, w may be written asw = xyz such that length(x) + length(y) ≤ k AND length(y) > 0

Pumping lemma: xyyz must also be an element of L. We see this is not the case.

Solution:

Page 29: COS2601Workshop FIN 2013

w = xyz = abk + 1 c5ak. Three possible cases:1. y consists of only the first a2. y consists of the first a and some of the b’s3. y consists of only b’s.

In all cases the pumped string xyyz is not in L:Case 1: xyyz contains more than one a in beginning of the string but only k a’s in the second group of a’s;Case 2: xyyz contains more than one ab sub-string;Case 3: in xyyz contains more than k+1 b’s but only k a’s in the second group of a’s.Pumped words do not belong to L.This a contradiction. Our assumption that L is regular is incorrect and we conclude thatL is not regular.

Page 30: COS2601Workshop FIN 2013

(a) An effective solution to a problem with a ‘Yes’ or ‘No’ as answer is called a decision procedure.

(b) A problem for which a decision procedure can or may be found is called decidable.

(c) If some FA has N states and there are m letters in its alphabet, then there exist

mN + mN + 1 + mN + 2 + … + m2N – 1

different input strings in the range N ≤ length of string < 2N.

All these words can be tested by executing the FA on them.

If any of these words are accepted, then the language is infinite. If none of these words is accepted, then the language is finite.

Question 10 in TL 103. Question 11 (a) Define a decision procedure.(b) Use the definition in (a) to define decidability.(c) Describe an effective procedure to decide whether a given FA accepts a finite or infinite language. (Th. 20 p. 216, Cohen)(d) Using the decision procedure in (c) above, determine whether each

FA1 and FA2 accepts a finite or an infinite language (see next pp.) Solution:

Page 31: COS2601Workshop FIN 2013

- +a

b

a

a b

b

a

a, bb

Question 11 cont.(d) Using the decision procedure described in (c), determine whether the following FA1 accepts a finite or infinite language.

Solution:

N = 5; 2N = 10.

Only words accepted by FA1: b and ab. (Lengths 1 and 2.) Words of length 5 ≤ length of string < 10not accepted. Thus language is finite.

Page 32: COS2601Workshop FIN 2013

Question 11 cont.(d) Using the decision procedure described in (c), determine whether the following FA2 accepts a finite or infinite language.

-

+

b

a, ba

b

a

Solution:

N = 3; 2N = 6.

Consider bbaa:Length(bbaa) = 4 > N = 3

bbaa is accepted by FA2 where 3 length(bbaa) = 4 < 6. Thus language accepted is infinite.