25
Languages: Finite State Machines Chapter 6 problems strings (languages) machines answers

1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

Embed Size (px)

DESCRIPTION

3 6.1 Language: The Set Theory of Strings

Citation preview

Page 1: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

1

Languages: Finite State Machines

Chapter 6

problems strings (languages) machines answers

Page 2: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

2

6.1 Language: The Set Theory of Strings

: a nonempty finite set of symbols, collectively called an alphabet.

{0,1,2,11} or {a,b bb} is not considered as an alphabet.That is, juxtaposition of symbols is not included in an alphabet.

Def. 6.1 If is an alphabet and Z we define the of recursively as follows:(1) and(2) where denotes the justaposition of and .In general, for all Z |Def. 6.2 Define where denotes the .

Note (1) { } since and (2) { }

+

1

+ ,

+

0

n powers

xy x y xyx y

nempty string

n n

n n

,

,{ | ,

, | | |{ },

}1

Page 3: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

3

6.1 Language: The Set Theory of Strings

Def. 6.3 If is any alphabet, then

(a) and (b)

Ex. 6.2 For = {0,1}, the set consists of all strings of 0' sand 1' s together with the empty string.

Def. 6.4 string equivalence if and

Def. 6.5 length of a string: , then Furthermore, = 0.

+ *

*

n

n

n

n

m ni i

n

w x x x w y y y w w m nx y i m

w x x x w n

1 0

1 1 2 2 1 2 1 2

1 2

1

, ., .

.

Page 4: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

4

6.1 Language: The Set Theory of Strings

Def. 6.6 operation

We have and for any If then But, Def. 6.7 The power of a string

1

concatenationx x x x y y y y xy x x x y y y

x x x xy x y x ya b c

x x x x xx x xx

m n m n

n

1 2 1 2 1 2 1 2

2 12

23

23

12

22

13

13

22

12

23

23

12

22

13

13

22

0 1 2 1

0 1 25

, ,, , .

{ , , }, { , , }, ..

, , , ,

*

n

n

n

x x nnx n x

,

.

Ex. 6.3 If = {0,1} and = 01, then a string of 0' s and

1' s where the first symbol is 0 and the symbols alternate. Here

L={xn|x=01,n in N}y recognizer of

Lyes, y in L

no

Page 5: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

5

6.1 Language: The Set Theory of Strings

Def. 6.8 If , and = , then is called a prefix of , andif , then is said to be a proper prefix. Similarly, is called asuffix of ; it is a proper suffix when .

= has + 1 prefixes (suffixes), of which are proper.

Def. 6.9 If , , and = , then is called a substring of .When , is a proper substring.How many substrings are there for =

*

*

x y w xy x wx x y

w xx x x x n n

x y z w xyz y wy w y

x x x xx n x n x x x x n

n

nn n n n

1 2

1 21 2 1 11 1 2 3

?, ( ), ( ), , , , : ( )

,

1

Def. 6.10 For a given alphabet , any subset of is called a language over . This includes the empty language.

*

Page 6: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

6

6.1 Language: The Set Theory of Strings

Def. 6.11 For an alphabet and languages , theconcatenation of and , denoted , is { | , }.

Ex. 6.10 = { , , }, = { , , }, = { , }.Then = { , , , , }, = { , , , , , }So, (1) | |= =| | and (2) | |= =| | | |In general, for finite languages and , | | | | | | .

Also for , the decomposition into = where and is not unique. For example,

* A BA B AB ab a A b B

S x y z A x xy z B yAB x xy z xyy zy BA x xy z yx yxy yz

AB BA AB A BA B AB A B

x AB x ab a Ab B

,

5 6 5 6

AB x

= {0,1,00}, = {11,10,011}. Then = 0011 = (00)(11) = (0)(011).

Page 7: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

7

6.1 Language: The Set Theory of Strings

Theorem 6.1 For an alphabet , let , , Then(a) { } = { } = (b) ( ) = ( ) (c) ( ) =(d) ( ) = (e) ( )(f) ( ) .Proof of (f): ( ) = , ,

, , , .And with = { , , }, B = { , , }, = { , }, and = { , }.Then

*

A B CA A A AB C A BC A B C AB ACB C A BA CA A B C AB ACB C A BA CA

x B C A x yz y B C z Ay B y C z A yz BA yz CA yz BA CA

x y z x xx y C y xy A y yyxyy BA

.

CA xyy B C A

A A A A ab a A b AA A AA A A

n n

nn Z

nn N

, but ( ) .

Def. 6.12 (a) b the positive closure of

(c) the Kleene closure of A

+

+

0 1 1{ }, , { | , }( ) ,

