q - Ankara Üniversitesi Bilgisayar Mühendisliği Bölümü

advertisement
FINITE AUTOMATA
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
1
Protocol for e-commerce using e-money
Allowed events:
P The customer can pay the store (=send the moneyFile to the store)
C The customer can cancel the money (like putting a
stop on a check)
S The store can ship the goods to the customer
R The store can redeem the money (=cash the check)
T The bank can transfer the money to the store
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
2
The protocol for each participant
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
3
Some actions
• Customer may try to copy the money file, use it to pay
several times, or both pay and cancel the money, thus
getting the goods “for free”.
• Store should ship the goods but should be careful as
well. It should not ship goods until it is sure it has been
given valid money for the goods.
• The bank must behave responsibly, or it cannot be a
bank. It must not allow money to be both cancelled
and redeemed.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
4
Completed Protocols
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
5
State (2,c) is accessible and corresponds
to a situation where the bank received a cancel
message before redeem message where as
store shipped the good.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
6
Deterministic Finite Automata (DFA)
Formal Definition of Finite Automaton
1. Finite set of states, typically Q.
2. Alphabet of input symbols, typically Σ.
3. One state is the start/initial state, typically q0 ∈ Q.
4. Zero or more final/accepting states; the set is typically F⊆Q.
5. δ is a transition function (q; a) → p. This function:
–
–
–
–
Takes a state and input symbol as arguments.
Returns a state.
One “rule" of δ would be written δ(q; a) = p, where q and p are states,
and a is an input symbol.
Intuitively: if the DFA is in state q, and input a is received, then the DFA
goes to state p (note: q = p OK).
A DFA is represented as the 5-tuple: A = (Q; Σ ; δ ; q0; F).
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
7
DFA Example
An automaton A that accepts
L = {x01y : x; y ∈ {0; 1}}
The Automaton as a transition diagram:
The automaton A = ({q0; q1; q2}; {0; 1}; δ; q0; {q1})
Transition table for the DFA
Mart 2006
→q0
*q1
q2
0
q2
q1
q2
1
q0
q1
q1
Ankara Üniversitesi Bilgisayar Mühendisliği
8
Extending the Transition Function to Strings
An DFA accepts a string ω = a1a2…an if there
is a path in the transition diagram that
1. Begins at a start state
2. Ends at an accepting state
3. Has sequence of labels a1a2…an
Example: The following DFA accepts e.g.
the string 01101001
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
9
Extending the transition functions to strings
The transition function δ can be extended to δˆ that operates on
states and strings (as opposed to states and symbols)
Basis:
δˆ(q, ε ) = q
and
δˆ(q, a1 ) = p1
Induction:
Mart 2006
Assume:
δˆ(q, x) = p
Then
δˆ(q, xa) = δ (δˆ(q, x), a) = δ ( p, a)
Ankara Üniversitesi Bilgisayar Mühendisliği
10
DFA for language L
Now we can formally define a DFA A = (Q; Σ ; δ ; q0; F). This
language is denoted L(A), and is defined by
L( A) = {w | δˆ(q0 , w) ∈ F }
If Language L is L(A) for some DFA A, then we say L is a
regular language.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
11
Design a DFA to accept the language
DFA accepting all and only strings with an even number of 0's and
an even number of 1's
Both even
0’s even
1’s even
Both odd
*→q0
q1
q2
q3
0
q2
q3
q0
q1
1
q1
q0
q3
q2
The automaton A = ({q0, q1, q2, q3}, {0, 1}, δ, q0, {q0})
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
12
Marble-rolling toy from p. 53 of textbook
A marble is dropped at A or B. Levers x1, x2 or x3 cause the
to fall either to the left or to the right. Whenever a marble
encounters a lever, it causes the lever to reverse after the
marble passes, so the next marble will take the opposite
branch.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
13
A state is represented as sequence
of three its followed by “r” or “a”
(previous input rejected or accepted)
Mart 2006
A
B
→000r
100r
011r
*000a
100r
011r
*001a
101r
000a
010r
110r
001a
*010a
110r
001a
011r
111r
010a
100r
010r
111r
*100a
010r
111r
101r
011r
100a
*101a
011r
100a
110r
000a
101a
*110a
000a
101a
111r
001a
110a
Ankara Üniversitesi Bilgisayar Mühendisliği
14
Nondeterministic Finite Automata (NFA)
• A NFA can be in several states at once, or, viewed
another way, it can guess" which state to go to next.
This ability is expressed as an ability to “guess”
something about its input.
• Like DFA, NFA has a finite set of states, a finite set of
input symbols, one start state and a set of accepting
states. It has also transition function δ. The difference
is in the type of δ. NFA δ function returns a set of zero,
one , or more states rather than returning exactly one
state, as the DFA must.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
15
Example: An automaton that accepts all and
only strings ending in 01
Here is what happens when the NFA processes the
input 00101
Input is not finished
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
16
Definition of Nondeterministic Finite Automata
1. Finite set of states, typically Q.
2. Alphabet of input symbols, typically Σ.
3. One state is the start/initial state, typically q0 ∈ Q.
4. Zero or more final/accepting states; the set is
typically F⊆Q.
•
δ is a transition function from Q x Σ to the powerset
of Q
A NFA is represented as the 5-tuple:
A = (Q; Σ ; δ ; q0; F).
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
17
Example: An automaton that accepts all and
only strings ending in 01
The NFA can be specified fromally as
A = ({q0, q1, q2}, {0, 1}, δ, q0, {q2}).
The transition table is as follows:
→q0
q1
*q2
Mart 2006
0
{q0, q1}
Ø
Ø
1
{ q0 }
{ q2 }
Ø
Ankara Üniversitesi Bilgisayar Mühendisliği
18
The extended transition function
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
19
Now we can formally define a NFA A = (Q; Σ ; δ ; q0; F). This
language is denoted L(A), and is defined by
L( A) = {w | δˆ(q0 , w) I F ≠ 0/ }
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
20
Equivalence of DFA and NFA
• NFA’s are usually easier to program in.
• DFA in practice has about as many states as the NFA,
although it is often has more transitions. In the worst
case, however, the smallest DFA can have 2n states
while the smallest NFA for the same language has
only n states.
• Surprisingly, for any NFA N there is a DFA D, such
that L(D) = L(N), and vice versa.
• This involves the subset construction, an important
example how an automaton B can be generically
constructed from another automaton A.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
21
Equivalence of DFA and NFA
Given an NFA
N = (QN, Σ , δN , q0 ,FN)
We will construct DFA
D = (QD, Σ , δD , {q0 },FD)
Such that L(D) = L(N).
• First of all alphabets are same.
• QD is the set of subsets of QN. i.e., QD is the power set of QN. If
QN has n states then QD will have 2n states. Often not all these
states are accessible from the start state of QD.
QD = {S | S ⊆ QN}
• FD = {S ⊆ QN | S ∩ FN ≠ Ø }
• For every S ⊆ QN and for each input symbol a in Σ
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
22
Equivalence of DFA and NFA example
N = ({q0, q1, q2}, {0, 1}, δ, q0, {q2}).
Look all the states p in S, and find the union of the transitions.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
23
→q0
*q1
q2
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
0
q2
q2
q2
1
q0
q0
q1
24
Equivalence of DFA and NFA
Given an NFA
N = (QN, Σ , δN , q0 ,FN)
We will construct DFA
D = (QD, Σ , δD , {q0 },FD)
Such that L(D) = L(N).
• First of all alphabets are same.
• QD is the set of subsets of QN. i.e., QD is the power set of QN. If
QN has n states then QD will have 2n states. Often not all these
states are accessible from the start state of QD.
QD = {S | S ⊆ QN}
• FD = {S ⊆ QN | S ∩ FN ≠ Ø }
• For every S ⊆ QN and for each input symbol a in Σ
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
25
Theorem
If D = (QD, Σ , δD , {q0 },FD) is DFA constructed from
NFA N = (QN, Σ , δN , q0 ,FN) by the subset
construction, then L(D) = L(N).
Proof:
We actually prove first, by induction on |ω| that
Basis: ω = є the claim follows from the definition.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
26
Theorem cont.
Induction Hypothesis
Construction of subsets
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
27
Theorem result
We demonstrated that
Now, why it follows that L(D)=L(N)?
When we observe that both D and N accept ω if and
only if extended transition functions, respectively,
contain an accepting state FN, we have a complete
proof that L(D)=L(N).
Theorem: A language L is accepted by some DFA
if and only if L is accepted by some NFA.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
28
Exponential blow up
• It is possible that NFA N with n+1 states that has no equivalent
DFA with fewer than 2n states
• Suppose an equivalent DFA D with fewer than 2n states exists.
• D must remember the last n symbols it has read.
• There are 2n bit sequences a1a2 … an
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
29
• Since any 2n subsets of the last n symbols could have
been 1, if D has fewer than 2n states, then there would
be some state q such that D can be in state q after
reading two different sequences of n bits, say ai and
bi.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
30
Case 1: q has to be
both an accepting and
a nonaccepting state.
Case 2:
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
31
Finite Automata with ε transition
• The new feature is that we allow transition on ε, the
empty string.
• NFA is allowed to make a transition spontaneously,
without receiving an input symbol.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
32
ε transition
An ε-NFA accepting decimal numbers consisting of:
1.
2.
3.
4.
5.
Mart 2006
An optional + or - sign
A string of digits
a decimal point
another string of digits
One of the strings (2) are (4) are optional
Ankara Üniversitesi Bilgisayar Mühendisliği
33
Definition of ε-NFA
1. Finite set of states, typically Q.
2. Alphabet of input symbols, typically Σ.
3. One state is the start/initial state, typically q0 ∈ Q.
4. Zero or more final/accepting states; the set is
typically F⊆Q.
•
δ is a transition function from Q x Σ U {ε} to the
powerset of Q
A ε-NFA is represented as the 5-tuple:
A = (Q; Σ ; δ ; q0; F).
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
34
example
E = ({q0, q1, …q5}, {.,+,-,1,2,….9},δ,q0,{q5})
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
35
Enclose
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
36
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
37
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
38
Details of construction
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
39
Example
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
40
Theorem
• A language L is accepted by some ε-NFA if and only if
L is accepted by some DFA.
Mart 2006
Ankara Üniversitesi Bilgisayar Mühendisliği
41
Download