Finite Automata and Formal Language (19CS3501) : Department of Computer Science

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Dayananda Sagar University Bangalore

Department of Computer Science


Finite Automata and Formal
Language (19CS3501)
Module 1

Introduction to Finite Automata: Study and Central


concepts of automata theory, An informal picture of finite
automata, deterministic and non-deterministic finite
automata, applications of finite automata, finite automata
with epsilon – transitions
Introduction
•What is Automata?
Automata is an abstract machine for modeling computations.
• Why abstract machines?
Used to model the essential parameters and ignore non essential
parameter.
•What is computability?
Computability is the ability to solve a problem in an effective manner.
•Applications
•Design of Digital Circuit
•Compiler Construction
•String Matching
•String processing
•Software Design
•Other Applications
Alphabet
• An alphabet is a finite and non empty set of symbols. Denoted by ∑
• The example of alphabet is,
∑ ={a, b, c,……..z}
The elements a, b, c, …….z are alphabets.
• If ∑ ={0,1}
Here 0 and 1 are alphabets.

String
• It is finite collection of symbols from alphabet.
• For example, if ∑ = {a,b} then various strings that can be formed
from ∑ are {ab, ba, aaa, bbbb, aba, bab, ….}. An infinite number of
strings can be generated.

Empty String
• String with zero occurrence of symbol. Empty string denoted by є


Examples
• The set of all strings over {a,b,c} that have ac as substring can be
▪ Input alphabet ∑ = {a,b,c}
▪ String = {ac, aac, aca, bac, acb, cac, acc, aaca, aacb, aacc,
baca, bacb, back, caca, cacb, cacc, aaac, abac, acac, baac,
bbac, bcac, caac, cbac, ccac, acaa, acab, acba, acbb, acbc,
acca, accb,

accc,…..}
▪ Language L = {xacy | x,y ∈{a,b,c}∗}

• The set of all strings over {0,1} that start with 0’s can be
▪ Input alphabet ∑ = {0,1}
▪ String = {0, 00, 01, 001, 010, 000, 011, 0000, 0111, 0101,
0010……}
▪ Language L = {0x | x ∈{0,1}*}
Examples

Finite State Machine / Finite Automata
• The finite state machine represents a mathematical model that used
to recognize patterns
• It takes string as input, move to next state according to the current
input symbol and current state and generate accept or reject as
output
• At the end of string if we are at the final state that means string is
valid or accepted otherwise invalid or rejected.
• The input is processed by various states.
▪ initial state or start state
▪ intermediate states
▪ final or accept state
• The finite state system is very good design tool for the programs
such as TEXT EDITORS and LEXICAL ANALYZERS.
Definition of Finite Automata
• A finite automata is a collection of 5-tuple(Q,∑,δ,q0,F) Where ,
▪ Q is finite set of states, which is non empty.
▪ ∑ is input alphabet, indicates input set.
▪ δ is transition function or mapping function. We can
determine the next state using this function.
▪ q0 is an initial state and q0 є Q
▪ F is set of final states. F є Q
Finite Automata Model
• The finite automata can be
represented as
❖ Input Tape – It is a linear tape having
some number of cells. Each input symbol
is placed in each cell
❖ Finite Control – Finite control decides
the next state on receiving particular
input from input tape
❖Tape Reader – It reads the cell one by
one from left to right, at a time only one
input is read
Deterministic Finite Automata (DFA)
The Finite Automata is called Deterministic Finite Automata if
there is only one path for a specific input from current state to
next state. It can be represented as follows:
• A machine M = (Q,∑,δ,q0,F) Where ,
▪ Q is finite set of states, which is non empty.
▪ ∑ is input alphabet, indicates input set.
▪ δ is transition function or mapping function. We can
determine the next state using this function.
δ : Q x ∑ -> Q
▪ q0 is an initial state and is in Q
▪ F is set of final states.
Non-Deterministic Finite Automata (NFA)

Difference Between DFA and NFA
Representation of Machine
• Transition Diagram or State Diagram: A DFA is represented by
digraphs called state diagram.
▪ For each state in Q, there is a node.
▪ For each state q in Q, input symbol a in ∑. Let δ(q,a) = p then
there is a arc from node q to node p labelled by a.
▪ The initial state is denoted by an empty single incoming arc.
▪ Nodes corresponding to final state denoted by double circle
Notation for constructing transition diagram
When machine accept
• Initial state is indicated by single arrow null or empty string
q
q0 0

