0% found this document useful (0 votes)
67 views100 pages

Foundations of Computation Theory

The document introduces the theory of computations, focusing on mathematical models such as finite automata, pushdown automata, and Turing machines. It covers basic preliminaries in set theory and graph theory, defining concepts like sets, relations, and operations on sets. The document also discusses finite state systems and their applications in programming, particularly in lexical analysis.

Uploaded by

Shivam Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views100 pages

Foundations of Computation Theory

The document introduces the theory of computations, focusing on mathematical models such as finite automata, pushdown automata, and Turing machines. It covers basic preliminaries in set theory and graph theory, defining concepts like sets, relations, and operations on sets. The document also discusses finite state systems and their applications in programming, particularly in lexical analysis.

Uploaded by

Shivam Pandey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Keld

ibsekana
(cA)1
Preliminaries

heory of computations is based on mathematical cornputations. These


computations are used to represent various mathematical models. In this subject we
will study many interesting models such as finite automata, pushdown autornata,
turring machines. This subject is a fundamental subject, which is very much cluse to
the subjects like compilers, operating system, language processors. pattern
recognition system This subject represents the importance of regular expressior:
and context free grammar used in compilation of the user program.
In this chapter we will learn some basic preliminaries. These prelimiraies afe
based on graph theory and set theory. The terms and detinitions whicn we ar:
going to use throughout the book, are discussed in this chapter. Let us start win ine
set theory first.

1.1 Seti
Set is defined as a collection of various objects. These objects are catied tie
elements of the set.
For example : A set of vowels. This set has the elements such as a, e, i, , l. t
The set is denoted by capital letter. Let us see how the set o: vowes e
represeted,
A = a, e, i, o, u

The elements of the set are grouped into the curly brackets and Separated by
Commas. The set can be finite or infinite.

Finite set : A finite set is aset of finite number of elements. As you have seer
set of vowels is a inite set of elements.

(1)
Prellminarles
Theory of Computation If
difference operation
Infinite set : The infinite set is a collection of all the elements which are infinite iii) A B is the and B = { 2, 3, 4 |
then
in nmber. A = {1, 2, 3 ), in set A b1h ne.
which are there
elements
A-- B = |1} ie.
For example : Set of natural numbers. in set B

lí we want to describe any elenent of a set we can write it as,


{a! a is alwavs even number }
1.1.2 Cardinality of Sets number of members in the
sat
set is nothing but the
This can be read as the element a of set A such that A (a)
i.e. element a is The cardinality of the
alwavs even number. A
Ag
then we can denote it as beA
If we want tO show that b is any element in set A
i.e. b belongs to A.
element of set A is
Subset: The subset A is called subset of set B if every
set B. But reverse is not true. It is denoted as AcB.
present in
empty set. It is denoted by
Emptv set : The set having no elements in it is called
A = ! i,the emptt set is written as . Fig. 1.1 Cardinality of two
sets
[ or ^ character. Nulelerment
Null string : The null element is denoted by same cardinality as there is one to one :
These sets A1 and A2 have the
means no value character. Remeber [ # .
of the elements of Al and A2.
subsets of its elements.
Power set:The powerset is a set of all the
For exampie : A = |1, 2, 3 } 1.2 Relations this is true in
(2, 3}, {1, 2,3}} Refationship is a major aspect between two objects, even
Then the power set Q ={¢.{1}, {2} {3} {1, 2} {1, 3}. elements Iife: One objeçt can be related with te other obect
by a 'mother of relatie
to 2" where n is number of relationship.
The number of elements are always equal O there are'. those twO objects form a pair based on this certain
original set. As in set A there are 3 elements, therefore in powerset
in represents
Definition : The relation R is a collection for the set S which
2 = 8 elements.
of elements.
1.1.1 Operations on Set Ko
For example : (a, b) is in R. We can represent their relation as a
On the séts following operations are possible. Component of each pair is chosen from a set called doman and second
i) A B is union operation - If of each pair is chosen from a set called range.
then
A = | 1, 2, 3 B = 1, 2, 4) 1.2.1 Properties of Relatioms
both the sets.
AUB = |1, 2,3, 4 i.e. combination of A relation R on set S is

i) A B is intersection operation - If 1: reflexive if iRi for all iin S.


A = {1, 2, 3 and B = |2,3, 4 | then [Link] if iRi is false for all i in S.
AnB = (2, 3 | ie. collecton of common elements from both 3. transitive if iRj and ¡Rk imply iRk.
the sets.
4 Theory of Computation Preliminarles

Svmmetic if iRj implies jRi 1.3 Graphs


5
Asvmmetnc if iR} implies that /Ri is talse. Agraph, denotedG= (V,,E) Consists of finíte set of vertices (or nodes) V and set
of edges, the edges are nothing but pair of vertices.
Everv asvmmetric relation must be irrefleive.
For example :
For exampie it A =a. b} then
retlexive relation Rcan be = (a, a), (b, b)
E E
roflenive relation Rcan be = (a, b) }
transitive relation R can be = (a, b), (b, a), (a, a) }
sVmmetric relation R can be ={ (a, b), (b, a)}
is\ metnc relation R can be = (a, b) }

1.2.2 Equivalent Relation Fig. 1.2


A relation is said to be equivalence relation if it is reflexive, symmetric and
transitive, over some set S. Her the E1 is a edge connecting the vertices V1 and V2.
The set V= (V1, V2, V3, V4 ) and E = E1, E2, E3, E4 }
Suppose R is a set of relations and S is the set of elements.
For example S is the set of lines in a plane and R is the relation of lines 1.3.1 Directed Graph
:ntersecting to each other. The directed graph is also a collection of vertices and edges winere the directions
Ex. I Deternune whether R is equivalence relation or not where are mentioned along the edges.
A=0,1, 2 1, R ={ (0, 0), (1, 0), (1, 1), (2, 2), (2, 1) For example :
Sol. : The R is reflexive because (0, 0), (1, 1), (2, 2) e R.
Where as R is not symmetric because (0, 1) is not in R whereas (1, 0) is in R.
Hence the R is not a equivalence relation.
EA
1.2.3 Closures of Relations
Sometimes when the relation R is given, it may not be reflexive or transitive.
By adding some pairs we make the relation as reflexive or transitive. E
For example : Let|(a, b), (b, c), (a, a), (b, b) }
Now this relation is not transitive because (a, b), (b, c) is there in relation R but
(a, c) is not there in R so we can make the transitive closure as Fig. 1.3

I(a, a), (, b), (a, b), (b, c). (a, c) | The edge El shows the direction to V2 vertex from vertex V1.

We can even define reflexive closure and symmetric closure in t!. same way. We will see the directed graphs in further chapters and we will cal! those grapns
as transition graphs.
6 Theory of Computation Preliminaries

1.3.2 Trees Here 0 and 1are alphabets.