{ },

Page 8: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

8

6.1 Language: The Set Theory of Strings

Ex. 6.12 Let = { , }.(a) If = { , , , } = then is the language of allstrings in where the length of the string is even.(b) If = { , }, then consists of all the strings in ofodd length. Also Bd) { }

Ex. 6.13 = { , , = { then but .

2 *

*

* *

*

*

x yA xx xy yx yy A

B x y BAA A B A BA

x y x y

A x x x B x nA B B A B

n

,

, .( { } { , }

, , }, | },,

* * * *

* *

3 4

2 20

Page 9: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

9

6.1 Language: The Set Theory of Strings

Lemma 6.1 Let be an alphabet, with languages ,If , then A for all ZProof: Let ( ):

is true. Assume ( ) is true. For where and by definition). Therefore,

and

*

n +

+

k+1

A BA B B n

S n A B A BS S k x A x x x

x A x Ax B x B x B

n

n n

k

k

k

..

.( ) ,

(, , .

1 11 2

1 2

1 2

Page 10: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

10

6.1 Language: The Set Theory of Strings

Theorem 6.2 For an alphabet and languages ,(a) (b) (c) d) (e) f) g) ( )

Proof of (g): (1) Conversely, ,

*

* * +

* *

*

*

*

*

A BA AB A B A A B A BA B A B AA A A AA A A A A A

A B A B A BA A B B A B A B

A B A B A B A B A BA B

,

