Module 2-9
Module 2-9
1 / 184
Outline
1 Finite Automata
5 Minimization of DFA
2 / 184
Introduction of Finite Automata
Exampe 3 : Elevator
3 / 184
Example 1: Vending Machine
Formal Representation:
A vending machine FA can be modeled as a 5-tuple:
(Q, Σ, δ, q0 , F)
Real-Time Mapping:
Think of it as a controller:
Insert Accept Dispense Return Change
4 / 184
Vending Machine: Transition Diagram
InsertCoin SelectItem
Answer: Accepted
InsertCoin SelectItem
start Waiting Paid Dispensing
5 / 184
Example 2: Traffic Light
Formal Representation:
A traffic light FA can be modeled as a 5-tuple:
(Q, Σ, δ, q0 , F)
States Description:
q0 (Red): Stop – initial state.
q1 (Yellow): Transition after Red.
q2 (Green): Go – vehicles can move.
q3 (Yellow): Prepare to stop – before Red.
Timer
start Red Yellow 1
Timer
Timer
Yellow 2 Green
Timer
7 / 184
Example-3: Elevator (Lift)
To better understand finite automata, let’s consider a
simple example involving an elevator system with four
floors: Ground, First, Second, and Third floor.
Formal Definition:
Let the finite automaton be defined as a 5-tuple:
M = (Q, Σ, δ, q0 , F)
Where:
Q = {q0 , q1 , q2 , q3 } — States: Ground, First,
Second, Third Floor
Σ = {0, 1, 2, 3} — Inputs: Floor button
selections
q0 — Initial state (Ground floor)
F = {q3 } — Final state (Third floor)
8 / 184
Elevator: Transition Table and State Diagram
Transition Function δ
δ 0 1 2 3
q0 q0 q1 q2 q3
q1 q0 q1 q2 q3
q2 q0 q1 q2 q3
q3 q0 q1 q2 q3
9 / 184
Elevator: Transition Table and State Diagram
State Diagram
3
0 2
2
1 2 3
start q0 q1 q2 q3 3
0 1 2
0 1 3
1
0
10 / 184
Features of Finite Automata
11 / 184
Working of Finite Automata
Every automaton takes a language as input, processes it through its state ma-
chine, and outputs whether the input string is accepted or rejected based on its
final state.
L1 L2 ··· Ln Input
Automata Machine
{q0 , q1 , q2 , . . . , qn } States of Automata
O1 O2 ··· On Output
12 / 184
What is an Informal Finite Automaton?
An informal finite automaton is a conceptual model that explains how a
system or process moves through different stages (called states) based on
inputs or actions.
Key Characteristics:
14 / 184
Informal Finite Automaton
q0 send q1 verify q2
start
Start Sent Verified
cancel ship
cancel cancel
qf q3
Cancelled Completed
Explanation
The process begins in q0, where the customer has the digital money.
If the customer proceeds, they send it to the store (q1), the store verifies
it (q2), and then ships the goods (q3).
At any stage before shipping, the customer can cancel the transaction,
ending in state qf. 15 / 184
Types of Finite Automata
Finite Au-
tomata
16 / 184
Deterministic Finite Automata (DFA)
A DFA is defined as a 5-tuple: {Q, Σ, δ, q0 , F}
where
Q is the set of all states.
Σ is the set of input symbols.
δ : Q × Σ → Q is the transition function.
q0 is the initial state.
F is the set of final (accept) states.
Note:
For a particular input symbol, the machine moves to only one state.
ε (null) transitions are not allowed.
It means the state cannot change without an input symbol.
17 / 184
Example 1
Construct a DFA which accept a language of all strings starting with ‘0’
over two alphabets Σ = {0, 1}
Language:
L = {0, 00, 01, 000, 001, 010, 011, . . . }
This DFA accepts all binary strings that start with the symbol ‘0’.
Transition Diagram:
0,1 0,1
Accept Reject
0 1 0
01 10 q0 q1 q∅
start
001 101
Trap State
1
18 / 184
Example 1
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q∅ }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 q1 q∅
q1 q1 q1
q∅ q∅ q∅
Initial State q0 = q0 ∈ Q
Construct a DFA which accept a language of all strings starting with ‘1’
over two alphabets Σ = {0, 1}
Language:
L = {1, 10, 11, 100, 101, 110, 111, . . . }
This DFA accepts all binary strings that start with the symbol ‘1’.
Transition Diagram:
0,1 0,1
Accept Reject
1 0
1
10 01
100 001 start q0 q1 q∅
Trap State
0
20 / 184
Example 2
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q∅ }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 q∅ q1
q1 q1 q1
q∅ q∅ q∅
Initial State q0 = q0 ∈ Q
Design a DFA over the alphabet Σ = {0, 1} that accepts all strings ending
in “01”
Language:
L = {01, 001, 101, 0001, 0101, 1101, 11001, 01101 . . . }
This DFA accepts all binary strings that end with the symbol “01”.
Transition Diagram:
Accept Reject
01 11 1 0
001 011 0
0101 1010 q0 q1 q2
start
11001 11000 0 1
01100101 10011100
1
22 / 184
Example 3
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 q1 q0
q1 q1 q2
q2 q1 q0
Initial State q0 = q0 ∈ Q
Language:
L = {w ∈ {0, 1}∗ | w has even number of 0s and even number of 1s}
We can define a language L as follows
L = {0011, 0110, 110110, 1001, · · · }
Transition Diagram:
0
start q0 q1
Accept Reject 0
0011 0111 1 1 1 1
0110 0001 0
110110 100110
q2 q3
0 24 / 184
Example 4
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q3 }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 q1 q2
q1 q0 q3
q2 q3 q0
q3 q2 q1
Initial State q0 = q0 ∈ Q
Language:
Accept Reject
FT FF
TF TT
FFF FTF
26 / 184
Example 5
Transition Diagram
q0 F q1 F q2 F q3
start
T T F,T
q∅ F,T
Trap State
27 / 184
Example 5
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q3 , q∅ }
Inputs Σ = {F, T}
Transition function δ :
Input
F T
State
q0 q1 q2
q1 q2 q3
q2 q3 q∅
q3 q∅ q∅
q∅ q∅ q∅
Initial State q0 = q0 ∈ Q
Language:
29 / 184
Example 6
Transition Diagram
C, B, T 0–9
start q0 q1 q2
C,B,T
0–9 C,B,T, 0–9
q∅ C,B,T, 0–9
Trap State
30 / 184
Example 6
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q∅ }
Transition function δ :
Input
C,B,T 0-9
State
q0 q1 q∅
q1 q∅ q2
q2 q∅ q∅
q∅ q∅ q∅
Initial State q0 = q0 ∈ Q
Language:
32 / 184
Example 7
Transition Diagram
A–Z,
A–Z, @, #, $ A–Z,
@, #, $ 0–9 A–Z,
0–9,
0–9,
@, #, $ q∅ @, #, $
Trap State A–Z,
0–9,
@, #, $
33 / 184
Example 7
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q3 , q4 , q∅ }
Inputs Σ = {{A − Z}, {0 − 9}, {@, #, $}
Transition function δ :
Input
A-Z 0-9 @, #, $
State
q0 q1 q∅ q∅
q1 q∅ q2 q∅
q2 q∅ q3 q∅
q3 q∅ q∅ q4
q4 q∅ q∅ q∅
q∅ q∅ q∅ q∅
Initial State q0 = q0 ∈ Q
Accept State F = {q4 } ⊆ Q
34 / 184
Exercise Problems
1. Construct a DFA that accepts input strings of 0’s and 1’s that ends with “11”
2. Design a DFA to accept strings over Σ = {0, 1} with three consecutive 0’s.
3. Construct a DFA that accepts sets of all strings over {0,1} of length 2. [Hint:
Use trap state (dead state)]
5. Construct a DFA that accepts any string over {a,b} that does not contain the
string “aabb” in it.
35 / 184
Exercise Problems
7. A system monitors log files for error patterns over the alphabet e, w, n (error
= e, warning = w, normal = n). It flags logs that contain at least two consecutive
errors (“ee”) to trigger an alert. Design a DFA to detect such error patterns.
36 / 184
Exercise Problems
8. Design a DFA for an exam monitoring system that processes logs over the
alphabet {s,c,f}, where s represents a student signing in, c represents complet-
ing the exam, and f indicates a cheating flag. The system should accept only
valid session logs that begin with a sign-in (s) and do not contain any instance
of cheating (f). Any log that starts with c or includes the character f should be
rejected, transitioning to a trap state. Construct a DFA that enforces this rule.
37 / 184
Exercise 1: Construct a DFA that accepts input strings
of 0’s and 1’s that ends with “11”
Language:
L = {11, 011, 111, 0011, 0111, 1111, 00011, 00111, 01111, 01011, . . . }
This DFA accepts all binary strings that ends with “11”.
Accept Reject
11 10
011 101
1011 1001
11011 11001
Transition Diagram
0 1
0
q0 1 q1 1 q2
start
0
38 / 184
Exercise 1
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 q0 q1
q1 q1 q2
q2 q2 q2
Initial State q0 = q0 ∈ Q
q0 0 q1 0 q2 0 q3
start
1 40 / 184
Exercise 2
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q3 }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 q1 q0
q1 q2 q1
q2 q3 q2
q3 q3 q3
Initial State q0 = q0 ∈ Q
Construct a DFA that accepts sets of all strings over {0,1} of length 2.
[Hint: Use trap state (dead state)].
Language:
L = {00, 01, 10, 11}
This DFA accepts all binary strings that of length 2.
Transition Diagram
0,1
Trap State
42 / 184
Exercise 3
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q∅ }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 q1 q1
q1 q2 q2
q2 q∅ q∅
q∅ q∅ q∅
Initial State q0 = q0 ∈ Q
Language:
L = {w ∈ {0, 1}∗ | w has even number of a’s and even number of b’s}
We can define a language L as follows
L = {aabb, abba, bbabba, baab, · · · }
Transition Diagram:
a
Inputs Σ = {a, b}
Transition function δ :
Input
a b
State
q0 q1 q2
q1 q0 q3
q2 q3 q0
q3 q2 q1
Initial State q0 = q0 ∈ Q
Construct a DFA that accepts any string over {a,b} that does not contain
the string “aabb” in it.
Accept Reject
aabb abab
baabb babaa
baaabb aaabaa
abaabb baaaba
abaabba aabaaab
baabaabb ababaaab
46 / 184
Exercise 5
Transition Diagram
b a a,b
q0 a q1 a q2 b q3 b q4
start
b
a
47 / 184
Exercise 5
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q3 , q4 }
Inputs Σ = {a, b}
Transition function δ :
Input
a b
State
q0 q1 q0
q1 q2 q0
q2 q2 q3
q3 q1 q4
q4 q4 q4
Initial State q0 = q0 ∈ Q
The above DFA accepts any string over {a, b} that contains the substring
“aabb”.
Note:
We need a DFA that accepts any string over {a, b} that does not contain
the substring “aabb”.
49 / 184
Exercise 5
Construct a language consisting of all strings over {a,b} that do not contain the
substring “aabb”.
Language:
L = {abab, abba, aaaba, babaaab, . . . }
This DFA accepts all strings that does not contains the string “aabb”.
Transition Diagram
b a a,b
q0 a q1 a q2 b q3 b q4
start
b
a
50 / 184
Exercise 5
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q3 , q4 }
Inputs Σ = {a, b}
Transition function δ :
Input
a b
State
q0 q1 q0
q1 q2 q0
q2 q2 q3
q3 q1 q4
q4 q4 q4
Initial State q0 = q0 ∈ Q
Language:
L = {sh, msh, ssshmhmsh, mmsmsh, smhsh, . . . }
Accept Reject
sh ss
msh mhs
smhsh ssmhs
52 / 184
Exercise 6
Transition Diagram
m, h s m, s, h
s
q0 q1 h q2
start
m
53 / 184
Exercise 6
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 }
Inputs Σ = {m, s, h}
Transition function δ :
Input
m s h
State
q0 q0 q1 q0
q1 q0 q1 q2
q2 q2 q2 q2
Initial State q0 = q0 ∈ Q
54 / 184
Exercise 7
A system monitors log files for error patterns over the alphabet e, w, n
(error = e, warning = w, normal = n). It flags logs that contain at least two
consecutive errors (“ee”) to trigger an alert. Design a DFA to detect such
error patterns.
Language:
L = {ee, nee, weee, neewn, eenen, . . . }
Accept Reject
ee en
nee ene
weee wene
55 / 184
Exercise 7
Transition Diagram
w, n e, w, n
e
q0 q1 e q2
start
w, n
56 / 184
Exercise 7
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 }
Inputs Σ = {e, w, n}
Transition function δ :
Input
e w n
State
q0 q1 q0 q0
q1 q2 q0 q0
q2 q2 q2 q2
Initial State q0 = q0 ∈ Q
57 / 184
Exercise 8
Design a DFA for an exam monitoring system that processes logs over the
alphabet {s,c,f}, where s represents a student signing in, c represents com-
pleting the exam, and f indicates a cheating flag. The system should accept
only valid session logs that begin with a sign-in (s) and do not contain any
instance of cheating (f). Any log that starts with c or includes the char-
acter f should be rejected, transitioning to a trap state. Construct a DFA
that enforces this rule.
Language:
L = {s, ss, sc, sss, ssc, . . . }
Accept Reject
s f
ss cs
ssc ssf
58 / 184
Exercise 8
Transition Diagram
s, c
s
start q0 q1
f
c, f
q∅ s, c, f
Trap State
59 / 184
Exercise 8
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q∅ }
Inputs Σ = {s, c, f }
Transition function δ :
Input
s c f
State
q0 q1 q∅ q0
q1 q2 q0 q0
q2 q2 q2 q2
Initial State q0 = q0 ∈ Q
60 / 184
Extended Transition Function in DFA
δ̂(q, ϵ) = q
δ̂(q, w) = δ δ̂(q, x), a for x ∈ Σ∗ , a ∈ Σ
Note:
Here w = xa is a string; that is, a is the last symbol of w, and x is the string
consisting of all but not the last symbol.
61 / 184
Example of Extended Transition Function
Process the string 101 for the transition diagram given below
0 0
1
start q0 q1
States: Q = {q0 , q1 }
Alphabet: Σ = {0, 1}
Start state: q0
62 / 184
Example of Extended Transition Function
Transition function:
δ(q0 , 0) = q0
δ(q0 , 1) = q1
δ(q1 , 0) = q1
δ(q1 , 1) = q0
Transition table:
Input
0 1
State
q0 q0 q1
q1 q1 q0
63 / 184
Computation of δ̂(q0 , 101)
δ̂(q0 , ∅) = q0
δ̂(q0 , 1) = δ δ̂(q0 , ∅), 1 = δ(q0 , 1) = q1
δ̂(q0 , 10) = δ δ̂(q0 , 1), 0 = δ(q1 , 0) = q1
δ̂(q0 , 101) = δ δ̂(q0 , 10), 1 = δ(q1 , 1) = q0
Result: The DFA ends in state q0 after reading input string “101”.
64 / 184
Language of a DFA
M = (Q, Σ, δ, q0 , F)
L(A) = {w | δ̂(q0 , w) ∈ F}
That is, the language of M is the set of strings w that take the start state
qo to one of the accepting states.
65 / 184
Nondeterministic Finite Automata
An Nondeterministic Finite Automata (NFA) is a 5-tuple:
M = (Q, Σ, δ, q0 , F)
where,
Q is a finite set of states.
Σ is a finite set of input symbols.
δ : Q × Σ → 2Q is a transition function.
q0 ∈ Q is the start state.
F ⊆ Q is the set of accepting/final state.
Note:
For a particular input symbol, the machine can moves to multiple
states.
If Q = {q0 , q1 }, then 2Q = {∅, {q0 }, {q1 }, {q0 , q1 }}
66 / 184
Concept of NFA Construction
Start with “101”:
0,1
q0 1 q1 0 q2 1 q3
start
Containing “101”:
0,1 0,1
q0 1 q1 0 q2 1 q3
start
q0 1 q1 0 q2 1 q3
start
67 / 184
Example 1
Construct an NFA accepting the strings over Σ = {0, 1} that end with “0”.
Language:
L = {0, 00, 10, 000, 010, 100, . . . }
This NFA accepts all strings that ends with “0”.
Accept Reject
0 1
10 01
001010 100101
Transition Diagram
0,1
q0 0 q1
start
68 / 184
Example 1
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 {q0 , q1 } {q0 }
q1 − −
Initial State q0 = q0 ∈ Q
69 / 184
Example 2
Construct an NFA accepting the strings over Σ = {a, b} ending in “aba”.
Language:
L = {aba, aaba, aaaba, baba, bbaba, ababa, . . . }
This NFA accepts all strings that ends with “aba”.
Accept Reject
aba bba
aaba aaaa
abaaba abbaab
Transition Diagram
a,b
q0 a q1 b q2 a q3
start
70 / 184
Example 2
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q3 }
Inputs Σ = {a, b}
Transition function δ :
Input
a b
State
q0 {q0 , q1 } {q0 }
q1 − {q2 }
q2 {q3 } −
q3 − −
Initial State q0 = q0 ∈ Q
Language:
L = {baa, bbb, bab, bba, aaabaa, bbbaabbb, . . . }
Accept Reject
baa abb
aaabaa abaaba
ababab abbaab
Transition Diagram
a,b
b a,b a,b
start q0 q1 q2 q3
72 / 184
Example 3
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q3 }
Inputs Σ = {a, b}
Transition function δ :
Input
a b
State
q0 {q0 } {q0 , q1 }
q1 {q2 } {q2 }
q2 {q3 } {q3 }
q3 − −
Initial State q0 = q0 ∈ Q
Language:
L = {01, 001, 0101, 001101, . . . }
This NFA accepts all strings that ends with “01”.
Accept Reject
01 10
001 111
0101101 0101110
Transition Diagram
0,1
q0 0 q1 1 q2
start
74 / 184
Example 4
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 {q0 , q1 } {q0 }
q1 − {q2 }
q2 − −
Initial State q0 = q0 ∈ Q
75 / 184
Example 5
Construct a nondeterministic finite automaton (NFA) over the alphabet
Σ = {a, b} that accepts all strings which contain the substring “ab” at
least once.
Language:
L = { ab, aab, bab, cab, babab, abb, . . . }
Accept Reject
ab aa
abaab baaaa
ababaa bbbbba
Transition Diagram:
a, b a, b
a b
start q0 q1 q2
76 / 184
Example 5
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 }
Inputs Σ = {a, b}
Transition function δ :
Input
a b
State
q0 {q0 , q1 } {q0 }
q1 – {q2 }
q2 {q2 } {q2 }
Initial State q0 = q0 ∈ Q
Construct an NFA over the alphabet Σ = {a, b} that accepts the language
L = {(ab)n | n ≥ 1}
Language:
78 / 184
Example 6
Accept Reject
ab ba
abab abba
ababab ababa
Transition Diagram:
a b
start q0 q1 q2
79 / 184
Example 6
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 }
Inputs Σ = {a, b}
Transition function δ :
Input
a b
State
q0 {q1 } −
q1 – {q2 }
q2 {q1 } –
Initial State q0 = q0 ∈ Q
Construct an NFA over the alphabet Σ = {0, 1} that accepts the language
L = {0m 1n | m ≥ 0 and n ≥ 1}
Language:
Let m = 0, n = 1 =⇒ 0m 1n = 00 11 = 1
Let m = 1, n = 2 =⇒ 0m 1n = 01 12 = 011
Let m = 2, n = 3 =⇒ 0m 1n = 02 13 = 00111
Let m = 3, n = 4 =⇒ 0m 1n = 03 14 = 0001111
..
.
81 / 184
Example 7
Accept Reject
1 0
011 110
00111 11100
Transition Diagram:
0 1
1
start q0 q1
82 / 184
Example 7
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 {q0 } {q1 }
q1 – {q1 }
Initial State q0 = q0 ∈ Q
83 / 184
Example 8
Language:
L = {{a − z}, ·, p, d, f , o, c}
84 / 184
Example 8
Transition Diagram
a–z
· p d f
start q0 q1 q2 q3 q4
q5 o q6 c q7
85 / 184
Example 8
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q3 , q4 , q5 , q6 , q7 }
Initial State q0 = q0 ∈ Q
86 / 184
Example 8
Transition function δ :
Input
a−z · p d f o c
State
q0 {q0 } {q1 } − − − − −
q1 − − {q2 } {q5 } − − −
q2 − − − {q3 } − − −
q3 − − − − {q4 } − −
q4 − − − − − − −
q5 − − − − − {q6 } −
q6 − − − − − − {q7 }
q7 − − − − − − −
87 / 184
Example 9
Design a finite automaton (NFA) that accepts passwords matching the pattern:
Starts with a capital letter (A–Z)
Followed by two digits (0–9)
Ends with a special character (@, #, $)
Language:
88 / 184
Example 9
Transition Diagram
89 / 184
Example 9
NFA:
M = {Q, Σ, δ, q0 , F}
States: Q = {q0 , q1 , q2 , q3 , q4 }
Input symbols: Σ = {A–Z, 0–9, @, #, $}
Transition function δ is defined by the table below:
Input
A–Z 0–9 @, #, $
State
q0 {q1 } ∅ ∅
q1 ∅ {q2 } ∅
q2 ∅ {q3 } ∅
q3 ∅ ∅ {q4 }
q4 ∅ ∅ ∅
Start state: q0
Accepting state: F = {q4 }
90 / 184
Exercise
1. Construct an NFA accepting the strings over Σ = {0, 1} that end with “1”.
3. Construct an NFA accepting the strings over Σ = {a, b} that start with “a”.
4. Construct an NFA that accepts sets of all strings over {0,1} of length 2.
91 / 184
Exercise 1
Construct an NFA accepting the strings over Σ = {a, b} that end with “1”.
Language:
L = {1, 01, 11, 001, 011, 101, 111 . . . }
This NFA accepts all strings that ends with “1”.
Accept Reject
1 0
01 00
001011 100100
Transition Diagram
0,1
q0 1 q1
start
92 / 184
Exercise 1
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 {q0 , q1 } {q0 }
q1 − {q1 }
Initial State q0 = q0 ∈ Q
93 / 184
Exercise 2
Construct an NFA accepting the strings over Σ = {a, b} ending in “aaa”.
Language:
L = {aaa, aaaa, baaa, abaaa, bbaaa, abaaaa, . . . }
This NFA accepts all strings that ends with “aaa”.
Accept Reject
aaa aba
aaaa aaba
abaaaa ababaa
Transition Diagram
a,b
q0 a q1 a q2 a q3
start
94 / 184
Exercise 2
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q3 }
Inputs Σ = {a, b}
Transition function δ :
Input
a b
State
q0 {q0 , q1 } {q0 }
q1 {q2 } −
q2 {q3 } −
q3 − −
Initial State q0 = q0 ∈ Q
Language:
L = {a, aa, ab, aaa, aba, abb, . . . }
This NFA accepts all strings that starts with “a”.
Accept Reject
a b
ab ba
abbaa bbbaa
Transition Diagram
a,b
q0 a q1
start
96 / 184
Exercise 3
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 }
Inputs Σ = {a, b}
Transition function δ :
Input
0 1
State
q0 {q1 } −
q1 {q1 } {q2 }
Initial State q0 = q0 ∈ Q
97 / 184
Exercise 4
Construct an NFA that accepts sets of all strings over {0,1} of length 2.
Language:
L = {00, 01, 10, 11}
This NFA accepts all binary strings that of length 2.
Transition Diagram
0,1 0,1
start q0 q1 q2
98 / 184
Exercise 4
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 }
Inputs Σ = {0, 1}
Transition function δ :
Input
0 1
State
q0 {q1 } {q1 }
q1 {q2 } {q2 }
q2 − −
Initial State q0 = q0 ∈ Q
99 / 184
Exercise 5
Language:
Accept Reject
1user user
xyz9 XYZ
hello123hello hello
100 / 184
Exercise 5
Transition Diagram
0-9
start q0 q1
101 / 184
Exercise 5
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 }
Transition function δ :
Input
a−z 0−9
State
q0 {q0 } {q1 }
q1 {q1 } {q1 }
Initial State q0 = q0 ∈ Q
102 / 184
DFA vs NFA
DFA NFA
It allows only one move for It allows more than one move
single input symbol. for single input symbol.
It cannot use empty string It can use empty string
transition. transition.
Dead (Trape) State may be Dead (Trape) state is not
required. required.
It requires more space. It requires less space.
It is difficult to construct. It is easy to construct.
All DFA’s are NFA. Not all NFA’s are DFA.
Both DFA and NFA can have multiple final states.
103 / 184
Extended Transition Function in NFA
δ̂(q, ϵ) = {q}
Let
k
[
δ(pi , a) = {r1 , r2 , . . . , rm }
i=1
104 / 184
Example of Extended Transition Function
Process the string 00101 for the NFA transition diagram given below
0,1
q0 0 q1 1 q2
start
States: Q = {q0 , q1 , q2 }
Alphabet: Σ = {0, 1}
Start state: q0
105 / 184
Example of Extended Transition Function
Transition function:
δ(q0 , 0) = {q0 , q1 }
δ(q0 , 1) = {q0 }
δ(q1 , 0) = ∅
δ(q1 , 1) = {q2 }
δ(q2 , 0) = ∅
δ(q2 , 1) = ∅
Transition table:
Input
0 1
State
q0 {q0 , q1 } {q0 }
q1 − {q2 }
q2 − −
106 / 184
Computation of δ̂(q0 , 00101)
δ̂(q0 , ϵ) = {q0 }
δ̂(q0 , 0) = δ(q0 , 0) = {q0 , q1 }
δ̂(q0 , 00) = δ(q0 , 0) ∪ δ(q1 , 0) = {q0 , q1 } ∪ ∅ = {q0 , q1 }
δ̂(q0 , 001) = δ(q0 , 1) ∪ δ(q1 , 1) = {q0 } ∪ {q1 } = {q0 , q2 }
δ̂(q0 , 0010) = δ(q0 , 0) ∪ δ(q2 , 0) = {q0 , q1 } ∪ ∅ = {q0 , q1 }
δ̂(q0 , 00101) = δ(q0 , 1) ∪ δ(q1 , 1) = {q0 } ∪ {q1 } = {q0 , q2 }
Result: The NFA ends in state {q0 , q2 } after reading input string “00101”.
107 / 184
Language of an NFA
M = (Q, Σ, δ, q0 , F)
L(A) = {w | δ̂(q0 , w) ∩ F ̸= ∅}
That is, the language of M is the set of strings w in Σ∗ such that δ̂(q0 , w)
contains at least one accepting state.
108 / 184
Conversion of NFA to DFA
Theorem: For every NFA, there exists a DFA that accepts the same language.
That is,
L(N) = L(D).
F = {S ⊆ Q̄ | S ∩ F̄ ̸= ∅}
109 / 184
Example 1
Convert to a DFA the following NFA.
Let N = ({q0 , q1 }, {0, 1}, δN , q0 , {q1 })
where δN is given by
δN (q0 , 0) = {q0 , q1 }
δN (q0 , 1) = {q1 }
δN (q1 , 0) = ∅
δN (q1 , 1) = {q0 , q1 }
Solution:
Given NFA
States QN = {q0 , q1 } =⇒ |QN | = 2
Input Σ = {0, 1}
Start state q0N = q0
Final state FN = {q1 }
110 / 184
Example 1
Transition Table:
α 0 1
q0 {q0 , q1 } {q1 }
q1 − {q0 , q1 }
Transition Diagram:
0 1
0,1
start q0 q1
111 / 184
Example 1
Construct a DFA
QD = 2|QN |
QD = 22 = 4
QD = {∅, {q0 }, {q1 }, {q0 , q1 }}
112 / 184
Example 1
δ 0 1
q0 {q0 , q1 } {q1 }
q1 α(q1 , 0) = ∅ α(q1 , 1) = {q0 , q1 }
α(q0 , 0) ∪ α(q1 , 0) α(q0 , 1) ∪ α(q1 , 1)
q0 , q1 = {q0 , q1 } ∪ ∅ = {q1 } ∪ {q0 , q1 }
= {q0 , q1 } = {q0 , q1 }
q∅ q∅ q∅
113 / 184
Example 1
Transition Diagram:
0,1
0
q0 1 q1 1 q0 , q1
start
q∅ 0,1
Trap State
114 / 184
Example 1
D = (QD , Σ, δD , q0D , FD )
where,
QD = {q0 , q1 , (q0 , q1 ), q∅ }
Σ = {0, 1}
q0D = q0
FD = {q1 , (q0 , q1 )} ⊆ QD
115 / 184
Example 2
Design a DFA from the given NFA
0 1
q0 0 q1
start
0 1
q2
Solution:
Given NFA
States QN = {q0 , q1 , q2 } =⇒ |QN | = 3
Input Σ = {0, 1}
Start state q0N = q0
Final state FN = {q0 }
116 / 184
Example 2
Transition Table:
α 0 1
q0 {q0 , q1 } ∅
q1 ∅ {q1 , q2 }
q2 q0 ∅
117 / 184
Example 1
Construct a DFA
QD = 2|QN |
QD = 23 = 8
QD = {∅, {q0 }, {q1 }, {q2 }, {q0 , q1 }, {q0 , q2 }, {q1 , q2 }, {q0 , q1 , q2 }}
118 / 184
Example 2
δ 0 1
{q0 } {q0 , q1 } q∅
α(q0 , 0) ∪ α(q1 , 0) α(q0 , 1) ∪ α(q1 , 1)
{q0 , q1 } = {q0 , q1 } ∪ ∅ = ∅ ∪ {q1 , q2 }
= {q0 , q1 } = {q1 , q2 }
α(q1 , 0) ∪ α(q2 , 0) α(q1 , 1) ∪ α(q2 , 1)
{q1 , q2 } = ∅ ∪ {q0 } = {q1 , q2 } ∪ ∅
= {q0 } = {q1 , q2 }
q∅ q∅ q∅
119 / 184
Example 2
Transition Diagram:
q0 0 q0 , q1
start
1 1
0
0,1 q∅ q1 , q2 1
Trap State
120 / 184
Example 2
D = (QD , Σ, δD , q0D , FD )
where,
QD = {q0 , (q0 , q1 ), (q1 , q2 ), q∅ }
Σ = {0, 1}
q0D = q0
FD = {q0 , (q0 , q1 )} ⊆ QD
121 / 184
Exercise Problem
δN (q0 , 0) = {q0 , q1 }
δN (q0 , 1) = {q1 }
δN (q1 , 0) = {q1 }
δN (q1 , 1) = {q0 }
122 / 184
Exercise 1
Design an equivalent DFA for the following NFA.
Let N = ({q0 , q1 }, {0, 1}, δN , q0 , {q0 })
where δN is given by
δN (q0 , 0) = {q0 , q1 }
δN (q0 , 1) = {q1 }
δN (q1 , 0) = {q1 }
δN (q1 , 1) = {q0 }
Solution:
Given NFA
States QN = {q0 , q1 } =⇒ |QN | = 2
Input Σ = {0, 1}
Start state q0N = q0
Final state FN = {q0 }
123 / 184
Exercise 1
Transition Table:
α 0 1
q0 {q0 , q1 } {q1 }
q1 {q1 } {q0 }
Transition Diagram:
0 0
0,1
start q0 q1
124 / 184
Exercise 1
Construct a DFA
QD = 2|QN |
QD = 22 = 4
QD = {∅, {q0 }, {q1 }, {q0 , q1 }}
125 / 184
Exercise 1
Transition Table:
δ 0 1
q0 {q0 , q1 } {q1 }
q1 {q1 } {q0 }
α(q0 , 0) ∪ α(q1 , 0) α(q0 , 1) ∪ α(q1 , 1)
q0 , q1 = {q0 , q1 } ∪ {q1 } = {q1 } ∪ {q0 }
= {q0 , q1 } = {q0 , q1 }
126 / 184
Exercise 1
Transition Diagram:
start q0 q1 0
q0 , q1 0,1
127 / 184
Exercise 1
D = (QD , Σ, δD , q0D , FD )
where,
QD = {q0 , q1 , (q0 , q1 )}
Σ = {0, 1}
q0D = q0
FD = {q0 , (q0 , q1 )} ⊆ QD
128 / 184
ε − NFA
An ε − NFA is a 5-tuple:
M = (Q, Σ, δ, q0 , F)
where,
Q is a finite set of states.
Σ is a finite set of input symbols.
δ : Q × (Σ ∪ ε) → 2Q is a transition function.
q0 ∈ Q is the start state.
F ⊆ Q is the set of accepting/final state.
Note:
The machine can transition to multiple states on a single input symbol
or ε.
An ε-transition allows the automaton to change states without
consuming any input symbol. 129 / 184
Example 1
Construct an ε-NFA accepting all strings over Σ = {a, b} that contain the
substring “ab”.
Language:
L = { ab, aab, bab, abab, abbb, aaab, baaab, . . . }
This ε-NFA accepts all strings that contain the substring “ab”.
Accept Reject
ab a
aab bb
baab bba
ababbba bbbaaaa
Transition Diagram
a,b a,b
start q0 ε q1 a q2 b q3
130 / 184
Example 1
ε-NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 , q3 }
Inputs Σ = {a, b}
Transition function δ :
Input
a b ε
State
q0 {q0 } {q0 } {q1 }
q1 {q2 } ∅ ∅
q2 ∅ {q3 } ∅
q3 {q3 } {q3 } ∅
Initial State q0 ∈ Q
Language:
L = { a, ba, bba, aa, baa, abba, . . . }
This ε-NFA accepts all strings that end with “a”.
Accept Reject
a b
ba bb
bba ab
Transition Diagram
a,b
start q0 ε q1 a q2
132 / 184
Example 2
ε-NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 }
Inputs Σ = {a, b}
Transition function δ :
Input
a b ε
State
q0 {q0 } {q0 } {q1 }
q1 {q2 } ∅ ∅
q2 ∅ ∅ ∅
Initial State q0 ∈ Q
133 / 184
ε - Closure
Algorithm:
1 Initialize: εc (q) = {q}
2 For each p ∈ εc (q), if there is a transition δ(p, ε) = r, then add r to the
closure.
134 / 184
Example: NFA with ε-Transitions
q0 ε q1 ε q2
start
135 / 184
ε-Closure of q0
Step 2: q0 →
ε
− q1 add q 1
Step 3: q1 →
ε
− q2 add q 2
Result: εc (q0 ) = { q0 , q1 , q2 }
136 / 184
ε-Closure of q1
Step 2: q1 →
ε
− q2 add q 2
Result: εc (q1 ) = { q1 , q2 }
137 / 184
ε-Closure of q2
Result: εc (q2 ) = { q2 }
138 / 184
Summary Table of ε-Closures
State εc
q0 { q0 , q1 , q2 }
q1 { q1 , q2 }
q2 { q2 }
139 / 184
ε - NFA to NFA conversion: Example 1
Construct the following ε - NFA and find it’s equivalent NFA.
0 1 2
start q0 ε q1 ε q2
Solution:
M ={Q, Σ, δ, q0 , F}
Q ={q0 , q1 , q2 }
Σ ={0, 1, 2, ε}
q0 ={q0 } ∈ Q
F ={q2 } ⊆ Q
140 / 184
ε - NFA to NFA conversion: Example 1
Transition Table
δ 0 1 2 ε
q0 {q0 } − − {q1 }
q1 − {q1 } − {q2 }
q2 − − {q2 } −
εc (q0 ) ={q0 , q1 , q2 }
εc (q1 ) ={q1 , q2 }
εc (q2 ) ={q2 }
141 / 184
ε - NFA to NFA conversion: Example 1
142 / 184
ε - NFA to NFA conversion: Example 1
143 / 184
ε - NFA to NFA conversion: Example 1
144 / 184
ε - NFA to NFA conversion: Example 1
145 / 184
ε - NFA to NFA conversion: Example 1
δ̂ 0 1 2
q0 {q0 , q1 , q2 } {q1 , q2 } {q2 }
q1 ∅ {q1 , q2 } {q2 }
q2 ∅ ∅ {q2 }
146 / 184
ε - NFA to NFA conversion: Example 1
0 1 2
0,1 1,2
start q0 q1 q2
0,1,2
147 / 184
ε - NFA to NFA conversion: Example 1
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 }
Inputs Σ = {0, 1, 2}
Initial State q0 = q0 ∈ Q
148 / 184
ε - NFA to DFA conversion: Example 1
Construct the following ε - NFA and find it’s equivalent DFA.
0 1 2
start q0 ε q1 ε q2
Solution:
M ={Q, Σ, δ, q0 , F}
Q ={q0 , q1 , q2 }
Σ ={0, 1, 2, ε}
q0 ={q0 } ∈ Q
F ={q2 } ⊆ Q
149 / 184
ε - NFA to DFA conversion: Example 1
Transition Table
δ 0 1 2 ε
q0 {q0 } − − {q1 }
q1 − {q1 } − {q2 }
q2 − − {q2 } −
150 / 184
ε - NFA to DFA conversion: Example 1
Processing States
151 / 184
ε - NFA to DFA conversion: Example 1
152 / 184
ε - NFA to DFA conversion: Example 1
153 / 184
ε - NFA to DFA conversion: Example 1
Process the state C:
154 / 184
ε - NFA to DFA conversion: Example 1
δ′ 0 1 2
A = {q0 , q1 , q2 } A = {q0 , q1 , q2 } B = {q1 , q2 } C = {q2 }
B = {q1 , q2 } q∅ B = {q1 , q2 } C = {q2 }
C = {q2 } q∅ q∅ C = {q2 }
q∅ q∅ q∅ q∅
155 / 184
ε - NFA to DFA conversion: Example 1
0 1
1
start A B
2 0
2
2 C q∅ 0,1,2
0,1
Trap State
156 / 184
ε - NFA to DFA conversion: Example 1
DFA:
M = {Q, Σ, δ, q0 , F}
Inputs Σ = {0, 1, 2}
157 / 184
Exercise Problems
1. Construct the following ε - NFA and find it’s equivalent NFA.
0 1 0,1
start q0 ε q1 ε q2
0 1 0,1
start q0 ε q1 ε q2
158 / 184
Exercise 1
Construct the following ε - NFA and find it’s equivalent NFA.
0 1 0,1
start q0 ε q1 ε q2
Solution:
M ={Q, Σ, δ, q0 , F}
Q ={q0 , q1 , q2 }
Σ ={0, 1, ε}
q0 ={q0 } ∈ Q
F ={q2 } ⊆ Q
159 / 184
Exercise 1
Transition Table
δ 0 1 ε
q0 {q0 } − {q1 }
q1 − {q1 } {q2 }
q2 {q2 } {q2 } −
εc (q0 ) ={q0 , q1 , q2 }
εc (q1 ) ={q1 , q2 }
εc (q2 ) ={q2 }
160 / 184
Exercise 1
Extended transition function
161 / 184
Exercise 1
162 / 184
Exercise 1
163 / 184
Exercise 1
δ̂ 0 1
q0 {q0 , q1 , q2 } {q1 , q2 }
q1 {q2 } {q1 , q2 }
q2 {q2 } {q2 }
164 / 184
Exercise 1
0 1 0,1
0,1 0,1
start q0 q1 q2
0,1
165 / 184
Exercise 1
NFA:
M = {Q, Σ, δ, q0 , F}
States Q = {q0 , q1 , q2 }
Inputs Σ = {0, 1}
Initial State q0 = q0 ∈ Q
166 / 184
Exercise 2
Construct the following ε - NFA and find it’s equivalent DFA.
0 1 0,1
start q0 ε q1 ε q2
Solution:
M ={Q, Σ, δ, q0 , F}
Q ={q0 , q1 , q2 }
Σ ={0, 1, ε}
q0 ={q0 } ∈ Q
F ={q2 } ⊆ Q
167 / 184
Exercise 2
Transition Table
δ 0 1 ε
q0 {q0 } − {q1 }
q1 − {q1 } {q2 }
q2 {q2 } {q2 } −
168 / 184
Exercise 2
Extended transition function
169 / 184
Exercise 2
170 / 184
Exercise 2
171 / 184
Exercise 2
δ̂ 0 1
A = {q0 , q1 , q2 } A = {q0 , q1 , q2 } B = {q1 , q2 }
B = {q1 , q2 } C = {q2 } B = {q1 , q2 }
C = {q2 } C = {q2 } C = {q2 }
172 / 184
Exercise 2
0 1 0,1
1 0
start A B C
173 / 184
Exercise 2
DFA:
M = {Q, Σ, δ, q0 , F}
States Q = {A, B, C}
Inputs Σ = {0, 1}
Initial State q0 = q0 ∈ Q
174 / 184
Minimization of DFA
Procedure:
Start from the initial state and see which states can be reached.
175 / 184
Minimization of DFA
176 / 184
Example 1
Minimize the following DFA with C and D are the final states.
δ Input 0 Input 1
→A B C
B A D
C D A
D C B
Solution:
Given DFA
States: Q = {A, B, C, D}
Alphabet: Σ = {0, 1}
Start state: A
Transition Diagram:
0
start A B
0
1 1 1 1
0
C D
0
178 / 184
Example 1
179 / 184
Example 1
P0 = {A, B}, {C, D} .
180 / 184
Example
Step 3: Refine Partitions
182 / 184
Example 1
183 / 184
Example 1
M = {Q, Σ, δ, q0 , F}
States Q = [A, B], [C, D]
Input Σ = {0, 1}
184 / 184