Trees is a collection of vertices and edges with following properties. Strings is a collection of alphabets.
the "0011" is string
1. There is one verte, called root which has no predecessors. For example: If E =(0, 1 is a set of alphabets
string and.
rom this root node all the suoessors are ordered. The prefix of string is any number of leading symbols of that
For example in any number of trailing symbols.
For example In the string "0011" the prefixes can be , 00, 001 Simiarly
Computer valid suffixes can be 1, 11, 011.

The languages is nothing but collection of appropriate strings.


CPU Monitor Keyboard Mouse
For example L = (0, 00, 000, 0000, ..
keys left right The set L indicates any number of 0's.
mother menu cathod ray button button
button tube
boara
Notice that langugage is fornmed by appropriate strings and strings are
Memony Hard disk by alphabets.
Some basic operations can be possible on strings as - Palindrome, concaten
Fig. 1.4
comparision of two strings and many more.
in the above example, at the leaf node all the components of computer are
P'alindrome of strings is a property in which strings can be read same tt
given, when motherboard, memory and harddisk these components are cornfigured to right as well as from right to left.
they are mearnt for CPU always. Keys are the prominent component for keyboard.
When CPU, monitor, keyboard and mouse are taken as one set, then it constitutes a For example : MADAM is a palindrome string because it is
from left to right or from right to left. same it x
device called computer.
Computer is a root node in the above figure. CPU, Monitor, keyboard, mouse Concatenation of strings means combining of two strings.
are the interior nodes. Motherboard, memory harddisk are the leaf nodes for the For example: If S1 = "Hello"
CPU. Mornitor is a parent node or father of Menu button and cathode ray tube
whereas manu button is a left child of mnonitor and cathode ray tube is a right child S2 ="World"
of monitor.
Then the concatenated string = Hello world.
The ondy difference between graphs and trees is that graphs do not have special We will see how the
node called root node.
chapters. languages can be represented as smaller unit in
1.4 Alphabets, String and Languages
Alphabet is a finite set of symbols we can not define symbol formally.
We can give supporting example for definition of alphabets.
For 4lnple : S = |a, b, c, ..z}
The elemnents 4, b, C, .. Zare the alphabets or S = 0, 1
Finite State System

The finite state systen is a very good design tool for the programs sucBi as text
editors and lexical analysers (which is used in compilers). The lexical analyzer is a
program which scans your program character by character and recognizes those
words4s tokens.
2 L.2.1 Basic Definition of Finite Automata (FA)
A finite automata is a collection of 5 - tuples (Q, Z.ö, qo, F) where
Finite State System O is a finite set of states, which is non ermpty.
2 is input alphabet, indicates input set
q0 in Q is the initial state
9 bo
is a set of final states

i he ot computer science is based on a basic unit recognized as language. is a transition function or a function defined for going to rext state.
in Jiv language is a collection of alphabets - the alphabets which form the strings The finite automation can be represented as
: ors rervbodv of us, develop our own ideas when we think in our language.
Ihs language becomes a fundamental aspet for building any idea or a thought.
ba c 1nput lape
In theory of computations there are two types of languages regular language and 4Tape reader
Tular inguage Let us define the language.
Finite
2.? Definition of Language control

The language can be defined as a collection of strings or words over the certain Head

Fig. 2.1 Finite Automation


For example Rama is a boy.
Usually the finite automation is a nathematica! representation of finite state
Ihis above statemernt indicates certain language and the input set is L = (A, B, machine.
Z a. b. c .. z) The words Rama, boy, is a are collected together in certain
nannet to torm a sentence of the language. Many such statements help us to speak From Fig. 2.1 we can see that, the machine has a input tape on wiich the out
i aigudge In this chapter we will discuss a mathematical model called finite state -can be placed. The tape reader is for pointing the character which is to be read tron
input

2.2 Finite State The finite control is always one of internal states which decides what wui be the
System next state after reading the input tape by a reader. For example suppose curen:
Ihe tinite state system represents a mathematical model of system with certain state is qi and suppose now the reader is reading c, it is a finite control which
nyt Ih model finally gives certain output. The input when is given to the decides what will be the next state at input c.
1dehine it i5 prOcessed by various states, these states are called as intermediate
Transition graph:The FA is associated with a directed graph called a transiio
he very good example of finite state system is a control mechanism of elevator. diagram or transition graph. The vertices of this graph corresponds to the states
Ths mechanism only remembers the current floor number pressed, it does not For example - We draw transition diagram for the input a a bb as tvs
remember all the previously pressed numbers.
(8)
Theory of Computation Finlte State System
SxI = S
So S. state.
where S indicates the next
either on arrival of an :
Fig. 2.2 Thus at any stage the machine will act as
will change the state using state function or it will give output using :
Note that the start state is So and
S,S and S; all are the final state is S4. The input set here is function.
E= (a, b)
intermediate states.
For a ttansition diagram following notations FSM
are used
State Q0 is a name of the
state (you can give any name to a
State)

transition from one state to another FSM With FSM without


output output

Flg. 2.3 Finite state machine


denotes the start or initial state
Now, we will solve some examples based on FSM without output.
Ex. 1: Design a FA which accepts the only input 101 over the input set Z=(0.
Sol. :

{or 0
denotes the final state

Fig. 2.4 For Ex. 1


Mapping functions : The FSM has two forms of Note that in the problem statement it is mentioned as only input
mapping functions.
gchine function and state function. accepted. Hence in the solution we have simply shown the transitions, lor d,
There is no other path shown for other input.
The machine function is a
function which dernotes, with
be the output from current state. It can certain input what will Ex. 2 : Design a FA which checks whether the given binary nunber is e
be written as Sol. : The binary number is made up of 0's and 1's when any binary u
with 0it is always even and when a binary it is alwavs
example
number ends with
where S is a current state
is the input 0100 is a even number, it is equal to 4.
O is the D011 is a odd number, it is
output equal to 3.
The state finction is a and so while designing FA we will assume One start state, One state en
function which
ctate what will be the next state. It can be denotesas with certain input from current and other state for
ending with 1. we want to check whether
written number is even or not, we will make Since
the state for 0, the final state.
13
12 Theory of Computation Finite State System
the current sta tes. And under the
Thus the table indicates in the first column al
column 0 and 1 the next states are shown.
read as When current state is So, on
The first row of transition table can be
next state will be S) and on input 1 the next state will be S. The arrow
input 0 the
and circle markedto S, indiça tes that it
marked to So indicates that it is astart state
is a final state.
which start with I and ends twith (.
Ex. 3 : Design FA which accepts only those strings
frorn which only the edge with input I wil
Sol. : The FA will have a start state A
go to next state.

Fig. 2.5 For Ex. 2

e FA indicates clearlv S is a state which handles all the 1's and 5) is a state
1
h h handles all the 0's. Let us take some input
Fig. 2.6 For Ex. 3
01000 = 0S; 1000
read 0 at state B we wi!
will be in B state but if we
01s 000 In state B if we read 1 we if we read either 0 or we will go
sta te. In state C
reach to state Cwhich is a final if the input
010S, 00 Note thatthe special care is taken for 0,
to state C or B respectively.
with 0it will be in final state.
0100 S, 0 ends 0's.
number of 1's and any number of
Ex. 4 : Design FA which accepts odd there will be a state which is
01000 S2 statement, it is indicated that on
Sol. : In the problemn state. There is no condition
and that will be the finai
vON at the end of input we are in final or in accept state
so it is a even meant for odd number of 1's
number. SimHariy let us take another input. number of 0's.

1011 = 1S 011
10S, 11

1015,1
10115;
final state.
-ow we are at the state S which is a non
transition table.
'another idea to represent FA with the help of
state input Fig. 2.7 For Ex. 4
state
S state S which is a qnal
So S input 1 then we will go to
At the start if we read nmber of zeros at any state
1's. There can be any
S2 S
we have read odd number of well as to state S. For
example
to state S as
S S therefore the self loop is applied
Transition table
16

1
14 | Finite State System divisible by 3. So at: nun
Theory of Computatlon equal to 6 i.e.
number 111111 which is q3.
if the input is 10101101, in this string there are any Consider a final state
number of zeros but odd number we reach to
number of ones. The machine will derive this input as follows complete scan of this
Start 111111
1 input ends here
which is a final state
S. State qo
in lq1 11111
S
in
11 q2 1111
S 111q3 111
S.
1111 q1 11 mahi in
11111 42 1
11111lq3 ’ Now we are in final state.
number is divisible by three.
Design FA to check whether given decimal
determine whether the given decimal number is divisible by three, we nes
Sol. : To considering its divisibiliy
Star
to take the input number digit by digit. Also, while
state
consider that the possible remainders could be 1, 2 or 0 T
S. three, we have to
remainder 0 means, it is divisible by 3. Hence from input set {0, 1, 2,
testing
Fig. 2.8 Ladder diagram of'processing the input decimal number is a input), we will get either remainder 0 or 1 or 2 while
E 3: Design FA which checks whether the given unary number is divisible by 3. divisibility by 3. So we need to group these digits according to their remainders T
groups are as given below
5oi. :
1
remainder 0 group : So : (0, 3, 6 9)
91 93
remainder 1group :S :(4, )L
remainder 2 group : S2 : (2)
Fig. 2.9 For Ex. 5 We have named out these states as So,S and S. The state So will be the i
state as it is remainder 0 state.
The unary nunmber is made up of ones. The number 3 can be written in unary
íorm as 111, number 5 can be written as 11111 and so on. The urnary
number which (2:3,8)
is divisible bv 3 can be 111 or 111111 or 111111111 and so on. The
transition table is (2,5,8) (0,3,6,9)
as foliows

Input (1.4,7)
(0,3,6,9)
25,8)
(1,4,7)

92 93
q3
(2.5,3 (0,3,6,9)
(0,3,6,9)
(1,4,7)

Fig. 2.10 For Ex. 6


17
Theory of Computation Flnite State System
Let us test the above FA, it the number is 36 hen it
will proceed by reading he to 2. We will scan this number
number digt bv digit. Let us take some number 010 which is equivalernt
from MSB to LSB.

From
start3js
So S S2
go to state So remainder 2 state. Similarly for input
Then we will reach to state S which is
1001 which is equivalent to 9 wewill be in final state after scanning the complete
input.
1 1
from S, to So
36

S, S S So
input ends and we are in
final state Sp Thus the number is really divisible by 3.
number of 1's.
Ex. 8 : Design FA which accepts even number of 0's and even
ifferent stages for input 0 and 1. The stages could be
Fig. 2.11 Sol. : This FA will consider fouY
of 1,
even number of 0 and even number
}ience the number is divisible 3, isn't it ? Similarly if number is 121 which is not -

of 1,
sihle h 3. it will be processed as even number of 0 and odd number
odd number of 0 and even number of 1,
of 1
odd number of 0 and odd number
2s Let us try to design the machine
121S which is remainder 1 state.
Design FA which checks whether a given binary number is divisible by three.
Sol. : As we have discussed in previous example, the same logic is applicable to this
oblem even ! The input number is a binary number. Hence the input set will be
;0, 1 The start state is denoted by S, the remainder 0 is by So, remainder 1 by S
and remainder 2 by S2.

0
S
Flg. 2.13 For Ex. 8

Note carefuliy that a symmetry of


1 Here qo is a start state as well as final state.
Sand 1's is maintained. We can associate meanings to each state as:
1l's.
go: State of even number of 0's and even numnber of

gi : State of odd number of O's and even number of 1's.


Fig. 2.12 For Ex. 7
Finlte State System
Theory of Computatlon In the above transition graph we have handled the case as if the input
State of odd number of O's and dd number ot l's. with 1 then it will be in q3 state which is a dead state and never lead to fina,
State of even number of 0's and odd number of l's, starting with 0 and
Thus this machine strictly handies the strings ending
withtrn
Design FA to accept L where L =(Strtngs in which a always appears
The transition tatble can be as folloWs
la, b.
0ver the set E
larnguage the valid strings are aagb, baaaaaa, bbaaab a
0
Sol. : For this particular in a clump of 3. The TG (transitibn
Q3 on. The a always appears graph) will n
q0 92
this
93

Ex. : Dsign FA to acept the string that albays ends with 00.
Sol. :

Start Fig. 2.17 For Ex. 11 the

Note that the sequence of tripple a is maintained to reach to the final state FSN
Ex.12 : Design FA to accept L where all the strings in L are such that total r:
a's in them are divisible by 3.
Fig. 2.14 For Ex. 9
2.4
Sol. : As we have seen earlier, while testing divisibility by 3, we group the
íthe input is 01001100 it wil be processed as remainder 0, remainder 1 and remainder 2.
Det
Start Hence mar

So : State of remnainder 0 sho

S :State of remainder 1
Fig. 2.45
S : State of remainder 2
The q2 is a final state, hence the input is
Ex. 10 : accepted.
Construct the transition graph for a FA which accepts a
in wnich every string start with 0and ends with 1. language L over E {0, 1}
Sol.
S

Fig. 2.18
0.1 Dead
state
Note the difference between previous example and this, here there
condition as a shoBd be in clump but total number of a's in a
Fig. 2.16 For Ex. 10 3..Hence b's are alowed in between. string are a
20 FiniteState System Hahul 21
Theory of Computation
input a there are two next states q1 and
2:3 Deterministic Finite Automata (DFA) Note that the NFA shows fromn qo for
next states are qu dnd qi
The finite Automata is called Deterministic inite Automata if a). Similarly, from qo for inputb the
there is only one particular input where to go nexi
path for a speiftic input from current state to neNt state. For exanmple, the DFA can Thus it Is not fixed or determined that with a
finite Automata
be shown as below Hence this FA is called Nondeterministic
be derived as
Consider the input string bba, This string can
Input b b
I'ath qo qo

Input b b
or
Path qo q0 92
Input b b
or
Path q0-.q1
q1.
has to be followed for
Thus you can not take the decision of which path
Fig. 2.19 Deterministic finite automata
derjvng the given string.
tupies,
From state So for input 'a there is only one path, going to S,. Similarly from So The NFA can be formally defined as a collection of 5
there is only one path for input b going to S,.
Q is a finite set of states
The DFA can be represented by the same 5-tuples described in the definition of
PSM._All the above examples are actually the DFAs. E is a finite set of inputs
transition function
¿ is called next state or
2.4 YNon-Deterministic Finite Automata (NFA or NDFA)
go is initial state
The concept of Nondeterministc Finite Automata is exactly reverse of FcQ.
Dererministic Finite Automata. The Finite Automata is called NFA when there exists F is a final state where
be what is the
Thus the next question mightbecause
Imany paths for a specific input from current state to next state. The NFA can be (There can be multiple final states.
used in theorv of computations thev are
shown as below. basically
use of NFA. The NFA is
than the DFAs. )
more flexible and easier to use
and the NFA
The Equivalence of DFA
41
5 we have discussed, the
finite automata can either be
DFA or NFA. You might
a
has more power ? Here is
better NFA or DFA. Which
be thinking now, who is converted to its equivalent DFA.
That
NFA can be
theorem which tell you that any its equivalent DFA. The
by NFA can be acceptable by
is any language L acceptable that, DFA keeps track of all the states, that NFA Could
basic idea in this theorem is
as the DFA has read.
be in reading the same input

Fig. 2.20 Non-deterministic flnite automata


r

22
Theory of Computatlon Finite State System
hypothesis,
Let us see the theorem. By the induction
Theorem : Let L be a set aepted by non deterministic finite automation. Then 8(g6, x) [P P2...PJ
there exists a deterministic finite automation that acepts L.
ifand only if,
P3 P
Proof : Let ö(q0,x) = (PP2
M =(Q , S, qo. F) be an NFA for language L. Then define DFA Msuch that By definition of
8'
[n,r2...*]
M= (QES, q. F)
o([P. Pz,. P].4) =
The states of Mare all the subset of M. The Q'=20 if and only if,
F be the set of all the inal states in M. . k
({P Pz, ..P,}a) = n2,
The elenments inQ will be denoted by (q1,92.93 ,...qi]and the
are deioted [Link] q1,92,...qi will be assumed as one
elements in Q Thus
state in Q n,,.. ]
if in the NFA go is a initial state it is denoted in DFA as qó =fgol
We define, 8' (q6, xa)
8 (i9:.92.q3.q]. a) =[P. P2 Ps, P; if and only if
if and only if, S(qu, Xa) = n, 2,... K}
ö( iq:.q: [Link]} a) ={P: P2 P3. Pi} is shown by inductive hvpothesis.
This means that whenever in NFA, at the current states Thus L (M) = L (M)
get input aand it goes to the next states P1 P2. ... P then while
q1,q2,93....qi if we
for it the current state is assumned to be
constructing DFA With the help of this theorem, let us try to solve few examples.
lq1,q2,93,....qi At this state, the input is a
and the next is assumed to be p1, P2,... P,.On applying 8 function on
each of the
Ex. 13: Let M = (qu.g1} {0, 1},O, qu (91)
states qi.92.q3... q, the new states may be any of the states from P1 be NFA where 8(qu.0) = 90.q1}, (q0.1)= (q1}
The theorem can be proved with the induction P2, ... P; (q1.0) =, (91.1) =(q091}
method by assuming length of input Construct its equivalent DFA.
string x
Sol. : Let the DFA M'= (Q,E, 8', qo, F)
6(qu, x) = [9i.92, 9]
if and only if, Now, the 8' function will be computed as follows
S(g x) =i9:92.93.9:}
As
`(q0.0) ={g0.q1} ö(l90). 0) -[qo.q:}
As in NFA the initial state is ao. the DEA will also contain the initial state
Basis : If length of input string is 0
Let us draw the transition table for § function for a given NtA.
i.e. lx! = 0, that means x is [ then qo =q0

Induction : If we assume th¡t the hypothesis is true for the input string of 0 1
length or less than mn. Then if x a is a string of
length m+1. Then the function 8' 6(q0. 0) ’ Q0 S(q0. )
CouldBe written as
o(4:. 0)
8(g. xa) =8(6(go. x), a)
f unction for NFA)
4 Finite State System
Theory of Computation
of the states of DFA
From the transition table we can compute that there are qo 9ilq0,q1]states Even, we can change the names
or its equivalent DFA. We need t0 compute the transition from state lao. al
S(iqo. qi}0) = s(4o.0)wS(qi. 0) B - (qi]
C= [go.q:}
= iq0.q:} With these new names the DFA wil] be as follows
So, 8(90.9:}o) = [90.9:)
Smilarl
A )o.1
S(40.q:1) = S(40.1)S(q:.1)
1
= q:}fq0.q1}
B

s, la0.q:}1) = [qo.q:]
As in the given NFA q: is afinal state, then in DFA where ever q1 exists that for Ex. 13
Fig. 2.22 An equivalent DFA
at becomes a final state. Hence in the DFA final states are •qil and [go,q1l
neretore set of final states F =q1} qo.q1
Ex. 14: Convert the given NFA to DFA
The equivalent DFA is 0
1
’ q0 {q0.91}
’ 90 90. 91 q2
q1
q3 q3
q2
90. q1 q2
Transition table for equivalent DFA for Ex. 13
compute S' funcion
Sol. : As we know, first we will

8({90). 0) = {90791}
((9%-41)
Hence 8'(go10) = [90. q1}
1
Similarly.
s((90},1) = (90}
Hence 8(go),1) = [90)
Thus we have got a new state qo,q:}.
input 0 and 1.
Let us check how it behaves on
Fig. 2.21 For Ex. 13
26 Theory of Computatlon Finlte State System

So, [92}
S(30.q:).0) = (9),0) vs([91),0) [92]
={q0-q:}iqz) [g0.9:J [90.q1-92] (90.9:]
(90.91 92) (90.q1q2.q3) [90.q193]
Hence a new state is generated i.e. 90.q1,q2
(qo.q193 [g0.9)-92) [go.q1.92)
Similariv
[90.9192 93D (90.91.92.q3) [90.q1.92.93)
S(lq0.9:)1) = s((90].1) vs(91].1) DFA for example 14
= {qo}u {q:}
Ex. 15 :. + Construct DFA
equivalent to the oiven NFA
= {qo.91} 1
No new state is generated here.
’ p Ip, q!
Again 8' funcion will be computed for qo. q1,92 the new state being
generated.
r
State 0 1
S
(90.91) [90]
[g0.93] (qo.q1.92 The NFA M = ((p, q, I, s), (0, 1), 5 (pl, (sl)
(90.q1.92] qo.q192.93 (90.9193) The equivalent DFA will be constructed.
As, vou have observed in the above table for a new
state qo, 1
will give a new state qo,q1,42,93 and input 1will give a new q1,92 the input 0
because state qo,q1.q3)
’ lpl P, q! (pl

ö(q0.919a10) = 8(10].0) v8 (191].0) v(4:].0) tql


= {g0.91}u {92}{9} [s)
=(q0.91q2.93}
(G) [sl Is]
= [go.q1q2.q3)
Same procedure for input 1. Thus the final DFA is as given below.
(p.
State
Continuing with the generated new states.
1
1
(90.9) [90]
’ tpl lpl
[r]
28
Theory of Computation 29
Finite State System
[s]
6 NFA with.c. Moyes
[s] s
The t is acharacter used to indicate null string i.e the string which is used
lp. q! Ip, r) sinpty tor transition from one tate to other without any input. The NFA with
moves can be shown as below.
lp, 4, rl lp, 4, r, s] Ip, r]
For example
Ip. rl) lp, 4 s] Ipl
lp, q, r, s] Ip, r, sl
p. q, s] Start
[p, q, r, s] p, t, s]
Ip, r, s] lp. q, s] P)
(p, s] Ip, q, s] p. s]
The final state F = |[s], Ip, q, I, s], (p, q, s], lp, r,
s], p, s] )
Fig. 2.24 NFA with moves
0
[Link] (p.q.r) We can convert the given NFA with [ moves to the NFA without [ moves. Let
us take one example to see how this happens.
Part 1

1
Start
p.r.
(P.q.s) (p.r
0

((P.q.r.s]) Fig. 2.25 Finite Automata with [ moves


q
( The important aspect for building the transition table is to, compute the
(P.s) function. The othe transition function, mapsQx (E {}) to 22.)The intention is
0.1
that &(q, a) will consist of all states p such that there is a transition labelled a from
q to p where a is either [ or the symbol in [Link] us design the transition table with!
8 function for Fig. 2.25.

Part J|
-OD y

(90}
Fig. 2.23 For Ex, 15
The transition graph shows two
disconnected parts. But part Iwil
as final DFA because it consists of start state and final states, in part II be accepted 42 (92}
start state. there is no
Let 8" (g, w) will be all states p such that one can go from q top along a
path
Abelled w, sometime including edges labelled e. While constructing §" we have to
30
Theory of Computation Fnite State System
compute the set of states reachable trom a
us use [
CLOSURE (g) to denote the set ofgiven state q using E transitions only.
all vertices Let
from q to p labelled . such that there is a path
X,y y,7
The §" can be Start
interpreted as follows
S (q, s) = CLOSURE ()
For X.y,z
in ànd a in ,8* (a, wa) =
CLOSURE (p)
where p=p ! for some r in §* (q, w) p is in d(r, Fig. 2.26 NFA without transitions
a)}
For the transition tabie Conversion of NFA with [ transitions to NFA without [ transitions
S"(qo, [) = s CLOSURE go} As we have seen one example of conversion of NFA with [ transitions I
= (q0.q1,92} try to prove this with the induction method.
Thus Theorem : If L is accepted by NFA with [ transitions, then there exist L whu
accepted by NFA without [ transitions.
8(go, x) =CLOSURE (8 (8 (90.E). x)) Proof :
= s-CLOSURE
(6({qo.q:.92). x)) Let, M= (Q8, qu. F) be an NFA with e transitions.
E-CLOSURE (8(q0, x)uo(91.)U(92.x)) Construct M= (Q,8, qu, F) where
E-CLOSURE ({qo}uÙU¢) F= FUgo if [ - CLOSURE contains state off
=

E-CLOSURE |{q0} otherwise


8"(go.x)= {q0.q192} M is a NFA without [ moves. The 8' function can be denoted by j" with
8" (qo, xy) = -CLOSURE (6 (8" (90. x), y)) input. For example, 8' (q, a) = 8 (q, a) for some q in Q and a from We wil! a
the method of induction with input x. The x will not be [ because
= E-CLOSURE
(5({qo.q1.92} y)) 8'(qu, e) = (q0}
= &-CLOSURE ({q1})
= {q1.92} $"(q0.t) = -CLOSURE (qu). Therefore we will assume length of string to be
Thus the transition table can be drawn as - Basis : lxl = 1. Then x is a symbol a

X
8' (qu.a) =8"(q0.a)
Induction: lx> 1 Let x = wa
{qoq192 {992} 92)
{9.92} {92}
8'(q0,w a) = 8(8 (qo w),a)
By inductive hypothesis,
{92})
S(qu. w) S"(q0, w) =P
Now we will show that 8' (p, a) = ö(qo, w a)
33
32 Finite State System
Theory of Computatlo
But
h
9 in p

p = S(q0. w) we have.
e

$ (4, a) = S(q0. wa)


C
8
final state. NOW,
state a is a initial state, state c is a
Sol. : From the given table, the the same output states on receiving
he
states b and h gives
We can notice that non final states. So we can
reduce those
are
S (9:. wa) = 8(q0, wa) respective input. And both the states ocCurs we will replace it by b, similarly d =f
h
states we can say, b = h ie. wherever
and hence we will replace every f by d.
2.7 NFAwith [ Moves to DFA
As we have seen that NFA with s moves can be converted to NFA without
mores. To gain the DFA, we can follow the following diagram.
b

NFA with NFA without


¬ move
DFA
E moves
e

e
8
Fig. 2.27 Moves to DFA a.
states. So we will replace every e by
2.8 Minimization of DFA
Now a =e and both are non final
1

The minimization of DFA means reducing the number of states from given FA.
b
Thus we get the DFA with redundant states after minimizing the DFA.
While minimizing DFA we first find out which two states are equivalent we
then replace those two states by one representative state. Now for finding the
equivalent states we will apply the following rule. d
a
"The two states S and S are equivalent if and only if both the states are final
states or both are non-final states. a minimized DFA.
possible further. Hence this is
Now there is no reduction
Let us understand the rule with the help of some examples. given transition table.
Ex. 17 : Construct a minimized DFA for the
Ex. 16 : Construct a minimized DFA for 1

1
-’ q0
-’a f q2

C
q0 42
34
Theory of Computationeinite State System
qo
(92)
92 q6
93
6 q3
6
94
Again, qo =46
Sol. :
From given ransition table
we can get two non
equivalent and those are final states which are
input 0 and 1. So we will q3 =95. These two states have similar output on receiving
q3
replace by g3 every where.
qs -’ o

0
92) 42
q3
96

output states tor input Ua


92 Now again we can see q1 and q2 gives the same
final state and
92 But these can not be the equivalent states because q2 is
nonfinal state. Hence this is a final minimized DFA.
44
93
Solved Examples
96 q4
Ex. 1 Design FSM that accepts the string with exactly tvo zeros.
97 96 92 Sol. :
Now again we find q; =q7. These two non
final states are equivalent.
1
0,1

-’ q0
93
q6 92

92
93 92 Fig. 2.28
96
94 means there can be possibili:
43
At every state there is a loop of 1 that maintained as exact!y
to be
q6 q6 OCCurance of 1's in between 0's. The count of 0 has
There is no restriction on number of 1's. You can try out a valid string 10100
Again, qo =94 lead to the final state g2. The state a is a dead
state. For the invalid npUts
will fell in deadstate like q3
at ie
-’ qo
Ex. 2 Design FSM for the language that accepts the string contawmg
93 zero's over the ={0, 1}
there is no restriction on ni
q6 92 Sol. : The condition is for at least two zeros and
T's. At least tWo zeros means minimum two zeroS are necessary.
36
Theory of Computation 37
inite Stato System
The edge from C to qo is followed for input either 1 or &. The t indicates n0
Start string
0,1 For example if 'wish to derive the above FSM for 0000, then E edge will be -
important. The FSM will derive it as 00: 00, but which is equal to 0000. Thus
because of r the cursion is pOssible.
Fig. 2.29 Ex, 5 Desjg, FSM whuch accepts the language in which every string starts with 0
} r suggested to note the ditterene betveeen the FSM folloux dbu any number of 1's but having no troo consccutrve zeros.
Jrawn in example 18 Sol. : Here the strirgs are starting with 0 and to
to have the Situation as not to have

Ex. 3 Design FSM for the language L the consecutive zeros. we have shown the transition between gË and q as alterna te 0
consisting of the strings containing at the most and 1. The machine can accept the input as 0111 i.e. starting with 0 and followed by
Sol. : in this problem, at the no (absolutely nol cansecutive zeros.
most two 0's are allowed. This
ero or two zeros will do ! But not means no
more than two zeros are allowed. The zero, single
wl! iook like this TG for this

Start Fig. 2.32

Ex. 6 Describe in English the string accepted by following FA.


Fig. 2.30
Here q is a start and final state. This \0,1
state indicates that there is no zero in
[Link] string. As there is no the 1

number of times at any placerestriction


on number 1's, the input 1 Start 93
in the
mnay occur at any
Ex. 4 string.
Design FSM which contains, the string
I={0, 1 containing double zeros always over
Sol. : Here, the condition is that
whenever zero will appear it should be double. No
single zero is allowed in the string. Fig. 2.33
number of times The 1 will be flexible i.e. it may appear any the state q:
Sol. : If we follow the above FA, in straight, we will get a string 101. If
l we will reach to
is a'current state [Link] even after many iterations to q1 with input
of 0's and
final state g3 after following 01, latter on it may come across any number
l's. Thus we can predict that the above machine is for the language containing 101 as
Start 0 a substring, over the input set E =0,1}

1,8

Fig. 2.31
Ths will g
Regular Expression dnd b)
indicates occurance t
closure or closure which 1he (e + b)
The r* is known as kleen tx. 4 Co
number of times.
R = a*, then R i
we have regular expression
For example if E = la) and Sol. :
T.e =(
3 denoted by R = lE, a, aa, aaa,
aaaa, ...

That is Rincludes any number


of a's as
appearing, denoted by [
well as
character.
empty string which ind..
This regula
bs except a n
zero number of a's
Regular Expression Similarly there is a positive
closure of L which can be
string.
shown as L: T Ex. 5
The null string ca
Co
00
except the [ or null
denotes set of all the strings So1. ihe re

denoted by [ r
a set dernoted b
regular expression R= a then R is
If E = (a) and if we have
R= a, aa, aaa, aaaa, ....
n the
previous chapter, we have learnt how to Thus the
machine or model. In this chapter we will represent any language by a We can construct L* as
converted into expressions. The discuss how these models can be
described by simple expressions, languages accepted by finite automata are easily L* = [-L+
ol: he fi
called regular expressions. The regular expressions with the help of some examples
is the very effective way to
represent any language.) expression Let us try to use regular
accepting all coriibinutio:s
In this chapter, we will also see how Ex. 1 Write the regular expression for the language
to design the finite Automata over the set E = a} Note tha
regular expression and vice [Link]. regular from given
expressions are mainly to All combinations of a's means a may be single, doubie, tripple and mbols cori
see the language which is not represent
the set of regular language. We will also Sol. :
appearing for zero times, which means d nui! cluding a r
us start with the regular regular. Let There may be the case that a is regular expression te
expression. we can give
That is we expect the set of ([, a, aa, aaa, .... So 7
as
3.1 Definition of Regular Expression R = a t. : The r
Let E be an alphabet which is
used to denote the input set. The
expression over can be defined as follows. regular That is kleen closure of a.
accepting all comb1nat.. 8
1. ¢ is a regular expression which Ex. 2 Design the regular expression (r.e.) for the language
denotes the empty set. a's except the null string over 2 =a}
2. ¬ is a regular expression and
denotes the set [}. Sol. : The-regular expression has to be built for the language
3. For each 'a' in E 'a is a We
regular expression and denotes the set la). L = a, aa, aaa, ..
4 If r and s are This set indicates that there is no null string. So we can denote re as
regular expressions denoting the
respectively, then languages LË and L
R= a* The lang
rts is equivalent to LË L2 i.e. union. nul strin:
As we know, positive closure indicates the set of strings without a
rs is equivalent to LË L i.e.
concatenation EX. 3 Design regular expression for the language contaiMIng all the strUg unh
r is equivalent to L ie. closure. any number of a's and b's.
ol. : The regular expression will be
(38) r.e. = (a + b)*
40 41
Theory of Computati Regular Expression
This will give the set as L = e, a, aa, ab, b, ba. bab, abah L over E', where E -a,b, c} n
: and b) any combination Write regular expression to denote the language
nunber of u's Is followed by any
which every string will be such that any
c's
he (i b) means any combination with a and b even a null nunmber of b's is followed bå any number of
strine means b" any
a's means a any number of bs
Ex. 4 As we have seen any nunber of and
Construct the rcqular erpressoN for the language "Sol. :
problem statement, b's appear after s
aNy Mumber of a's and hs, except the null strmg. containing all strings havin anumber of c's means c*. Since as given in
be
Sol. : Tc's appear after b's. So the regular expression could
r.e =(a - b) r = ab*4
I-a,b, c}
a language L over E', whereone b followed
This regular expression will give the set of strings of any
combination of a's and Ex. 10 Write a regular expression to denote
at least one afolloved by
atleast
have
b'secept a null string. such that every string will
by at least one C. That
Ex. 5 Construct the re. tor the larguage accepting all the strings which are changed to "at least
ending with the condition is slightly
00 over the se: S= (0, 1}. Sol. : Now, in this problem at all. So, we can write
null string is not allowed
So!. : The re. has to be tormed in which at the end, there should be 00. r eans the
That means R= abc*
which begir Gr erut
I.e. (any combination of 0's and 1's) 00 to denote a language L
which accepts all the strings
Ex. 11 Write r.e.
r.e. = (0+1)* 00 with either 00 or 11.
categorized into twO subparts.
Thus the valid string are 100, 0100, 1000, 10100 .... strings ending with 00. Sol. : The r.e. can be
x. 6 R = LË +L2
Nrite r.e. for the language [Link] strings which are starting with 1 and)
11.
ending with 0, over the set E = {0,1} which begin with-00 or
Sol :
LË = The strings
The tirst svmbo! in re. should be 1and the last symbol should be 0. which end with 00 or 11.
L = The strings
R = 1(0 +1) *0 and L2.
Let us find out LË and l's)
Note that the condition is strictly followed by keeping starting and ending (any number of 0's
LË = (00 + 11)
symbols correctly. In between them there can be any combination of 0 and 1 LË = (00 + 11)
(0+1)*
including a null string.
Ex. 7 If L = (The language starting and ending with a and having any combination of
b's in between, then what is r?
Similarly, and 1's) (00 + 11)
number of O's
L = any
bol. : The regular expression
+ 11)
= (0+ 1)* (00
r = a b* a
X. 8 Describe in simple English the language represented by the following regular
+1)]+[(0+1) *(00 +11)]
Hence
expresson R = (00+11)(0
language L over S',
where = a, b} such
thut the
a
r = (a + ab)* Write a r.e. to denote a.
Ex. 12
character from right end of the string is always as a
1.: We will first try to find out the set of strings, which can be possible by this r, from right end
symbol
contains the third Either a or b
Sol. : The r.e. Either a or b
L () = la, aba, abab, aab, aaa, .. character
any number of
The language is begining with a but not having consecutive (in a clump) b's. r=
of a's and b's
2nd
1st
3rd
Expression
Theory of Computatid Regular b's)
(a+b)* a (a+b) (a+b) (any combination of
combination of b's) (aaa)*
Thus the valid strings r.e. =(any
are babb or baan or a's appear tripnlod.
Ex. 13 baba or aaab and of the stringS in which
Construct re. for the L= (The language consists
b's over the language L hich accepts all the so on. vith number of b's
S=a,b strings at leust tud restriction on
Cunstruct regular expressiOn for the language L over the set
Ex, 19 are drvisible by 3.
At least two bs tlhe total nuniber of a's So the valid strings can be
are
maintained a's are divisible by 3.
LwInbination of a's and b's. but the two b's are Sol. : The total number of
Ex, 14
Construct re. r the
surrounded by any
L = (baan, bababa, ababab,
....
languaçe which consists of exuctly two b's over the be
sol. : Now, this set The regular expression can
that there should beproblem is similar to the previous problem. = (b* a b* a b* a
b*)*
exactlv two b's. But only
difference is
r.e.
=}a, b} such that uil the
R= a b a b a Ex. 20 Write the r.e. to denote the language L over Z
not contain the substring "ab
o indicates either
can derive anv string
the string
contains any number of a's or anull Sol. : The L = lE, a, b, bb, aa, ba, baa ...)
Ex. 15 having exactly two b's and any string. Thus we r.e. = (b* a)
nrite re. uhich contains L number of a's.
having strings which should have at
The
atieast one 1 = E we get all combinat1or:
Sol. : The least one 0 and In this regular expression if we substitute a*
required expression will be get all combinations ot a
and similarly if we substitute b = E then we will
have ab as substring by givn
have strictly maintained a condition for not to
R [(0+1) 0 (0+1) regular expression as b* a*.
EL 16
Construct re.
of string
Sol. : Sine L={0}there are
which
"1(0+1)]+[(0+1)*1(0+1)having*o(0+ 1)]length
denotes a language L over the
set E= {0} en 3Z Equivalence of Finite Automata and Regular Expression
So we can give the strings in L of even length i.e. L = (E,00, 0000, re
regular expression as 000000, ... There is a close relationship between a finite Automata and the
R = (00)* expression we can show this relation by following figure.
Thus Kleen
g the even closure indicates the
Can be

length of the string. recursion of two zeros which will Can be


Converted
Regular
expression
Converted

Write r.e. which denotes a ultimately to

language L over the set E= {1}


Sol. : R- strings.
1(11)) having odd length of Deterministic NFA With
finite
The odd length E MOves
strings are L automats
previous exanple and this one. = 1, 111, 11111, . . Note the
Ex 14
Describe the language denoted by difference between Can be Can be
Converted
Converted

re.
following regular expression NFA without
to

Sol. : The (b (aaa) *b ')* :moves

We can split language


the re. as
can be predicted
from the r.e. by finding out Fig. 3.1 Relationship between FA and regular expression
the meaning of it.
The figure 3.1 shows that it is convenient to convert the regular epre
NFA with [ moves. Let us see the theorem based on this conversion.
44
45
Theory of Computaa0ar Expression
3.2.1 Equivalence of NFA and Regular Expression
we wil form
Theorem 1: Let r be a regular Aet do be new initial state and f, be the new final state
tIantins that dcepts L()
eresto, then there exists a NEA withl
M = ((QU0,{[Link]).(2,Ea),5, qofo)
I'root : This thcorem can be pOved by induction method The Sis denoted by,
Ihe bass Ot inducion will be bv i) ö(q0,E) = (qq2}
considering r has ZerO operators.
DasN (zero operators) - Now, since r has
zerO Operators, means r can be either ii) S (q, a) = ö1 (q, a) for q in Q1 -{K} and a in E, ua}.
tor some ain input set
iii) 8 (q, a) = 62 (q, a) for q in Q2 -{}and a in Ez w E}
Ihe inite automata tor the same can be written as
iv) 8(, ,e) = ö (f2 .E) = {fo}
machine M which is as shown below
All the moves are now present in
Sa
M
r=a
(no path to final _tate) Start

Fig. 3.2 Finite automata for given


regular expression
Induction : This theorem can be true for n
number operators. The n is
of Fig. 3.3 The machine M for
union
F tyual to 1. The regular expression contains equal to or more thangreate
one the transition from q0 to ty must begin
The construction of mnachine M is shows
perators
to qi, then it follows the path
in
the path goes
in an type of regular expression there are only three cases by going to q1 or q2 on [. If to fo on [. Similarly, if the path goes
to
possible. machine Mand goes to the state.f and then to state f and thern to on [.
1 Union 2. machine M2 and goes
Concatenation 3. Closure. q2, then it follows the path in
means either the path in machine M, or M2
Let us see each, Thus the L (M) = L (M) UL(M;) That
will be followed.
case 1: Union case:
Case 2 :Concatenation case
Let r=r, +F where rj and rn be the regular M, and M,
two regular expressions. The
expressions. Let, r = nI2 where rn and n are L{
(M) = L() and L (Ml;) =
There exists two NFA's M,
-(Qi,Ei,61.{6}) denotes the two machines such that L
machine M will be
and M, = (Q?,Ez .ö2.{f2 }) The construction of
I(11;) L(n) means the language states by regular expression n is M = (QiuQ2, Eiu ,6.{q} {f2})
same which
ropresented by M, Similarly L(M2)= L (). The mapping function will be given as
QI represents the set of all the states in
machine M). -{6} and a in Eue}
i) S(g, a) = S, (q, a) for q in Q1
Q2 represents the set of all the states in machine
M2. ii) 6(6,e) =(92}
We assume that O1 and Q2 are e}
ain
totally different i.e. Q1 and Q2 are disjoint. iii) ö (q, a) =8; (q a) for q in Q2 and
46
Theory of Computetiodke0ular Expression
Thus L. (M) = L (M,)* is proved.
Now ba sed on this proof let us solve Some examples. These examnlas
Star NFA with [ moves
M how to convert given regular expression to
r.e.
Problems based on construction of NFA from
Ex. 1 Construct NFA for the regular expression b + ba .
Fio. 3.4 Machine M for
he tntial state is
concatenatlon Sol. : The regular expression
b some input a the r=b+ bat can be broken into rË and r2 as
the transihon il next state will be f. And on
trom t to q: and the final
state will be f. The receiving r = b
transition I2 = ba*
Thus
Let us draw the NFA for n, which is very simple.
That is N nl(M and b is in L
(M,) Start
tiene ve can prove L (M) =
L(M;) L (M2)
Case 3: Closure case :
Fig. 3.6 For A
Le'= where r be a regular
he mahine M Is expression. Now, we will go for r = ba, this can be broken into g and I4 where r =
such that L(M,)= L(n). I4 = a*. Now the case for concatenation will be applied. The NFA will look i
Tien sonstrut M I3 will be shown as below,
Ihe
=(Q:qo,fo E,×.qo.{fo})
iapping funcion ois given by, Start b
Q4
!öfqut) = 6 (í,E) =q:, }
)E (9 4) = 6, (4, 4) for q in Q Fig. 3.7 For
The rachune M will be
-{6} and a in E; u fe}
and r, will be shown as

Siar Start
M. A6

Fig. 3.8 For r4


Fig. 3.5
The machine M, The will be n =3 I4
similariy, from 9o to shows that from qu to qi
ty on t there is a there is a
nath. Similariy a
transition path. The path existstransitionh on receiving [
from
1s possible Thus one can derive E, a,h to to final
state, on receiving from
[.
to qi, a back Start
the input a. The total recursion
ag, aaa, for 46
expressibns.
euiar
Smail Sol. Ex. 2
VFA \FA 48
\FA i
here Star \on
for 1or The
tor
r; I2 Construct
wil
r, =0, NFA '
Start wil will
will
be be be I wil
draw
= beNFA
1 constructed with NEA
Start Start
e tor
moves
Fig. r
3,Fig.12 [Link].11 step for =
Fig.
340 3.9 r

1 0 bythe For
0 le
stepregular Ex,
1 b+
93
4 by
expression
breaking v

96
regular (0
+1) Computatlo
Theory of
".
expression

into
Ex. 3
And Begular
Sol.
The where finally
The :
NFA The Expression
NFA
regular Construct
for for
r n
is is I2
10
= =expression

01 r NFA
= for
(01
can +10)" the
regular Fig.
be 4
Fig. Fig. split 3.13
3.14
0 expression
3.15 into For
For For Ex.
two
n A 1 given 2
small
93
below.
regular

expression.

49
Theory of Computation Regular Expression
1+10 is drawn as
NFA for F -(00)

Start

47 Start

Fig. 3.16 For rs Fig. 3.19


Now let us draw NFA with s
moves for (01 +10). The final NFA is,

Start

Star

Fig. 3.20 For Ex. 4

Ex. 5 Construct NEA for the r.e. (01 +2*) 1.


Sol.: Let us design NFA for the regular expression by dividing the expressi
Fig. 3.17 For Ex, 3 Smaller units.
Note the differernce as of if r = (01+10) , we r = (n t F)3
¿)q with E ransition.
would have added the edge fromn
Ex, 4 where n = 01, F = 2* and g = 1
Construct the NFAfor the language having odd number
of zero's over the set The NFA for n will be
Got. : in this problern
language L is given, we have to first Start
expression. The re. for this L is written as r.e. = 0(00). convert it to regular
The r is now written as Fig. 3.21
I = 2
The NFA for r will be
Ih tor I = O is

Start Start

Fig. 3.18

Fig. 3.22
52
Theory of Computation Kogular Expression 53
The NFA for will be
The s will be
Start 1
Q

a (Q13
Fig. 3.23 G10

The inal NFA will be

Fig. 3.26

Star
The r will be
qo 10)

Fia. 3.24 For Ex, 5


Ex. 6 Construct the NFA with[ moves for the language given by Start

r e.=(a(aa) *b - ab a) ". 413


Soi. Lot the re r= (n)* 410

And I} = I) + I3

Where Naa)" b
I3 = ab'a Fig. 3.27
Let us design the NFA
Finally the r will be represented as
!e Wili b
Please refer Fig. 3.28 on next page.
it does not contain the substring
Ex, 7 Construct the NFAfor the language in which
'ab' over the set ={0}.
Sol. : The r.e. for this particular language is
r.e. = b* a
b
47
in n and In.
Let us try to break the r

where
Flg. 3.25
54
Theory of Computatilon Regular Exprossion

Hence, NEA for f will be

Start

Fig. 3.29

The NFA for I, will be

Start

Fig. 3.30

The final NFA will be

Please re fer Fig. 3.31 on next page.


Automata and Regular Expre
3.2.2 Equivalence of Deterministic Finite
represented by its e"
1heorem : The regular expression can also be
deterministic finite automata.

Proof : Let, L be the set of the language accepted by the DFA.


The DFA can be denoted by the

M =(q1. q2.. qn}E, 8, q1, F)


Let. he R
The
R denotes the set of the strings x such that S(qi,x) =4
g015t
indicates source state to target state respectively. The inputs are into the st
Fig. 3.28 For Ex, 6
states of finite automata means that with some input entering
coming out of it. The value of k is always less than ior j.
56
Theory of Computatioaular Expression 57

| l6(9,4)= 4} if izj

We have to show that for each i, , k there exists a regular expression


basis (k = 0). The R' is a
lenoting the language R*. We will put the induction on k
either & or a single synbol. The r is based on
set of strings each of which is it will be
there is no such a then
$(qi,a) q). The r denotes the set of such a's, if
taken d's . If i = jthe all the a's +E will be the set.
given by regular
Induction : The formula for getting the language RK, is
expression
()(r)(*)
to which completes the induction.
NFA
to simply get the language Rf,
this To get the final regular expression, we have will be number of
indicates final state and n
reduce where i indicates start state, and j
final
The
NFA items. If there are p number of paths, leading to final state, the
can
We
where F is aset of final states F =91.q24ip
Let ussolve some examples based on above proof
Problems based on FA to r.e. conversion :
autonata given below.
Ex. 1 Construct the regular expression for the finite

Start
q

Fig. 3.32 FA

Start Sol. :
k=0

Fig. 3.31 For Ex. 7


I2
Ihe R is denoted by,
R= R (R$1)*RuR I2
Expression
58
Theory of Computatlo Regular k=1
i= 2, j= 1,
above table we have calculated the values as hi will indicate the set of a
the input string from 9 to q,. If i =j then we add s with the input string. If i
and there is no path from q, to q, then we add .
= (e)*[ +)
.. E =
= h+
.where i= l.j =1, k =0. There is no path from qi to qi but i=j. So we add
i-0 column at row. =

=1
Similarlv i =2, / = 2, k
r = The input trom q: to q2
r rh = r (r)° ()+r
= - (e)*(0)+ [
rÊ = No input from q2 to q1 and ij
=+ ¬
3we add ¢over there.
r = Noinput from q2 to q2 + since i = j.
regular expression.
state, which denotes the
Now let us compute for final
add [.
and final state is q: w
inuld the table when k= 1 because there are total 2 states
r, will be computed,
k = 1 start state 1s q1.

Computation
Reg. Exp.
(4) (%)* (tb)+(2)
O(e)*() +0
() = 0+0
i= 1,j =1, k= 1
r = 0 which is a final regular expression.
Ex, 2 Construct regular expression from given finite automata. the impurtant te
following
= e(e)"(e)+e Sol. : As we have seen in previous example, we are
as

i =1,j =2, k = 1
Start
r= (8)(9)+r 41

=e(e)*(0)+0
Flg. 3.33
= E-0+0

= 0+ 0 -()(4)+*
=0 Let us compute when k = 0
61
60
Theory of ComputatioRegular Expression
Considering i = 1, | =2, k= 2

- (01(1l +)) +0
- 01*+0
) arnd
This is a language begining with
This is a final regular expression.
\on we will calculate for k = 1 followed by any number of 1's.
Regular Expressions
3.3 Equivalence between Two
the same set of
if and only if R, represents
i = 1.j= l, k = 1 Two regular expressions RË = R2
strings as R2 does.
identi ties of regular
relation we need to show some
For showing the equivalence
= E([)*[+E expressions.
expressions.
r = Here P, Q, R are. taken as some regular
k = 1 [1]eR = Re =R
I2
i= l.)= 2, k = 1 12] ([) =E and ()* =[.
denotes empty string
where E indicates null string and ¢

= t: 0+ 0
[3] ¢ R = R =¢
4] + R= R
I =0
(5) R + R=R
i= 2, j =1, k = 1
(6]R R =R* R = R:
r, =r ( ) r +r
17] (R)* = R'
8 c+ RR* = R*

(9)(P + Q) R = PR + QR
(P* + Q)*
i= 2, j= 2, k = 1 (10] (P + Q)* = (P* Q")* = Theorem which helps in
theorem named Arden's
Let us see one important
expressions.
Checking the equivalence of two regular
= (E)*·0 +(1+¬)
= 1+t

Now for calculating regular expression we should compute for the path from
start state to final. That is from qË to q2
Expression
62
Theory òf Computation Regular
Theorem : Arden's Theorem : Similarly,
RH.S.
Let, P and Qbe the two regular expressions over the input set . The regular = (0+1)*
expression Ris given as
10,...
R= Q + RP = fe, 0, 00, 1, 11,111, 01,
0's, any combination of 1's
which has a unique solution as R = Q P. = lE, anycombination of
0 and 1i
We will solve few examples based on this theorem, any combination of

Problems for equivalence of two r.e. Hence,


Ex. 1 Prote (1 - 00*1) -(?-00 1)(0+10*1)(0+10'1)=0*1(0+ 10*1) * L.H.S. = RH.S. is proved.
Sol. : Let us solve L.H.S. first,
Ex. 3 Slhow that (ab)'z(a *b)
(i-00*1) +(1- 00*1) (0-101) (0+10*1) Sol. : Consider
We will take (1 + 00*1) as a common factor ababab,
L.H.S. = (ab)* = e, ab, abáb,
i+ 00*1 (e+(0-10*1)(0+10*1)) R.H.S. = (at b) = ([, a, aa, aa, b, bb,
bbb, ab...)
ant
the strings of 'ab combination i
(c+RR) where R-(0+101) Note that in the L.H.S, the r.e. will give
there is possibility of ib but not of abub
As we know, ([ + RR) = ([ + RR*) = R* abab, ababab and so on. But in R.H.S.
(1- 00*1) (0+101*) out of this consider on.

L.H.S. * R.H.S. is proved.


(1+ 001) (0+10*1)* Thus
Ex. 4 Shov that (r +s) *# r*+s *
Sol. : Let us consider the [Link]. first.
Taking 1 as a common factor L.H.S. = (r + s)*
(E +00) 1.(0+10*1)" = ¬, r, rr, s, SS, rS, SI, rsrs, ....
Applying e+00* =0*
= le, any combination of r and s)
0*1(0+10*1)*
- RHS. R.H.S. = E, I, rr, rrr, S, ss, ....
only
Hence the two regular expressions are E, any combination of only r or any combination of
Ex. 2
Shoe that (0'1")°=(0+1)* equivalent. Note that in R.H.S. there is no combination ofr and s together
ielce
Consider [Link].
L.H.S. # R.H.S. is proved.
= (0*1*)* Ex, 5 Prove that r(s +)=rs+r!
= e,0,00,1,11,111, 01, 10,.. Sol. : n case of L.H.S.
= (any
cornbination of O's, any combination of 1l's, any LH.S. = r(s + t) solving this further
combination of 0 and 1, e)
64 65
Theory of Computation Regular Expression
.e. r is concatenated with s or with t
b+q2 b
= R.H.S.
Similarly 42

LHS = R.H.S. is proved. Let us simplify q first

3.4 Arden's Method for


(Another Method) Constructing Regular Expression from DFA We can re-Write it as

A 1e hae seen the Arden's


theorem is useful for checking the equivalence of reduced to R = QP"
WOrular RP which further gets
rsSiOn, we vill alsO See itS use in
conversion of DFA to r.e. which is similar to]R = Q+
Foilowing aigorithm is used to build the r.e. from given DFA. Assuming R = qi, Q= E,
let ) be the in1tial state.
We get
qn number of states. The final state may be some qi
here iS n. [R = R

3 Let a, represents the transition fromn qi to qi. Substituting value of q: in q2 we get


4. Calculate q, such that b+q2 b
92 91

= aji 4i a b + q2 b
q2 b,
assuming R = 92, Q=
If q is a start State equation with R = Q+ R P
We can compare this
P=b which gets reduced to) R =QP*.
= ab b*
Similarly compute the final state which ultimately gives the regular q2
Let us solve few examples based on this expression r. RR = R*
As
algorithm. 92 = a b nomaally
Problems : regular expression, we
given DFA, if we want to find out the is a final state and
q:
Constriuct r.e. from given DFA. From the
for final state. Since in the given DFA expression
calculate the equation DFA represents a as
regular
conclude as the
Q2 =n * b . We can DEA.
expression from given
Start Ex. 2 Find out the regular
92

Fig. 3.34
Start
Let us write down the
equations
q1 = qj a+¬ 92

nce q1 is a start state [ will be added and the input a is coming to q1


we write trom qi 93

State = input coming to it x source state of input Fig. 3.35


Regular Expression
66 Theory of Computation +&
= qi0
Sol. : Let us solve the DFA bv writing the regular expression, for each state. 9il+q2 1
(0+1)
. nitial state q1= 2 0+ q: interested in solving q1 and q: only
are
q2, we
tes are cq and
Since final sta
first
Let us See
For getbng the re. we have to solve q the fmal state. write
equivalent so we can
R= ) + RP
Which is
92 = q:(l+01) +q:l q1 = E(0) *
[ R=R
We will compare R = Q- R P with above equation, so R = 92, Q = qi, 1,
get
P-{l -01) which ultimate gets reduced to QP. into q2, We will
Substituting this value
q: = qil{1+ 01)* = 01 + q2
42 QP*
Subst1utng this value to q1 R= Q + RP
q2 = 0*1 (1)*
q = q 0-q:0+E expression is given by
The regular
= q:0- q2 00+[
q: 0+q: (1(1+01) ) 00 +[ = 0* + 0* 1
1*
11* =1*
q: =q: (0+1(1 +01) *00) +[ r = 0* + 0*
1-
DEA given below
Again R = Q-RP
Ex. 4
Construct r.e. for the
Where R = 9:

P = 0- i(1 -01)* 00
Hence
qi = [o+ p(1+01)*00]
0,1
qi = [0+1(1 +01)*00]* &:R= R 93
Q4
Ex 3 Construct re. for the given DFA.
Fig. 3.37
0,1
equations
Star. Sol. : Let us see the q2l+q3 0+[
q1

Fig. 3.36 q3 = qil


q31 +q4 (0+1)
Sol. : Let us build the reglar expression for each
state. q4 = q2 0+
Theory of Gomputation 69
Regular Expression
Iet us solve q1 tirst,
expression,
= q:l+ q3 0+[ Sol. : T'o find the language, we will solve for regular

= qi0l+ qi l0 +[
43 = q3q1 b+qoa
q1 = q (01+10) +
:"R = Q+ RP Substituting the value of qo in q:
q = (01 +10)* =’ QP" where
43 = q3 (a+b) e a
9 =(01 - 10)* R = q1. Q=, P= (01 +10) +a R= Q +RP OP*
43 = q3 (a + b)
Ihus the regular expression will be q= a, p =(0 + b), R = g3
bj where
43 = a (a +
= (01 - 10) which represents the language con
taining al!
This is a final regular expression,
biei a tinal state, iwe are
interested letter a.
in g only. the strings which start with exprrssiOn
Keres nt the language accepted bu following DEA. Ex. 7 Describe in English the language indicated by followIng reqular

Start
Start 4

Fig. 3.38
Since there is only one state in the finite Fig. 3.40
autommata let us solve for qo only.
states are
qo = U qo-1qo +[ Sol. : The equations for the
qu = (0+1) q0 +E q0 = qia+ qi b+[

= e (0-1) 91 = qo (a + b)
.: R= Q + RP
9o = (0-1) " We will simplifyqo first
Ct do is afinal state, qorepresents the final r.e. qo = (a + b) q +[
as
q1
r =(0+ 1) * Let us put this value of qo into

L (r) = E, 0, 00,1, 11,10,... q: = ((a +b)qi t[) (a + b)


L (r) = any g = (a + b) (a + b) q) + (a +.b)
combination of 0 and 1) = (a + b) (4 + b}
Represent the language given by following DFA. P where R=q, Q = (u + b), P
This is equivalent to R = Q+ R
which gets reduced to QP*,
b qi = (a +b) [(a +b) (a +b)]:
indicated by DFA.
This is a regular expression
àccepts all the
is the language in which it
The language given by this DFA
strings of odd length.
Fig. 3.39
70 Regular Expression
Theory of Computatlon that if 7 Now let
a constant n such
regular set. Then there 1S
Ex. 8 Construct the regular expression for following DEA. Theorem : Let L be a |u vl <n Ivl 21for :
u v W Such that
in L I2I> n we can write z = than the number of statoe
Word greater tis deno
The n should not be
i ) , u yi wis in L.
0,1 regular it is accepted by a DFA. M=(Q,,0, qui
Proof : If the language L is Consider the innut e
Start 0.1
92 particular number of sta tes say, n. could be written iv pumP
With some mapping function §
The
0,1 m m2 n vhere I
l|, 2, l3,...
(q0.q1q2.43.9)=9
is as shown below.
The transition diagram
Fig. 3.41
OW WO
Sol. We can get the regular expression from state q2. Let us see the
each state. equation of
ay+f-...9m

9: = q0l +qo0+q21+q20 Fig. 3.42 Pumping lemma


a,a2, ... a, akai ak-2
q1,42,q3,..qm iS in L (M) then
lt qm 1s in F i.e. qm that goes through q bu
Since there is path from qo to
Purting value of qe in q: n'
also in L (M).
Thus
q1 =[l +e-0-q2 (0+1) around the loop labeled aj1 ....a7.
...a m)
S(qu.1,a, akil .am)= (6(qo.q.9,). 4kol
q: = (1+0) +q2 (0+1) = (4,,[Link]) iUS the
Now solve q2 perfec
=8(qk, qk+l .qm )
q2 = (1 -0) q: = qm
= (1+0))[(a +0) +q2 (1 +0)] given any long string can be accepted
That what we have proved is that string thàt
92 = (1+0)-(1 +0) +q2(1 +0) (1+0) substring near the beginning of the
we should be able to find a resulting string may be d
as we like and
Here R= 92. Q = (1-0)(1 +0), P (1+0) (1+0) pumped i.e. repeated as many times
by FA. is regular e
q2 = (1+0) (1+0) [(1 +0)(1 +0)] check whether given language
The pumping lemma is used to
is a regular expression. Problems based on pumping lemma
3.5 Pumping Lemma for Regular Sets
Ex, 1 Show that the set L-bli>1} is not regular.
This lans
is not regular.
TThis is a basic and inportant theorerm used for Sol. : prove that the language L=bi
We have to
accepted by regular expression or not. In short, thischecking whether given string is
Jemma tells us whether given
such that number of b's is always a perfect square.
Pun
language is regular or not.| For example, if we take i = 1
One key therne is that any language tor which it is L = bl = b the length = 12
ion rs definitely the regular language.
possible to design the finite
= b2 = bbbb length = 22

and sO On.
73
Theory of Computation Regular Expression
No et us cOnsider
uvw

where length - n
t is dnoted b
= bbbbbb

= length of b is not a perfect square


B pumping emma 2= u W pumping something
Thus the behaviour of the language is not regular, as after
where i the same property (being square for this example.)
onto it does not show
As w where i = 1 Ex. 2 ls the following language regular ? Justify your answer
Le{0n| nz1}.
Now w w! ump V !.e make i = z. Sol. : This is a language length of string is always even.
As ne niàde 1 =21e have addei one n n = l : L=00
i.e.
n = 2 L =00 00 and so on.

<n +n2 Let L = uvw

ulw I<n +n +n +1 L = 02n


n- -i < urw i s (n +1) |z = 2n =uvi w

utwi s (n +1) If we add 2n to this string length.


hus the string lies |z = 4n =[Link]
between two
perfect square. Hence we can sayconsequetive perfect squares. But the string
the given language is not is = even length of string.
or exampie. regular.
even length. So the
Thus even after pumping 2n to the string we get the
L= hi2 language L is regular language.
Ex. 3 Prove L=faP|p is a prime is not regular
1 = 2
Sol. : Let us assume L is a regular and P is a prime number.
L = bbbb
L= aP
L = uvw
|z| = uyw i= 1
uvw = bbbb
Now consider L= uy! W where i=2
ake
u = b
= [Link]
V= bh

W = b
Adding 1to P we get,
P< luvvw
pumpirng lemmna, even if we pump v i.e.
te length as perfect square. increase v then language should P< P+1
Regular Expression
74 Theory of Computatlon some programs which are
Text editors :
Text editors are used tor
uses the regular expressin for pr.
But
- 1 is not a pr1me nunbet Henve what we have assued becomes |
UNIX text editor
example
contaditom ihus ! behaves as it is not a regular language the text. For
as
Ex. 4 the strings, such
Sol. !et Lis assue that l is a regular languge S/bbb /b/
blank for the first string of two or Mor-
2 =
gives that
substitute a single
found in a given line.
regular expressiIon is converted to an Ne
length of stringz = n-n * l = 2n + 1. In the UNIX text editors any directly.
simulated
transitions, this NFA is then
Thet means iength is alwavs odd anabe
Compiler uses this program of lexical
2. Lexical analyzers : analvzer is to scan the input o
process of
compilation. The task of lexical
separate out the 'tokens'.
token in the source languoe
That is i! we add -l For example : Identifier is a category of
2n - 1 <2n - 1) + 2n + 1 be identified by regular expression as
2n - 1 < n - 2 (letter) (letter + digit)*
regulàr expres
But if r=1 then we obtain 4n + 2 = 6 which is no
way odd. Hence the If anything in the source language matches with this
language Deomes irregular. recognized as identifier. The letter is nothing bit a set |A, B,
Even ii we add 1 tu ie lenguh of zi,then
digit is {0, 1, ... 9} *. The regular expression is effective way for ueiu le

from a language.
z = 2r + ] + 1
= 2n - 2 1ule
For
even length of the string 1ars

So tus is not a regular larnguage.


The sinple way to knoW wether given
language is
drau fnte automata for it, if you carn easily draw the regular or not is that trv to
on

FA for the given L then hot


language is surely the regular otherwise not.

3.6 Appiications of Regular Expression and Finite !pr


Automata n
The regular expressions can be Imodelled by
fhnte automata. As We bavo
manv exarnples and experienced that regular
expressions are effective
of languages. The snaller unit of regular expression can
express representation
the given a st
0ver certairi 1nput set.

There are folioWing appications of regular expreSSIOns and finite autense.


language
Context Free Languages

For example
P | S -’S + S

4 S-(S)

Context Free Languages II the language Is 4 + 4* 4 then we Cah ise


The start symbol is S. The number of non
the producthon rules given hy P
terninals in the rules P is one and the
non terminal ie. S. The terinals are +,, , )and 4
only
We are using following convensions.
terminals.
1. The capital letters are used to denote the non
Ln the previous chapter we have learnt the effective use of regular denote the termas.
The lower case letters are used to
for defining regular languages. But there are certain languages which
expressions
checking syntax of any Engish
Jeed b regular expressions. For example we can not write a regular
can not be Example:The formation of production rules for
i
expression statement is
hecKing a match of well formed parenthesis or even we can not check with
e regular expression, whether the given string is SENTENCE ’ NOUN VERB
palindrome or not. We need
e itt to derine certain rules for defining any language. This is Rama / Seeta / Gopal
possible by NOUN ’
7it ng the context free grammars for the languages. As, grammar is a set
of rules
ur iening the svntax of the language, context free grammar is also used to VERB ’ goes / writes/ sings
define
he rules for svntax of context free languages. The then we can follow the above rdes.
construction of language with Thus if want to derive a string "Rama sings"
Orrect form is known as syntax of it whereas the correct meaning of the
language is
alled symantic of the language.
4.2 Defivation and Languages formally
While compiling the program, it is checked syntacticly first and semantically derive certain strings. We wil! now
The production rules are used to S). The generaion of
atter. For checking the correctness of the program syntactically we need context free grammar G = (V, I, P,
define the language generated by derivation.
rammars. Hence in this chapter we will discuss context free grammar with the help called
f some useful examples. Let us see the definition first.
language using specific rules is
Definition :
grammar. If A ’ß is a production of P
.1 Context Free Grammar (CFG) Let G = (V, T, P, S) be the context free
from non terminals or terminals i.e. in (VUT)* then
and a and y are any strings
ine context free grammar can be formally defined as a set denoted by aAy ’aßy.
- T P S) where V and T are set of non terminals and terminals respectively. P (VUT)", m > 1
set of production rules, where each production rule is in the form of Suppose a1,@2,a3, .. am are strings in

non terrninal ’ non terminals


non terminal ’ terminals
3S a start svmbol.

(76)
Context Free Languages
78 Theory of Computation
rule 1
Then we can sav that aj ’ am
rule 2
he language generated bv G is denoted by L (G). The language L is called = 0a a a a
Cuitext free language CFL if L (G) is tor CG. We can have a
The r.e. = a* suggest a set
of ¬, a, aa, aaa, .... null strings
For evample gives S ’ E.
because S is a start symbol, and rule 2
given CFG.
G-S B (a. bl S-’a Bb, S)
Ex. 2 Try to recognize the language Lfor
B ’ bbb
G=[1S). (a,b}.P.{5}] nb
IS å grammar. Then we can derve a string abbbb as
where P =
[S’ aSb T}
: ile wi! first start from start svmbol S
|S’ab
Sol. : Since S ’ aSb | ab is a rule. | indicates the 'or operator.
iThen we will eplace B by bbb S ’ aSb
S= aBb abbbb
If, this rule can be recursively applied then
Thus we can obtair the desired language L by
b descbed as a language which starts certain rules. The language L can
with letter a and having 4 b's following. Let Ihe
us solve some examples for derivation of CFG. aSb
Problems on context free grammar :
Corstruc: the FG for the ianguage having any
number of a's over the set a aSb b
So! s we hnow the regular
expression for above mentioned language is >nce
Ie.
a aaSbbb
Henc
Ls build the prOd uction rules for the
same, and if finally we can put S - ab then it becomes aaaa bbbb. Thus We
S ’ aS rule 1 any number of a's first then equal number of b's following it.
the language as {L=an bn where n1}. The only way to recognize ience wethe lar
rule 1
is to try out various strings from the given production rules. Simply byobs
NOw if want "aaaan string to be
derived we can start with start symbols the derived strings, one can find out the language getting generated trom
CFG.
Ex, 3
Construct the CFG for the regular expression (0+ 1)*
Sol. : The CFG can be
given by Thus a
rule 1
P = (S ’ 0S | 1S
rule S ’
The rules are in
combi ation of 0's symbol Sne
indicates (E, 0, 1, 01, 10, 00, 11, ...] in thisand
set 1's
[ is with the start
a string So in the rules we
the rule S ’.
Theory of Computation
80 Theory of Computation
strings which are all
EA 4 (0struta granmmar tor the language containing strings of at least two a's. Ex. 6 Construct CFG for the language . twhich has all the
Sol. palindronne over E- a,b}
if they posses same alphabets from
Sol. : As we know the strings are palindrone
V= (S. A forward as well as from backward
palindrome because
For example the string "madam" is a

read read

tie ruieS ’AaAaA is something in which the two a's are maintained since at
ieasi :ivO s should be there in the strings. And A ’ aA |bA E gives any It is the same
cornhinaton ot es and bs ie. this rules gives the strings of (a + b)".
7:us the iogic tor this exampie will be
Fig. 4.1
anv thng a(anv thing) a (anv thing)
production rules to be build
= (a, b). We want the
beiore or after a there could be any combination of a's and b's. Since the language L is over Epalindrom, a can be palindrome even b can be
the
Ex 5 Construct a grammar generating a's and b's. As [ can be production rules as
palindrome. So we can write the
he strngs which can be generated for given L is laacaa,bçbabcba, bacab ...] G = ((S), {a, b}, P, S)
S ’ a Sa
.e gammar could be P can be
S ’b Sb
S’aSa
S ’ a
S ’ bSb
S’ b
S ’c

Since the language L = wcw where w e (a + b)*


be derived as
-ience S ’ aS a or S ’b S b. The string abcba can be generated from given The string abaaba can
PIOAuCtion ruies as
aba a b
S a Sa
,ab Sba abaa ba
a Sa

abSb C abaS aba a baa b a

abcb a a ba[ aba

abaq b a
production
Ihus any of this kind of string could be derived from the given which is apalindrome.
rules
Theory of Computation Conte
82 rule 1
S -’a B
Ex, 7 Construct CEG otich consists of all the strings having at least one occurence of rule 2
000. S ’b A
rule 3
Sol. : The CFG for this language can be cquivalent to re. (any thing) (000) (anything) A -’ a
Thus re. = (0+ 1) *000 (0 +1)* rule 4
A -’ a S
rule 5
Let us build the production rules as A -’ b A A
$-ATA rule 6
B’ b
lA l rule 7
B ’b S
rule 8
B ’a BB
or the language in which there are n0 conseCutive b's, the strings with start symbol.
Ex. S Construct CH
let us apply the rules randomly starting
"10V 0 Q n0t hare conseciutive a's. -Now
Sol. : S
rule 1 -
A ’ aS!ais aB
a's. But no consecutive rule 8
n he above rules, there is no condition on occurence of aaB B
the strings which are
bs are allowed. Note that in the rule S ’ bA, and A gives all rule 7
a ab S B
starting with letter a. rule 2
aab b A B
Thus G = (iS, A), a, b}, P, S) rule 3
a ab ba B.
Let us derive the string rule 7
Derivation input Productions aabb abs
rule 2
aabb abb A
rule 3
S - a S a ab bab ba something else
aS
try out
as "aabbabba". Let us
aeb ab S ’a S We have got some string
Babab S -’b A
k2b A
A ’ a S rule 2
b A
S ’b rule 5
aab ab bbA A
rule 4
for the given CFG. bbaS A
Ex4/ Pecogn1Ze the context free language
rule 1
S -’ a Blb A b baaBA
’ ataS IbAA rule 6
A bbaabA
B -’ bibSIaBB rule 3
bbuaba
given Ci We try to derive the rules apd cet
a tind the language denoted by such a string !
Sol. stiTgs We come to know, which language Now we have got "bbaaba"
wahuLS strigs Then after observing those
rules and number them
84 85
Theory of Computation Context Free Languages
tis b obiemm more and more stings by
appiyng more and more rules
Eandomly wll heip us t0 tind out what language it indicates.
Observe the strings A ’ 0 AI1A .
enerated caretulv. iwe can draw a conchuslon as these stings contain
equal nuber
tind cuai numbr ot bs Even vOU can ty out aious rules to start and end svmbols are maintained.
obtain some Ihus dearly, in the above CFG different equivalent
0re sUings An e that it is a language l contaning all the The non terminal A ’ 0A|1AlE ndicates (0 + 1) Thus the given CFG is
umber ot as and bs
strings having cqual
*0)
X. 10
to the regular expression (0(0+1)*I +1(0 +1)L= a" b2n where n 21.
Constut iG or the language ontarning at least one ocurence of double a. Ex, 12 Construct the CFG for the language
of an b Now the
ol The C van be built with a logIC as one rule we will built for double a example we have seen the case
i.e. Sol. : As in Some previous
number of b's are doubled. So we can write
Ad the othe ue we will bult for any nåmber of a's and b's in any S’ aSbb | abb

t is as simple as this
3 Bwhch s alw avs equiv alent to (4 + b)* vOu can note it as
4.3 Derivation Trees rivA valiy
representahok or th¿ given
mbnt both t these rçes we can get the desired context free grammar. Derivation trees is a graphical is the simple way toshow how the derivationren
production rules for a given CFG. It production rules. The derivation
one occurrence from given set of
-- BAB
(anything) (anything) can be done to obtain some string
of double a
tree is also called parse tree.
alo a l a a ag ru}
Following are properties of any derivation tree
indicat1ng sta symbol tOY a CFa
ihe root node is always a node
- e FC con:ains l =(A, B, S) I = la, b). The start symbol is S. The derivation is read from left to right. i (VN , c)
nodes.
e Us derve abaab
3The leaf nodes are always terminal
non terminal nodes.
ArThe interior nodes are always the
BAR
BAB
For example :
abaab B ’a B
The Sis a start symbol.
abBAB abaab B -’ bB S-’ bSb lalb is a production rule.
bAB abaab B ’¬

abaab A -’ Qa

zbaab B abaab B -’ bB
zbaabe abaab B ’¬
= abab abagb
11
Constru t CFG for the language containing all the strings of diferent first and
5t symbois over }=0,1
Since the problem statement says as if the string starts with 0 it should end
hi or it the string starts with 1 it should end with 0. Fig. 4.2 Derivation tree
Context Free Languages
obtain derivation for Basis of induction :
Theory of Computatlon
will first try to the string aabba
86 a tree we
Sol. : To haw
string bbabb. By simply
Ihe abOre tree is a dervation tree drawn fo devng a l , and
reading the leaf nodes we can obtan the desired sting The same tree can als0 be S >aB
uB
denoted by
S -’ aBB
a aBB
B ’ bS
bS B
S -’bA
aab

aabb a] B A -’ 0
B ’ bS Inducthon step : We a
aabba
KI 1teror nodes, W
aubbab bA S ’ bA tor K intenor nodes
are le)es or inter
aabbabb a A’ a
Fig. 4.3 Osidenng tor ther

Probiems on derivation tree -


Now let us draw a tree.
Ex. 1 Dratr a erivator tree for the string abaaba for the CFG given by
Gzehere P =/S ’ aSa S

S ’aib le 7

hus this derivation t


b
We have chosen S->asa Leftmost Derivat
b
We nnew derivat
A
prrate dehn1tion.
We have chosenSasb
1als Then wvhich
mes connusing to

We have chosen S ->n sa


Fig. 4.5
itivn l whch th
Derivation and Derivation Tree
S c is chosen 4.4 Relationship between
le nghtmost der
context ree grammar 1hen S
Theorem : Let G= (V, T, P, S) be a ed turst trom the
Fig. 4.4 G which gives the string .
only if there is a derivation tree in grammar
Ex. 2 Construct the derivation tree for the string aabbabba from the CFG given by :t us see how it t
V then S ’ a if and oniv it the
Proof : If there is a non terminal S in
S ’ aB bA prove this using induction '
S-tree which gives the string a. We can easily
nterior nodes.
A ’ alaslbAA We can apply induction on number ot
B -’ blbS I aBB
88 Context Free Languages
Theory of Computation
Basis of induction
For examnple
let N dsume 5 iS the oniy one interlor which S >XYX S ’XYX
gives the string a which can be
an hen there exists à derivation tree which S-’ XYa
a, and gives the string S -’ aYX

S ’ abX S -’ Xba

S ’ aba S > aba

Leftmost derivation Rightrnost derivation


from left ro right in leftmost
derivatior
Note that we have replaced first X
XYX the last X i.e. the rightmost symbol.
Fig. 4.6
or rightmost derivation we ge
Actually, we may use leftmost derivation
lnduction step: We assUme, that the derivation will enable us to draw the tree not affect on getting of a string
same string. The type of derivation does
hh-l nterOr nodes. We have to prove, that it is possible to draw the
ile to K interior nodes hich gives the string a. In derivation Let us solve few problems based on this topic.
the derivation tree not all the
ies are leaes or interior nodes. Some are leaves
where as others are interior Problems on leftmost and rightmost derivations
1 es considerng tor them S is a parent node. and rightn1ost deri
Ex.T Derive the string "aabbabba" for leftnost derivation
using a CFG given by
S ’ BlbA

A ’ alaslbAA
2
B ’ b lbS IaBB

Fig. 4.7 Sol. : Let us see the leftmost derivation first,

Thus this derivation tree will ultimately give the


string a.
44.1 Leftmost Derivation and Rightmost aB
Derivation B ’ aBB
we know, derivation for any string aaBB
means replacement of non terminal by its
appropriate defin1tion. There may be a situation, in which there aabB B ’b
terminals. Then which non terminal should be replaced by itsare many non
sometimes confusing to decide definition is aabbS B -’ bS

Tece te iormally appty two methods of aabbaB S ’ aB


lerivation n which the leftmost non
deriving. The leftmost derivation is a
terminal is replaced first from the sentential aabbabS B’ bS

aabbabbA S -’ bA
ihe rightmost derivation is a
replaced first frOm the sentential form.
derivation in which rightmost non terminal is A -> a
aabbabba
let us see hoW it
works. is derived. Now let us solve using rightmost derivation.
ConteX
Theory of Computation
Right most derivation
90
S
S B TOOT
T ’ 1T
aaBB B’aBB TO01T
B bs T ’ 1T
aaBbS TO011T
acBbbA S bA T ’ 1T
TO0111T
aBbba A -a
TO0111 E T ’E

acbSbba B- bS
TO0111 th
ibb Abba T ’1T
1TO0111
wtboabb
10TO0111 T -’ 0T em

10 e 00111 T ’8
Ex 2 ethe stng 0001212 for letmost and rightmost derivation using CFG
G = VTP S) where 1000111

V= S T
I = 0, 1}
4.5 Ambiguity
either leftmost derivation or
The grammar can be derived in
P =S- TOOT derivation. One can draw a derivation tree called as parse tree or
syntax h
ce
derivations. The parse tree has to be unique even though the denu
on these
Soi. ttmost derivation carn be leftmost or rightmost.
grammar, thatibt
But if there exists more than one parse trees tor a g1Ven
possiblea
TOOT S’T00T there.. could be more than one leftmost or rightmost derivation
1TUOT T’ 1T
that grammar is said to be ambiguous grammar.
10T00T For example - The CFG given by G = (V, T, P, S)
1000T where V= (E)
1090T T = id}
10001T
P = (E ’E +E
100011T
E ’E* E
1000111T T ’11
E ’ id
1000111 a
10001 11
S = E)
difterentp
Now if the string is two
id* id + id we can draw the
indicating our id * id + id. then
Erng -oi. X.
1 it. n
ituonbtiea lut Problems Inus
tor
us To
derivation the
draw check uhether
Checktihe
on above
the
whether ambiguity
derivation
tree. tree grammar
:SeS
’bC S’iC
such
b C given
as grammar
ginen is of
grammar is
Fiy. ibtibtibtaea.
grammar an
4.9 ambiguous
(a) Fig.4.8
For :
is
ambiguous [Link]
Ex.
b C grammar.
1

E
a or Let
not
us E Computation
Theory of
we solve
will
somne E
have
exrcise
some

Ex. 2 Languages
Context
Free
grammar. is Sol. : ambiguous.
consists A.6 Thus
ambiguous. Thus Please
As
Simplification
of
we Consider we
there refer
some All have Show whereConsider have
the are Fig.
seen athat
extra more string R V the got
grammars 4.10 this
various of = la, b} =
= grammar more
symbols than on
babbab. is bA
A
’ ’a AAAA ’A (S (S,
CFG ’A
an Ab
next A) than
languages
are one ambiguous ’ G Fig.
page. AA =(V,
(non not
parse two 4.9
2, (b)
termúnals).always can
tree grammar R, parse For
S)
ettectively Ex.
optimized. getting trees.
1
Having
Hence
be
generated.
extra That
represented the

symbolsmeans Hence given

grammar by the grammar


unnecessary context grammar
1s 93
may tree
Theory of Computatlon Context Free Languages f S-Xt
below -
of grammar as pont in the
We see the reduction n remoe thi

Reducedgrammar - (V, T, P, S.

0, 1| and

E linination of Removal of dstart svmb


Rermoval of unit production
useless symbols E production
A b sblems on re
Cons1der t
Fig. 4.11
wher
detail.
Let us study the reduction process in
a
(b)
Removal of Useless Symbols
Any svmbol is useful when it appears on the
right hand side, in the prod:
ii no such [Link]
rule and generates some terminal string.
supposed to be an useless symbol. 0W I the g
form0.
A Symbol P is useful if there exists some derivation in the following
tt ampie nu
S apß .mbol and
mbols we g
and apß w

(c)
where a and B may be some terminal or non terminal symbol and will he
to derive certain string w in combination with P. Let us see what exactly meat itmd CFG
useless symbol with some example. S’AB

Fig. 4.10 For Ex. 2 For example - G = (V, I, P, S) where V = (S, T, X), T = (0, 1} nSider the
S ’ 0TI1T|X10|1 rule 1
increases the length of grammar. Simplification of
grammar by renoving useless symbols. The grammar means reduction of T ’ 00 rule 2
gven below properties of reduced grammar are
Now, in the above CFG, the non terminals are S, T and X. rule 2, C.
i Lach variable (ie non
denvatión of SoTne word in L.
terrninal) and each terminal of G appears in the To derive some string we have to start from start symbol S. be replace
S ’AB
2 There should not be ary production as X S
-’ Y where X and Y are
terminals. non 0T S 0T
3. lfe is not in the
language L then there need not be the 000 T ’ 00
production X ’E.
Thus we can reach to certain string after following these rules.
Theory of Computation 97
But if S »\ then there is no Context Free Languages
no ont n the rule S ’ \. urther rule as a definition to \.
Hence we can declae that is a That means there Thus we com to kn0W as B Is an useless symbol Removing B the rules are
can remore this SO atter
removal of this useless Symbol CFGuseless symbol. And nOw,
G=l TP S) where V= S, T becomes
S CA
1-0. i and P =|S -’OT 1T|0|1
C ’ b
Ex. 3 Consuler the follooing CFG
G= (V,, R,S)
Problems on removal of useless Symbol where V = (S, X, Y}
- L =(0, 1|
Conside the CF=(V IP S) R = (S ’ XYI)
nherel= S A. B T X-’1|
0, 1) Renove the useless symbols fron1 it.
P = S ’A 11 B| 11A Sol. : As
S’ B| 11 S ’ XY

A ’0 S ’0

B’ BB| For removing useless symbols X ’ 1


Now in the gen CFG ií we try to derive any derivation for Y. Hence we can elimna ie
nboi as 0but Bdoes not give any terminal string. By
string A gives some terminal It is easy to recognize that there is no
following the rules with B we
ptv get ampie number of B's and no significant string. the production with Y, and the rules are
Hence we can declare B as
iess smbol and can remove the rules associated with it. S ’ 0
iess sTmbols We get,
Hence after removal of
X ’ 1
S’ 11 A| 11
4.6.2 Elimination of [ Productions
from Grammar
A ’ 0 null string
in finite automata and regular expression that [ or a
find CFG with no useless symbols equivalent to As we have seen even though some NFA
have also seen that
indicates a string with no value. We in conteXt free
5-’ AB | CA that NA without [ nmoves. Even
B ’ BC AB Contains [ moves we can convert changing the
C’ aB | b we can remove it, without
grammar, if at all there is [ production necessary in a grammar.
onsider. the rule productions are not
meaning of the grammar. Thus [
S -’ AB 1 For example
S ’ CA 2 S - 0SI1Sl[
1f
meaning of
But we have to take a care of
nthe rule 2, C and A can be replaced by some terminating string
but in rule 1, Then we can remove & production. S ’ in other rules
changed if we place
not be replaced by terminating string. It tends to formn a never ending loop. CFG. ie. meaning of CFG should not get
S ’0S and S E
we get S ’0 when
g. : S ’ AB -’ aAB -’ aaAB - aaAB and so on.
1 Sand S ’E
as well as S -’ 1 when S ’
Context Free Languages
Theory of Computation
Similarly
Hence we can rewrite the rules as removed ¬
the CFG with productions as
Collectively we can rewrite
S ’ XYIYXIXXIXIY
Thus production is remered
X ’ 0XI0
Problems on removal of s production:
Ex. 1 Y -’ 1Y|1
Renote the eproducton from following CFG bv preserving meaning of it. remove the [ production
Ex. 2 For the CFG given below
S ’ aSa

S bSb

Sol. : Now whde rernoving eproduction we are deleting the rules X’ and Y’&. S in the
To preserve the meaning of CFG we are actually placing [ at right hand side replacement procedure, we will place [ at
whereever X and Yhave appeared. Sol. : According to the
form.
Let us take if S = [
S ’ a Sa
S ’ XYX
S ’ aa
if first Xat right hand side is .
Similarly if S ’b Sb if S = [
then S’ YX S ’ b b
similarly ií last X in RHS. =E.
Thus finally the rules are
then S ’ XY
S ’aSalb S bla alb b
below -
Elininate the [ productions from the CFG given
Y = ¬ then
Ex, 3
A ’ 0 B1 | 1 B1
if Y and X are [ thern
B ’ 0 BI1 Ble
we will delete this production. Au
S ’ X also Sol. : Now the [ production is B ’
the productions having B replaced by [.
S ’Y when both X are replaced by [
A ’ 0 BI
S -’ XYYXIXX1XIY
Now let us consider if B =

A ’ 01
X ’ 0X

.f we place t at right hand side for X then Similarly A ’ 1B1 ’11


A ’ 0 B1|1 B1|0111
X- 0
X -’ 0X10 Similarly, B ’ 0 B

if B = E
00 101
Theory of Computation Context Free Languages

BW

2 -’ AB
W > Z

A -’ aA lbA lE

A ’0B 11B 1101111 B ’ Ba lBb l


Sol. : In this example only A and B shows &productions
If we put A = E in A production then
A ’ a |b

B ’ a |b

Also

OU seve caretull then not only A have [ production but even B also Z ’ A B Putting A =&, B =[
ate Prucion ie A ’ straight forward but B ’ A ’e. Let us apply the
thed relai ement
S ’A b
So wherever Z appears we place [
X ’ b
then
Since ’Z E we can straightaway delete it.
Y ’ b

Collectively let us rewrite the rules


S ’ Y
S -’ alA blbla ala B a X ’ b
’b Y ’ b

B ’ b A ’ a Alb Alalb
B -’ B alBblalb
mal!: the rules are yields b as a
defines XY only whereas X and Y
Since S is a start symbol and it rules by A or B. Hence A and B
producing
S -’ alA blbla ala B a
terminal symbol. There is no chance for rules are
are useless symbolsand we can remove it. Finally
S ’ XY
3 -’ b
X ’ b
COnSider the CFG given below
Y ’b

which ultimately
-’ zb S ’ bb
Context Free Languages
Theory of Computatlon
102 as below
Ex. 1 I[ the CFG is
Ex. 6 Consider the CFG given belou S ’ 0A |1BIC
For eliminating [ productions
A
S P0QQQ0R| RQP
B -’ 1IA
PR01R|RRI RQP
C -’ 01
productions.
then rennove the unit
while removing S - Cwe:
unit production. But
Sel As we an see that E Sol. : Clearly S ’Cis a can add a rule to S.
we
S1milarly
consider what Cgives. So,
S -> 0A |1B|01
Hence we will renlae Qand R bv [ and accordingly add the production rules.
production so we can modify it as
Similarl P ’RR’s Similarly B ’ A is also a unit
B -’ 1|OSI00
Let us moaity the ruies if P or Q o R becomes [
unit production as
S’POQo0RQQRQP Thus finally we can write CFG without
P ’RO,0,1|1 RI|RR| RQP S ’ 0A |1BI01

Q-Q010|PQ A ’ 0S I00

R ’0Pi0QQQ B ’ 1I0ST00
Note that with P,QRother meanings are associated, they are not purely giving C’ 01
o s Therefore we car not remove P, Qand R symbols.
Ex. 2 Optimize the CFG given below by reducing the granimur.
S is a start synbol.
4.6.3 Removing Unt Productions
Thé un1t productions are the productions in which one nonterminal gives S ’ AI0C1
arother Ron terminal.
A ’ BI01|10
For exampie i
C ’ [|CD
Sol. : S -’ A ’ Bis d unit production
YZ Z’X
ther X, f and Z are urút C ’ is a null production
productions. To optimize the grammar we need to
rerrrve the unit producions. C ’ CD and D are useless symbol.
:A = E s a unit produchun and B ’ X,
; X3 ...Xn then while Reducing a grammar we have to avoid all the above conditions
A ’B productorn we should add a
rule A ’ X, X; X3 .... X. removing
Let S ’ A
Let us solve something
1e. A -’ Bis a useless symbol because B is not defined further me
S -’ 01|10
04 105
Theory of Computation Context Free Languages

Problems on CNF:
Ex. 1 Convert the followng CfG into CNE
1341,
Chomsky's Normal Form is
Sol. : As we know the rule for
terminal
Non terminal» Non terrminal. Non
i Non terminal terminal

But the CFG given is


i iw \ ! the dervation ot A so by considering A also as a useless
S ’ aaaaS rule 1

S rule 2

If we add a rule
7 Normal Forms
A -- Q which is in CNF.
h e t nthe grammar can be simplified by reducing the e production
ieie mbols, unit production. There is also a need to have grammar in Then out rule 1and 2 becomes.
in As ou hae seen in CFG at the right hand of the production A ’ AAAAS
anii ot terminal or non terminal symbols in any combination. We
uh à granimar. That means we want the grammar in sonme S ’ AAAA
st 1!means there should be fixed number of terminals and non Let us take
tet ree grammar
S ’ A AAAS can be replaced by P
Tt tongortant normal forms - Chomsky's Normal Form and Greibach
K21 : n lhe will study these with the help of examples. If we define

B ’ AAAS then the rule becomes


71 Chomsky's Normal Form (CNF)
1 S ’ A B which is in CNF
Cilomsks hornal Form can be defined as
CNE, so let us convert it
i uimnat -, Non-terminal. Non terminal But the new rule P is not

" terminal B ’ AAAS can be replaced by P,


B ’ A P which is in CNF
, Shouid be Conerted in the abOve format then we can say that then
let us convert it to (NF
Betore converting the grammar into CNF it should be in and P, -’ AAS which is not in CNF so
hel wan remove all the useless synbols, productions and unit
i! Thu this educed grammar can be then converted to CNF P, ’ A As can be replaced by P,
P -’ A P where P ’ AS
"»anples for Chomsky's Normal Form.
CNF.
Now both P and P are in
I of tl.e iven CEC
Collectively rewrite these rules (these all indicate rule
106 Theory of Computation lcontext Free Languages
S’ BSB
S -’ A B
B ’ b
B ’ A P,
replacine
’ a are similar still we are not
Note that although s - aand A other productions als
simply giving a. It has
I; -’ A S by S. This is because, S is not production and that is a. Same is.
only one
want such a non-terminal having and B -’ b.
rules A -’ a
Now consider rule 2 Hence we have added the

Let us take S ASA for converting to CNF.


I We breah the rule 2 aN S ’ ASA
replace it by A
indicated by S’ A A1 both are in CNF
Aj ’ SA
The rule becomes S’ B SB
Take,
S P. R which is in CNE. replace it by A2
2:P. and indicate the same rule. So we can eliminate either of them. S’ B A, both are in CNF.
Hence rue 2 ecomes A, ’ SB
S ’ P, P,
Hence we can write the rule in CNF as
Falv ne can collectivelv show the CFG converted to CNF as
S ’ A A1
S’ AP Aj ’ SA
A ’ a

S ’ B Az
P ’ AS
A ’ SB
S ’ P, I'4
B ’ b
P - AA
S ’ a
A
Ex. 2 S -’ b
Corerl he giver CiG to CNF S aSa | bSb talb
Sol. : et us start by add1ng nevw Ex, 3 Consider a =(S, A} a,b}, P,S)
symbols for the terminals. Where P consists of
S -’ aASla
A -’ a A ’ SbA|SSIba
Convert it to its equivalent CNE.
Sol. : Let us consider,
108
Theory of Computation
Context Free Languages 109

then S - R,B is in CNF for S ’ aB


S ’R, AS
8 any nd R, ’ b

then S bocomes S - R, A is in CNF for S - bA


withi A
is now in CN
A ’ S can be written as
is alreadv in CNE
A -’ R,S
Now A -’ bAA can be written as

A ’ R2 AA replace it by R,
i.e. R3 -’ AA
R -’R;A
tthen then A ’ R, R3
B ’b
A ’ SS is already in CNF
B’ bS can be witten as
N We have defhned R ’a and R b
B’ R,S is in CNF
A Will be a conversion in CNF of A ’ ab.
Now, B ’ aBB
nalh we can wite
i.e. B ’ R, BB R, ’ a
5 R R: A ’SR4
Let RA -’ BB
R: -’ R; A
then B ’ R R.
R R: ’b
Finally we can write,
R: - AS A ’ SS
S’ R,B
A -’R, Ra
S ’ R, A
Cont er the given CFG to CNE
Conside G-(V,T, P, S) A ’ R,S
=1S. A, BI
A’ R R3
! n s t of B ’ b
A ’ bAA
B ’ RS
B-
B ’ aBB B -’ R R
start with furst rule
Languages
Context Free
Theory of Computation
We will define R,
R: S ’ R, A
R: ’AA
R, -’ a

S ’ a
already in CNF
omskv's normal form. S -> bB
onret the tollovmg CFG to CNF
Ra - b then
S - ABA Let

A - aA ls S ’ R, B

|S ’ b already in CNF
in the Chomskv 's normal from the s [Link] is not allowed. So first we will Now consider the rules,
eun inatt E pmductions.
A ’ [ and B ’[ A ’ aA
ure instead of A and B We can rewrite it as

A ’ R2 A
S’ ABA I ABIBA IAA IAIB A -’
A -’ aAla
Also, B ’ bB can be reWritten as
B -’ bBIb
B ’ Ra B
Now S ’ A and S ’ B is unit
afte val of[ production getting introducedin the grammar B ’ b
production. So we will
remove unit production also,
S’ ABAI ABI BAIAA l aA lalbB|b Collect all the CNF s to integrate and we will
get
A ’ aA la S’ A R, IABI BAIAA
B -’ bBIb
IR;AIRBlab
A ’ R Ala
Now let us convert this
grammar to Chomsky's normal form.
B ’ R BIb
Let
R, ’ BA
S ’ ABA
R,
R, ’ BA
R, -’ b
S ’ AR,
is done.
rSow S ’ AB S -BA
S An
already in
113
112
Theory of Computatlon Context Free Languages

4,7.2 Greibach Normal Form (GNF) For ewnple


NOn we will se one more irteresting normal torm called as Greibach Normal A An| Bblalb
o

grammar to equivalent GNF as


Then ve can convert this
hx ruie for NF Is
=A, 47
Here Di B) =b
Non-ternunat ’ one [Link] number of non-terminals A ’ alb

ioreg A -’ aZ| bZ.


’A is in GNF Z ’ A IB
Is in GNE
Z ’ AZIBZ
conversion to GNF
examples to get cleared with
Let us solve more
iS not in GNF
Problems on GNF :
Onert given CG into GNF is an interesting procedure. We can effectively Ex. 1 Convert the gien CFG to GNF
i e ieimas based on which it is easy to convert given CFG into its equivalent
S -’ ABA

A
letG= TP. S) be a given CFG. And A ’ a, Bis a production in B
Bn Then we can obtain G=(V, T, R.S) where R consists of B -’ bB lE
usuallv reduce the
CFG to its normai form we
Sol. : As before converting the symbols.
production, unit production and useless
grammar i.e. we first eliminate [
TNeans replacing B bv its productions. Then L (G) is equivalent to L(Gi). and B ’ [
As A -’ E
Frevampie - removing [ productions, and the CFG
will
Let us simplify the given CFG by
look like this,
A ’ aA bA aAS
S ’ ABA I ABIBAIAAIAIB

A ’ aA la
S ’ afa bAa aAS
Lemna B ’ bB|b
Let, G= (V, I, P, S) be a context free grammar. If P consists of
A ’ A a A a2 A a3 ... A an [We can even remove the unit productions]
|B,|B2|B3.(Bk
S h that ß do not start with A then equjvalent grammar in
Greibach Normal Let us replace A by aA or a in S
S ’ aABA laAB TABA IaBIlaAAlaAla
A - Bi B21B;... B, where
which is GNF

similarly S-’ BAIB can be written as


S -’ bBA TbAI bBIb
X ’ aXla, X\a; X | ... 4,X
Context Free Languages
114 Theory of Computatlon
A2 ’ 0 Aj| 1
A,Z I1Z
is also in GNF. Az ’ 0
Finally equivalent GNF will be - Z ’ A2 A1
S ’ aABA aABiaBAlaBlaAAlaAla A, A Z
Z ’

in GNF form.
Note that A2 is modify Aj as
apply' lemma 1 to
A ’ aA la
Now we can
B ’ bBIb
A1Az Azl0
Ex. 2 Convert given CFG to GNF
where V= (S, Aj T=(0 1/and P is That is

Aj ’ 0 Aj A;I1A2
A, I0
Sol. : Let us rename S as A and A as A; then the CFG becomes A ’ 0 A, Z A, |1 2

is in GNF

Now let us modify Z


Let us start with A:
Z ’ A2 A, lA2 A1 Z
hand side of Z ru:
As A’A; Az and we need to convert this production to GNF we will reduce substitute value of A, at right
A; irst By using lemma 1 we can
GNF as
Take Z production and çonvert to
Let us keep Á: las it is, since it is already in GNF. Z ’ 0A, A,llA| l0AZA, l1ZA;
A Z I 12 A
Now Z ’ 0A AjZ I 1A, ZI0 A Z
Collectively we can rewrite the given CFG to its GNF as
Here if we place A, by its productions the rule becomes using Lemma 1, Aj ’ 0Aj A I1A 0 A, ZA |1ZA; 0
A2 ’ 0Ajll10A,Z I1 Z
ZlA
Here a =A and Bi =0 Aj p2 = 1 Z ’ 0 AjAjll A|l 0 Aj ZA| IZA0A, A
By lemma 2
0 A,Z A, Z I 1Z A, Z
Ex. 3 Convert the given CFG to GNE
S -’ CA

A ’ a

C’ aB|b NormalFr
So now
Sol. : If you observe the given ggrammar Aand Care in Greibach
S is not in GNF.
6 Theory of Computation Context Free Languages

For A;>A A, we will use lemma 1and substitute latest values of A3


Z A1 !b
A - a A | b A A, A,I a Z A, lb A3A
Now let us mnodify A
A, at right hand
A »A A we wil| use lemma| and substitute values of
side of A and we will get
Aj ’aAjA3lb A,A,AA, la ZAA, lb A,A 22AjA3 ib A:

Now consider Z production for modification


Z’ AjA3A; IAjA,A; Z

-A;A: A ’BC We will use lemma 1 for substituting by A


8 ’CA|b Z -’ a A1A 3AgA2lb A3 A2AjAAsA
C’ A8q A A,A
2 ’ a Z AjA;A3A; lb ¢3A;Z A,A,A;A2 i b
n ti ith A: because the nght hand side of A and A; begin with Z-’ aA;AyAsA;Zib A;A;AA
3A4Z
aZA j A3 A3A ;Zi b A,A¿Z A;A3A ;AzZibA,A,A;Z

AP: iena i tor A3 ’A A and we will get 4,&Applications of Context Free Grammar
compiled, the compiler checks
-- A;A; A2 Again if we apply lemma 1. When any high level program like C or Pascal is svntax tree And for
by constructing
the syntax of every programming statement grammar for each
building the syntax tree,it is necessary to write context free
A: ’ A:A A; A; lb A3 A2 statement in the program.

tw'e Cal piy iemma 2 to A 3. For example if in your C program the statement is
X = y + z;

lexical analvzer. It will be


> A: A;A3 A2lbA3 A,la Then the x, y and z are' identified as 'id by
interpreted as
f:= b A;A2, a = A1 A3 A2 Then according to lemma 2 we can
"id' = 'id' + 'id';

ulb A, A2 The CFG for this will be


S’ id = ET
’ aZib A AZ
E ’ E + F
7 Aj A;A, 1 A, A A; Z
E ’
is non in Greibach Normal Form. Let us solve for A2. F ’ id
Theory of Computation
118

Let us build the parse tree accordingly

5PushdowWn Automata

a machine wh
chapters that finite automata is
Fig. 4.12 Ds we have seen in earlier
languages.
accepts or models the regular could be tinit
cat nodes and vOu will get id = id + id ; If ; is missing in yor
expression the pictorial representation
stete nt get matched with T branch and hence it will generate syntax That means for arny regular context free language, we may thik
automata. By same way as, we have learnt the basic relationsh
which can model the CFL. The
as there should be something the CFG can t
Tee grammar 1s a powertul tool used in compilers for syntax regular expression is that
between context free grammar and that CFG can also
more than
constructed for every regular expression. But are subset
O00 0n 1. Thus regular languages
written for non regular languages like
context free languages.
language. If we thinK
It will always effective if we can model this context free
CFG then it shows its limitations. In other words FA can it
FA to model any
model soms-languages like 0n 1^. This is anon regular language. So we Can det
finite autom
one important property and that is For every regular expression the
can be drawn, whereas we can not draw finite automata for any non reg nes
Tanguage. Thus FA is not sufficient to model any context free language bo
nodel has come up, which is called pushdown automata. )The speciality o
machine is that it has a stack which is used to remember he input Thus in
memory is used by means of stack.
In this chapter we will show the relationship of pushdown automata
coext, free grammar. We will also discuss some properties of context free languty

51 Befinition of Pushdown Automata


The pushdewh automata will have input tape, finite control and stack
(119)
120 121
Theory of Computation Pushdown Automata

input tape START) Start symbol

Accept state (which is always a final state)


(ACCEP)

Finite Reject state


COntroi
RE JECT )
Stack
Connecting edge
Fig. 5.1 Pushdown automata
Fig. 5.2 Symbols used in PDA
The nput tape is divided in many cells. At each cell only one input
symbol is
piced thus certan input sting is placed on tape. The finite control has some Follow ths
branch for p o
pte hich point the current s mbol which is to be read. At the end of input $ or
Read
A biank) b (blank) smbol is placed which indicates end of
input. next Follow this branch
i/p for i/o
The stack is such a stucture in which you can push and remove the items from
one end onlv. For example if you place some coins one above the other, then a stack
of cons is forned. While removing a coin we can only remove the coin which is at
the top. Similarly while adding more coins to the stack we can place a, new coin Fig. 5.3 Read symbol

on on the topmost coin. Thus the stack of coin shows clear cut use of only one more powerful than FA. Any language
As we have discussed earlier PDA i
t th stack
accepted by FA can also be accepted by PDA Not even this, PDA
which can be
which even can not be accepted by FA. Thus PDA is
In he pushdown automata, we are using the stack for storing the items accepts a class of languages
how it works.
much more superior to FA. Let us see
er poraril. Inserting the symbol onto the stack is called push operation and of a's over I=a,b}
Ex. 1 Design pushdown autonata tuhich accepts only odd number
ercving a svmbol from stack is called pop operation. which we have soived for drawing FA in
Sol. : This problem is similar to the one
be drawn as
Let us have a tormal definition of pushdown automata (PDA). previous chapter. The FA for the same can
The PDA can be defined as a collection of seven components.
! Tie funite set of states Q.

2 Tne uput set


[Link] astack alphabet. Fig. 5.4

to this
+y is initial stage, qo EQ. We can draw the PDA similar

Z is a start symbol which is in Start

6. Set of final states F


cQ.
Read ACCEPT)
mnapping function used for moving from current state to next state. Read

hollowing symbols are used while drawing the pushdown automata.


the
Jr arlditgn Fig. 5.5

34ore( PDs).
122 Pushdown Automata
Theory of Computation
Vote that in a designed PDA ive are starig with a start symbol and we come
to a 'Read node if read out b' the selt loop will be applied and if we read out 'a'
then it will move to neNt Read state if n the input tape only single ' is placed
POP
adi nunber ot as) then that means t will Read out A - the blank
character. The Push
assumption s that hen

the input ends the nrut tare is hlled with blank Fig. 5.8
characters. In the PDA, the
loop of :s naintauned such that odd number of a's are allowed to
state
reach to accept b"} uhere n 2 I/
Design a PDA(for the lunguage L = fa"solved
be by the PDA. Note that We
Des PDA he language L= (001). `ol. : This is a typical example which can because, here the condition s that
Soi. The sole FA : this language is not draw finite automata for this problem
b's should be after a's. That mean
many number of a's are occuring those nany
n=5 then the string will be aaanabbbbb. If we try to design FA for this we are una
memory in FA
to keep track of how many a's or b's have occured, since there is no
Let us draw PDA for the same using memory i.e. stack

