cmps 3223 theory of computation

63
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

Upload: baka

Post on 29-Jan-2016

52 views

Category:

Documents


1 download

DESCRIPTION

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. Finite State Machines & Regular Languages. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CMPS 3223 Theory of Computation

CMPS 3223Theory 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

Page 2: CMPS 3223 Theory of Computation

Finite State Machines& Regular Languages

Chapter 5APages 54-71

2

Page 3: CMPS 3223 Theory of Computation

Homework for 5A

Page 121+

•Exercises 1 – 6 (all parts)

•#2 – may need to develop non-deterministic first

•#6 – can be deterministic or non-deterministic

More will be assigned in 5B.3

Page 4: CMPS 3223 Theory of Computation

Languages and Machines

4

Page 5: CMPS 3223 Theory of Computation

Regular Languages

Regular Language

Regular Expression

Finite State Machine

L

Accepts

Regular Grammar

Defines

5

Page 6: CMPS 3223 Theory of Computation

Finite State Machine (FSM)Non-technical definition

Finite State Machine (FSM)•Computational device having a string as input and having one of 2 possible outputs – Accept or Reject

6

Page 7: CMPS 3223 Theory of Computation

Finite State Machine (FSM)Non-technical Description of Operation

• There is a designated Start state• Reads 1 character at a time, left to right• (Current State, Character) New State

– Repeat• Stop when no more characters OR no defined

New State• Every state is designated as either an Accepting

or Rejecting state• Ending state designation determines if string is

Accepted or Rejected 7

Page 8: CMPS 3223 Theory of Computation

Example Finite State Machine

8

Incoming arrow indicates start stateDouble Circles Accepting stateSingle Circles Rejecting stateNote: time required <= |string|

Page 9: CMPS 3223 Theory of Computation

Finite State Machine Example• Drinks in a machine cost 25¢• Accepts Nickels, Dimes, Quarters only• Insert coins, push “Soda” button, drink is

dispensed if “Enough Money”• LANGUAGE: String of coin sequences adding

to 25¢ (or more) allowing drink to be dispensed– {Q,DDN, NNND, DNNN,DND, etc…} – Actually, S can be inserted within any string– What about extra money (change)?

• Look at one final product!9

Page 10: CMPS 3223 Theory of Computation

FSM for Drink Machine

An FSM to accept $.25 in change for a drinkNote that states are “labeled” for clarityMachine has ERRORS!! Can you find them??

But you get the idea!

Should there be a “50” state? Why or why not?

10

Page 11: CMPS 3223 Theory of Computation

Formal Definition of a DFSMMEMORIZE!!!

Deterministic Finite State Machine (DFSM) is M:M = (K, , , s, A), where: K is a finite set of states is an alphabet s K is the initial state A K is the set of accepting states, and is the transition function from (K ) to K

*The set of ALL strings accepted by M form the Language defined by the DFSM.

11

Page 12: CMPS 3223 Theory of Computation

Configurations of DFSMs

A Configuration of a DFSM M is an element of K *

Configuration captures the two things that make a difference to M’s future behavior:

• its current state• the input that remains to be read.

The Initial Configuration of a DFSM M, on input w, is (sM, w) , where sM is start state of M

12

Page 13: CMPS 3223 Theory of Computation

Transition Function -

• Defines the DFSM, one state at a time

is set of all pairs of states in M & characters in ∑

(Current State, Current Character) New State

Note: we often reduce the set to those pairs that are “useful” or to only those that can lead to an Accepting state

13

Page 14: CMPS 3223 Theory of Computation

Complete vs. Incomplete FSM• Complete FSM

– A transition is defined for every possible state and every possible character in the alphabet

– Note: Can cause FSM to be larger than necessary, but ALWAYS processes the entire string

• Incomplete FSM– One which defines a transition for every

possible state & every possible character in the alphabet which can lead to an accepting state

– Note: If no transition is defined, the string is Rejected14

Page 15: CMPS 3223 Theory of Computation

Complete vs. Incomplete Example

∑ = {a, b, c}

L = {strings beginning with c & followed by

only a’s & b’s, at least 1}

15

SC

q1a

b

Page 16: CMPS 3223 Theory of Computation

The Yields Relations

The yields-in-one-step relation |-M

(q, w) |-M (q', w') iff • w = a w' for some symbol a , and • (q, a) = q'

|-M * is the reflexive, transitive closure of |-M.

Transitions defined by processing ONE character

M refers to the particular machine, so can leave it off, usually.

16

Page 17: CMPS 3223 Theory of Computation

Computations Using FSMsA Computation by M is a finite sequence of

configurations C0, C1, …, Cn for some n 0 such that:

*C0 is an initial configuration,*Cn is of the form (q, ), for some state q KM

• indicates empty string, entire string is processed & implies a complete DFSM

*C0 |-M C1 |-M C2 |-M … |-M Cn.

However, M Halts when the last character has be processed or a next transition is not defined

17

Page 18: CMPS 3223 Theory of Computation

Accepting and RejectingA DFSM M Accepts a string w iff

