You are on page 1of 240

Lecture One:

Automata Theory
Automata theory
 The word “Automata“ is the plural of “automaton"
which simply means any machine.

 automata theory is the study of abstract machines


and problems they are able to solve.

 Automata theory is closely related to formal language


theory as the automata are often classified by the
class of formal languages they are able to recognize.
There are several reasons why the study of automata and
complexity is an important and core part of computer science

 Automata theory plays an important role when we are making


software for designing and checking the behavior of digital
circuit.
 The lexical analyzer of typical compiler uses automata theory
 Software for scanning large bodies of text such as collection of
web pages, to find occurrence of words, phrases or other
patterns also uses automata theory
 Automata theory is key to software or verifying systems of all
types that have a finite number of distinct states such as
communication protocols or protocol for secure exchange of
information
 Automata theory is most useful concept of software for natural
language processing
Automaton

 An automaton is an abstract model of a digital


computer

 It has a mechanism to read input (string over a given


alphabet, e.g. strings of 0’s and 1’s on S = {0,1})
written on an input file.

 An automaton has a set of states

 Its control moves from state to state through external


“inputs”
Automaton
 With every automaton, a transition function is
associated which gives the next state in terms of the
current state

 An automaton can be represented by a graph in which


the vertices give the internal states and the edges
transitions

 The labels on the edges show what happens (in terms


of input and output) during the transitions
Components of an automaton
 Input file: Contains strings of input
symbols
 Storage unit: consists of an unlimited
number of cells, each capable of holding
a single symbol from an alphabet
 Control unit : can be in any one of a finite
number of internal states and can change
states in defined manner
Some Terms used in automaton theory

 Alphabets-Everything in mathematics is based on symbols. This


is also true for automata theory. Alphabets are defined as a finite set of
symbols. An example of alphabet is a set of decimal numbers
∑={0,1,2,3,4,5,6,7,8,9}

 Number of strings of length n from an alphabet set of size m is given by mn

 Strings- A string is a finite sequence of symbols selected from some


alphabet
If ∑ {a,b} is an alphabet then abab is string over alphabet ∑. A string is
generally denoted by w. The length of string is denoted by |w|

 Empty string is string with zero occurrence of symbols. This string is


represented by є
Some Terms used in automaton theory

 The set of strings, including empty, over an alphabet ∑ is denoted by ∑*.

 ∑+ = ∑* -{є}

 Languages-A set of strings which are chosen from some ∑*, where ∑ is a
particular alphabet, is called a language.

 If ∑ is an alphabet, and L subset of ∑*, then L is said to be language over


alphabet ∑. For example the language of all strings consisting of n 0’s
followed by n 1’s for some n>=0:
{є,01,0011,000111,-------}
Some Terms used in automaton theory

 Langauge in set forms-

{w|some logical view about w}


e.g {anbn | n>=1}

 Kleene closure- Given an alphabet, a language in which any string of letters


from ∑ is a word, even the null string, is called closure of the alphabet . It is
denoted by writing a star, after the name of alphabet as a superscript ∑*.
Finite Automaton

 One of the powerful models of computation which are restricted model of


actual computer is called finite automata. These machines are very similar
to CPU of a computer. They are restricted model as they lack memory.

 Finite automation is called finite because number of possible states and


number of letters in alphabet are both finite and automation because the
change of state is totally governed by the input.
Pictorial representation of FA

X Y X Y X y --------

Reading Head
Finite control System
p0 p1 p2 p3 p4
states
Main parts of pictorial representation of
Finite machine

 Strings are fed into device by means of an input tape which is


divided into square with each symbol in each square.
 Main part of machine is a black box which serve that what
symbol is written at any position on input tape by means of a
movable reading head
 p0,p1,p2,p3,p4 are the states in finite control system and x
and y are input symbols.
 At regular intervals , the automation reads one symbol from
input tape and then enters in a new state that depends only
on the current state and the symbol just read.
Main parts of pictorial representation of
Finite machine

 After reading an input symbol, reading head moves one


square to the right on input tape so that on next move, it will
read the symbol in next tape square. This process is repeated
again and again.

 Automation then indicates approval or disapproval

 If it winds up in one of the final states, the input string is


considered to be accepted. The language accepted by the
machine is the set of strings it accepts.
The 5-Tuple
A DFA M is a 5-tuple M = (Q, , , q0, F), :
where Q is the finite set of states
 is the alphabet (that is, a finite set of symbols)
  (Q    Q) is the transition function
q0  Q is the start state
F  Q is the set of accepting states
 Q is the set of states
 Drawn as circles in the diagram
 We often refer to individual states as qi
 The definition requires at least one: q0, the start state

  is the alphabet (that is, a finite set of symbols)


The 5-Tuple
A DFA M is a 5-tuple M = (Q, , , q0, F), where:
Q is the finite set of states
 is the alphabet (that is, a finite set of symbols)
  (Q    Q) is the transition function
q0  Q is the start state
F  Q is the set of accepting states
 F is the set of all those in Q that are accepting states
 Drawn as double circles in the diagram

  is the transition function


 A function (q,a) that takes the current state q and next input
symbol a, and returns the next state
Simpler Notations for DFAs

There are two preferred notations for describing


automata:
 Transition Diagram

 Transition Table
Transition Diagram Notations
 A transition diagram for DFA, M=(Q,∑, ,q0,F) is a
graph defined as follows:
 For each state q in Q and each input symbol a in ∑,
there is a node represented by the circle
 For each state q in Q and each input symbol a in ∑, let
(q,a)=P. This is represented by an arc from q to P
labelled a.
Transition Diagram Notations
 If any state q in Q is the starting state
then it is represented by the circle with
arrow as
q

 Nodes corresponding to accepting


states are marked by a double circle
Transition Table
 Transition tables- A transition table is a
conventional tabular representation of a
function like δ that takes two arguments and
returns a state. The rows of table correspond
to the states and columns correspond to input
δ/∑ a b
q0 q1 q2
q1 q2 q0
*q2 q2 q2
Language of a DFA
 The language of a DFA can be defined as:

 L(M)= { w|δ(q0,w) is in F}

 where q0 is the start state and w is a string in ∑


Example :

 Let Σ = {0, 1}. Give DFAs for {}, {ε}, Σ*, and Σ+.

For {}: For {ε}:

0/1 0/1

0/1
q0 q0 q1
For Σ*: For Σ+:

0/1 0/1

0/1
q0 q0 q1
Example: Design a FA that accepts set of strings such that every string
ends in 00, over the alphabet
{0,1} i,e ∑={0, 1}

Inorder to design any FA, first try to fulfill the minimum condition.

0
Start q0 0 q1 0 q2

Being DFA, we must check every input symbol for output state from every
state. So we have to decide output state at symbol 1 from q0,q1 and q2.
Then it will be complete FA
 1 1 0
start 0 0
q0 q1 q2
q2

1
Example: Design a FA that accepts set of strings such that every string
contains two alphabets, over the alphabet
{a,b} i,e ∑={a, b}

Sol: Inorder to design any FA, first try to fulfill the minimum
condition. The Language set for the statement={aa, ab, ba,
bb}
a a
q0 q1 q2
Start

Being DFA, we must check every input symbol for output state from every
state. So we have to decide output state at symbol 1 from q0,q1 and q2.
Then it will be complete FA

start a/b a/b


q0 q1 q2
q2

a/b
a/b
q3 Dead State
Construct a FA that accepts set of strings where the number of 0s
in every string is multiple of 3 over alphabet ∑={0,1}

1 1 1
start q0
0 q1 0 q2

0
As 0 existence of 0 is also multiple of 3, we have to consider
starting state as the final state.
Design FA which accepts set of strings containing exactly
four 1s in every string over alphabet ∑={0,1}

0 0 0 0 0

start
1 1 1 1
q0 q1 q2 q3 q2
q4

1
q5 is called the trap state or dead state. Dead 0/1
states are those states which transit to q5
themselves for all input symbols.
Design a FA that accepts strings containing exactly one 1 over
alphabet {0,1}. Also draw the transition table for the FA
generated

