Languages and Finite Automata

Non Deterministic Automata
1
Nondeterministic Finite Accepter (NFA)
Alphabet = {a}
a
q0
q1
a
q2
a
q3
2
Nondeterministic Finite Accepter (NFA)
Alphabet = {a}
Two choices
a
q0
q1
a
q2
a
q3
3
Nondeterministic Finite Accepter (NFA)
Alphabet = {a}
Two choices
a
q0
q1
a
q2 No transition
a
q3 No transition
4
First Choice
a a
a
q0
q1
a
q2
a
q3
5
First Choice
a a
a
q0
q1
a
q2
a
q3
6
First Choice
a a
a
q0
q1
a
q2
a
q3
7
First Choice
a a
a
q0
q1
a
q2
“accept”
a
q3
8
Second Choice
a a
a
q0
q1
a
q2
a
q3
9
Second Choice
a a
a
q0
q1
a
q2
a
q3
10
Second Choice
a a
a
q0
q1
a
q3
a
q2
No transition:
the automaton hangs
11
Second Choice
a a
a
q0
q1
a
q2
a
q3
“reject”
12
Observation
An NFA accepts a string
if
there is a computation of the NFA
that accepts the string
13
Example
aa
is accepted by the NFA:
a
q0
q1
a
q2
a
q3
14
Lambda Transitions
q0
a
q1 
q1
a
q3
15
a a
q0
a
q1 
q2
a
q3
16
a a
q0
a
q1 
q2
a
q3
17
(read head doesn’t move)
a a
q0
a
q1 
q2
a
q3
18
a a
q0
a
q1 
q2
a
q3
19
a a
“accept”
q0
String
a
q1 
q2
a
q3
aa is accepted
20
Language accepted:
q0
a
q1 
L  {aa}
q2
a
q3
21
Another NFA Example
q0
a
b
q1
q2

q3

22
a b
q0
a
b
q1
q2

q3

23
a b
q0
a
b
q1
q2

q3

24
a b
q0
a
b
q1
q2

q3

25
a b
“accept”
q0
a
b
q1
q2

q3

26
Another String
a b a b
q0
a
b
q1
q2

q3

27
a b a b
q0
a
b
q1
q2

q3

28
a b a b
q0
a
b
q1
q2

q3

29
a b a b
q0
a
b
q1
q2

q3

30
a b a b
q0
a
b
q1
q2

q3

31
a b a b
q0
a
b
q1
q2

q3

32
a b a b
q0
a
b
q1
q2

q3

33
a b a b
“accept”
q0
a
b
q1
q2

q3

34
Language accepted
L  ab, abab, ababab, ...

 ab
q0
a
b
q1
q2

q3

35
Another NFA Example
0
q0
1
q1
0, 1 q2

36
Language accepted
L   , 10, 1010, 101010, ...
 10*
0
q0
1
q1
0, 1 q2

37
Formal Definition of NFAs
M  Q, ,  , q0 , F 
Q:
Set of states, i.e.
q0 , q1, q2 
:
Input aplhabet, i.e.
a, b
:
Transition function
q0 : Initial state
F:
Final states
38
Transition Function 
 q0 , 1  q1
0
q0
1
q1
0, 1 q
2

39
 (q1,0)  {q0 , q2 }
0
q0
1
q1
0, 1 q
2

40
 (q0 ,  )  {q0 , q2}
0
q0
1
q1
0, 1 q
2

41
 (q2 ,1)  
0
q0
1
q1
0, 1 q
2

42
Extended Transition Function  *
 * q0 , a   q1
q5
q4
a
q0
a
a
b
q1

q2

q3
43
 * q0 , aa   q4 , q5 
q5
q4
a
q0
a
a
b
q1

q2

q3
44
 * q0 , ab   q2 , q3 , q0 
q5
q4
a
q0
a
a
b
q1

q2

q3
45
Formally
It holds
q j   * qi , w
if and only if
there is a walk from
with label w
qi to q j
46
The Language of an NFA M
F  q0 ,q5 
q5
q4
a
q0
a
a
b
q1
q2

q3

 * q0 , aa   q4 , q5
aa  L(M )
47
F  q0 ,q5 
q5
q4
a
q0
a
a
b
q1
q2

q3

 * q0 , ab   q2 , q3 , q0 
ab LM 
48
F  q0 ,q5 
q5
q4
a
q0
a
a
b
q1
q2

q3

 * q0 , abaa   q4 , q5
aaba L(M )
49
F  q0 ,q5 
q5
q4
a
q0
a
a
b
q1
q2

q3

 * q0 , aba   q1
aba  LM 
50
q5
q4
a
q0
a
a
b
q1

q2
q3


LM   aa  ab*  ab aa
51
Formally
The language accepted by NFA
M is:
LM   w1, w2 , w3 ,...
where
 * (q0 , wm )  {qi , q j ,...}
and there is some
qk  F
(final state)
52
w LM 
 * (q0 , w)