(s, w) |-M * (q, ), for some q AM.

A DFSM M Rejects a string w iff(s, w) |-M* (q, ), for some q AM.

The language accepted by M, denoted L(M), is the set of all strings accepted by M.

Theorem: Every DFSM M, on input s, halts in |s| steps.

18

Page 19: CMPS 3223 Theory of Computation

Example Language

L = {x | x is an odd integer}•Is 3456 L?

•How will we develop a machine to answer the question?

•Process characters left to right

•Assume the current character is the LAST character

•What are the necessary states?19

Page 20: CMPS 3223 Theory of Computation

An Example Computation

An FSM to accept odd integers: even odd

even

q0 q1

odd

On input 235, the configurations are:

(q0, 235) |- (q0, 35)(q0, 35) |- (q1, 5)(q1, 5) |- (q1, ) Accept

Thus (q0, 235) |-* (q1, ) [* means 0 or more steps]20

Page 21: CMPS 3223 Theory of Computation

Developing a DFSM

Heuristic – a generality, a recommendation

•List or Label the possible states of a machine based upon the language or problem definition

•Determine the transitions between the states

21

Page 22: CMPS 3223 Theory of Computation

Developing DFSM Examples

• L = {x | x contains an even number of a’s}– What would the states of the DFSM be?– What would the transitions be?

• L = {x | x contains an even number of a’s & odd number of b’s}– What would the states of the DFSM be?– What would the transitions be?

22

Page 23: CMPS 3223 Theory of Computation

Even a’s Odd b’s

23

Let L = {w {a, b}* : w contains an even number of a’s and an odd number of b’s}

Page 24: CMPS 3223 Theory of Computation

5.2 - 5.3 Regular Languages

A language is regular iff it is accepted by some FSM.

We have definedseveral regularlanguages…

24

Page 25: CMPS 3223 Theory of Computation

Regular Examples

L = {w {a,b}*| every region of a’s is of

even length}

L = {w {a,b}*| w contains at most 1 b }

L = {w {a,b}*| no 2 consecutive

characters are the same }

L = {w {a,b}*| anbn, n>=1}

25

Page 26: CMPS 3223 Theory of Computation

A Very Simple Example

L = {w {a, b}* | every a is immediately

followed by a b}.

26

The Existence Problem•Does the definition imply the existence of any characters?•Is the empty string a member of the language?

Page 27: CMPS 3223 Theory of Computation

Parity Checking Example

L = {w {0, 1}* : w has odd parity}.

27

Complete or incomplete DFSM?

Page 28: CMPS 3223 Theory of Computation

No More Than One b

L = {w {a, b}* : w contains no more than

1 b}

28

Complete or incomplete DFSM?

Page 29: CMPS 3223 Theory of Computation

Error States or Dead States

An Error State or Dead State is a rejecting state from which the string can never go back to an Accepting State.

Some DFSM can have, others cannot!

29

Page 30: CMPS 3223 Theory of Computation

Error States or Dead States

∑ = {a, b, c}

L = {strings beginning with c & followed by

only a’s & b’s, at least 1}

30

SC

q1a

b

d

a,b

a, b

Page 31: CMPS 3223 Theory of Computation

Checking Consecutive Characters

L = {w {a, b}*| no two consecutive characters are the same}.

31

ERROR – d is not an accepting state

d is Dead State; once in that state string cannot be accepted.

Page 32: CMPS 3223 Theory of Computation

DFSM Example

L = {w {a, b}* : every a region in w is of even length}

d is a Dead State – Can Be Removed!

32

Page 33: CMPS 3223 Theory of Computation

The Language of Floating Point

Numbers is Regular

Example strings:

+3.0, 3.0, 0.3E1, 0.3E+1, -0.3E+1, -3E8

The language is accepted by the DFSM:

33

Page 34: CMPS 3223 Theory of Computation

A Simple Communication Protocol

34

Page 35: CMPS 3223 Theory of Computation

Controlling a Soccer-Playing Robot

35

Page 36: CMPS 3223 Theory of Computation

A Simple Controller

36

Page 37: CMPS 3223 Theory of Computation

Vowels in Alphabetical Order

L = {w {a - z}* : all five vowels, a, e, i, o, and u,

occur in w in alphabetical order}.

37

Page 38: CMPS 3223 Theory of Computation

Developing FSMs

Sometimes, it is “easier” to develop the FSM for the complement of the language, then reverse the Accept & Reject states

Do you believe this would work???

L = {w {a, b}* : w does not contain the substring aab}.

38

Page 39: CMPS 3223 Theory of Computation

Complement of FSMs

L = {w {a, b}* : w does not contain the substring aab}.

Start with a machine for L

How must it be changed?39

Page 40: CMPS 3223 Theory of Computation

A Building Security System

L = {event sequences such that the alarm should sound}

40

Page 41: CMPS 3223 Theory of Computation

FSMs Predate Computers

The Prague Orloj, originally built in 1410.41

Page 42: CMPS 3223 Theory of Computation

