Finite Automata and Non Determinism

advertisement
Finite Automata and
Non Determinism
http://cis.k.hosei.ac.jp/~yukita/
Definition 1.1: Finite Automaton
A finite automaton is a 5 - tupple (Q, ,  , q0 , F ), where
1. Q is a finite set called the states,
2.  is a finite set called the alphabet ,
3.  : Q    Q is the transition function,
4. q0  Q is the start state, and
5. F  Q is the set of accept states.
2
State Diagram for M1
1
0
q1
1
0
q2
q3
0, 1
3
Data Representation for M1
1. Q  {q1 , q2 , q3}
2.   {0,1}
3.  is described as
q1
0
q1
1
q2
q2
q3
q3
q2
q2
q2
,
4. q1 is the start state, and
5. F  {q2 }.
4
Task 01
DFA
1. Implement M1 with your favorite
programming language.
2. GUI
•
•
Two buttons for input 0 and 1
State chart with the current state highlighted
5
Language of M1
The language of machine M , written as L( M ),
is the set of all strings that machine M accepts.
We can say that M recognizes A.
L( M 1 )  {w | w contains at least one 1 and
an even number of 0s follow the last 1}
6
State Diagram for M5
0
q1
1
0, <RESET>
0
1
2
q0
2
q2
1, <RESET>
7
Data Representation for M5
1. Q  {q0 , q1 , q2 }
2.   { RESET ,0,1,2}
3.  is described as
q0
q1
q2
R
q0
q0
q0
0
q0
q1
q2
1
q1
q2
q0
2
q2
q0
q1
,
4. q0 is the start state, and
5. F  {q0 }.
8
Informal Description of M5
• M5 keeps a running count of the sum of
the numerical symbols it reads, modulo 3.
• Every time it receives the <RESET>
symbol it resets the count to 0.
• M5 accepts if the sum is 0, modulo 3.
9
Definition 1.7: Regular Language
• A language is called a regular language if
some finite automaton recognizes it.
10
Example 1.9: A finite automaton E2
• E2 recognizes the regular language of all
strings that contain the string 001 as a
substring.
• 0010, 1001, 001, and 1111110011110 are
all accepted,
• but 11 and 0000 are not.
11
Find a set of states of E2
You
1. haven’t just seen any symbols of the
pattern,
2. have just seen a 0,
3. have just seen 00 or,
4. have just seen the entire pattern 001.
Assign the states q,q0,q00, and q001 to these
possibilities.
12
Draw a State Diagram for E2
1
0, 1
0
0
q
q0
0
q00
1
q001
1
13
Regular Operations on Languages
Let A and B be languages.
Union : A  B  {x | x  A or x  B}.
Concatenat ion : A  B  {xy | x  A and y  B}.
Star : A  {x1 x2  xk | k  0 and each xi  A}.
*
The first two operations are binary operations ,
and the last one a unary operation.
14
Example 1.11
Let the alphabet  be {a, b,, z}.
Let A  {good, bad} and B  {boy, girl }. Then, we have
A  B  {good, bad, boy, girl },
A  B  {goodboy, goodgirl, badboy, badgirl }, and
A*  { , good, bad, goodgood, goodbad, badbad,
goodgoodgo od, good goodbad, goodbadgoo d,
goodbadbad , }.
15
Theorem 1.12 Closedness for Union
The class of regular languages is closed under
the union operation.
In other word s, if A1 and A2 are regular languages,
so is A1  A2 .
16
Proof of Theorem 1.12
Let M 1 recognize A1 , where M 1  (Q1 , , 1 , q1 , F1 ),
and M 2 recognize A2 , where M 2  (Q2 , ,  2 , q2 , F2 ).
Construct M to recognize A1  A2 ,
where M  (Q, ,  , q, F ).
Then, check the correctnes s of the constructi on.
Proof of Th 1.12
17
Construction of M
1. Q  Q1  Q2  {( r1 , r2 ) | r1  Q1 and r2  Q2 }.
2. We can assume that M 1 and M 2 have the same
alphabet . (Why?)
3. (r1 , r2 )  Q, a  ;  (( r1 , r2 ), a)  (1 (r1 , a ),  2 (r2 , a ))
4. q0  (q1 , q2 ).
5. F  ( F1  Q2 )  (Q1  F2 )  {( r1 , r2 ) | r1  F1 or r2  F2 }.
Proof of Th 1.12
18
Correctness
You should check the following.
1. For any string recognized by M1 is
recognized by M.
2. For any string recognized by M2 is
recognized by M.
3. For any string recognized by M is
recognized by M1 or M2.
Proof of Th 1.12
19
Theorem 1.13 Closedness for
concatenation
The class of regular languages is closed under concatenat ion
operation. In other word s, if A1 and A2 are regular languages,
so is A1  A2 .
20
Nondeterminism
• To prove Theorem 1.13, we need
nondeterminism.
• Nondeterminism is a generalization of
determinism. So, every deterministic
automaton is automatically a
nondeterministic automaton.
21
Nondetermistic Finite Automata
• A nondeterministic finite automaton can be
different from a deterministic one in that
– for any input symbol, nondeterministic one
can transit to more than one states.
– epsilon transition
• NFA and DFA stand for nondeterministic
finite automaton and deterministic finite
automaton, respectively.
22
NFA N1
0,1
0,1
q1
1
q2
0,
1
q3
q4
23
Parallel world and NFA
...
...
accept
24
Example 1.14
NFA N2
0,1
q1
1
q2
0,1
q3
0,1
q4
Let language A consist of all strings over {0,1} containing a 1 in the third
position from the end. N2 recognizes A.
25
A DFA equivalent to N2
0
q000
0
0
1
q100
0
q010
0
q110
1
1
0
0
1
0
1
q001
q101
1
1
q011
1
q111
26
Example 1.15
NFA N3
0
0