Fig. 5.6
where :s a start state and q3 is a accept state.
We can draw an ( START
PDA as equivalent

b
Read Pop Read
STAPT
PUSH
REJECT)
aEADREAD READ
1
READ
(ACCEPT Pop (REJECT

REJECT) (ACCPT)

Fig. 5.7 Fig. 5.9


W Cr te tiiat firSt read The simnple logic we have applied here is that we are starting
hles
node resembles state qo, second read sta with start s
state ard third read tement At the first read statement if we read a.
statemernt resembles q2 state, and so on we push that a onto the stack
t a d tel*(wst I will lead to accept state After repeated by applying a self loop to this read statement. By this all T I S
pushed onto the stack. Now when we will read one b the ds'
r ,xt problem orwards we wil] introduce two we will pop one .
more new symbols. process will be repeated till the end of input. Tho end marker is A. After re
from input tape, we pop the contents from the
stack. lf we pop à thdl
125
124 Theory of Computation Pushdown Automata

should not start with '). For example (0} or


her 0t a's are more than total nunmber of bs. So we go to a rejet state. Let us of ( and ') should be equal. The input valid string.
Si)uldte this imachne for some valid string. 0 0 ) or (()00).Thus we will design PDA which will check a

START
Input tape for a" h, now n = 2.
Step 1 : We will start with start symbol.
Read Pop
Step 2: le wl reach to a read state, we will read first a and push it onto the