The Abacus

42

Page 43: CMPS 3223 Theory of Computation

The Missing Letter LanguageLet = {a, b, c, d}.

Let LMissing = {w : there is a symbol ai not appearing in w}.

Try to make a DFSM for Lmissing

First develop machine for set of strings containing ALL 4 characters, then reverse.

43

Page 44: CMPS 3223 Theory of Computation

5.4 Definition of an NDFSMMemorize

A Non-deterministic Finite State Machine M = (K, , , s, A), where:• K is a finite set of states• is an alphabet• s K is the initial state• A K is the set of accepting states, • is the transition relation, a finite

subset of (K ( {})) to KNon-deterministic if there is at least one state from

which there are 2 or more transitions defined for a single character. 44

Page 45: CMPS 3223 Theory of Computation

NDFSM Example

45

Page 46: CMPS 3223 Theory of Computation

*NDFSM – Accepting a String

A NDFSM M accepts a string w iff there exists some path along which w drives M to some Accepting state.

The language accepted by M, denoted L(M), is the set of all strings accepted by M.

NOTE: significant difference between deterministic & non-deterministic

46

Page 47: CMPS 3223 Theory of Computation

*NDFSM – Rejecting a String

A NDFSM M rejects a string w iff all paths along which w drives M lead to an Rejecting state.

NOTE: significant difference between deterministic & non-deterministic

47

Page 48: CMPS 3223 Theory of Computation

DFSM vs. NDFSM

Time Complexity of determining accept/reject for string w

•DFSM – O(n) where n = |w|

•NDFSM – O(X) where X = ????– X stated in terms of what??– How does complete vs. incomplete

machine affect time?

48

Page 49: CMPS 3223 Theory of Computation

NDFSM Time Complexity

49

Given a string w with|w| = 2, how many possible paths to determine accept orreject? What if |w| is larger? What if the FSM gets larger?

Page 50: CMPS 3223 Theory of Computation

Epsilon () Transitions

• An Epsilon () Transition

– Is one that does not consume a character of the input string.

– Allows movement from one state to another without consuming a character of the input string.

50

Page 51: CMPS 3223 Theory of Computation

Sources of Non-determinism2 Types

51

1. Epsilon transition2. 2+ transitions for a single character

Which path should be taken???

Page 52: CMPS 3223 Theory of Computation

Use of Epsilon TransitionsOptional Substrings

L = {w {a, b}* : w is made up of an optional a followed by aa followed by zero or more b’s}

L = {aa, aaa, aab, aaab, aabb, aaabb, … }

52

Page 53: CMPS 3223 Theory of Computation

Use of Epsilon TransitionsMultiple Sublanguages

L = {w {a, b}* : w = aba or |w| is even}

53

Page 54: CMPS 3223 Theory of Computation

Pattern Matching

L = {w {a, b, c}* : x, y {a, b, c}* (w = x abcabb y)}.

An NDFSM:

54

But how do you know when to take the a-transition to q1?Strategically, 1.First time, take it on first a,2.Second time, take it on second a, etc…Until all a’s are exhausted or you accept the string.

Page 55: CMPS 3223 Theory of Computation

NDFSM to DFSM

1. Try again –develop a Deterministic Machine

2. There is an algorithm that allows you to convert NDFSM to DFSM

** but more on that later

55

Page 56: CMPS 3223 Theory of Computation

Pattern Matching

L = {w {a, b, c}* : x, y {a, b, c}* (w = x abcabb y)}.

A DFSM:

56

Page 57: CMPS 3223 Theory of Computation

Multiple Keywords

L = {w {a, b}* : x, y {a, b}*

(w = x abbaa y or w = x baba y)}.

57

Page 58: CMPS 3223 Theory of Computation

Checking from the End

L = {w {a, b}* : the fourth to the last character is a}

58

Page 59: CMPS 3223 Theory of Computation

Another Pattern Matching Example

L = {w {0, 1}* : w is the binary encoding of a positive integer that is divisible by 16 or is

odd}

How can you rewrite as a string problem?

59

Page 60: CMPS 3223 Theory of Computation

Another Pattern Matching Example

L = {w {0, 1}* : w is the binary encoding of a positive integer that is

(1) divisible by 16, or (2) is odd}

How can you rewrite as a string problem?(1)5th character from end is a 1(2) last character is 1

60

Page 61: CMPS 3223 Theory of Computation

Another NDFSMDo for Homework!

L1= {w {a, b}*: aa occurs in w}L2= {x {a, b}*: bb occurs in x}L3= {y : L1 or L2 }

M1 =

M2=

M3= 61

Page 62: CMPS 3223 Theory of Computation

Analyzing Nondeterministic FSMs

Does this FSM accept: baaba

Remember: we just have to find one accepting path.

62

Page 63: CMPS 3223 Theory of Computation

Homework for 5APages 53-71

Page 121+

•Exercises 1 – 6 (all parts)

•#2 – may need to develop non-deterministic first

•#6 – can be deterministic or non-deterministic

More will be assigned in 5B.63