Foundations of Computation Theory
Foundations of Computation Theory
ibsekana
(cA)1
Preliminaries
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
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
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
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)
{or 0
denotes the final state
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)
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
Ex. : Dsign FA to acept the string that albays ends with 00.
Sol. :
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
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
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
1
Start
p.r.
(P.q.s) (p.r
0
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
=
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
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
-’ 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
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, ...
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
re.
following regular expression NFA without
to
Siar Start
M. A6
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
IË
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
Start
Star
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
Fig. 3.26
Star
The r will be
qo 10)
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
Start
Fig. 3.29
Start
Fig. 3.30
| l6(9,4)= 4} if izj
Start
q
Fig. 3.32 FA
Start Sol. :
k=0
=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
= 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
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
= 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
and sO On.
73
Theory of Computation Regular Expression
No et us cOnsider
uvw
where length - n
t is dnoted b
= bbbbbb
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
For example
P | S -’S + S
4 S-(S)
(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
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
S ’aib le 7
S ’ abX S -’ Xba
A ’ alaslbAA
2
B ’ b lbS IaBB
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
Reducedgrammar - (V, T, P, S.
0, 1| and
(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
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
if B = E
00 101
Theory of Computation Context Free Languages
BW
2 -’ AB
W > Z
A -’ aA lbA lE
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
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
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
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
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
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
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
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
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;
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
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.
to this
+y is initial stage, qo EQ. We can draw the PDA similar
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)
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
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
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
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
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
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
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
input tape
Fig. 5.21
the list can be divided in two halves.
The NPDA is as below
Start
POP
Part 1 Part 2
now We
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
POP
Fig. 5.25
iSts a NPDA for a given DPDA but reverse is not always true.
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
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
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
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.
ng Machines
Input tape
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
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,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
(0,1.R) (1.0.R)
Slart (1,1,R)
HALT
(00.R) (0.0,R)
09.R)
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
Start
goto R, next state is qo
1 1Q91010A
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
A ab b A
Skip it, move ahead
(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
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
("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
*0 100 A
Move left, replace by a
Start (".".R)
HALT
(4.4,L) *0 10 4 A
1 move left
Move left
1111$ 11 4 If current symbol is
-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
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
(1,1,R) (".".R)
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
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
ABAABA
Move right
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
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 .
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
(".R
A101041 0A Move left upto
(.R
Start (((.R Move left, convert to and place it at the end
q.
).",L A1010A10 A
).".L
place it at the end
(a.4.S) Move left, convert I to " and
A101.dl0 0 A
HALT
A104A1001A Move left upto "
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
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
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
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
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
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.