PUSH(
Pop REJECT)

(ACCEPT)
Tne stack init:ail, contains A that means it is empty.
Fig. 5.10 Well formedness of parenthesis
Step 3: Read next sVrnbol from input tape. It is a and push it onto the stack.
input
Let us simulate this PDA for the
Please refer Fig. 5.11 on next page.
b
Hence we reach to accept state.

5.2 Instantaneous Description
like an bn
of PDA for some typical problems
Step 4: Read bfrom the tape and pop from the stack if the popped symbol is A As we have seen a pictorial model experienced one thing that for
we have
t g0 in reject states. That means number of a's are lesser than b's. So after and well formedness of parenthesis. Also we need PDA wherein there is an
not sufficient,
representing certain problems FA is by
Tea dng first b, we pop one a. can describe this behaviour of PDA
effective use of stack as a memory. We moves of ID's are as
description. It is given by ID. The
means of instantaneous
b shown below.

as

.iter reading second b we pop one mnore a. Current


input S X4
Current ( S Z
state Current
b stack X
Z
symbol
Vote that both tape and stack both are empty. Move

X. 4 Design PDA that checks the well formedness of parenthesis.)


ol. :
The wl) formedness of parenthesis means that the input should start with ("
ening parenthesis and it should be followed by ')' closing parenthesis. The number
Pushdown Automata
126 oTheory of Computation
current state
where qo 1s a

input symnbol
x is a current
symbol ie. the symbol which is at the top
Zo is the current stack
Push ( of stack.
Read
state
Also q indicates the next
interpreted
The above description can be
x(3 indicates top of the stack. there is Zo then on mr
on the top of the stack
Suppose we are in state go and push or pop oper
Push ( either we perform
Reac we will go to the next state q1 and the stack. Let us solve
the top of
depending upon input x we get xß on
Pop
examples basud on it.
Problems on PDA with instantaneous description :
Reac
Ex. 1 For agiven PDA
Push (
P = ({90.91.92}.{a,b. c}.{a,b, zo}. 8, [Link]. (92}). the mapp1ng uh
as given below.
Read R : 6(90, a, Zo) = 8(90,a zo)
R2 : (g0.b, zo) = 8([Link])
Pop (
Rs : s(qo,a, a) =8([Link])
Reac
R4 :8(90,b,a) = o([Link])
Pop ( Rs : (90.a,b) = 8(90, ab)
Rs :8(90. b, b) =S(40,bb)
R> : 8(90, c, zo) = (91,20)
Stack is empty if you
pop, you will get A Ry : S(4q0, C, a) = S(4.a)
Ro : o(90,c,b) = (91.b)
Fig. 5.11 Sirnulation for Ex, 4
Ro : a(9.a, a) = S(91e)
Ru : ö(91.b, b) = ö(91.e)
he ug =)2 chowvs [Link] if we are reading the current symbol aj at current state
S. and current stack sVmbol Z; then alter a move we will reach to state S. and howo R12 : (9,[, 2o) = S(92,z0)
wil be some nev: current symnbols on Stack afia on input tape. This description can Sinulate for the input i) baa and ii) acaa ant
qo
Sol. : As from a given problem statement it is clear that initial state is
state is q2- Let us sildiaLe it for given inputs.
28 129
Pushdown Automata
Theory of Computation
input we can cdraw a
As we reach to a final state qT the PDA IS accepting tBhis
input
transition diagram for this PDA
Stack State Move

inntial
Start

Fig. 5.13

which accepts the language L which


Rs We can interprete this PDA as a machine
contains at least one Cand any number of n's and b's
Ex. 2 Design a PDAaccepting a language L= a" b' n z0}
studied a modei of the language
Sol. : In previous section we have already instantaneous description.
L=un bn, Let us revise it and then find out

b R3 Start
Zo

b
POP Read
Read
iire there is no further input, stack is also not empty and the current state is go
1ih is anon inal state. So PDA crashes over there.
Push
POP
Input Stack State Move
initial ACCEPT
a Zo 8 (90.4,20)
Fig. 5.14
S(g0. c, a)
diagram for the above PDA it will be
Now if we try to design transition
aa z0 8(91.4, a)

S(q1, 4,a)

ZO 8(q1, E, 20)
= ö(92,20)
Fig. 5.15
Accept
Pushdown Automata
Theory of Computation
show that for each
have observed all the models, they
the example for L=an b' as
fact if you
Le, r =i(go.q1.9:) {a, b}. (a, zo},8,[Link] {42}) In
one specific path. (observe
input there is only
1. &(qo.a,z0) = 5 (qo.a zo) well formedness of parenthesis.]
the example for checking the of DFA.
u:4,a) = 8(q0,a a) exactly similarly to the concept
The concept of DPDA is
based on DPDA.
Let us solve some examples
Sb,a) = 8(q1.e) Problems on deterministic finite
automata :
{" 12m | n2 0}
E,z) =8(9:.s) Ex,1 Design DPDA which accepts a language L=
the same type of problem and i.e. an bn. Here the in
Sol, As we have seen 1's which are following the number
of
çhanged to E= {0, 1}, And the number of 001111, 011, 0001111 11
The lcoic for this problem statement is very simple and that is we start reading this DPDA the valid strings are
double. In
Ititiallv we expect input symbol to be a. As we encounter a. we push
te stack. As soon as we read b we start popping a's from the stack. Let us draw DPDA for it.
Here
hat after reading each b we perform pop operation and we
expect the
,1 to be a:When we reach at the end of the input i.e. we
read out A
all the svmbols from the stack are popped and now if
we pop Ztart
astack then that should be A or null character.
he simulate this PDA for given moves then it will be as
follows - 1
POP Read
1nput Move Next state Stack
Read Read
a abbA ö(g0.4, zo) 90 Zo
PUSH 0
abba
REJECT) POP
(qo.a,a) q0

bbA
s(ap.b.a) Gan Zo (ACCEPT)
ö(go.b,a) a Zo

o(go.a, zo) 92 Fig. 5.16

tic Pushdown Automata The PDA which we have drawn is deterministic in nature The reason be
is that for every specific input there is a unique path. The logic is very sit
Tle pusndown automata which we have discussed in previous Initially we expect all the symbols trom input string as 0, we stack all the
determiniic
same defiritionpushdown automata. The determiniustic pushdown sections are all And we when, encounter l's, we read out two consecutive l's and then pof
which is discussed in section 5.1. The automata has a zero. This method will be repeated till end of the input. When we read A asi st
e lhet t
should not show more paths model should be deterministic marker for the input, then the stack also should be empty. Hence atter popp
Same nput moving to
different states for the we reach to accept state.
133
132 Pushdown Automata
Theory of Computation
some symbol x
w and reversed w We will insert
is hard to find transition between
be n the form of w x w
in betwveen lence the input will

Read
Start POP

ush
ACCEPT
Read Reat
PUSH 0

01
100 Push 0
PUSH 1 PEJEC1
Read.

A00 -Read

Fig. 5.17
pop,
Action
Stack
Input
Read Start
011 X110
Read
011 X110
Read
Read

011 X110 PUSH 0


POP
pop
Read
011X 110

Accept PUSH 1
011 X110 A01
Drson DPDA for the language L which contains all the strings which are
wandrose oter E=(0, 1} 011X110 01 Read

PuaHe of palindrome consists of strings which are same even though


rom right to left or from left to right. That is 001100 or 010010 and A011 PUSH I
011XI10
ormulate this Janguage as wwI where (o e {0,1} As we are trying to
ie going to use stack for remembering the input. First of all let us 011X 1104 A011 Read
nyut! 100
roverse of w Read
011X l0A A011

we will stack the straight wand as soon as we read reverse


2wwl pop aid check each symbol and expect it to be the same. But since it
Pushdown Automata
Action
Theory of Computation Stack
434 Input Start
pop 1 ab + c*A
011 XII0A Read
ab + c *A
pop 1
e1X1104 PUSH
ab +c A
Pop 0
a11\1 10A
Read
ab + c *A
Read
PUSH
ab +c * A Aab
011X1104 pop a
and 2
ab +cA Aab pop operand I
ACCEPT

Read
Thus the str1ng 011X110 is a palindrome. ab +c*A
not.
Drsign a DPDA to check whether given postfix expression is valid or
operator ie. if a +b
Sol. : The postfix expression mears two operands and then one ab +c * A
PUSH
Here the simple logic is
is ar infix expression then its postfix expression could be ab+.the stack. As soon as the
nuballv wher we read all the operands we push them onto
operato s Tead we pop two operands (we are assuming only for binary operators). ab +c* A
Read

Thus process Will be continued tl end of the input string.


Let us See how DPDA iooks ab+c *A pop

ab + c * A Read
San

ab + c * A pop
Operator Operand 1 Operand 2
Reac POP POP
ACCEPT

PUSH Operand Thus the DPDA shows that given input is valid postfix expressiot
POP REJECT)
5.4 Context Free Grammar and Pushdown Automata
model for re
AcCEPT) As we have discussed earlier, the FSM acts as the acceptor
languag
expression. Similarly PDA is a model for recognizing context freesolveduptl
might have noticed that for whatever problems on PDA we have menth
Fig. 5.18
we can construct the context free grammar for them But now does it basica"
are
the PDAs are accepting all CFGs. Then answer is that yes PDA notsutt
accepting all the context free languages but only deterministic PDA IS
136 137
Theory of Computatlon Pushdown Autormata

