0% found this document useful (0 votes)
49 views47 pages

Understanding NFA-ε: Definitions & Examples

The document explains the structure and functioning of Nondeterministic Finite Automata with ε (NFA-ε), detailing its components such as states, input alphabet, and transition functions. It introduces concepts like ε-closure and provides methods for converting NFA-ε to equivalent Deterministic Finite Automata (DFA). Additionally, it discusses the equivalence of NFAs and NFA-εs in terms of the languages they can accept.

Uploaded by

Mr Shoper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views47 pages

Understanding NFA-ε: Definitions & Examples

The document explains the structure and functioning of Nondeterministic Finite Automata with ε (NFA-ε), detailing its components such as states, input alphabet, and transition functions. It introduces concepts like ε-closure and provides methods for converting NFA-ε to equivalent Deterministic Finite Automata (DFA). Additionally, it discusses the equivalence of NFAs and NFA-εs in terms of the languages they can accept.

Uploaded by

Mr Shoper
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NFAs with ε Moves

An NFA-ε is a five-tuple:
M = (Q, Σ, δ, q 0, F)
Q A finite set of states
Σ A finite input alphabet
q0 The initial/starting state, q0 is in Q
F A set of final/accepting states, which is a subset of Q
δ A transition function, which is a total function from Q x Σ U {ε} to 2 Q
δ: (Q x (Σ U {ε})) –> 2Q
δ(q,s) -The set of all states p such that there is a
transition labeled a from q to p, where a is in Σ U {ε}
Sometimes referred to as an NFA-ε other times, simply as an NFA.

1
Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0

δ: 0 1 ε
q0 - A string w = w 1w2…wn
is processed {q0} {} {q1}
as w = ε *w1ε*w2ε* …
ε* w nε
*
{q1, {q0, {q2}
q1 - Example: all
computations on 00: q2} q3}
{q2} {q2} {} 0 ε 0
q2 q0 q 0 q 1 q 2
{} {} {} :
q3

2
Informal Definitions
Let M = (Q, Σ, δ,q0,F) be an NFA-ε.

A String w in Σ* is accepted by M iff there exists a path in M from q0


to a state in F labeled by w and zero or more ε transitions.

The language accepted by M is the set of all strings from Σ* that


are accepted by M.

3
ε-closure
Define ε-closure(q) to denote the set of all states reachable from q by zero
or more ε transitions.
Examples: (for the previous NFA)
ε-closure(q0) = {q0, q1, q2} ε-closure(q2) = {q2}
ε-closure(q1) = {q1, q2} ε-closure(q3) = {q3}

ε-closure(q) can be extended to sets of states by defining:


ε-closure(P) = ε-closure(q)
Examples:
q3
1
ε-closure({q1, q2}) = {q1, q2} 0 0 0/1
ε ε
ε-closure({q0, q3}) = {q0, q1, q2, q3} q0 q1 q2
1 0
4
Extension of δ to Strings and Sets of
States
What we currently have: δ : (Q x (Σ U {ε})) –> 2Q

What we want (why?): δ : (2Q x Σ*) –> 2Q

As before, we will do this in two steps, which will be slightly


different from the book, and we will make use of the
following NFA. q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0
5
Step #1:
Given δ: (Q x (Σ U {ε})) –> 2Q define δ#: (2Q x (Σ U {ε})) –>
2Q as follows:
1) δ#(R, a) = δ(q, a) for all subsets R of Q, and symbols a
in Σ U {ε}
Note that:

δ#({p},a) = δ(q, a) by definition of δ#, rule #1 above


= δ(p, a)
Hence, we can use δ for δ#
δ({q0, q2}, 0) These now make
sense, but previously
δ({q0, q1, q2}, 0) they did not.

6
Examples:

What is δ({q0 , q1, q2}, 1)?

δ({q0 , q1, q2}, 1) = δ(q0, 1) U δ(q1, 1) U δ(q2, 1)


= { } U {q0, q3} U {q2}
= {q0, q2, q3}

What is δ({q0, q1}, 0)?

δ({q0 , q1}, 0) = δ(q0, 0) U δ(q1, 0)


= {q0} U {q1, q2}
= {q0, q1, q2}
7
Step #2:

Given δ: (2Q x (Σ U {ε})) –> 2Q define δ^: (2Q x Σ*) –> 2Q as follows:

