finite-state machines with no output

98
Finite-State Machines with No Output

Upload: tiana

Post on 12-Feb-2016

30 views

Category:

Documents


1 download

DESCRIPTION

Finite-State Machines with No Output. Kleene closure. A and B are subsets of V*, where V is a vocabulary The concatenation of A and B is AB={xy: x string in A and y string in B} Example: A={0, 11} and B={1, 10, 110} AB={01,010,0110,111,1110,11110} What is BA? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Finite-State Machines with No Output

Finite-State Machines with No Output

Page 2: Finite-State Machines with No Output

Kleene closure• A and B are subsets of V*, where V is a

vocabularyThe concatenation of A and B isAB={xy: x string in A and y string in B}

• Example: A={0, 11} and B={1, 10, 110}AB={01,010,0110,111,1110,11110}

• What is BA?• A0={λ}

An+1=AnA for n=0,1,2,…

Page 3: Finite-State Machines with No Output

Let A be any subset of V*.Kleene closure of A, denoted by A*, is

0

*

k

kAA

If B={0,1}, B*=V*.

Page 4: Finite-State Machines with No Output

Regular Expressions

Regular expressions describe regular languages

Example:

describes the language

*)( cba

,...,,,,,*, bcaabcaabcabca

Page 5: Finite-State Machines with No Output

Recursive Definition,,

1

1

21

21

*r

rrrrr

Are regular expressions

Primitive regular expressions:

2r1rGiven regular expressions and

Page 6: Finite-State Machines with No Output

Examples

)(* ccbaA regular expression:

baNot a regular expression:

Page 7: Finite-State Machines with No Output

Languages of Regular Expressions

: language of regular expression

Example

rL r

,...,,,,,*)( bcaabcaabcacbaL

Page 8: Finite-State Machines with No Output

Definition

For primitive regular expressions:

aaL

L

L

Page 9: Finite-State Machines with No Output

Definition (continued)

For regular expressions and

1r 2r 2121 rLrLrrL

2121 rLrLrrL

** 11 rLrL

11 rLrL

Page 10: Finite-State Machines with No Output

ExampleRegular expression: *aba

*abaL *aLbaL *aLbaL *aLbLaL

*aba ,...,,,, aaaaaaba

,...,,,...,,, baababaaaaaa

Page 11: Finite-State Machines with No Output

Example

Regular expression bbbaar **

}0,:{ 22 mnbbarL mn

Page 12: Finite-State Machines with No Output

Example

Regular expression *)10(00*)10( r

)(rL = { all strings with at least two consecutive 0 }

Page 13: Finite-State Machines with No Output

Equivalent Regular Expressions

• Definition:

• Regular expressions and

• are equivalent if

1r 2r

)()( 21 rLrL

Page 14: Finite-State Machines with No Output

Implementing Regular Expressions

• Regular expressions, regular grammars reasonable way to generates strings in language

• Not so good for recognizing when a string is in language

• Regular expressions: which option to choose, how many repetitions to make

• Answer: finite state automata

Page 15: Finite-State Machines with No Output

Three Equivalent Representations

Finite automata

Regularexpressions

Regular languages

Each can

describethe others

Kleene’s Theorem: For every regular expression, there is a deterministic finite-state automaton that defines the same language, and vice versa.

Page 16: Finite-State Machines with No Output

Regular Expression Regular Grammar

a* S | aS(a+b)* S | aS | bSa* + b* S | A | B

A a | aAB b | bB

a*b S b | aSba* S bA

A | aA(ab)* S | abS

Page 17: Finite-State Machines with No Output

EXAMPLE 1

Consider the language { ambn | m, n N}, which is represented by the regular expression a*b*.

A regular grammar for this language can be written

as follows: 

S | aS | B B b | bB.

Page 18: Finite-State Machines with No Output

Finite (State) Automata• A FA is similar to a compiler in that:

– A compiler recognizes legal programs in some (source) language.– A finite-state machine recognizes legal strings in some language.

• Example: Pascal Identifiers– sequences of one or more letters or digits,

starting with a letter:

letterletter | digit

S A

Page 19: Finite-State Machines with No Output

Finite Automaton

• Input

“Accept” or“Reject”

String

FiniteAutomaton

Output

Page 20: Finite-State Machines with No Output

Finite State Automata

• A finite state automation over an alphabet is illustrated by a state diagram:

– a directed graph– edges are labeled with elements of alphabet,– some nodes (or states), marked as final of “accepting”.– one node marked as start state

Page 21: Finite-State Machines with No Output

Transition Graph

initialstate

accepting state

statetransition

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

Page 22: Finite-State Machines with No Output

Initial Configuration

1q 2q 3q 4qa b b a

5q

a a bb

ba,

Input Stringa b b a

ba,0q

Page 23: Finite-State Machines with No Output

Reading the Input

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b b a

ba,

Page 24: Finite-State Machines with No Output

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b b a

ba,

Page 25: Finite-State Machines with No Output

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b b a

ba,

Page 26: Finite-State Machines with No Output

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b b a

ba,

Page 27: Finite-State Machines with No Output

0q 1q 2q 3q 4qa b b a

accept

5q

a a bb

ba,

a b b a

ba,

Input finished

Page 28: Finite-State Machines with No Output

Rejection

1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b a

ba,0q

Page 29: Finite-State Machines with No Output

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b a

ba,

Page 30: Finite-State Machines with No Output

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b a

ba,

Page 31: Finite-State Machines with No Output

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b a

ba,

Page 32: Finite-State Machines with No Output

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

reject

a b a

ba,

Input finished

Page 33: Finite-State Machines with No Output

Another Rejection

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q

Page 34: Finite-State Machines with No Output

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q

reject

Page 35: Finite-State Machines with No Output

Another Example

a

b ba,

ba,

0q 1q 2q

a ba

Page 36: Finite-State Machines with No Output

a

b ba,

ba,

0q 1q 2q

a ba

Page 37: Finite-State Machines with No Output

a

b ba,

ba,

0q 1q 2q

a ba

Page 38: Finite-State Machines with No Output

a

b ba,

ba,

0q 1q 2q

a ba

Page 39: Finite-State Machines with No Output

a

b ba,

ba,

0q 1q 2q

a ba

accept

Input finished

Page 40: Finite-State Machines with No Output

Rejection Example

a

b ba,

ba,

0q 1q 2q

ab b

Page 41: Finite-State Machines with No Output

a

b ba,

ba,

0q 1q 2q

ab b

Page 42: Finite-State Machines with No Output

a

b ba,

ba,

0q 1q 2q

ab b

Page 43: Finite-State Machines with No Output

a

b ba,

ba,

0q 1q 2q

ab b

Page 44: Finite-State Machines with No Output

a

b ba,

ba,

0q 1q 2q

ab b

reject

Input finished

Page 45: Finite-State Machines with No Output

Finite State Automata

• A finite state automation M=(S,I,f,s0,F) consists of

• a finite set S of states, • a finite input alphabet I, • a state transition function f: S x I S, • an initial state s0, • a subset F of S that represent the final

(accepting) states.

Page 46: Finite-State Machines with No Output

Finite Automata• Transition

s1 a s2

• Is read ‘In state s1 on input “a” go to state s2’

• If end of input– If in accepting state => accept– Otherwise => reject

• If no transition possible (got stuck) => reject• FSA = Finite State Automata

Page 47: Finite-State Machines with No Output

Example FSA

Construct the state diagram for M=(S,I,f,s0,F), where S={s0, s1, s2, s3}, I={0,1}, F={s0, s3}and the transition function:state Input 0 Input 1s0 s0 s1

s1 s0 s2

s2 s0 s0

s3 s2 s1

Page 48: Finite-State Machines with No Output

Language accepted by FSA• The language accepted by a FSA is the set of strings

accepted by the FSA. • in the language of the FSM shown below:

x, tmp2, XyZzy, position27. • not in the language of the FSM shown below:

• 123, a?, 13apples.

letterletter | digit

S A

Page 49: Finite-State Machines with No Output

Example:• FSA that accepts three letter English words that begin with

p and end with d or t.• Here we use the convenient notation of making the state

name match the input that has to be on the edge leading to that state.

pt

a

o

u

d

i

Page 50: Finite-State Machines with No Output

Languages Accepted by FAs

FA

Definition:The language contains all input strings accepted by

= { strings that bring to an accepting state}

M

MLM

M ML

Page 51: Finite-State Machines with No Output

Example

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

abbaML M

accept

Page 52: Finite-State Machines with No Output

Example

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

abbaabML ,, M

acceptacceptaccept

Page 53: Finite-State Machines with No Output

Example•

a

b ba,

ba,

0q 1q 2q

}0:{ nbaML n

accept trap state

Page 54: Finite-State Machines with No Output

Formal Definition

Finite Automaton (FA) FqQM ,,,, 0

Q

0q

F

: set of states: input alphabet: transition function: initial state: set of accepting states

Page 55: Finite-State Machines with No Output

Input Alphabet

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

ba,

Page 56: Finite-State Machines with No Output

Set of States

Q

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

543210 ,,,,, qqqqqqQ

ba,

Page 57: Finite-State Machines with No Output

Initial State

0q

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q

Page 58: Finite-State Machines with No Output

Set of Accepting States

F

0q 1q 2q 3qa b b a

5q

a a bb

ba, 4qF

ba,

4q

Page 59: Finite-State Machines with No Output

Transition Function

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

QQ :

ba,

Page 60: Finite-State Machines with No Output

10 , qaq

2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q 1q

Page 61: Finite-State Machines with No Output

50 , qbq

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q

Page 62: Finite-State Machines with No Output

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

32 , qbq

Page 63: Finite-State Machines with No Output

Transition Function

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

a b0q

1q

2q

3q

4q

5q

1q 5q

5q 2q5q 3q

4q 5q

ba,5q5q5q5q

Page 64: Finite-State Machines with No Output

Extended Transition Function

*

QQ *:*

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

Page 65: Finite-State Machines with No Output

20 ,* qabq

3q 4qa b b a

5q

a a bb

ba,

ba,0q 1q 2q

Page 66: Finite-State Machines with No Output

40 ,* qabbaq

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

Page 67: Finite-State Machines with No Output

50 ,* qabbbaaq

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q

Page 68: Finite-State Machines with No Output

qwq ,*

Observation: if there is a walk from to with label then

q qw

q qw

q qkw 21

1 2 k

Page 69: Finite-State Machines with No Output

50 ,* qabbbaaq

1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,0q

Example: There is a walk from to with label

0qabbbaa

5q

Page 70: Finite-State Machines with No Output

Recursive Definition )),,(*(,*

,*

wqwq

qq

q qw1q

qq

qwq

),(

,*

1

1

1

,*

),(,*

qwq

qwq

)),,(*(,* wqwq

Page 71: Finite-State Machines with No Output

0q 1q 2q 3q 4qa b b a

5q

a a bb

ba,

ba,

2

10

00

0

,,,

,,,*),,(*

,*

qbq

baqbaq

baqabq

Page 72: Finite-State Machines with No Output

Language Accepted by FAs

• For a FA

• Language accepted by :

FqQM ,,,, 0

M

FwqwML ,*:* 0

0q qw Fq

Page 73: Finite-State Machines with No Output

Observation

• Language rejected by :

FwqwML ,*:* 0

M

0q qw Fq

Page 74: Finite-State Machines with No Output

Example

• ML = { all strings with prefix }ab

a b

ba,

0q 1q 2q

accept

ba,3q

ab

Page 75: Finite-State Machines with No Output

Example

• ML = { all strings without

substring }001

0 00 001

1

0

110

0 1,0

Page 76: Finite-State Machines with No Output

Example

*,:)( bawawaML

a

b

ba,

a

b

ba

0q 2q 3q

4q

Page 77: Finite-State Machines with No Output

Deterministic FSA’s

• If FSA has for every state exactly one edge for each letter in alphabet then FSA is deterministic

• In general FSA in non-deterministic.

• Deterministic FSA special kind of non-deterministic FSA

Page 78: Finite-State Machines with No Output

Example FSA

• Regular expression: (0 1)* 1• Deterministic FSA

0 1

1

0

Page 79: Finite-State Machines with No Output

Example DFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1

0 1

1

0

Page 80: Finite-State Machines with No Output

Example DFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1

0 1

1

0

Page 81: Finite-State Machines with No Output

Example DFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1

0 1

1

0

Page 82: Finite-State Machines with No Output

Example DFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1

0 1

1

0

Page 83: Finite-State Machines with No Output

Example DFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1

0 1

1

0

Page 84: Finite-State Machines with No Output

Example DFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1

0 1

1

0

Page 85: Finite-State Machines with No Output

Example DFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1

0 1

1

0

Page 86: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 1)* 1• Non-deterministic FSA

0

1

1

Page 87: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1

0

1

1

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1

Page 88: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1

0

1

1

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1

Page 89: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1

0

1

1

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1

Page 90: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1• Guess

0

1

1

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1

Page 91: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1• Backtrack

0

1

1

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1

Page 92: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1• Guess again

0

1

1

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1

Page 93: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1• Guess

0

1

1

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1

Page 94: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1• Backtrack

0

1

1

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1

Page 95: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1• Guess again

0

1

1

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1

Page 96: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1

0

1

1

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1

Page 97: Finite-State Machines with No Output

Example NFSA

• Regular expression: (0 1)* 1• Accepts string 0 1 1 0 1• Guess (Hurray!!)

0

1

1

• Regular expression: (0 + 1)* 1• Accepts string 0 1 1 0 1

Page 98: Finite-State Machines with No Output

If a language L is recognized by a nondeterministic FSA, then L is recognized by a deterministic FSA

See thm. 1, p. 813. Why?

Intuition: construct the deterministic finite automaton (may be much larger!).