dpt n t ron model a non detemnist symbol and we will see, whether
ushdown automata which Here the tape head will read the current input
ept d largU Cass ot contet trve language he non stack same symbol is there or not.
detenisic PDA is at the top of the
pualli tiar to NTA n NIDA or a unngue nput there
could be ultiple
ths pOSsibe whih are dirwtd to neM state But t is
observed that construction
1anT non deiermnstic DA iN muh moe smpler than DPDA
Fundamentally
onttt!iee language can be mdelled b VPDA
1

5.4.1 Non deterministic Pushdown Automata


ne have diSCUssed earier the non deterministic pushdown
automata is very
ue t NEA iNe nill discuss some CFG's which accepts NPDA. The CFG
hih cepts deterministic PDA-accepts non deterministic PDAs as well. Similarly
th e OMe his which can be accepted onlv b÷ NDPA and not by DPDA. Thus
.A s M eertul than DPDA. Let us see how to design NPDA.
ConstrCt a NPDA for the language L containing all the strings which are
ainndronie over E=fa.b}.
Snce the ianguage consists of all the strings which are palindrome, L = (e, aba,
The string can be of odd palindrome or even abb A

irome he logic is verv simple for constructing PDA and that is we will push a
h onto the stach till halí of the string then we will read each symbol and then
mpop optat:on We will compare to see whether the symbol which is popped
ar to he svmbul which is read. Whenever we reach to end of the input we

Tampie i! the string is


b

input tape
Fig. 5.21
the list can be divided in two halves.
The NPDA is as below

Start
POP
Part 1 Part 2

Fig. 5.19 Read


Poc
Push

Is1 l the elements of part 1onto the stack. POP


b
Push b

now We

are read1ng Fig. 6.22

Fig. 5.20
Theory of Computation Pushdown Autormata
138
we push A and B onto
is of the form S -’ AB,
The above PDA is non deterministic because for input aor b there are multiple Step 3 : If the production the reverse order
stack in reverse order. (since
We get after popping
paths tor going to next state. We have solved this problem of palindrome for straight).
t ministic pushdown automata but there little bit manupulation is straight reverse. reverse =
0serting some symbel other than the input symbol exactly at a mid, So
S -’ AB
t we get the mid of the string easily. Refer example 2 of section 5.3. We
balindrome over I= 0. 1} by inserting X at the mid of the string. We
s DPDA for the input set a,b POP