δ^(R,w) – The set of states M could be in after processing string w,


having starting from any state in R.

Formally:

2) δ^(R, ε) = ε-closure(R) - for any subset R of


Q
3) δ^(R,wa) = ε-closure(δ(δ^(R,w), a)) - for any w in Σ*, a in
Σ, and

subset R of Q

Can we use δ for δ^?


8
Consider the following example:

δ({q0}, 0) = {q0}

δ^({q0}, 0) = ε-closure(δ(δ^({q0}, ε), 0)) By


rule #3
= ε-closure(δ(ε-closure({q 0}), 0)) By
rule #2
= ε-closure(δ({q 0, q1, q2}, 0)) By ε-
closure
= ε-closure(δ(q 0, 0) U δ(q1, 0) U δ(q2, 0)) By
rule #1
= ε-closure({q 0} U {q1, q2} U {q2})
= ε-closure({q 0, q1, q2})
= ε-closure({q 0}) U ε-closure({q1}) U ε-closure({q2})
= {q0, q1, q2} U {q1, q2} U {q2}
= {q0, q1, q2}

So what is the difference?

δ(q0, 0) - Processes 0 as a single symbol, without ε transitions.


δ^(q0 , 0) - Processes 0 using as many ε transitions as are possible. 9
Example:

δ^({q0}, 01) = ε-closure(δ(δ^({q0}, 0), 1))


By rule #3
= ε-closure(δ({q0, q1, q2}), 1)
Previous slide
= ε-closure(δ(q0, 1) U δ(q1, 1) U δ(q2, 1))
By rule #1
= ε-closure({ } U {q0, q3} U {q2})
= ε-closure({q0, q2, q3})
= ε-closure({q0}) U ε-closure({q2}) U ε-closure
({q3})
= {q0, q1, q2} U {q2} U {q3}
= {q0, q1, q2, q3} 10
Definitions for NFA-ε Machines
Let M = (Q, Σ, δ,q0,F) be an NFA-ε and let w be in Σ*. Then w is
accepted by M iff δ^({q0}, w) contains at least one state in F.

Let M = (Q, Σ, δ,q0,F) be an NFA-ε. Then the language accepted


by M is the set:

L(M) = {w | w is in Σ* and δ^({q0},w) contains at least


one state in F}

Another equivalent definition:

L(M) = {w | w is in Σ* and w is accepted by M}


11
Equivalence of NFAs and NFA-εs
Do NFAs and NFA-ε machines accept the same class of
languages?
• Is there a language L that is accepted by a NFA, but not by any NFA -ε?
• Is there a language L that is accepted by an NFA -ε, but not by any DFA?

Observation: Every NFA is an NFA-ε.

Therefore, if L is a regular language then there exists an


NFA-ε M such that L = L(M).

It follows that NFA-ε machines accept all regular languages.

But do NFA-ε machines accept more? 12


Lemma 1: Let M be an NFA. Then there exists a NFA-ε M’
such that L(M) = L(M’).

Proof: Every NFA is an NFA-ε. Hence, if we let M’ = M, then


it follows that L(M’) = L(M).

The above is just a formal statement of the


observation from the previous slide.

13
Lemma 2: Let M be an NFA-ε. Then there exists a NFA M’ such that L(M) =
L(M’).

Proof: (sketch)

Let M = (Q, Σ, δ,q0,F) be an NFA-ε.

Define an NFA M’ = (Q, Σ, δ’,q0,F’) as:

F’ = F U {q} if ε-closure(q) contains at least one state


from F
F’ = F otherwise

δ’(q, a) = δ^(q, a) - for all q in Q and a in


Σ

Notes:

• δ’: (Q x Σ) –> 2Q is a function


• M’ has the same state set, the same alphabet, and the same start state as M
• M’ has no ε transitions 14
Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0

Step #1: q3

• Same state set as M


• q0 is the starting state

q0 q1 q2

15
Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0

q3
Step #2:
• q0 becomes a final state

q0 q1 q2

16
Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0

q3
Step #3:
0
0
q0 q1 q2

17
Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0

q3
Step #4:
1
0/1

0/1
q0 q1 q2

0/1

18
Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0

q3
Step #5:
1
0/1
0
0/1 0
q0 q1 q2