• Final state is indicated inside double circle


q
1

• Transition (q0,a) = q1 where q0 is initial state and q1 is final


state
a q
q0 1
Representation of Transition Function
• 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"). A transition table is represented by
the following things:
▪ Columns correspond to input symbols.
▪ Rows correspond to states.
▪ Entry for the row corresponding to state q and column corresponding to input a is the
state of δ(q,a).
▪ The start state is denoted by an arrow with no source.
▪ The accept state is denoted by a star or single circle
Representation of FA
M = (Q,∑,δ,q0,F) where
• Q = {q0,q1,q2}
• ∑ = {0,1}
• q0 is initial state
• F = {q2}
• δ is define as function, diagram, table:
δ(q0,0) = q1
δ(q0,1) = q2
δ(q1,0) = q0
δ(q1,1) = q2
δ(q2,0) = q2
δ(q2,1) = q2

Processing of String using DFA
• To check the validity of any string for the DFA, always start with
initial state.
• Pass current alphabet of string to current state and check entry
in transition table or transition diagram. Move to next state
according to the entry in the transition. Then move with next
input symbol of string with new state.
• At the end of string if we reach to the final state that means
string is valid or accepted otherwise invalid or rejected.
Check the validity of the string 101100 for the given DFA.
For checking validity of string we have to start from initial state
q0.
δ(q0,101100) -> δ(q2,01100) { δ(q0,1)=q2 }
-> δ(q1,1100) { δ(q2,0)=q1 }
-> δ(q2,100) { δ(q1,1)=q2 }
-> δ(q4,00) { δ(q2,1)=q4 }
-> δ(q1,0) { δ(q4,0)=q1 }
-> δ(q3,є) { δ(q1,0)=q3 }
Now we are at the end of string and reach to q3 i.e final state.
At the end of string if we reach to final state that means given string
101100 is valid for the given DFA.
Construct DFA which accept string 0011
• String = {0011}
0 0 1 1 q
q0 q1 q2 q3 4

• DFA is define as M = (Q,∑,δ,q0,F) Where


• Q = {q0,q1,q2,q3,q4}
• ∑ = {0,1} State/Input 0 1
q0 q1 -
• q0 is initial state q1 q2 -
• F = {q4} q2 - q3
q3 - q4
• δ is define as table:
*q4 - -
Dummy or Dead State
• Having no meaning in the generation of the string.
• It is only designed to full the requirements of the finite state
machines that every state has one transition for each alphabet.
Machine with dummy state (dead state)
0 0 1 1 q
q0 q1 q2 q3 4

1 0 0

1 0,1
q5

0,1
• DFA is define as M = (Q,∑,δ,q0,F) Where
▪ Q = {q0,q1,q2,q3,q4,q5} State/Input 0 1
▪ ∑ = {0,1} q0 q1 q5
▪ q0 is initial state q1 q2 q5
▪ F = {q4} q2 q5 q3
▪ δ is define as table: q3 q5 q4
*q4 q5 q5
q5 q5 q5
Construct DFA which accept string start with 0 and end with 1
• String = {01,001,011,0001,0011,0101,0111……}
• Language L = {0x1| x∈{0,1}∗} 0
1
1
q 0 q
q2
q2
0 1
0
1 q
3
0,1

• DFA is define as M = (Q,∑,δ,q0,F) Where


▪ Q = {q0,q1,q2,q3}
State/Input 0 1
▪ ∑ = {0,1} q0 q1 q3
▪ q0 is initial state q1 q1 q2
*q2 q1 q2
▪ F = {q2}
q3 q3 q3
▪ δ is define as table:
Construct DFA which accept string either a or b
• String = {a,b}
• Language L = {a+b} q
0
a,b q1
q2

q a,b
2
a,b
• DFA is define as M = (Q,∑,δ,q0,F) Where
▪ Q = {q0,q1,q2}
State/Input a b
▪ ∑ = {a,b}
q0 q1 q1
▪ q0 is initial state
*q1 q2 q2
▪ F = {q1} q2 q2 q2
▪ δ is define as table:
Construct DFA which accept string starting with string ab
• String = {ab,aba,abb,abaa,abab,abba,abbb……}
• Language L = {abx | x є (a+b)*} a b
q0 q1 q2 a,b