qi
w
q0
qk
w
w
qk  F
qj
53
Equivalence of NFAs and DFAs
54
Equivalence of Machines
For DFAs or NFAs:
Machine
if
M1 is equivalent to machine M 2
L M1   L  M 2 
55
Example
LM1   {10} *
NFA
0
q0
1
q1
0, 1
q2

DFA
LM 2   {10} *
M1
M2
0,1
1
q2
0
q0
1
q1
0
56
Since
LM1   LM 2   10*
machines
NFA
M1 and M 2 are equivalent
0
M1
q0
1
q1
0, 1
q2

0,1
DFA
0
M2
q0
1
q1
0
1
q2
57
Equivalence of NFAs and DFAs
Question:
NFAs
=
DFAs ?
Same power?
Accept the same languages?
58
Equivalence of NFAs and DFAs
Question:
NFAs
=
DFAs ?
YES!
Same power?
Accept the same languages?
59
We will prove:
Languages
accepted
by NFAs

Languages
accepted
by DFAs
60
We will prove:
Languages
accepted
by NFAs

Languages
accepted
by DFAs
NFAs and DFAs have the same
computation power
61
Step 1
Languages
accepted
by NFAs

Languages
accepted
by DFAs
62
Step 1
Languages
accepted
by NFAs
Proof:

Languages
accepted
by DFAs
Every DFA is also an NFA
63
Step 1
Languages
accepted
by NFAs
Proof:

Languages
accepted
by DFAs
Every DFA is also an NFA
A language accepted by a DFA
is also accepted by an NFA
64
Step 2
Languages
accepted
by NFAs

Languages
accepted
by DFAs
65
Step 2
Languages
accepted
by NFAs
Proof:

Languages
accepted
by DFAs
Any NFA can be converted to an
equivalent DFA
66
Step 2
Languages
accepted
by NFAs
Proof:

Languages
accepted
by DFAs
Any NFA can be converted to an
equivalent DFA
A language accepted by an NFA
is also accepted by a DFA
67
NFA
q0
NFA to DFA
a
a
 q2
q1
b
DFA
q0 
68
NFA
q0
NFA to DFA
a
a
 q2
q1
b
DFA
q0 
a
q1,q2 
69
NFA
q0
NFA to DFA
a
a
 q2
q1
b
DFA
q0 
a
q1,q2 
b

70
NFA
q0
NFA to DFA
a
a
 q2
q1
b
a
DFA
q0 
a
q1,q2 
b

71
NFA
q0
NFA to DFA
a
a
 q2
q1
b
a
b
DFA
q0 
a
q1,q2 
b

72
NFA
q0
NFA to DFA
a
a
 q2
q1
b
a
b
DFA
q0 
a
q1,q2 
b

a, b
73
NFA
q0
NFA to DFA
a
a
 q2
q1
b
a
b
DFA
q0 
a
q1,q2 
b

a, b
74
NFA to DFA: Remarks
We are given an NFA
M
We want to convert it
to an equivalent DFA M 
With
LM   L(M )
75
If the NFA has states
q0 , q1, q2 ,...
the DFA has states in the powerset
, q0 , q1, q1, q2 , q3 , q4 , q7 ,....
76
Procedure NFA to DFA
1.
Initial state of NFA:
q0
Initial state of DFA:
q0 
77
Example
a
NFA
q0
a

q1
q2
b
DFA
q0 
78
Procedure NFA to DFA
2. For every DFA’s state {qi , q j ,..., qm }
Compute in the NFA
 * qi , a ,
 * q j , a ,
}
 {qi , qj ,..., qm
...
Add transition
}
 {qi , q j ,..., qm }, a   {qi , qj ,..., qm
79
Exampe
a
NFA
q0
a
q1

q2
b
 * (q0 , a)  {q1, q2}
DFA
q0 
a
q1,q2 
 q0 , a   q1, q2 
80
Procedure NFA to DFA
Repeat Step 2 for all letters in alphabet,
until
no more transitions can be added.
81
Example
a
NFA
q0
a

q1
q2
b
a
b
DFA
q0 
a
q1,q2 
b

a, b
82
Procedure NFA to DFA
3. For any DFA state {qi , q j ,..., qm }
If some
q j is a final state in the NFA
Then, {qi , q j ,..., qm }
is a final state in the DFA
83
Example
a
NFA
q0
a

q1
q2
q1  F
b
a
b
DFA
q0 
a
q1,q2 
q1, q2  F 
b

a, b
84
Take NFA
Theorem
M
Apply procedure to obtain DFA
Then
M
and
M
M
are equivalent :
LM   LM 
85
Finally
We have proven
Languages
accepted
by NFAs

Languages
accepted
by DFAs
86
We have proven
Languages
accepted
by NFAs

Languages
accepted
by DFAs
Regular Languages
87
We have proven
Languages
accepted
by NFAs
Regular Languages

Languages
accepted
by DFAs
Regular Languages
88