Part-01 (Finite Automata-DFA & NFA)
Part-01 (Finite Automata-DFA & NFA)
Finite Automata
An example: Controlling a Toll Gate
Before we give a formal definition of a finite automaton, we consider an example in which such
an automaton shows up in a natural way. We consider the problem of designing a “computer” that
controls a toll gate.
When a car arrives at the toll gate, the gate is closed. The gate opens as soon as the driver has
payed 25 cents. We assume that we have only three-coin denominations: 5, 10, and 25 cents. We
also assume that no excess change is returned.
After having arrived at the toll gate, the driver inserts a sequence of coins into the machine. At any
moment, the machine has to decide whether or not to open the gate, i.e., whether or not the driver
has paid 25 cents (or more). In order to decide this, the machine is in one of the following six
states, at any moment during the process:
✓ The machine is in state q0, if it has not collected any money yet.
✓ The machine is in state q1, if it has collected exactly 5 cents.
✓ The machine is in state q2, if it has collected exactly 10 cents.
✓ The machine is in state q3, if it has collected exactly 15 cents.
✓ The machine is in state q4, if it has collected exactly 20 cents.
✓ The machine is in state q5, if it has collected 25 cents or more.
Initially (when a car arrives at the toll gate), the machine is in state q0. Assume, for example, that
the driver presents the sequence (10,5,5,10) of coins.
✓ After receiving the first 10 cents coin, the machine switches from state q0 to state q2.
✓ After receiving the first 5 cents coin, the machine switches from state q2 to state q3.
✓ After receiving the second 5 cents coin, the machine switches from state q3 to state q4.
✓ After receiving the second 10 cents coin, the machine switches from state q4 to state q5. At
this moment, the gate opens. (Remember that no change is given.)
The figure below represents the behavior of the machine for all possible sequences of coins. State
q5 is represented by two circles, because it is a special state: As soon as the machine reaches this
state, the gate opens.
1
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
Observe that the machine (or computer) only has to remember which state it is in at any given
time. Thus, it needs only a very small amount of memory: It has to be able to distinguish between
any one of six possible cases and, therefore, it only needs a memory of ⌈log 6⌉ = 3 bits.
A finite automaton/machine has a finite number of states where a state can either be an accepting
state or rejecting state. It takes a sequence of input and changes its current state pointer according
to the rules of the machine known as the state function. After the input sequence ends the current
state of the machine determines whether the input is accepted or rejected based on the type of the
state. In other words, if the current/active state of a machine after the input given ends, is an
accepting state the input is known to be accepted and vice versa.
Hence a finite automaton is designed to result in an accepting state if the input lies in the language
(set of strings) we want to recognize. Also vice versa.
Basically, it is an abstract model of a digital computer. The following figure shows some
essential features of general automation.
2
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
Types of FA
FA is characterized into two types:
Before discussing DFA and NFA, we are going to discuss about transition graph and transition
table.
A transition graph or diagram or state transition diagram is a directed graph which can be
constructed as follows:
o There is a node for each state in Q, which is represented by the circle.
3
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
Transition Table
The transition table is basically a tabular representation of the transition function. It takes two
arguments (a state and a symbol) and returns a state (the "next state").
4
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
Example:
Solution:
Transition table of given DFA is as follows:
→ q0 q1 q2
q1 q0 q2
* q2 q2 q2
Explanation:
o In the above table, the first column indicates all the current states. Under column 0 and 1,
the next states are shown.
o The first row of the transition table can be read as, when the current state is q 0, on input 0
the next state will be q1 and on input 1 the next state will be q2.
o In the second row, when the current state is q1, on input 0, the next state will be q0, and on
1 input the next state will be q2.
o In the third row, when the current state is q2 on input 0, the next state will be q2, and on 1
input the next state will be q2.
o The arrow marked to q0 indicates that it is a start state and circle marked to q2 indicates that
it is a final state.
5
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
The term “deterministic” refers to the fact that on each input there is one and only one state to
which the automaton can transition from its current state. Thus, each state sequence, is unique. In
a DFA, a string of symbols is parsed through a DFA automata, and each input symbol will move
to the next state that can be determined. (A DFA is an automaton where for each state, there exists
exactly one following state for each possible input.)
These machines are called finite because there are a limited number of possible states which can
be reached.
Figure: Machine M1
2) 110010
1. Start in state q1
2. Read 1, follow transition from q1 to q2.
3. Read 1, follow transition from q2 to q2.
4. Read 0, follow transition from q2 to q3.
5. Read 0, follow transition from q3 to q2.
Md. Mohibullah, Assistant Professor, Department of CSE, Comilla University
6
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
Experimenting with this machine on a variety of input strings reveals that it accepts the strings 1,
01, 11, and 0101010101. In fact, M1 accepts any string that ends with a 1, as it goes to it accept
state q2 whenever it reads the symbol 1. In addition, it accepts strings 100, 0100, 110000, and
0101000000, and any string that ends with an even number of 0’s following the last 1. It rejects
other strings, Such as, 0, 10, 101000 etc.
Example-01: Write the formal definition of the following TG of finite automaton M1.
7
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
Example-02: Write the formal definition of the following TG of finite automaton M2.
string is accepted by a DFA if and only if the DFA starting at the initial state ends in an accepting
state after reading the string.
A language L is accepted by a DFA< Q, , q, , F >, if and only if L = {w | (q, w) ϵ F}.
*
That is, the language accepted by a DFA is the set of strings accepted by the DFA.
If A is the set of all strings that machine M accepts, we say that A is the language of machine M
and write L(M) = A. We say that M recognizes A or that M accepts A. Because the term accept
has different meanings when we refer to machines accepting strings and machines accepting
languages, we prefer the term recognize for languages in order to avoid confusion.
8
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
A machine may accept several strings, but it always recognizes only one language. If the machine
accepts no strings, it still recognizes one language—namely, the empty language ø.
Example-01:
It accepts {ε}.
This DFA accepts {ε} because it can go from the initial state to the accepting state (also the initial
state) without reading any symbol of the alphabet i.e. by reading an empty string ε. It accepts
nothing else because any non-empty symbol would take it to state 1, which is not an accepting
state, and it stays there.
Example-02:
This DFA does not accept any string because it has no accepting state. Thus, the language it accepts
is the empty set ø.
2. Nondeterministic Finite Automata (NFA/NDFA)
Like the DFA, an NFA has a finite set of states, a finite set of input symbols one start state and a
set of accepting states It also has a transition function which we shall commonly call δ. The
difference between the DFA and the NFA is in the type of δ. For the NFA δ is a function that
takes a state and input symbol as arguments (like the DFA’s transition function), but returns a set
of zero, one or more states (rather than returning exactly one state as the DFA must).
9
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
In the following image, we can see that from state q0 for input a, there are two next states q1 and
q2, similarly, from q0 for input b, the next states are q0 and q1. Thus, it is not fixed or determined
that with a particular input where to go next. Hence this FA is called non-deterministic finite
automata.
In the automata theory, a nondeterministic finite automaton with ε-moves (NFA-ε) (also known as
NFA-λ) is an extension of nondeterministic finite automaton (NFA), which allows a
transformation to a new state without consuming any input symbols. The transitions without
consuming an input symbol are called ε-transitions or λ-transitions. In the state diagrams, they are
usually labeled with the Greek letter ε or λ.
You will notice three differences with the finite automata that we have seen until now. First, if the
automaton is in state q1 and reads the symbol 1, then it has two options: Either it stays in state q1,
or it switches to state q2. Second, if the automaton is in state q2, then it can switch to state q3
without reading a symbol; this is indicated by the edge having the empty string ε as label. Third,
if the automaton is in state q3 and reads the symbol 0, then it cannot continue.
10
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
Let us see what this automaton can do when it gets the string 010110 as input. Initially, the
automaton is in the start state q1.
• Since the first symbol in the input string is 0, the automaton stays in state q1 after having read
this symbol.
• The second symbol is 1, and the automaton can either stay in state q1 or switch to state q2.
– If the automaton stays in state q1, then it is still in this state after having read the third symbol.
– If the automaton switches to state q1, then it again has two options:
∗ Either read the third symbol in the input string, which is 0, and switch to state q3,
∗ or switch to state q3, without reading the third symbol.
If we continue in this way, then we see that, for the input string 010110, there are seven possible
computations. All these computations are given in the figure below.
11
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
• The automaton does not read the third symbol (equivalently, it “reads” the empty string ε), and
switches to state q3. At this moment, the automaton cannot continue: The third symbol is 0, but
there is no edge leaving q3 that is labeled 0, and there is no edge leaving q3 that is labeled ε.
Therefore, the computation hangs at this point.
From the figure, you can see that, out of the seven possible computations, exactly two ends in the
accept state q4 (after the entire input string 010110 has been read). We say that the automaton
accepts the string 010110, because there is at least one computation that ends in the accept state.
Now consider the input string 010. In this case, there are three possible computations:
None of these computations ends in the accept state (after the entire input string 010 has been
read). Therefore, we say that the automaton rejects the input string 010. The state diagram given
above is an example of a nondeterministic finite automaton (NFA). Informally, an NFA accepts a
string, if there exists at least one path in the state diagram that (i) starts in the start state, (ii) does
not hang before the entire string has been read, and (iii) ends in an accept state. A string for which
(i), (ii), and (iii) does not hold is rejected by the NFA. The NFA given above accepts all binary
strings that contain 101 or 11 as a substring. All other binary strings are rejected.
12
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
Definition
A nondeterministic finite automaton (NFA) is a 5-tuple M = (Q, Σ, δ, q, F), where
1. Q is a finite set, whose elements are called states,
2. Σ is a finite set, called the alphabet; the elements of Σ are called symbols,
3. For NFA or without epsilon NFA
δ: Q × Σ→ 2Q
For ε-NFA (Epsilon-NFA)
δ: Q × Σϵ → 2Q
(Here the power set of Q’s = P(Q) or 2Q has been taken because in case of NFA, from a state,
transition can occur to any combination of Q states). P(Q) or 2Q is a function, called the transition
function,
Example-01: Write the formal definition of the following TG of finite automaton N1.
13
Theory of Computation: Finite Automata-DFA & NFA [Part-01]
Example-02: Write the formal definition of the following TG of finite automaton N2.
14