finite automata 1

advertisement
Finite Automata 1
Representation
Application and Examples
1
What are FA?
Finite automata are finite collections of
states with transition rules that take
you from one state to another.
2
Representing FA
Simplest representation is a graph.
 Nodes = states.
 Arcs indicate state transitions.
 Labels on arcs tell what causes the
transition.
3
FA model of on-off switch
push
off
on
Start
push
4
Protocol for Sending Data
data in
Ready
Sending
Start
timeout
done
5
FA Recognizing Strings Ending in “ing”
Get next character in any state except the finish state
Where we go depends on which character
Not i or g
Not i
Start
i
Not i or n
empty
i
Double circle denotes
“finish” or “accepting”
state
Saw i
n
Saw in
g
Saw ing
i
String recognized may not be the entire input
6
Automata to Code
 In C/C++, make a piece of code for
each state that does the following
1. Reads the next input.
2. Decides on the next state.
3. Jumps to the beginning of the code for
that state.
7
Code for state “saw i”
2: /* i seen */
c = getNextInput();
if (c == ’n’) goto 3;
else if (c == ’i’) goto 2;
else goto 1;
3: /* ”in” seen */
. . .
8
E-commerce example
E-commerce requires protocols to
prevent fraud
Protocols can be modeled by finite
automata
Example: protocol to ensure that emoney is transferred from customer to
merchant on shipped purchases
9
E-commerce 2
pay
2
customer
cancel
cancel
start
bank
transfer
redeem
1
3
4
Pay does not change state of customer.
Free to cancel order
Bank can go down either cancel path or
path that leads to e-money transfer
following a redeem request from e-store
10
E-commerce 3
pay
a
redeem
c
b
c
redeem
f
ship store
ship
ship
start
transfer
e
transfer
g
Store preferred path a->b->c->f->g
Alternate paths allow ship before transfer
complete
11
This representation is not complete
pay
a
start
cancel
pay
b
redeem
c
2
cancel redeem transfer
1
3
4
start
redeem
f
ship
ship
ship
c
transfer
e
transfer
g
All parts must have a
defined response to every
input even if the response
is no change of state.
12
Each loop labeled by actions that do
not change states
C
P,C,R,S,T
P,C
P
a
start
2
start
1
P,S
c
P,S
P,C
P,C,R,S
C
R
b
S
3
P,C,R,S
T
4
P,C
P,C
R
R
T
c
S
e
T
P,C
P=pay
C=cancel
R=redeem
S=ship
T=transfer
f
S
g
P,C
13
Customer is in a “free” state
C
P,C,S,R,T
P,C
P
a
b
S
start
2
1
P,S
P,C
P,S
P,C,R,S
C
start
c
R
3
P,C,R,S
T
4
P,C
P,C
R
R
T
c
S
e
P,C
T
f
S
g
P,C
No interactions between
store and bank can
change customer’s state
14
Analysis of Store-Bank interactions reveals paths
to failure of e-protocols
Analysis requires “product” FA with all possible
combinations of (store, bank) states and paths
that connect them.
Will discuss construction of product FAs later in
course
15
Store-Bank product FA
Paths exist to state
(c,2) that has no exit.
Customer cancelled
and store shipped
before transfer of
payment from bank.
16
17
Analysis of protocol using FA
C
P,C,S,R,T
P,C
P
a
b
S
start
2
1
P,S
P,C
P,S
P,C,R,S
C
start
c
R
3
P,C,R,S
T
4
P,C
P,C
R
R
T
c
S
e
P,C
T
f
S
g
P,C
If customer state is C,
then interactions between
store and bank can
lead to failure
18
Store-Bank product automaton 3
Product automaton
reveals problem with
the protocol. State
(c,2) is accessible
from (a,1) but only
loops back.
2
C
start
1
P,S
P,S
C
a
start
P,C
P
b
S
c
P,C
P,C
P,C
R
R
T
c
S
e
P,C
T
f
S
g
P,C
State (c,2) is case where
P,C,R,S
P,C,R,S store has shipped before
redeeming payment from
R
T
bank and customer has
3
4
canceled
19
Far out example
 On a distant planet, there are three
species, a, b, and c.
 Any two different species can mate. If
they do:
1. The participants die.
2. Two children of the third species are
born.
20
Strange Planet – (2)
Observation: the number of individuals
never changes.
The planet fails if at some point all
individuals are of the same species.
 Then, no more breeding can take place.
State = sequence of three integers –
the numbers of individuals of species a,
b, and c.
21
Strange Planet – Questions
In a given state, must the planet
eventually fail?
In a given state, is it possible for the
planet to fail, if the wrong breeding
choices are made?
22
Relationship to Computers
These questions mirror real ones about
protocols.
Planet “failure” is like protocol
“termination”
We want protocols to terminate
 “Must the planet fail” is like asking whether
a protocol, in its current state, is
guaranteed to terminate.
23
Strange Planet – Transitions
An a-event occurs when individuals of
species b and c breed and are replaced
by two a’s.
Analogously: b-events and c-events.
Represent these by symbols a, b, and
c, respectively.
24
Strange Planet with 2 Individuals
200
020
002
a
b
c
011
101
110
all states are “must-fail”
all possible events lead to states where have all same species
analogous to “termination” states automata
25
“Meta-states” are populations of
different size
200
020
002
a
b
c
011
101
110
This analysis of a “meta-state” consist of “exhaustive
enumeration”.
List all possible species distributions.
For each distribution, list all possible transitions.
26
Strange Planet with 3 Individuals
111
a
b
300
“must-fail” state
c
030
003
a
210
a
102
c
021
b
201
120
b
012
c
“can’t-fail” states are parts of cycles.
27
Strange Planet with 4 Individuals
a
211
a
c
022
121
b
103
b
400
130
c
b
b
a
c
310
c
202
040
013
a
112
c
b
c
a
031
a
004
301
b
220
“might-fail” states appear
Some breeding choices lead to termination
Others lead to a cycle
28
Taking Advantage of Symmetry
In exhaustive enumeration of metastates, we see repeated patterns.
Nodes and edges have different labels
but topology is same.
200
020
002
a
b
c
011
101
110
29
Taking Advantage of Symmetry 2
Outcome (termination) is the same
regardless of which species is extinct and
which 2 have a single individual.
200
020
002
a
b
c
011
101
110
30
Taking Advantage of Symmetry 3
Develop a “prototype” that illustrates states and
transitions allowed for populations of various size
List species-specific populations in decreasing
order
Replace specific type of transition (a, b, c) with x
prototype
200
020
002
a
b
c
x
011
101
110
200
110
31
Prototypes for 2, 3, 4
110
111
211
x
x
x
200
300
x
x
400
310
x
210
220
x
The “might-fail” state of prototype 4 is a nondeterminism.
Different transitions are possible from 211 on the same input.
32
Prototype for meta-state 5
Drop the unnecessary edge label “x”
Prototype has standard form of a directed graph
Basic graph algorithms can find cycles
500
410
221
320
311
1 must-fail state; 1 can’t fail cycle; 1 might fail state
33
Strange planet example illustrates
3 types of states you could find in finite
automata: must terminate, might
terminate and can’t terminate
Methods of analysis to find those states:
exhaustive enumeration and graph
theory
34
Download