COM3064
Automata Theory
Week 3: Nondeterministic Finite
Automata
Lecturer: Dr. Sevgi YİĞİT SERT
Spring 2023
Resources: Introduction to The Theory of Computation, M. Sipser,
Introduction to Automata Theory, Languages, and Computation, J.E. Hopcroft, R. Motwani, and J.D. Ullman
BBM401 Automata Theory and Formal Languages, İlyas Çiçekli
2
Non-Deterministic Finite Automata (NFA)
• A nondeterministic finite automata (NFA) can be in several states at once.
• A NFA state can have more than one arc leaving from that state with the same
symbol.
• A NFA can allow state-to-state transitions on ε input.
• These transitions are done spontaneously, without looking at the input string.
• A NFA starts in the start state and it accepts if any sequence of choices for the string
leads to a final state.
• Nondeterminism may be viewed as a kind of parallel computation wherein multiple
independent “processes” or “threads” can be running concurrently.
• When the NFA splits to follow several choices, that corresponds to a process “forking”
into several children, each proceeding separately. If at least one of these processes
accepts, then the entire computation accepts.
3
NFA – Example
• An automata that accepts all and only strings ending in 01.
• State q0 can go to q0 or q1 with the symbol 0. (non-determinism)
• NFA accepts a string w if there is a path accepts that string.
– There can be other paths that do not accept that string.
4
NFA – Example
• What happens when the NFA processes the input 00101
0 0 1 0 1
0 0 0
1 1
• All missing arcs go to a death state, the death state goes to itself for all symbols, and
the death state is a non-accepting state.
5
First Choice
a a
q1 a q2
a
q0
a
q3
6
First Choice
a a
q1 a q2
a
q0
a
q3
7
First Choice
a a
q1 a q2
a
q0
a
q3
8
First Choice
a a
All input is consumed
q1 a q2 accept
a
q0
a
q3
9
Second Choice
a a
q1 a q2
a
q0
a
q3
10
Second Choice
a a
q1 a q2
a
q0
a
q3
11
Second Choice
a a
q1 a q2
a
q0
a
q3
12
Second Choice
a a
q1 a q2
a
q0
a
q3 No transition: the automata stucks
13
Second Choice
a a
Input cannot be consumed
q1 a q2
a
q0
a
q3 reject
14
aa is accepted by the NFA:
q1 a q2 accept q1 a q2
a a
q0 q0
a a
q3 q3 reject
because this computation accepts aa
• A NFA accepts a string if all the input is consumed and the automata
is in an accepting state
15
Rejection
q1 a q2
a
q0
a
q3
16
First Choice
q1 a q2
a
q0
a
q3
17
First Choice
a
Input finished
reject
q1 a q2
a
q0
a
q3
18
Second Choice
q1 a q2
a
q0
a
q3
19
Second Choice
a
Input finished
q1 a q2
a
q0
a
q3 reject
20
a is rejected by the NFA:
reject
q1 a q2 a
q1 q2
a a
q0 q0
a a
q3 reject q3
because all possible computations lead to rejection
21
aaa is rejected by the NFA:
q1 a q2 reject q1 a q2
a a
q0 q0
a a
q3 q3 reject
because all possible computations lead to rejection
22
Language accepted: L = {aa }
q1 a q2
a
q0
a
q3
23
NFA – Example with transitions
q0 a q1 λ q2 a q3
24
a a
q0 a q1 λ q2 a q3
25
a a
q0 a q1 λ q2 a q3
26
(read head does not move)
a a
q0 a q1 λ q2 a q3
27
a a
q0 a q1 λ q2 a q3
28
all input is consumed
a a
accept
q0 a q1 λ q2 a q3
String aa is accepted
29
Rejection
a a a
q0 a q1 λ q2 a q3
30
Rejection
a a a
q0 a q1 λ q2 a q3
31
(read head does not move)
a a a
q0 a q1 λ q2 a q3
32
a a a
q0 a q1 λ q2 a q3
No transition: the automata stucks
33
Input cannot be consumed
a a a
reject
q0 a q1 λ q2 a q3
String aaa is rejected
34
Language accepted: L = {aa }
q0 a q1 λ q2 a q3
35
Another NFA Example
0
q0 q1 0, 1 q2
1
λ
36
Another NFA Example
Language
accepted L ( M ) = {λ , 10 , 1010 , 101010 , ...}
0
q0 q1 0, 1 q2
1 (redundant
state)
λ
37
Another NFA Example
Language L ( M ) = {λ , 10 , 1010 , 101010 , ...}
accepted
= {10 }*
0
q0 q1 0, 1 q2
1 (redundant
state)
λ
38
NFAs are interesting because we can express languages easier
than DFAs
NFA
M1 DFA M2 a
q2
q0 a q1
a
q0 a q1
L ( M 1 ) = {a} L ( M 2 ) = {a}
39
NFA for Text Search
• An NFA accepting the set of words ending with ebay or web
40
Corresponding DFA for Text Search
41
Formal Definition of NFA
• A Nondeterministic Finite Automata (NFA) is a 5-tuple (Q, Σ, , q0, F)
1. Q is a finite set of states
2. Σ is a finite set of symbols (alphabet)
3. Delta ( ) is a transition function from : × Σε ε → = ⊆ }
Σ ∪ {ε} power set
4. q0 is the start state (q0 ∈ Q )
5. F is a set of final (accepting) states ( F ⊆ Q )
• Transition function takes two arguments: a state and an input symbol or .
• (q,a) = the set of the states that the NFA goes to when it is in state q and a is
received.
– where a is an input symbol or .
42
Transition Function -
δ (q0 , 1) = {q1}
0
q0 q1 0, 1 q2
1
λ
43
Transition Function -
δ ( q1 ,0 ) = {q0 , q 2 }
0
q0 q1 0, 1 q2
1
λ
44
Transition Function -
δ ( q1 ,1) = {q 2 }
0
q0 q1 0, 1 q2
1
λ
45
Transition Function -
δ ( q 0 , λ ) = {q 2 }
0
q0 q1 0, 1 q2
1
λ
46
Transition Function -
δ ( q 2 ,1) = ∅
0
q0 q1 0, 1 q2
1
λ
47
Language of a NFA
• Formally, the language accepted by a NFA is:
• The transition function delta is extended delta star that operates on states and strings
(as opposed to states and symbols).
• A string is accepted by a NFA iff the states that are reachable from the starting
state by consuming contain at least one final state.
48
∗
Extended Transition Function -
δ * (q0 , a ) = {q1}
q4 q5
a a
q0 a q1 b q2 λ q3
λ
49
∗
Extended Transition Function -
δ * (q0 , aa ) = {q 4 , q5 }
q4 q5
a a
q0 a q1 b q2 λ q3
λ
50
∗
Extended Transition Function -
δ * (q0 , ab ) = {q 2 , q3 , q0 }
q4 q5
a a
q0 a q1 b q2 λ q3
λ
51
Epsilon Closure
• We close a state by adding all states reachable by a sequence εε…ε .
• ECLOSE(q) is the epsilon closure of the state q.
52
Epsilon Closure
ECLOSE(1) = {1,2,3,4,6}
ECLOSE(2) = {2,3,6}
ECLOSE(3) = {3,6}
ECLOSE(4) = {4}
ECLOSE(5) = {5,7}
ECLOSE(6) = {6}
ECLOSE(7) = {7}
53
NFA Examples:
• Give NFA’s accepting the following languages over the alphabet {0,1}.
1. The set of all strings ending in 00.
2. The set of all strings ending in 1010.
3. The strings whose second characters from the right end are 1.
4. The strings whose third characters from the right end are 1.
54
Equivalence of DFA and NFA
• NFA's are usually easier to construct.
• For any NFA there is a DFA , such that ( ) = ( ), and vice versa.
• Given a NFA
= ( , , , !" , #)
We can construct an equivalent DFA
% %
$=( , , , !" % , # % )
such that ( ) = ( )
55
Equivalence of DFA and NFA
• Given a NFA
= ( , , , !" , #)
we can construct an equivalent DFA
$=( %, , % , ! % , #% )
"
• % =&
• For ∈ %, % ,( = ) ∈ ) ∈ (*, () for some * ∈ }
• )+% = ,- ./,{q+ }
• 1′ = ∈ % contains an accept state of }
56
Equivalence of DFA and NFA
• Construct a DFA that is equivalent to the following NFA.
= ({3, 4, 5}, 6, 7 , , 3 , {3})
57
Equivalence of DFA and NFA
• % =&
• )+% = ECLOSE 1 = {1,3}
• 1′ =
58
Equivalence of DFA and NFA- Summary
• Every DFA recognizes a regular language, and there is a DFA for every regular
language.
• There is an equivalent DFA (their languages are equal) for every NFA, and there
is an equivalent NFA for every DFA.
• Thus, every NFA recognizes a regular language, and there is a NFA for every
regular language.
DFA NFA
Regular Languages