(( ( ) ( ) ( )( ( ) ( )

, ( ) ( )( ) ( ) . ,

( )

* *

* * * * * *

* * * * * *

* * *

* * * * *

* A B A B A BA B A B A B A B A B

A B A B xy A Bx A y B xy A B xy A B

A B A B B A B

* * * * * *

* * * * * * * *

* * * * * * *

* * * * *

* * * * * * * * *

( ) ( )( ) ,

( ) ( ) ., . ( ) .

( ) ( ) ( ) .

(2) Conversely, if where

Then So A

*

*

* *

* *

Page 11: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

11

6.1 Language: The Set Theory of Strings

Ex. 6.14 = {0,1}, define = { | contains only 1 occurrence of the symbol 0}. Then can be recurively defined asfollows: (1) 0 (2) for any , , .

Ex. 6.15 = {(, )} and consisting of those nonemptystrings of parentheses that are grammatically correct for algebraicexpressions. can de defined as (1) () (2) for any , ,

and ( ) .

Ex. 6.16 = {0,1} and where if the number of 0' sin is equal to the number of 1' s. Then can be defined as(1) (2) if , then , , , , ,

*

*

*

A x x xA

A x A x x A

A

A A x y Axy A x A

A x Ax A

A x A x x x x x

,

1 1

01 01 0 1 10 10 .1 0x A

Which languages in the above examples are harder to recognize?

recursively defined sets

Page 12: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

12

6.1 Language: The Set Theory of Strings

Ex. 6.17 Given an alphabet , consider the string x = xin Define the reversal of x, x x can berecursively defined as (1) (2) for any n N, if xthen we can write x = zy where z , y Then xUsing this definition, we can prove that (xProof: Induction on the length of x If x then x and (x Assume theresult is true for any y, x

1* R R

R n+1

n R

11 1 1

1

2

x xx x x

y zx x x

x x x x x x x

n

n n

R

R R R

R R R R R R R R

2

1 1

2 2 1

2 2 2 2 2 2 1

0

. .

,. .

) .. ,

) ( ) .

* . ,, . ) ( )

( ) ( ) .

where y Now for xwhere x with z (

1

1

k xzy y k x x zy x

y x z x y z x zy x x

R R

R R R R R R R

2

1 1 1 2 1 2

1 2 2 1 2 1 2 1

1

Which one is harder, xx or xxR?If x=xR, x is called a palindrome. (noon, madam, did,...)

6.16

Page 13: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

13

6.2 Finite State Machines: A First Encounter

A vending machine: 20cc for cola (C), root beer(RB)and accepting 5c, 10c, and 25c, and returning the necessary changes

State s s s s sInput 5c 5c 10c WOutput N N N RB

0 1 2 3 0

t t t t t0 1 2 3 4Purchase root beer (white button)

State s s sInput 25c BOutput 5c C

0 3 0

t t t0 1 2Purchase cola(black button)

Page 14: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

14

6.2 Finite State Machines: A First Encounter

The major features of such a machine:(1) in only one of finitely many states at a given time (state set S)(2) accept as input only a finite number of symbols (input alphabet set I)(3) state transition function depending on input and current state ( is the next state function)(4) finite set of output alphabet set O (optional) ( is the output function)(5) deterministic (vs. nondeterministic)

v S I S:

w S I O:

Page 15: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

15

6.2 Finite State Machines: A First Encounter

Def. 6.13 A finite state machine is a five - tuple = ( , , , , ).M S I O v w(or use final states set F instead of O)

1

v w

s s ss s ss s s

0 1 0 1

0 00 00

0 0 11 2 12 0 1

Ex. 6.17

state transition table

s0 s1 s2

start

0,0

1,0

1,0

0,0

1,1

0,0state transition diagram

Page 16: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

16

6.2 Finite State Machines: A First Encounter

language A={x|x in {0,1}* and has at least 1 0's}

s0

s1

1,0

start

0,10,11,1

x

x is in A if the output contains 1's.

s0

s1

1

001

x

start

x is in A if the machine stays ats1 when the input is consumed.

final state

Page 17: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

17

6.2 Finite State Machines: A First Encounter

Ex. 6.19 Design a 1-bit binary adder.

s0 s1

start

00,001,110,1

01,010,011,111,0

00,1

state to remember a carry

Page 18: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

18

6.3 Finite State Machines: A Second Encounter

sequence recognizer

Ex. 6.20 Construct a machine that recognizes each occurrenceof the sequence 111 as it is encountered in any input string of {0,1}*.For example, input 1110101111, the output should be 0010000011.

s0 s1s2

start

0,0

1,01,0

1,1

0,00,0

s3

0,0

1,1

1,1a minimal machine (3 states)

Page 19: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

19

6.3 Finite State Machines: A Second Encounter

Ex. 6.21 recognize the occurrences of 111 that endin a position that is a multiple of three. (input 1110111,output 0010000)

s0 s1 s2

s3s4

start1,0 1,0

1,10,0

0,00,0

0,01,0

0,01,0

used to consumeextra symbolsbefore counting

Page 20: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

20

6.3 Finite State Machines: A Second Encounter

Ex. 6.23 Languages that cannot be recognized by a finite state machine. A={0i1i|i is a positive integer}

Assume there is a FSM that recognizes A with n states.

s0 s1 s2 ... si ... sj ... sn-1start

transition for x= 0n1n by the pigeonhole principle,this cycle must exist

If x is recognized, then 0n-(j-i+1)1n would also be recognized.Therefore, such FSM cannot exist.

Page 21: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

21

6.3 Finite State Machines: A Second Encounter

k-unit delay machinesinput = output (1

'x x x x w s x x x xm

k sm2 0

01 200 0 , , ) .

Ex. 6.25 1-unit delay machine

s0

s1

s2

start0,0

1,0

0,1 1,0

0,0

1,1

remembering 0

remembering 1

Page 22: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

22

6.3 Finite State Machines: A Second Encounter

Ex. 6.25 2-unit delay machine

s3 s4 s5 s6

s1 s2

s0

start

0,0 1,0

0,0 1,0 0,0 1,01,1

1,00,1

0,10,0

1,11,0

0,0

Page 23: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

23

6.3 Finite State Machines: A Second Encounter

Def. 6.14 (a) reachable v(si,x)=sj (b) transient statev(s,x)=s implies x= , s2 is the only transient state.

s0

s2s1

s3

s4 s5

s7 s6

0,0

0,11,0

1,1

0,1

1,00,0

1,1

0,1

1,1

1,10,0

1,0

1,1

0,00,1

(c) sink state v(s,x)=s for all x s3 is the only sink(d) submachine(e) strongly connected for any si,sj, si is reachable from sj

a stronglyconnectedsubmachine

Page 24: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

24

6.3 Finite State Machines: A Second Encounter

Def. 6.15 Let be two distinct states. shortestinput string is called a (or ) sequencefrom to if(a) ( (b) ( (may not be unique)

+s s A

x I transfer transitions s

v s x s v s y s y x

i j

i ji j i j

,

, ) , ) .

Ex. 6.26 v w

0 1 0 1s0 s6 s1 0 1s1 s5 s0 0 1s2 s1 s2 0 1s3 s4 s0 0 1s4 s2 s1 0 1s5 s3 s5 0 1s6 s3 s6 0 1

Find a transfer sequence from s0 to s2.

s0 s1s6

s6

s5

s5

s3

s3

s0

s0

s1 s2

s4

0

0

0

0

1

1

1

1

0 1

1 0

Page 25: 1 Languages: Finite State Machines Chapter 6 problemsstrings (languages) machines answers

25

Chapter 6 Languages: Finite State Machines

Exercise: P318:8,21 P324: 3,5,8 P332: 5 P334: 6,8