Tcs Es 2019 Easy Solution Textbook For Tcs
Tcs Es 2019 Easy Solution Textbook For Tcs
• . TM
,;• TechKno111ledge
Pub I i c a t i o n s
Scanned by CamScanner
easy - solutions
EM04'6A
• . TM
';5' TechKnowledge
Pu b I I c a t i o Ii s 1111111111
Scanned by CamScanner
I
-·
'
Theory
.
of ComputerScience
Semester V - ComputerEngineering(MU)
Copyright © with TechKnowledge Publications. All rights reserved. No part of this ·publi~tion may be reprod~.
copied; or stored in a retrieval system, distributed or transmitted in any form or by any means, in~luding photocop_y,
recording, or other electronic or mechanical .methods, without the prior written pe.rmission·of the publ~sher.
~s book is sold subject to the condition that it shall not, by the way of trade or otherwise, be lent, resold, hired out,,or
otherwise circulated without th~ publisher's prior written consent in any form of binding or cover other than which it is
published and without ,a similar conditio~ including this condition · being imposed on the subsequent purchaser and
without limiting the rights under copyright reserved above.
Edition 2019
edition is·for sale in India, Bangladesh, Bhutan, Maldive~, Nepal, Pakistan, Sri Lanka and designated countries in
South-East Asia Sale and purchase of this book outside of these countries is unauthorized by the publisher . .
Published by
TechKnowledge Publications
Head Office: B/5, First floor, Maniratna Complex, Taware Colony, Aranyeshwnr Corner,
Pune - 411 009. Maharashtra State, India ·
Ph: 91-20-24221234, 91-20-24225678.
Email : [email protected],
Website : www.techkno~ledgebaoks .com
Scanned by CamScanner
INDEX
Chapter 1 : Introduction
Scanned by CamScanner
Table of Contents ·
" . • Index ..
• Syllabus
'
• Chapter 1 : Introduction TCS-01 to TCS--01
• Chapter .
. . 3 : Regular Expressions and Languages TCS-14 to TCS-22
TCS-56 to TCS-59
, .
• Dec. 2018 . D(18)-01 to D(18)-16
Scanned by CamScanner
·SYLLABUS
• Minimization ofDFA ·
• . RE Applications
3.0 Grammars
• Equivalence of RG and FA
Scanned by CamScanner
• I
4.0 .PushdownAutomata(PDA)
/
• DeterministicPDA , Non-DeterministicPDA
.. Applicationof PDA.
5.0 Turiilg Machine ·(TM)
• Variantsof.TM : Multitrack,Multitape
• UniversalTM.
. Equivalenceof Single and_Multi Tape TMs . .
• Recursiveand Recursivety·EnumerableLanguages.
• HaltingProblem
..
• Rice's Theorem •
Post Corresponden~ Problem
. ..
L ------------.;.....---~ '·
'
...'•.,
• /''j'I •
,,,
'
Scanned by CamScanner
Theory of Computer Science
Chapter 1 : Basic Concepts and Finite ·Automata
A Type 3 grammaris also a Type 2 grammar. A Type 3 grammar is also a Type 2 grammar.
Scanned by CamScanner
') Theory of Comp. Sci. (MU-Sem. 5-Comp.)
-
_ TCS-2
1. a is in input
- 2. - 0 is an output.
State transition behavior and output behavior of a niealy
machine can be shown separately as in·Fig. 2.2 and 2.3; or they can
be combined together as in Pig: 2.4.
b/0 a/0
b/0 Formal Definitionof a Mealy Machine
A mealy machine M is defined as :
M = {Q, :I:, 0, 6, A, qo)
Fig. 2.1 : State diagram of a Mealy machine
Where, Q = A finite set of states.
State transition function (6) (or STF): l: :!!: A finite set of input alphabet
a b 0 - A finite set of output alphabet
Q
4>
4> Cb
,. ==
6 A transition function :I: x Q
An output function :I: x Q 0
Cb 4> 43 'Io = 'Io e Q is an initial state.
<b <b <b a. 2 Distinguish between NFA and DFA. _
- Fig. 2.2 : State transition function for Mealy machine of
MU - Ma 2007, Dec. 2009, Ma 2011. Ma 2014.
Fig. 2.1 -
Ma 2015, Ma 2016. Ma 2017. Dec. 2017
_ Output ~ction (A) (or MAF): Ans.:
a b_ Difference between NFA and DFA
O' 0
q( 0 0
Transition Non-deterministic . Deterministic
Cb 0
No. of NPA has fewer More, if NFA
q3 0 0 - "' states. number of states. contains Q states then
Fig. 2.3 : Output function for mealy machine of Fig. 2.1 the corresponding
DFA will have S: 2Q
State table for both 6 and A (both STF and fy1AF) :
states.
A b Power NPA is as powerful as DFA, is as powerful_
4IO qi!O a DFA asanNPA
qi 410 q/0 Design Easy to design due to · Relatively, more
non-detenninism.
q2 411 q/0 difficult to design as
transitions are -
q3 _ (b/0' q/0
deterministic.
I I .. Ouipul
Next state
Acceptance It is difficult .to find
whether we Las there
It is easy to find
whether w e L as ·
are se:veral paths. transitions are
Fig. 2.4 : State table depictingboth transitJonand output
behaviorof mealy machine of Fig, 2.1 Backtracking is deterministic .
required to explore
several parallel paths.
easv-solutions
Scanned by CamScanner
.
.Y
.
Q. 3 Define DFA. W An input 1 in state q2,' will make numberof _1's odd.
Ans.: 6 (qz, 1) q3
Definition of DFA An input Oin state q3, will make numberof O's even. ·
'
A detenninisticfinite automatais a quintuple.
6 (q3, qi
M = (Q, !: 6, <Jo, F), where
An input 1 in state q3, will make numberof 1'seven.
Q is a set of states.
6 (q3, Q) => ql
:E is a set of alphabet
CJo is the starting state: An empty string contains even number
<Jo e Q is the initial state,
of O's and even numberof 1's. is a final state. CJo stands for even
F C: Q is the set of final states, and 6, the transitionfunction, number·of O's and even numberof 1's.
is a function from Q x :E to Q. 1 0 1
Q.4 Obtain a grammar to generate the langua;.,. ·
2
L = {Oa 1 a I n 0) }.
Ans.: 0 <Io
Productionsfor the required languageare as _follows.
P.::
1
CFG for the above languageis ({S}, {0, 1}, P, S)
(a) Transition diagram (b) Transition table
Q. 5 Give deterministic finite automata accepting the Fig; 2.5: Final DFA for Q .S(a)
followlng languages over the alphabet {O, 1}
(b) Numberof ·1'sos odd and number of O's is odd.
(a) Number of 1's Is even and number of O's Is
even. In solution of Q. 5(a), the state q3 stands for od~ number of
(b) Number of 1's Is odd and number of O's is O's should be declaredas final state.
odd. · W 0 1
Ans.:
(a) · Number of 1' sis even and number of O's is even.
0 CJo
At any instance of time, we will have following cases for
number of O's and number of 1's seen by the machine. ·
1
(c) Transition diagram (d) Transition table
Even Even CJo Fig. 2.5: Final DFA for for Q .S(b)
Even Odd Q. 6 Give the finite automation M accepting
Odd Even (a,b)*(baaa).
An input 0 in state CJo· "".'ill make numberof O's odd. The R.E. = (a, b)* ~baaa), represents strings ending in baaa.
The FA is given below
6 ~<lo• q2
a,b
An input I in state CJo, will make numberof l's odd.
6 (<lcJ, l) q1
An input Oin state q 1, will make numberof O's odd.
Fig. 2.6
.
An input I in state q 1, will make number of l's even. a. 7 Give appUcatl~ns of Finite Automata. · W
Ans.:
6 (qi, 1) <lo
.Appllcatlons of Finite Automata
An input O in state q2, will make number of O's even.
Finite automata are used for solving several common types of
6 (q2, 0) <lo computer algorithms.Some of them are :
easv-solut1011s
Scanned by CamScanner
Y · Theo'ry of Comp. .Sci. (MU-Sein.5-Comp.)
1
(i) Design of digital circuit • . carry as,. .o,.qi _ Previous carry as 1)
(q0- Previous
·. (ii) String matching
Ans.:
Fig. 2.9
Q. 9 . Design a MOORE . and MEALY machine to ·An input of I in q3 will make the previous four characters as ..
decrementa b1narynumber.
'1001': Oot of the four characters as ' 1001' only the last character
Ans-:
'I' is relevant to '100;.
One can decrement a binary ·by adding 11. .. 1 _(all I's is 2•~
<h to q0 on Input O :
c.omplement.of l) to the given . number. ·The. addition
. should start
An input of O in ·q3 will make the previous four characte~
from the least significant digit
· Meafy machine '1000'. Out of the four characters '1000' , nothing is relevant to
'100',
Flg.2.8
easv-solulions
Scanned by CamScanner
.·TCS-5
Fig. 2.13
·Ans.:
To convert e-NFA to NFA without e ·.
Step 1 : To remove e transition from q state to r state, we do
following
4z 4,,B (b,A
Cb qi, C , <Io, C
q4 qi; C. . (h,C .
Fig.2.14
Stepl: . Minimization
The two states 4 1 and 44 can be merged into a single state, say q 1. Since all 3 states in the NFA are final states, we can merge all
3 states
a b
: . NFA - without e is
~··
4r q,.c CJo; C
q, 41,C 'h,C
Qo = initial state
F = (q3, ~- q,}
,
. 'p/A
Fig. 2.12
easv-so1u11ons
Scanned by CamScanner
·• Theory of Comp. Sci. (MU-Sam. 5-Comp.)
TCS-6 :: · ,.
6 (X, a) => S
b,c
6 (X, b) => Y
6 (Y, a) => X
6_(Y, b) · => S
h OFA to accept all the binary atrlnga
Q. 15 Design e{O 1} that are beginning with .1 and .
over - ' I I I of 5
having Its decimal value mu t p e ,· _.._._
Ans.: .
.,;ned is maintained through the states Qo, q1, 'b,•
. rem...
, Runnmg
1
'h• <4· If the number start with 0, it is rejected1 . ·.
Fig. 2.16 0
. 6 = Transitions are :
6 (Qo, a) ::;> Ch 6 (q6, c) => 'l6
6 (Qo, b) => <4 · 6 (<IJ, a) =>42
_6 (<Jo. c) => 'l6 6 (<IJ, b) => 'h
6 (42, a) => Ch · 6 (<IJ, c) => 'h
6 (42, b) => 'h 6 (4s, a) :::::,· 4s
6 (42, c) => 'h 6 (4s, b) => <4
6 (<4, a) => % · 6 (4s, c) =>(b
Fig. 2.18
6 (<4, c) => % 6 (q 7, a) 7 q7
···o(<4, b) => <4 6 (q7, b) => q7 Remindercalculation for finding the next state
6 ('l6, a) => q7 ·15 (q7, c) => q6
6 ('l6, b) => q7
Scanned by CamScanner
TCS-7 ·
.Theory of Comp. Sci. (MU-Sem. 5-Comp.)
The required mealy machine is given below. If 'a' is followed by 'a' then the machine enters the failure
state <I+ · ·
The input is enteredfrom right to left.
A 'b' immediatelyafter 'a' talces the machine to the accepting
1.
{}
{ q1,qJ {}
Fig. 2.24
Ans.:
The transition graph of the given NFA is: State (loiS for the runningreminder as 0.
State q 1 is for the runningreminder as 1.
State q2 is for the runningreminder as 2.
Output 1 indicatesdivisibilityby 3
Output Oindicatethat the number is not divisible by 3.
State e- closure
4o { CJo}
qi { q1}
Ch {q1,Chl
b
Fig. 2.23 •
easv -solut1ons
Scanned by CamScanner
' . !'.
V Theory of Comp. Sci. (MU-Sem. ~-Comp.) TCS-8 . ,, .
Step 2 : Writing transitions for the state {q 1} A DFA without explicit f.ailurestate is given in Fig. 2 ·25 (a)
a a b
-+ CJo qi ' I cp
qi cp q2
%. q, q3
a b
easv-solut1ons
Scanned by CamScanner
• .Theory ·of Co~p. Sci. (MU-Sem . 5-Comp.-) TCS-9
q2 to q3 on input b :
Number to be divided: 101101. •
q3 is for preceding three characters as 'ab6'. . ·Binary number
0 0 - to be dMded by 3
to q3on input a or b :
The substring 'abb' has ~en seen by the machine and a new
l
1
'inputwill not change _this status. ·
Next Input Is 1 .
Remainder1 1 o. (
(c) All strings ending In ebb next Input 0
An input of ain q 3
will make the previous four characters as 0 (CJo) 0 00%3 0 (CJo)
'abba' . Out of the four characters as 'abba' only the last 0 (CJo) 1 01 % 3 1 (qi)
character 'a' is relevant to 'abb'.
1 (qi) 0 ·10%3 . 10 (42)
-q3 to ·<lo on input b :
An input of b in q3 will make the previous four characters . . 1 (qi) 1 11 %3 0 (CJo)
'abbb' : Ont of the four characters 'abbb', nothing is relevant 10 (q2) 0 100%3 1 (qi)
to 'abb'.
10 (q2) 101 %3 10 (4i)
a.-22 Design a ·DFA which can accept a binary number
divisible by 3. l t· \ 1
Binary Binary decimal Binary
Or
• Design of a divisiblllty - by - 3 - tester for a
binary number. Dec. 2005, Ma 2014, Ma 2017 0 l
Ans.: Clo ql
. A binary number ,is divisible by 3, if the ~mainder when.
qi q2 Clo
divided by 3 will work out to be zero. We must device a
mechanismfor finding the final remainder. 'Ii ql , q2
.We can calculate the running remainder based on previous · (b) State transition diagram (c) State transition table
remaind~rand the next input.. Fig. 2.30: DFA for Q. 22
The running remainder could be ; Q. 23 Design aDFA for a mod 5 tester for ternary lnpuL
0 associated state, CJo
1 associated state, q{ Ans.:
2 associated _state, q2 A ternary system has three alphabets
Starting wi~ the m~st significant bit, input is take·n one bit at l: -= {0,1,2}
.a time. Running remainder is calculated after every input. The
~ase of a ternary number is 3.
process of finding the running remainder is being explained with
the he_lp of an example.
easv-solutions
Scanned by CamScanner
1'Jrnieoryof Comp . Sci. (MU-Sem. 5-Comp .)
TCS-10
•
The running remainder~ould be :
(0)3 = O associated state, q0
(1)3 = '1 associated state, q1
(2)3 = 2 associated state, Cb
.
(10)3 = 3 associated state, <b
0
. (11)3 = 4 associated state, q4
i i
Ternary Decimal
Fig. 2.33(c)
0
block 2
(q~.-L_T
+
5 7
·t1
ransi on on
1
block 1 block 2
Fig. 2.32(b)
Cii easv-solut1ons
Scanned by CamScanner
.Theoryof Comp. ·Sci. (MU-Sem.5-COmp.) TCS-11
1
Step.3: .Finding · 2-equivalc11ce partitioning of states by ·a. 26 A langu~ge L Is· accepted by some NFA If and.
consideringtransition·on •o• arid transitionon •1'. only ff It Is acceptedby some _
DFA. ·
R · q5 r I ff -Tran<lon on O
OR
·(Qo q,
' It· I' I Cle
It· <·y q4) ( 1:17) ·(1:1,>
F.or ev~ry.NFA, there exists an equlvalentDFA..
t r·· •-Tran<lon on 1
bloolt10
f
block 1t
I Ana.:
blodt 12 · blodt 2
Proof ·
. On input 1, block 11 is succcs~orof q , q,:
1
Given theoremhas two parts :
bn input 1, block 10 is successorof <Jo•~-
1. · If Lis accepted by a DFA ~ . then Lis accepted by some
q1, q5 is distinguishablefrom <Jo,~-
NFAM 1•
. P2 = (<Jo, qJ (q,. qJ (ql. q.)'(q1)(%) 2. If Lis accepted by..;, NFA M1, then Lis accepted by some
Step 4 : · Fmding 3-equivalence partitioning of st.ates by . DFA~ .
. consideringtransition on O·and 1.
First part can be proved trivially. Dete_rminismis a case of
I. non-determinism.Thus.a DFA is also an NFA.
.- Transitionon.O Second part of the theoremis proved below :
Construct from M1 using subset generation algorithm as
.._.. Tranaltion on 1 · explained earlier. We can prove the theorem using induction on the
. length of ro.
...__ ___,
'+ Base case : Let ro = £ with I ro I ,;,, 0, where I ro I is length of ro.
t Starting state for both NFAand DFA are taken as%- When
Blocks can'not be divided further. ·w_= £. both DFA and NFA will be in CJo· Hence, the base case is
. :. P3 = P2 = (<Jo, (q 1; q5) (q 2, q.) (q7) (q3) w'?ich is final set
proved.
of blocks of equivalent classes.
Assumption : Let us assume that both NFA and DFA are
Steps: Constructionof minimum state DPA. equivalent for every string of length. n. We must show that _the
0 1 · machines M1 (NFA) and (DFA) are equivalent for strings of
length (n + I). Let (1) + 1 = w a, where ro0 is a string of length n and
<Jo, <Ji;) (q1• qs) (CJo.~
0 0
Scanned by CamScanner
.• ..
. •' ·
.
Informally describe the language it accepts.
r.
..
0 1
{p, q} {p}
q {r, s} {t}
r {p, r} {t}
s•
r
t•
(b) State diagram
Ans.:
Fig. 2.35 : Final DF A for Q. 27
Step 1 : . {p j is ttlcen as the first subset
Q. 28 •Construct a NFA that accepts · a set of all strtn91_
0-Successorof{p} = 6({p},O)={p,q)
o~er {a, b} ending in aba. Use this NFA to ·
1-Successorof{p} = 6({p) , l)={p} construct DFA accepting the same set of strings. ·
Step 2_: The new subsets {p, q} . is genei:ated. Successors of
· {p, 4} are calculated .
w
. . .. Ans.:
6 ({p, q}, 0) = 6 (p, 0) u 6 _(4, 0)
· = {p, q} u {r, s)
== { p, q, r, s}
, 6({p ,4},l) = 6(p,l)u6(q,l)={p}u{t}
Fig. 2.36 (a) : Non-deterministic finite automata
= {p,t}
Non-detenninism should be utilized to full extent while
Step 3 : Two new subsets {p, q, r, s) and {p, t) are generated.
designing an NFA. A string of length n, ending in aha can be
.Their successors are calculated.
recognized by the NFA given in Fig. 2.36(a) . Erst n-3 characteis
6 ({p, q, r, s}, O) 2 6(p, 0) u 6 (q, 0) u 6 (r, 0) u 6 (s, 0) can be absorbed by the state <Jo by making a guess. On guessing the
· = {p, q) ·u {r, s) u {p, r)u last three characters as aba, the machine can make a transition from
= {p,q,r,s} . <Jo to qr
Scanned by CamScanner
.,,,~'¥
, I
°TCS-13 .
of Comp. Sci. (MU-Sem.5 ·comp.)
• . . I
6 (('le,, q1q3 ), b) .. 6<<Jo, b) u 6 (q., b) u
(q3, b)
= ('le,} U {q1} Ut= {<Jo,q1}
I
f.
<4 - Running~m~nder is 4 = (11
s~stem system
1
a b
Fig. 2.37(b): Moore machine
~ {<Io} {cio,q.} {'lo}
. {<Io, qi} {'lo, qi} f'lo, ql) Q. 30 Design a mealy machine for a binary Input
sequence such that If the sequence ends _with 100
{<Jo, ql} {<Jo, qi' q3} {<Jo} the output' Is 1 otherwise output Is 0.
{'lo· q •• q3}. {'lo, qi} {<Jo, ql} Dec. 2006. Ma 2008 . Dec. 2008
'h 4>. 1
214
(b) State table
Fig. 2.38
casv-s01111ions
Scanned by CamScanner
'TCS-14
'1r111eory of Comp. Scl..(MU-Sem.5-Comp.) a '·
3. If the number of class~s L creates is finite, then Lis regular. · . If an opeiation on regular languages generates a regular
language then we say that the class of regular languages is closed
. ·In finite automata, each state can be thought of as creating a · under the above operation. S_ome of the important closure
class of strings. Two strings are said to be in the same class_if they properties for regular languages are given below.
a
both trace path from starting state 4, to some state qi (say).
1. Union 2. Difference
Number of strings is infinite. Intersection .
3. Concatenation ,4.
. Number o~,$tates in FA is finite.an 5. . · .Complementation 6. Kleene star
Many strings when applied to the FA will end up in the same 7. Transpose or reversal.
_state. Each state of FA can stand for a ciass of strings. .
1. · ·· Reg1,1lar Language Is Closed under Union
Q. 2 Show that Let M1 = (S,t,6 1,s0 ,F)and
. (1 + 00*1) + (1 + 00*1) (0 + 10*1)*(0 + 10*- M2 e (Q, :E, 62 , 4,, G) be two giv_en automata.
(0 + 10*1)* · ·. . . .· ,,.
To· prove the closure property; we must show that there is
Ans.: another machine M3 which accepts every string accepted by either
L.ILS . = (1 + 00* 1) + (1 +00*1)(0 + 10*1)*(0 + 10*1) M1 or M2 and no other string. The construction M3 is quite simple
_ c1 +00*1) [E+(0+ 10*1)*(0+ 10*1)1 as shown in Fig. 3.1.
= (l+OO*l)(O+IO*l}*
= .[(e + oo•) 1] (0; 10•1)* =0*l(0 + 10*1}*
= R.H.S. ----F
a. 3 Prove L = { (ab)"ak: n > k, k:.?: 0} Is not regular.
(@ @)
BEili1i
Ans.:
Step 1: Let us assume th~ Lis regular and Lis accepted·by .,
an FA with n states.
_Step 2 : ~t us choose a.string
D+ I D
ro = (ab) -a
I ro I = 2(n + 1) + n =3n + 2 n
. Let us write ro as xyz, with
-I y I > O and I xy I n.
.
UML'·.:
.
The string xy will contain a maximum of n symbols from (ab)o. Fig. 3._l : M3 ls construc_ted such the L(M,) L(M1) u
® easv - solut1ons
-{js
Scanned by CamScanner
·9 Theoryof Comp. Sci. (MU~Sem.·5-Comp.) TCS-15
----F
(@ @»
4. ·Regular Language is Closed under
Complementation
Let M:;;; {Q, 1:, 6, CJo, F) be the given automata...To prove the
closure property under compiementation,we must show that there
· is another machine M which accepts L(M) where
L(M) = L(M)
M3 :. M (Q, ~. o, <Jo, Q - F)
Fig.3.2 : M:, is constructed such that L(MJ = L(M 1) • L(Mz) 5. Regular Language Is Closed un·der Intersection
is ronstructed by adding e-move from every final state of If L1 and Lz are two regular languages, then
M1 to start state of Mi· · L1 ': Lz = ((LI r'I Lz)'}' = (L, U Li)'
Machine is given by : = 1:* - [(1:* - L1) u CE* - Lz)]
= (R, l:, 63, s0, G) where
Closeness under intersection follows directly from closeness
63 = 61 u u {e-move from every final state ofM 1 under union and complementation.
to start state of Mi}
6. Regular Languages are Closed un_der Difference
~hine recognizes L(M 1) • L(M2) by going non-
deterministicallyfromJhe final state of M 1 to start state of M2• · Let L 1 and are two regular languages. The difference
L1 is the set of strings that are in language L 1 but not in ·Lz.
3. Regular Language Is Closed under Kleene Star Constrnction of a composite automata for L(M1) - L(M2) is
Let M = (Q, :E, 6.- 'lo• F) be the given automata. We can explained in Chapter 2. Thus regular languages are close~ under
1
construct a non-detenninistic finite automata M2 such that difference. ·
l(Mi) = L(M,t. Toe· construction of Mi from M1 is shown in
Fig. 3.3.
easv-solutions
Scanned by CamScanner
_,.:...: .
. ')'Theory of Comp. Sci. (MU-Sem. 5-Comp.) -. TCS-.fa"-.' ',
. :.
7. Regular Languages are Closed under Reversal (Ill) L = {co e (a, b)* _I {number of a'a In m)_moct ::,·:
3110} . .
Reversal of a language L is obtained by rev~~ing every string
. (Iv) . L = {a"bm I n >= 4, m<= 3} . ' ,:
in L Reversal of a language L is represented by LR_ Mct 2006. Dec. 2007. M,1 2008
For example,
Ana.:
'.
if L = {aab,abb,aaa}; thenLR=:={baa,bba,aaa} (i) The set of all strings over (0, 1} without length two.
Let M 1 = (Q, :E, o, <Jo, F) be the given automata. To prove the e~·co+ 1) +·co+ o (o + t)_co+o <o+ o•
closure property under reversal, we ~ust show that there is another
(ii) L = (a"bm I (n + m) is even}
machine Mi whi~h accepts L(M/.
((aa)*ab + bb) (bb)*
or L(M 2) = L(M/
(iii) {~ (~ e (a, b)* I (number of a's in oo) mod 3 =0}
Mi can be constructed from M 1 by:
(b + ab*ab*a) •
· 1. · By reversing every transition in Mj.
(iv) L = (a"bm In>= ·4, m <= 3}
2. Start state ofM 1 is made the only final state.
. 3. A new start state s0 is added with t>move to every final state
Q. 7 Prove L = { (ab)"ak I n > k, k >= O} la not
.. . w :.
•• •
. ofM 1•
lyl > 0
· and I xy I n
=
The string xy will contain a maximum of n.symbols from (ab)". ·
· Fig. 3.5: NFA to DFA a. 8 Construct a NFA for the RE (01* + 1) and convert It ,
easv-solutions
Scanned by CamScanner
TCS-17 ,
• Th~ry of Comp. Sci. (MU-Sem. 5-Comp.)
' .'
I • : , 01* , ,, Q. 10 S~e the pumping lemma for regularlanguage.
. ~- ~ !' Ana.:
~~-- -~- - ~ Pumping lemma for regularlanguage
. 1
. Pumping lemma gives a necessary condition. for. an input
l' string to belong to a regular set.
= = Pu~ping ·lemm~ does. not give sufficient condition for a
language to be regular.
Pumping lemma should not be used to establish that a giv,~
. languageis regular. .
Pumping lemma should be used td establish th~ a given'
= language is not regular.
The pumping' lemma uses the pigeonhole principle which
states._that if n pigeons are placed into less than n holes, some holes
have to have· more than one pigeon in it · Similarly, a string· of .
Fig. 3.6(a): RE to NFA .
length~ n when recognized by a FA with n states will see some
NFAtoDFA states repeating.
Definitionof Pumping Lemma
0,1 Let L be. a regular language and M e .(Q, l:, 6, Clo, F) be a
finite automata with n:states. Language L is accepted by m. Let
w E L and 1.wl n, then w can be written as xyz, where
(i) I y I> 0
. q1 Is repeating
Input A •
I a/2 c~b c b
r-qo-..q1-+q2-+q3-+q,-+q2-+q3-+q4
States ·
Scanned by CamScanner
' I
11 . { {<Jo} ifk=O }
Qk= {Qk_1u{6(q,a)}lqe~_ 1andaeL} ifk ·>O
We can go on computing the Qk for each k 0 until one of
the two cases arise :
I. Qk contains a final state.
The language is not empty.
2. Qk=Qk-1 .
.. .
The language is empty as the final states are not reachable
from4o.
=
Decisionalgorithmfor finiteness/ Infiniteness :
The set of strings accepted by a finite automata M with n
states is finite if and only if the finite automata accepts only strings ,
Fig. 3.lO(a) : RE to NFA of length less than n. ·
NFAtoDFA The set of strings accepted by a finite automata M with n
(I) such that
states is infinite if and only if it accepts some string
nSlrol<2n. · ·
From the pumping lemma we know :
I. If ?> with length of ro ;:: n is accepted by M then ro can be.
wntten as xyz. · . • .
1
2. For every, i xy z will be accepted by M.
We can always design an · algorithm to generate all strings_·
over t with length between n and 2n. ,_:
F11, 3,lO(b) : NFA to DFA
easv-s011111011s
Scanned by CamScanner
. ·.• Theoryof Comp. Sci. (MU~Sem. 5-~omp;) rcs.:.19
• If ~y of these stri~gs is aC?Cpted by M then L(M) is infinite Now, ro = anblb = • ,· n--s-rb "b
. else L(M) is finite. · • ' . .
77-r ·
X · Y Z
Let us write ro as xyz, with
lyl > 0
.. . . I -
and I xy I - :S; n
StepJ: Let us check whether xy z for i = 2 belongs to L.
Since, I xy I S n, y must be of the form a' I r > 0
xy z =· a1a2zan-s-rbb an=· an+rbb
2
. an
Since, I xy I Sn, x must be of the form a'.
Sin~ r > 0, an + 'bbaD is not of the form (OO)R as the
Now, a"bn can be written as :
strings starts with (n + r) a's but_ends in '(n) a's.
·. a'a'an-•-' ban
Therefore, xy2z i L. Hence b~ contradiction,we can
say that the given language is not regular.· Step 3 : Let us check whether xyiz for i = 0 belongs to L.
xy°z "" a• (a')0 an-s-rb a"'
. Q.14 Using pumping lemma tor regular sets. Prove that
,;, an-rb an
the language L_= { oxo I roe {O, 1}*} Is not regular. ·
Since, r > 0 the string a"'-'ba"'EL.
Dec. 2006. Dec. 2010
Hence by contradiction we can say that the given
Ana.: languageis not regular.
Step_l: . Let us assume that the given language is regular and
Lis accepted by a FA with an states. Q. 16 Write short note on application areas of R.E.
easv-solllllOIIS
...__ ,;
Scanned by CamScanner
'1t.Theoryof Com~. Sci. (MU-Sem. 5-~o~p.) TCS-20.- ·
It will list those lines · from the file e~p.txt which has the
pattern "president". ·The pattern in grep. command can be b
specified using regular expression. Fig. 3.11
· 6. • matches zero or more occurrences of previous character. Q. 18 Construct an NFA with epsilon transition for the
7. • matches a single character.. following RE. (00 + 11)* (10·)* WWW
8. [" pqr] Matches a single character which is not a p, q or r. Ans.:
(00+11t (10)*
9. "pat Matches pattern pat at the beginning of a line .\...------1 ~
Example f
I
I
. ' I
(a) The regular expression [aA) g [ar] [ar] \Y&l stands for either
"Agarwal" or 'agrawal".
(b) stands for zero or more occurrences of. g.
(c) $grep "A · • thakur'' emp.txt will look for a pattern starti~g
with A. and ending with thakur.in the file emp.txt.
2. Lexical Analysis
l E \
Lexical analysis is an impo_rtant phase of a compiler. The '
lexical analyser scans the source program-and converts it into a
steam of tokens. A token is a string of consecutive symbol defining
an entity.
For example a C statement x = Y. + z has the following tokens :
X An identifier
= - Assignment operator
y = An identifier
+ Arithmetic operator +
z An identifier
Fig. 3.12
Keywords, identifiers and operators are common examples of
tokens. 19 Convert (O + e) (1 0)• (e + 1) Into NFA with
Q.
Toe UNIX utility lex can be used for writing of a lexical e-moves and hence obtain a DFA. ••NW4•il
Ans.:
analysis program: Input to lex is a set of regular expressions for
each type of token and output of lex is a C program for lexical Step I: RE fo NFA for (0 + e) (10)* (e + 1)
· analysis.
easv-so11111011s
Scanned by CamScanner
TCS-21
• Theory of Comp. Sci. (MU-Sem. 5-Comp.)
I . lyl > 0
and I xy I S n
We can assume that y = am form> 0.
1
Step 3 : Length of xy z can be written given below :
1
I ~y z I = I xyz I+ I_ t I
I= p + (i - 1) m
as · I y I = I am I = m
I
Let us check whether P (i - 1) m is a prime for every i.
For i = p + 1; p + (i 1) m = P + m = P (1 + m).
0
R.E. form state diagram
Step 1 : Remoyingloop between 'h and 'h we get
Fig. 3.14
Ans.:
Step 1: Let us assume that the given language is regular and
Lis acceptedby a FA with n states. .
Step 2:
--io
Removing the ·main loop, we get
+ 1(1 + 01)-00
easv-solulions ·'
Scanned by CamScanner
I •
TTY
X y . z. .
1
Step 3 : Let us check wheth~r·xy z f~r i = 2 beloµgs to L. Q. 24 • Convert (0 + t:) (10)*(e + 1) Into NFA with £-movet .
xy2z = ~s(ar)2ao-s-rbo and obtain DFA.
= a•a2r~n-so'bD Ans.:
Step 1 : NFA for the given expression :
0 0
Since r > 0, number of a' s in a +' b is greater than number
2
ofb's. Therefore, xy z 11: L. Hence by contradictionwe can say that
the given language is not regular.
casv -sotu11ons
Scanned by CamScanner
.,,.Theoryof Comp. Sci. (M~-Sem~5-Comp,) TCS-23
-
a. 1
Chapter 4 : Regular Grammar (RG}
a,b
= =
00
Fig. 4.l(a)
Fig. 4.l(b)
easv - solulions
Scanned by CamScanner
Theo~ of Comp. Sci. (MU-Sam. 5-Comp.)
Fig. 4.3(a)
Ana.:
Re-writing the production we get
S bB Fig. 4.4(b)
B ~lb
B aB
C a
easv-solut1011s
Scanned by CamScanner
.Theoryof Comp. Sci. (MU-Sem. 5-Comp~) 1
, • TCS-25
. .
Step3: States {S}, {A),
'
{B }, {C,F}, nnd {A, F} nre re~amed
. Step2: Interchanging the start state with the final state and
as <Jo, q,, (h, (b,q4 nnd a dead state is introducedto reversingdirection of transitions,we get
handle I) - transitions. The resulting DFA is shown
in Fig. 4.4(c) :
Fig. 4.S(b)
Ans.:
Step 1 : Transition system for the ~ven right linear grammar
· is as shown in Fig. 4.S(a).
0
Q. 1 Write an unambiguous CFG for arithmetic . T --+ T * F I T / F I F [* and / has higher priority over
expressionswith operators: .+, •• . /, "• unary
+ with L R associativity]
minus and operand a, b, c, d, e, f.Alao, If should
be · possible . to generate brackets with . your · F--+ F" o I a [" has higher priority over * and / with
grammar. Derive (a + b) " d / e + (- f) from your L R associativity]
·
grammar. 11J4&4111~1
[unary- has the highest priority]
Ana.:
An unambiguousgrammar is given below. H a I b I c Id I e I f I (E) [to ~andle b~ckets and identifiers]
Derivationtree for (a+ b) (\ d I e + (- f)
E.--+ E +TI T [+ has lowest priority ~ith L R associativity]
·,
casv-solutions
Scanned by CamScanner
·.. Theory of Comp. Sci. (MU-Sem. 5-Co~p.)
. E
.
Where a e T.is a terminal and ex is a string of zero _orm~~ -·.
·' , .
-
' .·
.
variables.
The language L .(G) should be without£ ..
I E
+ T Right hand side of each production· should start with a :
T I terminal followed by a string of non-terminals of length zero or . ,
. more.
;t\
T I .F '
G Q. 4 Prove that the language L =- {aP I P Is a Prime} la
not context free language. Ma 2006. Ma 201:,
-; ·1 ·I·
F G H Ana.:
·/T\_ I
F"G .H
/T\
(E)
.l .
2.
Let us assume that Lis a CPL.
Let n be the natural number for L, as per the pumping lemma.
I. I · I
G H e
I
T
3. _Let p be a prime number greater than n. Then z.= ~e L. We
can write z = uvxyz.
I I
H d
I
F
4.
0
By ~umpinglemma uv xy°z = uxz e ·L. Therefore.
I uxz I is a prime·number.
(
/T\
E .)
-- 1
G.
Let us assume that I uxz I= q.
Now, let us consider a string uv<lxy'lz,
IT\ .
E +.T
·A H
The length of uv'Sc.y~is given by:
I -I
T F
I I uvq xyq z I = q + q (I v I+ I y I), which is not a-prime with
q is a factor.
Thus. uv'Sc.y~-~ L This is a contradiction.
I I
F- G
_Therefore,L ~snot a context free language.
I Q. 5 Given a CFG G, find G' In CNF generating L (G) - e
I-
G H s ASB I E Aas I a B SbS I A I bb .
I I
H b Ana.:
Ma 2006. Ma 2009. Ma 201 O. Dec. 2011
. 1·
. Step 1 : Simplificationof grammar
a_
Symbol S is millable.
Fig. 5.1 : Derivationtree for (a + b) Ad/ e + (- f)
• by .
After removing£-productions• the set of producti·ODS .lS given
a. 2 Convert the following CFG to GNF : S ASB IAB
s aSa I bSb I c . A AaSIAala
Ans.: B SbS I Sb I bS I b I A I bb
The grammar can be brought to GNP through simple U~it p~~ction B A is remo~ed. the resultin set of
snbstitutionsC1 a and c;, b. productions1s given by g_
S aSC1 IbSCi,I C S ASBIAB
C1 a A AaSIAata
B SbS.ISblbSlblAaSIAalalbb
Step 2 : Every symbol . .
Q; 3 Write short note on GNF. A in a, m Productions of the fonn
Scanned by CamScanner
'1 Theory of Comp. Sci. (MU-~em. 5-C~mp.)
B
These production can easily be written from the FA for the
above language.The FA is shown in Fig. Ex. 5.2.33 .
B sc;, I c;,s I b s scb I c;,s I b
b
Fig. 5.2
b
Convert the following grammar Into GNF
Set of variables V = {S, X}
Q. 6
1X1 Set of terminalsT = { a, b}
Ma 2006, Ma 2012 Start symbol = S ·
Ans.: (iii) The set of all strings over alphabet {a, b} with exactly twice
Simplificationof grammar as many a's as b's .
y is removed, the equivalent set of
The unit production x The CFO G = (V. T, P, S)
productionsis given by : · · Where V = {S}
S XYII0 T = {a,b}
X OOXllXl ·
P = {S aSaSbS I aSbSaS I bSaSaS I e}
. Y lXI
S = . Start symbol
The symbol Xis non-generating.
The set of productions after elimination of X is given by : (iv) Language having number of a's greater than number ofb's.
The set of productionsfor the grammarare given by :
S 0, itis in GNF
p = {
Q.7 Find CFG for generating
S SaS I aSS I SSa I a I aX I Xa
(I) String containing alternate sequence of O's
X aBlbA
and 1'e, l: = {O, 1}
The string .containing no consecutive 'b's A aXlbAAla
(II)
but 'a's can be consecutive. B- bX I aBB I b
(Ill) The set of all string over alphabet {a, b} with l
exactlytwice as many a's as b's. · '
Tho variable X generates a string having equai'number of a's
(Iv) Language having number of a's greaterthan and b's. Group of excess a's over b's are generated by
number of b's. S-productions.·
Dec. 2006. Mc1 2009, Dec. 2009
•· '
easv-solutions : •;.. j • ; I
Scanned by CamScanner
1' Theory of Comp. Sci. (MU-Sem.5-Comp.) TCS-2e·
a.....
Q.10 Pro~e that L = {a'b'c' 112: 1} Is not a·cFL...,._
Where .. .
Scanned by CamScanner
-
• Theoryof Comp. Sci. (MU-Sem. 5-Comp.) , TCS-29
Q. 12 Given a CFG G, find G' In CNF g~neratlng L (G). e Q.13 -Let G = (V, T, P, S) be the CFG having following
· set of productions. Derive the string .~aabbaa"
using leftmost . derivation . and rightmost
· derivation.
Ma
lbb
2006 . Ma 2009. M.1 201 o. Dec . 2011
W ·
Ana.:
Ans.:
(I) Leftmost .derivation :
Step 1 : - Simplification of grammar
Symbol S is millable. Leftmost derivation of aabba:,.is being shown with the heip of
the parse tree.
After_removing £-productions, the set of productions is given by
1. S. 2. S
S ASBIAB
AaSIAala
II\
a A S
II\
a_A S
B
Unit production B
SbS I Sb I bS I b I A I bb
A is removed, the resulting set of
II\
Sb A _
_productionsis given by
S ASBIAB
3. ·s 4. s 5. s
AaSIAala
B SbS I Sb I bS I b I AaS I Aa I a I bb II\a A
II\S
II~ a A S a A S
Step 2 : Every symbol in a, in productions of the fonn
where la.I 2 should be a variable.
A -:+a. · ·
· II\ -_ II\
S b- A
II\ I. S b A S b A a
This can be do~e by adding hvo productions : •
- c. a a
I 1-/\ . _I /\ a b a a b .a
and Ci, b
The set of productions afterthe above changes is Fig. 5.4(a)
/1\I
Step 3 : f'"indingan equivalent CNF
ll\
a
-
3. s 4. S 5. s
II"'
a A ll"'s S
II~ .
a A
· Fig. S.4(b)
B AC0 I a I c;,c;, B AC 0 I a I c;,c;,
S aAS aAa aSbAa aSbbaa aabbaa
Scanned by CamScanner
·'
• ~eoryof Comp. Sci. (MU-Sem. 5-Comp.)
Q. 14 Let G be the grammar s aB I bA A aI as I s
· bAA B b I bS I aBBFlnci
(I) Left moat derivation A
a B
(II) Right moat derivation
(Ill) Panae Tree
a B · B
.,.
(Iv) la the grammarunamblguoua?
For given atrlnga (A) aaabbabbba(B) bbaaabbaba I /f\
b a B B
t
(C)00110101 ·
Ana.:
I
Dec. 2009. Dec . 2012 . Mn 2013
.I I b b
(A) For atrlng uaaal>b:&bbba"
(b)
It will be worthwhile .to draw the parse tree an~ from the Fig. 5.5
parse tree, one can easily write left most and right most derivation.
(B) ·For string •bbaaabbaba•
(i) Left most derivation :
(i) Leftmost derivation
S aaBB aaaBBB aaabBB
S bA bbAA bbaA bbaaS
aaabbB aaabbaBB aaabbabB aaabbabbS
bbaaaB bbaaabs bbaaabbA
aaabbabbbA aaabbabbba
bbaaabbas bbaaabbabA bbaaabbaba
(tl) Right most derivation :
(ii) Rightmost derivation
S bA bbAA bbAaS bb~
bbAaabS bbAaabbA bbAaabbaS .
. /l\ /l\
· B
b
/\""- A A
.
aBBaBB
'J·/ I I\ a/aAs
b b b b S
A
I\ a B
I
a A
bAs
a
bAA
Fig. 5.5
(iv) The grammar is ambiguous as we can draw two parse trees aAs .
foraababb: bAA :
. .I
a
Fig. 5.S(c)
c:isv SOIIIIIOIIS
Scanned by CamScanner
• Theoryof Comp. Sci. (MU-Sem.5-Comp.) TCS-31
(ii) Rightmostderivation Step J : Convertto CFO
s OOBB OOBlS OOBlOB'
Ginn production EqglyalentProchictlon' ··
OOB101S OOB1010B 001310101 . Jo..fil!l
· 001110101
-+
I
(iii) Parsetree
A3 aA 1A4 1a --+
.
·U
. Substltutl"9. >
u
Substituting A 3 >
-+
A1 A2!,-3
Fig. S.S(d)_ u
Substituting A2 aA1A4 A1~~ I aA 1~~
Q.15 Obtain a grammar to generate the ~uage
I aA1A4A1A:JA:3I~ I b~
. L = {0° 12n In.?: O)}. HZ4•1111
Ans.: The final set of productionsis :
A1 aA 1A4A1A3A3 I aA 1A3A3 I aA 1A4A3A3 I aA 3~ I b~
Productio~sfor the required language are as follows.
aA 1A 4A 1~ I aA 1A 3 I aA1A 4A 3 I aA3 I b
P ::. 1£}
CFG for the above language is ({S }, {0, I}, P, S)
Step 2: The variables S, A, B and Cb are renamed as A1, A2, l· A2 A,. A2 A1]
A, A,] [ lA,
~ - and . A res~tively. The resulting set of
4
The resulting set of productionsis :
productions is given below.
A1 A 2 A,. I 1
A1 A,. A,. A1 I 1 A1 11
A,_ ~A 1~1~~1b Step J : Removing left recursion:
The Ai - production contains left recursion. Left
aA1A 4 Ia
·recursioncan be removed.through
A4 b
A,. A1 B2 1l B2
easv-solutions
Scanned by CamScanner
·1' Theory of ~omp. Sci. (MU-Sem. 5-Comp.) . . .....
B2 A1 B2 I A2 A1 (Iv) The grammar is ambiguous as we can draw two parse.~
The resulting set of productions is : for aababb:
A1 '½ A2 I 1 . s
A2 1 A1B 2 I 1 B2 I l A1 I 1
B2 -+A2 Al B2 I Al A1
s I\
a 8 ,
Step 4 : A2 - productions are in GNP.
a
I\ 8
, A1 and B2 productions can be converted to GNF witlt a B B
the help of A2 - productions.
/\ I
A2
A1
l A 1B 2 I l B2 I l A1 I l
l A1B 2 I l B2 I 1 A1 A2 I 1 A2 11
a B
I /l\
b a
8
B B
b Ab
B2 1 Al B2 A1 B2 l B2 Al B2 I 1 Al A1 B2
I 1 Ai' B2 I 1 A 1 B2 A1 I 1 B2 A1 I 1 A1 A1 I 1 A1 I I -· b b
a B.
I
a I as I b
Q.18 Let G be the grammar S-+ aB I bAA
b I bS I aBB Find (a) (b)
bAA B
Fig. 5.6
(I) Left most derivation
(ii) Right most derivation · (B) For string "bbaaabbabau
(Iii) Parse Tree
(i) Leftmost derivation
(Iv) Is the gram1T1arunambiguous1 ·
S bA bbaA bbaaS
For given strings (A) aaabbabbba (B) bbaaabbaba
(C) 00110101 Dec . 2009. Dec . 2012 , Ma 2013 bbaaaB bbaaabs bbaaabbA
Ans.: bbaaabbas bbaaabbabA bbaaabbaba
;·
a
(C) For the string 00110101
(i) Leftmost derivation
Scanned by CamScanner
• Theory of Comp. S~i. (MU-Sem. ~-Comp.) TCS-33
00110 B 001 lOIS 0011010B Q, 20 .Convert the following Grammar to . CNF form :
le laA I e
A aA I bA I a I b
B bB I aA I b I a
aA I bA I a I b
B bB I aA I b I a
Fig. S.6(d)
j~S
I
b '~
I ct s e . s
I · aI
b a
I 0. 21 Obtain leftmost . derivation, rightmost derivation
and derivation tree for the string "cccbaccba".
Fig. 5.7 The grammar la S SS a I SSb I c
easv - solutions
Scanned by CamScanner
' '
----~
Derivation S V2 V4• V 4 SA
. . ''· tree :
. s
s s a
B Vs-+ v, S
/1\ Ssb
Sga
/1\ V1 V1 -+b
C
I II\
s b
S_
11 -
C C
Converting to GNF :
II
C C
Step 1 : Substituting symbols. we get,
· Sccbaccba
S aSASBXiS I aSBXiS I I
cccbaccba
b X 1 Xi S
a. 22 Convert following grammar to CNF and GNF. . [substituting for S]
S I a I bb
Ia
. Q. 23 Consider the following grammar G =' (V, T P, S),
V =(S, X), T = {O, 1} and productions Pare
Ans.: · I0X1101S1
. lal _bb _.-
Re-writing the grammar, we get, A grammar is said to be ambiguous grammar if the language
generated by the grammar contains some strings that has 2 parse
s trees.
Ex. : Let us consider the given grammar
B SV 1S I V 1V,
S 0 I OXI I 01S1
X OXXl 11S
.
where, S is the start symbol.
. . each production in its equivalent CNF fonn,
Now, re-wntmg
A string 010011 is generated by the given grammar .
we get, ·•
\
CNF forms The grammar generat~s the string OJOOl l in 2 different wa~.'
Productions
The 2 deviations are shown in Fig. l(a)-Q . 61 and Fig. l(b)·,P· 6l,_
As the same string h~ 2 different parse trees. The given ·
is ambiguous grammar. ··
easv-s011111011s
Scanned by CamScanner
• Theoryof Comp. Sci. (MU-Sem.·s-Comp.) .TCS-35
s s We can proceed for renamingof variables,VariablesSand A
0'+~s , ,
0 X 1
are renamedas A1 and A,. respectively. The set of productionsafter ·
renaming becomes:
0 .x 1
1
AS A1 A2A,.
.
i0 Productionsafter·renaming .
0 X 1
i0
(a) (b) . Step 2 : Every production·of the form Ai A1a. with i > j
Fig. S.9 must be modifiedto make i Sj.
Q.24 Consider the followlng grammar G = (V,· T, P, S), A2 - production A1 A1 shouldbe modified.
V = {S, X}, T = {a, b} and productions Pare U
S aSb I aX We must substitute_A,.A,. I a (or the fim A 1• yve should not
X Xa I Sa I a . , touch the second A1of A1A 1.
Convert this grammar In Grelbach Normal Form . [ A,. A,.A,.A1]
(GNF). A1AiJ-W a A1
A,. A 1 C 1 A3 I a A3 I A 1 C0 I a A2 aA 1B 2 I aB2 I aA 1 I b
A3 C1 A 3 I A,. B2 A 2A 1B 2 I A 2A 1
Re-writing productions in GNP from Step 4 : A2 - productionsare in GNF.
A1 a A 1 c;, I a A,. A 1 and B2 productions can be convertedto GNF with the help
A,_ a A 1 Ci, C 1 I a A,. C1 A3 I a A 3 I a A1Cb C1 of Ai-productions.
I a A,. C1 I a . A2 aA 1B 2 I bB2 I aA 1 I b .. . in GNF
a~ I a A 1 c;, C1 A 3 I a A,. C1 A3 I a A 3 A1 A2A2
I a A1 c;, C
1
I a A,. C1 I a .U Substitute aA 1B2 I bB2 I aA 1 I b for first A 2
C1 a A1 aA 1B:zA1 I bB1A1 I aA1A1 I hA:
c;, b A1 a .. . in GNF
0. 25 Construct 8 grammar In GNF which Is equivalent Now, for B 2 - Production
to the grammar S AA I a, SS I b. · B2 A2A1B2
Ma 2008. Dec. 2011. Dec . 2016
U SubstituteaA1B2 I bB2 I aA1I b for the first A2
Ana.:
B2 . aA 1B2 A1B2 I bB2 A 1B1 I aA1A1B 2 I bA1B2
Step 1: Grammaris already in a simple form without :
B2 A2A1
1. £--productions. 2. Unit productions.
ij Substitut~a A1 B2 I b B2 I a A1 I b for the first A,.
3. Useless symbol.
easv -solulions
Scanned by CamScanner
') Theory of Comp. Sci. (MU-Sem. 5-Comp.)
l CI IC t Se a=:::>i C tiCta ea
=::)ICtlbtaea ===>ibtlbtaea
Q. 1 Dlsilngulsh between NPDA and DPDA. 1t)§W-4•1t~i D Every_ context free language can not be recognized by a.
Ans.: PDA but 1t can be recognized by NPDA. The class of languagea
DPDA can accept'l' · be · Fl,.
' }.
Distinguish betw~n NPDA end DPDA . - tes tn tween a regular language and C
paltndromecan be · ._,1 bY
accep~ by NPDA but it can not be accepwu
A NPDA provides non-determinismto PDA. a~M .
In a DPDA there is only one move in every situation. Where
· case of NPDA there could be multiple moves under a
as, m
situation. DPDA is less powerful than NPDA.
easv-solUIIOIIS
Scanned by CamScanner
• Theory of Comp. Sci. (MU-Sem. 5-Comp.) TCS-37
a. 2 Design a PDA to accept (bdb)" c". .. . The character immediately before the middle position will
Ans.: identical to the character immediately afterthe middle position.
To solve this problem, we can take a stack symbol x. For Algorithm:
every 'bdb', one x will be pushed on top of the ·stack. After. reading There is noJ way of finding the middle position by a PDA;,
, (bdb)", the stack should contain n number of x's. These·x's will be therefore the middle position is fixed non-deterministically.
matched with e's. The transitions for the PDA accepting through an 1. First n characters are pushed onto the stack. n is non-
empty stack are given in Fig. 6.1. .
deterministic.
b,x/x d,x/x /
2. The n characters on the stack are matched with the last n
b,Zrflo d,zrfzo
characters of the input string.
3. n is decided non-deterministically. Every character out of first
n characters, · whose previous character is sanie as itself
b,x/xx should be considered for two cases :
c,xlf.
b,zrfxz0 (a) It is first character of the second half.
c,x/r. - Pop the current stack symbol using the transitions :
. r.,z,ff. 6(4o, a, a) (q 1, E)
Fig. 6.1 6(4o, b, b) (~,• E}
A cycle through q0 q1 'h 'lo traces a group _of bdb. L.. Must be identical
The PDA M = {Q, :E, r, 6, (lo, Zo• q,} (b) It belongs to first half. ·
The transition function 6 is given by, 4. n is decided non-deterministically. Every character out of fmt
n characters, whose previous character is not same as itself
6((lo, b, 7-o) = .(q,,zo) should be pushed onto the stack.
6((lo,b, x) = (q,, x) - Push the current symbol using the transitions :
6(q1, d, 7-o) = ((h, zo) 6(%, a, b)
6(q1, cl, x) = (q2, x) 6(%, b, a) . (%, ba)
6((h, b, zo) = ((lo, xzo)
The transition table for the PDA is given below :
6(<h, b, x) = ((lo, xx) 6(%, a, zo) {(4o, azo)}
6(4>, C, X) = ((b, E) 6(%, b, zo) {(%, bzo)}
6(q3, c, x) = (q3, E) 6(4o, a, a) {(4o, aa)(q,, e:)}
o{(b, E, zo) = (q3, E) Accept through empty stack. 6(4o, a, b) l(%, ab)}
Q. 3 Design a PDA for detection of -even palindrome 6('lo, b, a) {('lo, ba)}
over {a, b}. 6(%, b, b) {(%, bb),(q 1,e:)}
Dec . 2005, Ma 2006, Ma 2007, Ma 2016 6(q1, a, a) 7 {(q,,E)}
Ans.: 6(ql, b, b) {(q,, e:)}
. . . R
An even palindrome wlll be of the form ww 6(ql' e:, zo) {(qi' E)} [Accept through an empty stack]
w
i A
_w w w
i A the set of states Q = l'lo,q,l
Centre centre Centre the set of input symbols L = {a, b}
If the length of w is n then a palindrome of even length is : the set of stack symbols r == {a, b, 7.o}
First n characters are ~qual to the· last n characters in the Starting state == ·4o
reverseorder, Initial stack symbol == 7.o
easv-solutions
Scanned by CamScanner
. TCS-38
Theoryof Comp. Sci. (Mu~sem.5-Comp.)
0. 4 Conatnlct a PDA equlvalent to the followlng CFG. (q, 0, 0)
6(q, 0, 0) = (q, e)
Teat i, 010 4
la In the language (q, e, e)
Mn 2006.M;i 2011 . M;i 2012 4
Thus the string 010 is accepted by Musing an empty Stae.k.
Ana.:
:. ow• e L
The equivalent PDA, M is given by
Q. 6 construct a PDA accepting { anbman I m, n 2:: l} by
M = ({q},{0, 1},{0, 1, S, 8 }, 6, q, S, ~). null store.
where 6 is given by Dec . 2006. Dec . 2010. Ma 2012 . Ma 2013
6(q, t, _B) {(q, OS), (q, IS), (q, 0)} . in the given grammar Algorithm:
6(q, 0, 0) {(q, E)} t. The sequence of a's should be pushed onto the stack in state
(q, 10000, BB) The PDA accepting through empty stack is given by
=
. 6(q, E, B) (q, 15) M = ({CJo, qi' 42 }, {a, b}, {a, Zol. 6, 'lo• Zo, I!>)
6(q, e, B) =(q, 0) The transitions for the PDA accepting through an empty stack :
are given in Fig. 6.2. '
casv-su1u11011s
Scanned by CamScanner
. • ~Theoryof Com_p. Sci. (MU-Sem. 5-Comp.) 1
TCS--39
PDA accepts through the final state q.. (b) ,It is a middle character - go for matching of second half
with the first half. .
The PDA M :a: {Q, l:, r , 6,'lo, Zo, F}
6{'lo, a, E)
Where, ·
6(<Jo, b, £) (q 1, E)
Q = (4), qi. 'h• 'h• q4}
The status of the stack and the state of the machine is shown
l: = {a, b, c, d} in the Fig, ·6.3. Input applied is ababa
r = {x. Zol
left current input is taken as the.middle character
The transition function 6 is given by,
Right child current input is not a middle character.
6(4i, a, Zo) = . (q1, Zo)
6(4i, a, x) = (q1, x)
. 6{q1, b, Zo). = (<Jo, X Zo)
6{q1, b, x) = (<Jo, xx)
Alter first
Input 'a'
6(4i, c, x) :;: (qz, x)
6((b,d , x) = (q3, £)
6(43, C, X) = ((h, x) . fi)
Alter second ·Zo q,
.,,,,,:
6((Jz, £, Zo) = (q., Zo) Input b
4i is initial state,
Zo is initial stack symbol.
Alter third
Set of final stat.esF :a:_{<u) Input a
laU 1
2. wbw1
ab b ba, a b 1j b 1a b a1, ab a . lai
D 1 R R
w w w w w .w FJg. 6.3 : Processingor string by the PDA. Stringis taken as
"ababa"
(ii easv - solUIIOIIS . '
Scanned by CamScanner
1
Tho tronAltlonlnhlo for tho J>l)A IN slvon below,, Step 5: Add productionsfor 6(CJo, e, :r.o) = {(qi, e)}
1, ll), (q0, n) l
6(l]o, n, ll) I:') ( (q z
[CJo oq,J ,e
L c • ltullcnloH thnl
lrroHpcctlvcof tho current
Step 6: Ad~ productionfor 6(q1, 1, x) {(q1, e)}
Hinck Nymhol, perform lho t1·1111Hlllo11.
6(tJo, b, ll) ::) ((q,, n), (llo• b)) (q, X 1
8(q1, n, n) =-> l(q1, n)) Step 7: Add productionsfor6Cq1, O, zo) {(CJo, zo))
6(q1, b, b) => I(q 1, n) l
6(q1, n, :t'O) => {(q 1, I!) I [Acceptthmugh 1111 omply Hinck]
[q 1
Z
°q 0] 0 [go
Zo
CJo1
'/; Zo
Whore, n,o Rot ofNt111csQ .. (q0, q1I [q 1 0 [qo q1l
.. In, bI
TI10 set Input nlphnbot I:
Design a PDA for accepting a language
Q.9
11,c set of Rinck Rymb.oh1 r · "' I u, b, ZoI
L = { WcWT I w E {a, b}.} ·.
Sto11lngRtnto .. 'lo
Ma 2008 . Ma 201 0. Ma 2011
lnltlnl ~tnck Rymbol "' :t'O
Ans.: ..
Q, 8 Glvo tho CFG generating tho language accepted WT stands for reverse of W. A string of the fonn WcWT is an
by tho followlng PDA: M = ({ q0, q1}, {O, 1},·{Zo, x},
odd length palindrome with the middle character as c.
6, q0, Zo, ~) when 6 Is given below :
6(q0, 1, Zo) = {(q0, XZo)} 6(q 0, 1, x) ·= {(qo, Xl_(}} Algorithm:
6(q 0 , O, . x) = {(q11 x)} 6(q0 , e, Zo) = {(qo, e)}
If the length of the string is 2n + 1, .then the first n symbols
6(q 1, 1, x) = {(qi, e)} 6(q1, O, zo) = {(qo, Zo)}
should be matched with the last n symbols in the reverse order. A
stack can be used to reverse the first n input symbols.
Ans.: Status of the stack and state of the machine is shown in
Step 1 : Add productionsfor tJ1c sta1t symbol Fig. 6.4. Input applied is abbcbba
z0 a b -Input
S [qo CJol ..,1:'.~tr ~'P!7 'J;!!!?!~-
; ./
, ~/I (:·:t"-~:~· ·'{•t,'),i
S [qo
ZO
q,] '!~; -Slack
St.cp 2 : Add production8 for 6(11o, l, Zo) = I(q0, xio)) ·qo -State
Zo ' X Zo
[qo qol 1 [qo qol [(Jo 9o1 Fig. 6.4 : A PDA on input abbcbba
[qo
Zo
qol
x
1 [qo q,] [q,
zo
<Jo] The PDA accepting through final state is given by
Zo x zo M = ( {<Jo, q 1, q1 }, {a, b, c} , (a, b, Zc,}, 6, <lo• ?.o, {q1 })
[qo I [qo CJol [qo q,]
Step 3 :
[qo
Zo
q,]
Add production8for 6(q0 , 1, x)
x
1 [qo q,1 [<,Ii
Zo
q,]
I(q0 , xx)}
I.
2.
6(4o, a, e) 2 (<Jo, a)
6(4o. b, e) = (<Jo, b)
J
Where the transition function 6 is given below :
First n symbols are pushed onto .
the .stack
[Qo <Jo]
X
X
[go CJol..,.. 1 [<Jo qi) [g, Clol
X
1 [Clo CJol [9o qol
X
X
X
3.
. 4.
5.
6(4o, C, E) = (q 1, £)
6(q1, a, a) = (q1, e)
6(q1, b, b) = (q 1, e)
J [State changes on c]
Last n symbols are matched with
first n symbols in reverse order
' X X X
[go g1l I [CJo Qo] [qo g,] 6. 6(ql, £, .Zo) = (ql' Zo) [Accepted through final state] .
X X X
[go CJ1l I [CJo g,] [q, q,] A transition of the fonn 6(q0, a, e) = (Qo, a) implies that
nlwnys push a, irrespective of stack symbol.
~wp 4: Add productJonHfor 6(CJo, 0, x) ((q1, x))
X X Q, 10 Convert the following expression grammar to
(CJo CJo] 0 [qi <Jo) a I b I Ia I lb I IO I 11 E
PDA I I I E• EI E• EI
X X
(CJo q,] 0 (q, Qi] (E)
Ana.:
The equivalent PDA, Mis given by,
nnsv - solUIIOIIS
Scanned by CamScanner
• Theory of Comp. Sci. (MU-Sem. 5 Comp.) TCS-41
]
4. 6(4o. [. Zo) = (4o. [Zo) Push the opening bracket'[' Step 4: Add productions for 6(qi, b, a) :s {(q1, £)}
a
6(4o. [. () = (4o. [() [q1 b
5.
Step 5 : Add productions for 6(qi, £, {(q1, E)}
6. 6<cio, [. [ )= (4o. ([) Zo
. [qi E
Simuhtion of PDA for the input Every context free language cannot be accepted by. a DPDA.
For example, a string of the form ·wwR can not be processed by a
Ruic I
(4o, Zo) (4o, (Zo) DPDA.
Rule2
(4o, (( Zo) Thedass of a language a DPDA can accept lies in between a
regular language and CPL.
RulcS
(4o. ])[]), [(( Zo) A DPDA is defined as :
Rule 8 . M = (Q, ~. r, 6, 4o, Zo• F), where
(4o, ) []), (( Zo)
6(q, a, x) has one move for any 'q e Q, X e rand a e ~-
easv-s0111tions
Scanned by CamScanner
_~Theory of Comp. Sci. (MU-Sem. 5-Comp.) TCS-42
0. 14 Design a PDA for detection of palindromes over The transaction, 6(q0,a,b) => { (Cfo, ab), (q1, b) }
rl
- {a, b}. .
Ans.:
. A palindrome will be of the form :
1. WWR
- even palindrome Input 'a1 Is Input'a' Is
2.
3.
wawR
wbwR
J - odd palindrome
Q, 15 Write application of PDA.
part of w middle point
ofwaw
• R
i,M&Mi
If the length of w is n then a palindrome is ·:
Ans.:
First n characters are equal to the iast n characters in the
reverse _order with the middle character as : Applications of PDA
(1) a [ For odd palindrome ] PDA is a machine for CPL.
(2) b [ For odd palindrome] A string belonging to a CPL can be recognized by a PDA.
(3) . E [ For even palindrome] PDA is extensively used for parsing.
The transition table for the PDA is given below: PDA is an abstract machine; it can also used for giving proofs
of lemma on CFL. ·
6(4o. a. Zo) {(qi, Zo), (<Jo, aZo)}
0(4o, b, Zo) {(ql' Zo), (4o, bZo)} a. 16 Desli:in a PDA to accept languag 'e
1 1
.
· {a"- b2n+ In' 1}
"" 11m9'·1'H1 -
6(4o. a. a) · {(4o, aa) (q1, a), (q1, E)} .
6(4o, a. b) {(4o, ab),(q1, b)} Ans. : ·
6(CJo, b, a) fo10 , ba), (q 1, a)} For every 'a' in the input, 2 b's are pushed onto the stack..
0(4o, b, b) {(4o, bb), (q1, b), (q1, E)} For the first 'b' in the input, 2 b's are pushed onto the stack.·
6(q1, a, a) {(q 1, E)} For every 'b' in the input, I 'b' is popped out from the stack.
u
Input
0(%, b, b)
o(q 1, b, b)
=
=
(qi' bl:?b)
(qi, E)
'a' is part O(q1, E, Zo) = (q1, E)
ofw
[Accept using empty stack]
· .Input 'a' Is mlddle
point of odd palindrome a. 17 Design PDA tci check even palindrome over
. l:= {O, 1}
Input 'a' Is first Ans.:
character of wR ·
An even palindrome will be of the form wwR
of even palindrome
l5 ~• lo 1 o, lo 1 o1, e!J a
The transition rule for 6(4o, a, a), must consider the three cases : W i W W i WR W i WR
Scanned by CamScanner
1)'Theoryof Comp. Sci. (MU-Sem. 5-Comp.) TCS-43
.Algorithm: Q.18 Design DPDA to accept language L = e {a, b}* . {x
There is
no way of finding the middle position by n PDA; . N.(x) > Nb(x)}, N.(x) > Nb(x) means number of e's
thereforethe middle position is fixed non-detenninisticnlly . . are greater than number of b's In str_
lng x.
1. Fust n characters are pushed onto the stack. n is non- N4W-Mi-i
detenninistic. Ans.:
2. The n characters on the stack are matched with the last n ·The PDA is being designed to ~pt the string using final
characters of the input string . · state. The stack is being used to store excess of a's over b's or
3. n is decided non-detenninistically . Every character out of fin.t excess of b's over a's out of input seen so far.
(! characters, whose previous character is same as itself Transitions
should be considered fur two cases :
(a) It is finlt character of the second half.
1. 6 (go, a, Xu) =(go, a l.o) [Extra 'a' is pushed]
2. 6 (Clo, b, l.o) =(go, b l.o) [Extra 'b' is pushed]
- Pop the current stack symbol using the transitions : 3. 6 (cio, a, a)= (Clo; aa) [Excess ·a•s are pushed]
·6('1o; 0, 0) (q , E)
1 4. 6 (Clo, a, b) =(Qo, e) [Excess b's decreased by 1)
6('1o, 1, 1) (qi' E) 5. 6 (q0, b, b) =(Clo, bb) [Excess b's are pushed]
,:_ Must be identical 6. . 6 (Clo, b, a)= (Qo, Ei) [Excess a's decreased by I]
(b) It belongs to fin.t half. 7. 6 (Clo, e, a)= (q 1, e) [Input ends with excess a's on
-- Push the current input the stack]
6('1o, 0, E) (<Jo, 0) The PDA is given by :
. S('lo, 1, E) (<Jo, 1) · M :c ({Qo, qi} {a, b}, {a, b, 7.o};o,<Jo, 7.o, {q 1})
4. n is decided non-deterministically . Every character out of first
Q. 19 Construct PDA accepting the language_.,_...,...,.
n characters, whose previous character is not same as itself 2n n .,
L = {a b I n > 0 }.
should be pushed onto the stack. ·
Ans.:
- Push the current symbol using the transitions : ·
Algorithm:
OC<Jo, o. 1) <<1o, 01)
0(<Jo; l, 0} 1. For every pair of leading a's, one Xis inserted in the stack .
('lo, 10)
2. X's on the stack are matched with trailing b' s. ·
'The transition table for the PDA is·given below : ·
6<<Jo, o,.z.o> The PDA is given by
. {(<Jo, OZo)l -
M = ({CJo,q1, q2.q3 },{a,b} {X.2u},6,CJo.¾•$)
6{<Jo, 1, z.o> {(<Jo, lz.o>}
where the transition function 6 is
6(<Jo, 0, 0) {(<Jo, 00) (q 1, E)}
o{<Jo, 0, 1) {(<Jo, 01)}
1. 6( CJo, a, 2i) = (q •• Zr)
OC<Jo, 1, 0) {(<Jo, ·10)}
2. 6(q., a, Zr) = (Clz. XZr)
6{<Jo, 1, 1) {(<Jo, 11), (q 1, E)}
3. 6(q2, a, X) = (ql'.X)
4. ' 6(q 1, a, X) = (Clz, XX)
O(ql'0,0) {(q.,£)}
O(q , 1, 1) {(qi,£)}
5. 6(q2, b, X) = (q3, E)
1
6. 6(q 3, b, X) _ = (q3, E)
6(ql' £, z.o> {(q •• £)}
7. O(q3, E, 'ZJ = (q3, E)
[Accept through an empty .stack]
Accept through empty stack.
Where,
the set of states Q = { 'lo• q• l Q.20 Design a PDA·correspondlng to the grammar:
Scanned by CamScanner
-
,.
Q. 1 Write short note on : Unlversal TM. UTM should be able to simulate every turing machine.
Simulation of a Turing will involve :
Dec . 2005. Ma 2007. Dec. 2007. Ma 2008. Dec . 2008 .
1. Encoding behaviour of a particular TM as a program.
Ma 2009, Ma 2010, Dec . 2011. Ma 2012 ,
Dec.2012 . Dec.2015 2. Execution of the above program by UTM.
Ans.: A move of the form (Q;,3i,qi•3:i•IDc)can be represented as 101• 1
2. The transition function 6 of M can be specified a.'I given : 3. The current state of TM is written on the third tape.
If the current state of M is q1 and the symbol under the head is The control unit of UTM by counting number of O's between
8j then the machine moves to state qJ while changing l1t to 31. The I's can find out the current symbol under the head. It can find the
move of tape head may be : current state from the tape 3. Now, it can locate the appropriate
·
move based on current input and the current state from the tape 2.
I. To-left,
Now, the control unit can extract the following infonnati.on from'
2. To-Right or the tape 2:
3. Neutral l. Next state 2. Next symbol to be written
Such a move of TM can be represented by tuple 3. Move of the head.
{(<IJ,Bi,qj,&_j•~): 'L,qJ,e Q ; 8i•8J•E r ; "1t E {To- left, To- Based on this infonnation, the control unit can take the ·
appropriate action.
Rigbt, Neutral}}
easv-s011111011s
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) TCS-45
•
o. 2 Design a TM which recognizes pallndromeaover
t
b b ab b aBI-B Db ba bbaB f-BB b bah b aB I-B bba b baB
t t t .
alphabet{a,b} Qo QI . Q.J Q.J
1-Bbb ab baB f-'.BbbabbaB 1-Bbba bbaB 1-BbbabbaB ·
l\l;i 2006 . 1\1;-i 1 2009. M;i 2014. Dec. 2017
Ana.:
l l .i 1
I-Db b ab ba B 1-B b b abb BB f-B bb ab bB 1-B bba b b B
A palindromecan have one of the following fonns : t '17 q, '17
t t t
(I,
Initial
Fig. 7.l(a): Transition diagram ...,,.. -..,...- -..,...-
3 5 Product will be
If the leftmost character is 'a' the machine taJces a path stored here
(ii easv-solutions
Scanned by CamScanner
1Jr Theory of Comp. Sci. (MU-Sem.5-Comp.)
Hl#,R I-BO x#x O#OO Bl-BO x# xO#OO B
0/0,R
'b
t t
q,
1-DOx#xO#OOBI-BOx#xO#OOB
t
q,
t
q,
1-BOx#x 0#00 Bl-BO x# xO# 00 OB
0/0,L t t
#/11,L <b q.
I-DO x#xO# 00 OB 1-B Ox #xO# 00 08
t t
q. q.
Fig. 7.2: Transition diagramfor TM . 1--BOx#xO#OOOBI-BOxhO#OOOB
Let us assume that the two numbers to be multiplied are x1 and x2• J. 4
x1 is represented by ro1, where ro1 is a string of O's. I-BO x#O 0#00 OB 1-B Ox #Ox#O 00 B
In the TM shown in Fig. Ex. 7.3.6, there are two cycles. · 1--BOx#Ox#OOOBI--B Ox #Ox#OOO B
t t by n.
q. q.
1
I-Bx0#0:11100B 1-Bx0#00#0 .0B i.e. if 2n S n < 2° + , then log 2(n) = n
t t Let us consider the case of a number
q. qJ
1-BxO#OO#OOB 1-BxO#OO#OOB 1-BxO#OO#OOB
t
q,
t
q,
t
q,
1-BxO#OO#OOB l-8110#00#008 I-B00#00#00B Therefore,
t
q,
t
q,
t
qg 36 can be written u 100100.
5
1-BOx#OO#OOB 1-BOdOO#OOB .... Any number n satisfying the condition 2 S < 2 6 can be
t t written u lXXXXX (where X stands for either 1 or 0). loSJ
qi Qi
(lXXXXX) can be calculated by erasing the most significant bit i
and renamingother bits u 'O'. Unary representation of 5 is 00()()().
Fig. 7.2Contd.•.
Scanned by CamScanner
,(f Theoryof Com~. Sci. (MU-Sem. 5-Cpmp.) TCS-47
Halting 1. 10 # o o o # o o o # o o # o o o o o o # o, o o o o o o
c.:;;-:, c.:;;-:, c;;:> .... y c=:::.:==
•I
state n 1 xn n-1 nx(n-1) n-2 nx(n-1)(n-2) ,
F1g. 7.J(a): Transition diagram
0 1 B
(4>,B.R) (q1,B,R)
41 (q1,0.R) (q1,0.R) (42.B,L)
42• 'h . 'h 'h Halting state
Fig. 7A(a)
Fig. 7.3(b): 'l'ramiUontable ·
Working of TM for (36) 10 is shown in Fig. 7.3(c) : Subroutine for multlpllcatlon :
(36)10 = (0100100)2 0/0,R
B0100100BI-B100100B 1-BOOlOOB
t . t t
.. . Clo Clo 41
t t t oro,L
41 41 41
1/#,L
1-B O O 00 OB 1-B O O 00 OBI-B 00 0 0 OB
Fig. 7.4(b)
t t t
-~ ~ .
Subroutine to copy n -1 :
Fig. 7.J(c)
Cycle2:
Io# o o o # o o o # o o" o o o o o ol
. 1. Multiplication '-v-' '-v-' Y -==::::=
n nx1 n-1 nx(n-1)
easv-solutlons
Scanned by CamScanner
•
a. 6
•
Theoiy of Comp. Sci. (MU-8em. 5-Comp.)
Write note on 'Multiple Turing machine'. 3.z is the symbol under the head on tape 2.
s 1 is the symbol written in the current cell on tape 1•
TCS-48
-
Ans.: s2 is the symbol written in the current cell on tape 2 •
Multiple Turing machine
M1 is the movement (L. R. N) of head on tape 1•
1. A Tining Machine with Multiple Heads
Mis the movement {L, R. N) of head on tape 2 ·
2
A turing machine with single tape can have multiple heads.
Let us consider a turing machine with two beads H 1 and H2• Each
a. 7 Design a TM which recognizes words of the form
a"b"c" I n :.!: 1.
head is capable of performing read/write /move operation
Ma 2006. ·Ma 2008. Dec . 2011, Dec. 2016
independently.
BabaabbaBBB Ans.:
t t
H1 Hz
Fig. 7.5 : A Turing machine with two heads
S 1 is the symbol to be written in the cell under H 1• Fig. 7.7(a): Transition diagram
easv - solutions
Scanned by CamScanner
·-· Y ·Theoryof Comp. Sci. (MU-Sem.5-Comp.) TCS-49
Algorithm:
For a stringitlc-, the TM will need -~ cycles.In each cycle :
I. Leftmosta is wri~n as x
2. Leftmostb is written as y · if~
b/b,L
3. Leftmostc is written as z
x/x,L
At the end of n cycles, the tape should contain only x's, y's
andz's.
Worlcing~fthe TM forinputaVc 3 is shown in Fig. 7.7(c):
• B aaa bbbc ccB 1-Bxaabb b ccc Bl-Bx aa b b b ccc B
t . t t B/B,N
QI qi q1
1-Bxaa bbb cc cB I- Bxaay b be ccB I- Bx.aay bb ccc B
t t t
q1 Cb lb
1-BxaaybbcccB 1-Bxaayb bz cc Bl-Bx aay bb zcc B Fig. 7.8(a) : State transition diagram
. . t t . -t .
Cb 4i
a b X B
1- B xaa1b bzccB I-Bxaayb bzccB I-Bx aayb bzcc B
i . t . t
4i 4i
1-B xaaybbzccB I-Bxa ay b bz cc Bl-Bx xay b hzccB
t t . t
q3 ()I q1
1-Bxxayb bzccB I-Bxxay b bzc cB I- Bxxayy b zc cB 'h (43,X,L) ('b,b.R) ('h.x.R)
t t . t
'b q1 · Cb
1-BxxayybzccB 1-Bxxay.y bz c_cB I- Bxx ayy bz zcB Cb (43,a,L) (43,b,L) (43,x,L) (<Jo,B.R)
t t t
Cb Cb 4i '4 'l4 '4 '4 Halting
I-B xxayyb zz cB 1- B xxayybzz cB \- Bxxayy bz zcB
t , t . i state
4i 4i . · 4J
Fig. 7.8(b) : Transition table .
I-B xxa yyb zz cBI-B xx ayybzz cB \- Bxxa yy bzz cB
i -~ .i The turing machine M is given by :
I-B xxxy yb zz cB I-B xxx yyb zz cB I-B xXx yybzzc B M = (Q, l:. r, 0,<Jo, B, F)
t -, t t
Cb . q1 q, Where, Q = (<Jo, q 1, 'h• 43. q.}
.. I-B xxxyyy zz cB 1-B xxx yyy zzcB 1-B xxxyyy zz cB
t . t t :E = (a, b}
Cb lb Cb
1-B xxxyyyz ·z zBI - B xxx yyy zzzB 1- B xxx yyy zz.zB f =<> (a, b, X, B}
t
4i 4i
t
.
.t q0 = Initial state ·
4i
I-B xxxyy y zzzB 1-B xxxyyy zzzB I-B xxx yyyzzzB
. t . i .t . B = Blank symbol
4i 4i . 4i .
F = ('4}
Fig. 7.7(c)
Working of machine for an input abba is shown in Fig. 7.8(c)
Q. 8 Design a turJng machine to · check whether a
atrlng_over {a,b} contains equal number of e's end
~BabbaB~BxbbaB~BxxbaB~BxxbaB
b's. Dec . 2009, Ma 2008, Dec. 2015 t
qo
t
q1
t
Q1
t
Q3
Ana.: . -~BxxbaB~BxxbaB~BxxbaB~BxxxaB
t t . . t t .
Algorithm:_ qo Qo Qo Cb.
1. Locatefirst a or first b.
Fig. 7.8(c) Contd.••.
2. If it is 'a' then locate 'b' rewrite them as x.
3. If it is 'b' then locate 'a' rewrite them as x.
4. Repr.atsteps from I to 3 till ~very a orb is re-writ~n as x.
easv -solutions
.,
I
Scanned by CamScanner
TCS-50
• Th~ry of Comp. Sci. (MU-Sam. 5-Comp.)
....
I- B X XX X B I- B X X X X B I- B X X X X B I- B X X X X B In unary system, 3 is represented as 000.
t
Cb
t
<b . .
t
<b
t
Cb
In unary system, 0 is represented by n blank tape.
Subtraction will require several cycle. In each cycle :
I- B X X XX B I- B XX X X B I- B X X X X B I- B XXX X B
t t t t I. Leftmost Ois erased
2. Rightmost O is erased.
I- B XX XX B I- BX XX X B Situation of tape after each cycle is shown below :
l t Initial j BI' 0 : ..o ~,..O, 0 0 # ·0 ,q,:~~i>;·.s'.I'.~
Fig. 7.8(c)
Q x r to Q x r x (L.R.N) O/B,R
B/B,N
A transition in turing machine is written as,
6(4>, a) = (q 1, b, R), which implies, when. in state CJo and Fig. 7.9(a): Transition diagram
scanning symbol a. the mac~e will enter state q 1, it will rewrite a
0 # B
as b and move to the right cell.
(q1,B.R) (<4.B,R)
A transition 6(CJo. a)= (q1, a. R), implies that the machine will
enter state q1.- it will not change the symbol being scanned and q, (q 1,0,R) (q1,#.R) ([b.B,L)
move to the right cell 'h (43,B,L) (<Is O,N) .
easv-solutions
Scanned by CamScanner
'(I Theory of Comp. Sci. (MU-Se~. 5-~mp.) TCS-51
Where,
(ii easv-sotutions
Scanned by CamScanner
•
• of
Theory Comp. Sci. (MU-Sem. 5-Comp.)
Non-deterministic does not make a TM more powerful. Where, Q = {qo, q1, Ch, q3, <14, 45}
For every non-deterministic TM, there is an equivalent . = {O, l}
It is easy to design a non-detenninistic TM for certain class of 6 = Transition function is shown using the transition
diagram
problems .
B :,; Blank symbol for ·the tape
A string is said to be accepted by a NDTM, if there is at least
F = {45), halting state
one sequence of moves that takes the machine to final state.
Working of the machine for input 0101101 is shown in
An example of non-deterministic move for a TM is shown in
Fig . 7.13(a):
Fig. 7.12.
0101101B I- 0101101 l-01011018 I-0101101
a/a,R t
qo
t
qo
t
q1
t
qo
a/x,R
clr---+--- }fa
:,..- l-01011016 1-· 0101101B
t . t
q1 CorqJ. 57
Fig. 7.12 : A sample move for NDTM
l-01011118 f- 01010118
1be transition behaviour for state <Jo for'TM of Fig. 7.12 can t t
be written as
f-010111B 1-0101 118 1- 0101118
o(<Jo, a) = {(<Jo, a, R)(q 1, x, R)}
t t t
Q.12 Design a turlng machine to replace string 110 by qo · q1 qs (halt)
(ii easv-s01u11ons
Scanned by CamScanner
• Theory cit Comp. Sci. (MU-Se~. 5-~omp.) TCS-53
The Turing machine will have 3 states : ·
B. B B 0
~- previous cany as 0
4t - previous cany as 1 B B 1 l 0
b/b.,L
a/a,L
Fig. 7.14
Fig. 7.15
Simulation for 110 + IO
B 1 I 0
B B B 0
' i
State <Jo .
1/0,R
B B I 0
Fig. 7.16
B I 1 0
Scanned by CamScanner
lJt Theoryof Comp. Sci. (MU-Sem.5-Comp.) TCS-54
Q. 16 Construct a TM for checking well for medness of 6 is given in Fig. 7.17(a) or7.l7(b)
· parentheses. Ma 2012 . Ma 2015. Ma 2017 4i = Initial state .
Ans.: B = Blank symbol
In each cycle, the left-most ')' is written as X; then the head F = (q 3 ), halting state
moves left to locate the nearer '(' and it is changed t~ X. Making of the machine for input (()0)() is given in Fig. 7.17(c) :
B/B,R
Halt state I-Bx xxxxxx xB I-Bx xxxxx xxB I-Bx xxxxx xxB
Fig. 7.17(a): State transition diagram
Qi
t t
Q:z
t·
<b
( ) X B I-BxxxxxxxxBI-BxxxxxxxxBI-BxxxxxxxxB
easv - solution
Scanned by CamScanner
·• Theory~f Comp. Sci. (MU-Sem.5-Comp.) TCS-55
. x/x,R
j-B xxxxB r-BxxxxBI-BxxxxB r-B xxxxB
' a/a,R
t t .t t
Cb Cb Cb Cb
1-B xxxxB r- Bxxx xB I-Bx xxxB r-B x·xx:x B
a/a,L t t . . t t
b/b,L qo cfo Qo <li
·x/x,L
1-B xxxxB r-Bxxx xB
t t
Fig. 7.18(c)
B/B,R
B/B,N Q. 18 Design a Turing machine as an acceptor for the
language
{a" bml n , m 0 and n)
Ans.:
Fig. 7.18(a): State transition diagram a/a,R
b/b,R
a b X B
q4• 44 44 44 44 Halting
state
easv-sotutions
Scanned by CamScanner
. '
B = blank symbol
F :a: {43}, halting state.
Chapter 8 : Undecidability
Q. 1 Write short note on : Recursive and Recursively Every Turing decidable language is Turing acceptable.
Enumerable Languages. Every Turing acceptable language need not~ Turing decidable.
Dec . 2005. Dec. 2009. Dec. 2010 . Ma 2014 , Dec . 2014. Turing Acceptable Language
• I , t · I ,
A language L c: I:* is said to be a Turing Acceptable .
language if there is a Turing machine M which halts on every
Ans.: ro e L with an answer 'YES'. However, if ro E L, then M may not
Recursive and Recursively Enumerable Languages halt.
There is a difference between recursively enumerable (Turing Turing Decidable Language
Acceptable) and recursive (Turing Decidable} language.
A language L C: I:* is said to be turing being decidable if
Following statements are equivalent : there is a turing machine M which always halts on every c.o e l:*. If
I. The languageL is Turing acceptable. roe L then M halts, with answer 'YES', and if 0> E L then M halts,
2. The language L is recursively enumerable. with answer 'NO'.
Following statements are equivalent A set of_solutions for any problem defines a language.
t. The languageL is Turing decidable. A problem Pis said to be decidable /solvable if the language
L C: I• representing the problem (set of solutions) is turing ·
2. 1be languageLis recursive. decidable. · ·
3. Thereis an algorithm for recognizing L.
easv - so1u11ons
. ,
Scanned by CamScanner
.... · Theory of Comp. Sci. (MU-Sem.5-Comp.) TCS-57
easv-s01ut1ons
Scanned by CamScanner
Ans.:
Theory of Comp. Sci. (MU-Sem. 5-Comp.)
.Post correspondenceproblem
a. 5 ·Write short notes on : Halting problem.
TCS-58
-
Definition: Let A and B be two non-empty lists of strings over :E. i•MRMil•i4R•Mi--W•MR-i•iidl&WiliDj
A and B are given ns below : Ans.:
A = {x 1, x2, x 3 ••• xk} Halling Problem of a Turing Machine
B = {y,.y2, Y3 ••• yk} The halting problem of a Turing machine states :
'There is a post correspondence between A and B if there is a Given a Turing machine Mand an input w to the machine M,
sequence of one or more integers i, j, k ...m such that: detcnnine if the machine M will eventually halt when it is·given
'The string lt; lt_i ••• x,,,·is equal to y I YJ .. . y m· input ro.
Example : Does the PCP ~th two lists : Halting problem of a Turing machine is unsolvable.
A = (a. aba3. ab} and Proof:
3
B = {a .ab.b} . Moves of a turing machine can be represented ~sing a binary
have a solution ? number. Thus, a Turing machine can be represented using a string
So to find a sequence using which when the elements of A over l:•(O,l). This concept has already been explained in the
and B are listed. will produce identical strings. chapter.
The required sequence is (2, 1, 1 , 3) Insolvability of halting problem of a Turing machine can be
3 6 proved through the method of contradiction.
A-i A 1 A 1 A3 - aba aaab = ab a b
3 Step 1 : Let us assume that the halting problem of a Turing
Bi B1 B1 B3 = aba a3 b =aba6 b
machine is solvable. There exists
Thus, the PCP has solution.
t. A string describing M.
So accept the un-decidability of post correspondenceproblem 2. An input ro for machine M .
without proof.
H 1 generates an output ''halt'' if H 1 determines that M stops
Example: on input w; otherwise H outputs '1oop". Working of the machine
Determining the solution for following instance of PCP. H1 is shown below.
~halt
~loop
-
I
Scanned by CamScanner
:• Theoryof Comp. Sci. (MU-8em. 5-Comp.) TCS-59
If H, halts OD H, as input then H3 would loop (that is how we a. 7 Write short note on : Rice Theorem ·
- constructedit). If H3 loops forever OD ff, as input H3 halts (that is Dec. 2012. lv1n 2013 . Mn 2014. Ma 2015 . Dec . 2015 .
bow we comtructcdit). mtMi4•i4W.;•1@HWMfl• 1W.;11ti
In either case. the result is wrong.
Ans.:
Hence. Rice Theorem
H3 docs not exist.
"Every property that is satisfied by some but not all
If H 3 does not exist than H 2 does not exist.
recursively enumerable language is un-decidable". Any property ·
IfH 2 docs not exist than 6 1 does not exist..
that is satisfied by some recursively .enumerable language but not
a. 6 Does PCP with following two Hat : A = (10, 011, all is known as nontrivial property. We have _seen many properties
101) and B = (101, 11, 011) have a solution ?
of R.E. languages that are un-decidable. These properties include :
Justify your answer. diiiUiD
Ans.: 1. Give.n a TM M. is L(M) nonempty ?
Ai, and A3 differ from B2 and B 3 at the .first of place. 2. Given a TM M, is L(M) finite '!
Therefore.WC must pick Al and B1
3. Given a TM M, is L(M) regular '!
Sequence String
4. Give~ a TM M. is L(M) recursive '!
(1) (A 1 = 10) (B 1 = 101)
The Rice's theorem can be proved by redu~g some other
The next string to be picked up must be A3 and B3• Any other
sequencewill not lead to a solution. unsolvable problem to nontrivial property of recursively
enumerable language.
Sequeoc:e String
eas11-solutio11s
' I
Scanned by CamScanner
I- Theory of Computer Science
Statistical Analysis
~•':w,,ffi-w...
(),':~'""~'if(!'i~f\f\~~•~,~,-1CT)~~:lr ,'·;li'I:
i :i f...,::¥. '4•lrl" ;1>;;~~~i
1 '~'I j
I·· . /;yj
t,,-J:-11•• ~t~~J'\/ ,;~ll-~ ?,: ,1~ ';').'Ar.if:,:J
Chapter 1 27.5 Marks 10 Marks
· Chapter4 - -
Chapter 5 - 10 Marks
Dec. 2018
A E
A a
A aB . ,
A Ba
Scanned by CamScanner
., :n,eory_of f°"'P"19rScience (MU) . D(18t-2
A grammaris called a Type 1 or C9{1text sensiti~e grammar!fall its productionsare of the foll~wing form:
a p.
- Where. Pis atleast as long as a.
Type Oor unrestrictedgrammar -
Productions can be written without any restriction in an unrestricted grammar._If there is production of the a ~. then length of a
could be morethan length of~-
Ana.:
(i) Left most derivation:
ibtSeS b]
ibtaeS [usingS
ibtaeibts [usingC b]
ibtacibta
. ..... .. .. '
eas v S OIUII O II S
.
. '•
: ,•, .....
Scanned by CamScanner
• Th~ry of ComputerScience(MU)
(ii) Righbnostdcrivation:
iCtSeiCta [usingS a]
iCtSeibta [using b]
iCtaeibta [using S a]
ibtaeibta [using C b]
Q. !S(b) Construct Mealy and Moore Machine to convert each occurrence of 100 by 101. (lOMarks)
Ans.:
L Mady Mathlne
011
Ftg.1-Q. S(b)
easv-s01u11uns I ·'
Scanned by CamScanner
.I lheory of ComputerScience (M~) 0(18);;4
2- Moore Machine
. j\•::·~· ·:·\i;,:i:-::1:•:•t·:-~ •-<·1,
1;:.: :: · .•:: . ,.
. ,• ..
. !. •·:
Fig.2-Q. S(b)
Q. 6(d) Write short note on Mealy an~ Moore Mac~lne. (2.5 Marks)
Aris.:
Final state.machinesarc characterisedby two behaviours :
·-
1: State transitionfunction (6) ·
2. Output function0,.)
. Set of output alphabet O can _be different from the set of input alphabet l:.
Q. 2(a) Design a Rnlte State machine to detennlne whether ternary number ~base3) Is divisible by s. (10 Marks)
AnL:
- A system tw three alphabets
l: = {0, I, 2)
Base of a ternarynumber is 3.
- The running remainder could be :
(% = 0 associated state, <Jo
ea s v-solu11ons
Scanned by CamScanner
Jt ·Theory of Comp~ter Science (MU)
(1~ = 1 associntedstate, q 1 I •
....
(10)3 =3 associated state, 'h
(11)3 =4 associated state, '4
t t
Ternary Decimal.
Scanned by CamScanner
. .
. & -Theoryof Compute~Science(MU) I ·, .· 0(18)-i> ·
(1) Set of all strings over {O, 1} ·that end with 1 has no substring 00 (5 Marks) .·
Ana.:
Regular expression
- An cxpressi~n written using the set of operators (+, • , *) and describing a regular lan~age is known as regular expression.
- The trnnsition graph is shown in Fig. 1-Q. 1(c).
I •.•• ,•
:. ILE. can be written from the transition graph. The requiredRE.= 1 (1 + 01)*
Q. 2(b) Give and explain formal definition of Pumping Lemma for Regular_Language and prove that following
language is not regular. L = {am bm- 1 Im> O} (10 Marks)
-Ans.:
Pumping Lemma for Regular Language
- Some languages are regular. There are other languages which are not regular . One can neither express a non-regular langua.,oe using
regular expression nor design finite automata for it
Pumping lemma gives a necessary condition for an input striiig to ~long a regular ~t
,- PwnJJing lemma does not give sufficient condition for a wiguage to, be regular. ,- ,
Pumping lemma should not be used to establish ~at a given language is ~gular.
- Pumping lemma should be used to establish ·that a given language is not regular.
- · The pumping lemma uses the pigeonhole principle which states that if n pigeons are placed into less than n holes, some holes have to
have more than one pigeon in it SimjJarly, ·a string of length~ n wh~o recognized by FA with n states will see some states
• • • •, •, I I "' , • '
repeating. . . .
Definition of Pumping Lemma
Let L be a regular language and M = (Q, l:. 6, 'lo, F) -~ .a fini~ automata with n-states. Language L is acceptedby m. Let ro e Land I
<01 n. then co can be writtenas xyz. where · ,: · ·
(i) I y I >0
(ii) I xy I :S n
(iii) X'f z eL for all i 0 ,here y1denotes th3:1 y is repeated or pumpedi times.
easv-s01u11011s
Scanned by CamScanner
:1! Theory of Computer Scie~ce (MU) 0(18)- 7
a
Provingthaithe languageL = {am bm.:.1 1m > O} Is not n,gular:
Step 1 : Let us assumethat the given languageL(an bn- 1 Iii> 0) is regularand Lis acceptedby an FA with n states.
Step 2 : Let us choose a string
ro = 8n b n-1
I ro I = 2° - 1 n for n > o
Let us write ro as xyz. with
IYI > O
andlxyl n
Fig.1-Q. 2(b)
= an+rbn-1
Since r > o;·an + r b0 - 1 e: L.
Hence, by contradiction,we can say that the given languageis not regular.
Q. S(a) Convert (0 + 1) (10)*(0 + 1) into NFA withe-moves and obtain DFA. (10 Marks)
Ans.:
R. E. to NFA
easv-solutions
Scanned by CamScanner
_I Theoryof ComputerScience(MU) 0(18);. 8
Q. 6(b) Write short note on : Applications of Regular expression and Finite automata. (2.5 Marks)
Ans.:
1. Applications of regular expression
(a) R.E. in Unix
'The UNIX regular expression lets us specify a group of characters using a pair of square brackets []. The rules· for character classes
are:
'The grep utility in UNIX,scans a file foi-the occurrence of a pattern and displays those lines in which the given pattern is found. ·
For example :
It will list those lines from the file emp.txt which has the pattern "president". TI1e pattern in grep command can be specified using
• regular expression.
7. • matches a single_character.
8. [" pqr] Matches a single character which is not a p, q or r.
(c) $grep "A . • thakor"emp.txt will look for a pattern starting with A:and ending with thakur in the file emp.~t.
(b) Lexicalanalysis
Lexical analysis is an important phase of a compiler. 1be lexical analyser scans the source program and converts it into a steam of
tokens. A token is a string of consecutivesyfllbolsd~finingan entity.
easv-so1u11011s
Scanned by CamScanner
Y Theoryof ComputerScience(MU) 0(18)-9
x - An identifier
= - Assignment operator
y = Anidentifier
+ - Arithmetic operator+
z - An identifier
The UNIX utility lex can be used ro/wntingof a lexical analysis program. Input to lex is a set of regular expressions for each type of .
token and output of lex is a C program for lexical analysis.
2. Appllcatlons of Finite Automata
Finite automata are used for solving several common types of computer algorithms. Some of them are :
Finite automata can work as an algorithm for regular language. It can be used for checking whether a string wel.., where Lis a
regular language.
Ans.:
Difference between PDA and NPDA is as follows:
I. Always a single move on a new input Multiple moves are possible on a new input
3. Algorithms related to PDA are simple Algorithms related to NPDA are complex
4. Algorithms related to PDA do not require backtracking Algorithms related to NPDA require backtracking
easv-s011111011s ,· . , .
Scanned by CamScanner
1
' • I Theoryof ComputerScience(MU)
. ,, .· . ;· , /· 0(18) • 10
Transitiontable·(&)
1. 6(Qo. a, Zo) e (q1, Zo)
S ASAIAb
B ble
Ans.:
L Nuilable .set of symbols= (B, A)
weget.
s ASlsAIASAlaela
els
B b
B b
3. Every symbol in a, in productionof the form A a where Ia I~ 2 should_be a variable.This can be done by adding the production
a
The set of productionsbecome,
s AS lsA !ASA lc1B la
A b IAS lsA IASA lc1Bla
eas v - s 01u11011s
. . -. .,,
'
Scanned by CamScanner
if Theoryof ComputerScience(MU) D(18) • 11
4. Finding an equivnlentgmmmnr In CNF.
Initial B (()())() B
I. B (XX())QB
2. B (XXXX)() B
3. BXXXXXX()B
4. BXXXXXXXXB
( ) X B
)/x,I., , (<Jo.(,R)
CJo (q 1,x,L) (cio,x.R) ((b,B,L)
qi (CJo,X,R) (q1,x.L)
xi~ Huiting
13/8,R.
Hall11tatc state
Fig. 1-Q, 4(a)(a) : Stale translUon diagram Fig, 1-Q. 4(a)(b) : State trruasltlon table
® '
(ji easv-solUIIOIIS
Scanned by CamScanner
• I
4, = Initial state
B = Blank symbol
F = {43}, halting state
Making of the.machine for input (00)0 is given in Fig. 1-Q. 4(aXc): ·
l~J,3 ( xx xx)()
•·•
·- xx·o....r~· t . .
·•• · t xx
<xx xx x <) B ·:
::1~·Bffx .• · (
~:·:· · t -xxx
}B 1-B (-x
: . -:- -qt .. . .. ql .: , Q1
l~Bxxxxx:x ()B ~Bxxxxxx(}BkB x xxxxx () B· _.:
. :: 4o ·.·. ·•· · . ' . .: ' Jo . . ' . t .
' i. :a.~ ·xxxxx OB rBxx~xXx()BF :Bx xxx :xx () B ' .
. · .. · :x_ ·to . _. .· .· i ·.· Jo· ·
1-:Bx xxxxx ()B)~B x·xxx
.. ' xx(t xB - t
. Cb . ql
1-]3 xx BI-Bx xx xx x xx B
t
.•·:- ·- ·' .......t· . '· : . ·.. . • qo
· -= qo
easv-s01u11ons
Scanned by CamScanner
../
'
Ans.:
Universal turlng machine
A general-pwpose computer can be programmed to solve different types of problems..A TM can also behave like a general-purpose
computer. A general purpose computer solves a problem as given below :
1. A program is written in a high level language and its machine-codeis obtained with the help of a complier.
4. Program stored in memory is executed line by line. Execution involves reading a line of code pointed by IP (mstruction pointer),
· decoding the code and executing it
We can follow a similar approach for a TM. Such a TM is known as Universal Turing Machine. Universal Turing Machine (UTM) ·
can solve all sorts of solvable problems.
1. To-left.
2. To-Right or
3. Neutral
Such a move of TM can be represented by tuple
UTM should be able to simulate every turing machine. Simulation of a Turing will involve :
Scanned by CamScanner
. I
A move of .the fonn (<L,&i,q1,8J,Dlc)can be represented~ 101+1 101 to'+ 1 to' 101\
State 4, is represented by 0, .
The control unit.ofUTM by counting number of O's between l's can find out the current symbol under the head. It can find the current
state from the tape 3. Now, it can locate the appropriate move based on current input and the current state from the tape 2. Now, the
control unit can extract the following infonnation from the tape 2 :
Based on this information. the control unit can take the appropriate action.
easv-solUIIOIIS
Scanned by CamScanner
!J Theoryof Computer Science (~U) 0(18) - 15 -
Proof:
Moves of a turing machine can be representedusing a binary number. Thus, a Turing machine can be represent.edusing a string over
l:*(0,1). - -
lnsolvability of halting problem of a Turing machinecan be proved through the method of contradiction.
Step 1 : Let us assume that the halting problem of a Turing machine is solvable. There exists a machine H,(say).
H1 talces two inputs :
1. A string describing M.
2. An input Cil for machine M.
H, generates an output ''halt''if H1 dete~es that M stops on input ro; otherwise H outputs "loop". Working of the machine HI is
shown below.
.:.: .. ;•
halt:
c:--:c:~..:;_...,...,,---
- foop
Step 2: _Let us revise the machineH 1 as H2 to talce Mas both inputs and Hz should be ab!e to determine ifM will halt
Step 3 : Let us construct a new Turing machineH3 that takes output of H2 as input and does the following :
Hence,
easv-solutions
Scanned by CamScanner
9! Theory of eome&r Science(MU) 0(18)-16
1be Rice's lbcorcm cao· be proved by reducing some other unsolvable problem to non-trivial property of recursively enumerable
. .
langpagc..
ea s v S UIIIIIOII S
Scanned by CamScanner
I •,
. .r .
May2019
Q. 5(b) Conwrt the following gramrnara to the Chomsky normal form (CNF)
S-OAO 11B11 BB .
A-C
_)
B-SIA
(10 Marki) ,
Ans.:
Step 1 : EHminfJoa of e ..:production. ----,
'
The symbols (A. B, C. S) arc millableand hence the given granularleads to the following granular :
S OAO I 00 I IBI 1111 B I BB
, Granular G 1
Weld,
S OAO I0011B1 1111 BB -
111 IBB'
A1
..
S&qt 5: Wrltlns prodadlonsIn CNF
S A1B1, B1 SA1
, .. , .. I '•
-~...:'
t . ..... :
. .:-
Scanned by CamScanner
. • .Theoryof ComputerSdence {MU)
. Chapter 2: Finite Automata [Total Marks - 20].
DFA stands for deterministic finite automata. NFA stands for non-deterministic finite automata.
2. The transition Is deterministic. The transition is non-deterministic.
3. A ~stic finite automata is a quintuple, A non7<1etenninisticfinite automata is a 5-tuple.
M = (Q. L 6, <Jo• F) M = (Q, L 6, <Jo, F) _
0.1(b) Design a DFA to accept string of Os and 1a ending with tha string 100. (5 Marks)
Ans.:
The substring 'abb' should be al·the end of the string. Transitions from q should be modified to handle the condition that the string ·
3
bas to end in 'abb'. ·: .
a a b
-+<Jo qt 'lo
qt qi 'Ii
'Ii qi .
b q: qi 'lo
(a) State tramition diagram{b) State transition table
Fig.1-Q. l{b): Final ~FA \ .
q3 to q1 on input a : An input of a in q3 will make the previous four characters as 'abba' . Out of the four characters as 'abba' only the last
charactec'a' is relevant to 'abb '.
q 3 to flt oa input b: An input of bin q 3 will.make the previous four characters 'abbb'. Out of the four c~ 'abbb'. nothing is
relevant to 'abb'.
Q. Design NFA for recognizing the strings that end In ..aa" over r =.{a,b} and convert NFA to DFA. ·(1 OMarks)
Ans.:
(i) NFA for strings ending in "aa' is given below :
Scanned by CamScanner
. ~Theoryof~rSdence(MU) M{19)-3
ta e;1s •1 \0 11111011 \
Scanned by CamScanner
• Thffl of eongrter Science(MU) M(19) -4
Chapter 5 : Pushdown Automata (PDA) [Total Marks·- 1OJ
Q. 4(b) Slate and explain pumping lemmafor context free languaa-. (10 Marb)-
·Ana.:
Let O be a contextfree grammar.Then there existsa constantn such that any string
3. For all i 0, uv1xy1z is in L The two stringsv and y can be pumped zero or more times.
Proof:
Let us imume that the grammar
it
In pmnping lemma, is a requ.ucmentthat the constantn should satisfythe following
condition
Let us take a ·string w e L (G), such that I w I n. Let us constructa parse tree T with
l'.iA f. ,l \ V S U I 11111111 S
Scanned by CamScanner
1, Theoryof ComputerScience(MU)·
: , -.. Chapter 6 : Regular Grammar [Total Marks - 1OJ.
W' stands for reverse ofW. A string of the form Wcvl' is an odd length palindrome with the middle character as c.
Algorithm:
If the length of the string is 2n + 1. then the first n symbols should be matched with the last
n symbols in the reverse order. A stack
'
.
can be used to reverse the fim input symbols.
Status of the stack and state of the machine is shown in Fig. -1-Q. 5(a). Input applied is abbcbba.
a b b C b b a e -Input
: :~;E : : :;
[State changes on c]
A transitionof the form O(<Jo, a.£)= (<Jo, a) implies that always push a, irrespective of stack symbol.
In a standard luring machine number of positions for leftmost blanks i~ fixed and they are included in instantaneous descriptton.
·
. . ·
wbtte the right-hand blanb are not included. ·
, In the two way infinite Turing machine, there is an infinite sequence of blanks on each side of the pu ouwg.
m· t HR- In- •
an-mstantaneous
. . .
desaiption.theaeblanb 'are nevershown.
Scanned by CamScanner
t Theory of eomputerScience(MU) ·- M(19)-6
The transition behavior of 2.-hcadone tape Turing machinecan be definedas given below :
6 {State.Symbol under H1, Symbol tinder Hi)= (New state, (S1, M1), {S2, MJ)
Where,
S1 is the symbol to be written in the cell under H 1•
M 1 is the movement (L. R. N) of H1•
3. Multi-tapeTuring machine
Multi-tape twing machine1w multiple tuples ~tti each tape having its o~ independent head. Let us considerthe case of a two tape
turing machine. It is sliownin Fig. 2-Q. 3(b).
Tape 1:
i;:;...i:i.=~~~~-~=--~~~
~:a.
Where,
. I
Scanned by CamScanner
r . .
. •·
4. Non-detennlnlsticTuring machine
A
Non-deterministic is a powerful feature. non-detenninistic TM machine might have, on certain combinations of state and·
symbol under the bead, more than one possible choice of behaviour.
Non-detenninistic does not make a TM more powerful.
For every non-deterministic TM, there is an equivalent detenninistic TM.
It is easy to design a non-deterministicTM f~r certain class of problems.
A string is said to be accepted by a NDTM, if there is at least one sequence of moves that takes the machine to final state.
·The transition behaviour for state CJo for TM of Fig. 3-Q. 3(b) can be written as
S. UniversalTuring machine
A general-purposecomputer can be programmed to solve different types of problems. A TM can also behave like a general-purpose
computer. Ageneral purpose computer solves a problem as given below :
1. A program is written in a high level language and* machine-code is obtained witlithe help of a complier.
4. Program stored in memory is executed line by line: Execution involves reading a line of code pointed by IP (instruction poin~).
decoding the code and executing it ·
We~ follow a similar approach for a TM. Such a TM.is known as ·umversal Turing Machine. Universal Turing Machine (UTM)
can solve all sorts of solvable problems.
If the current state of M is <t and the.symbol under the head is 8i then the machine moves to state qj while changing lit to 8:i· 1be move
of tape bead may be : ·
· t. To-left,
2. To-Rightor
3. Neutral
UTM should be able to simulate every turirtg machine. Simulation of a Turing will involve :
1. F.ncodingbehaviour of a particular TM as a program.
e;is v s0 11111011s
Scanned by CamScanner
_t Theory
'
M(19)-$_:·
of eongrter Science {MU)
2. Executionof the above programby UTM.
1 1
A move of the form (CJi,Bi.qJ•8J•IDr) can be repmicntcd as td+ 1rJ I rY + I~ 1oK.
K = 3. if move is 'no-move'
State 4, is representedby 0,
Q. 4(a) Design a hiring machine that computes • function f(m,n) = m+n I.e. addition of two Integers.
. . -· . .
(10 Marka)
Ans.:
Addition of two unary numbers can be petformed through append operation. To add two numbers 5 (say ro1) and 3 (say caj ~ -.
ICqUirefollowing steps :
2. w, is appended to Olz·
While every •o• from mis getting appendedto Olz, 'O' from ro is erased. Olz contains 8 O's, which is sum of 5 and 3.
1 1
A language over an alphabet L can be described recursively.A recursive definition has three steps :
1. Specify some basic objects in the set
2. Specify the rules for constructing moreobjects from the objects aln:ady known .
3. Declaration that no objects except those constructedas given above are allowed in the set
Scanned by CamScanner
·.Y Theory of ComputerSci~ (MU)
Reclnlvelyenumerablelanguage
( . .
There is a difference between recursivelyenumerable(Turing Acceptable)and m:unrive (Turing Decidable) language. .,,
A = {X1, ~ . X3 •• • x.:l
B· = {Y1,Yi,Y3 ••• Ytl
We say, there is a post correspondencebetween Aand B if there is a sequence of one or more integers i, j; k ... m such that:
has a solution.
We will have to find a sequence using which when the elements of A and Bare listed. will produce identical strings.
Scanned by CamScanner
f Theoryof ComputerScience(MU)· ,M (19)-10~
Glftll a Tmtnamachine M and an Input m to lhe rucblne M, determine If the IIUlddneM wlD nmtaally halt when It la pYa
IDpatm.
lnsolvability of halting problem of a Turing machi~ cim be pmved throughthe method of contradiction.
Step 1 : Let us assume that the baiting problem of a Turing machine is solvable. There exists a machine H 1(say). H 1 takes two inputs :
1. A string desaibing M.
2. An input 0> for machine M.
. H 1 gcncratcs an output "halt" if H 1 dctermincs that M stops on input ro; otherwise H outputs "loop". Working of the machine H 1 is
shown below.
halt
loop
Step 2 : Let us revise the machine H1 as to take M as both inputs and aboalclbe able to determineif M will halt on M as its input.
A machine can be describedas a string over Oand 1.
M
Step 3 : Let us construct a new Turing machine }'3 that takes output of~ as input and does the following :
H "3 halts on "3 as input then "3 would loop (that is how we constructtJCiit). ·
If "3 loops forever on "3 as input "3 halts (that is bow we constructedit).
Hence,
"3 docs not exist.
If ff, docs not exist than Hi docs not exist.
· If"2 docs not exist than H1 docs not exist.
Scanned by CamScanner
t lhaory.ot ComputerScience (MU)
M(19_) • 11 ,
(c) Rice'• theorem
Every property that is satisfied by some but not all n:cursivelyenumerablelanguageisun-decidable.Any property that is satisfied by
have seen many ~es of R.E. languagesthat
some RCUISivclyenumerablelanguage but not all is known as nontrivial property. We
are un-decidable.These propertiesinclude : ·
' :,
ta f. ,l S S O 11111 Cl II S
Scanned by CamScanner
.• Theoryof eomre;:Science (MU) . 0..:1
Q.2 (a) Design a Finite State machine to determine whether ternary number (base 3) is divisible by 5.
(10Marks)
(b) Give and explain format definition of Pumping Lemma for Regular Language and prove that ·
following language is not regular. L = {am bm - 1 I m > 0} (10 Marks) .
Q.3 (a) Construct PDf' accepting the language L = {a2n b".I ~0}. (10 Marb)
Q. 5 (a) Convert (0 + 1) (10)*(0 + 1) into NFA withe-moves and obt~in DFA . . · (10 Marks)
(b) Construct Meaty and Moore Machine to convert each occurrence of 100 by 101. (10 Marks)
Scanned by CamScanner
·=of-Science(MU) Q-2
· ' (b) -Design a DFA to accept string of O's and 1's ending with the string 100. (5 Marks)
Q. 2 '(a) Design NFA for recognizing the strings that end In •aa• over I ={a,b} & convert above NFA to DFA.
(10 Ma~)
If input ends in '101' then output should be A, if input ends in '110' output should be B, otherwise
output should be C· and : convert it into mealy rn/c. - (10 Marks)
Q. 3 (a) Obtain a regular expression for the FA shown below : (10 Marks)
Fig. 1 Q. 3(a)
Q. _4 ,(a) . . Design turing machine that computes a function f(m,n) = m + n i.e. addition of two integers.
(10 Marb)
(b} .State and explain pumping Lemma for Context Free Languages. Find out whether the language
L= {x'Y'z.'1I n ~1} is context free or not. • (10 Marb)
L(M) = {we-,/{I w {a,b}i wh_ere v/!' is reverse of w & c is a constant. - (10 Marks)
aS V
£il I! '> fl 11111 IJ II~
Scanned by CamScanner
t
a. 6
= eome Science(Mu)
Write detailed note on (any two):
Q-3
(20 Marks)
(a) Post CorrespondenceProblem
(b) Hafting Problem.
(c) · Rice's Theorem.
.. .. '·
Scanned by CamScanner
r
I •
• Your Successis Our Goal
.,,,111111 1n1un111,,1,1u,nuun1n1,11,, ........ . ,,,1,un111111111111111nn1111u1uu111t111n11,1111~1u1u1u111u1u,u,111nuuuu1uuuuu1uu,,,,.,., .. ,,,,.,,.
•
euuuu,
SemesterV • Computer Engineering·
111111111u1111111n1111111111111111nnn1111~111n111111u1u11•• .. • .. •••••~•••n111111n1u11u1uuu1u1,uu1htu1uuuuuu~u,,u,.,,..,.,,,. ..,,.,,,,,,.,,,,,,
•• , , .... ,,,
Compute~ Networks
1u1111111n1n11111111n111n1111111nnn11111nn1111un,1 .. •••• .... ••••n1u1111111u1111111•• .. •••• .. • .. ,,,,,.,.,,,,,,.,,,,,,,,,,,,,,,,, ..................... ,,,,,,
r
!
&S
e as s I u i
V- O I Ons
~·&' TechKliowledgl!1
~r pubIi Cat iOns l
\ . I
Distributors
Scanned by CamScanner