FLAT - Ch-2
FLAT - Ch-2
Finite Automata
1
A Finite-State Machine
• Finite automata – is a machine which takes and reads an
input string one by one and then after the input is
completely read, it decides to whether the string is
accepted or not accepted.
• The automaton consists of states and transitions.
• As the automaton sees a symbol of input it makes a
transition or jump to another state according to its
transition function.
• A finite state machine is a mathematical model of a
system, with discrete inputs and outputs.
2
A Finite-State Machine…
• Finite Automata FA:
• a finite set of states
• a set of transitions (edges)
• a start state
• a set of final states
Defining a FA is a kind of programming.
• Problem definition
• Includes defining possible actions & accepting condition.
• States structure of program
• Includes designating which are initial & final.
• Transitions program
3
Types Finite Automata
4
Deterministic Finite Accepters (dfa) Initial state q0
Input file
1 2 3 4 5 6 7 8 9 …
Definition
A deterministic finite accepter or dfa Control unit
(transition function δ)
is defined by the quintuple
M = (Q, , , q0 , F ) δ(q0 ,σ1 ) = q1
where State q1
Q is a finite set of internal states, Input file
is a finite set of symbols called the input 1 2 3 4 5 6 7 8 9 …
alphabet,
: Q → Q is a function called Control unit
(transition function δ)
th e transition function,
q 0 Q is the initial state,
F Q is a set of final states. 5
Initial state q0
Input file
1 2 3 4 5 6 7 8 9 … State qn −1
Input file
1 2 3 4 5 6 7 8 9 … n
Control unit
(transition function δ)
Control unit
(transition function δ)
δ(q0 ,σ1 ) = q1
State q1
If the end of string is reached and
Input file
δ(q n −1,σ n ) = qn is a final state,
1 2 3 4 5 6 7 8 9 …
then the string is accepted.
Otherwise, the string is rejected.
Control unit
(transition function δ)
6
Deterministic Finite Automata…
• DFA M
• Q = {q0, q1}
• = {a, b}
• F = {q1}
• The transition function is given in a tabular form called the
transition table.
• (q0, a) = q1 (q0, b) = q0
• (q1, a) = q1 (q1, b) = q0
7
Deterministic Finite Automata…
• A DFA M can be considered to be a language acceptor.
8
Deterministic Finite Automata…
• The DFA M accepts the set of strings over {a, b} that contain the
substring bb.
• M : Q = {q0, q1, q2}, = {a, b}, F = {q2}
• The transition function is given in a tabular form called the
transition table.
• (q0, a) = q0 (q0, b) = q1
• (q1, a) = q0 (q1, b) = q2
• (q2, a) = q2 (q2, b) = q2
• Is abba L(M)? Yes, since the computation halts in state q2, which
is a final state.
• Is abab L(M)? No, since the computation halts in state q1, which
is NOT a final state.
9
State Diagrams and Examples
11
• Transition graphs
Deterministic finite accepter
M = (Q,, δ, q0 , F)
Transition
0 0 1 function: δ(q1 ,1) =q 2
q0 q1 q2 0
1
1
Initial vertex Final vertex
Example
The above transition graph represents the dfa
Definition
The language accepted by
a dfa M = (Q,, , q0 F ) is
the set of all string on Σ
accepted by M. In formal notation,
L(M) = {w * : * (q0 , w) F}.
Let w = a1a2 an .
If (q1 , a1 ) = q2 , (q2 , a2 ) = q3 ,, (qn , an ) = qn +1 ,
then we write δ * (q1,w) = qn +1.
Especially,δ * (q,) = q.
13
Example: Consider the dfa in the following transition graph.
q0 q1 q2 a,b
b a,b
Solution: Trap state
L = {a nb: n 0}
Example: Find a dfa that recognizes the set of all strings on
= {a, b} starting with the prefix ab.
Solution:
a q1 b q2
q0 a,b
b Trap state
q3 a,b 14
State Diagrams and Examples…
b b b
a a a
q0 q1 q2 q3
15
State Diagrams and Examples….
b
b
a a a q3
q0 q1 q2
16
State Diagrams and Examples…
• Draw a state diagram for DFA M that accepts the set of
strings over {a, b} that contain the substring bb
a a, b
b b
q0 q1 q2
17
State Diagrams and Examples…
• The DFA
b
a a
q0 q1 q2
b
• accepts (b|ab)*(a|e)
• the set of strings over {a, b} that do not contain the
substring aa
18
State Diagrams and Examples…
a a b
0a 1a 2a 2a
a
a b
b
b
b
1b 2b
19
State Diagrams and Examples…
b
[ea, eb] [ea, ob]
b
a a a a
b
[oa, eb] [oa, ob]
b
20
State Diagrams and Examples…
• Let M be the DFA previous slide
• A DFA M’ that accepts all strings over {a, b} that do not contain an
even number of a’s AND an odd number of b’s is shown below
• L(M’) = {a, b}* - L(M) = * - L(M)
• Any string accepted by M is rejected by M’ and vice versa
b
[ea, eb] [ea, ob]
b
a a a a
b
[oa, eb] [oa, ob]
b
21
Definition: A language L is called regular if and only if there exists
some deterministic finite accepter M such that L = L(M).
Example b a
Show that language L={awa: w {a, b}* is regular.
a
q0 q2 a q3
b
b
a,b
Example q1
Let L be the language in Example above. Show that L2 = {aw1aaw2 a} is regular.
b a b a
a
q0 q2 a q3 a
q4 a q5
b b
b
a,b
q1 22
State Diagrams and Examples…
• The language {anbn, n0} is not regular, so we can not build a DFA
that accept this language.
• It needs an infinite number of states.
a a a
q0 q1 q2 q3
b b b
q4 q5 q6
b b
23
State Diagrams and Examples…
• DFA M1 accepts (ab)*c
• M1 is incomplete determinism.
• The string abcc is rejected since M1 is unable to process the final c
from state q2
a
q0 q1
b
c
q2
M1
24
State Diagrams and Examples…
a
q0 q1
b
c a, c
b
q2 qe
a, b, c
M2
25
State Diagrams …Exercise
b
a
start a b b
0 1 2 3
a
b a
26
Deterministic Finite Automata…
• Extended transition function * of a DFA with transition
function is a function from Q * → Q defined
recursively on the length of the input string w.
• Basis: |w| = 0. Then w = e and *(qi, e) = qi
• Recursive step: Let |w| = 1. Then
• *(qi, av) = *((qi, a), v)
• qi Q, a , v *
27
Deterministic Finite Automata…
28
Deterministic Finite Automata...
• M rejects w
• M rejects w iff the computation of M on w ends up (halts) in a
rejecting configuration.
• *(q0, w) F
29
Nondeterministic Finite Automata
NFA: Formal Definition
• NFA M = (Q, , , q0, F)
30
Nondeterministic Finite Automata…
Q = { 0, 1, 2, 3 } a
start a b b
q0 = 0 0 1 2 3
F={3}
b
= { a, b }
What Language is defined ?
input
What is the Transition Table ?
a b
s
t 0 { 0, 1 } {0}
a 1 {2}
t
e 2 {3}
31
Nondeterministic Finite Automata…
• Change in
• For an DFA M, (q, a) results in one and only one state for all
states q and alphabet a.
• For an NFA M, (q, a) can result in a set of states, zero, one,
or more states:
qi
a
a
qn qi qn a qj qn
a
qk
32
Nondeterministic Finite Automata…
a,b a,b
a a b a
1 2 3 4 5
33
Nondeterministic Finite Automata…
34
Nondeterministic Finite Automata…
a
start a b b
0 1 2 3
35
Nondeterministic Finite Automata…
• Extended transition function * of a NFA with transition
function is a function from Q * → 2Q (power set)
defined recursively on the length of the input string w.
• Basis: |w| = 0. Then w = e and *(qi, e) = {qi}
• Recursive step: Let |w| = 1. Then
• *(qi, av) = U*(qj, v), qj (qi, a)
• qi Q, qj Q , a , v *
• The language of a NFA M is
• L(M) = {w * | *(q0, w) F ≠ }
• The language consists of all strings w for which there is a walk
labeled w from the initial vertex of the transition graph to
some final vertex.
36
NFA…
• An NFA that accepts string over {a, b} with substring aa or bb.
a, b a, b
start a a
0 1 2
b a, b
3 b 4
37
Equivalence of DFA & NFA
• Is NFA more powerful than DFA? NO!
• NFA is inefficient to implement directly, so convert to a DFA
that recognizes the same strings.
• Is there a language accepted by an NFA that is not
accepted by any DFA? No
• There is an equivalent DFA for any NFA.
• Each state in DFA corresponds to a SET of states of the NFA.
a a, b
M1 start b b
0 1 2
a, b a, b
start b b
M2 0 1 2
39
Equivalence of DFA & NFA…
b a a, b
M1 start a b b a
0 1 2 3 4
a
b
a, b a, b
M2 start a b b a
0 1 2 3 4
40
Equivalence of DFA & NFA…
41
Equivalence of DFA & NFA…
42
Subset Construction
• Given an NFA with states Q, inputs Σ, transition function
δN, start state q0, and final states F, construct equivalent
DFA with:
• States 2Q (Set of subsets of Q).
• Inputs Σ.
• Start state {q0}.
• Final states = all those with a member of F.
43
Subset Construction…
• The DFA states have names that are sets of NFA states.
• But as a DFA state, an expression like {p,q} must be read
as a single symbol, not as a set.
• Analogy: a class of objects whose values are sets of
objects of another class.
44
Subset construction…
45
Subset construction…
• Let M′ = (Q′, Σ’, δ’, q’0, F′ ) be a DFA. Finite state
automaton (DFA),
where
q0 ,q1
46
Convert nfa to dfa
Example a
q0 a q1 q2 nfa
b
({q0}, a} = {q1 , q2 }
({q0 }, b} =
({q1, q2}, a} = {q1 , q2}
({q1, q2 }, b} = {q0 }
( , a} =
( , b} = a
b
{q0 } {q1 , q2 } dfa
a
b
a, b
0
Example q1 0,1 q2
q0 1 nfa
({q0 },0} =
({q0 },1} = {q1}
({q1},0} = {q0, q2 }
({q1},1} = {q2 }
({q0 , q2 },0} =
({q0 , q2 },1} = {q1}
({q2 },0} =
({q2 },1} =
{q0 } 1 {q1}
0 {q0 , q2 }
1 dfa
1
0 {q2 }
0,1 0
0,1
Conversion from NFA to DFA
Procedure to convert nfa to dfa
1. Create a graph G(D) with vertex {q0}. Identify this vertex as the initial
vertex.
2. Repeat the following steps until no more edges are missing.
take any vertex{qi,qj,…,qk} of G(D) that has no outgoing edge for some
a . Compute N* (qi , a), N* (q j , a),..., N* (qk , a).
If N* (qi , a) N* (q j , a) ... N* (qk , a) = {ql, qm,..., qn},
create a vertex for G(D) labeled {ql,qm,…,qn} if it is not already exist. Add
to G(D) an edge from {qi,qj,…,qk} to {ql,qm,…,qn} and label it with a.
3. Every state of D(D) whose label contains any q f FN is identified as a final
vertex.
4. If M N accepts , the vertex {q0} in G(D) is also made a final vertex.
Reading (Self Study)
• NFA with e-Transitions
• Reduction of Number of States in Finite Automata
• Two way Finite Automata
50