0% found this document useful (0 votes)
70 views97 pages

Tcs Es 2019 Easy Solution Textbook For Tcs

Uploaded by

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

Tcs Es 2019 Easy Solution Textbook For Tcs

Uploaded by

Kritika Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 97

Mumbai University Paper Solutions

l Strictly as per the New Revised Syll~bus (Rev - 2016) of


Mumbai University w.e.f. academic year 2018-2019
(As per Choice Based Credit and Grading System)

THEORY OF
COMPUTER SCIENCE
Semester V - Computer Engineering

Chapterwise Paper Solution upto May 2019.

• . TM

,;• TechKno111ledge
Pub I i c a t i o n s

Scanned by CamScanner
easy - solutions

Theory of Computer Science ·


I I • ' •

Semester V - Computer En ineerin

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 . .

Printed at: 37/2, Ashtvinayak Industrial Estate, Near Pari Company,


Narhe, Pune, Maharashtra State India, .
Pune - 411041

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

(Book Code : EM046A) .

Scanned by CamScanner
INDEX

Chapter 1 : Introduction

Chapter 2 : Finite Automata .

Chapter 3 : Regular Expressions and Languages

Chapter 4 : Context Free Grammars (CFG)

Chapter 5 : Pushdown ·Automata (PDA)

Chapter 6 : Regular Grammar (RG)

Chapter 7 : Turing Machine (TM)

Chapter 8 : Undecidab _illty and Recursively Eni.Jm~rableLanguages

Scanned by CamScanner
Table of Contents ·
" . • Index ..

• Syllabus
'
• Chapter 1 : Introduction TCS-01 to TCS--01

• Chapter 2 : Finite Autom 'ata TCS-02 to TCS-13

• Chapter .
. . 3 : Regular Expressions and Languages TCS-14 to TCS-22

• Chapter 4 : Context Free Grammars (CFG) . TCS-23 to TCS-25

• Chapter 5 : Pushdown Automata (PDA) TCS-25 to TCS-36

• Chapter 6 : Regular Grammar (RG) TCS-36 to TCS-44

• Chapter 7 : Turing Machine (TM) TCS-44 to TCS-56

• Chapter 8 : Undecidability and Rec_ursively Enumera~le Languages

TCS-56 to TCS-59
, .
• Dec. 2018 . D(18)-01 to D(18)-16

• May 2019 M(19)-01 to M(19)-11

• University Question Papers Q-1 to Q-3

Scanned by CamScanner
·SYLLABUS

1.0 Basic Concepts and Finite Automata

1.1 • Alphabets, Strings, Languages, Closure properties .

• Finite Automata (FA) and Finite State machine (FSM) .

1.2 Deterministic Finite Automata (DFA) and Nondeterministic Finite


Automata (NFA): Definitions, transition diagrams and Language recognizers

• NFA to DFA Conversion

•· Equivalence between NFA wi(h and without£- transitions

• Minimization ofDFA ·

• · FSM with output: Moore and Mealy machines, Equivalence .

• Applications and limitations of FA

2.0 Regular Expressionsand Languages

2.1 • Regular Expressio ili (RE)

• Equivalence of RE and FA, Arden's Theorem

• . RE Applications

2.2 • Regular Language (RL)

• Oosure properties of RLs

• Decision pr_operties of ·RLs

• Pumping lemma for RLs ,

3.0 Grammars

3.1 • Grammars and Chomsky hierarchy.

3.2 • Regular Grammar (RG)

• . Equivalence of Left and Right linear grammar

• Equivalence of RG and FA

3.3 Context Free Grammars (CFG)


• Definition, Sentential forms, Leftmost and Rightmost derivations, Parse tree, Ambiguity.

• Simplification and Applications.

• Normal Forms: Chomsky Normal Forms (CNF) arid

• Greibach Normal Forms (GNP).

• CFLs - Pumping lemma, Closure properties

Scanned by CamScanner
• I
4.0 .PushdownAutomata(PDA)
/

4.1 · • Definition,Transitions ,Languageof PDA

• Language acceptanceby final state and empty stack

• PDA as generator: ~ecider and acceptorof CFG.

• DeterministicPDA , Non-DeterministicPDA
.. Applicationof PDA.
5.0 Turiilg Machine ·(TM)

5.1 • Defmition, Transitions

• Design of TM.as generator,decider and acceptor

• Variantsof.TM : Multitrack,Multitape

• UniversalTM.
. Equivalenceof Single and_Multi Tape TMs . .

·• Applications,Power andLimitations ofTMs


.. -Context Sensitivityand Linear Bound Automata.
-6.0 ·Undecidability
tu • Decidabilityand Undecidability

• 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

O. 1 Write note on.Chomsky Hierarchy. Q. 2 State applications of Finite Automata In brief.

MU - Dec. 2009, Dec. 2012 . Ma 2013, Ma 2014, mtWMt••.


Dec. 2014, Ma 2015. Dec. 2016 . Ans.:
I •
'. A~pllc~tlons of Finite Automata
Ans. : Chomsky Hierarchy
Finit.e_automata ar~ used for solving several common types of
A grammar· can be classified on the basis of production rules. computer algorithms. Some of them are :
Chomsky classified grammars
into the following types :
. (i) Design of digital circuit
.1. Type 3 : Regular grammar ·
. (ii) String matching
2. . Type 2 : Context free grammar
·3_ Type l : Context sensitive grammar (iii) Communication protocols for informationexchange.
4. Type O·: Unrestricted grammar. (iv) Lexical analysis phase of a compiler.

1; Type ·3 or Regular-Grammar Finite automata can work as an · algorithm for . regular


_language. It can be used for checking whether a string WE L, where
A grammar is called Type 3 or regular grammar if all its
L i,s a regular language.
productions are of the following forms :
E Q. 3 What Is Finite Automata?
a Ans.:
A aB
Finite Automata
Ba
Finite automata are also called a finite·state machine.
Where, ae Land A, Be V.
A finite state machine is a mathematical model for actual
A language ·generated by Type 3 grammar is ~nown as
physical process. By considering the possible inputs on which
.regular language.
these machines. can work, one can analyse their strengths. and
2. Type 2 or Context Free Grammar
weaknesses.
A grammaris called Type 2 or"-context·free grammar·if all its Finite automata are used for solving several common types of
productions are of the following form A a. where A E V and
cqmputer algorithms. Some of them are :
ae (VuT) *.
l. Design of digital circuits.
V is a set of variables and T is a set of terminals.
The language generated by a Type 2 grammar is called a 2. String matching.
context free language, a regular language.but not the reverse.
. ' '
3. Communication protocols for information ex.change.
3. Type 1 or Context Sensitive Grammar 4. Lexical analyser of a typical compiler.
A grammaris called a Type 1 or cont.extsensitive grammar if Q, 4 · Define the term : Unrestricted grammar
all its productions are of the following form. .
a 13
Ans.:
Where, is atleast as long as ex.
Unrestricted grammar
4. Type O or Unrestricted Grammar
Produc_tions can be written wit.bout any restriction in a
Productions can be written without any restriction in a unrestri~ted grammar. If there is production or' the a 13, then
unrestricted grammar. If there is production of .the a p, then length of ex could be more than length of l3.
length of a could be more than length of ll
Every grammar also is a Type O grammar.
Every grammaralso is a Type Ogrammar.
A Type 2 grammaris also a Type I grammar A Type 2 grammaris also a Type 1 grammar

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

0.1 Write sho~ note on Mealy machine.


Chapter 2 : Finite Automata
An arc from state in a mealy machine is associated with :
-· I

Ans.: t. Input alphabet e l:


2. An output alphabet E 0 .
Mealy_Machine
An arc marked as 'a/0' in Fig. 2.1 implies that:

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
.

Theory of Comp ·sci. (MU-Sam. 5~Comp.)


'
TCS-3

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).

Odd Odd Ans.:

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

(iii) Communication protocols for infonnation e~change.


· all trailing O's are wn'tten
I.e., as 1 and the·first 1 is written as 0. .

(iv) Lexical an~ysis phase of a compiler. Moore machine: ·

Finite automata can work as an algorithm for ~gular


language. It can be used for checking whether a string we L, where
L is a regular language.

0. 8 Design a DFA to accept strings over the alphabet


set {a, b} that' begin with 'aa' but not end with 'aa'.

Ans.:
Fig. 2.9

a. 10 Design minimized DFA for accepting .strings ·,:


ending with 1oo over alphabet (0, 1). ·
Ans.:
All strings ending in 100: ·

Toe substring '100' should be at the end of the string.


Transitions from q3 should be modified to handle the condition that ,.

the_string has to end in '100'.


Fig. 2.7
l 0

A string not starting with aa will ·reach lhe dead state . qi

A string starting with aa will reach the state q2 • qi qi q2

A string starting wi~ aa and not ending in aa will be either in q4 q2 qi q3