PUSH B
Start
POP

PUSH A

Reac Read POP ACCEPT

POP
Fig. 5.25

Step 4 : If the production is of the form


we design as
Fig. 5.23

iSts a NPDA for a given DPDA but reverse is not always true.

Construction of PDA from CFG


nhae discussed in the previous section that the PDA and CFL are strongly POP Read
ther. In this section we will take some CPGs and we will model
[Link] using shdown automata. The steps to convert given CPG to PDA are as
foliows. Flg. 5.26
Ster 1 :ert the given CFG to Chomsky's normal form.
This means replacing a nonterminal s by a.
Step 2: Tne PDA should start by pushing start symbol onto the stack. To derive tape, we encou
[Link] rules for the start syrnbol, we immediately perform the pop Step 5 : Finally when the complete input is rèad from the
also ey
Awe get ensured with the fact that stack is
} tetion. It is as shown below with A. Hence by popping
This can be designed as
Slart

POP READ ACCEPT


|PUSH S POP

Fig. 5.27
Fig. 5.24
40
Theory of Computation 141
Pushdown Autonata
Problems on constructing PDA from CG
B’b, C ’a, D’a can be

/pop

b Read Read
Read
rammar is alread in Chomskv's normal form. So we will take the

S ’AB
Fig. 5.30

Start complete PDA as


Finally we could draw the

PUSH S POP

S
Start
PUSH B Read Read

PUSH A
PUSH S

POP Reac
Fig. 5.28
Read
A ’ CD can be
PUSH B PUSH D
4CCEPT

POP
PUSH C
PUSH A

PUSH D

Fig. 5.31
PUSH C

Fig. 5.29
Pushdown Automata
Theory of Computation
S -’ AS

string "aab". S -’ 1S
Let us simulate this PDA for the
Stack Action
B -> 1
Input
Start
S ’ BS
Then, will take care o
Push S allowed in CNF but we
S’E is now
SA
value. The ru
pop S Now,
and This indicates S has [
popping S.
rule by simply pushing
BA Push B
CNF are
ABA Push A S ’ AS

BA pop A S ’ BS

DBA Push D
A -’ 0
CDBA Push C
aaba B ’ 1
DBA pop C
DBA Read a

BA pop D Read Read Read


Start
BA Read a

pop B
PUSH POP Read dACCEPT)
Read b

pop a
Read A
PUSH S PUSH S
ACCEPT

L. 2 struct PDA for the language of any combination of 0's and 1's. PUSH A PUSH B
fal we will corstruct regular expression from this L.
re. = (0+1)*
Fig. 5.32
ir the TIEXt step we will build the CFG for this
As per given PDA one S after Popping can be replaced by AS and anothe
S -’ 05 11Slc simulation
simply popped to have a effect of S ’[. Thus you can try out the
45 cUnvert this CFG to CNF as PDA for any string of 0's and l's.
5.4.3 Constructlon of CFG from given PDA
A ’ 0 As per our discussion, the CFG and PDA has a strong relationship. As e
Iius S becomes
seen in the previous section that we can construct a PDA from
we can obtain a CFG from WIth the
given PDA. The algorithm for this is given
of following theorem.
14-4 145
Theory of Computation Pushdown Automata
Theorem : If A = (Q,, T8, Qo, Z0. F) is a PDA, then will construct a CFG
Jc ted hy IDA 1 there exists CFG Gwhich is Sol. : Let, we

Il G
G= (V, I. P, S)
bUntet tree grammai which wd
0. 1; V (SUfqo, A, qo.[u, A, q: ) (qo.S, qo] . l9u. S, q:}
detined ene ated by PDA A The G
Here, 1
Ig.S, qo). (q.S, qo). (q. A, y} (.A, qo)}
And tor getting the producton rules P we will apply the Now let us build the production rules as
following algorithm.
Algornthm tor getting production rules of CFG: Using rule 1 from the algorithm
Rie 1 : !he start smbol S ’ go, S, 4o)
roduction can be

iilcates the net st. 'e and q is a start state q and qo


¬Q. Using [ule3ralgorithm for the
Rule 2: It there eusts a move of PDA a z {(go. AS)} we get.
z) =(4q.c)}
Then the production rule can be written as: P,: (go. S. qg]’ 1(40. 4 q:J[q;.s, qo)
(, 2. q) ’ a [go, S, q:]’ 1(g0, A, qo}[go, S, q}
Rule 3:I: there exists a move of PDA as P,: [9o.S, q:]’ 1[q0. A, qi](q1.S, q1]
we get,
’iq d 2) = {(9,z1,22,.. zn )} Now for 6 (q0.&, S) = {(90. s)} using rule 2 of algorithm
iten the production rule of CFG can be written as P, :>qo, S,. qo]
5(40.1,A} = {(90 AA)} using rule 3 gives
lt is solve some example to see how this algorithm works. P: : [qo, A, qo]’ 1[q0A qo][40. A qo]
P : (qo, A, qo)’ 1 (9o. A, qi][q:. A, qu]
A =(90.91} {0,1), (SA}, &.q0,S.¢) Po : (qo, A, qi)’ 1(90, A, J(40. A, q)
where ¢Is as given below. (90. A, q:}’ 1(go. A, q]{q, A, qi]
’lg01.) = {(96,A S)} Similarly 8(90.0, A) {(91. A)}
o,S) = {(90. )} P2 : [9o, A, qo]’ 0[q1, A, qo}
-ilol A) = {(q0. AA)} (qo, A, q1]’ 0 [q1,A, qu}
ola0.0.A) = {(91. A)} (q1, I, 2) =(91s) gives
g1, A, q1}’1
)4) {(a1.)} S(q1.0, S) {(90. S)} gives
yla.0s) {(90. 5)}
Construct the CFG eguivalent to this PDA.
Pusndown Automata
Theory of Computation Induction step : Let Wbe a string which is derived by grammar G

=2k, I wl n then Iw > 2k-1 while deriving


{g1.S, q:}’ 0(90.S, q] variable such that n - G can be repeated for
wstring
willsome
get non
give the
terminals of CFG
string w. If we
any number
pump the substrings to wsuch that of tn.a
the path
S Pumping Lemma for CFL this newly formed string w' (w + pumped string = w) is i and the wod g
The pumping lemma tor regular sets grammar G deriving w' is called a regular
states that every sufficiently long string in w' is s 21- then the
glar set ontains a short substring that can be pumped. In other words, if a necessary condition is that grammar G is in Chomsky's normal f grammg
tig string is given and if we
push or pump any number of
nunber of times then we alwavs get a regular set. substrings in any Let us consider a grammar
CFLs there are aiwavs two short According to pumping lemma for
both the substrìngs can be substrings which are close to each other and these G= ({A, B, C}, {a}, (A’ BC
repeated as many times as required. B ’ BA
Lemma: Let L be any context free
which depends only. upon L, such that there language, then there is a constant n,
C’ BA
W= pqrst such that exist a string w eL and |wnwhere
A ’a
1. qs>1
2prs <n and
B’ b}, A)
3. for ali i>0 pq' rsi tis in L.
Proof : This pumping lemma states that if there is a
nit productions and without a null language L which is without
production and there exist w where we L. The
string w can be derived by a context free grammar G. The G
in Chomsky's normal form. The be a grammar which is B
grammar G
we can obtain a parse tree which derives the generates language L. For the string w,
to wis less than equal to i then string w. Then if the length of the Þath
the length of the word w is less than or
2:-. We can prove this by induction step. equal to b B A
IPath
Basis : lf i= 1

Fig. 5.34
Thus

Fig. 5.33
A ’ bba = w
Iet Gcontains the rule S ’a where length of the
derived string is 1 i.e. i 1
Now according to the rule the word length should be s 2 i.e. 20 =1.
ie. path length i 3
Observe thas Iw| s 2i-! i.e. 3s22
We have a word 'a' which is of length 1. Also observe that the grammar Ci :
orSAy's nornal form. This language is regular since |w |= |pqrst| = 1 If we pump a
the substring into w
which satisfies the condition as
grammar producing string o is a regular
gramma.
Pushdown Automata
of
I. and L is a language
f43 Theory of Computatlon
If we consider W =
pqrst be the string which
language.
Proe that a b" a" is a non reular grammar palindrome. Let us say L is a context free
So! For determuning whether grammar is regular or not, we will try out the According to lemma
u t d e n autmata for it.

Normallv to chek whether there are equal number of b's following equal 2. | prs| sn
t:unber ot s we ush all the a's encountered onto the stack and as we read each 'b' substring of length i
this w but if we pump some
ite'poit single smbol and we expet it to be 'a' but in the given language after Which could be true in case of
b an those m.:v number of a's has to come, in such situations only one stack is we should get
ot sutrient and it requires two stacks. Thus the grammar generated for a b" a" L where i > (0
W =pq'rs' t e
0d regular one. Let us see PDA for this grammar
= abaaba
For example if we take w
DA Wth two stacks for l = (an bn an |n 0)
Let us group the input
START
W = (ab) (a)(a) (b) (4)
(a) (a)(a) (b)(b) (a) and not
w by pqi+l rsi+l tit becomes w'= (ab) language
If we modify L is a context free
PUSH 1al ACCEPT) then w' eL. Thus what we have assumed as language.
READ POP1 palindrome context free
(SS )|Se (a, b)* is not a
is contradiction. Hence L= L={a bi ck} i</and i< k} is a non context
free
Ex. 3 Show that the language
context
PUSH [2,bË | (REJECT language. contradiction. Assume L is a
statement by method of
Sol. : Let us prove this
any string such that weL.
free language. And w be
READ REJECT Let, w be
W = pqrst
a's
at the most strings of
snand the prs can contain
POP1 Let,qs| 1 andp r s occurrences of q's and ss.
rs' t contains additiona!
and c's. The string p q' either q or
1this means that
mentioned in a lemma, qs| language L
As per the condition contain any c's, and as per the
one a then qs can not L. Similarly
POP2 S contains at least than number of a's. Therefore the w
number of c's has to be more 's which can not
than
the
then also there are fewer occurrences of c's context free
if w = pq'rst made that L is a
hence the assumption which we had
be our L.
READ POP1 POP2 ACCEPT) language is wrong.

Fig. 5.35

Prore whether the given language L={ssT | Se (a, b) is context free or not.
string n the
IS IS a language of even palindrome. In order to recognize a
i.

ht, marhine needs to remember the first half so as to be able to compare 1t to


has the
id. ror the palindromes we use last-in, first-out principle and stack
peperty
Turing Machines
Machine
Turing
6.2 Model
of modelled with the help of following
The turing
machine can be of cells, each cell
representation
input tape having
infinite number
string can be placed on a tape. The containing one irg
empty tape is ile
1) The the input
symbol, and thus
by blank characters.

ng Machines
Input tape

Fig. 6.1 Input tape


head which is responsible for reading
control and the tape
2) The inite can move to left or right.,
e have discussed in the earlier chapters - DFA, NFA, PDA and tape head
NPDA current input symbol. The
are urious models having their OWn strengths and limitations. As we have to undergo.
seen through which machine has
ressions can be modeled by finite automata so it recognizes a 3) A finite set of states buidine
the language. The finite automata can remember only external symbols which are used in
current
not remember previous long sequence of input. Thus limitation of 4) Finite set of symbols called
, which is overcome in another model called
PushDown Automata logic of turing machine.
The DA uses a stack which is helping the automata to
remember any
v lorg input sequence. And thus accepts a larger class of Input tape
sil! he model has certain limitations and those languages than that
are removed by a | bAA8 b
Turing Marchine. The basic limitation of pushdown automnata is + tape head
à lm1ted computational capability. The Turing machine is a
mathematica! model of modern digital computers having strong simnple
Caparit computational Finite
Control
6.1 la ticiion
Fig. 6.2 Turing machine
Alan Turing is father of such a model which has
computing capability of general
crDuter. Hence this model is popularly known as Turing machine. This 6.3 Definition of Turing Machine (TM)
toiluwing features
t h£s esternal mermory which The turing machine is a collection of following components.
remembers arbitrarily long sequence of input.
ihes unlimited rmemory capab1l1ty. M = (Q E, r,8, qo. Aor B, F)
The model has a faciliry by whuch the input at left or right on the 1) Ois a finite set of states.
tape can be
read easily
e machine can produce certain output based on its input. 2) T finite set of external symbols.
Sometimes it may
be required that the same input has to be used to generate the output. o in hi 3) E is a finite set of input symbols.
.machine the distinction between input and output has been removed input
Th. marker tor
4) Aor b or Ber is a blank symbol majorly used as end
coro0 5et of alphabets can be used for the turing machine
5) Sis a trans:in or amapping
(150) function.
152
153
Theory of Comput Turing Machines
Ihe mppng function
nput symbol on the tape toshows the maping
the next
rom states ot inite
automata and
ng the tape head. This is states external Symbols and the direction
known as a triple or a program' tor
turingmachl Start (a,A,R)

9oa) (9A, L) (b,8,R)


(3.0.R)
This means that if currentlv we aA,R)
are
state ther we can go to q: sta te br reading the input symbol a and we are in (a,4,R)
a dto lett replacing or printing 'a by A and now move
(b.B.R)
be the initial state
where qoeQ
is e! t final states The Fig. 6.3
halts final state is such à state where
turing machine
will get the equivalent upper case
Thus turing nachine is such The turing machine is designed such that we final state. The & function is
machine which is used compute a large string to the input. The q0 is a initial
state and q3
transition
class of of understanding consider the
written along each edge. For the sake a on the tape the A will be printed
and qi, if the finite control reads
6.4 between qo string to
Construction of Turing Machine there and the move will be in right
direction. After converting the given
state.
state g3. The g3 is a halt
nstrution
if turing machine is a process upper case we reach to the final
tt nd net ove tunction This is a oi writing out the complete set of
totally conceptual phenomenon. The turing
: i e :an he iesigned with the help of some 6.4.2 Multiple Tracks
be as
conceptual tools. Let us discuss multiple tracks then the input tape will
. :these tools If the input tape is divided into
follows -
4.1 Storage in Finite Control
1 1 1S
hown in Fig. 6.2, the model of turing machine has a finite #1 1

