Chapter Two - FSM
Chapter Two - FSM
Campus
Department of Computer
Automata and complexity
Science
theory
is a transition
is a start state
is a final state
• is defined as
1 1 0
𝑞0 0
𝑞1 0
𝑞2
1
06
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 9
FA Examples
• The string with next to last symbol as 0.
0
1
0 𝐶
𝐴 0
𝐵 1
1
0 𝐷
1
0 1
𝐴 1
𝐵 𝐶
1
as follows:
1. For any
2. For any and ,
1. calculate
9
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 17
String Acceptance by DFA
Example: Consider the following DFA
M = ({q0, q1, q2}, {a, b}, , q0, {q1, q2} ) where (q0, a) = q1,
(q0, b) = q2, (q2, a) = q2, (q1, b) = q1, (q1, a) = q2
A. Draw transition diagram of M
B. Check if the following strings are accepted by M
i) x = abbb ii) x = abba iii) x = bab
10
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 18
String Acceptance by DFA
b)
I) *(q0, abbb) = *(T(q0,a), bbb) = ( *(q0,abb), b)
= *(q1, bbb) = ( ( ( ( *( q0, ,a),b),b), b) = ( ( (
( q0,a),b),b), b) = ( ( ( ( q0,a),b),b), b)
= *(T(q1,b), bb) = ( ( (q1,b),b), b)
= *(q1, bb) = ( (q1,b), b)
= *(T(q1,b), b) = ( q1, b)
= (q1, b)
= q1
So, abbb is accepted by M because q1 e {q1, q2}
Following the same step as above it can be proved that (ii) abba is accepted and
(iii) bab is rejected
11
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 19
Language Acceptance by DFA
Definition: The language accepted by DFA M = (Q, Σ, , q0, F) is the set of all
strings on Σ accepted by M. Formally,
L(M) = {w ϵ Σ * : *(q0, w) ϵ F} and
The language which is not accepted or rejected by M is given as :
[L(M)]’ = {w ϵ Σ * : *(q0, w) ≠ F}
12
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 20
Language Acceptance by DFA
Example: Find a deterministic finite accepter that recognizes the set of all
strings on Σ = {a, b} starting with the prefix ab.
a,b
a
𝑞0 𝑞1 b
𝑞2
is the set of states, is the alphabet, is the initial state and is the set
of accepting states.
The major difference between DFA and NFA
The range of the transition function is 2Q
Empty string, λ , is allowed to be an argument of δ
• is defined as
𝑞1
1 1
𝑞0 0
𝑞4
1 0
𝑞2 1 𝑞3
Mekonen M. #CoSc3071 Unit 2 – Finite Automata 24
Nonrecursive Definition of for NFA
• For an NFA , and any . For any and any is the set of all states for
which there is a sequence of states satisfying
• is defined as
0, 1
𝑞0 1
𝑞1 0, 1
𝑞2 0, 1
𝑞3
¿{𝑞 0 , 𝑞1 } ∪ {𝑞 2 }
¿ {𝑞 0 , 𝑞1 , 𝑞 𝑞 0 2𝑞
1
}1
0, 1
𝑞2 0, 1
𝑞3
¿ ¿ 𝑟 ∈ {𝒒 𝟎 }𝛿 (𝑟 , 1)
¿ 𝛿 ( 𝑞0 , 0, 1
1 )
¿ {𝑞 0 , 𝑞1 }
𝑞0 1
𝑞1 0, 1
𝑞2 0, 1
𝑞3
21
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 37
Conversion of NFA to DFA
In NFA, when a specific input is given to the current state, the
machine goes to multiple states. It can have zero, one or more than
one move on a given input symbol.
On the other hand, in DFA, when a specific input is given to the
current state, the machine goes to only one state. DFA has only one
move on a given input symbol.
Let, M = (Q, ∑, δ, q0, F) is an NFA which accepts the language L(M).
There should be equivalent DFA denoted by M' = (Q', ∑', q0', δ', F')
such that L(M) = L(M').
2
a b
1 {2,3} {4}
b 2 {} {4}
1 4
3 {4} {3}
a a 4 {} }
3
Transition Table
b
NFA
¿ {3 , 4 } b
𝛿 1( 4 , 𝑎)= { ∅ } a b
1 2,3 3,4
𝛿 1( 4 , 𝑏)={ ∅ }
a
b
𝛿 1 ( { 3 , 4 } , 𝑏 ) = 𝛿 ( 3 , 𝑏 ) ∪ 𝛿 (4 , 𝑏) b
1 4
¿ {3 }
𝛿 1( 3 , 𝑎)={4 } a a
3
𝛿 1( 3 , 𝑏)={3 } b
a b
1 2,3 3,4
a a
b b
a
4 3
b
Mekonen M. #CoSc3071 Unit 2 – Finite Automata 42
Example 2: Conversion from NFA to DFA
δ Input
State
0, 1
𝑞0 1
𝑞1 0, 1
𝑞2 0, 1
𝑞3
0 𝑞0 𝑞3
𝑞0 1
𝑞1 0, 1 𝑞 2 0, 1 𝑞 3 0
𝑞0 𝑞2 1
0
𝑞0 1
𝑞0 𝑞1 𝑞0 𝑞1 𝑞3
1
𝛿 1 ( {𝑞0 }, 0 ) ={𝑞0 } 𝑞0 𝑞1 𝑞2
𝛿1 ( {𝑞0 }, 1 )={𝑞 0 , 𝑞 1 }
𝛿 1 ( {𝑞0 , 𝑞1 }, 0 ) = 𝛿 ( { 𝑞0 } , 0 ) ∪ 𝛿 ( { 𝑞1 } , 0 ) ={ 𝑞 0 } ∪ { 𝑞 2 }={𝑞 0 , 𝑞 2 }
𝛿 1 ( {𝑞0 , 𝑞1 },1 ) = 𝛿 ( { 𝑞 0 } , 1 ) ∪ 𝛿 ( { 𝑞1 } , 1 ) ={ 𝑞 0 ,𝑞 1 } ∪ { 𝑞2 }={𝑞0 , 𝑞1 ,𝑞 2 }
𝛿 1 ( {𝑞0 , 𝑞 2 }, 0 ) = 𝛿 ( { 𝑞 0 } , 0 ) ∪ 𝛿 ( { 𝑞 2 } , 0 ) = {𝑞 0 } ∪ { 𝑞 3 } ={𝑞 0 ,𝑞 3 }
𝛿 1 ( {𝑞0 , 𝑞 2 },1 ) = 𝛿 ( { 𝑞 0 } , 1 ) ∪ 𝛿 ( { 𝑞2 } ,1 ) = { 𝑞0 , 𝑞1 } ∪ { 𝑞 3 } ={𝑞 0 ,𝑞 1 , 𝑞 3 }
0
𝑞0 𝑞3
1
0
𝑞0 𝑞2 1
0 𝑞0 𝑞1 𝑞3
𝑞0 1
𝑞0 𝑞1
1
0 𝑞0 𝑞2 𝑞3 0, 1
𝑞0 𝑞1 𝑞2 1
𝑞0 𝑞1 𝑞2 𝑞3 𝑞0 1
𝑞1 0, 1
𝑞2 0, 1
𝑞3
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 45
Example 2: Conversion from NFA to DFA
𝛿 1 ( {𝑞0 , 𝑞1 , 𝑞3 }, 0 ) = 𝛿 ( { 𝑞0 } , 0 ) ∪ 𝛿 ( { 𝑞 1 } ,0 ) ∪ 𝛿 ( { 𝑞 3 } , 0 ) ={𝑞0 , 𝑞2 }
𝛿 1 ( {𝑞0 , 𝑞1 , 𝑞3 },1 ) = 𝛿 ( { 𝑞 0 } , 1 ) ∪ 𝛿 ( { 𝑞1 } , 1 ) ∪ 𝛿 ( { 𝑞 3 } ,1 )= {𝑞 0 ,𝑞 1 , 𝑞2 }
𝛿 1 ( {𝑞0 , 𝑞 2 , 𝑞3 }, 0 ) = 𝛿 ( { 𝑞 0 } , 0 ) ∪ 𝛿 ( { 𝑞 2 } , 0 ) ∪ 𝛿 ( { 𝑞 3 } , 0 ) = {𝑞 0 , 𝑞 3 }
𝛿 1 ( {𝑞0 , 𝑞 2 , 𝑞3 },1 )= 𝛿 ( {𝑞 0 } , 1 ) ∪ 𝛿 ( { 𝑞 2 } ,1 ) ∪ 𝛿 ( { 𝑞 3 } , 1 ) ={𝑞 0 ,𝑞 1 ,𝑞 3 }
0
0
𝑞0 𝑞3
1
0
𝑞0 𝑞2 1
0 0 𝑞0 𝑞1 𝑞3 0
𝑞0 1
𝑞0 𝑞1 1
1
1
0 𝑞0 𝑞2 𝑞3 0, 1
𝑞0 𝑞1 𝑞2 1
𝑞0 𝑞1 𝑞2 𝑞3 𝑞0 1
𝑞1 0, 1
𝑞2 0, 1
𝑞3
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 46
Example 2: Conversion from NFA to DFA 0, 1
𝛿1 ( {𝑞0 ,𝑞1 ,𝑞2 ,𝑞3 },0 ) =𝛿 ( {𝑞0 } ,0 ) ∪𝛿 ( {𝑞1 } ,0 ) ∪𝛿 ( {𝑞2 } ,0 ) ∪𝛿 ( {𝑞3 } ,0 )={𝑞0 ,𝑞2 ,𝑞3 }
𝑞 0 𝑞1 1 0, 1
𝑞 2 0, 1 𝑞 3
𝛿1 ( {𝑞0 ,𝑞1 ,𝑞2 ,𝑞3 },1 ) =𝛿 ( {𝑞0 } ,1 ) ∪𝛿 ( {𝑞1 } ,1 ) ∪𝛿 ( {𝑞2 },1 ) ∪𝛿 ( {𝑞3 } ,1 )={𝑞0 ,𝑞1 ,𝑞2 ,𝑞3 }
0
• As now no new states are
obtained, the process 0
𝑞0 𝑞3
1
stops and we need to 0
𝑞0 𝑞2 1
define
states.
the accepting 0 0 𝑞0 𝑞1 𝑞3 0
26
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 50
Conversion of NFA with ε to DFA by ε-closure
NFA with ε move: If any FA contains ε transaction or move, the finite automata
is called NFA with ε move.
ε-closure: ε-closure for a given state A means a set of states which can be
reached from the state A with only ε(null) move including the state A itself.
b
𝐶 30
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 55
Conversion of NFA with ε to DFA by ε-closure – example1
δ'(C, a) = ε-closure {δ((1,2,4,5,6,7), a) }
= ε-closure {δ(1, a) ∪ δ(2, a) ∪ δ(4, a) ∪ δ(5, a) ∪ δ(6, a) ∪ δ(7, a) }
= ε-closure {3 ∪ 8}
= {1,2,3,4,6,7,8} - State B
δ'(C, b) = ε-closure {δ((1,2,4,5,6,7), b) }
= ε-closure {δ(1, b) ∪ δ(2, b) ∪ δ(4, b) ∪ δ(5, b) ∪ δ(6, b) ∪ δ(7, b) }
= ε-closure {5} a
b
𝐷
= {1,2,4,5,6,7} - State C
𝐵
𝐴 a
a
b
𝐶
b 30
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 56
Conversion of NFA with ε to DFA by ε-closure – example1
δ'(D, a) = ε-closure {δ((1,2,4,5,6,7,9), a) }
= ε-closure {δ(1, a)∪ δ(2, a)∪ δ(4, a) ∪ δ(5, a) ∪ δ(6, a) ∪ δ(7, a) ∪ δ(9,
a) }
= ε-closure {3 ∪ 8}
= {1,2,3,4,6,7,8} - State B
δ'(D, b) = ε-closure {δ((1,2,4,5,6,7,9), b) }
= ε-closure {δ(1, b)∪ δ(2, b) ∪ δ(4, b) ∪ δ(5, b) ∪ δ(6, b) ∪ δ(7, b) ∪ δ(9, b) }
a
= ε-closure {5,10} b
𝐷
= {1,2,4,5,6,7,10} Mark this States as a E 𝐵 a b
𝐴 a
a
𝐸
b
𝐶
b 30
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 57
Conversion of NFA with ε to DFA by ε-closure – example1
δ'(E, a) = ε-closure {δ((1,2,4,5,6,7,10), a) }
= ε-closure {δ(1, a)∪ δ(2, a)∪ δ(4, a) ∪ δ(5, a) ∪ δ(6, a) ∪ δ(7, a) ∪ δ(10,
a) }
= ε-closure {3 ∪ 8}
= {1,2,3,4,6,7,8} - State B
δ'(E, b) = ε-closure {δ((1,2,4,5,6,7,10), b) }
= ε-closure {δ(1, b)∪ δ(2, b) ∪ δ(4, b) ∪ δ(5, b) ∪ δ(6, b) ∪ δ(7, b) ∪ δ(10, b) }
a
= ε-closure {5} b
𝐷
= {1,2,4,5,6,7} - State C 𝐵 a b
𝐴 a
a
a
𝐸
𝐸
𝐶
b b
b 30
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 58
Conversion of NFA with ε to DFA by ε-closure – Exercise
Exercise 1: Convert the following NFA to DFA. States 2 and 4 are final states
33
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 59
Conversion of NFA to DFA - exercises
Exercise 2: Convert the given NFA to its equivalent DFA
34
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 60
DFA Minimization
Questions of DFA size:
? Given a DFA, can we find one with fewer states that accepts the same language?
? What is the smallest DFA for a given language?
? Is the smallest DFA unique, or can there be more than one "smallest" DFA for the same
language?
All these questions have neat answers…
Unreachable states, like some of those introduced by the subset construction,
can obviously be eliminated
Even some of the reachable states may be redundant…
DFA minimization means converting a given DFA to its equivalent DFA but with a
minimum number of states.
35
Mekonen M. # CoSc3071 Unit 2 – Finite Automata 61
DFA Minimization using Equivalence Theorem
• If X and Y are two states in a DFA, we can combine these two states into {X, Y} if they are not
distinguishable i.e. equivalent or indistinguishable.
• Two states are equivalent states if
• δ (X, w) ∈ F and δ (Y, w) ∈ F OR
• δ (X, w) ∈ F and δ (Y, w) ∈ F
1. Delete all unreachable states from initial states
2. Draw state transition table
3. Find out equivalent set
4. Draw minimized DFA
Note:
0-equivalence set - separate non-final states from final states
n-equivalence set – we take information from previous(n-1) equivalence set. i.e. we check on
seeing on input symbol, are they going to same state or different state.
if they are in same state group in previous equivalent set – write in same group
otherwise - make separate group
• Follow this procedure until you get a point where no change in the state group
A)
C)