0
q3* qi ~ -
or4s .
(a) State transition diagram (b) State transition table
The DFA is given by. Fig. 2.10
,M = ({4,.q1,'h • <h•'ti•qs, qtl, {a.,b},6,'lo, {q4,,4s}) q3 to q1 on Input 1 :

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',

Q. 11 Design Moore Machine t . generate output A If _


string la ending with ·ebb, B ·If string ending with
aba and C othe!'Wlse over alphabet (a, b); and ·,
convert It to meaty machine. · · ·:

Flg.2.8

easv-solulions

Scanned by CamScanner
.·TCS-5

Ana.: a: 12 Convertfollowl_ng_e•NFA to NFA without e.


-· ·. Design of Moore machine

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

(a) Duplicate transitions of r state on q state


J'ig.2.11 (b) Since r is the final state, we make q as well as the

Conversion Into ~ealy machine : final state .


Step 2 : . To remo ; e e transition from p state to q state · do
.Step l: Construction of a trivial Mealy machine by mo~ing •
following :
·output associated ·with a state to transition entering
into ·that state . (a) Duplicate the transitions of q state on p state
·a b (b) Since q is a final state we make pas well as the

<Jo q,.c <Jo, C final state .

qi q,.c (h,C 'fhius, the NFA is :

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

'h 41,B (b,A

1lJ 41,C (lo, C Fig. 2.1S


The two state <Jo, q 3 can be merged in~ a single state, say CJo· a.·13 Design the DFA to accept the language containing
a b all the strings over = {a, L b. c} that starts and
<Jo 4,,c . <Jo, C ends with different symbols.
41 4,,c (h,C Ans.:
41,B <Jo, A L, ~. q0, F}
M = (Q,
The final Mealy machine is Q = ·{go, Q1, 'h, QJ, 44, 'ls, 46• q,}
b/C a/C
L = (a, b, 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

a. 14 Convert the following grammar Into finite 'lo 0 00 + 5 = 0('lo) 01 + 5 = l (Cit)


automata.
qi 10 + 5 = 2 ('h) 11 + 5 = 3 (<IJ)
s ax I bY I a I b
Ch IO 100+5=4(<4) 101 +5=0(CJo)
I bY I b
'b 11 110 + 5 = 1 (qi) 111 + 5 2 (42)

Ans.: q4 ' 100 1000 + 5 = 3 ('h) 1001+5=4(qJ


· The above grammarcan be convertedto FA as follows : The operator+ is f?r reminder.
· For every non tenninating symbol we consider it as a
0. 16 Design mealy machine to find out 2's complement
different st.ate of a binary number.
M = {Q, l:, 6, S, F} Ans.:
Q = {S, X, Y} 2's complement of a binary number
l: = {a, b} 2's complement of a binary number can be found by not
s = initial st.ate changing bits from right end till the first •1• and then
F = {X. Y} comp~ementingremaining bits. For example, the 2's complement
of a binary number O101101000is calculated gi ve!!_.,below: .
0 1 0 1 1 0 1 0 OO
Fig. 2.17 L I II I J =>1010011000
Complement No change
6 : TraJ)sition functions are : every bit .

6 (S,a) => X Fig. 2.19


6(S, b) Y·

(ii easv - solut1ons

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

-41-~--~f Fig. 2.20


state 'lo

Q, 19 Design a mealy machine to determine the residue


· mod 3 of a binary number.
Ana.:
Q._17 Convert the following NFA to an equivalent DFA 0

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.

a :. RequiredR.E. = (0 + 1 (1 + 01)* 00)*


Fig. 2.21
a: 20 Convertthe following NFA to an equivalentDFA
e -closure of states :
'lo (Clo) qo { qo , q1} q1 {}
qi (q1) q1 { qz} { qt, qz} {}
Ch (q1,(b)
*q2 { qo} { q2} {q1}
NFA to DFA using direct method.
Ans.:
e - closure of states

State e- closure

4o { CJo}

qi { q1}
Ch {q1,Chl

ConstructingDFA using the direct method


Fig.2.22 Step 1 : Transitions for the state {q0 }

Q.18 Design a . DFA oyer an alphabet l: = {a, b} to


. recognize a language !n which every 'a' Is
_followed by 'b'
Ans.:

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

Step 3: . Writing transitions for the s~te {q0, qi)


(a) State transition diagram (b) State transition table
,' . Fig. 2.26: Final DFA for Q. 2l(a), without a
failure/ dead state

(b) All strings with abb as a substring


The machine will _have fours states :
State 'lo -It is th~ starting state and indicates that nothing _of
relevance to complete 'abb' has been seen.
Step 4: Writing transitions for the ·states {q1, q2 } and
State q - preceding character is 'a' and 'bb' is required to ·
{Qo, qi' (h} 1
complete 'abb'.
State q • - Preceding characters are 'ab' and 'b' is requiredio
2
complete 'abb.'
State q , - Preceding characters are 'abb' and the substring
3
'abb' has been seen by the machine.

a b

a. 21 Draw DFA for the following language over {a, b} : a


(a) All strings starting with abb.
q3* Q3 q3
(b) · All strings with abb as a substring i.e., abb
anywhere In the string. . (a) State transition diagram (b) State transition table ·
(c) All strings ending in abb. - M'E!ID Fig. 2.27: Final DFA for Q. 21(b) .
Ans.:
'lo to 'lo on inp!Jt 'b' :
(a) All strings starting with abb
First character in 'abb' is a.'
First input as 'b' will take the machine to a failure state. 'lo to q1 on input 'a' :
First two inputs as 'aa' will take the machine to a failure state.
q1 is for preceding characters as 'a' . first character of abb.
Fust three inputs as 'aba' will take the machine to a failure siate.
q 1 to q1 on input 'a' :
First three inputs as 'abb' will take the machine to a final state.
An input of 'a' in state q1 will make the preceding two
a b characters as 'aa' Last • • -11 .
· a w1 still constitute the first 'a' of
qi nbb.
CJ+
q 1 to q2 on input 'b' :
' qi q2
q2 is for preceding two characters as 'ab' of 'abb' .
q2 qt q)
q2 to q1 on input 'a' :
. 3
q• Qj q3 An input 'a' in q2 WI-11
a,b make the preceding three c~ters as 1 •
'aba'. Out of the three h
CJ+ CJ+ , ,. c aracters ,aba , , only the· last character .·· . .-
a 1s relevant to 'abb'.
· (a)~tate transition diagram (b) State transition table
' Fig. 2.25: Final DFA for Q. 2l(a)

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

. · As the substring 'abb' should: be at the ~nd ~f the string. Remainder 10 1 0, 1


(
Transitionsfrom q3 should be modified to handle the condition that next Input 1

the string has to end in 'abb'. Remainder 1 0 1 0 1 (


next Input 1
a· b
Remainder 10 ,
1 0 '
0
a
next Input 1
41 CJo
Remainder 1 (11 )2 MOD 3 = (0)2 ·
41 qi 42 next Input 1
X X
q2 qi . q3
.b • Fig. 2.29
q3 qi ~ -

The calculationof next remainder isshown below,


(a) State transition diagram (b) State transition table
Fig: 2.28: Final DFA for Q. 2l(c) . Previous Next Calculation of Next
%to q1 on input a: remainder input remainder remainder

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)

Q.25 Construct th e mlnlmum state automata equivalent


to given DFA. IW'B!1III
·o 1
q1 qo
q, qo q2
q2 q3 q,
Fig.2.31 q3• q3 qo
q4 q3 q5 _
a. 24 Design DFA that accepts the ·fol_lowlng language: ..
(I) Set of all strings with odd number of 1's q5 q6
followed by even number of O's I= {O, 1}, q6 qs q6
(II) Set of all strings which begin and end with q7 q6 q3
different letters I= {x, y, z}.
Ans.:
(Ill) Strings ending with 110 or 111.
Step 1: Finding O-equivalence partitioning of states by
Dec_2006.Dec.2009,Dec . 2010
putting final and non-final states into independent
Ans.: block.
(I) Po = (<Jo, q1, q2, ~• qs, '4• q/ (q3)
block 1 block2
Step 2 : Finding I-equivalence partitioning of states by
considering transition on 'O' and transition on 'l '.

0
block 2

On input 0, block 1 is successor of <Jo• qi' q , ~• q.,.


5
Fig. 2.32(a) On input 0, block 2 is successor of q , ~-
2
:. q2, ~ -are distinguishable from CJo, q q , ~• q
(II)
(qQ •
Lf
t . ,14 , ~j , q~ , q 7)
Lf L
1

(q~.-L_T
+
5 7
·t1
ransi on on
1

block 1 block 2

On input 1, block 2 is successor of qr


On input 1, block 1 is successor of a _ q q a. q a_
~' 1 2' ~' 5 ' ~-
q7 is distinguishable from <Jo• qi' q2, ~· q • ~--
5
Pi= (CJo, qi' qs• ~) (qz, ~) (q1) (q3)

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&ltlon 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&ltlon 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

ron+ 1 is a string of length (n + 1). 'a' is an arbitrary alphabet from


(qi. qJ (<Jo.~ (~.q.) :E,·

(q3) (q,;qJ ~(q 2, ro0 ) = 62(<io, w0 ), where~ is transition functionofDFA


(ql' q.)
and 6, is transitionfunctionof NFA (M 1) .
((b)* (qJ (CJo,
If the subset reachedby NFA is given by
(q,,) (~.~) (q3) {pl' Pt ... pk}
k
(a). State transitiondiagram for minimum-
then, ~(CJo, (l)n + 1) = U Oz(p;,a) •.. (i)
i= I
k
or~({p 1, p2, .. . Pt}• a) = U 61(pi' a) .•• (ii)
i =1
also, ~(<Io· ron) = {P.,'P2, •• • Pt} .. .(iii)

from (i), (ii) and (iU) we get,


~(<Io, con+ 1> = (CJo, ron)• a) •
= ~({pl' P2• ... Pt}• a)
k
(b) State traositi~odiagram for minimum-stateDFA state DFA = U 61(p1, a)= &,(<IQ, Sn+ 1)
Fig~l.34 . i=1 ·
Thus, the result is true for I ro I= n + 1, hence it is always true.
easv-solutions

Scanned by CamScanner
.• ..
. •' ·

_..Theoryof Cotnp. Sci. (M U-Sem. 5-Comp.)


the followlng NFA to a · DFA and :: ~,..
Q. ·21 Convert ~-..
)

.
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

6({p,q,r,s}, 1)=6(p, 1)u6(q , l)u6(r, 1)u6(s, 1) NFA to DFA conversion:

= {q} u {t) u {t}u Step 1 : {<Jo} is taken as first subset

= {p, t} a-successor of {<Jo} = •6( <Jo, a) = {'to• q 1 }


. 6 ({p, t), 0) = 6(p, 0) u 6 (t, 0) b-successo rof{CJo} = O(<Jo,b)={<Jo}
= {p; q) u = {p; q} Step 2 : A new subset { <Jo, 4 1 } is generated . Successors of
6 ({p, t}, 1) = 6(p, 1) u 6 (t, 1) { CJo· q 1 } are calculated.
= {p} Uq>= (pl 6({CJo,q1 },a) = 6<CJo,a)u8(q 1,a)={<Jo,q 1 }u¢,={<Jo , q 1}
No, new sub~t is generated . Every subset containing _either s . 6 ({<Jo, q 1}, b) = 6(CJo, b) u 6 (qi' b) = {<Jo} u {4 } = {CJo,<Ji} ·
2
or t is marked as a final state . .
A new subset {Clo• 4 2 } is generated. Successors of.
Step 3 :
Informal Description:Strings over {O, 1) with second digit {<Jo, 42 ) are calculated. ·
from the end is 0. 6 ({CJo, q2 }, a) = 6(-~, a) u 6 (q2, a)= (<Jo, q } u {q }
1 3
0 I
1
"' {CJo, qi. q3)
(p. q) {p}
6 ({CJo, q2}, b) = o{tJo, b) u 6 (q2' b) = (<Jo} u ~={<Jo}
{p, q} (p, q, r, s} {p. t}
Step 4: A 'new s~bset {<Jo, q 1q 3 } is generated . Successors of
{p, q, r, s}• {p, q, r, s) {p, t}
{<Jo, q1q 3 } are calculated. :- .
{p, t}• (p,q} (p}
6((CJo, q~q~}. a) == 6(CJo, a) u ·6 (q , ·a) u 6 (q3, a)· '. ·.·
a) State table
1
\ . ,.
= ( <Jo, q I} U q> U q> = ( <Jo• qi} ' .:·
··' :- ,, .
... . \ .
easv - solUIIOIIS

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

No, new subset is generated. Every subset containing q3 is


·marked as a final state. In decimal
~7 In ternary 1

s~stem system
1

(b) State diagram of the DFA

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

(c) sta·te table of the DFA Ans.:


Fig. 2.36 0/0 , 1/0 1/0

· Q. 29 Give Mealy and Moore machine for the following :


From Input };*, where }; = (0, 1, 2) print the residue
modulo _5 of the Input treated as ternary (base 3). 0/1
Ma 2006. Dec . 2015 {a) State diagram
Ans.:
0
(a) Mealy machine

'h 4>. 1
214
(b) State table
Fig. 2.38

. Meaningof various states :


·110 <Jo- start state
Fig. 2.37(a) : Mealy machine
q 1- previous·symbol is 1

Meaningof variousstates is : Qi"". precedingtwo symbols are 10


<Jo - Run_ning remainderis 0
A transition from to <Jo will ';11ake the preceding three
q1 - Runnin~remainderis 1
symbol as 100 and hence the output 1.
q2 - Runningremainderis 2.
q3 - Runningremainderis 3 = (10)3

casv-s01111ions

Scanned by CamScanner
'TCS-14
'1r111eory of Comp. Scl..(MU-Sem.5-Comp.) a '·

. .Jz with i = o, at least one ·'a' or atleast · · ·


Q. 1 Write short note on Myhlll-Nerode theorem. Step 3: In the stnng xy n + 1 n + 1 n •
'b' will be erased from (ab) of (ab) a • ,
one .
Dec . 2005, Ma 2006, Dec . 2006 , Ma 2007 . ·. . d to one of the following situations : .
This w1111ea . . . .
Ma 2008, Dec . 2008, Dec. 2012. Ma 2013
N umber of a's in (ab)n is equal to number of a's .
L · ' Jc · n ·k .
Ans.:
in a of(ab) a -
' n k
Myhlll-Nerode theorem xy°z will not be of the fonn (ab) a ·
2_
0 .
Given a language L, two strings x and y are said to be in the Therefore, xy z e L.
same class if for all possible string's z either both xz and y~ in L are Hence, this is proved by contradiction.
I

. or both are not


The Myhill-Nerode theorem says : Q.4 Write short notes on closure ~-rtles of Jular
. 1. A language L divides the set of all possible strings into
languag~. · 1•)444,M:£~•iil•J§_,,ej
. mutually exclusive clas.ses. Ans.:
2. If L is regular, the number of classes created by L is finite. · Closure pro~rtles of regular language ·

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

Machine M3 is constructed to accept L(M1) u L(Mi ·


r--,F
M3 = (R, :t. ~. ro, H) where r0 is a new start state. Two
@)
s-moves,one from r0 to So and another from r0 to g0 are added. ·
R = SuQv{r 0 } ©.
@
H = FuG
'-""
= 61 v 62 v {(r0 , e, so), (r0, e, <Jo)}
Machine M3 can non-deterministically choose either M1 or
M2• Therefore,
L(M3) = L(M,) U L(MJ
' Fig."3.3 : is constructed such that L(Mz) = L(M 1)*

2. Regular Language Is Closed under Concatenation M2 is constructed·as given below :


Let M1 = (S, l:, 61, So, F) (a) A new start state s0 is added with an e-move from s0 to <Io·
and M2 = (Q, l:, ~. <Jo, G) be two given automata · (b) A new final state f0 is added with £-moves from every state of
F to f0 , An £-move is added from s0 to f0 as £ is a member of
To prove that closure property under concaten~tion,·we must
L(M,)*.
. show that there is another machine M3 such that L(M 3) = L(M1) •
L(M 2). The .construction ofM 3 is shown in Fig. 3.2. Machine Mi :;;; ~Q v (s0 , f0 }, 1:, 6, so, {f0 })
Machine can accept a string e L(M 1) and resume back from
the start state q0 through the £-move from f0 to _
<Io· Thus accepting
. L(M1):- .

----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)

Given Machine after


. machine complementation

If M is a deterministic finite automata then M can be


constructed by interchangingfinal and non final states ofM.

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•

Q. 5 Design a NFA to accept (a + b}*aba convert It to a Ans.:


reduced DFA. -. . · . : · em&m
Step 1 : . Let ~s assume that L is regular and L is accepted by ·
Ans.:
an FA with n states.
{a+ b)* aba
· Step 2: . Let us choose a string
REtoNFA
(.I) = (ab)n+la•
~ -
I c.o I = 2(n + 1) + n = 3n + 2 n

Let us write ro as xyz, with

lyl > 0

· and I xy I n
=
The string xy will contain a maximum of n.symbols from (ab)". ·

= Step 3 : In the string xy;z with i = 0, at least o~e 'a' or atleast ·


1
one 'b' will be erased from (ab)°• of (ab)"+ 1a".
Fig. 3.4 : RE _
to NFA
11ris will lead to one of the following situations :
NFAtoDFA . 1. Number of a's in (ab)° is equal to number of a's
in a1' of (ab)"a1'.
0
2. _xy z will not be of the form (ab)°a1',
Therefore, xy°z e L.

b . Hence, this is proved by contradiction.

· Fig. 3.5: NFA to DFA a. 8 Construct a NFA for the RE (01* + 1) and convert It ,

Write RE for the followlng languages


to DFA. . · ..14.1.jnll· ·<
Q.6 Ana.:
(I) The set of all string over {O, 1} without
(01* + 1)
length two.
(II) L = {a"b_mI (n + m) Is even} RBtoNFA

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

Fig. 3.6(b): NFA to DFA (ii) I xy I~ n


1
(iii) xy z EL for all i 0 here yi denotes that y is repeated or
Q. 9 Construct an NFA with · £-moves for the
pumped i times.
RE 10(0 + 01 + 0110)* llith&D
Interpretationof Pumping Lemma
Ana.:
a

Fig, 3.8 : FA considered for interpretation of pumping lemma

· Let us consider the FA of Fig. 3.8


No. of states = 5 (Clo to '14)
Let us talce a string w with I w I~ 5, recognized by the FA.
w :a: abcabcb
To recognize the string w :a: abcabcb, the machine will transit
through various states as shown in Fig. 3.6.2.

. q1 Is repeating

Input A •
I a/2 c~b c b
r-qo-..q1-+q2-+q3-+q,-+q2-+q3-+q4
States ·

Fig. 3.7 Fig. 3.9 : Transitionsof FA on Input abcabcb

As the input abcabcb takes the machine through the loop


q1 Ch Cb q1, this loop can repeat any number of times. In
tenns of abcabcb, we can say.that if abcabcb is accepted by FA
easv -solutions
I •

Scanned by CamScanner
' I

.'1tTheoryof Comp. Sci. (Mu~sem. s~comp.)


then every string in a(bca)*bcb will be accepted by ·the·FA of Q. 12 Expialndeclsl~n propertiesfor regular languasiee.:·
Fig. 3.8. The portion bca is input during the loop.
4t <h 'h q_.• .
Ans.: .
Thus, if ~bcabcb is accepted by the FA then abcabcb can be
-'Decision.propertiesfor regular languages
writtenanyz, with
t. Is a regular ~t empty 7 - Emptiness property' .
X = a
2_ Whether a finite automata accepts a finite number of strings ? •
y = bca
- Finiteness property.
Z· = bcb. 3. · Whether a finite automata accepts an infiilite number of .
·Length of abcabcb is n strings ? - Infiniteness property.
xy;z for every i 0 or a(bca)'bcb for every i 0 will be In addition to above decision problems, we can formulate a
accepted by the_F~ of Fig. 3.8. number of other decision problems. Some of them are :
1. Given a regular ~xpression Rand a string ro, does ro belongto
0.11 Construct Nf:A from {O + 1)*(00 .+ 11) and convert L{R)?
Into minimized DFA. · ·· ·2. Given two FAs M 1 and~. is L(M 1) = L(MJ 7
Ans.: .
3. Given two FAs M 1 and~. is L(M 1) subset ofL(MJ? ·
(0 + 1)*(00 + 11) 4. Given an FAM, is Ma minimum state FA accepting L(M}?
REtoNFA DecisionAlgorithmfor emptiness :
·~
Finite automata will fail to accept any string if it does not
have a final state.
=
Finite automata will fail to accept a string if none of its
Fig. 3.10 : RE to NFA accepting states is reachable from the initial state.
We can determine the emptiness of language accepted by an
FA by calculating QI<' the set of states that can be reached from 4i
= by using strings of length k or less.

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. · • ' . .

Q. 13 Using pumping lemma tor re·gular sets, prove that .


ttY
X · Y Z .
the language L = {ox,l Im e .·{o; 1}i la not regular. Step 3: Let us check whetherx~z for i = 2 ~longs to L.
2 ·
xy z a1a2tan-a-rb a"b.
Ana.: - a"+'ba"b
Step 1 : Let us assume that
L is regular and L is accepted by a Since r > 0, a" + 'bi'b is not of the form axil as the
FA with n states. number of a's in the first half is n + r and in the
Step 2 : Let us choose a string second half is n.

0>= anb ba" Therefore, xy2z e L. Hence by con~ction, the


'-r'-v-' ' given languageis not regular.
ro ro R :+-- from
R
Q)(l) Q.15 Show that the language L {anba" I n > O} Is not
1ml = · 2n+2~n regular. - Dcc. 2009.Dec.201 1
Let us write w as xyz with Ans.:
I y I > ·0 and · I xy I , S n Step 1 : Let us assume that L is regular and L is accepted by
9 an FA with n states.
Since I xy I S n, x must be of the form a • '- ·
Step 2 : Let us. choose a string _
Since I xy I S n, y must be of the form ar I r > 0.
. ro = a"ba"
·Now,
m = a"bba n = a,, a' an-s-rbba" lrol = 2n + 1 n

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.

Step 2 : Let us choose a string


m =anb Ans.:
--;r' ro .- from roro
Application areas of Regular Expression
1ml = 2n+2<!:n
1. R.E. in Unix
Let us write (J) as xyz with
The UNIX regular expression lets us specify a group of
lyl > 0 characters using a pair of square brackets [ ]. The rules for
and I xy I S n , , I
characterclasses·are :
SinceI xy I s n, x must be of the form a • 1. [ab) Stand for a + b
Since I xy I s n, y must be of the form a' I r > 0. 2. [0-9) Stand for a digit from Oto 9
3. [A-Z] Stands for an upper-caseletter

easv-solllllOIIS

...__ ,;

Scanned by CamScanner
'1t.Theoryof Com~. Sci. (MU-Sem. 5-~o~p.) TCS-20.- ·

4. [a - zl · Stands for a lower-case letter Q. 17 Design a DFA correepond_l~g to the.regular · . i


expreHlon. (a +.b)* aba (~ + b)
5. [0 - 9A-Za- z) a
Stands for letter or a digit.
\
Ans.:
The grep utility in UNIX, scans a file for the occurrence of a The language associated with the R.E. (~-~ b)•~a(a + b)• = ·
~ttem and displays those !in~ in which the given pattern is 'strings with."~a" as substring.
found.
DFA for-strings with aba as substring.
For example :
· $ grep president emp.txt

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 ~

10. pat $ Matches pattern at end of line. ,'


I
I
I

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).

P (I + m) is nota prime as ·it has two factors p and


(I+ m) and
Ip I > 1,
11 +ml >
1
So xyP + _z eL Hence by contradiction the given
language is not regular.

Q. 21 Draw a state diagram and construct a regular


expression corresponding to the following state
Fig. 3.13 transition table. M4¥-Niii
State 0 1
{l;lote: States have been removed.)
q, q2
Step 2: e-NFA to DFA
q2 q3 q2
e -closure of states
q3 q, q2
{<Io, (h, (b}
Ans.:
{Ch, 'bl, State diagram
The DFA using the direct method is given below.
0

0
R.E. form state diagram
Step 1 : Remoyingloop between 'h and 'h we get

Fig. 3.14

Q. 20 Using pumping lemma for regular sets, prove that


the language, L = {O" I n Is a prime} Is not regular.
0
Dec . 2007. Dec . 2009. Dec . 2015, Ma 2016

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

· Step2: Let us choose a string w = aP, where pis a prime and


p>n. Q, 22 Show that the language L = {a"b"} Is not regular.
10>1 = laPl=p~n Dec . 2006 , Ma 2010. Dec. 2010. Dec . 2012. Ma 2013.
Let us write w as xyz with Mn 2014, Dec . 2016 . Mn 2017, Dec. 2017

easv-solulions ·'

Scanned by CamScanner
I •

-Theoryof Comp.·set. (MU-Sam.5-Comp:)


Ana.:
Step 1 : Let u~ assume that L is regular·and L Is acceptedby a
FA with n states.
Step 2 : Let us choose a string
ro = anbn
lro I = 2nc!:n
Let us write w as xyz, with
lyl > 0
andlxyl s n
Since, I xy I S n, y must be of the fonn ar I r > 0
Since, I xy IS n, x must be the fonn a1 • ·

Now, a'l,° can be written as


as ar -an-s-rbn

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.

a. 23 Construct NFA for given regular expressions: Step 2 : E -closure of states :


(I) (a+ b)*ab qo { ~ . qi' 4i }
(II) aa(a + b)*b
q1 { q1, Cb}
· (Ill) (aba) (a+ b)*
(Iv) (ab/ba)*l(aa/bb)*
Ans.-:
Step 3 : DFA using direct method :
(I) (a+ b)*ab: NFA
0,1

(ill) (aha) (a+ b)* : NFA

casv -sotu11ons

Scanned by CamScanner
.,,.Theoryof Comp. Sci. (M~-Sem~5-Comp,) TCS-23

-
a. 1
Chapter 4 : Regular Grammar (RG}

Construct right llnear grammar. and. left · unear


·
Step 3 : A dead state is added to handle ~sition. Toe
resultingDFA is shown in Fig. 4.l(c).
grammar for the language (ba)*,
An••:
Transitionsystem for (ba)* is given by :

a,b

We can write left linear grammar and the right linear


grammarforin the transition systems.
Right lineargrammar:
S baS I£
Fig. 4.l(c)
Left lineargrammar:
a. 3 Construct left linear and right linear grammar for
S Sbal £
. the regularexpression.
Q. 2 Rnal the equivalent DFA accepting the regular «01 + 10>•11>•00>• WWW
language defined by ·the right linear grammar Ans.:
given as: ·
The given expression can be represented using a transition
I be I
system as shown below :
iD1!EltwJ' ((01 + 10)*11*)00
Ans.:
A new final stateJt is being introducedto handle productionslike,·
Q
-1
Step 1 : . Adding transitions · corresponding to every (01+10)*11
production,we get the FA shown in Fig. 4.l(a).
a

= =
00

Fig. 4.l(a)

Step 2: Drawing~ equivalent DFA, we get :


----.b
= =

Fig. 4.l(b)

easv - solulions

Scanned by CamScanner
Theo~ of Comp. Sci. (MU-Sam. 5-Comp.)

Removing e - transitions, we get : Step 1 : Adding transition~corresponding to every


production, we get
·b

Fig. 4.3(a)

. Step 2 : Adding a state E to handle <>-transitions,we get the


Fig. 4.2(1:>)
final DFA.
Writing of right linear grammar we get,
S OOS 11 lA I OIB 110B It
A -o+ llA I OlB I 10B I 00S a,b

B OIB I 10B I llA


. .
For writing of left linear grammar, we interchange the start
state and the final state and chan·ge c:iirectionof all trans~tions.The a

resulting transition system is given by : Fig. 4.3(b): Final DFA

Q.5 Convert following RG to DFA


I1B, I 1AI O,
. B 1B I _1A 11,_ C OA.
Ans.:
A new final state F is being introduced to handle productions·like, .
B-d,
Step 1 : Adding transitions corresponding to every
production, we get the FA shown in Fig. 4.4(a).
Fig. 4.2(c)

Writing of left linear grammar we get,


S SOO I AOO I t
IS11 .
B B01 I 10B I SOl I SlO I AOl I AlO

Q. 4 Convert the following right-linear grammar to an


equivalent DFA. Fig. 4.4(a)

Step 2: Drawing an equivalent DFA, we get


1

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)

· Step 3: Writing of left linear grammar from the transition


. I .
Fig. 4.4(c): Final DFA . system, we get :

Q. 6 Write an equivalent left linear grammar from the S CO I AO I B1


given right linear grammar. · · Al ICOIBI 10
. 18 .B BIil
C AO.

Ans.:
Step 1 : Transition system for the ~ven right linear grammar
· is as shown in Fig. 4.S(a).
0

Fig. 4.S(a) : Transition gra.ph

Chapter 5 : Context Free Grammars (CFG)

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

Ans.: a where loo 2 should be a Variable.


This can be done by addi
·GrelbachNormal Form (GNF) . ng two Productions :
c, a ,
A context free grammarG = (V, T, P, S) is said to be in GNP and c;, b
if every production is of the fonn :
A aa, . '
easv-solutions

Scanned by CamScanner
'1 Theory of Comp. Sci. (MU-~em. 5-C~mp.)

Tho_set of productionsofter tho nbovo changes ls Ans.:


S ASBIAB (i) String contain_ingalternate sequence of O's and
A AC1S I AC1 I n
l's,L= {O, n
B sc;,s I sc;, I c;,s I b 1·Ac.s I AC. I n I cbcb
Sirice, nny binary number will satisfy the condition of
C1 a
c;, b
=
alternate sequence of O's and l's, the language L (0 + I)*
The set of productionsare : ·
Step 3 : Finding nn equivalent CNF
.. ,. . . -,,.. ·' .:~ i',M-'1.1 S OS 11S le
.OrlslnalproducUon ,t
CFGG =
(ii) The string containing no consecutive b's bu( a's can be
consecutive.
The set of productionsfor the given language Lare :
p = {
S aS I bX I b I e
X aSla

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

B AC1 I a I c;,c;, B AC.I al cbcb


a

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 .. .

Set of variables V = {S,X,A,B} ' IBiiJq.


Set of terminals T = {a,b} Ana.:
Start symbol ='S 1. Let us assume that L is CPL.
onn h th
Let us pick up a word w = a b c w ere e constant ,n ia
o:8 Convert the given grammar to GNF. 2.
given as per the pumping lemma.
s SSlaSblab
Ans.: 3. w is rewritten·as uvxyz.
Where I vxy I n and v • y e i.e., both v and Y are not nnIL
Step 1 : Other than the first symbol on the RHS of every
production, every symbol must be a variable. 4. From pumping lemma, if uvxyz e L then uvixt z is in L (G)
We can make the substitu.tion X for b. for each i = 0, 1, 2, ...
There are two cases :
The resulting set of productions after the above
substitution is : Case I: vy contains all three symbols a. b and c.
S SSlaSXlaX If vy contains all three symbols a, b and c then eitber
X v or y contains two symbols. The exact orderingof a,
b and c will be broken in uv2xy2z and hence
Step 2 : Removing left recursion from s-production, we get :
uv2xy2ze: L(G)
S aSXS11aXS11aSXlaX
Case II : If vy does not contain three symbols a, b and c then
S1 _ SS 11S 2 2 ·
uv xy z will have unequal number of a's, b's
2 2 -
X and e's and hence uv xy z E L (G).
Step 3 : Sj-productions ru:e not in GNF. They can be brought Hence, it is proved by contradiction.
to GNF by substituting S.
S aSXS11aXS11aSXlaX
a. 11 . Convert the following grammar to CNF S AACD
aAb I e C aC I a aDa I bDb I e
S1
X
aSXS1S11aXS1S11aSXS11aXS11aSXS1iaXS11aSXlaX
b Ans.:
w
a. 9 Prove that L ={0 11 2 'i 11 >= 1 and J> =1} Is not
1 1
- First of all, the grammar must be simplified .
~ntext free. ••MW4mtJ Step 1 : Removing null productions.
Ans.: Nullable set = {A}
1. Let us assume that L is CPL
Null productions are removed with the resulting set of
2. Let us pick up a word ro = on 1n 2n 3 , where the constant n is
11
production as :
given as per the pumping lemma.. S AACDIACDICD
3. ro is rewritten as uvxyz where lvxyl n and v•y * £ i.e. both v A aAblab
and y are not null.
C aCla
1 1
4. From pumping lemma, if uvxyz E L then uv xy z is in L(G)
A aDalbDb
for each i = 0, 1,2,...
Step 2 : Removing non-generating symbol
There are two case :,
Symbol S and Dare non-generating . .
Case I : vy contains , three symbols. These three symbols
Since, the starting symbol · . · · an
could be 0,1,2 or 1,2,3. itself 1s non-generating, it 1s
Invalid grammar. ,
The exact ordering of 0,1,2,3 will be broken in
2 2 ' 2 2 .
uv xy z and hence uv xy z l!L(G)
Case II : If vy does not contain three symbols then uv2xy2z
will have either unequal number of O's and 2's or
unequal number of 1's and 3's. Hence, uv2xy2z I! L
(G).
Thus, proved by contradiction. Fig. 5.3

ei~lmiiiimmiin___________________ --!... _ _ _ __.,,

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)

S ASBIAB · S aAS aSbAS aabAS aabbas aabbaa


A AC.S I AC 1 I a (ii) Rightmost derivation :
B sc;,s I sc;, I c;,s I b I Ac.s I AC. I a I cbc;, . Rightmost derivation of aabbaa is being shown with the help
- c. a of the parse tree.
Ci, b
1. S 2. S

/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

/!\ ! /1\ ! II\ I


S b A a
b/\
b a
I /\a
a b
B sc;,1 c;,s I b s sc;,1 c;,s I b

· 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

Cea easv -solutions

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 .

(Iii) Parse tree : . bbAaabbabA bbAaabbaba b~


s (iii) Parse tree for bbaaabbaba .
I\ s
a B
b
A A
·
a .B

. /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) For the atrlng 00110101


(i) Leftmostderivation

S 0BB OOBB 001B 001 lS


. 00110 B 001 lOlS 001 l0i0B ··
00110101
(a)

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)

· 0.16 Reduce the following grammar to GNF.S AB,


BSB I BB I b B aAb I a a. 17 Reduce the following grammars to GNF
911
Ans.: .,.W,_1'!11!11,
. . Ans.:
Step 1 : Making every symbol other than the first symbol (in
Step 1 : Renamingof variablesby substitutingA 1 for S and A,_
derived string a in A a) as a variable :
. for A.
Variables c;, is substituted for b with resulting set of A1 I
productions give as :
Step 2 : Every production of the form A; a with i > j
S AB
must be modifiedto make c;; j.
A BSBIBBlb A,. - production,A2 A1 A1 should be modified.We
B aA<; I a • c;, b . must substituteA,_ A,_ I I for the first A1• .

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

' (ii) . · Right~ost derivation


· (A) For string "aaabbabbba" .

S bA bbAA bbAaS bbAaaB


It will be worthwhile to draw the parse tree and from the
parse tree, one can easily write left most and right most derivation . bbAaabS bbAaabbA bbAaabbaS
(i) Left most derivation : bbAaabbabA bbAaabbaba bbaaabbaba
S aaBB aaaBBB aaabBB (iii) Parse tree for bbaaabbaba
s
aaabbB aaabbaBB aaabbabB aaabbabbS
aaabbabbbA aaabbabbba ., <·b.A
. - A
(ii) · Right most derivation :
b/\"'
A A
/aAa S

(iii) Parse tree : aAB


s
bAs
·I\
a B . .
bAA
a B B aAs
/l\ /l\
aaaaBB bAA
'/ I I I\s
b b b b
I.
·a
I\
a ' A
Fig. S.6(c)


a
(C) For the string 00110101
(i) Leftmost derivation

Fig, 5.6 S 0BB OOBB OOIB 0011S


-::::<i;r=;;;;;;;;;;;;;;;;;;;;;;;:;--=--------_...;.--:------=--....:....::.::=-=.'.~~
. (j o~~-_;_:,

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

(ii) . Rightmost derivation WWW


S OB OOBIS OOBlOB Ans.:
00B101S 00B1010B 00B10101 t. The non-terminals (S, A, B) are nullable. Null productions
001110101 are removed. The resulting grammar is :
(iii) Parse S ABA I BA I AB I AA I A I B

A aA I bA I a I b

B bB I aA I b I a

2. Removing u~it productions, we get

!'BA IAB IAAI aA I bA lal bl bB I aA

aA I bA I a I b

B bB I aA I b I a

3. · Every symbol in a, in production of the fonn a where I


a I 2 should be avariable.
This can be done by adding two productions.

Fig. S.6(d)

· Q. 19 Consider the following grammar :


S iCtS I iCtSeS I a C b For the String . The set of productions after the above changes is :
'lbtlbtaea' find the following : (i) Leftmost
derivation {ii) Rightmost derivation {lll)Parse Tree S ABA I BA I,AB I AA I c. A I c;,A I a I b I c;,B I C,A
{Iv) Check if the above grammar Is Ambiguous A c.A I c;,A I a I b
IBiEW
B c;,B I c.A I b I a
Ans.:
{I) Leftmost derivation : {II) Rightmost derivation :
[using S iCtS] 4. Finding an equivalent CNF.
S ias [using S iCtS] S iCtS
ibtS [using C b] iCtiCtSeS
[using S iCtSeS]

[using S iCtSeS] iCtiCtSea [using S a]


iCteCtaea [using S a) S BA I AB. I AA I C1 A S BA I AB I AA I C,A I c;,A I
ibtibtSeS [using C b]
[using S a) iCtebtaea [using C b]

ibtibtaea [using S a) ibtebtaea [using C b]

(II') P~rse Tree :


S .

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
' '

'iJf'-.Theory,of Comp._Scl. (MU-Sem.5-Comp.)


S V1 V,

----~
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,

Fig. 5.8 S ASB I a I b X1

Left most derivation Right most derivation


lbX 1
s S

cSaSa Step 2 : Re-writing production in GNF :


Produ~tfons CNF forms
(1)
SSaccba (2)
·· SSSbaccba (3)
cccbaSSba · SScbaccba (4) S ASB I a I b X 1 .S aSASB I aSB [substituting A]

· 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 _.-

a S Is start symbol. Show that above grammar Is


ambiguous. ••NW-Ml
Converting to CNF : Ans.: ,

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

Ans.: The resultingset of productionsis :


Given set of productions A1 A,.A,. I a ·
S aSbl aX A,. A2A,.A1I aA 1I b
X XalSala ·
Step 3 : Removingleft recursion:
' SubstitutingC, for a, c;, for b, A1 for S and A,. for X.
The A2 - productionsA 2 A,.A,.A1 I aA 1 I b contains left
A1 a A 1 c;, I a A,.
recursion. Left recursion from A2-production can be removed ,
A2 A,. C1 I A 1 C1 I a
through introduction of B2-production.
C, a
A,. aA 1B2 I bB2
c;, b
Removing left recursion fonn A2 production,we get • B2 A2A1B2 I A2A 1
C1 a, b The resulting set of productions is :
A1 a A 1 c;, I a A 2 A1 A 2A 2 1a

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.)

B1 a A1B2A1 I bB1A1 I aA1A1 I bA1 (Ul) Parse tree s


The final set of productionsis :
A,, aA1B 2I bB2I aA 1 I b ~s
A1 aA1B2 A 2I bB2A 2 I aA1A2 I bA,_ I a
A set of productions P
l~ _I
b i C t S a

B2 aA1B2A1B 2 I bB 2A 1B 2 I aA1A 1B2 I bA 1B2 I


a A 1B 2A 1 I bB 2A 1 I aA1A 1 I bA 1
I I- b a

where,Set of variables V = (A 1, A,,. B 2) (iv) It is an ambiguousgrammar due to laughing if problem.

Set of terminals T = (a, b) Q. 27 Reduce ·tollowing grammar to GNF.


Start symbol = A 1 S AB
Set of productionsP = Given above. BSBIBBlb
Q. 26 Consider the following grammar : B alaAb
S ICtSIICtSeSla {I) S 01 SI01
C b -
S 10SI10
For the string 'lbtlbtaea' find the following :
S -o+ OOle
{I) Leftmost derivation
{ii) Rightmost derivation Ans.:

{iii) Parse tree Removing E -production, we get.


{Iv) Check if above grammar Is ambiguous. S OlSIOlllOSllOIOO

It can be converted into GNF in an easy way by introducing


Ans.: two production
{I) Leftmost derivation
X 1 and Y 0

iCtSeS=:>ibtSeS Productionsin GNF are

i b ti Ct Se S =:> i b ti b t Se S~i b ti b ta e S S OXSIOXII YSI I YIOY


btl bta ea

{ii) Rightmost derivation

l CI IC t Se a=:::>i C tiCta ea

=::)ICtlbtaea ===>ibtlbtaea

Chapter 6 :-Pushdown Automata (PDA)

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. ·

Where, - Push the current input


6(4o, a, E)
Q ' e . {CJo,q1,CJi,'b}, :E={b,d,c},r={x,7-o)
· 'lo is the initial state, 7-o is initial stack symbol. 6(%, b, E) (%, b)

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]

e. b al . ,ab a,, El El Where,


ab
CJ
w jw
ba
q.
I

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. S) {(q, 088)} For each production Ans.:

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

6(q, 1, 1) {(q,E)} Foreachtenninal


. . 4 6(CJo, a, zo> = (CJo, azo>
Acceptanceof 01 O by M :
6(CJo, a, a) = (CJo, aa)
6(q, 010000, S) 6(q, E, S) .(q, OBB) ·
2. On first b, the machine moves to q 1 and remains there for b's.
(q, 010000, OBB)
b's will have no effect on the stack.
O(q, 0, 0) = (q, E) 3. For every 'a', an 'a' is erased from the stal:k.

(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!>)

(q, 10000, 15B) Where the transition function 6 is :


I
O(q, 1, 1) = (q, E) . 1. 6(CJo, a, Zo) = (CJo, azo> [First 'a' is pushed]
(q; 0000, SB) 2. 6('lo• a, a) = ('lo• aa) [Subsequenta's are pushed]
6(q, E, S) = (q, OBB)
. 3. 6(CJo, b, a)= (q 1, a) [Input symbols b's are skipped]
(q, 0000, OBBB)
4. 6(q1, b, a)= (q 1, a)
6(q, 0, 0) = (q, E) .
5. 6(q1, a, a) = (q2, E) [An a is erased on first a oflast a's]
(q, 000, BBB)
6. 6(q2, a, a)=(~, E)
6(q, E, B) = (q, 0) [An a is erased on subsequent a's
of last a's]
(q, 000, OBB)

O(q; 0, 0) = (q, E) [Accepting through empty stack]

Design a PDA to accept (ab}"(cd)".


(q, 00, BB) . Q. 6
Ans.:
WWW
6(q, e, B) = (q, 0)
To solve ~is problem, we can take a stack symbol x. For
(q, 00, OB)
every 'ab'. one x will be pushed on top of the stack. After reading
6(q, 0, 0) = (q, E)
(ab)\ the stack should contain n number of x's. These x's will be ··
(q, 0, B) matched with (cdt. For every 'cd' one x will be popped.

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

a,Zr/Zo If the length of w is n then a palindromeof odd length is : ·


a,>c/x
First n characters are equal to the last n characters in reverse
order with middle character as 'a' or 'b'.
Algorithm:
b,zrfxzo
c,>c/x b,x/xx There is no way of finding the middle position of a string by a
PDA, lherefore the middle p>sition is fixed no'n-deterministically.
t. · First n characters are pushed onto the stack, where n is non-
deterministic.
d,x/s c,>c/x
2. The n characters on the stack are matched with the last n
characters of the input string.
3. n is decided non-deterministically.Every character out of first
n characters should be considered for two cases :
(a) It is not the middle character - . push the current
character using the transition :
6<<Io, a,£) (<Io, a)
Fig.6.2 6('lo, b, e) . (<Jo, b)

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

Q. 7 Design a PDA for detection of odd palindrome


over {a, b}. Alterlourtti
lnputb
Ana.:
An odd palindrome wlll be of the form · :
1. wavl
ab a ba , a b a a a b a,,
1 1 1 A
aaa After111th
CJ R Input II
w w w · w w w

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

!; Thaoryof Comp, Sol, (MU-Som.a-comp,). TCS-40


z

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

. M=({q}, {O, 1, a. b, •. +, (, )},{O, 1, a, b, •, +, (,},I, E}, 6, q, E, ~) Rule 7


(4o, (.Zo)
where, 6 is given by,
Rule5
6(q, £. E) = {(q, 1), (q, E • E), (q; E+ B), (q, (E))} (<Jo,]),[( Zo)
6(q, E, I) = {(q, a), (q'. b), (q, _lb), (q, I~). (q, IO), (q, 11)} Rule 8
,, (4o. ), ( 7.o)
6(q, 0, 0) = {(q, £)}
Rulc7
\ 6(q, I, 1) = {(q, £)} (4o, £, Zo)
6(q, a, a) .:i: {(q, £)} . Rule 9
(qt' £, 7.o)
6(q, b, b) = {(q, £)}
Q. 12 Consider the PDA with the following moves :
6(q,'+. +) = {(q, £)}
6(q0 , a, Zo) = {(q0 , a~)} 6(q0, a, a) = {(q0 , aa)} 6(qo,
6(q, '. , •) . = {(q, £)} b, a) = {(q,. e)} 6(q1, b, a) = {(q,, t)} 6(q,, £, · Zo)
6(q. (. () = {(q, £)} = {(q,, £)} Obtain CFG equivalent to PDA.
6(q, ), )) = {(q,£)} w
a. 11 Design a PDA for CFL that checks the well Ans.:
formedness of parenthesis I.e. the language L of Step 1·: . Add productions for the start symbol
all "balanced" string of two types of parenthesis z
say"()" and "[ ]". Trace the sequence of moves . s [4o O<lo]
made corresponding to Input string (([ ])[ ]). z .
[4o O qi]
Ma 2009, Ma 2014 , Dec . 201-7 Step 2: Add productions for (q0 , a, a)= {(4o, aa)}
Ans.: a a a
[4o a [qo Clol [qo Clol
The transitio~ function of the PDA is given below : a ·a a
[4J Qo] a [qo 411 C-41 qoJ
a a a
1. 6(4o. (. Zo) = (4o. (Zo) ] Push the ope<ringbracket '(' [4o a [qo 4ol [4o qi]
a a a
2. 6(4o;-(, () = (4o. (() [qo 411 a [qo qi] [qt qi]
Step 3 : Add productions for 6(4o, b, a) 1a1 {(q., £)}
3. 6(4o. (. [) = (4o. ([) a
[4o b

]
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

7• . 6(4o; ). ) ) = (4o; £) POP an opening bracket for a


Q. 13 Write short note on DPDA.
' ]
closing bracket
Ans.:
DPDA
8. 6(4o. ], ]) = (4o, £)
In a DPDA there is only one move in every situation . A
9. 6(4o, £, zc) = (qr zJ J Accept through a final state. DPDA is less powerful than NPDA. .

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.

ocq1, b, b) {(q1, 1::)J Finally the stack should become empty.

O(q1, E, Zo) {(q 1, E)} Transitions

[Accept through an empty stack]. 0(%, a. Zo) = (%, bb Zo)

Details of important transitions : 6(<w, a. b) = (<Io, bbb)

The transaction,6(q0 ,a,a) { (q0,aa), (q1,a), (q 1,e)}


6 (%, b, Zo) = (q 1, bb Zo)

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

Centre Centre Centre


1. Input 'a' is part of w of the palindrome.
2. Input 'a' is middle character of w_awR If the length of w is n then a·palindrome of even length is
. :.
R First n characters are al
3. Input 'a' is the first character of w • equ to the last n characters in the -·
reverse order. ....._ .,
The character immediately bcfo~ th 'ddl · . . ·. ·. ·• ·
·d · e mt e position will be. .,
i enttcal to the character immediately afte the . _ .. ·. · .
. r mtddle position . . ,

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:

the set of input S}'.mbolsl> (0, 1 }


the set of stack symbols r = (0, 1, 7.o} A

Starting state = 'lo


B i•M-Mti
Ans.:
Initial stack symbol = l.o
The equivalent PDA, Mis given by :

(.is easv - solutions

Scanned by CamScanner
-
,.

Theoryof Comp. Sci. (MU-Sem.5-Comp.) res~ ·


M = ({q},{a,b},{a,b,S,A,B},6,q,S,41>) a. 21 Oeslan a PDA to accept language
{an-f' b2n+1 In>=1}
· w~ 6 is given by:
{(q,nSA),(q,6)} -Ans.:
6 (q, e, I. 6(<1o, a. Zo> (q,. aaZ,) '
((q, bB)}
6 ( q, e, B)= {(q, b)} 2. 6(q 1, a. a) (q 1, aa)

6 (q, a, a)= {(q, e)} 3. 6(q 1, b, a) ((h, a)

6(q,b,b)= {(q,e)} 4. 6((h, b, a) (q 1, e)


5. 6(q2, e, Zc,) ((h, e)
Accept through empty stack.

Chapter 7 : Turing Machine (TM)

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

lOi 10i•I Hi l()K,


UniversalTM
A general-purpose computer can be programmed to solve Where K = 1, if move is to the left
different types of problems. A TM can also behave like a general- K = 2, if move is to the right
purpose computer. A general purpose computer solves a problem K :a: 3, if move is 'no-move'
as given below :
State 41 is represented by 0,
1. A program is ·written in a high level language and its ·
State q 1 is represented by 00,
machine-code is obtained with the help of a complier.
State q,, is represented by O,,. 1•
2. Machine code' is _loaded in main memory .
First symbol can be represented by 0,
3. Input to the program can als~ be loaded in memory.
Second symbol can be represented by 00 and so ori.
4. Program stored in memory is executed line by line. Execution
involves reading a line of code pointed by IP (instruction Two elements of a tuple representing a move are separatedby 1.
pointer), decoding the code and executing it Two moves are separated by 11.
We can follow a similar approach for a TM. Such, a TM is. Execution by UTM :
known as Universal Turing Machine. Urriversal Turing Machine
(UTM) can solve all sorts of solvable problems. We can assume the UTM as a 3-tape turing machi~e .
1. Input is written on the first tape.
A Turing machine is designed to solve ' a particular
problem p, can be specified as : 2. Moves of the TM in encoded form is written on the second
1. The initial state <Jc, of the TM M. tape.

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,

J. OXJ)R f-B b ha b b B 1-13 b bab b B f-Bb b ab b B I-Bb ba b b B


'17
t ' Cb Q7
t t t
Cb
2. o:,aol
' I-BB ba bbB f-Bbab bBj-BbabbBf-,B bah bB
3. roool t . t t t
<12 <t•
Where ro is a string over {a.b} with I ro I O 1-Bba b b B f-B bab b B f-BbabBB f-B ba bB
. t t, t . t
q4 q, q,
Algorithm:
i-B ba bB f-Bb ab B f-BbabB f-BBa bB
1. Algorithm requires n cycles, where I ro I= n. . t t t t
q, q7 'In q~

2. In each cycle, first character · is matched with the last f--B a b B I- B a b B I- B ab B I- B a BB f- Ba B


t t. t t
'17 q,
t
q, Cl4
character and both are erased.
f-BaB I-BBB f-13B B
ct a i
Fig. 7.l(a)

a. 3 Design a TM to compute multiplication of two


unary numbers.· lmEiliii
••
,ICJI'odd la1jtb Ans.:
i-tindn,mo .
Multiplication algorithm is being explained with the help of
an example. .
3 x 5 will require three cycles.

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

through q. 43 'Is 47, looking for last character as 'a'. Cycle I


If the leftmost character is · 'b', the machine takes a path
through
CJo 'h q. 'I6 47, looking for last character as 'b'.
Cycle 2 !ijxj{g:'il'.0>000 # 000000000B· . ::, ,. , 2x5 = 10
---...--,
2x5 = 10
The Turing machine M is given by :

M = (Q,l:,r,6,cio,B,f) Cycle 3 ,~~•#·00000 II 00000 00000_0()()()() B.J 3x5 = I 5


\, .,
where, Q = ('lo, Qi, 'b, Q3, q., (Is, 4;, Q7, 'bl V
3x5 = 15
l; = {a,b}
To calculate 3 x 5, three times, 5 zero's are appended.
r = {a, b, B}
Unary representation of 3 is 000.
The transition function 6 is given in Fig. 7. l(a)
Unary representation of 5 is 00000.
Clo = initial state 3, 5 and the result, are separated by#.
B = blank symbol Inside each major cycles (three cycles for 3), there will .be a
number of minor cycles (5 mir\or cycles for 5) to append O's one at
F = {<Ja}, halting state
a time. ·
Working of TM fodnput abbabba is shown in Fig. 7.l(a):

(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

x2 is represented by '°2• where '°2 is a string of O's. t t


q, q,
x1 • x1 is represented by~. where~ is a string O's: 1--B O x#O x# 00 OB 1-B Ox #Ox #0 00 B
t t
# separates ro1 and '°2,, '°2, and ~- q, q,

In the TM shown in Fig. Ex. 7.3.6, there are two cycles. · 1--BOx#Ox#OOOBI--B Ox #Ox#OOO B

1be cycle Clo q1 'h 41 <Jo appends '°2, to ro3 for t t


4' Cb
every zero in ro1, with the help of cycle 'h 43 'h 1-B O x#Ox# 00 0 OB 1-B Ox# Ox#O O 00 B
Worlcing of TM for2x2 is shown iri Fig. 7.2(a): t
q.
t
q.
BOO#OO#B 1-BxO#OO#B 1-BxO#OO#B 1-BxO#OO#B
1-BOx#Ox#OOOOBI-BOx#Ox#OOOOB
t t t t t t
4, q. q. 'h q. .44
1-BxO#xO#B 1-BxO'#xO#B . IBOx#0:1.#0000BI-BOx#OO#OOOOB
t
q,
t
q, t t
q. q.
1-BxO#xO#B 1-BxO#xO#OB f..B O x#O 0#00 00B 1-B O x#O 0#0 0 0 0 B 1-B Ox#O 0#00 0 OB
t
q,
t
q. t t t
4s 4s 4s
1-BxO#xO#OB I-Bx0#:1.0#0B 1-B O J:#00#00 00 B 1-B 00#00# 0 000 B 1-B 00#0 0#0 00 OB
t t t t . t
q. 'L 4s .
1-BxO#OO#OB 1-BxO#Ox#OB
Fig. 7.l(a)
t t
4l lb
a. 4 Design a TM to find the value of login), where n is
~: BxO #0 x# OB I-Bx 0# Ox #0 B
t . t any binary number. ••W.Jml
q, q3 Ans.:
1-BxO#Ox#OOB 1-BxO#Ox#OOB log2(n) of any number n lying between 2n and 2' + 1 is given
1

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.•.

Cii easv-so1u11ons I <,

Scanned by CamScanner
,(f Theoryof Com~. Sci. (MU-Sem. 5-Cpmp.) TCS-47

M>,R 2. Copy n - 2, i.e. I Io # o o o # o o o # o o # o o o o o 0 • oj


0/B.R 1/0,R -,r- -,r- -.,.. V
n nx1 n-1 nx(n-1) n-2
1/B,R B/8,L , Cycle3:

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

B O O 1 0 0 B 1-B O O 1 0 0 B 1-·B O O O O OB #/#,R

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)

Q. 5 Design a Turing machine to compute nl.


·••!'llm·w-4,~M-=•
-Ans~: ·
It is assumed that n is represented in unary system. 0/0,L
#1#,L
Factorial of n can be calculated through repeated application of:
1. Multiplication
2. Copy
Operations.
Algorithm is being explained with the help of example.
Algorithm for 11_ •

Initial configuration Io. # OOO #


---v-
B B .. , I.
Cycle 1: n

1. Multiplication IO# 0 0 0 # 0 0 0 B ... ,


---v-
Product

2. Copy .n = 1, i.e. 2 10#000#000#001


---v-
n nx1 n-1

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

1be transition behavior of 2-head one tape Turing machine


can be defined as given below :
b/b,L
o (State, Symbol under H 1, Symbol under H 2) = (New state, y/y,L
a/a,L
(S1, M1), (Sz. Mz))
y/y;R
Where, zlz){

S 1 is the symbol to be written in the cell under H 1• Fig. 7.7(a): Transition diagram

M1 is the movement (L, R. N) of H 1•


a b C X y Z B
Sz is the sytl!bol to be written in the cell under Hz. · (q,.x,R)
Mz is the movement (L, R. N) of Hz. Q1 (q,.a,R) (C12,y,R)
2. Multi-Tape Turing Machine Q2 · (Ci2,b,R) (Cb.z,R) (Ci2,Z R)
Multi-Tape turing machine has multiple tuples with each tape Cb (QJ,a,L) ((b,b,L) (<bX,R) (Cb,y,L) (Cb.Z.L)
· having its own independent bead. Let us consider the case of a two .
Cl4 (q.,y,R) (q..z,R) (qs.B,N)
tape turing machine. It is shown in Fig. 7.6.
% Qs Qs Qs Qs . Qs
Tape 1: LB"l'aJb,!. a'l~iaf~:j;b;j:1f:!;BlB:I:a::~ ! Haling
f state

Tape 2: I sJa j.a.:,j,:1;,;j:h::l i fb,l:ii',j.B.IBI Fig. 7.7(J,): Transition table


' t The Turing machine Mis given by:
Fig. 7.6 : A two-tape turing machine
M = (Q~ l:. rs 8,. <Jo. B~ F)
The transition behavior of a two-tape Turing machine can be Where, Q = (cio, q1, Cb, 'h• ~.<isl ·.
defined as: I: = (a.b.c}
liCq1.a1.az> =(qz,(S1.M1),(Sz,Mz}) r = la. b, c. X, y. z. B}
Where, 6 = The transition is given Fig. 7.7(n. b)

. q 1 is the current state, CJo = Initial state

qz is the next state,


B = Blank symbol

ai is the symbol under the head on tape I,


F = ((b}, Halting state

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. 9 What Is Turing machine ? st


After l cycle
Ans.:
Turing machine : Formal Definition of Turing Machine
A Turing machine Mis a 7-tuple given by:
M = (Q,L,r,O,~B,F)
where
1. Q is finite set of states Transition diagram and transition table are given · in
Fig. 7.9(a) and (b).
2. I: is t;initeset of input alphabet not containing B. ·
3. r is a finite set of tape symbols. Tape symbols include B.
4. CJo E Q is the initial symbol.
5. BE r is a special symbol representing an empty cell.
6. F s;;;; Q is the set of final states, final states are also known as m <; n m>n
halting states.
OA),L
.7. The transition function 6 is a function from #!IIJ.,

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) .

Movement of Read/ Write head is given L, R or N Cb (43,0,L) (43,#,L) (4>.B.R)


L Move to left cell Q4 (<4,B,R) (<is,B.N)
I '

R Move to right cell q; ci.~ <Is <Is Halting state


N · Remain in the current cell (No movement)
Fig. 7.9(b): Transition table
a. 1O Design a TM to compute proper subtraction of
The Turing machine M is given by : ,
two unary numbers. The proper subtraction
function f Is defined as follows : M = (Q, :E, r, 6, CJo. B, F)
m-n lfm>n where,
f(m, n) = { o otherwise
Q = ((Jo, q,, 'h, Cb, <ti, <Isl
Ma 2009, Dec. 2009
l: = (0, l, #}
Ans.: r = (0, l, #, B)
Toe working of the TM is being explained with subtraction of
The tmnsition function 6 is given in Fig. 7.9(n) and (b)
3 from 5.
CJo = initial state,
In unary system;5 is representedus 00000.

easv-solutions

Scanned by CamScanner
'(I Theory of Comp. Sci. (MU-Se~. 5-~mp.) TCS-51

B = blank symbol Q, 11 Write short note on Variants of TM.


F = {45}, Halting state
Dec.2006, Dec.2008.Dec . 2009. Dec.2010 .
The working of TM .is being simulated for 5-3 is shown in ••
1'.ig.&. 7.3(c) : Ans. :
B0000 0#000 Bl-BB 0000# OOOBf-B B000 0#000B
t t . 1. Two-w~y Infinite Turing Machine
. . l In a standiird ~ring machine number of positions for leftmost
· 1-B B 0000# 00081-B 8000 O#OOOB 1-BBO 000# 000 B
t _t t blanks is fixed and they are included in instantaneous description,
q <ti
. 1-B B 0000# 00081-B B000 0#0008 j-B80 000#0 008 where the right-hand blanks are not included .
t . t t In the two way infinite Turing machine, there is an infinite
. <ti qi qi
1-B B 0000# 000B I-B 00 00# 00 OB 1-B 0000# 00B8 sequence of blanks on each side of the input string . In an
.t ·i i . instantaneous description, these blanks are never shown.
. .
2. A Turing Machine with Multiple Heads
rBOOOO#OOBrBOOOO#OOBrBOOOO#OOBf-BO000#0 OB
t- t . .t t A turing machine with single tape can have multiple heads.
Let us consider a turing machine with two heads H 1 and ~ - Each
. t t
bead is capable of performing read/write /move operation
j-BOOOO#OOBrB OOOO#OOBI-B OOOO#OOBf-B0000#00B independently.
t t t t . BabaabbaBBB
lb . 'b Cb <Jo
t t
rBBOOO#OOBf-BBOOO#OOBf-BBOOO#OOBj-BOOO#OOB H, Hi
t t . t . t Fig. 7.10: A Turing machine with two beads
ClJ qi q, <Ii
The transition behavior of 2-bead one tape Turing machine
f-B OOO#OOBJ-BO00#0 OB j-BOOO#OOBrB 000#00B
can be defined as given below :
t t t t 6 (State, Symbol under H1, Symbol under~ = (New state,
ClJ 4I 1h ClJ
_(S 1, M1), (S2 , Mi))
f-BOOO#OBBf-BOOO#OBI-BO00#0Br BOOO# OB
t 1 t t
Where,
S 1 is the symbol to be written in the cell under H 1•
rBOOO#OBl-8000#0B rB OOO#OBI-BBOO#OB M 1 is the movement (L, R. N) ofH 1•
t t t t S 2 is the symbol to be written in the cell under H2•
lb lb I ClJ M 2 is the movement (L, R, N) of H2 •
j-BOO#OBf-BOO#OBf-BOO#OBr BOO#OBf-BOO#OB 3. Multi-Tape Turing Machine
t t . t · t t
'It <Ji <Ji q, Qz Multi-Tape turing machine has multiple tuples with each tape
having its own independent head. Let us consider the case of a two
tape turing machine. It is shown in Fig. 7 .11.
1-BOO#BBI-BOO#B rB OO#B rB 00# B
t
lb
lb <b Cb
t t t Tape 1: f'
!'BJ a j:b,F :a .ro-Lbl BTB Ilt_"7ii
j-BOO#Bf-BBO#B f-BBO# B
1. ! l Tape 2: IBlriJat,blh:l-a.j-'o-l:a·•l:eJBJ:.'~7 -•:-~
f
f-BO#Bf-B O#B j-BOOB Fig. 7.11 : A two-tapeturing madune

ti . Jz t The transition behavior of a two-tape Turing machine can be


Fig. 7.9(c) defined as:

Where,

(ii easv-sotutions

Scanned by CamScanner

• of
Theory Comp. Sci. (MU-Sem. 5-Comp.)

q 1 is the current state, Halting state


<u is the next state. ·
a 1 is the symbol under the head on tape 1,

82 is the symbol under the head on tape 2,

S 1 is the symbol written in the current cell on tape l,

S 2 is the symbol written in the current cell on tape 2;


M1 is the movement (L, R, N) of head on tape J,
M2 is the movement (L, R, N) of head on tape 2.
4. · Non-deterministic Turing Machine

Non-detenninistic is a powerful feature. A non-deterministic


Fig. 7.13
TM machine might have. on certain combinations of state and
symbol under the head, more than one possible choice of The turing machine Mis given by:
behaviour. M = (Q, l:,r,6,~B,F)

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}

deterministic TM. r E {O, I,B}

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)

· 101 In binary Input string. . W Fig. 7.13(a)


Ans.: Q, 13
Design Turing machine as generator to add-~ -
Tue turing DJachine will look for every occurrence of the binary numbers and hence simulate · for
"110 + 10" ___ ... :
string 110. Ana.:
·
State <u is for previous two symbols as 11.
This problem can be soived using a 3-tape Turing machine. -
Next symbol as O in state (h, will initiate the replacement
.. . the first b.mazy number . Second ·,:
.First machine Tl stores
prqcessto replace 110 by JOI. machine T2 stores the second binary number. Thint hi T3 •.
stores the result. mac ne

(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- Halting state_ B I O O 0


(0, 0, L) (0, 0, L) (B, 0, L) (I, 1, L) (0, 0, L) (B, 0, L) i .
(1, 1, L) (0, 0, L) (B, l,.L) (I, I, L) (B, B, L) (B, 0, L) 'h (Halt) ,
(0, 0, L)(I, 1, L) (B, 1, L) (0, 0, L) (I, 1, L) (B, 0, L) a. 14 Design a Turing machine as · acceptor for the
(B, B, L) (0, 0, L) (B,'.O, L) (B, B, L) (1, 1, L) (B, 0, L) language {a" bm I n, m 0 and m n}. 111JJWJ•H•
(0, 0, L) (B, B, L) (B, 0, L) (I, 1, L) (1, 1, L) (B, 1, L) Ans.:
a/a,R
(B, B, L) (1, 1, L) (B, 1, L) b/b,A
(I, I, L) (B, B, L) (B, I, L)

(1, 1,L) (1, 1,L) (B,O,L)

b/b.,L
a/a,L

Fig. 7.14
Fig. 7.15
Simulation for 110 + IO

B B I 0 Q.15 Construct turning machine that accepts the string


over I:= {O, 1} and converts every occurrence of
B 1 1 0 111to101. ll!i!l1ti1id
B B B B Ans.:
.i alting state
State <Jo ·
,L
B B I 0

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

B B 0 0 The turing machine Mis given by :


M = (Q, l:, r, 6, CJo, B. F)
i
State q 1 . Where, Q = (qo, Q1, (b, (b, <4. 4s}
l: = {O, 1 )'
B B 1 0
r = {O, 1,B}
B 1 1 0
6 = Transition function is shown using the transition
B 0 0 9 diagram .
i B - Blank symbol for the tape
State q1 F = ( q,}, halting sJate

easv - solutions . '

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) :

'Ille cycles of computationare shown below. B ( ( ) ( ) ) ( ) B 1- B ( ( ) ( ) ) ( ) B I- B ( ( ) ( ) ) ( )


Inputstring is assumed to be (00)0. . t t t
.~
I- B ( ( X ( ) ) ( ) B I- B ( X X ( ) ) ( ) B f- B ( X X( ) ) ( ) D
Cycle No. Tape ' t
t t '
CIJ '
Initial B(()O)OB
· 1-B(xx(p()BI-B(xxix) ()Bf-B( xxxt)()B
1. B (XXO)OB ' qi Clo
2. B(XXXX)OB I- B ( XX XX) ( ) B I- B ( XX X X X ( ) B I- B ( X X X X X ( ) B
t t . t
qo CIJ <1, .
3: BXXXXXXOB
1-B(xx xxx() B 1-B (x xxxx() B f-B ( xx xxx () B
4. BXXXXXXXXB t t t
q, Q1 ·qi
I-Bx x xxxx () B f--B xxx x xx() Bl- Bx xxxxx () B
Jo ' Jo t
I-Bx xxxxx () B f-B xxxx xx() BI- Bx xxxxx ()B
t t Qo
t
Qo
(/x,R I-Bx xxxxx () B f-B xxxx xx(xB
' t t
B/B,L Q1
1-BxxxxxxxxB!-BxxxxxxxxB
' t t
qo Qo

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

<b
( ) X B I-BxxxxxxxxBI-BxxxxxxxxBI-BxxxxxxxxB

4> (CJo,(,R) (Q1.X.L) (Qo,X,R) ((h.B.L)


Ji
' '
t
Qi Qi
t
q, (CJo,X,R) (q1,x,L) f-BxxxxxxxxBI-BxxxxxxxxB!-BxxxxxxixB
t t' t '
'h ((h.X.L) (q3,B,R) ql Qi Q1
1-Bxx xxxxx xB
q3* QJ QJ QJ Q3
t
' q3
u Fig. 7.17(c)
Halting
stale a. 17 Design a turlng machine to check whether a
string over {a,b} contains equal number of
Fig. 7.17(b) ': State transitiontable
a's a nd b's. · bJ§W4•11€1@itf4•M:l•J4WMUI
Ans.:
1be Turing machine M is given by :
Algorithm:
M = (Q, l:, f; 6, 4>, B, F)
1. Locate first a or first b.
where, Q = (41. q,, Q2, Q3)
2. If it is 'a' then locate 'b' rewrite them as x.
l: = {(,)) 3. If it is 'b' then locate 'a' rewrite them as x.
r = {(, ), x, BJ 4. Repeat steps from 1 to 3 till every a orb is re.-writtenas x.

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

-+4J (%.x.R) ((h.x.R) ('lo,X.R) (<14,B,N)

41 (q1,a.R) (4:i,,X.L) (41.x.R)


blb,L
'h (4:i,,X.L) (42,b,R) ((h.X.R) a/a,L

'b (4:i.a.L) (4:i,b,L) (4:i.X,L) (CJo,B.R)

q4• 44 44 44 44 Halting
state

Fig. 7.18(b): Transition table ·


Fig. 7.19

Toe turing machine M is given by :


M = (Q. L. r. 6. <1o, B. F)
M = (Q.~r.6,4o,B,F) Where, Q = {Clo, 41, 'h• 'b, 44}
. Where, Q = { 4o, 41, (h, 'b, 44} L = {a, b}
l: = {a,b} r = {a, b, X, Y, B}

r = . {a, b, X. B} 4o = initial state


B = Blank symbol
4o = Initial state
F = {q4}
B = Blank symbol
Q.19 Design a TM to add two unary numbers.

Ma 2011. Dec. 2016


Worldng of machine for an input abba is shown in
Ans.:
Fig. 7.18(c):
Addition of two unary numbers can be performed through
I- B ab b a B I-Bx b b a B I- B xx b a B I-Bx x b a B append operation. To add two numbers 5 (say ro1) and 3 (say ro.i>
t
qo
t
q1
' q3t t
q3 will require followingsteps :
1-B xxbaB 1-Bxx baB r-BxxbaBI-Bxx xaB 1. Initial configurationof tape :
t t t t
r1q:o•l,o.1t9,1~0r·o:I'#:bo::j:olqJa~:1 1

F1g. 7.18(c) Contd .... 5(ro1) 3(ro2)

easv-sotutions

Scanned by CamScanner
. '

'1f Theoryo(Comp. Sci. (MU-Sam.5-Comp.) ' TCS-56

2. 0>1 is ap~nded to <nz. The turing macbine M is given by :


M = (Q, L,·r, 0, qo, B, F)

Where Q = ( <Jo, Q1, (b, 43}


}; = (0, #}
While every 'O' from ro 1 is getting appended to co2.' 'O' from
0>1 is erased. <Oz contains 8 O's, which is sum of 5 and 3.
r = (0, #, B}

#/#Jl 6 = Transition function is given in


Ml;l Fig. Ex. 7.3.10 (a), (b)

'lo = initial state

B = blank symbol
F :a: {43}, halting state.

Q. 20 Write short note on: Church-Turing Thesis.


B/BJl
w
Ans.:
Church-Turing Thesis
Fig. 7.20(a) : Transition diagram The Turing machine is a general model of computation. Any
algorithmic procedure can be solved by G computer can also be
0 # B
solved by a TM. Proble~ computed by a computer or a TM are .
(4i,B,R) (<1J.B.R) also known as partial recursive functions . Some enhancements to ·
(4 1,0,R) (41,#.R) (42;0,L) TM made the Church-Turing thesis acceptable. These
41
enhancements are :
42 (42,0,L) (42,#,L) (4o,B,R)
1. Multi-tape 2. Multi-head
43• 'b 43 43 +- Halting state
3. Infinite tapes 4. Non-determinism.
Fig. 7.20(b): Transition table Since the introduction of TM, no one has suggested an
algorithm than can be solved by a computer but cannot be solved
by a TM.

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

ff P is solvable / decidable then there is an nlgorithm for B = tv 1,v 2,v 3 .. . vml


recognizingL. representing the problem. It may be noted that an Let us truce a new set of s~mbols a 1, 3i ... 8n, such~
on
algorithmterminates all inputs.
{a1, Bi ... a,,,} n :E=·,.
Followingstatements are equivnlent :
Symbols a1, Dz ••• am are being taken as index symbols. The
1. The language L is Turing decidable. index symbol 8t represents a choice of u1 from .A and v1 from the
2. The languageL is recursive. list B.
3. · There is an algorithm for recognizing L. A string of the form lJi uJ ut . . . 3t 3:i 3i. Over alphabet
Every ruring decidable language is luring acceptabl~. :Eu { a1, 3i; ... 3m} can be defined using the set of productions :
Every turing acceptable language need not be turing G _ u1 A a1 I Uz A 8i I ... I um Aau,}
A - u 1 a1 I Uz 8z I ... I um 3m
decidable..
Similarly a string of the form v1 vi vt ... 3t 3:i 3i over alphabet
A language L C: l:* many not be luring acceptable and hence
:Eu (a1, 32 ... 3m} can be defined using the set?f productions:
not truing decidable..Thus we cannot design a turing machine /
algorithm which halts for every m e L . Ga= ... lvm.A.a,,,}
v1 a1 lv2 3il ... I Vm3m ·
Q. 2 .Two recursive_languages L1 and Is recursive : Fin.illy, we can combine the languages and grammars of two
.
lists to form a grammar GAB :
Ans.: A new start symbol S is added to GAB
u Is recursive Two new productions are added to GAB
Let the tming machine M1 decides L 1 and M2 decides Li- S A
. ff a word 0) E LI then Ml returns "Y" else it returns "N". S B
Similarly. if a word m E Li then M2 returns "Y" else it returns "N".
All productions of GA and Ga are taken.
Let us construct a turing machine M3 as shown in Fig. 8.1.
_Now, we will show that GAB is ambiguous if and only if an
. instance (A, B) of PCP has a solution.
Assumption:
Suppose the sequence i1, i2, •• •_, i,,, is a solution to this instance
Fig. 8.1 : A taring machine for L 1 u L 2 of PCP. Two derivations for the above string in GAB is:
S A U;I A 8i1 U;I ¾ A 8i1 ¾ ...
Output of machine M 1 is written on the tape ofM 3.
ll;I ui2 • •• Uim311 ¾ ··· 8im
Output of machine~ is written on the tape of M3•
S B vii B 311 V;t vii B 311 ¾ ...
The machine M 3 returns "Y" as output, if at least one of the
outputs ofM 1, orof~ is "Y". vii Vil •• • vim 8;1 ¾ ··· 8im
· Consequently, if GAB is ambiguous, · then the post
It should be clear that M3 decides L 1 u ½· As both L 1 and Li
are turing decidable, after a finite time both M 1 and M2 will halt correspondence problem with the pair (A; B) has a · solution.
with answer "Y" or "N". The machine M3 is activated aft.erM 1 and Conversely, if GAB is unambiguous, then the post correspondence
are
halted. The machine M 3 halts with answer "Y" if ro e L 1 or cannot have a solution.
co e Lz. else halts with ·output "N" . If there exists an algorithm for solving the ambiguous
Thus L u Li is turing decidable or L 1 u Li is recursive. problem, then there exists an algorithm for solving· the post
1
correspondence problem. But, since there is no algorithm for the
Q. 3 ·Prove that there exists no algorithm for deciding post ·correspondence problem, the ambiguity of CFG problem is
whether a given CFG Is ambiguous. unsolvable.
Ma 2006. Dec. 2007. Dec. 2008 0. 4 ·Write short notes on post correspondence
Ans.: . problem and Grelbach Theorem.
1be post correspondence problem can be used to prove the Ma 2006. Dec . 2006 . Ma 2007. Dec . 2007 . Ma 2008.
un-decidabilityof whether a given CFG is ambiguous. Dec. 2008 . Ma 2009 . Ma 2010 . Dec . 2010.
Let us consider twQ sequences of strings over Ma 2011, Dec . 2011. Ma 2012. Ma 2016

A = {u 1, Ui, liJ •·· um}


/

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

1 01 0 Step 2: Let us revise the machine H 1 as Hi to ta1ce Mas both


2 110010 0 inputs and H2 should be able to determine if M will
3. 1 1111 halt on M as its input Please note that a machine can
be described as a string over O and 1.
,4 11 01
M~halt
The PCP h;as a solution. The required sequence is (1, 3, 2, 4, 4, 3) ·
~loop
_o>1W3'°2W.C04W3 = 01111001011111
Step 3: Let us construct a new Turing machine H that takes
x, ¼ X-4 X4X3 = 01111001011111 3
output of H2 as input and does the following :
GreibachTheorem
I. If the output of H2 is "loop" than H3 halts.
The Theorem states that : 2
· If the output of Hi is "halt" than H will loop·
3
"Let a be a class of languages that is effectively closed under forever. ·
concatenation with regular sets and union, and for which L = t• is
M
un-decidable for any sufficiently large fixed l:. Let P be any non-
~halt --.. Machine H3 loops forever
trivial property that is true for all regular sets and that is preserved · loop --.. Machine H halts ·
3
under a, where a is single symbol in I:. Then P is un-decidable for
a". . H3 will do the opposite of the output of H2.
Greibach theorem can be used to prove that many problems . Step 4 : Let us give H3 itself as inputs to H3._
related to CFG are un-de.c;idable.
H3

eel easv SOIUIIOIIS


-
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

(1.3) (A 1A3 = lQlOl) (B 1B3 = 101011)


The next string to be picked up must be A3 and B3 • Any other
scqlJCl!CC will not lead to a solution.
- - Sequence String

(I. 3, 3) (A 1A 3~ = 10101101) (B 1B3B3 =101011011)


There is only choice of next string. This choice is A3 and B3 •
This does not lead to a solution. The PCP has no solution.

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

Chapter 2 12.5 Marks 20 Marks

Chapter3 27.5 Marks 15 Marks

· Chapter4 - -
Chapter 5 - 10 Marks

Chapters 25 Marks 10 Marks

Chapter7 12.5 Marks 20 Marks

Chapter 8 7.5 Marks 25 Marks

Repeated questions - 5 Marks

Dec. 2018

Chapter 1 : Introduction [Total Marks - 27 .5]

Q.1(a) Explain Chomsky Hierarchy. (5 Marks)


Ans. : Chomsky hierarchy
A grammarcan be classified on the basis of production rules. Chomsky classified grammars into the following types :

1. Type 3 : Regular grammar


2. Type 2 : Context free grammar

3. Type 1 : Context sensitive grammar


4. Type O : Unrestricted grammar·
Type 3 or regular grammar
_ A grammaris called Type 3 or regular grammar if all its productionsare of the following forms:

A E

A a

A aB . ,

A Ba

Wbere,ae :EandA.B EV.


A languagegeneratedby Type 3 grammaris known as regular language.

Scanned by CamScanner
., :n,eory_of f°"'P"19rScience (MU) . D(18t-2

.'TYIJ82 or context free grammar


_ A grammaris called Type 2 or context free grammarif all its productionsare of the following form a whereA e V and
I

V is a set of variablesand T is a set of tmninals.


. . . ;,
The language genaaled by a Type 2 grammaris called a contextfree language,a regularlanguage but not the reverse.

Type 1 or context eenaltlvegrammar

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~-

Evezy grammaralso is a Type Ogrammar


A Type 2 is also a Type 1 grammar
A Type 3 grammaris also a Type 2 grammar

Q. 3(b) Consider the followinggrammar


S-+I Ct Sil Ct Se sla
C--.t,

For the string 'lbtaelbta'find the following:


(I) Leftmost derivation
(II) Rlghbnostderivation.
. (Ill) Parse tree
(iv) Check If above grammarIs ambiguous. (10 Marks)

Ana.:
(i) Left most derivation:

iCtSeS [using S iCtSeS]

ibtSeS b]

ibtaeS [usingS

ibtaeiCtS [using iCtS]

ibtaeibts [usingC b]

ibtacibta

. ..... .. .. '
eas v S OIUII O II S
.
. '•
: ,•, .....

Scanned by CamScanner
• Th~ry of ComputerScience(MU)

(ii) Righbnostdcrivation:

.s iCtSeS [using S iCtSeS]


• I

iCtSeiCtS [using S iCtS]

iCtSeiCta [usingS a]

iCtSeibta [using b]
iCtaeibta [using S a]

ibtaeibta [using C b]

(tli)Parse tree as shown in Fig. 1-Q. 3(b).

Fig. 1-Q. 3(b)


-
(iv) 1be grammar can be shown to be ambiguous by drawing two different derivation trees for the string ibtibtaea as shown in
Fig. 2-Q. 3(b).

Fig. 2-Q. 3~)

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,.)

· State transitionfunction (6} is also known as STF. ·· ''_:-=-· - · .~ , •·-.-

Output function0,.) is also known as machine function (MTF).

}.. : l: x Q 0 [for Mealy machine]

}.. : Q 0 [for Moore machine]


'.
There art: two types of ~tomata with.outpµts:

L Mealy machine : Output is associatedwith transition

. Set of output alphabet O can _be different from the set of input alphabet l:.

1. Moore machine : Output is associated with state

Chapter 2 : Finite Automata [Total Marks -12.5]

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 •

(2)3 =2 associated state, 'b

....
(10)3 =3 associated state, 'h
(11)3 =4 associated state, '4

t t
Ternary Decimal.

Fig. 1-Q. 2(a)

Q. 6(a) Write short noteon Closure propertiesof _


ContextFree Language. .(2.5 Marb)
Ans.:
Closure propertiesof contextfree language
A context free language is closed ~der following operations :
1. Union 2. Concatenation 3. . Kleene star

Context free languageis closed under intersection.


1be intersection of a context-free languagewith a regularlanguageis a context free language. ,
1be CF1. is closed under complementation.
1be CR. is closed under reversal
·1. CFL is closed under union
If Lz and Lz are context-free languages,then Lz u Lz is a context free language.
2. CFL Is closed under concatenation
Ifl..z and Lz are context-free languages,then Lz'L:z is a context-freelanguage.
. 3. CFL Is closed under Kleene Star
If Lis a cont.ext-freeianguage,then L• is a context-freelanguage.
4. CFL Is not closed under Intersection
Context--freelanguagesare closed under intersection.
s. CFL Is not closed under complementadon
1be set of context-free languagesis closedunder complementation.
6. lntereectlon of CFL and RL
If L is a CPL and R is a regularlanguage,then R n L is a CFL.
7. CFL Is closed under revenal
·If Lis a context-free language,.then so is L•.
..:

Scanned by CamScanner
. .
. & -Theoryof Compute~Science(MU) I ·, .· 0(18)-i> ·

Chapter 3 : Regular Expressionsand·~anguages·[Total Marks - 27 .5].

Q. 1(c) Define Regular Expl'8881onand give regular expression for :

(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 •.•• ,•

Fig. ·1-Q. l(c)

:. 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

since, Ixy I n, y must be of the fonn ar Ir > 0.

since I xy I . n, x must be of the form a5•


Now, a0 b0 1 can be writtenas
-c-

as·:: at ::.·- r;n.::::,·::.~::'b11:::, _


78:,;,l
·x c,· y ·:r···· .
-~ '.-.-.>

Fig.1-Q. 2(b)

Step 3 : Let_us check whetherx.f z for L :=.2 belongs to_L


x/~ .. :asci/~n:..s-rbn..:.1·
= a3 a2ran-s-rbn- -1

= 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

Fig. 1-Q. 5(a)

easv-solutions

Scanned by CamScanner
_I Theoryof ComputerScience(MU) 0(18);. 8

NFA to DFA using direct method

Fig. 2-Q. S(a)

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:

1. [ab] Stand fora+ b

2. [0 - 9] Stand for a digit from O to 9

3. [A -Z] Stands for an upper-case l~r

4. [a - z] Stands for a lower-caseletter

5. [O - 9A-Za - z] Stands for a letter or a digit

'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 :

$ grep president emp.~

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.

6. • matches zero or more occurrences of previouscharacter.

7. • matches a single_character.
8. [" pqr] Matches a single character which is not a p, q or r.

9. ,. pat Matchespattern pat at the' beginning of a line

10. pat$ Matches pattern at end of line.


Example ·:
(a) 1be regular expression [aA] g [ar] [ar] wal stands for either "Agarwal" or 'agrawal".

(b) ,:- stands for z:ero or more occurrencesof g.

(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

For example a C stntemcnt x = y + z has the following tokens :

x - An identifier

= - Assignment operator

y = Anidentifier

+ - Arithmetic operator+

z - An identifier

Keywords. identifiers and operators are common examples of tokens.

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 :

(i) Design of digital circuit

(ri} String matching

(rii}Communicationprotocols for information exchange.

(iv} Lexical analysis phase of a compiler.

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.

Chapter.6 Regular Grammar [Total Marks - 25]

Q.1(b) Differentiatebetween PDA and NPDA. (5 Marks)

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

2. Less powerful than NPDA More powerful than a PDA

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

Q. 3(a) Construct PDA accepting the language L a {a2n b" I ~}.


{10 Marks) ·
Ans.:
1. For every pair of a's one x is pushed on to the stack

2. For every b, one x is popped out from the stack._


3. F'mallythe stack shouldcontain the initial stack symbollo-

easv-s011111011s ,· . , .

Scanned by CamScanner
1
' • I Theoryof ComputerScience(MU)
. ,, .· . ;· , /· 0(18) • 10

Transitiontable·(&)
1. 6(Qo. a, Zo) e (q1, Zo)

2. 6(q.. a. Zo) = (<to, X Zo)


3. 6(<1o,a, x) = (qh X)
' 4.. 6(q.. a, x) = (Qo.XX)
5. 6(Qo. b,x) = ('b, e)
6. 6('11, b, x) = (<12, e)
7. 6C'b, e,Zo) = (Cb, e)
Aa:epting throughempty stack

· Thus, thcPDAM=({Qo.q1,Q2}, {a, b}, {x,ZJ},6,Qo.Zi, {tj,})

Q. 4(b) Convertfollowlng CFG to CNF (10 .Marks)


I.

S ASAIAb

B ble
Ans.:
L Nuilable .set of symbols= (B, A)

Re-writing grammar after removing E -production,

weget.

s ASlsAIASAlaela
els

B b

2 Re-writing~ after removing unit productions B, A S), we get ·

S ASISA !ASA laB I~


A b IAS lsA !ASA ·1ae I a

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.

S AS I SA I AC2 I C1B I a [ReplaclngSA by C2]


C2 SA·

A blAS lsA IA~2 IC1B la


B ·b

Chapter 7: Turing Machine (TM) [Total Marks -12.5]

0. 4(a) Construct TM to check well-formedness of parenthesis. (10 Marks)


Ans.:
In cycle, the left-most ')' is written a,; X, then Lhe head moves left to locale the nearer '(' and it is changed to X.

:1be cycles of computatioriare shown below.


Input string is assumed to be (00)().

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)

(hc,R. Cb (4z,x,L) ·((b,B.R)


BIB,I., q~ <1:1 <l.1 Cb Cb
ij

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

.:fTheory of Comp~ter Science (MU)


0(18)-12
/

The Turing machine M is given by :

M = (Q, :E, I', 6, 4,, B, F)


. '

Q = ' (<Jo. <It, 'h• 'b}


= ((. )}
r = ((, ). x, B}

. 6 is given in Fig. 1-Q. 4(aXa) or Fig. 1-Q. 4(~)(b)

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): ·

131() ()) ( .) B 1-=B.( (){)) () B !+:BJ('){)}() ..,


?i . . . . . •:i / •. ·•· ·/ ·_:.·. · ·•··t .. _· .
B rB(x
J~Bl(.x() )() B'i~;B (,ex(}) ()_ x() )() _B
· :;·~:>t ~·•oc·-• · ·· ,. ·. - i •··, •· ·. .. · • ·.·; . "Jo .·/:_:-~:).~_?··'.·.
c.
1...;.B xx c- >) <>_B 1- B <·xx< x >· >B B x x xx> ..> B --- =·
. . .. ·:.
t
c' r. c t
c
:. •.
t.
•Cl) . Q1 C Qo ;0• • .••.• •. ' •
--
. ;·_ ' .
···

l~J,3 ( xx xx)()
•·•

B 1-B( XX xx x() B 1-B.( X xx x·x( )B .1~~~:


:, .. < Jo·<:· ·/ ·:· · :" 4· · : . ·., · l --- :•,·i-~~

·- 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
../
'

.W; Theory of Computer Science (MU) D(18)-13 · . ._

Fig. 1-Q. 4(a)(c)

0. 6(e) Write short note on : Un,lversal Turing Machine. (2.5 Marks) _

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.

2. Machine code is loaded in main memory.

3. Input to the program can also be loaded in memory.

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.

A Turing machine M is designed to solve a particular p~blem p, can be specified as :


1. The initial state <Jo of the TM M.

2. The transition function 6 ofM can be specified as given:


If the current state ?f M is Q; and the symbol under head is 3i then the machine moves to state q1 while changing 3i to 3:i· The move
of tape bead may be :

1. To-left.

2. To-Right or

3. Neutral
Such a move of TM can be represented by tuple

{(<J;,8i,CJ.;.a;.mr>: <L,%,E Q ; 8i,8;,E r ;mc E {To-left, To-Right, Neutral}}

UTM should be able to simulate every turing machine. Simulation of a Turing will involve :

...:.__ _,, ,=,..,-:,-~;-::::~-------:------------ ,---------:--,--.:.._ __ _'._'~----- ,.


.-2il easv-s01u11ons..
. •·. ,_, .,,, ,.., ';, . .

Scanned by CamScanner
. I

., Theoryof ~puter Science (MU) D(18)- 14;


I ·.
. t. F.ncodingbehaviour of a particular TM as a progmin.
2. Execution of the above program by UIM.

A move of .the fonn (<L,&i,q1,8J,Dlc)can be represented~ 101+1 101 to'+ 1 to' 101\

"Where K = 1, if move is to the left


K = 2, if move is to the right
)
K = 3, if move is 'no-move'

State 4, is represented by 0, .

State q 1 is represented by 00,

State q,, is represented by 0.,,.. 1•

Pim symbol can be represented by 0,

Second symbol can be represented by 00 and so on.

Two cicuicntsof a tuple representing a move are separated by 1.

Two moves are separated by 11. .

Executionby UTM : We can assume the UTM as a 3-tape luring machine.


1. Input is written on the first tape.

2. Moves of the TM in encoded fonn is written on the second tape .


3. The current state of TM is written on the third tape.

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 :

1. Next state 2. .Next symbol to be written


. 3. Move of the bead.

Based on this information. the control unit can take the appropriate action.

Chapter 8: Undecidability _and Recursively Enumerable Languages


[Total Marks - 7.5]

- Q. 1(d) Explain Hatting Problem.


(5 Marks) .
Ana.:
Halting problem
The halting problem of a Turing machine states :
Given 8 Turing machine M and an input a, to the machine M, detenulne if the machine M will ev~tually halt ~hen it b given
Input a,.
.•
Halting problem of a Turing machine is unsolvable.

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

on M as its input A machinecan be describedas a string over Oand I.

Step 3 : Let us construct a new Turing machineH3 that takes output of H2 as input and does the following :

1. ff the output of H2 is "loop" then H3 halts.

· 2. ff the output of H2 is "halt'' than H3 will loop forever.

H3 will do the oppositeof the output of Hz.


Step 4 : Let us give H3 itself as inputs to H3•

ff Hi halts on Hi as input then H3 would loop (that is how we constructedit).

ff H3 loops foreveron Hi as input H3 halts (that is how we constructedit).

In either case, the result is wrong.

Hence,

Hi does not exist


H H3 does not exist then Hz does no~ exist.

If Hz does not exist then HI does not exist.

easv-solutions

Scanned by CamScanner
9! Theory of eome&r Science(MU) 0(18)-16

Q. 8(c) Write short note on : Rice'• Theorem. (2.5


Ana.:
Rice'• theorem
.. .
•"Everypropertythat is satisfied by so~ but not all recursi~elyenumerableJan~ f~~n~W' , Any property that.is satisfie.d
by some recumvely enumerable language but not all is known as non-trivial property. We have seen many properties of R.E. languages
that are on-decidable.These propertiesinclude :

1. Given a TM M. is L(M) nonempty?

2. Given a TM M. is L(M) finite ?

3. Given TM M. is L(M) regular? ·

4. Given a TM M. is L(M) recursive?

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

Chapter 1 : Introduction [Total Marks ·- ·1O]

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

S1ep 2 : Raohlq 2 ult prodlldioasfrom G 1 and also receiving non-racbable symbol C,

Weld,
S OAO I0011B1 1111 BB -

OAO I 0011B1 1111 BB Granular G2

111 IBB'

Step 3 : AD die three TUiabla are ldadiall and bmc:e.,the granularbec:oma:


1S1 Ill lSS GranularG 3

Step 4 : Sahdlf@Oo&.\1 for Oand Ai for 1; we get,

I A 1A 1 I A2S A2IA2A2 ISS-

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].

Q.1(a) - Dltrarentlate DFA and NFA. (5 Marks)


~= 11le -
. -~

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) _

4. The number of states is finite. NFA can be in several states at a time.

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 :

(li) NFA to DFA using the direct method

Ca\V SIJ IIIIIIJII S

Scanned by CamScanner
. ~Theoryof~rSdence(MU) M{19)-3

Chapter 3 : R&gular Expressions and Languages [Total Marks 15]

Q. 1(c) · Explain the appllcatlona of regular exprealona. · (5Marb)


Ans.: Plmle nler Q. 6(b) ol Dec. 2018.
Q. 3(a) 0blaln • regular upl"NSlon for the FA ahown below :. (10Marb)

Arm. : Given FA:

Step 1 : .Receivingloop between the states 'kl and qi, we get

Step 2 : .Receivingthe loop among 'kl- q, and Ch, we get

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

w c L (0) with I w I~ n can be rewrittenas w = uvxyz, subjectto the followingconditions:


1. I vxy I~ n, the middleportionis less than n.
2. Yy 'I f:. stringsV and y will be pumped.

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

·o is given by (V. T, P, S).

¢,(G) denotes that largest number of symbols on the right-bandside of a productionin P.

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

root as S. The parsetree T generatesw with smallest numberof leaves.


The tree T will have a path length of at least I V - T I+ 1.'This path will have

I V - T I + 2 nodes with the last node labelledas terminal and remainingnon-terminals.

Fig. 1-Q. 4(b) shows paths in detail.


s

Fl1. t-Q. 4(b): Pa&. In the parse tree

xis generatedby T:i


v is generatedby Tl
u is generatedby T
T1 excludingT2 can be repeatedany numberof times.
This will yield a string of the form uv1xyz where i O

l'.iA f. ,l \ V S U I 11111111 S

Scanned by CamScanner
1, Theoryof ComputerScience(MU)·
: , -.. Chapter 6 : Regular Grammar [Total Marks - 1OJ.

Q. 5(a)' Design PDA for the followlng language:


(10~) '•
. L(M) = {Wcw I w {a,b)-J where wA le reverseof w & c le a constant.
·Ana.;

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

Fig.1-Q. S(a) : A PDA on input abbcbba

The PDA accepting through final state is given by


M = ( {CJo. q1'. 1Jz}. {a. b, c}, {a. b, z.o}, o, <Jo, z.o, {1Jz})

Where the transition function ois given below :


I. . O(<Jo, a.£) = . (CJo, a)
2. OC<Jo, b. t) = (CJo, b)
J ·
Fll'Stn symbols are pushed onto the stack

: :~;E : : :;
[State changes on c]

6. O(q,, £. 7.o) = (q2, 7.o)


J Last n symbols are ·matched with first n symbols in
reverse order

. , [Accepted through final state]


·

A transitionof the form O(<Jo, a.£)= (<Jo, a) implies that always push a, irrespective of stack symbol.

Chapter 7 : Turing Machine (TM) [Total Marks - 20]

Q. 3(b) Explainthe types of Turing machine In detall.


.
. ,(10 Marks)
Ana.:
The types of Turtng machineare as follow• :
L Two-waylnftnlte Turing nuM:fdoe

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.

(jis P. :l S 'J S fJ Ill 11 fl II S

Scanned by CamScanner
t Theory of eomputerScience(MU) ·- M(19)-6

'2. Turmamachinewith muldple ham


A turingmachinewith single tape can have muldplcbcadll.Let 011 conaidera tming with two beadsH1 and ffi..Each bead is
capableof pcrfonningread/write/moveoperationindepcndendy. ·
, BabaabbaBBB
f
. Ha
f .
. H:z

Fil, 1-Q, J(b) : A 1'urlDImachinewith two beads

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•

is the symbol to be written in the cell under "2·

is the movement (L. R. N) of "2-

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.

Fig. 2-Q. J(b) : A two-tape turing machine

1be transition behavior of a two-tape Turing machine can be defined as :


~q,.ai,aJ = (4z,(S1.M1),(S2.MJ)

Where,

q1 is the current state,

4z is the next state,

&i jg the symbol under the head on tape J,

42 jg the symbol under the bead on tape 2,

S 1 jg the symbol written in the,current cell on tape J,

S2 ia the symbol written in the current cell on tape 2,

M 1 is the movement(L, R. N) of head on tape J,

, jg the movement(L, R. N) of head on tape 2.

f! :t sV S ll 111 I I 1111 S ·". .' . .....


, .: ••
, '

. I

Scanned by CamScanner
r . .
. •·

·;...t Theory of.... Computer


. .. ...
Science(MU)
. .,.~-
, M(19)-7 =

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.

An example of non-detenninistic move for a TM is shown in Fig. 3-Q. 3(b).


a/a.,R

Fig. 3-Q. 3(b) : A sample move for NDTM

·The transition behaviour for state CJo for TM of Fig. 3-Q. 3(b) can be written as

6 (<Jo. a) = {(<Jo. a, R)(q 1'. X, R)}

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.

2. Machine code is I~ in main memory.

3. Input to the program can also be loaded in memory.

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.

A Turing machine Mis designed to solve a particular problem p, can be specified as :

I. The initial state CJo of the TM M.

2. 1be transition function 6 ofM can be specified as given:

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

Such a move of TM can be represented by tuple

{(<Ji,lli,'Jj,3i,mJ: 4i,qi,e Q ; 8i,~.e r; m, e {To- left, To-Right, 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.

Where K = 1. if move is to the left

K = 2. if move is to the right

K = 3. if move is 'no-move'
State 4, is representedby 0,

State q 1 is represented by 00.


- . o+ I
Stale q. is represcn~ by O •_

First symbol can be representedby 0,

Secondsymbol can be representedby 00 and so on.

Two clements of a tuple representinga move are separated by 1.

moves an: separated by 11.

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 :

1. Initial configurationof tape :

2. w, is appended to Olz·

!i t~(;' f9.) :f.PJ ~1tr:i --


m2

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

Chapter 8 : Undecidability and Recursively El'.lumerable Languages


[Total Marks - 25]

Q.1(d) What are recursive and recursively enumerable languages? (5 Marka)


Ana.:
Recuslve language

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

eas v S OIIIIIOII S . :: '

Scanned by CamScanner
·.Y Theory of ComputerSci~ (MU)
Reclnlvelyenumerablelanguage
( . .
There is a difference between recursivelyenumerable(Turing Acceptable)and m:unrive (Turing Decidable) language. .,,

Following stalemcotsarc equivalent :


1. The languageLis 'furblsacceptable.
2. The languageL is nmrsin.ly enumerable.

Following statementsarc equivalent


1. The languageLis Turing deddable.

2. The languageLis nmrsive.

3. There is an ~ritbm for recognizingL.

EvecyTming decidable language is Turing acceptable.


Eveiy Tming acceptable language need not be Turing decidable .

Q. 6 Write detailed note on (any two):-


(a) Post correspondence problem
:•. ·
(b) Haltingproblem
(c) Rice'• theorem ·
Ana.:
. (a) Post corrapondence problem
Let A and B be two non-emptylists of strings over l:. A and B arc given as below :

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:

The string Xi x1 ••• X,,, is equal to Yi YJ ..• Ym•

Ezample: To check whether


' 3
A :::51 {a, aha:, ab} and

has a solution.

We will have to find a sequence using which when the elements of A and Bare listed. will produce identical strings.

The required 9c:Queoceis (2, I, I , 3) .

Ai A1 A:, = aba3 a aab = ab a6 b


3
B2 B 1 B 1 B 3 = aba a3 b = aba6 b
lbua , the PCP bas solution.

We arc accepting the un-decidabilityof post correspondenceproblemwithoutproof.

ta f' S V S 1111111 II II S ·•.·, .

Scanned by CamScanner
f Theoryof ComputerScience(MU)· ,M (19)-10~

' (b) Halllng problem

The baitingproblem of a Turing machinestates :

Glftll a Tmtnamachine M and an Input m to lhe rucblne M, determine If the IIUlddneM wlD nmtaally halt when It la pYa
IDpatm.

Halting problem of a Turing machine is unsolvable.


Proof:
Moves of a tming machine can be representedusing a binary number. ThWJ, a Turing machine can be representedusing a string over
1:-(0,1).

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 :

I. If the output of H2 is "loop" than H3 haJts.


2. If the output of~ is "halt" than "3 will loop forever.
M halt --+ Machine H3 loops forever
loop --+ M~chlne H3 halts
H3 will do the opposit.cof the output of ffi.
Step 4 : Let us give "3 itself as inputs to "3.

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).

In either case, the result is wrong.

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.

ca r, ;i S 'J SO 111 I I II II!,

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 : ·

1. Given aTM M. is L(M) nonempty?


2. Given a TM M. is L(M) finite?

3. Given a TM M, is L(M) regular?

4. ·Given a TM M. is L(M) recursive?


to nontrivial property of recursively enumerable
1be Rice's theorem can be proved by reducing some other unsolvable problem
'
language.

' :,

ta f. ,l S S O 11111 Cl II S

Scanned by CamScanner
.• Theoryof eomre;:Science (MU) . 0..:1

Q.1 (a) Explain Chomsky Hierarchy. (5 Marb)


(b) Differentiate between PDA and NPDA. (5 Martes)
(c) Define Regular Expression and giv~ regular expression for·:
(i) Set of all strings over {0, 1} that end with 1 has no substring 00
' 1 • ' , ' '
(5 Marica)
(d) Explain Halting Problem. (5 Marks)

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)

(b) Consider the following grammar


iCtSliCtSeSla
b

For the string 'ibtaeibta' find the following :


(i) _ Leftmost derivation
(ii) Rightmost derivation
(iii) Parse tree
'
(iv) Check If above grammar is ambiguous. · (10 Marks)

Q. 4 (a)° Construct TM to check wett-formedness of_parenthesis. (10 llarb)

(b) Convert following CFG to CNF (10 Marks)


s ASAIAb
A BIS
B ble

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)

Q. 6 Write short note on (any four) (10 Marb)


(a) Closure properties of Context Free Language.
(b) Applications of Regular expression and Finite automata.
(c) Rice's Theorem.
(d) Mealy and Moore Machine
(e) Universal Turing Machine
e :i s v S IIIIIIICIII S

Scanned by CamScanner
·=of-Science(MU) Q-2

Q. 1 (a) Differentiate DFA and NFA. ·(& Marks)

· ' (b) -Design a DFA to accept string of O's and 1's ending with the string 100. (5 Marks)

(c) Explai~ the applications of Regular Expressions. -(5 Marke)


(d). What are Recursive and Recursively Enumerable Languages? (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~)

(b) Design moore rn/c for.following:

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)

(b) Explain the types of Turing machine _


in d~il. (10 Marka)

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)

Q.'5 . (a) _Design PDA for the following language :·

L(M) = {we-,/{I w {a,b}i wh_ere v/!' is reverse of w & c is a constant. - (10 Marks)

(b) Convert the following Grammars to the Chomsky noITnalfoITn (CNF).


S-OAO 11811 BB
A-C
s-s1A
c-Slt (10 Ma~)

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.

.. .. '·

l'.a t! a S V. °' 111111111 II S

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•• .. •••• .. • .. ,,,,,.,.,,,,,,.,,,,,,,,,,,,,,,,, ..................... ,,,,,,

•.......... Database Management System


....................................................................
...................................
........................................
; ......................
...
. .
•...........................................................
MICROPROCESSOR
..................................................................................... J
................................
..
• Theory of Computer Science
........................... . ............................. h ............................ ... .... ,. ... . .. .. ........ . .................. , .. , .. ,,. .. ,.,,.,,., ................ ,,, .. , .. ,,
. i

• I Multimedia System (Dept. Elective I)


..........t........................................................
............................................................................................. ..................
• ·I Advance Operating System (Dept. _Elective I)
. • .........+...............................
...........................................
.............................
..................................................................
i

/ - - - -- - -- -------- ----- --------------- ---------- --- -- ------ -- ------------- -,

r
!
&S
e as s I u i
V- O I Ons
~·&' TechKliowledgl!1
~r pubIi Cat iOns l
\ . I

Paper SolutionsTrusted by lakhs of studentsfrom more than 15 years

Distributors

Student 's Age nc ies ( I) Pvt . Ltd. Bharat Sales Agency


102, Konark Shram, Ground Floor, Behind Everest Goregaonkar · Lane, Behind Central Plaza Cinema,
Building , 156 Tardeo Road, Mumbai. Charnl Road, Mumbai. M :1986572 92797
M :&91672 90777. Ved Book Distributors - Mr. Sachin Waingade
Vidyarthi Sales Agencies (For Library Orders) 1·
Shop. No. 5, Hendre Mansion, Khotachlwadl,157/159, M : 80975 71421 / 92208 77214 .
J.S.S Road, Girgaum, Mumbai. M ;'998197 76110. E : [email protected] ;I
I
'
EM046A Price~ 70/-

BOOKS ARE AVAILABLEAT ALL LEADING BOOKSELLERSII 111111 I

Scanned by CamScanner

You might also like