0 0
start 1
q1 q2
q2

q3 is the dead 0/1


q3
state
Transition table for previous problem
δ/∑ 0 1
q1 q1 q2
*q2 q2 q3
q3 q3 q3

Non final state


that transit in self
loop for all inputs
Design an FA that accepts the language
L={w ϵ (0,1)*/ second symbol of w is ‘0’ and fourth input is ‘1’}

1/0

start 1/0 0 1/0 1


q0 q1 q2 q3 q4

1 0

0/1
q5
Design DFA for the language
L={w ϵ (a,b)*/nb(w) mod 3 > 1}
As given in the language, this can be interpreted that
number of b mod 3 has to be greater than 1 and there
is no restriction on number of a’s. Thus it will accept
string with 2 bs,5 bs, 8bs and so on.
a a a
start b b
q0 q1 q2

b
Q={q0,q1,q2}
F={q2}
Design DFA for the language
L={w ϵ (a,b)*/nb(w) mod 3 > 1}
As given in the language, this can be interpreted that
number of b mod 3 has to be greater than 1 and there
is no restriction on number of a’s. Thus it will accept
string with 2 bs,5 bs, 8bs and so on.
a a a
start b b
q0 q1 q2

b
Q={q0,q1,q2}
F={q2}
Design FA over alphabet ∑= {0,1} which accepts the set of
strings either start with 01 or end with 01

1/0
start 0 1
q0 q1 q2
1
q3 1
0 0
1
q4 0
0
1
q5
Example:

 Give a DFA M such that:

L(M) = {x | x is a string of 0’s and 1’s and |x| >= 2}

0/1

0/1 0/1
q0 q1 q2
Example :

 Give a DFA M such that:

L(M) = {x | x is a string of (zero or more) a’s and b’s such


that x does not contain the substring aa}

b a/b
a
a
q0 q1 q2
b
Example :

 Give a DFA M such that:

L(M) = {x | x is a string of (zero or more) a’s and b’s such


that x does not contain the substring aa}

b b
start qo
a q1
b q2

a a
a/b q3
Example :

 Give a DFA M such that:

L(M) = {x | x is a string of a’s, b’s and c’s such that x


contains the substring aba}

b/c a a/b/c
a a
b
q0 q1 q2 q3

b/c
c
Example:

 Give a DFA M such that:

L(M) = {x | x is a string of a’s and b’s such that x


contains both aa and bb}
a
b
a
q1 q2 q3
b a/b
a a

q0 a b q7
b
b a
b a
q4 q5 q6
b
Design FA over alphabet ∑={a,b} such that
string starts and ends with same symbol

Sol: The Language of the desired automata will be


L={∊,a,aa,aba,aabbba,b,bb,bab,bbaaab.....}

start a b
A B b
C
b a

D b
a
b

E a
Design FA over alphabet ∑={a,b} such that
string starts and ends with different symbol

Sol: The Language of the desired automata will be


L={ab,ba,aabbbb,bbaaaa,baa,bbbaaa,bababa.....}. This language is
complement of previous language.
a

start a b
A B b
E
b a

C b

b a

D a
a)Design FA over the alphabet {a,b} such that string contains ‘a’
b) Design FA over the alphabet {a,b} such that string starts with ‘a’
c) Design FA over the alphabet {a,b} such that string ends with ‘a’

 Sol: The language of the desired automata is:


 L={a,aa,abb,aba,bbba,ba,bbbbaaa,ba......} c)

 a) b
a/b b
a
start a
A B start a
A B
 b) a/b b
start a
A B

C a/b
Automata for complement language

 The automata for a language which is complement of another


language can be formed by converting final to non final states
and non final to final states of the automata for original
language.

 The rule applies only to DFA and not to NFA


Design FA over alphabet ∑={a,b} such that string
contains even number of alphabets

Sol: The Language of the desired automata will be


L1={∊,aa,bb,aaaa,aabb,aabbbbaa,bb,bbbb,bbaa,bbaaaabb....} [Even number of alphabets]

start a/b B
A

a/b
L2={a,aaa,b,bbb,aba,baa,baabb....}[L2 is complement of L1] [Odd number of alphabets]

start a/b
A B

a/b
Design FA over alphabet ∑={a,b} such that string starts
with character ‘a’

Sol: The Language of the desired automata will be


L1={a,aa,aaaa,aaaaaa,ab,abbb,aba,abba,aabb,abbbb....}

start a
A B a/b

a/b
C
Design FA over alphabet ∑={a,b} such that string does
not start with character ‘a’

Sol: The Language of the desired automata will be


L2 (Complement of previous)={∊,b,bb,bbbb,bbbbbb,ba,baaa,bab,baab..........}

start a
A C a/b

B a/b
Complementation

 Applies to only DFA


 if a DFA is (Q,∑,𝛿,q0,F), the complement of
DFA is given by (Q,∑,𝛿,q0,F’) where F’=Q-F
Nondeterministic Finite Automata
A DFA has exactly one transition from every state on every symbol in the
alphabet. By relaxing this requirement we get a related but more flexible
kind of automaton: the nondeterministic finite automaton or NFA.

NFAs are a bit harder to think about than DFAs, because they do not
appear to define simple computational processes. They may seem at first
to be unnatural, like puzzles invented by professors for the torment of
students. NFAs and other kinds of nondeterministic automata arise
naturally in many ways, and they too have a variety of practical
applications.
Not A DFA
a
q0 q1

a,b
 Does not have exactly one transition from every state
on every symbol:
 Two transitions from q on a
0
 No transition from q (on either a or b)
0
 Though not a DFA, this can be taken as defining a
language, in a slightly different way
Formal Definition of NFA

 Let M be NFA then M is defined as


M={Q,∑,δ,q0,F}
 Where Q: is a non empty finite set of states in NFA
 ∑: is a non empty finite set of input symbols
 q0:initial state of NFA and it is the member of Q
 F: a non empty finite set of final states in Q
 δ : is the transition function that takes a state and Q and an input symbol in
∑ as arguments and returns a subset of Q
 that is Q x (∑ Ս {є}) 2Q
So in non deterministic acceptor, the range of δ is in the power set 2Q ,
so its value is not a single element of Q but a subset of it. This subset
defines the set of possible states that can be reached by transition.
Design a NFA for the language L=all strings over {0,1}
that have atleast two consecutive 0’s or 1’s

0/1
q1 0 q2

0/1 0
start
q0
0/1

1 1 q4
q3
Design a NFA for the language L=(ab Ս aba)*

a
b
start a b a
q0 q1 q2 q3

a
b b

q4 a/b
DFA for the given statement
NFA’s for previous DFA

ab
b a
start
start start
q1 q0 q1 q0
q0

a
є b
a b
q2
q2 a aba
Spontaneous Transitions
 An NFA can make a state transition
spontaneously, without consuming an input
symbol
 Shown as an arrow labeled with 
 For example, {a}*  {b}*:

q1 a

q0

q2 b
-Transitions To Accepting States

q1 a

q0

q2 b

 An -transition can be made at any time


 For example, there are three sequences on the empty string
 No moves, ending in q0, rejecting
 From q0 to q1, accepting
 From q0 to q2, accepting
 Any state with an -transition to an accepting state ends up
working like an accepting state too
-transitions For NFA Combining
q1 a

q0

q2 b

 -transitions are useful for combining smaller


automata into larger ones
 This machine combines a machine for {a}* and a
machine for {b}*
 It uses an -transition at the start to achieve the
union of the two languages
Incorrect Union
a
A = {an | n is odd}
a
b
B = {bn | n is odd}
b

a
a AB?
b
No: this NFA accepts
b
aab
Correct Union
a
A = {an | n is odd}

a
b B = {bn | n is odd}
b
a

AB
a
b

NFA, Lecture 4, slide 60 b


Incorrect Concatenation
a
A = {an | n is odd}
a
b
B = {bn | n is odd}
b
a b