0
0
0
Let language A consist of all strings 0k , where k is a multiple of 2 or 3. N3
recognizes A.
27
A DFA equivalent to N3
0, 1
q-1
1
1
1
1
q0
0
q1
0
1
0
q2
1
q3
0
q4
0
q5
0
28
Example 1.16 NFA N4
q1
a
b

a
q2
a,b
q3
N4 accepts , a, baba, and baa. N4 does not accept b, nor babba.
29
Definition 1.17: NFA
A nondetermi nistic finite automaton is a 5 - tupple (Q, ,  , q0 , F ), where
1. Q is a finite set called the states,
2.  is a finite set called the alphabet ,
3.  : Q    2Q is the transition function,
4. q0  Q is the start state, and
5. F  Q is the set of accept states.
30
Example 1.18 NFA N1
0,1
1. Q  {q1 , q2 , q3 , q4 },
2.   {0,1}
q1
3.  is given as q2
q3
q4
0,1
1
q1
0,
q2
1
q3
q4
0
1

{q1} {q1 , q2 } 
{q3 }

{q3}

{q4 }

{q4 } {q4 }

4. q1 is the start state.
5. F  {q4 }.
31
In what situation is Non
Determinism relevant?
• Von Neumann machines are deterministic.
• However, there are many cases where
machine specification is all we need.
32
Theorem 1.19
• Every nondeterministic finite automaton
has an equivalent deterministic finite
automaton.
– Def. The two machines are equivalent is they
recognize the same language.
33
Proof of Th. 1.19
Let N  (Q, ,  , q0 , F ) be the NFA recognizin g some language A.
Let us assume first that N has no  arrows.
Construct M  (Q, ,  , q0 , F ) such that
1. Q  2Q ,
2.  ( R, a )    (r , a ),
rR
3. q0  {q0 }, and
4. F   {R  Q | R  F  }.
Proof of Th 1.19
34
Incorporate  arrows
E ( R )  {q | q can be reached from R by traveling along zero or
more  arrows.}
We modify   as follows.
 ( R, a )  {q  Q | q  E ( (r , a )) for some r  R}.
We modify q0 as follows.
q0  E ({q0 }).
We omit the correctness proof.
Proof of Th 1.19
35
Corollary 1.20
• A language is regular if and only if some
nondeterministic finite automaton
recognizes it.
36
Example 1.21 NFA N4 to DFA
1
b
a
2

a,b
a
3
Given N 4  {{1,2,3},{a, b},  ,1, {3}}, we want to construct
an equivalent DFA D. The D' s state set may be takes as
2{1, 2,3}  {, {1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}}.
37
Task 02
Parallel World
1. Write a program that simulates N4.
2. GUI
– Three buttons for input 0, 1, and epsion.
– State chart that reflect the branching of the
world.
38
Start and Accept states
The start state is E ({1})  {1,3}.
The set of accept states is {{3},{1,3},{2,3},{1,2,3}}.
The state diagram is given as follows.
39
The state diagram of D
a,b
a
f
b
{1}
{2}
{1,2}
a,b
b
b
b
a
a
a
{3}
a
a
{1,3}
{2,3}
{1,2,3}
b
b
40
Theorem 1.22 The class of regular languages is
closed under the union operation.
N
N1


N2
41
Proof of Th. 1.22
Let N1  (Q1 , , 1 , q1 , F1 ) recognize A1 , and
N 2  (Q2 , ,  2 , q2 , F2 ) recognize A2 .
Construct N  {Q, ,  , q0 , F ) to recognize A1  A2 .
1. Q  {q0 }  Q1  Q2
2. The state q0 is the start state of N.
3. F  F1  F2
1 (q, a)
 (q, a)
 2
4.  (q, a)  
{q1 , q2 }

for q  Q1
for q  Q2
for q  q0 and a  
for q  q0 and a   .
42
Theorem 1.23 The class of regular languages is
closed under the concatenation operation.
N2
N1

N

43
Proof of Th. 1.23
Let N1  (Q1 , , 1 , q1 , F1 ) recognize A1 , and
N 2  (Q2 , ,  2 , q2 , F2 ) recognize A2 .
Construct N  {Q, ,  , q1 , F2 ) to recognize A1  A2 .
1. Q  Q1  Q2
2. The start state q1 is the same as that of N1.
3. The set of accept states is the same as that of N 2
1 (q, a )
 (q, a )
 1
4.  (q, a )  
1 (q, a )  {q2 }
 2 (q, a )
for q  Q1 and q  F1
for q  F1 and a  
for q  F1 and a  
for q  Q2 .
44
Theorem 1.24 The class of regular languages is
closed under the star operation.
N
N1



45
Proof of Th. 1.24
Let N1  (Q1 , , 1 , q1 , F1 ) recognize A1.
Construct N  {Q, ,  , q0 , F ) to recognize A .
*
1
1. Q  {q0 }  Q1
2. The start state q0 is the new start state.
3. F  {q0 }  F1
1 (q, a)
 (q, a)
 1
4.  (q, a)  1 (q, a)  {q1}
{q }
 1

for q  Q1 and q  F1
for q  F1 and a  
for q  F1 and a  
for q  q0 and a  
for q  q0 and a   .
46
Download