Unit-2 Introduction To Finite Automata PDF
Unit-2 Introduction To Finite Automata PDF
Automata Theory:
• Study of abstract model machine and their properties, providing a mathematical notion of
“computer”.
• Automata are abstract mathematical models of machines that perform computations on an input
by moving through a series of states or configurations. If the computation of an automaton reaches
an accepting configuration it accepts that input.
• Examples of Abstract models of computation: Finite automata, pushdown automata, and Turing
machines.
Computability Theory:
• Examining the limits of what can be computed algorithmically.
Complexity Theory:
• Analyzing the efficiency of algorithms and classifying problems by their computational complexity.
Abstract Model
• An abstract model is a model of computer system (considered either as
hardware or software) constructed to allow a detailed and precise analysis of
how the computer system works.
• Such a model usually consists of input, output and operations that can be
performed and so can be thought of as a processor.
• E.g. an abstract machine that models a banking system can have operations
like “deposit”, “withdraw”, “transfer”, etc.
Fig: Finite automaton modeling an on/off switch Fig: Finite automaton modeling of language accepting even
number of 0’s and even number of 1’s
Applications of TOC
Web Form Validations Algorithms: TOC concepts are employed in validation of web forms.
Application: Allow regex rule to validate web form fields with complex rules.
Compiler Design: TOC principles are fundamental in lexical analysis and parsing stages of compiler design.
Application: Regular expressions and context-free grammars (CFGs) derived from TOC help define the syntax
of programming languages. Validates the rules and structure of programs.
Formal Language Specification: TOC provides the theoretical foundation for formal languages and their
grammars.
Application: Finite-state machines are basis for programs for spell checking, indexing, grammar checking,
searching large bodies of text.
Algorithm Design and Analysis: TOC, particularly complexity theory, helps analyze and compare the efficiency
of algorithms.
Application: Engineers use TOC to choose the most suitable algorithm based on the problem's computational
complexity.
Artificial Intelligence (AI) and Machine Learning (ML): TOC contributes to the understanding of the
computational limits of AI and ML algorithms.
Application: Helps in assessing the feasibility and complexity of solving specific problems using AI and ML
techniques.
BASIC TERMS::
Symbol (basic building block): The symbol is the smallest building block in the theory of computation and can be
any letter, number or even pictograms.
For example: a, b, 0, 1
Alphabets (Σ) : The alphabet is nothing more than a collection of symbols (finite set).
For example: Σ = {a,b} Σ = {0,1}
String/word (w): finite sequence of symbols selected from some alphabets
For example: Σ = {a,b} ; Possible strings are:
String "a” of length 1, String "b" of length 1
String "aa" of length 2, String "ab" of length 2
String "aaa" of length 3
Languages (L): A set of strings all of which are chosen from Σ*, where Σ is particular alphabet, is called a
language
For example: Σ = {a,b}
L1 = set of all strings of length 2. = {aa, ab, ba, bb} [it is finite language or a finite set]
L2 = set of all strings of length 3. = {aaa, aab, aba, abb, baa, bab, bba, bbb}. [is called a finite set]
L3 = set of all strings where each string start with an "a”. = {a, aa, ab, aaa, aab, aba, abb, ...} [is an
infinite language or an infinite set.
2.1. Introduction to Finite Automata, Introduction of Finite State Machine
Finate State Machine (FSM) OR Finite Automata (FA) OR Finite State Automaton(FSA)
• A finite automaton or finite state automaton (FSA, plural: automata) is a
mathematical model of computation.
• FSM is an abstract machine that can be in exactly one of a finite number of
states at any given time.
• The FSM can change from one state to another in response to some external
inputs, and it can also produce outputs (Mealy machine, Moore machine).
• A finite Automata(FA/FSA) is a more general term that can refer to any system
that uses a finite state machine as its underlying model.
• For example, a regular expression engine is a type of FA/FSA that uses a finite
state machine (FSM) to match patterns in strings.
So, essentially, a finite state machine is a specific type of model, while a finite
state automation can refer to any system that uses this type of model.
Finate State Machine
OR
Finite Automata
OR
FA/FSM/FSA
• The figure above illustrates a finite state machine, which belongs to one well-
known variety of automatons.
• This automaton consists of states (represented in the figure by circles), and
transitions (represented by arrows).
• As the automaton sees a input symbol (any among alphabets 0/1), it makes a
transition (or jump) to another state, according to its transition function
(which takes the current state and the recent symbol as its inputs).
Automaton
An automaton is represented formally by a 5-tuple (Q,Σ,δ,q0,F), where:
• Q is a finite set of states.
• Σ is a finite set of input symbols, called the alphabet of the automaton.
• δ is the transition function, that is, δ: QxΣ → Q.
• q0 is the start state, that is, the state of the automaton before any input has been processed,
where q0∈ Q.
• F is a set of states of Q (i.e. F⊆Q) called final states or accept states.
1
● When we allow multiple
choices, non-deterministic 1
=> Several possible next states distinguishes NFA from DFA, where the next possible
state is uniquely determined.
=> Although the DFA and NFA have distinct definitions, a NFA can be translated to
equivalent DFA using power set construction, i.e., the constructed DFA and the NFA
recognize the same formal language.
=> Both types of automata recognize only regular languages.
=> So, both of same power
General Notations of FA/FSM
There are two preferred notations for describing this class of automata;
- Transition Table
- Transition Diagram
Eg:
Consider FA
Q = {q0, q1, q2, q3}
Σ = {0, 1}
q0 = q0
F = {q0}
δ = Q × Σ -> Q
And, We want to design DFA which accepts strings with an even number of 0’s or even
number of 1’s
Consider FA
Q = {q0, q1, q2, q3}
Σ = {0, 1}
q0 = q0
F = {q0}
δ = Q × Σ -> Q
DFA which accepts strings having an even number of 0’s or even number of 1’s
Transition Graph/ Transition Diagram Transition Table
δ 0 1
* -> q0 q2 q1
q1 q3 q0
q2 q0 q3
q3 q1 q2
Q. DFA which accepts strings having both an even number of 0’s & even number of 1’s??
a) Design FA with ∑ = {0, 1} accepts the set of all strings ending with 00.
Solution:
The strings that will be generated for this particular languages are 00, 000, 100, 1000,
.... in which 2 zeros are present at last.
The transition graph is as follows:
b) Design FA with ∑ = {0, 1} accepts the set of all strings with three consecutive 0’s.
Solution:
The strings that will be generated for this particular languages are 000, 0001, 1000,
10001, .... in which 0 always appears in a clump of 3.
The transition graph is as follows:
Design a DFA L(M) = {w | w ε {0, 1}*} and W is a string that does not contain
consecutive 1’s.
Solution:
The DFA will generate the strings that do not contain consecutive 1's like 10, 110, 101,..... etc.
When three consecutive 1's occur the DFA will be:
Basis:
𝛅’(q, ε) = q when the string is the empty string ε
Induction:
𝛅’(q,xa) = 𝛅(𝛅’(q,x), a) when the string is a non-empty string xa
where a is an input symbol and x is a string
Extended Delta Function – Delta Hat 𝛅’ (Example)
Computing 𝛅’(A,0100)
– Computes 𝛅’ for all prefixes of 0100
𝛅’(A, ε) = A
𝛅’(A,0) = 𝛅(𝛅’(A, ε),0) = 𝛅(A,0) = A
𝛅’(A,01) = 𝛅(𝛅’(A,0),1) = 𝛅(A,1) = B
𝛅’(A,010) = 𝛅(𝛅’(A,01),0) = 𝛅(B,0) = A
𝛅’(A,0100) = 𝛅(𝛅’(A,010),0) = 𝛅(A,0) = A
Since 𝛅’(A,0100)=A and A is a final state, the string 0100 is accepted by this
DFA.
Language Accepted by a DFA
• Informally, the language accepted by a DFA A is the set of all strings that are
recognized by A.
• Formally, the language accepted by a DFA A is L(A) such that
L(A) = { w | (q0 ,w) ∈ F }
where q0 is the starting state of A and F is the final states of A
Now compute:
(q0,1001)
Q. Compute: (q0,101)
=> ? Will it be accepted?
NOTE::
=> A string x is accepted
by a DFA (Q, Σ, δ, q0, F) if;
Not accepted (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.
i.e; L(M) = {w | (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.
NOTE::δ2 = = δ’)
Deterministic finite automata (DFA):
It is a finite state machine that accepts/rejects finite strings of symbols and only produces a unique
computation (or run) of the automaton for each input string.
The figure at in left illustrates a deterministic finite
automaton (DFA).
In the automaton, there are three states: S0, S1, and S2(denoted graphically by circles).
The automaton takes finite sequence of 0s and 1s as input.
For each state, there is transition arrow leading out to next state for both 0 and 1. Upon
reading a symbol, a DFA jumps deterministically from a state to another by following the
transition arrow.
For example, if the automaton is currently in state S0 and current input symbol is 1 then it
deterministically jumps to state S1.
A DFA has a start state (denoted graphically by an arrow coming in from nowhere) where
computations begin, and a set of accept states (denoted graphically by a double circle) which
help define when a computation is successful.
Examples & Exercises of DFA
Construct a DFA to accept a string containing a zero followed by a one.
Construct a DFA to accept a string containing two consecutive zeroes followed by two
consecutive
ones .
Construct a DFA to accept a string containing even number of zeroes and any
number of ones.
Q. Construct a DFA to accept all strings which do not contain three consecutive zeroes.
Q. Construct a DFA to accept all strings containing even number of zeroes and even number of
ones.
DFA accepting all strings over w ∈(a,b)* which contains “aba” as a substring
Given a binary string S, the task is to write a program for DFA Machine that accepts a set of all strings over w ∈ (a, b)* which
contains “aba” as a substring.
Examples :
Input-1 : ababa Output : Accepted Explanation : "ababa" consists "aba”
Input-2 : abbbb Output : Not accepted Explanation : "abbbb" does not consist "aba”
Approach : Below is the designed DFA Machine for the given problem. Construct a transition table for DFA states and analyze
the transitions between each state. Below are the steps –
Desired Language : Explanation :
L = {aba, baba, abab, aababbb.....} 1. Firstly, there will be 4states.(say q0, q1, q2, q3) , with q0 being initial
state and q3 being final state.
2. Initially we will be in q0 state, now we start reading the given string.
When we read ‘b’, we will remain in the same state
If we read ‘a’, then it transits to state q1.
3. Assuming that now we are in q1 state.
When we read ‘a’, we will remain in the same state.
If we read ‘b’, we will transits to state q2.
Transition Table:: 4. Assuming that now we are in q2 state.
Current state Final state If we read ‘a’, it transits to state q3.
a b If we read ‘b’, it transits to state q0.
->q0 q1 q0 5. Assuming we are in final state(q3)
q1 q1 q2 We remain in the same state, when we read ‘a’ or ‘b’.
q2 q3 q0 6. All strings which are accepted by this DFA will have “aba” as its
* q3 q3 q3 substring.
// C++ program for the above approach
#include <cstring> // Iterate through the string
#include <iostream> for (int i = 0; i < s.length(); i++) {
using namespace std; // Checking for all combinations
if ((previous_state == 0 && s[i] == 'a') ||
(previous_state == 1 && s[i] == 'a')) {
final_state = 1;
// Driver Code }
int main() if ((previous_state == 0 && s[i] == 'b') ||
{ (previous_state == 2 && s[i] == 'b')) {
// Given string final_state = 0;
string s = "ababa"; }
// Function Call if (previous_state == 1 && s[i] == 'b') {
checkValidDFA(s); final_state = 2;
}
} if ((previous_state == 2 && s[i] == 'a') ||
(previous_state == 3)) {
// Function to check whether the given final_state = 3;
// string is accepted by DFA or not }
void checkValidDFA(string s) // Update the previous_state
{ previous_state = final_state;
}
// Stores initial state of DFA
// If final state is reached
int initial_state = 0; if (final_state == 3) {
// Stores previous state of DFA cout << "Accepted" << endl;
int previous_state = initial_state; }
// Stores final state of DFA // Otherwise
int final_state; else {
cout << "Not Accepted" << endl;
}
}
Can FSA be designed for language accepting anbn for n > 1 ?
• No, an FSA cannot be designed for the language that accepts strings of the
form anbn where n > 1 .
• This language is not regular, meaning that it cannot be accepted by a finite
state automaton. The pumping lemma for regular languages states that if a
language is regular, then there exists a fixed number, the pumping length,
that can be used to "pump" the string and produce another string that is
still in the language. However, this property does not hold for the language
of strings of the form 'a'^n and 'b'^n.
• For example, if n=2, the string 'aa' is in the language, and if we try to pump
it by repeating the first 'a', we get 'aaa', which is not in the language.
• Thus, this language cannot be accepted by a finite state automaton, and a
different type of automaton, such as a pushdown automaton, is needed to
recognize it.
Testing whether a language is regular or not (without using pumping lemma :Will read in Unit 3)
Here n is unbounded and there is condition exists (i.e. we have to count all number of a
and compare with count of b ); FA can count finite element but can not count infinite one
When there is condition FA can not process it as it doesnot have memory;
When there is condition FA can not process it as it doesnot have memory
AP
NOTE: When there is infinite language and you have to construct FA, there should be loop
in the language when repeated should get entire language:
Eg: If there is AP, it’s regular
NOT FOR EXAM:
Exercises:
Give the DFA for the language of string over {0.1} in which each string end
with 11. [2067,TU BSc CSIT]
Give the DFA accepting the string over {a,b} such that each string does not end
with ab.[2067, TU B.Sc CSIT]
Nondeterministic finite automata (NFA)
It is a finite state machine where from each state and a given input symbol the automaton
may jump into several possible next states.
This distinguishes it from the deterministic finite automaton (DFA), where the next
possible state is uniquely
determined.
Although the DFA and NFA have distinct definitions, a NFA can be translated to equivalent
DFA using power set construction, i.e., the constructed DFA and the NFA recognize the
same formal language.
Both types of automata recognize only regular languages.
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.
Ex: Design a NFA for the language over {0, 1} that have at least two consecutive 0’s or 1’s
→The ε (epsilon) transition refers to a transition from one state to another without
the reading of an input symbol (ie without the tape containing the input string
moving). Epsilon transitions can be inserted between any states.
→ There is also a conversion algorithm from a NFA with epsilon transitions to a NFA
without epsilon transitions.
Convert NFA with epsilon to NFA.
Q. Consider the NFA-epsilon move machine
M = { Q, Σ, δ, q0, F}
Q = { q0, q1, q2 }
Σ= { a, b, c } and ε moves
q0 = q0
F = { q2 }
The language accepted by the above NFA with epsilon moves is the set of
strings over {a,b,c} including the null string and all strings with any number of
a's followed by any number of b’s followed by any number of c’s.
Now convert the NFA with epsilon moves to a NFA.
SOLUTION:
=> ?
=> Let NFA M = ( Q', Σ, δ', q0', F'); Find each tuples values ….. (see next slide)
SOLUTION:
Let NFA M = ( Q', Σ, δ', q0', F’)
Now, our job is to find each tuples values for NFA M = ( Q', Σ, δ', q0', F’)
First determine the states of the new machine (i.e. Q’),
Q' = the epsilon closure of the states in the NFA with epsilon moves.
There will be the same number of states but the names can be constructed by writing
the state name as the set of states in the epsilon closure. ( NOTE: The epsilon closure
is the initial state and all states that can be reached by one or more epsilon moves.)
Thus q0 in the NFA-epsilon becomes {q0,q1,q2} because the machine can move from
q0 to q1 by an epsilon move, then check q1 and find that it can move from q1 to q2 by
an epsilon move, i.e. q0’ = {q0,q1,q2}
Similarly, q1 in the NFA-epsilon becomes {q1,q2} because the machine can move from
q1 to q2 by an epsilon move.
, i.e. q1’ = {q1,q2}
Similarly, q2 in the NFA-epsilon becomes {q2} just to keep the notation the same. Q2
can go nowhere except q2, that is what phi means, on an epsilon move. i.e. q 2’ = {q2}
-> The initial state of our new machine is
q0’ = {q0,q1,q2} which is the epsilon closure of q0
-> The final state(s) of our new machine is the new state(s) that contain a state symbol
that was a final state in the original machine.
-> The new machine accepts the same language as the old machine.
Conversion of NFA to DFA
Equivalence of NFA & DFA
Although there are many languages for which NFA is easier to construct than DFA, it can be proved that
every language that can be described by some NFA can also be described by some DFA.
The DFA has more transition than NFA and in worst case the smallest DFA can have 2n state while the
smallest NFA for the same language has only n states.
We now show that DFAs & NFAs accept exactly the same set of languages.
That is non-determinism does not make a finite automaton more powerful. To show that NFAs and DFAs
accept the same class of language, we show::“Any language accepted by a NFA can also be accepted by
some DFA”.
=> For this we describe an algorithm that takes any NFA and converts it into a DFA that accepts the same
language. The algorithm is called “subset construction algorithm”.
Steps:
To convert a NFA, N = (QN, Σ, δN, q0, FN) into an equivalent
DFA, D = (QD, Σ, δD, q0, FD), we have following steps:
1.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}.
2.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.
3.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 ∈ Σ,
δD (S, a) = δN(p,a)
i.e. for any state {q0, q1, q2, … qk} of the DFA & any input a, the next state of the DFA is
the set of all states of the NFA that can result as next states if the NFA is in any of the
state’s q0, q1, q2, … qk when it reads a.
• Example1: Conversion from NFA to DFA NOTE::δ2 = = δ’)
SOLUTION::
Let, M2 = { Q2, Σ, δ2, q0, F2}
Compute Q2:
Initially Q2 is empty, i.e. Q2={}. Since the initial state of the new DFA (M2) will also be q0
={0}, {0} is added to Q2.
δ2( 0 , a ) = { 1 , 2 } , { 1, 2 } is added to Q2 and δ ({0}, a) = {1,2}
δ2 ( 0 , b ) = φ, φ is added to Q2 and δ ({0}, b) = φ
At this point Q2 = { {0} , { 1 , 2 }, }
Here, since { 1 , 2 } is now in Q2, the transitions from { 1 , 2 } on symbols a and b are
computed.
δ2( 1 , a ) = { 1 , 2 } , and δ2( 2 , a ) = φ and δ({1,2},a) = {1 , 2 } Similarly, δ({1,2}, b )
= { 1 , 3 } . Thus { 1 , 3 } is added to Q2 .
At this point Q2 = { {0} , { 1 , 2 }, { 1 , 3 }, } .
Here, since { 1 , 3} is now in Q2, the transitions from { 1 , 3 } on
symbols a and b are computed.
δ( { 1 , 3 } , a ) = { 1 , 2 } and
δ( { 1 , 3 } , b ) = φ
At this point Q2 = { {0} , { 1 , 2 }, { 1 , 3 } }
Thus no new states are added to Q2.
Since the transitions from all states of Q have been computed and
no more states are added to Q2, the conversion process stops here.
Note that there are no states of Q2 in φ. Hence there are no states
that M2 can go to from φ. i.e. δ(φ, a ) = (φ, b ) = φ
Compute F2: For the accepting/final states of M2, since states 0
and 1 are the accepting states of the NFA, all the states of Q that
contain 0 and/or 1 are accepting states.
Hence { 0 }, { 1 , 2 } and { 1 , 3 } are the accepting states of M2.
Compute Σ : same
Continue from previous::
Given NFA was:
OR
The other state are removed because they are not reachable from start state.
• Example3: Convert the NFA to DFA
Q1.
Q2.
Finite State Machines with output:
Next State
Present state Output
Input = 0 Input = 1
→a b c x2 / x3
b b d x1
c c d x2
d d d x3
Mealy Machine vs. Moore Machine
Generally, it has fewer states than Moore Generally, it has more states than Mealy
Machine. Machine.
The value of the output function is a function The value of the output function is a function
of the transitions and the changes, when the of the current state and the changes at the
input logic on the present state is done. clock edges, whenever state changes occur.
Mealy machines react faster to inputs. They In Moore machines, more logic is required to
generally react in the same clock cycle. decode the outputs resulting in more circuit
delays. They generally react one clock cycle
later.
Equivalence of NFAs and DFAs:
Show that the set of languages that can be recognized by some NFA is equal to the
set of languages that can be recognized by some DFA.
1. Every DFA has an equivalent NFA.
2. Every NFA has an equivalent DFA.
Every NFA has an equivalent DFA.