a b

{xy | x  A and y  B} ?
No: this NFA accepts abbaab
Correct Concatenation
a
A = {an | n is odd}
a
b

B = {bn | n is odd}
b

a b

a b

{xy | x  A and y  B}
Draw the state diagram for NFA accepting language
L=(ab)*(ba)* U aa*

We can construct NFA for the language L in two


parts.
L=L1 U L2
Where L1=(ab)*(ba)*
L2=aa*
ab ba

start ba a
q1 q2
a
NFA for start q3 q4
(ab)*(ba)*
NFA for aa*
For Combining the two, we take a new state q0 and join the two NFAs with
null transition

ab ba

ba q2
ϵ q1
start
q0
a
ϵ
a
q3 q4
Find NFA with four state for the language
L={(an:n>=0) U (bna:n>=1)}

L=L1 U L2
L1= an:n>=0
L2= bna:n>=1
a b

start start a
q1 q2 b q4
q3
NFA for an
NFA for bna
Combined NFA
a

q1

ϵ b

b a
q2 q3 q4
Transformation of NFA to DFA

 For every non deterministic finite automata, there exist an


equivalent deterministic finite automata.
 The equivalence is determined in terms of language
acceptance. Since a NFA is nothing but a finite automata in
which zero, one or more transitions on an input symbol is
permitted, we can always construct a finite automata which will
simulate all the moves of NFA on a particular input symbol in
parallel, then get a finite automata in which there will be
exactly one transition on every input symbol, hence it will be
DFA equivalent to NFA
Convert the following NFA into DFA

a,b b
start a a
q0 q1 q2

a,b
b
q3
Solution: Step1: Seek all the transition from starting state q0 for
every symbol in ∑ i.e (a,b).If we get a set of states for same
input, consider the set as a new single state
δ(q0,a)={q0,q1}-------------new state

δ(q0,b)={q0}
Step2: In step1 we are getting a new state {q0,q1}. Repeat step
1 for this new state only i.e check all transitions of a and b
from {q0,q1} as:
δ({q0,q1},a)= δ(q0,a) U δ(q1,a)
= {q0,q1} U {q2}
= {q0,q1,q2}-------------new state
δ({q0,q1},b)= δ(q0,b) U δ(q1,b)
= {q0} U {q1}
={q0,q1}---------------old state
Step 3: Repeat step 2 till you are getting any new state. All those
states that consist of any of the accepting state of given NFA
as member state will be considered as final states
δ({q0,q1,q2},a)= δ(q0,a) U δ(q1,a) U δ(q2,a)
={q0,q1} U {q2} U {q3}
={q0,q1,q2,q3}-------new state
δ({q0,q1,q2},b)= δ(q0,b) U δ(q1,b) U δ(q2,b)
= {q0} U {q1} U {q3}
={q0,q1,q3}--------new state

δ({q0,q1,q2,q3},a)= δ(q0,a) U δ(q1,a) U δ(q2,a) U δ(q3,a)


={q0,q1} U {q2} U {q3} U {q3}
={q0,q1,q2,q3}----------old state
δ({q0,q1,q2,q3},b)=δ(q0,b) U δ(q1,b) U δ(q2,b) U δ(q3,b)
={q0} U {q1} U {q3} U {q2}
={q0,q1,q3,q2}----------old state
δ({q0,q1,q3},a)=δ(q0,a) U δ(q1,a) U δ(q3,a)
={q0,q1} U {q2} U {q3}
={q0,q1,q2,q3}----------old state

δ({q0,q1,q3},b)=δ(q0,b) U δ(q1,b) U δ(q3,b)


={q0} U {q1} U {q2}
={q0,q1,q2}----------old state
Transition table :

δ/∑ a b
q0 {q0,q1} {q0}
{q0,q1} {q0,q1,q2} {q0,q1}
*{q0,q1,q2} {q0,q1,q2,q3} {q0,q1,q3}
*{q0,q1,q2,q3} {q0,q1,q2,q3} {q0,q1,q2,q3}
*{q0,q1,q3} {q0,q1,q2,q3} {q0,q1,q2}
Let us say:
q0----A
{q0,q1}-----B
{q0,q1,q2}-----------C
{q0,q1,q2,q3}---------D
{q0,q1,q3}-----------E
 A is the initial state and C,D and E are final states since they contain q2 and q3 as member
which are final states of NFA

δ/∑ a b
A B A
B C B
*C D E
*D D D
*E D C
b
b
start a B a
A

C
b
b a
E a,b
a D
Given NFA is

δ/∑ a b
q0 {q0,q1} {q2}
q1 {q0} {q1}
*q2 {q0,q1}

Convert it to DFA
A:{q0}
*B:{q2}
C:{q0,q1}
*D:{q1,q2}

start a C a
A b
a b
b
b D
B
a,b
a ᶲ
ϵ-NFA to NFA without ϵ

 ϵ-closure- This function is defined as set of all those


states of the automata which can be reached from q
on a path labeled by ϵ i.e without consuming any
symbol
Conversion of NFA with ϵ- transition to NFA without ϵ-
transitions
 Consider the NFA with ϵ- transition

M={Q,∑,δ, q0,F)
Q={q0,q1,q2}
∑={a,b,c} and ϵ moves
initial state={q0}
F={q2}
δ/∑ a b c ϵ
q0 {q0} {ᶲ} {ᶲ} {q1}
q1 {ᶲ} {q2} {ᶲ} {q2}
*q2 {ᶲ} {ᶲ} {q2} {ᶲ}
Solution:
Step1: Find the states of NFA with ϵ transition
including initial state and final states as follows:

ϵ-closure(q0)={q0,q1,q2}
ϵ-closure(q1)={q1,q2}
ϵ-closure(q2)={q2}
Final states of NFA without ϵ-transition are all
those new states which contain final state of
NFA with ϵ-transition as member
So {q0,q1,q2},{q1,q2},{q2} are all final states.
 Step 2: We have to decide δ’ to find out transitions as follows:
δ’({q0},a)=ϵ-closure(δ(ϵ-closure(q0),a)
=ϵ-closure(δ(q0,q1,q2),a)
= ϵ-closure(δ(q0,a) U δ(q1,a) U δ(q2,a) )
= ϵ-closure(q0 U ᶲ U ᶲ)
= ϵ-closure(q0)={q0,q1,q2}
Similarly, δ’({q0},b)=ϵ-closure(δ(ϵ-closure(q0),b)
=ϵ-closure(δ(q0,q1,q2),b)
= ϵ-closure(δ(q0,b) U δ(q1,b) U δ(q2,b) )
= ϵ-closure(ᶲ U q2 U ᶲ)
= ϵ-closure(q2)={q2}
δ’({q0},c)=ϵ-closure(δ(ϵ-closure(q0),c)
=ϵ-closure(δ(q0,q1,q2),c)
= ϵ-closure(δ(q0,c) U δ(q1,c) U δ(q2,c) )
= ϵ-closure(ᶲ U ᶲ U q2)
= ϵ-closure(q2)={q2}
In same fashion we can write δ’({q1},a)= ᶲ
δ’({q1},b)= {q2}
δ’({q1},c)= {q2}
 δ’({q2},a)= ᶲ
 δ’({q2},b)= ᶲ

 δ’({q2},c)= {q2}
 So transition table for NFA without ϵ transition
will be as follows:

δ’/∑ a b c
*{q0} {q0,q1,q2} {q2} {q2}
*{q1} {q2} {q2}

*{q2} {q2}
ᶲ ᶲ
NFA without epsilon

a q1
q0

a/b/c

b/c
c q2
Consider the NFA given by the following diagram

1 0
0
start є є
q0 q1 q2

Find the equivalent NFA without є transition


Solution:
Step1: Find the states of NFA with ϵ transition
including initial state and final states as follows:

ϵ-closure(q0)={q0,q1,q2}
ϵ-closure(q1)={q1,q2}
ϵ-closure(q2)={q2}
Final states of NFA without ϵ-transition are all
those new states which contain final state of
NFA with ϵ-transition as member
So {q0,q1,q2},{q1,q2},{q2} are all final states.
 Step 2: We have to decide δ’ to find out transitions as follows:
δ’({q0},0)=ϵ-closure(δ(ϵ-closure(q0),a)
=ϵ-closure(δ(q0,q1,q2),0)
= ϵ-closure(δ(q0,0) U δ(q1,0) U δ(q2,0) )
= ϵ-closure(q0 U ᶲ U q2)
= ϵ-closure(q0) U ϵ-closure(q2) ={q0,q1,q2}U {q2}= {q0,q1,q2}
Similarly, δ’({q0},1)=ϵ-closure(δ(ϵ-closure(q0),1)
=ϵ-closure(δ(q0,q1,q2),1)
= ϵ-closure(δ(q0,1) U δ(q1,1) U δ(q2,1) )
= ϵ-closure(ᶲ U q1 U ᶲ)
= ϵ-closure(q1)={q1,q2}
δ’({q1},0)=ϵ-closure(δ(ϵ-closure(q1),0)
=ϵ-closure(δ(q1,q2),0)
= ϵ-closure(δ(q1,0) U δ(q2,0))
= ϵ-closure(ᶲ U q2)
= ϵ-closure(q2)={q2}
δ’({q1},1)=ϵ-closure(δ(ϵ-closure(q1),1)
=ϵ-closure(δ(q1,q2),1)
= ϵ-closure(δ(q1,1) U δ(q2,1))
= ϵ-closure(q1 U ᶲ)
 = ϵ-closure(q1)={q1,q2}

δ’({q2},0)=ϵ-closure(δ(ϵ-closure(q2),0)
=ϵ-closure(δ(q2),0)
= ϵ-closure(q2)
={q2}
δ’({q2},1)=ϵ-closure(δ(ϵ-closure(q2),1)
=ϵ-closure(δ(q2),1)
= ϵ-closure( ᶲ)= ᶲ

𝛿/∑ 0 1
*q0 {q0,q1,q2} {q1,q2}
*q1 {q2} {q1,q2}
*q2 {q2} ᶲ
0/1
0 1 0

start 0/1
1/0 q2
q0 q1

NFA without ϵ transition


convert the given epsilon NFA to NFA
without epsilons

0. 1
A ∊ B
0 0 0

F 1 G
NFA without Epsilon

𝛿/∑ 0 1
*A {A,B,F,G} {B}
*B {G} {B}
F {B} {F G}
G {B} {F G}
convert the given epsilon NFA to NFA
without epsilons

B C


A 0 0

1
1
G E
NFA without Epsilon

𝛿/∑ 0 1
A {G,E} {G}
B {G,E} {ᶲ}
C {E} {ᶲ}
G {B} {E}
*E {ᶲ} {ᶲ}
Minimization of DFA
 Any DFA defines a unique langugae , but the converse
is not true. For a given language, there may be many
DFA’s that accepts it.
 Minimization is a technique through which we can
minimize a given DFA to an optimum number of
states.
State categorization based on
productivity
 Productive state- State is said to be productive
if it adds any accepting power to the machine
that its presence and absence affect the
language accepting capability.

 Non-Productive State- These states don’t add


anything to the language accepting power of
the machine
Non productive states

 Dead State/Trap State


 Unreachable States- States that are unreachable
from the starting state
 Equal States- These are those states that behave
in same manner on each and every input string.
Formally, two states q1 and q2 are equivalent if
both 𝛿(q1,x) and 𝛿(q2,x) are final states or non
final states for all x∊∑*
Equivalence: To check equal States
 For two states (p,q),
 If length of input string is 0, states are said to be 0-
equivalent
 if length of input string is 1, states are said to be 1-
equivalent
 if length of input string is n, it is called n-equivalent
Minimize the given DFA
a
a
a B b C

A
a a b
b
D E
b
b
Procedure for Minimization:
partitioning method

 Step 1: Check for any dead state or


unreachable state and remove them

 Step 2: Draw the transition table


𝛿/∑ a b
A B D
B B C
C B E
D B D
E B D
Check for equivalent sets
 0 equivalent sets- Separate final and
non final states into two sets
 [ A B C D] [E]
 1 Equivalent sets
 For each of the states in 0 equivalent
sets, check transition on each symbol
whether the states are going to states
in same set or different set.
 [A B D] [C] [E]
 2-equivalent- repeat procedure done in 1 equivalent
 [A D] [B] [C] [E]
 3-equivalent- repeat procedure done in 2-equivalent
 [A D] [B] [C] [E]
 As 2 and 3 equivalent are exactly same, there is no
change, we can stop
Minimized Automata
a

b a b
B

{A D} a a C

E b
b
Minimize the DFA given below

a
b
Start b q3
q0 q2
b a
a
q1 b
a a
b
q5 a
q6
a a b
b
b
q4 q7
MINIMIZED DFA

b q1
q0 b
b
a a
a q6

a b
q5 q2
b a
Minimize the given DFA

q4
0 1
start 0
q0 q1 q2 0,1
q3
0 1 0,1

1 q5
0,1
 Step 1: Eliminate unreachable states
q2 and q4 are unreachable. So eliminate them
After elimination of unreachable states

0
start
q0 q1
q3
0 1 0,1

1 q5
0,1
 Equivalence sets:
 0-equivalence:

[q0 q1] [q3 q5]


 1-equivalence

𝛿(q0,0)=q1 𝛿(q0,1)=q3
𝛿(q1,0)=q0 𝛿(q1,1)=q3
Thus, q0 and q1 belong to same set after 1 equivalence
 Equivalence sets:
 1-equivalence for [q3 q5]

𝛿(q3,0)=q5 𝛿(q3,1)=q5
𝛿(q5,0)=q5 𝛿(q5,1)=q5
Thus, q3 and q5 belong to same set after 1
equivalence
 2-equivalence for [q0 q1] [q3 q5]

 sets show equivalence at 2 equivalence also


 Transition table
𝛿/∑ 0 1
q0 q1 q0 q3
q1 q0 q3
q3 q5 q3 q5 q3
q5 q5 q5
Final DFA

0,1
0
start 1
q0
q3
Minimize the DFA

 a a
start
A B

b b b

a C
minimization Procedure
 Unreachable states: There are no unreachable states

 Dead State: No dead states


 Equivalence sets
 0-equivalence:
[C] [A B]
 1-equivalence:
𝛿(A,a)=A 𝛿(A,b)=C
𝛿(B,a)=B 𝛿(B,b)=C
 States A and B are equivalent at 1 equivalence. Sets [C] [A B]
 2-equivalence
𝛿(A,a)=A 𝛿(A,b)=C
𝛿(B,a)=B 𝛿(B,b)=C. Equivalent at 2 equivalence. Sets [C] [A B]
 Transition Table
𝛿/∑ a b
*A A C
B B C
C C B A
Minimized DFA

 a
start
A

b b

a C
Minimize DFA

a
start b
A B

b b a a

C D b
a
minimization Procedure
 Unreachable states: There are no unreachable states

 Dead State: No dead states


 Equivalence sets
 0-equivalence:
[B C D] [A ]
 1-equivalence:
𝛿(B,a)=D 𝛿(B,b)=A
𝛿(C,a)=D 𝛿(C,b)=A
 States B and C are equivalent at 1 equivalence.
 𝛿(D,a)=B 𝛿(D,b)=D
 State D is not equivalent to B. Thus sets after 1 equivalence are:
 [B C] [D] [A]
 2-equivalence
As can be seen, B and C show equivalence in 2 equivalence test.
Thus sets after 2-equivalence are:

[B C] [D] [A]
Transition Table Of Minimized DFA

𝛿/∑ a b
*A A C B
B D A
C D A
D B D
FA with outputs
 DFA and NFA are the automata which do not produce
any outputs. There are two automata which produce
output. These are:
 Moore machine
 Mealy machine
Moore machine
 It is the FA in which one output is
associated with each state. In moore
machine every state of this finite
machine has a fixed output
Mathematically moore machine is a six-tuple machine and defined as
M0={Q,∑,∆,δ,λ’,q0}

Q:A non empty finite set of state in M0


∑: A non empty finite set of input symbols
∆: A non empty finite set of outputs (output alphabet)
δ: it is transition function which takes two arguments as in finite automata,
one is input state and another input symbol
δ: Q x ∑ Q
λ’: is a mapping function which maps Q to ∆ giving the output associated
with each state
λ’: Q ∆
q0: initial state
Representation of Moore machine
 Moore machine can be represented by transition table
as well as transition diagram same as finite automata

Present state Next state output


a=0 a=1
q0 q3 q1 0
q1 q1 q2 1
q2 q2 q3 0
 Where λ’(q0)=0,
q3 λ’(q1)=1, λ’(q2)=0
q3 and λ’(q3)=0.
q0 0there is no concept of final state in moore
machineas we consider output for each state.
For input 011, the machine will give output as:
0 1 1
q0 q3 q0 q1
output
0 0 0 1
1 0
0
1 q1
q0

0
1
1 0
0 q3 0
q2
0 1
Output for every state is written just above the state. If the input string is
n then output will be (n+1). This is because without consuming any
input, q0 is going to give output.
Mealy machine
 It is finite automata in which output is associated with
each transition . In mealy machine every transition for
a particular input symbol has a fixed output.
Mathematically mealy machine is a six-tuple machine and defined as
M0={Q,∑,∆,δ,λ’,q0}

Q:A non empty finite set of state in Me


∑: A non empty finite set of input symbols
∆: A non empty finite set of outputs (output alphabet)
δ: it is transition function which takes two arguments as in finite automata,
one is input state and another input symbol
λ’: is a mapping function which maps Q x ∑ to ∆ giving the output
associated with each transition
q0: initial state
Representation of Mealy machine
 Mealy machine can be represented by transition table
as well as transition diagram same as finite automata

Present state For input a=0 For input a=1


state Output state output
q0 q3 0 q2 0
q2 q1 1 q4 0
q3 q2 1 q1 1
 Where λ’(q1,1)=0,
q4 λ’(q1,1)=0, λ’(q2,0)=1
q4 and λ’(q2,1)=0.
1 q3 0

λ’(q3,0)=1, λ’(q3,1)=1, λ’(q4,0)=1 and λ’(q4,1)=0
a/0
b/0
a,b/1
start a/1 b/0
q1
q0 q2

For an input string ab,


a b
q0 q1 q2
1 0 Output
Design a Moore machine that counts number of
a’s in input string over alphabet ∑={a,b}
 We associate 1 with every occurrence of a and 0 for b.

b a
a
q0 q1

b
Moore machine Design
 Design a Moore machine that accepts all the
strings on ∑={a,b} with substring “ab”

 We can associate an output 1 with a substring


ab and for others we can associate an output of
0. As we can see inorder to include all
occurrences of ab in a string, we have to create
a FA that includes all strings that end with ab.
 Then we will add the outputs to the states in
such a way that whenever ab occurs in our
input string, output should be a 1 and for all
other strings output can be 0.
b a

a b C,1
A,0 B,0
a
b

States a b Output
A B A 0
B B C 0
C B A 1
 Design a Moore machine that accepts all the
strings on ∑={a,b} with substring “baa”

 We can associate an output 1 with a substring baa


and for others we can associate an output of 0.
Inorder to include all occurrences of baa in a string,
we have to create a FA that includes all strings that
end with baa.
 Then we will add the outputs to the states in such a
way that whenever baa occurs in our input string,
output should be a 1 and for all other strings output
can be 0.
a b b
b a C,0 a D,1
A,0 B,0
b
a

States a B Output
A A B 0
B C B 0
C D B 0
D A B 1
 Design a Moore machine that accepts all the
strings on ∑={0,1} which end with 10 with an
output of A and those ending with 11 with an
output of B and rest with an output of C.
 Solution:
 As outputs are given as A,B,C and ∑={0,1} ,
 We can take Q={X,Y,Z}
 For strings ending in 10, output is A
 For strings ending in 11, output is B
 Else, output C
1
0 W/B
1
1
X/C Y/C
0

1
Z/A

0
States 0 1 Output
X X Y C
Y Z W C
Z X Y A
W Z W B
 Design a Moore machine that accepts all binary
numbers and give output the remainder
obtained by dividing the number by 3
(remainder modulo 3).
 Solution:
 As given , the outputs are: {0,1,2} as these are
the remainders we get when we divide any
binary number with 3 and ∑={0,1}
 We can take Q={X,Y,Z}
0 1

1 0 Z,2
X,0 Y,1

0
1

States 0 1 Output
X X Y 0
Y Z X 1
Z Y Z 2
Shortcut for previous problem

 Whenever we have to create an FA for the statement


requiring remainder calculation, we can use the
shortcut to create transition table as:
 Number of states will be equal to the number of
remainders that can be obtained which is indirectly
equal to the number by which we have to divide e.g
for modulo by 3, we will have 3 states.
 For each state, we can associate one output which
will remain constant.
 Fill the columns of transition table in same sequence
in which you have created the states e.g q0,q1,q2,q3
and repaet the sequence in each row.
Mealy Machine Design
 Design a mealy machine that recognizes the
double occurrence of symbol 0 in input string
w Є ∑* where ∑={0,1}
0/1
0/0 q1
q0

1/0 0/0

1/0
q2 1/0
 Design a mealy machine which checks
whether the number of b’s in the given
string are even or odd
 Solution: we can assume that even
number of b’s will give output 0 and odd
number of b’s will give output 1.

b/1
q0 q1

b/0
 Design a mealy machine to calculate one’s
complement of binary number assume that
string is read from LSB to MSB
 Solution: As we know, one’s complement of
a binary number is generated by changing 1
to 0 and 0 to 1 bit in the string.
0/1

1/0
q0
 Design a mealy machine to calculate 2’s
complement of binary number assume that
string is read from LSB to MSB and discard
end carry if any
 Solution: If we take 2’s complement of a
binary number and LSB of the number is 0,
we obtain 2’s complement by keeping all 0’s
as such till first 1 is reached which is again
retained and rest of the string is
complemented
 If LSB of the number is 1, we obtain 2’s
complement by keeping first 1 intact and
rest of the string is complemented
0/0
0/1

1/1
q0 q1 1/0
 Equivalence of Mealy and Moore
Machines
Convert the following moore machine into
equivalent mealy machine
1

0 a q1 1
a a,b
q0 q3
b
0
b b
q2
a
Steps:

 Construct a transition table for the given


moore machine

Present Next State Output


State
a b

q0 q1 q2 0
q1 q3 q2 1
q2 q2 q3 0
q3 q3 q3 1
For every input symbol, we form the pair
consisting of the next state and corresponding
output and reconstruct the table for mealy
machine
Present a b
State state output state output

q0 q1 1 q2 0
q1 q3 1 q2 0
q2 q2 0 q3 1
q3 q3 1 q3 1
a/1 q1
a/1 a/1,b/1
start
q0 q3
b/0

b/0 b/1
q2
a/0
Convert the given moore machine to
equivalent mealy
a,b
q1/1

a a

b
q0/0 q2/0

b
Solution:
a/1,b/1
a/1 q1
q0
a/1
b/0 b/0

q2
 Moore equivalent of given mealy
machine
Exercise: Convert the given mealy machine into
moore machine

b/1
a/0 a/1
q1
b/0
q0 a/1 q2

b/0 a/0
b/1 q3
Construct the transition table for the
given mealy machine

Present Next State


State
input a input b
state output state output

q0 q2 1 q3 0
q1 q0 0 q1 1
q2 q1 1 q2 0
q3 q2 0 q0 1
 For every state qi, we look into the next
state column and determine the number
of outputs associated with qi in that
column.
 We split qi into several different states
number of such states being equal to
number of outputs associated with qi.
 In the given example, we will split q0,and
q2 as they have different outputs
Present input a input b
State state output state output

q00 q21 1 q3 0
q01 q21 1 q3 0
q1 q00 0 q1 1
q20 q1 1 q20 0
q21 q1 1 q20 0
q3 q20 0 q01 1
Present Next state output
State
i=a i=b

q00 q21 q3 0
q01 q21 q3 1
q1 q00 q1 1
q20 q1 q20 0
q21 q1 q20 1
q3 q20 q01 0
 Solution:
b
1
a
0 a q1 0
a b
q00 a q20

b a
a b
1 0 1
b a
q 0
1
q3 q21

b
Convert the given mealy machine
into equivalent moore machine
Current Input Symbol
State a b
Next state Output Next State Output

q0 q1 1 q2 1
q1 q2 0 q0 1
q2 q0 1 q3 0
q3 q3 0 q1 1
Solution:
Current Input Symbol output
State
a b

q0 q1 q21 1
q1 q20 q0 1
q20 q0 q3 0
q21 q0 q3 1
q3 q3 q1 0
Regular expression
 The simplest mechanism to define a language is to
express its grammatical rules through a regular
expression.
 A regular expression generates a set of strings, known
as a regular set, and all the strings belonging to a
regular set are valid in the language, while others are
not
Definition of regular expression

 The set of regular expression is defined by the


following rules:
 Every letter ∑ can be made into a regular
expression, null string, є itself is a regular
expression
 If r1 and r2 are regular expression, then
 (r), r1r2,r1+r2,r1*,r1+ are also regular
expressions
Correspondence between Regular
expression and Regular Set
 A regular expression generates a set of strings,
known as a regular set. This set is the language
corresponding to a regular expression and is
also known as regular language.
 A regular expression a* means any
number of repititions of a, that is, from
zero to infinity. Corresponding regular
set is {ϵ,a,aaa,aaaaaa……}
 A regular expression (a+b) means string
can be either a or b. Regular set is
{a,b}.
 (a+b)c---strings will start with a or b and
will end in c {ac, bc}
 Regular set corresponding to regular
expression ϵ is {ϵ}
 Regular set for empty or no regular
expression is null set represented as ᶲ
 Write a regular expression for the
language
 L={aa , aaaa, aaaaaaaa,……..}
 Solution:
 As set contains multiple occurrences of aa
with minimum string aa,
 R=aa(aa)* or (aa)*aa
=aa(aa)* + (aa)*aa
Write a regular expression over alphabet (a,b,c) containing
atleast one a and atleast one b
 The legal regular expression for the problem can be represented by strings of the form
 1 a 2 b 3 where 1,2 and 3 can be replaced by any combinations of a,b,and c.Any
combination of a,b or c is denoted by (a+b+c)*
 So regular expression is
 r= (a+b+c)*a(a+b+c)*b(a+b+c)* + (a+b+c)*b(a+b+c)*a(a+b+c)*
 If r=r1+r2, minimum string for r1 will be ab and for r2 is ba
Write a regular expression for the set of strings of 0’s and 1’s
whose 10th symbol from the right end is 1

 By analyzing the problem, it is clear that 10th symbol from right has to
be 1 , Thus all symbols till 10th symbol from right can be either 1 or 0.
also symbols 11 onwards can also be either 0 or 1
 So regular expression is:
 r=(1+0)*1(1+0)9
Write a regular expression for the set of strings of 0’s
and 1’s not containing 101 as substring

 r= (0*1*00)*0*1*
Write a regular expression over alphabet {a,b}for the set of strings
with even number of a’s followed by odd number of b’s that is for
language
L={a2nb2m+1: n>=0,m>=0}

 r=(aa)*(bb)* b
Write regular expression for the language
L={w є {0,1}*: w has no pair of consecutive zeros}

 r1=(1*011*)*(0+ є)+1*(0+ є)

 OR

 r1=(1+01)*(0+ є)
Write regular expression for the language
L={anbm/n>=4,m<=3}

 r=a4 a*(є +b +b2 +b3)


Write a regular expression for the language
L={anbm: n+m is even}
 We know n+m can be even in two cases:
 Case 1: Both n and m are even
 Case 2: Both n and m are odd
 Case 1:Regular expression for case 1
 r1=(aa)*(bb)*
 Case 2: Regular expression for case 2
 r2=a(aa)*b(bb)*
 Thus r= r1 + r2
 =(aa)*(bb)*+a(aa)*b(bb)*
Write regular expression for the language
L={abn w : n>=3, w ϵ (a,b)+}

 Clearly every string of language L starts with a


followed by atleast three bs followed by atleast one a
or one b
 R=ab3b*(a+b)+
Regular expressions
6. starts and ends with
same symbol
1. strings start with ab 7. starts and ends with
2. strings start with bba different symbol
3. strings end with abb 8. |w| =3
4. contains substring 9. |w| >=3
aab 10. |w| <3
5. starts and ends with
a
Solutions

1. ab(a+b)* 8. (a+b)(a+b)(a+b)
2. baa(a+b)* 9. (a+b)(a+b)(a+b)
3. (a+b)*abb (a+b)*
4. (a+b)*aab(a+b)* 10. ∊+(a+b)+(a+b)2+
5. a+a(a+b)*a (a+b)3
6. a+a(a+b)*a +b+
b(a+b)*b
7. a(a+b)*b+b(a+b)*a
Regular Expressions
1. |w|a=2 6. |w| mod 3=0
2. |w|a>=2 7. |w| mod 3=2
3. |w|a<=2 8. |w|b mod 3=0
4. 3rd symbol from left end 9. |w|a mod 3=1
is b 10. |w|b mod 3=2
5. 28th Symbol from right
end is a
Solutions

1. (b)*a (b)* a(b)* 6. [(a+b)3 ]*


2. (a+b)*a(a+b)* 7. (a+b)2 [(a+b)3]*
a(a+b)* 8. a* (a*b a* b a*)*
3. ∊+(b)*a(b)*+ 9. b*ab*(b*ab*ab*ab*)
(b)*a(b)*a(b)* 10. a*ba*ba*(a*ba*ba*
4. (a+b)*+(a+b)*+b+ ba*)*
(a+b)*
5. (a+b)*a(a+b)27
∊-NFA for Regular Expressions
 We can construct ϵ-NFA for various regular
expressions. Some of these generalized NFAs
are

 NFA with ϵ for r+s where R and S are automata for regular expressions r
and s respectively. First we reach the starting state of R and S and reach
the final state of either R or S.

R
ϵ ϵ

ϵ
S ϵ
 NFA with ϵ for r*

ϵ ϵ
R

ϵ
 NFA with ϵ for r.s

The final state of R will be the starting state of S. There will be


one staring state for whole automate and one final state for
whole automata.

ϵ ϵ
R ϵ S
Find the automation for regular expression
a.(a+b)*.b.b

Automation for a+b


ϵ a
ϵ

ϵ b ϵ
Find the automation for regular expression
a.(a+b)*.b.b

Automation for (a+b)*


ϵ
a
ϵ ϵ
ϵ ϵ

ϵ
b ϵ
ϵ
Find the automation for regular expression
a.(a+b)*.b.b

Automation for a.(a+b)*


ϵ
a
ϵ ϵ
a
ϵ ϵ

ϵ ϵ
b
ϵ
Find the automation for regular expression
a.(a+b)*.b.b

Automation for a.(a+b)*.b


ϵ
a
ϵ
a ϵ
ϵ
ϵ

ϵ b
b ϵ
ϵ
Find the automation for regular expression
a.(a+b)*.b.b

Automation for a.(a+b)*.b.b


ϵ
a
ϵ
a ϵ
ϵ
ϵ

ϵ b
b ϵ
b
ϵ
Construct ϵ-NFA for the regular expression
(0+1)*1(0+1)

 First draw for (0+1)*


 Then combine with 1
 Then combine with (0+1)
Construct an ϵ-NFA for regular expression (a+b)*cd*e

a
C D
∊ ∊
A ∊ B ∊ c
G H I

∊ b ∊ ∊
E F d


∊ L K J
M d
N e


Arden’s Theorem
 Let P and Q be two regular expressions over alphabet ∑. If P
does not contain null string ϵ, then
 R=Q+RP
has a unique solution that is R=QP*
Arden’s theorem for finding regular
expressions for given automata

Assumptions for application of Arden’s


theorem to a finite automata
 The finite automaton should not contain

any ϵ moves
 There should be exactly one initial state

 State in a finite automaton is used like a

regular expression.
Find the regular expression for transition
diagram given
a

start
q2
q1
b
a
a b a,b
q4
q3
b
 Solution: The transitions required to reach a particular state in
given finite automaton are:
 q1=q2b+q3a+ϵ
q2=q1a
q3=q1b
q4=q2a+q3b+q4a+q4b
Put q2 and q3 in q1
q1=q1ab+q1ba+ϵ
q1=ϵ+q1(ab+ba)
q1=ϵ(ab+ba)* (using arden’s theorem)
Since q1 is the only final state , the set of strings accepted by
given finite automaton corresponds to the regular expression is
(ab+ba)*
So required regular expression is (ab+ba)*
Construct a regular expression corresponding to the
state diagram given
0
start 1
q1 1

q2
0 1

q3
0
 Solution:
q1=q10+q30+ϵ
q2=q11+q21+q31
q3=q20
q2=q11+q21+(q20)1=q11+q2(1+01)
q2=q11(1+01)* (using Arden’s theorem)
So q1=q10+q30+ ϵ=q10+q200+ ϵ
=q10+(q11(1+01)*)00+ ϵ
q1=q1(0+1(1+01)*00) +ϵ
q1= ϵ(0+1(1+01)*00)*
q1=(0+1(1+01)*00)*
As q1 is the only final state,
Find regular expression for the for the given DFA

0
1
1
q1 q2

0 0
0

1
q4 q3
1
 Solution: q1=q10+q30+q40+ϵ
q2=q11+q21+q41
q3=q20
q4=q31
Now q4=q31=q201
Using q2 equation, we get q2=q11+q21+q2011
=q11+q2(1+011)
q2=q11(1+011)*
=q1(1(1+011)*)
Put q3 and q4 in q1 equation q1=q10+q200+q2010+ϵ
=q10+q2(00+010)+ ϵ
=q10+q11(1+011)*(00+010)+ ϵ
q1= ϵ(0+1(1+011)*(00+010))*
Put q2 in q4 equation q4=q11(1+011)*01=q1(1(1+01)*01)
 Find the regular expression corresponding to
the given finite automaton

c c

a a
S A F
b
 Solution: The transitions required to reach various states in
the finite automaton
 S= ϵ +Sc
 A= Sa + Ac + Fa
 F=Ab
 Using Arden’s theorem, S=ϵ + Sc=ϵc*=c*
 Substituting value of S in A=c*a +Ac + Aba=c*a+A(c +ab)
 Using Arden’s Theorem, A=c*a(c + ba)*
 Substituting value of A in F= c*a(c + ba)*b
 Since there are two final states, F and A, regular expression for
the finite automaton are c*a(c +ba)* and c*a(c +ba)*b
 Find regular expression corresponding to the
finite automaton given

A b
a c
a,b

S B a F
b
Solution:
 (a+b)*(ba+ab)c*
Construct Regular expression for DFA
given below

a,b q3
Start a,b a
q1 q2
b
a,b q4
Solution
q1=ε
q2=q1(a+b)+q3(a+b)+q4(a+b)
q3=q2a
q4=q2b
(Computing q2 as computation of q3 will complicate the problem)

q2=q1(a+b)+q3(a+b)+q4(a+b)
=ε(a+b)+q2a(a+b)+q2b(a+b)
=ε(a+b)+q2(a(a+b)+b(a+b))
=ε(a+b).(a(a+b)+b(a+b))*
= (a+b).(a(a+b))*. (b(a+b))*
q3= (a+b).(a(a+b))*. (b(a+b))* b --------- ans
Algebraic laws for regular expressions
 P+Q=Q+P------ commutative law
 (P+Q)+R=P+(Q+R)-----associative law
for union
 (PQ)R=P(QR)----- associative law for
concatenation
 Identities: ᶲ + L=L + ᶲ=L
 ϵL=Lϵ=L
 ᶲL=Lᶲ= ᶲ
 P(Q+R)=PQ+PR Left Distributive law
 (Q+R)P=QP+RP Right Distributive law
 Idempotent law L+L= L
 Laws of Closure

(L*)*=L*
L+=LL*=L*L
L*=L+ + ϵ
Identities of regular expression
I1 ᶲ+r=r
I2 ᶲr=rᶲ= ᶲ
I3 ϵr=r ϵ=r
I4 ϵ*=ϵ and ᶲ*=ϵ
I5 r + r=r
I6 r*r*=r*
I7 rr*= r*r=r+
I8 ϵ +rr*=r*=ϵ+r*r
I9 (pq)*p=p(qp)*
I10 (p+q)*=(p*q*)*=(p*+q*)*
I11 (p+q)r=pr+qr
Prove that t+RR*=R* where t is a null string and
R is a regular expression
Two regular expressions P and Q are equivalent if
P and Q represent the same set of strings
From Arden’s theorem, R’=Q+R’P is R’=QP*
t+RR*=R* comparing it with Arden’s theorem
Q=t
R’=R
P=R*
Solution of this equation will be R’=QP*
R1=QP*
= tR* = R* hence proved
Equivalence of two FAs
 Two finite automata over ∑ are equivalent if
they accept the same set of strings over ∑.
When the two finite automata are not
equivalent, there is some string w over ∑
satisfying the following: One automaton
reaches a final state on application of w
whereas the other automaton reaches a
nonfinal state.
Equivalence of Two Finite Automata
• Two FA’s are equivalent if they accept the same set of strings
over any input.
Steps for comparison:
1. Construct a comparison table with entries (q,q’) where qЄM and q’ ЄM’
{M and M’ are two different machines}
2. Look for corresponding entries in a-column and b-column
reachable by(q,q’).
3. The pairs (q,q’) either belongs to both final states or both non final
states.
4. We repeat the construction by considering the pairs in second and
subsequent columns which are not in first column. There can be
two cases:
5. If there is a combination of non final and final state or vice versa,
we terminate the construction and conclude that two automatas
are not equivalent
6. Go on entering in first column till no new entry is left in second
and subsequent columns which are not in first column
Equivalence of Two Finite Automata
M and M’ are equivalent
(q,q’) (qc,q’c) (qd,q’d)

(q1,q4) (q1,q4) (q2,q5)

(q2,q5) (q3,q6) (q1,q4)

(q3,q6) (q2,q7) (q3,q6)

(q2,q7) (q3,q6) (q1,q4)

Both are non final Both are final


Equivalence of FA’s
Grammars-Phrase structure Grammars
 A phrase structure grammar is a collection of three
things:
 A finite alphabet ∑ of letters called terminals.
 A finite set of symbols called non terminals that include
the start symbol , S
 A finite set of production of the form
String1 String2
Where String1 can be any string of terminals and non
terminals that contains atleast one non terminal and
string2 is any string of terminals and non terminals.
 The set of terminals ∑ refers to those symbols that
ultimately make the sentences in the language.

 Set of non-terminals refer to those symbols that do


not exist in the sentences of a language but are
used to define the structure of a valid sentence in
the language.
 A set of productions constitute rules of the grammar
that specify how sentences are to be constructed in
the grammar. A production rule is of the type
α β
These rules state how string α can be replaced by
string β. The transitive application of rule leads to
formation of a sentence consisting of terminals.
The starting symbol is a special non terminal with
which the sentence generation process begins.
Notations used for representing a
Grammar
 A non terminal is represented by a
capital letter
 A terminal is represented by a small
alphabet
 The two sides (LHS and RHS) of a
production are separated by
symbol.
 S is the starting symbol unless
otherwise specified.
Example: Design a Grammar generating the language
L={wcwR | w ϵ (a,b)+ }
 It can be seen that in the language L, every string w belonging to the language is
symmetric over the symbol c. In addition, (a,b)+ indicates that w contains atleast one
symbol.
 Thus we have set of productions P as:

 S aSa| bSb | c

 Thus , grammar of the language is given as:


 G=( V={S,X} , ∑ = {a,b,c}, P, S )
Design a grammar for the language
L={anbn | n ≥1 }
 S aSb | ab
 Thus the grammar for the language is
 G={V={S},∑={a,b},P,S}
Design a grammar for the language
L={ancmbn | m,n ≥1}

 S aSb | aXb
 X cX | c
 Thus the grammar for the language is
 G={V={S,X}, ∑={a,b,c}, P, S}
Design a grammar for the language
L={anbm | m,n ≥1}

 S XY
 X aX|a
 Y bY|b
 Thus the grammar for the language is
 G={V={S,X,Y}, ∑={a,b}, P, S}
Define Grammer for language
L={an | n>=0}
Sol:

S aS | ∊
Define grammer for language containing alphabets a
and b with atleast 2 symbols

Sol: As we know, regular expression for the


language:
(a+b)(a+b)(a+b)*
S AAB
A a/b
B aB/bB/∊
Define grammer for language containing alphabets a
and b atmost 2 symbols

Sol: As we know, regular expression for the


language:
(a+b+∊)(a+b+∊)
S AA
A a/b/∊
Chomsky classification of Grammars
 Phrase structures grammar depends on its production system to derive the strings in
the associated language. Thus the entire onus of generating the strings associated
with a language and checking whether they belong to the language or not lies with the
production system of corresponding phrase structure grammar. Keeping this
dependency on the production system in view, Chomsky classified the phrase structure
grammar into four categories:
 Type-0 grammar (unrestricted grammar)
 Type-1 grammar (context sensitive )
 Type-2 grammar (Context free)
 Type-3 grammar (regular)
 Type-0 grammar- This grammar is referred to as
unrestricted grammar. A language associated
with type 0 grammar is known as Turing
recognizable language. The type 0 grammar
can contain productions of the form:

(∑ U V)* (∑ U V)*

 Thus in type 0 grammar both LHS and RHS of every


production consist of a combination of terminals and non
terminals and there is no restriction.
 Type 1 grammar- This grammar is referred to as
context sensitive grammar. The language associated
with type-1 grammar is known as context sensitive
grammar. The type 1 grammar contains productions of
the form:

 (∑ U V)* (∑ U V)*

 Thus in type 1 grammar both LHS and RHS of every


production consist of a combination of terminals and non
terminals. However, there is a restriction that the size
of string on RHS of the production should either
be greater than or equal to the size of string on
the LHS of the production. The tool used for
recognition of the language based on type -1 grammar
is linear bounded automaton.
 Type-2 grammar- This grammar is referred to as
context free grammar. The language associated
with type-2 grammar is known as context free
language. Type-2 grammar contains productions of
the type

A (∑ U V)*

Where A is some non terminal in the grammar. Thus,


LHS of every production in type-2 grammar
consists of one non terminal only while RHS
consists of a combination of terminals and non
terminals. The tool used for recognition of type-2
grammar is pushdown automata.
 Type-3 grammar- This type of grammar is called
regular grammar. This language associated with
type 3 grammar is called type-3 language.
 Type -3 grammar consists of the production of the
type

A a
A aB

 Where A and B are some non terminals and a is some


terminal in the grammar. Thus is type-3 grammar the
LHS of the production consists of a non
terminal only and RHS contains either a single
terminal or a terminal followed by a single non
terminal. The tool used for recognition of the
language based on the type -3 grammar is a finite
automaton
Classifying the type of grammar
 A grammar is classified by the highest category of
production. Thus it can be inferred that:
 Type 0 grammar includes type 1, type 2 and type 3
grammars
 Type 1 grammar includes type 2 and type 3 grammars
 Type 2 grammar includes type 3 grammar.
Containment relationship among
languages

Turing recognizable language


Context-sensitive language
Context free language

Regular languages
Sentential forms
 Starting symbol plays an important role in deriving the final sentence or a string . The
final string is obtained by transitive application of productions in the given grammar
beginning with the starting symbol S.

 Given a grammar G, with S as starting symbol, any intermediate or final string


consisting of non terminals, terminals or both that can be obtained from the starting
symbol is known as sentential form.
Derivation and recursive approach for
generating a string
 In derivation approach, process starts with a starting sentential form S and is replaced
by its corresponding productions which again contain some variables. These variables
are again replaced by their productions which in turn contain more variables. The
process continues till we obtain only terminals. In this approach, we obtain derivation
tree or parse tree.
 In recursive inference approach, the string w is taken and to check if w is element of L
for some CFL, an inverted tree is made. If inverse ends up in starting sentential form S
then w is element of L. In this approach we obtain inverted derivation tree.
Leftmost and Rightmost Derivation of
Strings
Using derivation approach, a string w can be derived in two ways:

 By using left most derivations

 By using rightmost derivations


Leftmost and rightmost derivations
 Inorder to restrict the number of choices of
replacement of variables, if at each step we replace
the leftmost variable by one of its production bodies,
such a derivation is called a “leftmost derivation” and
we indicate that a derivation is leftmost derivation by
using the relation

lm rm
 If rightmost variable is replaced by one of its
production bodies, such a derivation is called a
“rightmost derivation”
Parse Tree
 There is a tree representation for derivation that has proved
extremely useful. It is the second way of showing derivations,
independent of the order in which productions are used is also
called derivation tree. A parse tree is an ordered tree in which
nodes are labeled with the left sides of productions and in
which the children of a node represent its corresponding right
sides.
 Definition: let G=(Vn,Vt,P,S) be a context free grammar. An
ordered tree for this CFG is a derivation tree if and only if it has
the following properties:
 The root is labeled by starting non terminal of the CFG that is S
 Every leaf of the ordered tree has a label from Vt U {є}
 Every Interior node of ordered tree has a label from Vn
 A vertex with a child labeled є can have no other children.
Derivations
 If α β is a production of P in CFG and
a and b are strings in (Vn U Vt)*, then
 aαb G aβb
 We say that production α β is applied
to the string a α b to obtain a β b
 α1 G α2, α2 α3, α3 α4,………..
G G
αm-1 G
αm
 α1 G* αm
Let G be CFG
S bB/aA
A b/bS/aAA
B a/aS/bBB
For the string bbaababa find
1) left most derivation
2) rightmost derivation
3) parse tree

 S bB bbBB bbaB bbaaS b2a2bB


b2a2baS b2a2babB b2a2baba

 S bB bbBB bbBaS bbBabB


b2BabaS b2BababB b2Bababa b2a2baba
For the grammar G with production rules
E E+E
E E* E
E id
where V={E}, T={id}, obtain the derivation and the
derivation tree for the string w=id+id*id
 Sol: E E+E
 id+ E * E
 id + id * E
 id + id * id

For G=({S,A}, {a,b},S,P) where P is,
S aAS | a
A SbA | SS | ba
Find the leftmost and right most derivations for the
string aabbaa. Also draw the parse tree
 Leftmost derivation of aabbaa
 S aAS S
 aSbAS a A S
 aabAS S b A
 aabbaS a b a
 aabbaa a
Rightmost derivation of aabbaa

 S aAS S
 aAa a A S
 aSbAa S b A a
 aSbbaa a b a
 aabbaa
Example: Let there be a grammar G and is represented by the following
production
S ASA| BSB | a| b
A a
B b

 Show that string aaabbbaaa is element of L using a) derivation approach b)


recursive inference approach
 S

 A S A

 a A S A a

 a A S A a

 a B S B a

 b b b
Show that string aaabbbaaa is element of L using a) derivation approach b) recursive
inference approach

a a a b b b a a a
A A A B S B A A A
S
S
S
S
Recursive approach

You might also like