Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata)...

36
cl Finite-State Machines (Automata) lecture 12 a simple form of computation used widely one way to find patterns 1

Transcript of Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata)...

Page 1: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

clFinite-State Machines (Automata) lecture 12

• a simple form of computation

• used widely

• one way to find patterns

1

Page 2: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

2

A

D B

C

current

next

A B C DB C D A

Page 3: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

Industry• real-time control, vending machines, cash dispensers, etc.

Electronic circuits• data path / control path

• memory / cache handling

• protocols, USB, etc.

Communication protocols• initiation and maintenance of communication links

• error detection and handling, packet retransmission

Language analysis• natural languages

• programming languages

• search engines

Application Fields

001

3

Page 4: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

Informatics 1 School of Informatics, University of Edinburgh

4

Finite State Machines

• A conceptual tool for modelling reactive systems.

• Not limited to software systems. • Used to specify required system behaviour in a

precise way. • Then implement as software/hardware (and

perhaps verify behaviour against FSM).

• Finite state machines can also be viewed as rules for generating sets of strings ( = sets of finite sequences of symbols)

Page 5: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

A Decimal Number

d

d+⎮− .

d d

d

ε

startend

startstart

alternative paths repetition

skip

5

d = {"0","1","2","3","4","5","6","7","8","9"}+⎮− = { "+","-" } ε = "" . = "."

Each path from start to end, with a choice ofone of the labels for each edge traversal, gives a decimal number.

Page 6: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

6

This gives us a new way of defining sets by rules.

d

d+⎮− . d

ε

d d numberwhole number

A whole number is a digit followed by any number of digits,or a sign followed by a digit followed by any number of digits

Page 7: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

7

This gives us a new way of defining sets by rules.

d

d+⎮− . d

ε

d d numberwhole numberA whole number is a digit followed by any number of digits,or a sign followed by a digit followed by any number of digits.

A number is a whole number or a whole number followed by a decimal point followed by a digit followed by any number of digits

Page 8: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

finite state machines

R

8

A labelled directed graph nodes edges

with a set of start nodes and a set of end nodes

Each edge is labelled with a set of strings

Page 9: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

finite state machines

R

9

A labelled directed graph nodes edges

with a set of start nodes and a set of end nodes

Each edge is labelled with a set of strings

Each path from a start node to an end node, with a choice ofone of the labels for each edge traversal, gives, by concatenation, a string.

The language defined by the machine is the set of all such strings

Page 10: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

A Decimal Number

d

d+⎮− .

d d

d

ε

startend

startstart

alternative paths repetition

skip

10

The language defined by this machine is the set of all decimal numerals

Page 11: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

finite state machines

R S

11

What happens when we link two machines together?

Page 12: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

sequence RS

R S

ε

12

RS = { r ++ s | r ∈ R ∧ s ∈ S }

Page 13: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

alternation R | S

R S

13

R | S = R ∪ S

Page 14: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

iteration R*

ε

14

R

εε

"" ∈ R* x ∈ R*, r ∈ R ⇒ x ++ r ∈ R*

Page 15: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

finite state spaghetti

15

A language is a set of finite sequences of words.

A language is a set of finite sequences of symbols.

A language is regular iff it is the language recognised by some Finite State Machine.

Page 16: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

Start state

State

Accepting state

s ta,b Transitions

each transition is labelled with a finite sequence of tokens, the tokens, depend on the application, they may be letters, symbols, words, actions, …

0

2

1

b a,b a,b

a

Three statesSix transitions

Page 17: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

Start state

State

Accepting state

s ta,b Transitions

0

2

1

b a,b a,b

a

Three statesOne start stateOne acceptingSix transitions

Normally we only use sequences of length 0 or 1 as labels. Then we write a for <a> and ε for <>

We generate strings of tokens by tracing a path from a start state to an accepting state.

Which strings are accepted by this automaton?

Page 18: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

Start state

State

Accepting state

s ta,b Transitions

0

2

1

b a,b a,b

a

Three statesOne start stateOne acceptingSix transitions

Normally we only use sequences of length 0 or 1 as labels. Then we write a for <a> and ε for <>

Strings that begin with b and are of even length or begin with a and are of odd length

Page 19: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

0

2

1

b b a

a

Start state

State

Accepting state

s ta,b Transitions

This machine is non-deterministic some strings have more than one trace some strings have no traces

Which of these strings are accepted? abba abab baba aba bab ab ba a b

Three statesTwo start statesOne acceptingFour transitions

Page 20: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

0

2

1

b b a

a

Start state

State

Accepting state

s ta,b Transitions

This machine is non-deterministic some strings have more than one trace some strings have no traces

Which of these strings are accepted? abba abab baba aba bab ab ba a b

Three statesTwo start statesOne acceptingFour transitions

Page 21: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

0

2

1

b b a

a

Start state

State

Accepting state

s ta,b Transitions

This machine is non-deterministic some strings have more than one trace some strings have no traces

Which of these strings are accepted? abba abab baba aba bab ab ba a b

