Module 1 - PART 2
Module 1 - PART 2
Examples:
1. Let L be the language of all strings consisting of n 0’s followed by n 1’s:
L = {, 01, 0011, 000111,…}
2. Let L be the language of all strings of with equal number of 0’s and 1’s:
L = {, 01, 10, 0011, 1100, 0101, 1010, 1001,…}
Binary relations on strings
1. Prefix - u is a prefix of v if there is a w such that v = uw.
– Examples:
• is a prefix of 0 since 0 = 0
• apple is a prefix of appleton since appleton = apple.ton
2. Suffix - u is a suffix of v if there is a w such
that v = wu.
– Examples:
• is a suffix of 0 since 0 = 0
Let ∑ be an alphabet.
– ∑k = the set of all strings of length k
– ∑* = ∑0 U ∑1 U ∑2 U … kleene closure
– ∑+ = ∑1 U ∑2 U ∑3 U … positive closure
6
Finite Automata
• Formal Definition ,
• Languages of FA,
• Two types (NFA,DFA) ,
• String Processing
Finite Automata
Finite Automata is an abstract model of a computer.
Σ is the alphabet
: Q Σ → (2Q or Q) is the transition function
q0 is in Q is the start state or initial state
b a
a,b
a b
q0 q1 q2
Tabular representation:
Transition table
• It consists of Rows and columns.
• Rows indicate state and columns indicate symbol.
• The entries of the table indicate the output state.
• The arrow and star symbols are used to point out the starting
and final states respectively.
a
b a,b
δ a b
→q0 q1 q0 a b
q0 q1 q2
q1 q1 q2
* q2 q2 q2
Functional Representation - Transition functions
•The name of the function is δ.
• The input parameters are q,a where q is a state and a is a symbol. The function returns a state p.
δ(q0 , a) = q1
δ(q0 , b) = q0
a
b a,b
δ(q1 , a) = q1
a b
δ(q1 , b) = q2 q0 q1 q2
δ(q2 , a) = q2
δ(q2 , b) = q2
Language of an Automata
b a
a,b
a b
q0 q1 q2
= q2 is in F.
b
q0 q0 b q0 a q1 b q2 b Q2 is
There is a path from starting state to final state. Therefore thein F
given string is accepted
String Processing in DFA
11
q1 1
0,1
0 1
0111 111
q0 q12
0 0
1
Read string left to right q3
The machine accepts a string if the process ends
in a double circle
String Processing in NFA
0 1 2
Test whether the
→*q0 {q0, q1, q2 } {q1, q2 } {q2 } strings 01122, 1221
*q1 Ф {q1, q2 } {q2 } are accepted by M.
*q2 Ф Ф
{q2 }
= q2 F
0 q0 1 q1 1 q1 2 q2 2
q0 q2F
0 q1 1 q2 1 q2
0
q2
There is at least one path from starting state to final state. Therefore
the given string is accepted.
String Processing in NFA
0 1 2
Test whether the
→*q0 {q0, q1, q2 } {q1, q2 } {q2 } strings 1221 is
*q1 Ф {q1, q2 } {q2 } accepted by M.
*q2 Ф Ф
{q2 }
q0 1 q 2 q 2 q 1
1 2 2
1
q2 2 q 2 q 1
2 2
L(M)={0,10,100,110........}
Problem-2: Construct DFA, which accepts set of all strings over {0, 1} which
interpreted as binary number is divisible by 3.
Problem-3: Construct DFA, which accepts set of all strings over {0, 1} which
interpreted as binary number is divisible by 4.
Problem-4: Construct DFA, which accepts set of all strings over {0, 1} which
interpreted as binary number is divisible by 5.
NFA
Non-Deterministic: if there is zero or more output states in any of
the transition functions of an automata then that automata is called
Non-Deterministic Finite Automata (NFA). : Q Σ → 2Q .
1. The Finite Automata are called NFA when there exist many
paths for specific input from the current state to the next state.
2. It is easy to construct NFA than DFA for a given regular
Language.
3. Every NFA is not DFA, but each NFA can be translated into
DFA.
4. NFA is defined in the same way as DFA, but with two
Exceptions.
i) It contains multiple next states
ii) It contains Epsilon (ε) transitions
DFA is a special case of the NFA in which for each state there is
unique transition on each input symbol.
Example NFA
Design a NFA for the following languages over
the alphabet {a,b}
• The set of all strings ending in aa.
• The set of all strings with the substring aba.
• The set of all strings beginning with bb.
• The set of all strings with even number of a’s.
• The set of all strings with even number of b’s.
• The set of all strings with odd number of a’s.
• The set of all strings with odd number of b’s.
• The set of all strings whose third symbol from the right end is b
• The set of all strings whose third symbol from the left end is b.
The set of all strings ending in aa.
δ : Transition diagram:
a,b
a a
q0 q1 q2
Transition table
Transition function
The set of all strings with the substring aba
a,b a,b
q0 a q1 b a
q2 q3
The set of all strings beginning with bb
b a b
q0 q1
a
The set of all strings with even number of b’s
a b a
q0 q1
b
The set of all strings with odd number of a’s
b b
a
q0 q1
a
The set of all strings whose third symbol
from the right end is b
• NFA M=(Q, Σ, δ, q0, {q3})
• where Q={ q0, q1 q2, q3 } Σ={a,b}
• δ : Transition diagram:
a,b
b a,b a,b
q0 q1 q2 q3
The set of all strings whose third symbol
from the left end is b
• NFA M=(Q, Σ, δ, q0, {q3})
where Q={ q0, q1 , q2, q3 } Σ={a,b}
• δ : Transition diagram:
a,b
a,b a,b b
q0 q1 q2 q3
Automata for
• If the automata is given for a language L , then the automata
for can be easily constructed by changing all the non-
final states to final states and final states to non-final states.
• Given: The FA of L= {w | w consists of 10 as substring}
• M(L) =(Q, Σ, δ, A, {C}) where Q={ A, B ,C } Σ={a,b}
• δ : Transition diagram:
1 0,1
0
A B C
1 0
a
(p,r ps
a
b b b b
a
qr qs
Class Tutorial
1. Construct the FA for the language {0n| n mod 3 =2,
n≥0}
2. Construct the FA for the language {0n| n mod 3 =1,
n≥0}
3. Construct the FA for the language {0n| n mod 3 =0,
n≥0}
4. Construct the FA for the language {0n| n mod 2 =1,
n≥0}
5. Construct the FA for the language {0n| n mod 2 =0,
n≥0}
Class Tutorial
Design a NFA for the following languages over the alphabet {0,1}
i. The set of all strings ending in 01.
ii. The set of all strings with the substring 11.
iii. The set of all strings beginning with 010.
iv. The set of all strings with even number of 1’s.
v. The set of all strings with odd number of 0’s.
vi. The set of all strings whose second symbol from the right end is
0
vii. The set of all strings with even number of 0’s and odd number of
1’s
viii. The set of all strings with even number of zero’s and even
number of 1’s
ix. The set of all strings with odd number of zero’s and odd number
of 1’s
x. The set of all strings with odd number of zero’s and even
number of 1’s
Converting NFA to DFA
(Construct DFA from NFA):
‘Lazy Evaluation’-
• In this method the transition functions are
generated only for reachable states.
Subset construction method(Lazy)
INPUT : NFA
OUTPUT : DFA
Step 1 :
Include the starting state of NFA (q 0) in DFA as starting state of DFA.
Step 2 :
Find the transition for all the symbols from q 0
Step 3 :
If the output state is new state, include it in DFA and find the
transition for all the symbols from that state.
Step 4 : Repeat step3 until there are no more new states
Step 5 :The state which includes final state of NFA is the final state of
DFA.
NFA: Convert NFA to DFA
0,1
δN 0 1
0 1
q0 q1 q2 →q0 {q0, q1 } {q0}
q1 - {q2}
Step 1 : Include q0 * q2 - -
Step 2 : Find transitions for 0,1 from q0
New state
New state
Step 4 : Repeat step 3 for newstate
DFA 1 0
δD 0 1
0 1
→q0 {q0, q1 } {q0}
{q0, q1 }
q0 {q0, q2 }
{q0, q1 } {q0, q1 } { q0,q2}
1 0
* { q0,q2} {q0, q1 } {q0}
Convert NFA to DFA
1
N a b 3 N 0 1
→q0 { q0 ,q1 } { q0} →p { q ,s } { q}
q1 q2 q1 q {r} {q,r}
r {s} {p}
q2 q3 q3
*s {p}
* q3 - q2
2
N 0 1 4
N 0 1
→p { p ,q } { p} →p { p,q } { p}
q {r} {r} q {r,s} {t}
r {s} r {p,r} {t}
*s {s} {s} *s
*t
ε-NFA or NFA with ε- Transitions.
• A Non-Deterministic finite automaton with ε-
Transitions (NFA) is represented by 5-tuples.
i.e. M = (Q, Σ, δ, q0, F) :
• Q is a finite non-empty set of states
• Σ is a finite non-empty set of symbols ( an alphabet)
• δ : QX Σ{ε} → 2Q (subset of Q) is the transition
function
• q0 Q is the start state
• F Q is a set of final states
Example ε-NFA- L={0l1m2n|l,m,n>=0}
ε 0 1 2
→q0 q1 q0 Ф Ф
q1 q2 Ф q1 Ф
*q2 Ф Ф Ф q2
0 1
2
q0 ε q1 ε q2
ε-closure
• Epsilon closure of a state is the set of all states
that are reachable by following the transition
function from the given state through ε edge.
• ε-Closure (q0) = = { q0 , q1, q2}
• ε-Closure (q1) = = { q1, q2}
• ε-Closure (q2 ) = = {q2}
String processing in ε-NFA
Step 1: Compute ε-Closure [states
ε a b c that can be reached by traveling
along zero or more ε arrows] for
→p {q} {p} Ф Ф
all states .
q {r} Ф {q} Ф
ε-Closure (p) = {p,q,r}
ε-Closure (q) = {q,r}
*r Ф Ф Ф {r} ε-Closure (r ) = {r}
Step2: Start with ε-closure (p)= {p,q,r}, where p is the starting state of given ε –NFA
c
q {p} {q} {r} c ε
Ф b
r
Existing
state
Newstate
SKCET-CSE FLAT : UNIT - I (PART 3) 53
Conversion Contd.
a b c
{p,q}
*{p,q,r}
SKCET-CSE FLAT : UNIT - I (PART 3) 54
Conversion Contd.
Existing
state
Newstate
SKCET-CSE FLAT : UNIT - I (PART 3) 55
Conversion Contd.
a b c
*{p,q,r}
SKCET-CSE FLAT : UNIT - I (PART 3) 56
Conversion Contd.
Existing
state
Existing
state
SKCET-CSE FLAT : UNIT - I (PART 3) 57
Conversion Contd.
a b c
ε a b c
→p {q,r} Ф {q} {r}
q Ф {p} {r} {p,q}
*r Ф Ф Ф Ф
String Processing
ε +/- . 0-9
→q {q1} {q1 } Ф Ф
0
*q5 Ф Ф Ф Ф
SKCET-CSE 60
Minimizing DFA (Partitioning method)
• partition the set of states into two groups:
– G1 : set of accepting states
– G2 : set of non-accepting states
2
G1 = {2}
a
G2 = {1,3}
1 b a
b G2 cannot be partitioned because
3
δ(1,a)=2(G1) δ (1,b)=3 (G2)
b
δ(3,a)=2(G1) δ(3,b)=3 (G2)
So, the minimized DFA (with minimum states)
b
a a
{1,3} {2}
b
Construct a minimized DFA for the given DFA
1 2
δ a b
δ 0 1
-> A B E
-> A B A
B C F B A C
*C D H C D B
D E H *D D A
E F I E D F
*F G B F G E
G H B G F G
H I C H G D
*I A E