Top Banner
FINITE STATE MACHINES (AUTOMATA)
31

FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Mar 07, 2018

Download

Documents

vuongthien
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

FINITE STATE MACHINES (AUTOMATA)

Page 2: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Think about the On/Off button

Switch Example 1

Page 3: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Off OnPush

Push

Switch Example 1

The corresponding Automaton

PushPushPushPushInput:

Page 4: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Vending Machine Example 2

Vending machine dispenses Cola for $0.45

Page 5: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Input: DQQD

Vending Machine Example 2

Input Accepted

is final state45

Page 6: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

00,1

00

1

1

1

0111 111

11

1

The machine accepts a string if the process ends in a final state

Example 3

Page 7: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

00,1

00

1

1

1

The machine accepts a string if the process ends in a final state

states

states

q0

q1

q2

q3start state (q0)

final states (F)Input Symbols

Example 3

Page 8: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

An alphabet S is a finite set of symbols (in Ex3, S = {0,1})

A string over S is a finite sequence of elements of S (e.g. 0111)

For a string s, |s| is the length of s

The unique string of length 0 will be denoted by ? and will be called the empty string

The reversal of a string u is denoted by uR.Example: (banana)R = ananab

Definitions

Page 9: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

The + operator on strings

|u .v | = |u |+|v |

The concatenation of two strings is the string resulting from putting them together from left to right. Given strings u and v, denote the concatenation by u .v, or just uv.

Example: jap . an = japan, QQ . DD = QQDD

Q1: What’s the Java equivalent of concatenation?

Q2: Find a formula for |u .v |?

Definitions

Page 10: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

If S is an alphabet, S * denotes the set of all strings over S.

A language over S is a subset of S *

i.e. a set of strings each consisting of sequences of symbols in S.

Definitions

Page 11: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Example1: in our vending machine we haveΣ = { D, Q }Σ∗ = {λ,

D, Q, DD, DQ, QD, QQ,DDD, DDQ, DQD, DQQ, QDD, QDQ, QQD, QQQ,DDDD, DDDQ, … }

L = { u∈Σ∗ | u successfully vends }

Examples

Example2: in our switch example we haveΣ = { Push}Σ∗ = {λ,

Push, Push Push,Push Push Push,Push Push Push Push, … }

L = { Pushn | n is odd }

Page 12: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Q is the set of states

S is the alphabet

δ is the transition functionq0 ∈ Q is the start state

F ⊆ Q is the set of final states

A finite automaton is a 5-tuple M = (Q, S, δ, q0, F)

L(M) = the language of machine M= set of all strings machine M accepts

Definitions

Page 13: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

q2q2q2

q2q0q1

q1q0q0

baδ

}{},{

},,{

2

210

qFba

qqqQ

==Σ=

q2q0 q1

a b

a

b

a

b

DefinitionsState Diagram and Table

Page 14: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

FINITE STATE MACHINES (AUTOMATA)

Deterministic Finite Automata (DFA)

Non-Deterministic Finite Automata (NFA)

Non-Deterministic Finite Automata with empty move (?-NFA)

Page 15: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

A

B

C

D

E

F

G

E

H

G

I

A

B

C

D

E

F

G

E

H

G

I

Deterministic & Nondeterministic

Deterministic Non-Deterministic

One choice Multi choice è Backtrack

Page 16: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Deterministic & Nondeterministic

Deterministic Non-Deterministic

One choice Multi choice

A C

B

D

A Ca

a

a

a

From ONE state machine can go to another ONE state on one input

From ONE state machine can go to MANY states on one input

Page 17: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

DeterministicComputation

Non-DeterministicComputation

accept or reject accept

reject

Page 18: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

DETERMINISTIC FINITE AUTOMATA (DFA)

Page 19: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Q is the set of states

S is the alphabet

δ : Q × S ? Q is the transition function

q0 ∈ Q is the start state

F ⊆ Q is the set of accept states

A DFA is a 5-tuple M = (Q, S, δ, q0, F)

L(M) = the language of machine M= set of all strings machine M accepts

Definitions

Page 20: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

0

1

0

1

10011

Example 1Example 1

01

Deterministic Finite Automata (DFA)

Input Rejected

is not final state

Page 21: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

0

1

0

101

Q: What kinds of bit-strings are accepted?

Example 1Example 1

Deterministic Finite Automata (DFA)

A: Bit-strings that represent binary even numbers.

Page 22: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

01011010100100100100 010000010010λ

q1 q2 q3

1 0

0 1

0,1

Example 2Example 2

Deterministic Finite Automata (DFA)

reject

acceptaccept

rejectreject

Page 23: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

0,1q0

L(M) = {0,1}*∅

Exercise

Page 24: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

q0 q1

0 0

1

1

L(M) = { w | w has an even number of 1s}

Exercise

Page 25: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

q q00

1 0

1q0 q001

0 0 1

0,1

Build an automaton that accepts all and only those strings that contain 001

Exercise

Page 26: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Strings over {a,b} containing even number of a’s and odd number of b’s.

Σ*

Even(a) Odd(a)Even(b) Odd(b) Odd(b)Even(b)

bb

bb

aaa

aq0

Exercise

q0 = Even(a).Even(b)

q1 = Even(a).Odd(b) q3 = Odd(a).Odd(b)

q2 = Odd(a).Even(b)

q1

q2q3

Page 27: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Σ*

valid prefix invalid prefixEnd(a)

ab

b

a,b,c

a,cc

End(b) End(c)

a,b,c

Strings over {a,b,c} that has the form (ab)*c

Exercise

q0 = End(b)

q1 = End(a) q3 = Error

q2 = End(c)

q0 q1

q2q3

Page 28: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Design with a friend a machine that tells us when a base-10 number is divisible by 3.

What should your alphabet be?

How can you tell when a number is divisible by 3?

Exercise

Page 29: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

0 mod 3

1 mod 3

2 mod 30,3,6,9

0,3,6,9

0,3,6,9

1,4,7

1,4,7

1,4,7

2,5,8 2,5,8

2,5,8

Answer

Page 30: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

0 mod 3 1 mod 3

2 mod 3

0,3,6,9

0,3,6,9

0,3,6,9

1,4,7

1,4,71,4,7

2,5,8 2,5,8

2,5,8

Find the formal description of this automaton.

Exercise

Page 31: FINITE STATE MACHINES (AUTOMATA) - 会津大学hamada/AF/L2-FA07.pdfFINITE STATE MACHINES (AUTOMATA) Think about the On/Off button Switch Example 1. Off On Push Push Switch Example

Answer

Q = { 0 mod 3, 1 mod 3, 2 mod 3 } ( rename: {q0, q1, q2} )Σ = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }q0 = 0 mod 3F = { 0 mod 3 }

?),(d:Question

.)5,(d,)3,(d ,)7,(d

,)2,(d,)9,(d ,)2,(d:d

122221

010020

=

===

===→Σ×

jq

qqqqqq

qqqqqqQQ

i

3 mod )(),(d jii qjq +=