Three statesTwo start statesOne acceptingFour transitions

a ε Lba ε L

x ε L → x ba ε L

Page 22: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

KISS – DFA

22

Deterministic Finite Automaton

Exactly one start state, and from each state, q, for each token, t,

there is exactly one transition

from s with label t

Page 23: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

This machine has four states, Q = { s0, s1, s2, s3 }

Its alphabet has three symbols Σ = { 0, 1, 2 }

For each (state, symbol) pair, (q,s)

there is exactly one transition from q with label s

This means that qʹ is determined by (q,s)

q qʹs

Page 24: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

For each (state, symbol) pair, (q,s)

there is exactly one transition from q with label s

This means that qʹ is determined by (q,s)

It has only one start state, this means that any input string

determines a sequence of states.

q qʹs

This is a deterministic finite-state automaton

DFA

Page 25: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

For each (state, symbol) pair, (q,s)

there is exactly one transition from q with label s

qʹ is determined by (q,s)It has only one start state

Every DFA has a next-state function

qʹ = F(q,s)

q qʹs

This is a deterministic finite-state automaton,

DFA

Page 26: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

DFA ⊂ NFA

26

A DFA has only one start state For each (state, symbol) pair, (q,s)

there is exactly one transition from q with label s

qʹ is determined by q and s

Every DFA has a next-state function

qʹ = F(q,s)

any input stringdetermines

a sequence of states.

We will focus now on DFA and return later to NFA.Then we will show that for any NFA there isa a DFA that defines the same language.

Page 27: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

Every DFA has a next-state function

qʹ = F(q,s)

q qʹs

This is a deterministic finite-state automaton,

DFA

Since we have finitely many symbols and states

This can be given by a table

0 1 2s0 s0s1 s2s2 s3s3 s1

Page 28: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

Every DFA has a next-state function

qʹ = F(q,s)

q qʹs

This is a deterministic finite-state automaton,

DFA

Since we have finitely many symbols and states

This can be given by a next-state table

q\s 0 1 2s0 s0 s1 s2s1 s3 s0 s1s2 s2 s3 s0s3 s1 s2 s3

Page 29: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

Every DFA has a next-state function

qʹ = F(q,s)

q qʹs

This is a deterministic finite-state automaton,

DFA

We can also represent the transitions by a relation.

Which symbol(s) take us from state q to state r ?

q\r s0 s1 s2 s3s0 0 1 2s1s2s3 0 1

Page 30: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

Every DFA has a next-state function

qʹ = F(q,s)

q qʹs

This is a deterministic finite-state automaton,

DFA

We can also represent the transitions by a relation.

Which symbol(s) take us from state q to state r ?

q\r s0 s1 s2 s3s0 0 1 2s1 1 2 0s2 2 0 1s3 0 1 2

Page 31: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

For every DFA, any input string determines a path or trace

This is a deterministic finite-state automaton,

DFA

input 000 021 12011 12 21 22 111 110 212

path s0 s0 s0 s0 s0 s0 s2 s3 s0 s1 s1 s3

Page 32: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

For every DFA, any input string determines a path

This is a deterministic finite-state automaton,

DFA

input 000 021 120

path s0 s0 s0 s0 s0 s0 s2 s3 s0 s1 s1 s3

11 12 21 22 111 110 212

s0 s1 s0 s0 s1 s1 s0 s2 s3 s0 s2 s0

s0 s1 s0 s1 s0 s1 s0 s0 s0 s2 s3 s3

Page 33: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

Which paths end in an accepting state?

This is a deterministic finite-state automaton,

DFA

input 000021 120

path s0 s0 s0 s0s0 s0 s2 s3 s0 s1 s1 s3

1112 21 22111 110212

s0 s1 s0s0 s1 s1 s0 s2 s3 s0 s2 s0

s0 s1 s0 s1 s0 s1 s0 s0s0 s2 s3 s3

Page 34: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

This is a deterministic finite-state automaton,

DFA

45 7 813 1223

Which paths end in an accepting state?

07 15

path s0 s0 s0 s0s0 s0 s2 s3 s0 s1 s1 s3

s0 s1 s0s0 s1 s1 s0 s2 s3 s0 s2 s0

s0 s1 s0 s1 s0 s1 s0 s0s0 s2 s3 s3

input 000021 1201112 21 22111 110212

Page 35: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

This is a deterministic finite-state automaton,

DFA

What is the meaning of each state?

0 1 2 3 4 5 6 7 8 9

.Express each of these decimal numbers

in ternary notation, and find out

what state is reached by the trace they determine

Page 36: Finite-State Machines (Automata) lecture 12 - inf.ed.ac.uk · cl Finite-State Machines (Automata) lecture 12 • a simple form of computation • used widely • one way to find

This is a deterministic finite-state automaton,

DFA

What is the meaning of each state?

0 1 2 3 4 5 6 7 8 9

This machine counts ternary mod 4

F(q, s) = 3 * q + s (mod 4)