TOl can be used to hojd some am unt of control. This finite p t s , whia B B B 1 1
information. The finite automata stores tB111B B
t information in pair of elerments such as the
uted h the tape head. This is just a
current state and the current symbol Viguaustion eetimato
conceptual arrangement. wih multitci,
Of ample - The function &can be written as follows.
control
ot[g 01. 1) ’ ({91.1], A, R)
"s Means that if finite control shows the initial state is go and
stores the
lihvnihoi ) i it reads the symbol ] then the machine goes to
next state q1 For example :
iat hy 1 and moves t0 right. This helps in building the first track the
transition graph tape has muliple tracks on the
ti aguage As shown in Fig. 6.4 the input unary number equivalent to 5
by # and $. The
input which is placed is surrounded the second track unary 2 is placed.
s"it a turing nnach1ne M for - fa, b} whch wll convert lower case the first track. On
tter to .pper case is placed on the input tape, on the answer on the third track
subtracts 2trom 5 we get
T'rase refer Fig 6.3 on next If we construct a TM which unary numbers
pag and that is 3, in unary [Link] this TM is tor subtracting two
tracks.
with the help of multiple
54 Theory of Computatlon Turing Machines 155
5.4.3 Checking of Symbols
Checking of synbols is an effective way of recognizing the language by TM. The
svmbols are to be placed on the input tape. The symbol which is read is marked by
w special character. The tape head then can be moved to the right or left. Let us mark it
tahe sNe eample and we nill see how to build turing machine by checking of read a if it is
and mark it
sVmbols same as
marked
Ex. 1
Comst turing "acine M=(Q.E,r,S, qo.B, F) tvhich recognizes the

sol. : In this lang uage the input set is E = la, b}. The string which when will
be
placed on the nput tape it will have two distinct parts separated by letter c, such as

caba .

Fig. 6.5
in the checking off symbols, each symbol is marked by special
simpie logic in construction of this TM will be we mark the first letter and
character. The it is
then same as
move to the right tll we not get c, the very first letter after c will be
he marked letter. If it is the same as which we have compared with marked one
marked then mark this symbol
otherwise goto reject state. It can be shown as below,
Please refer Fig. 6.6 on next page.
Now machine goes to accept state. Thus in this TM we are scanning each
symbol
and trying to recognize the string.
6.4.4 Subroutine
In the high level languages use of subroutines built the modularity in the
program deveiopment process. The same type of concept can be introduced in
construction of TM. We can write the subroutines as a turing machine. Let us see
how it works with the help of some example.
Ex. 1 Construct a TM for the subroutine
f(a, b) = a b mark it
zuhcre a and b are unary numbers.
Sol. : The unary numnber are represen ted by l's. That if a = 2 and b = 3 then

Fig. 6.7

Flg. 6.6
156
Theory of Computation
For this subroutine we will pertorm acopy operation Turing Machines 157
1hat is
in1 CopVng it after S at b number of times sulatly markng marking frst Tof
second l of a anmd X X Move to left upto Y
oPVng t at the rightmost end (le. after provouslv copied l's) for b
nes 1his make's the number of
multiplication function comlete X X Y A (onvert this I to Y nd
rnove to right, copy thas I to
rightmost end

1 X X 1 Move to Jeft upto Y


.1 Movng to nght upto $

copied to
$
Copy this l afte Aby marking it as Y. The above steps will be repeated and all the 1's between $ and A are
the rightmost end. This will be like this
1 Now move to left upto Y XX $ Y Y YA1 ' 11 I1
will move to left by conver ting all Ys and Xs to Is Finally input tape
Now we
1
will have

1 1 Mark this 1as Y and copy this 1 at the | s 1 1 1 1 1 11 11


rightmost end.
a
answer
1 1 Now mnove to left upto Y
Fig. 6.8
Y 1 1 1
We put these actions together and draw
the transition graph and model out che
turing machine.
Mark this 1 as Y and copy this 1at the
rightmost end. (1,1,R)

(1,Y.R) (,4,R)
Y Y 1 Now move to left upto Y (1,X,R) (S.S.R)
Stat
1 1
(4.1)
(X,X,R) (1,1,L) 1.1.L

ine head is pojnting to Y next Y is A that means all the 1's are over
we will (a,A.L)
iYYR
.ert thesc Yto 1's and repeat the same procedure for the 1which is left to $. Tu.1.L)
1 (X.1.L) (S.$.L)
Convert this 1 to X and (HALT
1 1
move to right upto after the
$ symbol.
(X,1,L)
Mark it Y and copy this 1to muitíplication subroutine
1
rightmost end Fig. 6.9 Turng machine for
TM
final state in the above
The HALT state is a
Theory of Computation Turtng Machines 15
6.5 Computable Languages and Functions
Again the transition between q1 and q2 is (b, b, R). That means read b, print
Th rine machine accepts all the languges even and move right. Note that as tape head is moving ahead the states are getin
though there are recursively
.sive IMeans repeating the sanne set of rules for any number of changed
id cunerable means à ist ot elements. The TM also
accepts the
tions, such as adiition, multiplication, subtraction, division, power
sle tunchon logarithmic tunction and many more. We will solve some
pies based on languages and tunction tor construction of turing machine.
Ht aturing tahine vhih accepts the language of aba Fig. 6.13
0ver =a,b}
is onlv for L = lah The TM will accept the language when it reaches to halt state. Halt state is
always a accept state for any TM. Hence the transition between q and halt ts
Wrill assume that on the input tape the string aba' is placed like this
(A, A, S), This means read A, print A and stay there or there is no move left or rgt
Eventhough we write (4, A, L) or (4, A, R) it is equally correct. Because after all tte
complete input is already recognized and nowN we simply want to enter into a
accept state or final state. Note that for invahd inputs such as abb or ab or bsb
there is either no path reaching to finai state and for such inputs the TM get:
Fig. 6.10 stucked in between. This indicates that these all inval1d inputs can not be recognize
by our TM.
ad will read out the sequence upto the A character if 'aba' is readout
N al: after reading A. The same TM can be represented by another method of transition table
b

Start
(a,a,R) (b,b,R) (a,a,R) Start (q1.4, R)
91 (q2. b, R)
(4,4,S)
(q3,4, R)
HALT q3 (HALT, A, S)

HALT
Fig. 6.11 TM for 'aba'
In the given transition table, we write the triplet in each row as
The tripiet along the edge written is (input read, output to be printed, direction) (Next state, output to be printed, direction)
ake the transition between start state and q1 is (a, a, R) that is the current
Thus TM can be represented by any of these methods
svmbol read from the tape is a then as a output a only has to be printed on the tape
Ex. 2 Construct TM for language cons1sting of str1ngs huvng any nunber of 0 s a
and then move the tape head to the right. The tape willlook like this
only eyen number of 1's over the input set E= 0,1}
Sol. : The number of zero's can be any ! but the even number ot ls are t.
allowed. We have drawn FSM for this problem in earlier chapter.

Fig. 6.12
1

Fig. 6.14 FSM for even number of 1's


161
160 Turing Machines
Theory of Computation
npt language haruS a sihst
Ex. 3 Destgn aurng nahne tolmch recogntzes the
he sale Mea can be used to draw TM We alwav's assume that at the ehd of 0ccurrence of 101 bny 110.
a 101 nnd replaces every
Replacenent of any syrnbol by some another one means after reading ot that
Sol. : symbol In this case 101 has to be
speoti symbol we should print the replacement considering 101 as a substring and
replaced by 10. The TM has tg be constructed
leaving l10 substring after cornplete scan of the input.
Stan 00R) (0,1,R)
(1.1,R)

(0,1.R) (1.0.R)
Slart (1,1,R)
HALT
(00.R) (0.0,R)
09.R)

Fig. 6.15 (AA,R)


(0.0.L)

et us siMulate the above TM for the input 1 10 101 which has even number (a,4,R)
t I e isume that this input is placed on ainput tape. (1.0,L)
HALT
tnpu! 1
heat

tate Fig. 6.16

Let us simulate it for the input.


1

190101 0 a read 1. print !. goto 4:


Start

1 0. 1 goto R and next state LS 40

Start
goto R, next state is qo
1 1Q91010A

10010 10A goto R, next state q1

0
is q:
1001 110A print 1, goto R and next state

>
q3
1001110A print 0, goto R and next state is
1 1

Start print 0, goto R and next state is a
1001 100 A
1
1
1001100A since wve read current sVimbol as d we goto
HALT accept state i.e. HALT

Ihus this input is accepted by TM.


Theory of Computatlon 2 16
Turing Machines
t a TM for the language where nz 1.
of civn nune ot l's cven nnber of 0's languuge L =a" b"
Construct TM for the PDA. In PDA we have stack
So! : For this type of already solved. this problem by of TM js we have a
problem even we have drawn FSM have advantage
The n ain can b etape" ita
remember the previouS symbol or backward, and the input symbol
forward mark it by A
which can be moved out each a
will apply is read Repeat thi
simple logic which wetape and find out the b convert it to tB.
along the input in number of a's and bs
ahead kind of unequality he IM a
all a's and b's. If there is some us formulise the logic for an bn and then construct
HALT state. Let
not end up in Convert it A and move rIght in search ot h
aab b A

A ab b A
Skip it, move ahead

Fig. 6.17 Convert it to B and move left till A


Aab b A
And noW it will be err much easy for us to convert this FSM to
TM.
Move left
A aB b A

HALT Move right


AaBb A

(2.2,R) A ABbA Convert a to A and move right in starch of b


(1,1,R)
Start
A AB bA Move right

(1,1,R) Convert b to B and move left


A ABb A

{0.0.R) (0,0,R) (0,0,R) (0,0,R)


A ABBA Move left

(1,1,R)
Now immediately before Bis Athat
means all the
A ABBA
right to ensut
are marked by A. So we will move
that no b is present
(1,1,R) A ABBA Move right

Fig. 6.18 A ABBA Move right


