Theory of Computation CSE 4040: Nondeterministic Finite Automata (NFA)
Theory of Computation CSE 4040: Nondeterministic Finite Automata (NFA)
CSE 4040
Nondeterministic Finite Automata(NFA)
21-12-2023 1
Recall, Deterministic Finite Automata
a a
q0 q1 a a
𝒒𝟐
q0 q1 𝒒𝟐
𝛿 𝒒𝟎 , 𝝀 = 𝒒𝟎 𝛿 𝒒𝟏 , 𝑎 = 𝒒𝟐
a a a a
a a
q0
a
q1
a
𝒒𝟐
q0 q1 𝒒𝟐
𝛿 𝒒𝟐 , 𝝀 = 𝒒𝟐
21-12-2023𝛿 𝒒𝟎 , 𝑎 = 𝒒 𝟏 3
Recall, Deterministic Finite Automata
𝛿 ∗ 𝒒𝟎 , 𝑎𝑎 = 𝛿(𝛿 ∗ 𝒒𝟎 , 𝑎 , 𝑎)
= 𝛿(𝛿 ∗ 𝒒𝟏 , 𝑎 , 𝝀)
a a
= 𝛿 𝒒𝟐 , 𝝀 q0 q1 𝒒𝟐
= 𝒒𝟐
𝑎𝑎 ∈ 𝐿(𝑀)
21-12-2023 4
Nondeterministic Finite Accepters(NFA)
Σ = {𝑎}
there are two transitions labelled a out of 𝑞0
and 𝑞3 is lacking any transition for input.
No Transition
a a a a
q0 a q1 q2 a q3 q0 a q1 q2 a q3
a a
a a
q0 a q1 q2 a q3 q0 a q1 q2 a q3
21-12-2023
The 𝜆 symbol is never seen on the input tape. 6
2.2 Nondeterministic Finite Accepters (𝜆 − NFA)
Example Σ = {0,1}
Reject : 0,1,11
𝛿 𝑞2 , 0 = 𝜙 , 𝛿 𝑞2 , 1 = 𝜙
multiple edges leaving one vertex labelled with the same symbol
An nfa accepts a string if some possible sequence of moves ends in a final state.
An nfa rejects a string if no possible sequence of moves ends a final state.
21-12-2023 10
∗
Extended transition function 𝜹
21-12-2023 11
Extended transition function 𝜹 ∗
𝛿 ∗ 𝑞0 , 𝑎 = {𝑞1 }
𝛿 ∗ 𝑞0 , 𝑎𝑏 = {𝑞2 , 𝑞3 , 𝑞0 }
q4 q5
a a q4 q5
q0 a q1 b q2 q3 a a
q0 a q1 b q2 q3
𝛿 ∗ 𝑞0 , 𝑎𝑎 = {𝑞4 , 𝑞5 }
q4 q5
a a
q0 a q1 b q2 q3
21-12-2023 12
Extended transition function 𝜹∗
Use 𝛿 ∗ to describe how the string 01010 is processed by the NFA.
𝛿 ∗ 𝑞0 , 𝟎1010 = δ 𝛿 ∗ 𝑞0 , 0101 , 0
1 0,1 = δ 𝛿 ∗ 𝑞0 , 0101 , 0
q0 q1 𝒒𝟐
= δ 𝛿 ∗ 𝑞0 , 101 , 0
= δ 𝛿 ∗ {𝑞0 , 𝑞1 }, 01 , 0
M
= δ 𝛿 ∗ ( 𝑞0 , 0) ∪ (𝑞1 , 0), 1 , 0
𝑞2 ∅ ∅ = δ( 𝑞0 , 0 ∪ 𝑞1 , 0 , 𝜆) = δ({𝑞0 } ∪ {𝑞2 }, 𝜆)
= δ( 𝑞0 , 𝜆 ∪ 𝑞2 , 𝜆 )
L(M)= all the strings ending in 10 (or) 11. = {𝑞0 } ∪ {𝑞2 }
= {𝑞0 , 𝑞2 }
21-12-2023 13
Nondeterministic Automata NFA
Definition 2.6
The language L accepted by nfa 𝑀 = (𝑄, Σ, 𝛿, 𝑞0 , 𝐹) is defined as the set of all
strings accepted in the above sense. Formally,
𝐿 𝑀 = {𝑤 ∈ Σ ∗ : 𝛿 ∗ (𝑞0 , 𝑤) ∩ 𝐹 ≠ 𝜙}.
In words, the language consists of all strings w for which there is a walk labelled
w from the initial vertex of the transition graph to some final vertex.
21-12-2023 14
Example
Σ = {𝑎, 𝑏}
(1) 𝐿1 = {𝑠𝑡𝑎𝑟𝑡𝑠 𝑤𝑖𝑡ℎ ′𝑎′}
21-12-2023 15
Example (1): What is the language accepted by the automaton in fig?
L={(10)𝑛 : 𝑛 ≥ 0}
21-12-2023 16
2)Design an nfa with no more than five states for the set 𝑎𝑏𝑎𝑏 𝑛 : 𝑛 ≥ 0 ∪
{𝑎𝑏𝑎𝑛 : 𝑛 ≥ 0}.
21-12-2023 17
Equivalence of Deterministic and Nondeterministic Finite Automata
21-12-2023 18
Equivalence of DFA’s and NFA’s
Every DFA is also an NFA by definition.
The number of states of the DFA can be exponential in the number of states of the NFA.
21-12-2023 19
Equivalence of Machines
• Definition 2.7
Two finite accepters 𝑀1 and 𝑀1 are said to be equivalent if
L(𝑀1 ) = L(𝑀2 ).
That is, if they both accept the same language.
21-12-2023 20
Equivalence of DFAs and NFAs
L(𝑀1 )={(10)𝑛 , 𝑛 ≥ 0}
𝑀1 (NFA)
L(𝑀2 )={(10)𝑛 , 𝑛 ≥ 0}
𝑀2 (DFA)
21-12-2023
Thus, 𝑀1 is equivalent to 𝑀2 . 21
Subset Construction
Note:
The DFA states have names that are sets of NFA states.
But as a DFA state, an expression like {𝑝, 𝑞} must read as a single symbol.
21-12-2023 22
Subset construction
• Example: We ‘ll construct the DFA for the following NFA which recognizes strings in {0,1}∗
containing a 1 in the second position from the end.
Solution:
Construction DFA 𝑀1 as 𝑀1 = (𝑄1 , 0,1 , 𝜹𝑫 , 𝑨 , 𝑭)
where 𝑄1 = 𝑃 𝑄 = 2𝑄 =
{∅, 𝐴 , 𝐵 , 𝐶 , 𝐴, 𝐵 , 𝐴, 𝐶 , 𝐵, 𝐶 , {𝐴, 𝐵, 𝐶}}
𝛿𝐷 𝐴, 𝐵, 𝐶 , 0 = 𝛿𝑁 𝐴 , 0 ∪ 𝛿𝑁 𝐵 , 0 ∪ 𝛿𝑁 𝐶 , 0 = 𝐴 ∪ {𝐶} ∪ Φ = {𝐴, 𝐶}
𝛿𝐷 𝐴, 𝐵, 𝐶 , 1 = 𝛿𝑁 𝐴 , 1 ∪ 𝛿𝑁 𝐵 , 1 ∪ 𝛿𝑁 𝐶 , 1 = 𝐴, 𝐵 ∪ {𝐶} ∪ Φ = {𝐴, 𝐵, 𝐶}
NFA
21-12-2023 24
DFA
Equivalence of DFAs and NFAs
Every DFA is obviously an NFA.
A language accepted by a DFA is also accepted by an NFA.
21-12-2023 25
Epsilon-Closures
It is a set of reachable states from a given state with 𝜀 − 𝑡𝑟𝑎𝑛𝑠𝑖𝑡𝑖𝑜𝑛.
0 1 0 1
q0 𝜀 𝒒𝟏 𝜀 𝒒𝟐 q0 𝜀 𝒒𝟏 𝜀 𝑞2
0
1
𝜀 − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒 𝑞0 = {𝑞0 , 𝑞1 , 𝑞2 }
𝜀 − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒 𝑞1 = {𝑞1 , 𝑞2 } 𝒒𝟑
𝜀 − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒 𝑞2 = {𝑞2 }
𝜀 − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒 𝑞0 = {𝑞0 , 𝑞1 , 𝑞2 }
𝜀 − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒 𝑞1 = {𝑞1 , 𝑞2 }
𝜀 − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒 𝑞2 = {𝑞2 }
𝜀 − 𝑐𝑙𝑜𝑠𝑢𝑟𝑒 𝑞3 = {𝑞3 }
21-12-2023 26
Summary
DFA’s , NFA’s and 𝜆 − 𝑁𝐹𝐴′ 𝑠 all accept exactly the same set of languages:
the regular languages.
NFA types are easier to design and may have exponentially fewer states than a
DFA.
21-12-2023 27