S - Cs Mwsu - Department of Computer Science

Download Report

Transcript S - Cs Mwsu - Department of Computer Science

CMPS 3223 Theory of Computation

Automata, Computability, & Complexity

by Elaine Rich ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Slides provided by author Slides edited for use by MSU Department of Computer Science – R. Halverson 1

Finite State Machines & Regular Languages

Chapter 5B Pages 72 - ??

2

Sections to Omit from Study

• P. 73 – A simulation algorithm • 5.4 – Will use method discussed in class, not Theorem 5.3

• 5.5, 5.6 – Omit • 5.7 – Omit • 5.7.1 – Learn definitions only • 5.8 - 5.12 - Omit 3

Epsilon Transitions (

)

• • • An Epsilon Transition is a transition that allows movement from one state to another without consuming a character of input string

Epsilon Closure : eps

(

q

) = set of states accessible from state q by epsilons

eps

(

q

) = {

p

K

: (

q

,

w

) |-*

M

(

p

,

w

)} 4

Epsilon Closure Example

eps(S) = {q3} eps(q1) = {q2} eps(q2) = { } eps(q3) = { } S a  b q1 q3 q2  b 5

Epsilon Transitions (

)

• Simple epsilon closure has limited use • Need an expanded closure…

eps

(

q,a

) = {

p

K

: (

q

, a

w

) |-*

M

(

p

,

w

)} = set of all states accessible from state q by consuming only character a 6

Expanded Epsilon Closure

S a  b q1 q3 q2  b eps(S,a) = {q1,q2,q3} eps(q2,a) = { } eps(q1,a) = { } eps(q3,a) = { } eps(S,b) = {q2} eps(q2,b) = { } eps(q1,b) = { } eps(q3,b) = {q2} 7

eps

(

q

0 ) =

eps

(

q

1 ) =

eps(q

2 ) =

eps

(

q

3 ) =

An Example of eps

8

Algorithm Simulating a NDFSM But why do this?? Convert NDFSM to DFSM

ndfsmsimulate

(

M

: NDFSM,

w

: string) =

1. current-state

=

eps

(

s

).

2.

While any input symbols in

1. c

= get-next-symbol(

w

).

2. next-state

=  .

w

remain to be read do: 3. For each state

q

For each state in

current-state p

do: such that (

q, c, p

)  

next-state

=

next-state

eps

(

p

).

do:

4. current-state

=

next-state

.

3. If

current-state

contains any states in

A

, accept. Else reject.

9

5.4.4 Nondeterministic and Deterministic FSMs

Clearly: {Languages accepted by a DFSM}  {Languages accepted by a NDFSM} More interestingly:

Theorem:

For each NDFSM, there is an equivalent DFSM .

What is meant by “proof by construction”?

10

Nondeterministic to Deterministic FSM

Theorem:

For each NDFSM, there is an

Proof:

equivalent DFSM.

By construction : Given a NDFSM

M

= (

K

,  ,  ,

s

,

A

), we construct

M

' = (

K'

,  , 

'

,

s'

,

A'

), where

K'

= P (

K

)

s' A'

=

eps

(

s

) = {

Q

K

'

(

Q

,

a

) = :

Q

A

  }  {

eps

(

p

):

p

K

and (

q

,

a

,

p

)   for some

q

Q

} 11

An Algorithm for Constructing DFSM from NDFSM

1. Create a table with 1. A row for each state [s] 2. A column for each alphabet character [c] 2. Fill in body of table with eps(s,c) 3. If table contains any new combined states, create a new row for each & repeat step 2, until no new states are created.

4. Draw DFSM from table starting with state S and using only accessible states 1. Final states in NDFSM will be final in DFSM 12

Expanded Epsilon Closure

S a  b q1 q3  b S q1 q2 q3

a b

13

Expanded Epsilon Closure

S a  b q1 q3  b S 1 2 3 1,2

a

1,2 ~ ~ ~ ~

b

2 ~ ~ 2 ~ 14

Expanded Epsilon Closure

Are the machines equivalent? Is the new DFSM Efficient?

Minimal?

q1  S 1 2 3 1,2

a

1,2 ~ ~ ~ ~

b

2 ~ ~ 2 ~ a b S a  s b q3 b 15

An Example

Compare our table method to that of the author’s on page 76+ 16

Another Example

L

= {

w

 { a , b }* : the fourth character from the end is a } 17