Note that for any FSM we can construct turing machine. Thus turing machine
A AB BA HALT
more Dowerful than TM. In the next example we will see that the problem which is
not S0iv abi. yrsM and solvable by PDA can be solved by TM as well.
765
164 Theory of Computatlon Turing MachineA,
same.
transition table for the
Thus equality between a's and b's can be checked by moving tape head to and Let us design the
A
ir let us trv tO design a TM using abOve logic
(an, 8, R)
(8,8,R) (q1. A, R)
R) (a4, B P)
(8,6,L) (a1. a, FR) |(92. 8, L)
(a.a,L) (an, A R) (as. B, L)
Start (a,A,R) (b8,L) (a, a. L)
(HALT, A L
(a B R)
Qabb 43
6.8R)
(AAR) AaBB HALT
AABb 23 Transitíon table for Ex. 5
(B.6,R) invalid input.
HALT state for such a
Thús TM doesnot lead to
(aA. Construct a TM for L=fa"b" c" }nz1 this type of
PDA. We have solved
not be solved even by an) but turung
HALT Zol. : This problem can by two stack PDA (we have solved an bn have used in
problem in earlier chapter problem even ! The same logic which we
Fig. 6.19 machine _olves his type of
used now. Let us assume that the input is
previous exampe could be
te that for the invalid inputs the machine does not go to HALT state.
Ior example
aabb bA Convert to A, move right and next state = q1
Fig. 6.20
Aabb ba Move right, keeping a as it is, state = q1

A abbb 4 Convert to B, move to left, Gae 2

AaB bb a Move to left, state = q2 (B,B,R) (C,C,R)


(a,a,R) (bb,R) (b.b,L}
(a.2,L)
A nB bbA Move to right and state = qo (B,B,L)
(b,B.R) (C.C.L) (C.C.L}
Start (a,A,R)
A ABbba Convert to A, move to right now in state q1

A A Bb bA Move right keeping B as it is. (B,B,R)


(A,A,R)
Convert b to B and move left state is q2 (B,B.R)
AABbba 94 (c.C,R)

AA BBb A Go on noving left |(A,4,R)

Now state qo move right HALT


A A BBbA

AA BBb A Move right skipping all B's


input b Fig. 6.21
AABBbA From state q3 there is no path for
‘ and TM stops there
166 231
Theory of Computation
The Turing Machines palindrOme of the string af a.
sìmulation for & abbcc can lbe
shown as below. for checking the
Construct a TM machine for pushdown automata. NoW :
a ab bccA
Convert a to A, move right transition from qo Ex: problemn we have
solved
read tirst symbol fron
to q `of. : The same type is that we Will check
TM for it. The logic from right to whether it is the s£n
going to construct symbol
Aab bec with the first
Move nght then we compare it with the second symbol from rig
from left
second symbol
Again we compare symbols. If we found any symbol not matchine
Aabbca Convert process for all the siulation of machine f
b to B, move right
transition from q1 to q2 We repeat this HALT state. The
cannot lead the machine to
we
A a BbcCA input such as
Move right We will mark it by * and move
most end in search .o*
tO right
a'bab bábaA
A BbCca Convert cto C, move right transition from q2 to q3
* bab babaA
A-Bb Ccs Move left
Moved right upto A
*bab baba A
A BbCcs
Move left till A, skipping a, b, B, C, see state q3
*babbaba A Move left, check if it a
A2 BbCca Convert a to A, move right
* babbab AA if it is a, replace it by A
AaBbCca Skip B and move right
*bnb bab A Move to left, upto
A A B b Ccs Convert b to Band move right, transition from q1 to q2
* babbab A Move right, read it
A A B b CcA Move right
Convert it to + and move right
* bab bab A
AABBCcA Convertc to C, move left till A, refer q3 state
** ab bab A Move right upto A in search of b
AA BBCCA Move right, keep B as it is refer transition from go to g4 .

convert it to A
** a b bab A Move left, if left synbol is b
AA B BCCA Go on moving right by ignoring B, Crefer q4 state
"* ub bad Move left till
A ABBCCA Since A is read TM will reach to HALT state

* ab ba d Goto right
AAB BCCA

*" ab bad replace aby and move right, upto A


a" b ch and more powerful than PDA
Thus TM can very effectively recognizes
169
168
Theory of Gomputation Turing Maohines
you will find it very
rnachine drawn for palindrome, symbol in between
We will move left and check it it is a, then replace it by A If you observe the turing no need to insert any special
logically constructed. Even there is
halves
it is a so replace by A to separate out two pal1ndrome of a string of odd
pal1ndrone for
Ex, 8 Construct a TM for checking the
1.-f0, 1} but with a slight difference
much sirnilar to previGus one
Move to lett tll Sol. : The TM will be very
(""R)

Move right (0.0,R)


(1.1,R)
(0.4.L) (0,0.L)
(0,'.R) (0,s,L)
replace it by (1.1.L)

("S)
M eright upto J
Start HALT
Move left to see if it is b, if so then replace it by A
("S)
Move left till . (0.0.L)
(1,1 L)
(A,4,L) (1,a.L)
(1,".R)
Move right and check whether it is A, if so

(".".R)
Goto HALT state
Fig. 6.23

Let us draw TM for it,


For examnple : If the string is
(a.0,R) Move right by making 0 to
(b.b.R) 00100 A

(a,".R) (4,4,L) (a,4,L) Move right


(b,b,L) *0 100 A
(a,a,L)

*0 100 A
Move left, replace by a
Start (".".R)
HALT
(4.4,L) *0 10 4 A

(".".R) Move left


*0 10A
(b,b,L) ‘
94
(b,",R) (a,0,L) (b,0.L)
*010 A
(u,a,R)
(b,b,R)
Fig. 6.22
171
70 Turing Machines
Theory of Computatlon the input iS -
that on input tape
Let us assume $
Move right upto A Move right till
111$1 110
10 A Move left and replace 0 by A Move right
111$ 1 1 1 A
I move left and convert
If current symbol is
$to )
Move lett till +
111$111 4

Move right Convert 1 to $


1111 1114

Move right 1111$ 11 4


Move right

1 move left
Move left
1111$ 11 4 If current symbol is

Since it is , goto HALT state


1111 111 4 convert $ to I and move right

-orSTAc TM for concatenation of the tuo strings of unary numbers. This TM is 11111$ 1 A Move right
´or 2 corcaterate function.
will be constructed for concatenate two strings of unary numbers. The left convert $ to 1
.eed as follows 1111 1$1A If current symbol is 1 move

e 1 1 1 $1 1|1a 11111114 Move right convert 1 to $

Fig. 6.24
11111 1$A Move right
S are separated by $ we will replace that $ by next 1. That is we
2 0 # e$ ahead. move left convert
1111 11$ A Since it is end of input we will

HALT
1111 11 A $ to A and HALT

(S,4,R) 4
Ex. 10 Construct a TM for a successor function for a given unary
f(n)=n+4
Sol. : Since the TM has to be constructed for unary number system we assume
(1,1,R)
(4,0,R) set I= (1). The logic is very much simple. We just go on moving towards rig
(3,$,R) (1,1.L) ($.1.R) end of input string. The end marker of the string Ais replaced by 1.
Start (1,1,R)
43
For example, the input tape consists of 4 the successor function will give ou
as 5.
(1,5,R)

Fig. 6.25
173
172
Theory of Computatlon TunngMachines
with string
addition, you will find the resemblance
We move to etreme Ight by keepn% all T's as it is If you observe this process of
concatenation function
for
Move right What we are trying to do IS, we simply replace by 1and move ahead right
A
searching end of the string we will convert last 1 to
1 111A Move nght The input is 3 + 2
Move right upto Sign
Move right
Convert + to l and move right
Convert A to 1

Move right
HALT
The construction of TM wil be 1111 114 Move right
(1,1,R)
111111 4 Now A has encountered so Just move left
Start (1,1,R) (4,1,R) (A.4,S)
HALT
Convert A to 1

Fig. 6.26
unary numbers
The another method of 1111 1 A Thus the tape now consists of the addituon of wo
representation of TM will be by
alwavs convenient to build transition table from the transition table. It `is
graph. The TM will look like this
States 1
(1,1,R) (1.1,R)
(q1. 1, R)
(1,1,R) (+,1,R) (A.A.L)
(9. 1, R) (92. 1, R) Start

92
(HALT, a, S) (1.4,R)
HALT

In the transition table the triple is of


(next state, output, direction).
Ex. 11 Construct TM for the addition function for the unary nunmber system. (A,a,R)
Sol. : The unary number is made up of only one
Nritten in unary number systemn as 11111. In character i.e. The number 5 can be HALT
addi tion of two this TM we are going to perform
unary numbers.
For example 2+3 Flg. 6.27
11 + 111
11111 Here we are implementirng the function of f (a+b) = c. We assume a and bboth
are non zero elements.
Ex. 12 Theory of Computatlon Turlng Machines
Construct TM forperforming subtraction of tuo unary numbers f(a -b) tape as answer for f (3 -
2).
zwhere a is
alvaus greater Thus ve get 1 on the input
Sol. : Here we have than b. =c
Let us assume that a = certain
3, b = 2,assumption
as first number is
greater than second one.
Let us draw TM for it.
so the input tape will (A,4,S)
be HALT

(1,1,R) (".".R)

--.R) (1,"L) (--,L)


Start (1,1,R)
Fig. 6.28
We will move nght to - (""L) (1.".R)
symbol as
number. Let us look at the simulation forperform reduction of number of l's from first
understanding the logic. Fig. 6.29
111-1 14 Move right upto '
greater than
Note that we have enforced a condition that first number is
by introducing oe
Move right and convert 1 to * second. Even without such condition also the TM can be draw
for the reader.
extra logic in existing TM. This part is left as exercise
Now move left
6.6 Two Way Infinite Tape

Move left The turing machine is widely accepted as a model of computation because or
versatality in the power of computation. You have already experienced in t
previous section, how powerful this model is. The TM can perform stu
111-*1 A Convert 1to * operations; arithmetic computations, recogrnizing languages either regular or a
regular languages. In this section we are going to see more power of TM:
11*-* 1 4 Move right allowing the two way infinite tape as shown in figure.

11*- 1A 1 10 1 1 A
Move right till 1
4

1 1*-"1 4 Convert 1 to * and move left


Fig. 6.30 Two way infinite tape

Move left till 1 The tape head as usual can move in forew ard and backward direction.
denoted b
The turing machine with infinite also be
tape can

11*-* * A Convert 1to and move right M=(Q, r,8,qo, B, F) where Q is afinite, nonempty set of states
E in nonempty finite set of input
T is the set of external symbols used
Move right till A nextstut
8 is a mapping function which gives the mapping from current state to
qolnitial state
HALT ends
1* B is a blank symbol. In the two way infinite tape it is placed at both the
placed
the tape. In between these blank symbols the input string has to be
177
176
Theory of Computation TUting Machines
F
denotes the final state. The TM halts atter reaching in
75 inal state.
One important theorem can be stated regarding two way intinite tape as The TM M, can be
follows.
Theorem :L is recognized by a turing machine with a two way infinite tape if Start (ag.4gL)
and only if it is recognized by a TM with one way intinite tape.
(23.43.)
Proof : As theorem sta tes, if any language is recognized by aTM with one way
[Link] tape then it should also be recognized by a TM with two way infinite tape.
Let M; be a TM with one wav ininite tape and can be denoted by HALT
(A,A,S)
)
M; = (Q:E.,8,q:. B, F)
Fig. 6.33
Sim1lar!v, M; be a TM with two way infinite tape and can be denoted by
HALT stat
6(97,4) ’ (HALT, A, S) leads to
M; = (Q:.E2,I: b2,S,, B, F) M wil be
infinite tape the machine
The input tapes are as shown by following figure. Similarly, with one way

(a)
Fig. 6.34 (a)

Start
(20.40.L)
S
(a41 S Sg
(b) So
(ag.3,L)
Fig. 6.31 (a) TwO way infinite tape for M TM
(b) One way Infinite tape for M, TM

As shown in the above figure, the TM with


one way infinite tape has a external HALT
$
(a4.4)
used as indicator ([Link].L)
side. This symbol is (##,S)
symbol # placed in the very first cell from left
or the left side termination.
two way Fig. 6.34 (b)
q1, q2,q3, q4,95 Sequence then in the tape to
if ive want alanguage L=q0, symbol. way ininite tape
as a multitrack
ntinite tape the tape head is fixed at the rightmost the TM with one interesting problems
Even we can make now solve some
two way infinite tape. Let us turing machine.
simulate it as a ntite tape in the the
working of two way a'sand b's in it over
to see the a language having equal number of
Construct aTM for
Ex. 1
input ={a, b}
set ends of the input tape.
infin' tvo
advantagè of
clearly take the
Sol. : Here we will as follows.
placd
Fig. 6.32 The input can be
178
Theory of Computatlon Turing Machines
If AComes move to right by ignoring all A's and R.
ABA A, BA

ABAABA
Move right

Fig. 6.35 Move right


We will have a simple ABAA BA
right in search of b, if we getlogic as if we get awe will
b we will replace it by B replace it by A and move
to the leftmost end (ie. and move left. We will move Move right
A) and again repeat the ABAABA
b by B. We may have
string same process of marking a by A
have two separate paths shown starting with aor b, so from initial state and
for a and b. Remember we only we will ABA A BA Move right
ends, to check whether the have to check both the
the iogic first and then string is completely scanned or not. Let
move on for TM. us understand A is reached, goto HALT state
ABA A BA
Convert b to B and move right in search of a
A BaabA
The TM can be
a has to be
converted to A and move left
(A,4,R)
ABA ab A Skip B and move left
A,A,R
B,B,R
ABA ab A (b,b,L)
Keep A as it is and move right (a,A,R) (b,B,L) (a,a,L)
Start (A,A.L)
(B.B,L)
&BA a b A
Move right
(A,4,3)
ABA ab A (b,B,R) 43
(aA,L)
Move right HALT

ABA a b A (b,b,R)
Convert a to A and move right
(AA,R)
(B,B,R)
ABA AbA Convert b to B move left
Fig. 6.36
A BA A BA
Move left Similarly if the string is
AababA Convert a to A move ight
ABA A BA Move left
1
AAbab A Convert b to B move left
.ABA A BA Move left 1

AABab A Skip A and move left


180 181
Theory of Computation furing Machines
1ABab A Move right. ignore all A's
and B's A001 0 A Convert it to l and move left

1ABab Convert & to A


more nght AIO1 0 A Since A is reached, stop

Convert b to B, move left The TM could be

AABABJ Skip all As and B's and (0,0,R)


moe left (1,1,R)
(0,0,L)
(0.1,L)
(1.0.L)
Move right by skipping all A's and B's Start (A.4,L) q,(1.11)
(A.a,S)
Since both the ends are
checked and all the a's and b's
encountered as equal. So TM HALTs here. are HALT

Ex. 2
Construct TM for obtaining tvo's con1plement of a
given
computing two's complement is, we read thebinary
Sol : The iogic ior Fig. 6.37
number.
LSB to MSB From LSB, we keep all the zero's as it is binary string from turing machine.
and move left till we' do not brings significant power to
/ After read1ng tirst 1
from LSB we move left and get Thus two way infinite tape of the parenthesis.
oand go on mov1ng towards left. The thereafter we convert 0 to 1 and 1
Ex. 3 Construct TM for checking well formness also. The parenthesis
process continues upto leftmost A. PDA as well as with TM
problem with apply wil!
For examp The binary number Sol. : We can solve this The logic which will
0110 its two's complement can be be well formed in case of expression solving. of a's and equal number of
computed as should number
much similar to the logic for finding equal )' we will mark it as and
be very towards right, when we get
any
for the
will start moving will repeat this process
b's. We
Mark it as. Thus we
l's complement move left in search of (.
complete input string.
Adding 1
For example skipping (
1010.
Two's complement of 0110 Move right in search of ),
Bv our idea also we will get the
samne result and move left, in
search of ("
Mark it as
s0110 A Move to rightmost end upto A a()04
mark it as Move
Now ( has encountered
A01! 0 A mark it
Move left left in search of .

Move right, sk1p "


It is 0, so keep it as it is and move left
Move right
First 1 from LSB has encountered so keep it as it is and move left

0110 A
Complement it to 0 and move left
182
183
Theory of Computatlon Turing Machines
Convert to and move left For example
A101 001AAA We will move upto the end of the input string
Convert (to .
A101001AA.. Move left
Move right till A
Mark it . and copy it after A
A101O01AA..
HALT

A10100.41 Move left upto


The TV will look like this
the end
A10100"41 A Move left, convert 0 to, and place it, at

(".R
A101041 0A Move left upto
(.R
Start (((.R Move left, convert to and place it at the end
q.
).",L A1010A10 A

A101 A100 A Move left upto

).".L
place it at the end
(a.4.S) Move left, convert I to " and
A101.dl0 0 A
HALT
A104A1001A Move left upto "

Fig. 6.38 Convert it to +


A10*ál0010A
The transition table for thís TM will be ‘
tape
A1***A10010 A and similarly copy l at the end of
State

go (9,(R) (q2.", L) (90. 4, R) (HALT, A, S) Aa1001014 Move left skipping all

(q1. ( R) (92, ", L)


so we goto HALT state
A+*A100 10 1A Left to + is a A character
(92. ", R) (92., L)
HALT

Construct TM for reversing a binary string on the input tape. The TM will be

Sol. : Suppose on the input tape some binary string is placed, then we will read each Please refer Fig. 6.39 on next page.
character from left to right and copy it at the end of the tape.
184 185
Theory of Computatlon Dg Machinós
unary string on the tape
Construct TM for copying the inpt this TM wii!
copy operation also in
machine is able to perform visited symbois) and copy It
at he
Sol. : The turing
as A (to keep track of
read each symbol mark it
(0.0.R)
right end after A. 1
(1.1,R) left and reconvert A to
complete string we will move
(1,".R) (0,4,R)
Finally after copying the
(A,1.L) (0,0,L)
(1,1R) Q4 (1,1,L) again.
(00,R) (4,4,L)
The TM will be as follows
("."S) (A,A,R)
Star (3.4L)
(J,1,S)
HALT (1.1.L)
(1,1,R) (1.1,R)

(.S) (1.A.R)
Start
(0..R) 90
(0,4,R) (a,0,L) (A,4,L)
97 (0,0,L) (1.1.L)
(1,1,L) (0,0,L)
(".",R) (0,0,R)
(1,1,R) (A,1,L)
("."L) (A,4,L)

HALT
Fig. 6.39 Fig. 6.40
The transition table for the above TM wil] be
Let us simulate it right rerer start sta te qo
Mark 1 as A and move
.. A 1 111d ...
(go. 0, R) ‘
(9o. 1, R) (92.4, L) Keep l as it is and
move right, see q1 state
AA11 1 A
(95.. R) (92. *, R) (9). L) (HALT, 4, S)
(92. , R) (93.4, R)
t

(93.0, R) (q3. 1, R) (94. 1, L)


(94.0, L) (q4. 1,L) (9.. L) (94.4, L) A A11 14 -

from qË to q:
(q5.", R) (96.4, R) AA11 1 4 A Move right simply transition
(yo.0, R) (g6. 1, R) (97.0. L) left
Convert A to 1 and move
(q7.0, L) (97.0, L) (q1. L) (97.4, L) A A111 A1
HALT
Thus, TM reverses the
input string
186
Theory of Computatlon Turing Machines
=1
Move left log (10) = log (2)
(4) =2
AA11 1A1 log (100) = log
Move left by keeping l's as it is till 'A' carefully and you will find that the nu
Observe the above examples logarithmic value.
answer for
A AI1lA1
Move right zeros following 1is the
For example
AAII1a1 1are 2 and even answer
Convert 1 to A and move right upto A log (10O)-=-Number of zero's after
AA A11s1A - 2
Move right even after 1
The TM will be as follows -
Convert A to 1 and move left upto A (0,A,R)
(A,A.L)
4AA 1 la11 (1,1,R) (A,4,L)
Move rigkt convert 1 to A 42

AAA A1 A11A Convert A to 1


(1.4,L)

4AA A1411 1 93
Move left upto A
(4,4,L)
A A A A 1 A111
Move right convert 1 to A and copy it at the
end HALT

4AA A AA11 11 Move left upto A Fig. 6.41

2AA A AA1 111 let us take some input as log (8) = 3.


Move left and convert A to 1 upto A A1000 A Move right
sA A A 1411 1 1
A1000 A Convert 0 to A and move right
A111 1 A1111 Here TM HALT
A1A 0 0 A Convert 0 to A and move right
Construct TM for perforn1ing logarithmic operation of given
compute log (n) function. binary number i.e. A1A A0A Convert 0 to A and move right
Sol. : For obtaining
logarithmic value the binary number we will
condition as, that binary number has to be ofin the
power of 2 form. enforce a
A1A A AA Now move left
That means 1, 10, 100, 10Co, 10000, .. are
valid inputs for our TM.
log (1) = 0 A1AA A A Move left, ignoring A's upto 1
188
Theory of Computatlon
A1A A AA Convert 1to 1
18:
13A A A A
Move left

The answer is in the unary form and that is


number of A's =3
nber

6.7 Chomsky's Hierarchy


7
The
phrase structure grammar is a grammar which can be Undecidability
the machines. This grammar is modeled by any of
the grammar is given by Chomsky's
categorised in different types. The categorization of
hierarchy. Let us see this,
Iype 0: The type 0 is a class of
machines. This class consists all the languages which are accepted by Turing
computable functions and recursive languages. chapter that turing machine is suchn a
;s
Type 1: This is aclass of languages which are
they are of the form context sensitive in nature. That
A. we have discussed in the earlier
machine which computes all types of languages. It has a great computational power
automata and pushdown automata. TM has unimited
It is powerful than finite turingmachine
aj A a2 ’ Bi y B2 computational power. Thus eventhough
memory and extraordinary be
where A has to be replaced by y. There should be the model, there are certain problems which can not
is such a fantastic mathematical machine which
the right hand side of the production. start symbol appearing at
solved by turing [Link] problem such as"is thereany tuingthis problem is
always unanswered and hernce
accepts itsell as a inut ?" is
Type 2: This class consists of all the context free
grammar and the language súpposed to bean unsolvàkle problem.
ienerated by this grammar is context free language. This class of language, and if reaches to a HALT state
we
ür Syntax checking of the language like C or Pascal. language is useful The turing machine processe_ any
The turing machine thus either accepts or
TM.
call it as that language is accepted by chapter
Tvpe 3 : This is the smallest class of the languages in which basic concepts and theorems in this
are used. And this language can be modeled by FSM. This class
regular expressions rejects a language. We will see some machines.
also contains the which will focus on undecidability of turing
COntext free grammar which is of the form left linear or right linear i.e. A ’ Ba or
Å’ aB respectively. This class of languages is useful in scanning the programing 7.1 Basic Terminologies
instructions. As we know that regular expressions,
regular languages are accepted by finnt
automata. Context free grammnar and context free languages are accepted by
6.8 Power of Turing languages which are accepted by
Machine pushdown automata. Similarly there is a class of
As we have discussed with the help many interesting examples that language.
recursively enumerable
machine has a great computational capabilities. And hence it can be usedturing turing machines. Such a class is called
as a Recursive Languages
3eneral mathematical model for
modern computerS. 7.1.1 Recursively Enumerable and
input string and either accepts the
Turing machine As we have discussed that TM processes the
can model even recursive enumerable languages. Thus the TM which loops forever tor some Language
dvantage of input, rejects it or loops forever. The
ell às the Turing machine is that it can model all the computable func..[Link] as
give rise to a problematic situaion.
languages for which the algorithm is possible.
D00 (189)
490 19
Theory of Computation Undecidability
Definition of
recursively enumerable language languages as the languages for
The language L is called a Thus we could broadly define decidable
recursive enumerable language, if and only if each languages as the languages tor which there s
and every string which belongs to there exists a TM and undecidable
Lis either accepted by decidable languages TM gives yes answer for ta
sting which does not belongturing turing machine possible. For
Or loops torever every other machine and rejects
can sav recursivelv
enumerable (r.e.) languages are accepted toby [Link] other words we inputs and "no" answer for invalid inputs.
problems listed below
Definition of recursive languages - There are some undecidable
TM is regular language.
A language L is recursive if there is a 1. The language which will be acceptable by
turing machine that recognizes I. by TM is finite.
The recursively The language which will be acceptable
enunerable languages are called turing-acceptable and recursive j3. The language accepted by TM is empty.
languages are called turing-decidable respectively. is a context free language.
Let L be any language |4. The language accepted by TM
acceptable by TM
and l. he the complement of L Balting problem is undecidable
problem of TM is undecidabje The
Then The most important problem` called Halting
is stated as the problem for which (1) for certain inputs the T
halting problem the TM never reaches to
inputs
Recursive enumerable language = accepts L and rejects or halts after a finite number of steps. (2) For certain
loops for L'. halt state no matter how long it runs.
Recursive language = accepts L and rejects L'. input. Haltn
Thus from these two definitions we can say Thus TM can not predict whether the TM will halt for given
following figure
that problemn is undecidable. The halting problem can be shown by
recursively enumerable language but all recursively every recursive, language is
necessarily recursive. enumerable languages are not
Accepts for (t)
input
The word enumerable means a list of i.e. strings of L, and recursive means the (t)
Turing machine
Rejects for (t)
set which is defined by recursive
definition is
main difference between recursively enumerable acceptable by turing machine. The
is that the TM that accepts L and rejects L' languages and recursive languages Flg. 7.1 Halting problemn is undecidable
define the
recursively enumerable languages are those for which TM recursive languages. But
accepts L and rejects or Let us now concentrate on some. properties of languages.
loops forever for L':
Decidability and Undecidability 7.2 Properties of Recursive and Recursively Enumerable Languages
recursi
The terms decidability and undecidability are associated with the In the previous section we have discussed about recursive and
problems enumerable languages. We have also seen that all the recursive languages
whose language is recursive.
recursively enumerable but reverse is not true. That is, it is not necessary that d!
Turing machine takes some input as an instance of the problem, if there exists recursively enumerable languages are recursive. A langùage. is recursive lI d
some algorithm that takes such input of TM and produces the answer as "yes" on one turing machine exists which acçept it and rejects its complement. I here i
acceptance of it then that problem is decidable, on rejection of such input the 'loop forever' set existing in TM for recursive languages.
algorithm produces the answer "no" for these inputs. Generally the problems whose Let us discuss some important properties of both the type of languages.
language is recursive is said to be decidable.

And if there is no such algorithm who could solve the problem has a language
Theorem 1: Ifthe language L is a recursive language then the complen
is also a recursive language. That is recursive languages are closeu
Win is undecidable one. complementation.
192 193
Theory of Computatlon Undecldability
Proof : Let M be a TM
machine which accepts the input w and complement L' both are recursively
accept halt state. Construct Msuch that if Menters in halt state enters into the Pheoren 3 : If language L and its
W then Menters in the
halt state by accepting the same w without accepting enumerable, then L and L' are recursive.
respecttvely
languages accepted by MË and M
Similarlv if M enters in the halt state tor P'roof : Let L. and L' are the Sirmulate M and M, simultaneously M accepts
without accepting w. Thus M is an algorithm and aepting w, Menters in halt state Construct M as shown in fig 74
to
L(M) is complement of L ie. L' rejects w if M, accepts w
s recursive. This can be shown by
following tigure. wif M accepts w and M
Ma will accept it Thus
that means eitner M, or
Since w is either in L or L', never say both. It may take a long timne to
"no" but it will
will always say "yes" or algorithm that accepts
answer since M is an
M
that it will
answer this but it is certain
Yes
sNo
L. Thus L is recursive.
input
W M

No
Yes Yes
Yes
M,
Yes No
w
-
Fia. 7.2 Recursive
languages are closed under complementation
Theorem 2: The union of two recursive languages is recursive and union of Fig. 7.4
two
-ecursively enumerable larnguages is also a recursively enumerable language. and L' the pair
have some important results for the L
Proof : Let L and L2 be the two recursive languages. M and Ma be From above theorems we
the two of complementary languages.
Jgorithms which accepts LË and L. We construct M, which first simulates M,. If
4 accepts then M accepts, if M rejects then M simulates M2 and accepts recursive.
if and 1. Both L and L' are
niy if M, accepts. Since both Mi and M2 are algorithms, M is definitely going to recursively enumerable.
alt. Thus M accepts LË u L2. 2. Neither L nor L' is recursiveiy
enumerable the other is not
is recursively
For recursivelv enumerable languages the above construction does not work. 3. One of L and L'
enumerable.
Erce M may not halt. But M can simultaneously simulate M; and M2 on separate
Undecidable Problem
pes. If either Mi or M2 accepts, then M accepts. Following figure shows the Machine as an
7.3 Universal Turing many complicated
previous chapter that there are
onstruction for this theorem.
As we have seen in the The concept of universal turing
by turing machine.
problems which can be solved complex problems by turing
this property of sotving machune which
machine has come up by
Yes
"can we construct a turing
Yes
Yes
M Yes
getting raised is machine which is
start machine so the question construct such a turing
-M NO W
is universal ?" In
other words, "can we machine can do ?" Thus the
M Yes
doing anything which any other turing
capable of imitate any TM.
(UTM) has capacity to
universal turing machine
(a) Construction for (b) Constructlon for recursively
recursive languages enumerable languages

Fig. 7.3 Constructlon for union


194
ItworkS as follows Theory of Computatlon
Undecldablity
Let Tu be a transition graph as :
data set for the universal tuing machine whoOse input The TM can be shown by
form which is program to prvess, The program takesconsists of a
progranm and a
specitving some other
interpreted as input to h. The Tu ie. TM I.
a string which
The data set is a
is in the (1,0,R)
(B.1.L)

processing of w bv I,. second string w


universal turing machine simulates
Start

the
But in the provess of (0,1.R) (1,0,R)
onlv one imitation the UTM has basic
on one dimensional tape. The two dimensional problem and that is we have
dimensional tape of universal
which will convert this two turing
description of T, can not be
placed
machine. So we need
These coding is called turing dimensional description to one dimensionalsome coding
machine code. description.
7.3.1 Turing Machine Code Fig. 7.5

The universal turing can be coded with the formula


tuing machine I. Nowmachine
is something which
executes or imitates the other The move &(q1,1) = (q3,0, R)
we will see the
0i 10 10k 10! 1 0m
dimensional information (input string and coding used for converting the two
dimensional form. functions used for T;) to one We assume

Let
M=(Q f0, 1, B},8, q1, B,{92}) q1 =
one Zero
baga TM
where Q= i91.q2. . qn} q3 = three zero
We wi!! cal! the svmbols 0, 1, 1 = one Zero
B
directions of head movements will be calledas asX,,D,X and X3 respectively. Also the 0 = two zero
and D, for L and R
d{qi, X, ) = (gz, XÊ, Dm) Then the respectively.
move can be encoded as L= one zero
o' 1 0 1 0* 1 0' 1 0m
R= two zero
And a binary code for machine M will be = 8(q1,1) = (q3,0, R)
111. The machine starts and ends 111 code 1 11 code 2 11 code 3 11
with 111 and each code is separated by 11.
take some example to see how it works. Let us = i=q1, j=l, k=q3, l=0, m=R
= 0 10 1 0 00 1 0 0 1 00
Exampie - q1

Let M = (qi.q2.q3}. {0, 1}.0, 1, B} ,6, q1, B, {q2 })


have moveS Thus we can encode <M, 1011> as
111010010001010011000101010010011
S (gi, 1) = (q3.0, R)
00010010010100110001000100010010111 1011
8(q3, 0) = (9:,1, R) end string
2nated
ö(q3. 1) = (q2.0, R)
8 (q3.B) = (q3.1, L)
196
Undecldabllity
order where wi
7.3.3 Universal Languages Theory of Computatlon (0 +) in canonical
have a list of elements form. Construct a
Proof : Suppose we in binary
The language Lu is word and M, is the TM whoOse code is by M, ie whether
also acoeptable by any universal if and onl if some str1ng w is the ith whether w, is acceptable
other language denved some acceptable by Lu is hypothetical table which tellsby following Fig. 77
Nhether the uurersal language is other TM. We will now see w,eL(M,) The table is as shown
rursivel\ enumerable or not.
Theorem 1:lu is
recurs1vely enumerable
Proof : As the recursive 1 2
tept, halts tor teect enumerable languages have three stages - halts for
and loops
iànguage Lu a uniersal language isforever. When we are going to prove that the
1

tape method recursively enumerable. We will use a three 2

iet M be a three tape TM. 3

moves of TM M when given The first tape of M, is input tape which store the
code <M, w> is as a
betnen the bloc ot 111 <moves> 111.
input. The moves are stored
The second tape of M; will
contain the string w of TM M. Diagonal
Ine third tane will
store the current state.
All the three tape heads are
positioned at the leftmost symbols. Fig. 7.7 Hypothetical
table
The input tape will be as
shown in figure. that means w is not
in L (M) and if
row and jth column
If there is 0 in ih means wi is in L ( M, }
colun that
there is 1 in ih row and jth
(Q,.1) =(o- 0R) enumerable.
which is recursively
Let, Ld be a language
111 0 101 O00100100 TM
111
means there exists a
Str1ngw ’ 1011
if and only if the (i, i) entry is 0. That
The w i is in Ld
State g.
M; which does not accept Wi: means W, is
which accepts w,. That
there is another TM M; in M,. Thus it is
Let us assume that That means w, is not
that (i, i) entry is 0. not in M(( j) entry is 1
Fig. 7.6 Three tape for UTM M, in La but we find the other hand if w, is
contradictory that Ld = L( M;). On =L{M), By this, w; is in La
nhen 0' be the contents of tape 3 and shows wi is in M,, which again contradicts La accept Ld. And thus La could
which
*he universal TM M, halts in when it indicates a final state of there is no TM which will recursively enmerable
accept state. If in case of our example on accepting and wi is not in Ld. So This is because for every
Pe there is 00 then it will enumerable.
ate indicate final state q2. Hence MË will halt inthe third not be recursively
accept language a TM exists.
-itariy M, accepts <M, w> if and only if M Thus La is not r.e. is proved.
input wthen M will accepts w, M if at all runs forever recursive.
ect state if M also run forever for input w, Theorem 3:Lu is not
similarly M will halt in
at it is rejects the input w. Thus existance of M for language Lu Proof : Let A be an algorithm
recognizing Lu.
recursivelyenurmerable language. denotes
Theorem 2: 1.4 is not recursively enumerable.
Theory of Computation 199
Let La be a language Undecidablity
And hence La that is which is not r.e. since La is not either followed by all or none of th.
as follows complement of La is also not recursive. r.e. it is not recursive.
We will recognize La
Proof : As non-trivial property means
languages from the set.
Lat w Dè the string in (0 rivial property of r.e.
Let P be aproperty of which is non
sONE T ; . Give (M;, w; +1)* and w =w,. The integer i in
binary is a code
Me wj. The
as a input to
algorithm A and accept w if and onlyforif L, be the language which posses the
property P.
Construction is as shown in the Fig. 7.8. M is a machine which constructs
M;.
M, is a TM recognised by LË and
below.
The construction is as shown
M

Yes
<M, w> Yes
Convert Hypothetical
A for Lu No No
M
ACCEPT ACCEPT Accept
w'

algorithm for Lg
undecidabi!ity
Fig. 7.8 Reducing L4 to Lu Flg. 7.9 Rice's theorem for
reducing Lu to L. As
of M. We will prove it by
The
constructed algorithm accepts w if and only if w = Wi. That means wi is We have shown construction language) is undecidable so is L. As
in UTM ie. universal
. s we have an algorithm for L and as we
have we know Lu (language for w then L(M)=. And if M
accepts w then
tezinning A is algorithm for Lu, and La is not recursive. Thus mentioned
at the accept
shown in Fig. 7.9. If M does not to Li proves that P i
reducing LË to Lu where Ly (<M, W> 1M accepts w} not to be
we can prove by
accepts w i.e. <M, W> is in Lu. Thus reduction of L,
recursive. L; also
undecidable.
a property which is
74 Rice's Theorem
Machine
A pronerty means aset of languages. A class of 7.5 Limitations of Turing Machine UTM. But S it
languages. A property is context the concept of Universal Turing
means that all the context free languages form a set to have a As we have seen cannot solve ? There are many
the problems the TM
prope;. possible for us to indicate call these problems as
unsolvable
algorithm and we
A;re: is called trivial if it is either empty or satisfied by all the problems which has no
in ii 2 languages
ans either satisfied by no language or all the languages. Otherwise problems.
alled no-trivial. it is
For example
From the set of languages it is hard to find out which language is using a turing language, to determine whether
machine. There may be a case that certain languages which posses TM have finite 1) For any programming
loop forever for some input
iength input string or some may posses a TM of infinite length input string, Thus it i) the program will be in
is hard to find which languages have TM. Instead of that we can' construct another output on certain inputs
ii) will the program generates
TM M which recognizes the TMs, the TMs who accept the individual language. We is unsolvable.
rall u "Ms as M, and accepting the language as Lj. IfP is some property of
recuIsively enumerable languages then Li is a larnguage for each TM M, 'such that turing machines is unsolvable.
2) Halting problem of
L(M,)have property PNow we will see a Rice's theorem based on this principle. solution even by turing machines.
All these problems have no
Theorem : Every non-trivial property of recursively enumerable languages is
undeidable.

You might also like