Introduction, DFA and NFA
Introduction, DFA and NFA
Introduction
Introduction to Languages
The syntax of a sentence concerns its form while the
semantics concerns
its meaning.
04/18/24 2
Automata and Complexity Theory:
prepared by Kolana Korigne, CS, CCI,
04/18/24 WKU, Wolkite, Ethiopia 3
04/18/24 Automata and Complexity Theory: 4
04/18/24 5
04/18/24 Automata and Complexity Theory: 6
04/18/24 Automata and Complexity Theory: 7
04/18/24 Automata and Complexity Theory: 8
04/18/24 Automata and Complexity Theory: 9
04/18/24 Automata and Complexity Theory: 10
04/18/24 Automata and Complexity Theory: 11
04/18/24 Automata and Complexity Theory: 12
Descriptive definition of language:
The language is defined, describing the conditions imposed on its
words.
Example 1:
The language L of strings of odd length, defined over
Σ={a}, can be written as
L={a, aaa, aaaaa,…..}
Example 2:
The language L of strings that does not start with a,
defined over Σ={a,b,c}, can be written as
L={b, c, ba, bb, bc, ca, cb, cc, …}
Example 4:
The language L of strings ending in 0, defined over Σ
={0,1}, can be written as
L={0,00,10,000,010,100,110,…}
Example 8:
The language EVEN, of strings defined over
Σ={-,0,1,2,3,4,5,6,7,8,9}, can be written as EVEN =
{ …,-4,-2,0,2,4,…}
Example 10:
The language {anbn an}, of strings defined over Σ={a,b}, as
{anbn an : n=1,2,3,…}, can be written as
{aba, aabbaa, aaabbbaaa,aaaabbbbaaaa,…}
Example 12:
The language FACTORIAL, of strings defined over Σ={a},
as {an! : n=1,2,3,…}, can be written as {a,aa,aaaaaa,…}.
It is to be noted that the language FACTORIAL can be defined
over any single letter alphabet.
Example 14:
The language SQUARE, of strings defined over Σ={a}, as
{an2 : n=1,2,3,…}, can be written as
{a, aaaa, aaaaaaaaa,…}
Example 16:
The language PRIME, of strings defined over Σ={a}, as
Input Tape
String
Output
“Accept”
Finite
or
Automaton
“Reject”
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
initial accepting
state state
transition
state
2012 E.C. Formal Language and Automata 34
Alphabet {a , b }
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Initial state
2012 E.C. Formal Language and Automata 36
Scanning the Input
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4 accept
a b a
Input String
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
a b a
a, b
reject
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Tape is empty
( )
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
reject
2012 E.C. Formal Language and Automata 45
This automaton accepts only one string
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
To reject a string:
all the input string is scanned
and the last state is non-accepting
L , ab , abba
a, b
q5
b a a a, b
b
q0 a q1 b q2 b q3 a q4
Accept Accept Accept
state state state
2012 E.C. Formal Language and Automata 48
Empty Tape
( )
Input Finished a, b
q5
b a a a, b
b
q0 a q1 b q2 b q3 a q4
accept
2012 E.C. Formal Language and Automata 49
Another Example
a a, b
q0 b q1 a, b q2
q0 b q1 a, b q2
a a, b
q0 b q1 a, b q2
a a, b
q0 b q1 a, b q2
a a b
a a, b
accept
q0 b q1 a, b q2
b a b
Input String
a a, b
q0 b q1 a, b q2
a a, b
q0 b q1 a, b q2
a a, b
q0 b q1 a, b q2
b a b
a a, b
q0 b q1 a, b q2
reject
a a, b
q0 b q1 a, b q2
q0 q1
1
Language Accepted:
EVEN {x : x and x is even}
*
M Q, , , q0 , F
Q : set of states
: input alphabet
: transition function
q0 : initial state
F : set of accepting states
2012 E.C. Formal Language and Automata 61
Set of States Q
Example
Q q0 , q1, q2 , q3 , q4 , q5
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Example
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Example
F q4 a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
(q , x ) q
q x q
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
q2 q5 q3
a, b
q3 q4 q5
q4 q5 q5 q5
q5 q5 q5 a, b
b a a b
q0 a q1 b q2 b q3 a q4
2012 E.C. Formal Language and Automata 69
Extended Transition Function
:Q Q
* *
(q ,w ) q
*
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
2012 E.C. Formal Language and Automata 71
q0 , abbbaa q5
*
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
q , q
*
w 1 2 k
1 2 k
q q
states may be repeated
q w q
2012 E.C. Formal Language and Automata 75
Language Accepted by DFA
q0 w q q F
L M w : q0 ,w F
* *
q0 w q q F
q0 q0
L (M ) { } L (M ) *
a, b
q0 a, b q0
L (M ) { }
Language of the empty string
2012 E.C. Formal Language and Automata 80
{a , b }
q0 a q1 b q2
b a accept
q3 a, b
0,1
1 0
1
0 0 00 1 001
0
1 0 0,1
1
0 1
0 00 001
0
2012 E.C. Formal Language and Automata 83
L(M ) awa : w a , b *
a
b
b
q0 a q2 q3
b a
q4
a, b
2012 E.C. Formal Language and Automata 84
Regular Languages
Definition:
A language L is regular if there is
a DFA M that accepts it ( L(M ) L )
{ } { } {a , b } *
Alphabet = {a}
q1 a q2
a
q0
a
q3
Two choices q1 a q2
a
q0
a
q3
a a
q1 a q2
a
q0
a
q3
a a
q1 a q2
a
q0
a
q3
a a
All input is consumed
q1 a q2 “accept”
a
q0
a
q3
a a
q1 a q2
a
q0
a
q3
a a
Input cannot be consumed
q1 a q2
a
q0 Automaton Halts
a
q3 “reject”
“accept”
q1 a q2 q1 a q2
a a
q0
a
q0
a
q3 q3 “reject”
because this
this computation
computation
is ignored
accepts aa
2012 E.C. Formal Language and Automata 98
Rejection example
q1 a q2
a
q0
a
q3
a
“reject”
q1 a q2
a
q0
a
q3
q1 a q2
a
q0
a
q3
q1 a q2
a
q0
a
q3 “reject”
a a a
q1 a q2
a
q0
a
q3
a a a
q1 a q2
a
q0
a
q3
a a a
Input cannot be consumed
q1 a q2 “reject”
a
q0
a
Automaton halts
q3
a a a
q1 a q2
a
q0
a
q3
a a a
Input cannot be consumed
q1 a q2
a
q0 Automaton halts
a
q3 “reject”
OR
• The input cannot be consumed
2012 E.C. Formal Language and Automata 108
a is rejected by the NFA:
“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3
“reject” q3
“reject”
q1 a q2 q1 a q2
a a
q0
a
q0
a
q3 q3 “reject”
q1 a q2
a
q0
a
q3
q0 a q1 q2 a q3
q0 a q1 q2 a q3
q0 a q1 q2 a q3
a a
q0 a q1 q2 a q3
a a
“accept”
q0 a q1 q2 a q3
String aa is accepted
2012 E.C. Formal Language and Automata 116
Rejection Example
a a a
q0 a q1 q2 a q3
q0 a q1 q2 a q3
a a a
q0 a q1 q2 a q3
a a a
Automaton halts
“reject”
q0 a q1 q2 a q3
q0 a q1 q2 a q3
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 122
a b
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 123
a b
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 124
a b
“accept”
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 125
Another String
a b a b
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 126
a b a b
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 127
a b a b
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 128
a b a b
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 129
a b a b
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 130
a b a b
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 131
a b a b
“accept”
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 132
Language accepted
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 133
Another NFA Example
0
q0 q1 0, 1 q2
1
2012 E.C. Formal Language and Automata 134
Language accepted
•Simple automata:
M1 M2
q0 q0
NFA M1 DFA M2 a
q2
q0 a q1
a
q0 a q1
L( M1 ) = {a} L( M 2 ) = {a}
2012 E.C. Formal Language and Automata 137
Formal Definition of NFAs
M Q, , , q0 , F
: Transition function
q0 : Initial state
F : Accepting states
2012 E.C. Formal Language and Automata 138
Transition Function
q , x q1 , q2 , , qk
q1
x resulting states with
q x
q2 following one transition
x
with symbol x
qk
2012 E.C. Formal Language and Automata 139
q0 , 1 q1
0
q0 q1 0, 1 q
2
1
2012 E.C. Formal Language and Automata 140
(q1,0) {q0 , q2 }
0
q0 q1 0, 1 q
2
1
2012 E.C. Formal Language and Automata 141
(q0 , ) {q2 }
0
q0 q1 0, 1 q
2
1
2012 E.C. Formal Language and Automata 142
(q2 ,1)
0
q0 q1 0, 1 q
2
1
2012 E.C. Formal Language and Automata 143
*
Extended Transition Function
Same with but applied on strings
q0 , a q1
*
q4 q5
a a
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 144
q0 , aa q4 , q5
*
q4 q5
a a
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 145
q0 , ab q2 , q3 , q0
*
q4 q5
a a
q0 a q1 b q2 q3
2012 E.C. Formal Language and Automata 146
Special case:
q q ,
*
with label w
qi w qj
w 1 2 k
1 2 k
qi qj
qi
wm
q0 w
qk qk F
m
wm qj
q0 , aa q4 , q5
*
aa L(M )
F
2012 E.C. Formal Language and Automata 151
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
q0 , ab q2 , q3 , q0
*
ab LM
F
2012 E.C. Formal Language and Automata 152
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
q0 , abaa q4 , q5
*
abaa L(M )
F
2012 E.C. Formal Language and Automata 153
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
Definition:
if LM1 LM 2
NFA M1
0
LM1 {10} *
q0 q1
1
DFA M2 0,1
0
LM 2 {10} *
q0 q1 1 q2
1
0
2012 E.C. Formal Language and Automata 158
Theorem:
Languages
Regular
accepted
Languages
by NFAs
Languages
accepted
by DFAs
Languages
accepted Regular
Languages
by NFAs
AND
Languages
accepted Regular
Languages
by NFAs
Languages
accepted Regular
Languages
by NFAs
Languages
accepted Regular
Languages
by NFAs
Any NFA can be converted to an
equivalent DFA
DFA M
q0
DFA M
q0 a
q1, q2
M a
NFA
q0 a q1 q2
b
DFA M
q0 a
q1, q2
b
trap state
2012 E.C. Formal Language and Automata 165
(q1 , a ) {q1 , q2 }
*
M a * (q2 , a )
NFA
q0 a q1 q2 union
b q1, q2
a
DFA M
q0 a
q1, q2
b
2012 E.C. Formal Language and Automata 166
(q1 , b ) {q0 }
*
M a * (q2 , b ) {q0 }
NFA
a union
q0 q1 q2
b q0
b a
DFA M
q0 a
q1, q2
b
2012 E.C. Formal Language and Automata 167
M a
NFA
q0 a q1 q2
b
b a
DFA M
q0 a
q1, q2
b
a, b trap state
2012 E.C. Formal Language and Automata 168
END OF CONSTRUCTION
M a
NFA
q0 a q1 q2 q1 F
b
a
DFA M b
q0 a
q1, q2
q1, q2 F
b
a, b
2012 E.C. Formal Language and Automata 169
General Conversion Procedure
Input: an NFA M
step
1. Initial state of NFA: q0
DFA M
q0
q0 , a q1, q2
DFA M
q0 a
q1, q2
b a
DFA M
q0 a
q1, q2
b
a, b
2012 E.C. Formal Language and Automata 177
step
4. For any DFA state {qi , q j ,..., qm }
q0 a
q1, q2
q1, q2 F
b
a, b
2012 E.C. Formal Language and Automata 179
Lemma:
If we convert NFA M to DFA M
then the two automata are equivalent:
LM LM
Proof:
We only need to show: LM LM
AND
LM LM
2012 E.C. Formal Language and Automata 180
First we show: LM LM
w L(M ) w L(M )
q0 w qf
symbols
w 1 2 k
1 2 k
q0 qf
symbol
i
qi qj
w 1 2 k
1 2 k
NFA M: q0 qf
then
1 2 k
DFA M:
{q0 } {q f ,}
state
w L(M ) state
label label
2012 E.C. Formal Language and Automata 184
More generally, we will show that if in M:
(arbitrary string) v a1a2 an
a1 a2 an
NFA M: q0 qi qj ql qm
then
a1 a2 an
DFA M:
{q0 } {qi ,} {q j ,} {ql ,} {qm ,}
Induction Basis: |v | 1 v a1
a1
NFA M: q0 qi
a1
DFA M:
{q0 } {qi ,}
is true by construction of M
2012 E.C. Formal Language and Automata 186
Induction hypothesis: 1 | v | k
v a1a2 ak
a1 a2 ak
DFA M:
{q0 } {qi ,} {q j ,} {qc ,} {qd ,}
v
a1 a2 ak ak 1
DFA M:
{q0 } {qi ,} {q j ,} {qc ,} {qd ,} {qe ,}
2012 E.C.
v
Formal Language and Automata 188
Therefore if w L(M )
w 1 2 k
1 2 k
NFA M: q0 qf
then
1 2 k
DFA M:
{q0 } {q f ,}
w L(M )
2012 E.C. Formal Language and Automata 189
We have shown: LM LM
Therefore: L M L M