Unit-II (Introduction To Finite Automata)
Unit-II (Introduction To Finite Automata)
Prepared By:
Ghanashyam BK
1
Introduction to Finite Automata
A finite automaton is a mathematical (model) abstract
machine that has a set of “states” and its “control”
moves from state to state in response to external
“inputs”.
The control may be either “deterministic” meaning
that the automation can’t be in more than one state at
any one time
or “non deterministic”, meaning that it may be in
several states at once.
This distinguishes the class of automata as DFA or
NFA.
2
Applications
The finite state machines are used in applications in
computer science and data networking.
For example, finite-state machines are basis for
programs for spell checking, indexing, grammar
checking, etc
network protocols that specify how computers
communicate.
3
Introduction of Finite State Machine
a machine that can, at any point in time, be in a
specific state from a finite set of possible states
It can move (transition) to another state by accepting
an input.
The simplest machine is the finite state automaton
4
Deterministic Finite Automata
(DFA)
A deterministic finite automaton is defined by a
quintuple (5-tuple) as (Q, Σ, δ, q0, F).
Where,
Q = Finite set of states,
Σ = Finite set of input symbols,
δ = A transition function that maps Q × Σ -> Q
q0 = A start state; q0 ∈ Q
F = Set of final states; F ⊆ Q.
5
Deterministic Finite Automata
(DFA)
For example:
6
How a DFA process strings?
The first thing we need to understand about a DFA is
how DFA decides whether or not to “accept” a
sequence of input symbols.
The “language” of the DFA is the set of all symbols
that the DFA accepts.
Suppose a1, a2, …… an is a sequence of input symbols.
We start out with the DFA in its start state, q0.
We consult the transition function δ also for this
purpose.
7
How a DFA process strings? (Contd…)
Say δ (q0, a1) = q1 to find the state that the DFA enters
after processing the first input symbol a1.
We then process the next input symbol a2, by
evaluating δ (q1, a2); suppose this state be q2.
We continue in this manner, finding states q3, q4, …,
qn. such that δ (qi-1, ai) = qi for each i.
if qn is a member of F, then input a1, a2,…, an is
accepted & if not then it is rejected.
8
Notations for DFA
There are two preferred notations for describing this
class of automata
Transition Diagram
Which is a graph
Transition Table
which is a tabular listing of the δ function, which by implication
tells us the set of states and the input alphabet.
9
Notations for DFA
Transition Diagram
A transition diagram of a DFA is a graphical representation
where; (or is a graph)
For each state in Q, there is a node represented by circle,
For each state q in Q and each input a in Σ, if δ (q, a) = p then
there is an arc from node q to p labeled a in the transition
diagram.
If more than one input symbol cause the transition from state q
to p then arc from q to p is labeled by a list of those symbols.
The start state is labeled by an arrow written with “start” on the
node.
The final or accepting state is marked by double circle.
10
Notations for DFA
Transition Diagram Example
Fig: The transaction diagram for the DFA accepting all strings with a
substring 01
11
Notations for DFA
Transition Table
Transition table is a conventional, tabular representation of
the transition function δ that takes the arguments from Q ×
Σ & returns a value which is one of the states of the
automation.
The row of the table corresponds to the states while column
corresponds to the input symbol.
The starting state in the table is represented by followed
by the state i.e. q, for q being start state, whereas final
state as *q, for q being final state.
12
Notations for DFA
Transition Table Example
Fig: The transaction table for the DFA accepting all strings with a substring 01
13
Example 2
Fig: The state diagram & transaction table for the DFA accepting all strings
with even number of 0’s & even number of 1’s
14
Extended Transition Function of DFA
The extended transition function of DFA, denoted by
is a transition function that takes two arguments as
input, one is the state q of Q and another is a string
w∈ Σ*, and generates a state p ∈ Q.
This state p is that the automaton reaches when
starting in state q & processing the sequence of inputs
w.
i.e. (q, w) = p
15
Extended Transition Function of DFA
Let us define by induction on length of input string as follows:
Basis step: (q,є) =q. i.e. from state q, reading no input
symbol stays at the same state.
Induction: Let w be a string from Σ* such that w = xa, where x
is substring of w without last symbol and a is the last symbol of
w, then (q, w) = δ ( (q, x), a).
Thus, to compute (q, w), we first compute (q, x), the state
the automaton is in after processing all but last symbol of w. let
this state is p, i.e. (q, x) = p.
Then, (q, w) is what we get by making a transition from
state p on input a, the last symbol of w. i.e. (q, w) = δ (p, a)
16
Extended Transition Function of DFA
Compute (q0, w) for each prefix w of 110101,
starting at є and going in increasing size.
Compute (q0,1001)
Compute (q0, 100)
18
Language of DFA
The language of DFA M = (Q, Σ, δ, q0, F) denoted by
L(M) is a set of strings over Σ* that are accepted by M.
i.e; L(M) = {w/ (q0, w) = p ∈ F}
i.e. the language of a DFA is the set of all strings w
that take DFA starting from start state to one of the
accepting states.
The language of DFA is called regular language.
19
Examples
Construct a DFA, that accepts all the strings over Σ =
{a, b} that do not end with ba.
20
Examples
DFA accepting all string over Σ = {0, 1} ending with 3
consecutive 0‟s.
21
Examples
DFA over {a, b} accepting {baa, ab, abb}
22
Examples
DFA accepting zero or more consecutive 1’s. i.e. L (M)
= {1n | n = 0, 1, 2, ……}
23
Examples
DFA over {0, 1} accepting {1, 01}
24
Examples
DFA over {a, b} that accepts the strings ending with
abb.
25
Assignment-3
Give the DFA for the language of string over {0.1} in which
each string end with 11
Give the DFA accepting the string over {a,b} such that each
string does not end with ab.
Give the DFA for the language of string over {a,b} such that
each string contain aba as substring
Give the DFA for the langague of string over {0,1} such that
each string start with 01
Give the DFA for the langague of string over {0,1} such that
set of all string ending in 00.
Give the DFA for the langague of string over {0,1} such that
set of strings with 011 as a substring.
26
Non-Deterministic Finite Automata (NFA)
NFA can also be interpreted by a quintuple; (Q, Σ, δ,
q0, F)
Where,
Q = A finite set of states
Σ = A finite set of input symbols, (alphabets)
δ = A transition function that maps state symbol pair to sets of
states i.e. δ is Q × Σ =2Q
A state q0 ∈ Q, that is distinguished as a start (initial) state.
A set of final states F distinguished as accepting (final) state. F ⊆ Q.
Unlike DFA, a transition function in NFA takes the
NFA from one state to several states just with a single
input.
27
Non-Deterministic Finite Automata (NFA)
28
Non-Deterministic Finite Automata (NFA)
For input sequence w = 00101, the NFA can be in the
states during the processing of the input are as:
29
Non-Deterministic Finite Automata (NFA)
NFA over {0, 1} accepting strings {0, 01, 11}
30
Non-Deterministic Finite Automata (NFA)
31
Non-Deterministic Finite Automata (NFA)
32
The Extended transition function of NFA
Definition by Induction:
Basis Step:
(q, ε) = {q} i.e. reading no input symbol remains into the same
state.
Induction:
Let w be a string from Σ* such that w = xa, where x is a substring of
without last symbol a.
Also let, (q, x) = {p1, p2, p3, …pk}
and
34
Assignment-4
compute for (q0, 01101)
Compute for (q0, 1101011)
Compute for (q0, 1010101)
35
Language of NFA
The language of NFA, M = (Q, Σ, δ, q0, F), denoted by
L (M) is;
L (M) = {w/ (q, w) ∩ F ≠ φ}
i.e. L(M) is the set of strings w in ∑* such that (q0,w)
contains at least one state accepting state.
36
Examples
Construct a NFA over {a, b} that accepts strings
having aa as substring.
37
Examples
NFA for strings over {0, 1} that contain substring 0110
or 1001
38
Examples
NFA over {a, b} that have “a” as one of the last 3
characters.
39
Examples
NFA over {a, b} that accepts strings starting with a and
ending with b.
40
Examples
Design a NFA for the language over {0, 1} that have at
least two consecutive 0‟s or1‟s
41
Examples
Design a NFA for the language over {0, 1} that have at
least two consecutive 0‟s or1‟s
42
Assignment-5
Give a NFA to accept the language of string over{a.b}
in which each string contain abb as substring.
Give a NFA which accepts binary strings which have
at least one pair of „00‟ or one pair of „11‟
43
Equivalence of DFA and NFA
every language that can be described by some NFA
can also be described by some DFA
The proof that DFA’s can do whatever NFA’s can do
involves an important construction called subset
construction because it involves constructing all
subsets of the set of states of the NFA.
If NFA has n-states, the DFA can have 2n states (at
most), although it usually has many less.
44
Subset Construction Algorithm
To convert a NFA, N = (QN, Σ, δN, q0, FN) into an
equivalent DFA D = (QD, Σ, δD, q0, FD), we have
following steps.
The start state of D is the set of start states of N i.e. if
q0 is start state of N then D has start state as {q0}.
QD is set of subsets of QN i.e. QD = 2QN. So, QD is
power set of QN. So if QN has n states then QD will
have 2n states. However, all of these states may not be
accessible from start state of QD so they can be
eliminated. So QD will have less than 2n states.
45
Subset Construction Algorithm (Contd...)
FD is set of subsets S of QN such that S ∩ FN ≠ φ i.e. FD
is all sets of N’s states that include at least one final
state of N.
For each set S ⊆ QN & each input a ∈ Σ,
46
Subset Construction Algorithm (Contd...)
48
Reduction of NFA to DFA
The equivalent DFA is
OR
49
Assignment-6
Convert the NFA to DFA
50
Assignment-6
Convert the NFA to DFA
51
Theorem 1
For any NFA, N = (QN, Σ, δN, q0, FN) accepting
language L ⊆ Σ* there is a DFA D = (QD, Σ, δD,
q0’,FD) that also accepts L i.e. L (N) = L (D).
Proof:
The fact that D accepts the same language as N is as;
for any string w ∈ Σ*;
52
Theorem 1
Basis Step:
Let |w| = 0, then w = ε,
Induction step:
Let |w| = n + 1 is a string such that w = xa & |x| = n, |a| =
1; a being last symbol.
Let the inductive hypothesis is that x satisfies.
Thus,
D (q0, x) = N (q0, x), let these states be {p1, p2, p3, … pk}
53
Theorem 1
54
Theorem 1
Now, from subset construction, we can write,
δD ({p1, p2, p3, … pk}, a) =UδN(pi ,a)
so, we have
D (q0, w) = UδN(pi ,a)………….(2)
Now we conclude from 1 and 2 that
56
Theorem 2
We can proof this fact using induction on length of the string
Basis step:
Let |w| = 0 i.e. w = ε
∴ D (q0, w) = D (q0, ε) = q0
N (q0, w) = N (q0, ε) = {q0}
∴ D (q0, w) = N (q0, w) for |w| = 0 is true.
Induction:
Let |w| = n + 1 & w = xa. Where |x| = n & |a| = 1; a being the last
symbol.
Let the inductive hypothesis is that it is true for x.
∴if D (q0, x) = p, then N (q0, x) = {p}
i.e. D (q0, x) = N (q0, x)
57
Theorem 2
58
Finite Automaton with Epsilon Transition (ε
- NFA)
A NFA with ε-transition is defined by five tuples (Q, Σ,
δ, q0, F), where;
Q = set of finite states
Σ = set of finite input symbols
q0 = Initial state, q0 ∈ Q
F = set of final states; F ⊆ Q
δ = a transition function that maps; Q × Σ ∪ {ε} 2Q
59
Finite Automaton with Epsilon Transition (ε
- NFA)
Examples:
60
Finite Automaton with Epsilon Transition (ε
- NFA)
Examples
61
Exercise
Design ε-NFA for the following languages
The set of strings consisting of zero or more a’s followed
by zero or more b’s , followed by zero or more c’s.
The set of strings that consists of either 01 repeated one
or more times or 010 repeated one or more times.
62
Epsilon Closure of a State
ε-closure of a state ‘q’ can be obtained by following all
transitions out of q that are labeled ε.
Formally, we can define ε-closure of the state q as;
Basis: state q is in ε-closure (q).
Induction: If state p is reached with ε-transition from
state q then, p is in ε-closure (q). And if there is an arc
from p to r labeled ε, then r is in ε-closure (q) and so
on.
If δ is the transition function of ε-NFA involved , and p
is in ε-closure (q), then ε-closure (q) also contains all
the states in δ(p, ε).
63
Epsilon Closure of a State
Example:
ε-closure(1) = {1,2,3,4,6}
64
Extended Transition Function of ε–NFA
BASIS STEP
(q, ε) = ε-closure (q)
INDUCTION STEP
Let w = xa be a string, where x is substring of w without
last symbol a and a ∈ Σ but a ≠ ε.
Let (q, x) = {p1, p2, … pk} i.e. pi’s are the states that
can be reached from q following path labeled x which
can end with many ε & can have many ε.
Also Let, be the set {r1,r2,r3…..,rm}
Then, (q,w) = ε-closure({r1,r2,r3…..,rm})
65
Extended Transition Function of ε–NFA
66
Removing Epsilon Transition using the
concept of Epsilon Closure
Step 1 − Find out all the ε-transitions from each state
from Q. That will be called as ε-closure(qi) where, qi ∈Q.
Step 2 − Then, 𝛿1 transitions can be obtained. The 𝛿1
transitions means an ε-closure on 𝛿 moves.
Step 3 − Step 2 is repeated for each input symbol and for
each state of given NFA.
Step 4 − By using the resultant status, the transition table
for equivalent NFA without ε can be built.
NFA with ε to without ε is as follows −
δ1(q,a) = ∈-closure (δ (δ^(q,𝛜),a))
where, δ^(q,𝛜) = ∈-closure(q)
67
Removing Epsilon Transition using the
concept of Epsilon Closure
Convert the given NFA with epsilon to NFA without
epsilon.
68
Removing Epsilon Transition using the
concept of Epsilon Closure
Now we will obtain 𝛿1 transitions for each state on each
input symbol
δ1(q0, 0) = ε-closure(δ(δ^(q0, ε),0))
• = ε-closure(δ(ε-closure(q0),0))
• = ε-closure(δ(q0,q1,q2), 0))
• = ε-closure(δ(q0, 0) ∪ δ(q1, 0) U δ(q2, 0) )
• = ε-closure(q0 U Φ ∪ Φ)
• = ε-closure(q0) = {q0,q1, q2}
δ'(q0, 1) = ε-closure(δ(δ^(q0, ε),1))
• = ε-closure(δ(q0,q1,q2), 1))
• = ε-closure(δ(q0, 1) ∪ δ(q1, 1) U δ(q2, 1) )
• = ε-closure(Φ ∪q1 U Φ)
• = ε-closure(q1)
• = {q1, q2}
69
Removing Epsilon Transition using the
concept of Epsilon Closure
δ1(q0, 2) = ε-closure(δ(δ^(q0, ε),2))
• = ε-closure(δ(q0,q1,q2), 2))
• = ε-closure(δ(q0, 2) ∪ δ(q1, 2) U δ(q2, 2) )
• = ε-closure(Φ U ΦU q2)
• = ε-closure(q2)
• = {q2}
δ1(q1, 0) = ε-closure(δ(δ^(q1, ε),0))
• = ε-closure(δ(q1,q2), 0))
• = ε-closure(δ(q1, 0) U δ(q2, 0) )
• = ε-closure(Φ ∪ Φ)
• = ε-closure(Φ)
• =Φ
70
Removing Epsilon Transition using the
concept of Epsilon Closure
δ1(q1,1) = ε-closure(δ(δ^(q1, ε),1))
o = ε-closure(δ(q1,q2), 1))
o = ε-closure(δ(q1, 1) U δ(q2, 1) )
o = ε-closure(q1 ∪ Φ)
o = ε-closure(q1)
o = {q1,q2}
δ1(q1, 2) = ε-closure(δ(δ^(q1, ε),2))
o = ε-closure(δ(q1,q2), 2))
o = ε-closure(δ(q1, 2) U δ(q2, 2) )
o = ε-closure(Φ ∪ q2)
o = ε-closure(q2)
o = {q2}
71
Removing Epsilon Transition using the
concept of Epsilon Closure
δ1(q2, 0) = ε-closure(δ(δ^(q2, ε),0))
• = ε-closure(δ(q2), 0))
• = ε-closure(δ(q2, 0))
• = ε-closure(Φ)
• =Φ
δ1(q2, 1) = ε-closure(δ(δ^(q2, ε),1))
• = ε-closure(δ(q2), 1)
• = ε-closure(δ(q2, 1))
• = ε-closure(Φ)
• =Φ
δ1(q2, 2) = ε-closure(δ(δ^(q2, ε),))
• = ε-closure(δ(q2), 2))
• = ε-closure(δ(q2, 2))
• = ε-closure(q2)
• = {q2}
72
Removing Epsilon Transition using the
concept of Epsilon Closure
summarize all the computed δ1 transitions as given below −
δ1(q0,0)={q0,q1,q2}
δ1(q0,1)={q1,q2}
δ1(q0,2)={q2}
δ1(q1,0)= { Φ }
δ1(q1,1)={q1,q2}
δ1(q1,2)={q2}
δ1(q2,0)={ Φ }
δ1(q2,1)={ Φ }
δ1(q2,2)={q2}
73
Removing Epsilon Transition using the
concept of Epsilon Closure
The transition table is given below −
States/Inputs 0 1 2
q0 {q0,q1,q2} {q1,q2} {q2}
q1 Φ {q1,q2} {q2}
q2 Φ Φ {q2}
The NFA without epsilon is given below − Here, q0, q1, q2 are final
states because ε-closure(q0), ε-closure(q1) and ε-closure(q2) contain a
final state q2.
74
Equivalence of NFA and ε –NFA
every language that can be described by some NFA
can also be described by some ε-NFA.
75
Finite State Machines with output
Finite automata may have outputs corresponding to
each transition.
There are two types of finite state machines that
generate output −
Moore machine
Mealy Machines
76
Moore Machine
Moore machine is an FSM whose outputs depend on
only the present state.
A Moore machine can be described by a 6 tuple (Q, ∑,
O, δ, X, q0) where −
Q is a finite set of states.
∑ is a finite set of symbols called the input alphabet.
O is a finite set of symbols called the output alphabet.
δ is the input transition function where δ: Q × ∑ → Q
X is the output transition function where X: Q → O
q0 is the initial state from where any input is processed
(q0 ∈ Q).
77
Moore Machine
Next State
Present State
Input = a Input = b
State Output State Output
q0 q0 0 q1 1
q1 q1 1 q0 0
If length of input string is n then there will be n length output.
Example: If Input = abab then Output = 0110
80