b
a

q3 a,b

• DFA is define as M = (Q,∑,δ,q0,F) Where


▪ Q = {q0,q1,q2,q3}
State/Input a b
▪ ∑ = {a,b} q0 q1 q3
▪ q0 is initial state q1 q3 q2
▪ F = {q2} *q2 q2 q2
▪ δ is define as table: q3 q3 q3
Construct DFA which accept string not having substring
aab
• Trick :- first construct for having substring aab then reverse it by
making final as non final and non final as final
• DFA having substring aab b a
a,
q a q a q b q b
0 1 2 3
b

• Now Reverse above DFA by making q0, q1, q2 as final and q3 as


non final
b a
a,
a a q b q b
q
q0 1 2 3
b
Construct DFA which accept string end with either 00 or
11
• String =
{00,11,000,011,100,111,0000,0100,1000,1100,0011,0111,1111……..}
q 0 q
• Language L = {x(00+11) | x є {0,1}*} 1 2 0
1
0
q 0 1
0 0
q q
1 3 1
1 4

• DFA is define as M = (Q,∑,δ,q0,F) Where


State/Input 0 1
▪ Q = {q0,q1,q2,q3,q4} q0 q1 q3

▪ ∑ = {0,1} q1 q2 q3

▪ q0 is initial state *q2 q2 q3

▪ F = {q2,q4} q3 q1 q4

▪ δ is define as table: *q4 q1 q4


Construct DFA which accept string of {0,1] whose 2nd symbol from
left is 1.
• String =
{01,11,010,011,110,111,0100,0101,0110,0111,1100,1101……..}0,
• Language L = {(0+1)1(0+1)*} 0, 1 q
1
q0 1 q1 2

q3 0,
1
• DFA is define as M = (Q,∑,δ,q0,F) Where
State/Input 0 1
▪ Q = {q0,q1,q2,q3}
q0 q1 q1
▪ ∑ = {0,1}
q1 q3 q2
▪ q0 is initial state *q2 q2 q2
▪ F = {q2} q3 q3 q3
▪ δ is define as table:
Construct DFA which accept string having even no of 0’s and even
no of 1’s

State/Input 0 1
q0* q1 q3
q1 q0 q2
q2 q3 q1
q3 q2 q0
Construct DFA which accept strings of {a,b} such that length of the
string is divisible by 3
• String = {є,aaa,aab,aba,abb,baa,bab,bba,bbb,aaaaaa,
aaaaab……..}
a,
• Language L = {((a+b)(a+b)(a+b))*} 0 b 1
a,
b 2

a,
b
• DFA is define as M = (Q,∑,δ,q0,F) Where
▪ Q = {q0,q1,q2} State/Input a b
q0* q1 q1
▪ ∑ = {a,b}
q1 q2 q2
▪ q0 is initial state q2 q0 q0
▪ F = {0}
▪ δ is define as table:
Construct DFA which accept strings such that number of 0’s
divisible by 2 and number of 1’s divisible by 3.

1 1
00 01 02
1

0 0 0 0
0
0
1 1
10 11 12

1
Construct DFA which accept strings of decimal number
• String = {0.23,1.333,4.555333,…………..} 0-9
0-9
• Language L = {x.x |xє{0-9}+} 0-9 . 0-9 q
q q q
0 1 2 3

• DFA is define as M = (Q,∑,δ,q0,F) Where


▪ Q = {q0,q1,q2,q3}
State/Input 0-9
▪ ∑ = {0-9} .
q0 q1
▪ q0 is initial state q1 q1 q2
▪ F = {q3} q2 q3
*q3 q3
▪ δ is define as table:
Construct DFA which accept strings of integer divisible by 3
• 0,3,6,9 0,3,6,9

1,4,7 q
q0
1
2,5,8
1,4,7
2,5,8
2,5,8
1,4,7
q
2
0,3,6,9

State/Input 0,3,6,9 1,4,7 2,5,8


q0* q0 q1 q2
q1 q1 q2 q0
q2 q2 q0 q1

You might also like