0/1

19
Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0

q3
Step #6:
1
0/1 1
0/1
0/1 0/1
q0 q1 q2
1
0/1

20
Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0

q3
Step #7:
1
0/1 1 0
0/1
0/1 0/1
q0 q1 q2
1
0/1

21
Example:
q3
1
0 0 0/1
ε ε
q0 q1 q2
1 0

q3
Step #8: [use table of e-closure]
1
• Done!
0/1 1 0/1
0/1
0/1 0/1
q0 q1 q2
1
0/1

22
Theorem: Let L be a language. Then there exists an NFA M
such that L= L(M) iff there exists an NFA-ε M’ such that L =
L(M’).

Proof:
(if) Suppose there exists an NFA-ε M’ such that L =
L(M’). Then by Lemma 2 there exists an NFA M such that L
= L(M).

(only if) Suppose there exists an NFA M such that L


= L(M). Then by Lemma 1 there exists an NFA-ε M’ such
that L = L(M’).

Corollary: The NFA-ε machines define the regular


languages. 23
Method for conversion
NFA with ε to DFA
The method for converting the NFA with ε to DFA is explained
below −
Step 1 − Consider M={Q, Σ, δ,q0,F) is NFA with ε. We have
to convert this NFA with ε to equivalent DFA denoted by
M0=(Q0,Σ, δ0,q0,F0)
Then obtain,
ε-closure(q0) ={p1,p2,p3,……pn}
then [p1,p2,p2,….pn] becomes a start state of DFA
now[p1,p2,p3,….pn] ∈ Q0
Method for conversion
NFA with ε to DFA
Step 2 − We will obtain δ transition on [p1,p2,p3,…pn] for
each input.
δ 0([p1,p2,p3,..pn],a) = ε-closure(δ(p1,a) U
δ(p2,a2)U……………… δ(pn,a))
= U (i=1 to n) ε-closure d(pi,a)
Where a is input ∈Σ
Step 3 − The state obtained [p1,p2,p3,…pn] ∈ Q0 .
The states containing final state in pi is a final state in DFA
Example

Convert the following NFA with epsilon to equivalent DFA


Solution

Consider the following NFA for conversion of NFA with


epsilon to DFA

To convert this NFA with epsilon, we will first fin


the ε-closures, as given below −
•ε-closure(q0)={q0,q1,q2}
•ε-closure(q1)={q1,q2}
•ε-closure(q2)={q2}
Solution

Consider the following NFA for conversion of NFA with


epsilon to DFA

To convert this NFA with epsilon, we


will first find the ε-closures, as given
below −
•ε-closure(q0)={q0,q1,q2}
•ε-closure(q1)={q1,q2}
•ε-closure(q2)={q2}
Solution

Consider the following NFA for conversion of NFA with


epsilon to DFA
ε-closure(q0)={q0,q1,q2}, we will call this state as A.
•ε-closure(q0)={q0,q1,q2}
•ε-closure(q1)={q1, q2} δ'(A, a) = ε-closure(δ(A,a))
•ε-closure(q2)={q2} = ε-closure(δ(q0, q1, q2), a))
= ε-closure(δ(q0, a) ∪ δ(q1,a) U δ(q2,a) )
= ε-closure(ΦU q1∪q2)
= ε-closure(q1)
= {q1, q2} let us call it as state B
Solution

Consider the following NFA for conversion of NFA with


epsilon to DFA

•ε-closure(q0)={q0,q1,q2}
•ε-closure(q1)={q1,q2} δ'(A, b) = ε-closure(δ(A,b))
•ε-closure(q2)={q2} = ε-closure(δ(q0,q1,q2), b))
= ε-closure(δ(q0, b) ∪ δ(q1,b) U δ(q2,b) )
= ε-closure(q0 U Φ∪q0)
= ε-closure(q0)
= {q0,q1, q2} its nothing but state A
Solution

Consider the following NFA for conversion of NFA with


epsilon to DFA

•ε-closure(q0)={q0,q1,q2}
•ε-closure(q1)={q1,q2} δ'(B, a) = ε-closure(δ(B,a))
•ε-closure(q2)={q2} = ε-closure(δ(q1,q2), a))
= ε-closure(δ(q1,a) U δ(q2,a) )
= ε-closure(q1 ∪q2)
= ε-closure(q1)
= {q1, q2} its nothing but state B
Solution