What Happens if the Original FSM is Already Deterministic

?

18

The Real Meaning of “Determinism”

Let

M

= Is

M

deterministic?

An FSM is

deterministic

, in the most general definition of determinism, if, for each input and state, there is at most one possible transition.

• • • DFSMs are always deterministic. Why?

NDFSMs can be deterministic (even with  -transitions and implicit dead states), but the formalism allows nondeterminism, in general.

Determinism implies

uniquely defined

machine behavior.

19

Deterministic FSMs as Algorithms

L

= {

w

 { a , b,c }* :

w

contains at most one b } 20

Deterministic FSMs as Algorithms

until accept or reject do:

S

:

s

= get-next-symbol if

s

= end-of-file then accept else if

s

= a then go to

S T

: else if

s

= b then go to

T s

= get-next-symbol if

s

= end-of-file then accept else if

s

= a then go to

T

else if

s

= b then reject end But you have to write a new program for each different machine 21

Time Complexity

• Length of Program: |

K

|  (|  | + 2) • Time required to analyze string w:

O

(|

w

|  |  |) • Explain!!

22

Simulator for DFSM

Section 5.6

• Could we develop one?

• What would it entail?

23

Simulator for DFSM

What would it entail?

2 Parts • Read in the graph – How to represent the DFSM (graph)?

– Build the table • Read in the string – How to systematically process a string?

24

Minimal DFSM

Section 5.7.1

• Minimal DFSM : a DFSM is minimal iff there is no other DFSM which accepts the same language and has fewer states – Minimal refers to number of states – Relates to equivalence classes of strings & substrings 25

Collapsing States

The front part of the machine a b c d 1 2 3 4 Do you agree that we could combine states 1 & 4 and also states 2 & 3?

26

Collapsing States

The front part of the machine a d c b 14 23 You can ALSO collapse states at the initial end of the arrows.

BUT, we need a clear strategy for determining which states can be collapsed.

27

Another Collapse Example

12 a s 2 b S a  b q1 q3  b s a,b 1 28

Minimal DFSM

Section 5.7.1

• Theorem 5.5

: There exists a unique minimal DFSM for every regular language – There is no smaller machine (# of states) – If there is another machine with same number of states, the machines are equivalent except for names of states • There is an algorithm for developing the minimal DFSM. – First convert to deterministic, remove  29

Algorithm Overview for Minimizing

• Group all states into 2 sets –Accepting & Rejecting –Process each set and split as necessary –Continue until no sets can be split • A set of one state is finished 30

Building of Regular Languages

Fact: For every regular language, there exists a DFSM.

Questions: If L & K are regular languages • Is L U K a regular language?

• Is LK a regular language?

• Is L* a regular language?

• Is L+ a regular language?

Can you prove it???

31

Is L U K a regular language?

• Since L & M are a regular languages, there exist DFSMs M(L) & M(K) that recognize L & K.

• Each has a distinct Start State, SL & SK.

• Create a new state S &  -transition from S to SL & SK.

• The resulting machine is a NDFSM for L U K.

• THUS, L U K is a regular language.

32

Is LK a regular language?

• Since L & M are a regular languages, there exist DFSMs M(L) & M(K) that recognize L & K.

• Each has a distinct Start State, SL & SK and each has at least one final state F. • Create  -transition from all final states in L to SK.

• Convert all final states in L to non-final states • The resulting machine is a NDFSM for LK.

• THUS, LK is a regular language.

• What if epsilon is in L or in K?

33

Is L* a regular language?

• Since L is a regular language, there exists DFSM M(L) that recognizes L.

• M(L) has a distinct Start State, and at least one final state F. • If S is not a final state, make it a final state.

• Create  -transition from each final state (except S) to S. (or create a new final state as before) • The resulting machine is a NDFSM for L*.

• THUS, L* is a regular language.

34

Is L+ a regular language?

• Since L is a regular language, there exists DFSM M(L) that recognizes L.

• M(L) has a distinct Start State, and at least one final state F. • Create  -transition from each final state to S.

• The resulting machine is a NDFSM for L + .

• THUS, L + is a regular language.

• Why not make S a final state?

35

Homework for 5B

Page 123+ Problems 7 - 12 8 - can draw NDFSM & convert if you need to 9 – use method discussed in class 11 – Draw DFSM – minimize if necessary See posted handout for minimization problems.

36