Finite Automata (DFA and NFA, Epsilon NFA) : FSA Unit 1 Chapter 2
Finite Automata (DFA and NFA, Epsilon NFA) : FSA Unit 1 Chapter 2
Unit 1 Chapter 2
Chapter 2
Finite Automata (DFA and NFA, epsilon NFA)
Intuitive example
Consider a man watching a TV in his room. The TV is in on state. When it is switched off, the
TV goes to off state. When it is switched on, it again goes to on state. This can be
represented by following picture.
Switched off
TV on
TV off
Switched on
A language is a subset of the set of strings over an alphabet. A language can be generated by
grammar. A language can also be recognized by a machine. Such machine is called recognition
device. The simplest machine is the finite state automaton.
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 cant 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.
The DFA, i.e. Deterministic Finite Automata cant be in more than one state at any time.
The NFA, i.e. Non-Deterministic Finite Automata can be in more than one state at a time.
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, searching large bodies of text, recognizing speech, transforming text using markup
languages such as XML & HTML, and network protocols that specify how computers communicate.
Definition (Deterministic finite state automata [DFSA])
Page 1
FSA
Unit 1 Chapter 2
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.
A transistion function that takes as arguments a state and an input symbol and returns a state. In our
diagram, is represented by arcs between states and the labels on the arcs.
For example
If s is a state and a is an input symbol then (p,a) is that state q such that there are arcs labled a
from p to q.
Page 2
FSA
Unit 1 Chapter 2
* -> q0
q1
q2
q3
0
q2
q3
q0
q1
1
q1
q0
q3
q2
This DFA accepts strings having both an even number of 0s & even number of 1s.
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.
For the example I considered previously, the corresponding transition diagram is:
Page 3
FSA
Unit 1 Chapter 2
): -
(q, w) = p
(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
Then,
i.e.
(q, w) is what we get by making a transition from state p on input a, the last symbol of w.
(q, w) = (p, a)
For Example
Page 4
FSA
Now compute
Unit 1 Chapter 2
(q0,1001)
(q, x) = p F.
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.
Page 5
FSA
Unit 1 Chapter 2
(q0, w) = p F}
That is; 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.
2. DFA accepting all string over = {0, 1} ending with 3 consecutive 0s.
Page 6
FSA
Unit 1 Chapter 2
6. DFA over {a, b} that accepts the strings ending with abb.
Exercises: (Please do this exercise as homework problems and the question in final exam
will be of this patterns)
1. Give the DFA for the language of string over {0.1} in which each string end with 11.
[2067,TU BSc CSIT]
2. Give the DFA accepting the string over {a,b} such that each string does not end with
ab.[2067, TU B.Sc CSIT]
CSIT Nepal (www.csitnepal.com)
Page 7
FSA
Unit 1 Chapter 2
3. Give the DFA for the language of string over {a,b} such that each string contain aba
as substring.
4. Give the DFA for the langague of string over {0,1} such that each string start with 01.
5. The question from book: 2.2.4, 2.2.5 of chapter 2.
Here, from state q1, there is no any arc for input symbol 0 & no any arc out of q 2 for 0 & 1. So, we
can conclude in a NFA, there may be zero no. of arcs out of each state for each input symbol. While
in DFA, it has exactly one arc out of each state for each input symbol.
, the transition function is a function that takes a state in Q and an input symbol in as arguments
and returns a subset of Q The only difference between an NFA and DFA is in type of value that
returns. In NFA, returns a set of states and in case of DFA it returns a single state.
For input sequence w = 00101, the NFA can be in the states during the processing of the input are as:
Page 8
FSA
Unit 1 Chapter 2
Page 9
FSA
Unit 1 Chapter 2
(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
Page 10
FSA
Unit 1 Chapter 2
a.
Also let,
(q, x) = {p1, p2, p3, pk}
and
Then,
Consider, a NFA,
Page 11
FSA
Unit 1 Chapter 2
Since the result of above computation returns the set of state {q0,q2) which include the
accepting state q2 of NFA so the string 01101 is accepted by above NFA.
Examples (Design NFA to recognize the given language)
1. Construct a NFA over {a, b} that accepts strings having aa as substring.
2. NFA for strings over {0, 1} that contain substring 0110 or 1001
Page 12
FSA
Unit 1 Chapter 2
4. NFA over {a, b} that accepts strings starting with a and ending with b.
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.
Examples
CSIT Nepal (www.csitnepal.com)
Page 13
FSA
Unit 1 Chapter 2
1. Design a NFA for the language over {0, 1} that have at least two consecutive 0s
or1s
Exercise
1. Question from book: 2.3.4 of chapter 2
2. Give a NFA to accept the language of string over{a.b} in which each string contain abb
as substring.
3. Give a NFA which accepts binary strings which have at least one pair of 00 or one
pair of 11.
Page 14
FSA
Unit 1 Chapter 2
Page 15
FSA
Unit 1 Chapter 2
For Example
The same table can be represented with renaming the state on table entry as
Page 16
FSA
Unit 1 Chapter 2
Or
The other state are removed because they are not reachable from start state.
Example2
Convert the NFA to DFA
Page 17
FSA
Unit 1 Chapter 2
2.
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 DFA D, say can be defined as;
QD = 2QN , q0 = {q0}
Let S = { p1, p2, p3, pk} QD. Then for S QD & a ,
D (s, a) =
CSIT Nepal (www.csitnepal.com)
Page 18
FSA
Unit 1 Chapter 2
FD = {S / S QD & S FN }
The fact that D accepts the same language as N is as;
for any string w *;
N (q0, w) = D (q0, w)
Thus, we prove this fact by induction on length of w.
Basis Step:
Let |w| = 0, then w = ,
N(q0,
)= {q0)=q0= D(q0, )
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}
Now,
N (q0, w)
Also
D (q0, w)
= N (q0, xa)
= N ( N (q0, x), a)
= N ({p1, p2, p3, pk}, a) [Since, from inductive step]
=U N(pi,a).(1)
= D (q0, xa)
= D ( D (q0, x), a)
= D ( N (q0, x), a) [Since, by the inductive step as it is true for x]
= D ({p1, p2, p3, pk}, a) [Since, from inductive step]
Page 19
FSA
Unit 1 Chapter 2
Page 20
FSA
Unit 1 Chapter 2
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.
(q0, x) =
(q0, x)
Now,
D
(q0, w)
= D (q0, xa)
= D ( D (q0, x), a)
= D (p, a) [ from inductive step
= r, say
(q0, x,=p]
Now,
N
(q0, w)
= N (q0, xa)
= N ( N (q0, x), a) [from inductive steps]
= N({p}, a)
=r [from the rule that define N)
D
(q0, w)=
(q0, w)
For examples:
1.
CSIT Nepal (www.csitnepal.com)
Page 21
FSA
Unit 1 Chapter 2
-closure of a state:
-closure of a state q can be obtained by following all transitions out of q that are labeled .
After we get to another state by following , we follow the -transitions out of those states & so
on, eventually finding every state that can be reached from q along any path whose arcs are all
labeled .
Formally, we can define -closure of the state q as;
Basis: state q is in -closure (q).
Induction: If state q is reached with -transition from state q, 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.
Extended Transition Function of -NFA: CSIT Nepal (www.csitnepal.com)
Page 22
FSA
Unit 1 Chapter 2
then
Example
Page 23
FSA
Unit 1 Chapter 2
Page 24