Consider the following NFA for conversion of NFA with


epsilon to DFA

•ε-closure(q0)={q0,q1,q2}
•ε-closure(q1)={q1,q2} δ'(B, b) = ε-closure(δ(B,b))
•ε-closure(q2)={q2} = ε-closure(δ(q1,q2), b))
= ε-closure(δ(q1,b) U δ(q2,b) )
= ε-closure(Φ∪q0)
= ε-closure(q0)
= {q0,q1, q2} its nothing but state A
Example:
Convert the following NFA with epsilon to equivalent DFA

1.ε-closure {q0} = {q0, q1, q2}


2.ε-closure {q1} = {q1}
3.ε-closure {q2} = {q2}
4.ε-closure {q3} = {q3}
5.ε-closure {q4} = {q4}
Example:
Convert the following NFA with epsilon to equivalent DFA

1.ε-closure {A} = { }
2. ε-closure {B} = { }
3.ε-closure {C} = { }
4.ε-closure {D} = { }
5.ε-closure {E} = { }
Example:
Convert the following NFA with epsilon to equivalent DFA

1.ε-closure {1} = { }
2.ε-closure {2} = { }
3.ε-closure {3} = { }
4.ε-closure {4} = { }
5.ε-closure {5} = { }
6.ε-closure {6} = { }
Minimization of DFA
The process of reducing a given DFA to its minimal form is called as minimization of DFA.

It contains the minimum number of states


The DFA in its minimal form is called as a Minimal DFA.


How To Minimize DFA?


● The two popular methods for minimizing a DFA are-

36
37
Minimization of DFA Using Equivalence Theorem-

Step-01:
●Eliminate all the dead states and inaccessible states
from the given DFA (if any).
–Dead State
–All those non-final states which transit to itself for all
input symbols in ∑ are called as dead states.
–Inaccessible State
–All those states which can never be reached from the
initial state are called as inaccessible states.
38
Step-02:

● Draw a state transition table for the given DFA.


●Transition table shows the transition of all states on all
input symbols in Σ.

39
Step-03:

Now, start applying equivalence theorem.


● Take a counter variable k and initialize it with value 0.
●Divide Q (set of states) into two sets such that one set
contains all the non-final states and other set contains all
the final states.
● This partition is called P0.

40
Step-04:
● Increment k by 1.
● Find Pk by partitioning the different sets of Pk-1 .
●In each set of Pk-1 , consider all the possible pair of
states within each set and if the two states are
distinguishable, partition the set into different sets in Pk.
●Two states q1 and q2 are distinguishable in partition Pk
for any input symbol ‘a’,
if δ (q1, a) and δ (q2, a) are in different sets in partition

Pk-1.

41
Step-05:

•Repeat step-04 until no change in partition occurs.


•In other words, when you find Pk = Pk-1, stop.

42
Step-06:

•All those states which belong to the same set are


equivalent.
•The equivalent states are merged to form a single state
in the minimal DFA.

43
Example
Minimize the given DFA-
Step-01:
The given DFA contains no dead states and inaccessible states.
Step-02:
Draw a state transition table-

a b Step-03:
Now using Equivalence Theorem, we have-
→q0 q1 q2 P0 = { q0 , q1 , q2 , q3 } { q4 }
q1 q1 q3 P1 = { q0 , q1 , q2 } { q3 } { q4 }
q2 q1 q2 P2 = { q0 , q2 } { q1 } { q3 } { q4 }
P3 = { q0 , q2 } { q1 } { q3 } { q4 }
q3 q1 *q4 Since P3 = P2, so we stop. So, Our minimal
*q4 q1 q2 DFA is- 44
Example
Minimize the given DFA-

Step-03:
Now using Equivalence Theorem, we have-
P0 = { q0 , q1 , q2 , q3 } { q4 }
P1 = { q0 , q1 , q2 } { q3 } { q4 }
P2 = { q0 , q2 } { q1 } { q3 } { q4 }
P3 = { q0 , q2 } { q1 } { q3 } { q4 }
Since P3 = P2, so we stop. So, Our minimal
DFA is-

45
Example
Minimize the given DFA-

46
Example
Minimize the given DFA-

47

You might also like