TCS Book
TCS Book
MU
Theory of
Computer Science
Semester V - Computer Engineering
Same Subject, Same Author with New Publication
Dilip K u m a r Sultania
With Solved Latest University Question Papers '
upto Dec. 2018. _________________I
tj , tn
Tech Knowledge
Publications
Scanned by CamScanner
Theory of Computer
Science
(Code : CSC504)
Semester V - Computer Engineering
(Mumbai University)
University
Tech Knowledge
Publications
*46A)
Scanned by CamScanner
Theory of Computer Science
Dilip Kumar Sultania
(Semester V, Computer Engineering, MU)
Copyright © by Author. All rights reserved. No part of this publication may be reproduced. copied. or
stored in a retrieval system, distributed or transmitted in any form or by any means, including
photocopy, recording, or other electronic or mechanical methods, without the prior written permission
of the publisher.
This book is sold subject to the condition that it shall not, by the w'sy of trade or otherwise, be lent,
resold, hired out, or otherwise circulated without the publisher's prior written consent in any form of
binding or cover other than which it is published and without a similar condition including this condition
being imposed on the subsequent purchaser and without limiting the rights under copyright reserved
above.
This edition is for sale in India, Bangladesh, Bhutan, Maldives, Nepal, Pakistan. Sri Tanb, and
designated countries in South-East Asia. Sale and purchase of this book outside of these countries is
unauthorized by the publisher.
Zry s.
z
‘ • *'/■*
ISBN 978-93-8929 5-3 .7 ' ; /
Published by •<:,
TechKnowIedge Publications
Head Office : B/5, First floor, Maniratna Complex, Taware Colony, Aranyeshwar Comer,
Pune - 41 1 009. Maharashtra State, India
Ph : 91-20-24221234, 91-20-24225678.
Scanned by CamScanner
Preface
My Dear Students*
\ ' * I ' .’ ' X VA’ i * 1 •- * r> ii ' Up c V•
»' < i • * »■ 4.
A large number of solved examples have also been included. Therefore, we are
sure that this book will cater all your needs for this subject.
1 present this book in the loving memory of Late Shri. Pradeepji Lunawat,
our source of inspiration and a strong foundation of “TechKnowledge
Publications”. He will always be remembered in our heart and motivate us to
achieve our milestone.
- Dilip K u m a r Sultania
□□□
Scanned by CamScanner
Syllabus
Course objectives :
2. Build concepts of theoretical design of deterministic and non-deterministic finite automata and
push down automata. ’ r ■. •
1. Identify the central concepts in theory of computation and differentiate between deterministic and
2. Infer the equivalence of languages described by finite automata and regular expressions.
3. Devise regular, context free grammars while recognizing the strings and tokens.
Scanned by CamScanner
Prerequisite : Discrete Mathematics
• NF A to DFA Conversion
• Minimization of DFA
2.2
•
•
RE Applications
____ __________ • I
(Book Code : MO46A)
Scanned by CamScanner
tuns
■ ? 5s f
Bl i i
Theory Tutorial
s ' '
H»-
3.0 Grammars 08 03
• Equivalence of RG and FA
Scanned by CamScanner
W T>
■
Module Topic. 1 heory Tutonal.;
°’ Hrs.
""
(Refer chapter 7)
6.0 Undecidability
----------------- •< ~ - ,-r 03 01
6.1 • Decidability and Undecidability
• Rice‘s Theorem i
*
• Post Correspondence Problem
*• » 1, , 1
Total
39 .13
__________1
1
• f. 9 i ■ , * ■ m 4 * .,
□□□
1’ I * tsi i t > * A * • t
%,
■
w
(Book Code : MO46A)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 1 Table of Contents
1-1 to 1-21
Chapter 1 : Basic Concepts and Finite Automata
1.3. 1 Type 3 or Regular Grammar ...... ..... ........ ............... ........................ I -13
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2
_ _Table of Content*
Scanned by CamScanner
V Theory of Comp. Sei, (MU-Sem. 5-Comp.) 3 Table of Contents
Module 2
Scanned by CamScanner
3.4.2 Arden’s Theorem .......................................................................................................... 3.4?
3.4.2.1 Application of Arden’s Theorem ................................................................................ 3.4g
3.5 FA Limitations ............................................................................. 3.53
3.6 Pumping Lemma for Regular Language ....................................................................................... 3.55
3.6.1 Definition of Pumping Lemma .................................................................................... 3-54
3.6.2 Interpretation of Pumping Lemma .............................................................................. 3-54
3.6.3 Proof of Pumping Lemma ........;....... 3.55
Module 3 1
__ 1
Scanned by CamScanner
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 5 Table of Contents
4.4 Right Linear Grammar to Left Linear Grammar ........................... ........................................... 4-12
5-69
5.5. 1.1 Algorithm for CFG to CNF Conversion ........
5-79
5.5.2 Greibach Normal Form (GNF) .....................
Scanned by CamScanner
V Theory of Comp. Sd, (MU-Som. 5-Comp.) 6 Table of Contents
Scanned by CamScanner
Theory of Comp. Scl, (MU-Sem. 5-Comp.) 7 Table of Contents
Scanned by CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 8 Table of Contents
8.3 Compare Type 0, Type 1, Type 2 and Type 3 Grammars ........................................................... 8-10
□□□
Scanned by CamScanner
Basic Concepts and
*
Finite Automata
I ' » ' 1
Module 1
1 .1 Basics of Languages .
Scanned by CamScanner
CamScanner
Baslc Conce
Theory of Comp, Sei, (MU-Sem, 5-Comp.) 1-2 Pts and F i n i t e Autom
For example.
If co = xyz
Then coR = zyx.
• A string v is a substring of a string co if and only if there are strings x and y such that,
co = x v y, where both x and y could be null
Every string is a substring of itself.
For example,
if co = ‘abcdef is a string..
then *cde’ is a substring of co.
• For each string co, the string co' is defined as,
co0 = £, the empty string
co1 + 1 = co1 • co for each i > 0 where, i is a natural number.
For example,
if co = abb
then co0 = £
co1 = abb '
2 ■'
co = co • co = abbabb
3 2
co = co • co = abbabbabb
• The set of all strings over an alphabet E is denoted by E*.
Z* = Z° u Z1 u Z2 .....
£ String of String of
length 1 length 2
For example,
if E = {0,1}
then Z* = { g , 0 , 1,00, 01, 10, 11,000, 001 }
. A subset of strings over an alphabet Z is a language. In other words, any subset of Z* is
a language.
If E is an alphabet, and L c E*, then L i s a language over E.
For example,
L| = [co 6 {0, 1 }* I co has an equal number of 0’s and I’s]
i.e., L j is a language over alphabets {0, 1 }, having equal number of 0’s and T s .
r. Li = (£,01, 10,0011,0101, 1100, 1010, . . . )
Scanned by CamScanner
CamScanner
V Theory of Comp. Scl. (MU-Sem. 5-Comp.) 1-3 Basic Concepts and Finite Automata
Scanned by CamScanner
Baslc
jr Theory of Comp, Scl, (MU-Sem. 5-Comp.) K Concepts and Finite Automata
• L + is known as the positive closure of L. L* is the set of all finite strings formed by
concatenating words from L. Any word can be used one or more times.
Example : If L = (aa, b) then
L + = { b, aa, bb, bbb, baa, aab, ... )
Example 1.1.1 : Let L be a language. It is clear from the definition that L+ c L*. Under what
circumstances are they equal ?
Solution :
L* always contains an epsilon.
L + may not contain an epsilon.
. * •* ! I IU
Thus, the only difference between L and L* is that L + may or may not contain an
+
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 1-5 . y Basic Concepts and Finite Automata
• 2 .
Example 1.1.2 : Define a language of polynomials recursively. Give derivation for 5x + 7x.
Solution :
A polynomial is a finite sum of terms, where each term is of the form c • x , c and n are
integers.
We should be able to derive a term.
A polynomial can be derived by adding such terms.
Rule 1 : Any number is a term
Rule 2 : The variable x is a term
Rule 3 : If p and q are terms then pq is also a term
Rule 4 : A term is polynomial .
Rule 5 : If the terms p and q are in polynomial, then they are p + q and p - q.
2
Deriving 5x + 7x
.5 is a term - (by Rule 1)
x is a term - (by Rule 2)
5x is a term - (by Rule 3)
(5x) • x is a term - (by Rule 3)
5x is a polynomial - (by Rule 4)
, 7 is a term - (by Rule 1)
7x is a term
5x 2 + 7x is a polynomial - (by Rule 5)
1 .1 .3 Prefix and Suffix of a String
Scanned by CamScanner
Theory of Comp, Sci, (MU-Sem, 5-Comp.) 1-6 Basic Concepts and Finite Automata
1 .2.1 Notations
Scanned by CamScanner
V Theory of Comp. Sei, (MU-Sem, 5-Comp.) 1-7 Basic Concepts and Finite Automata
S = <sentence>
Several productions of <noun> and <verb> can be merged together and the set of
productions can be re-written as :
P = {<sentence> -» <noun> <verb>,
<noun> —> Mohan I Soham I Ram,
<verb> —» eats I plays I reads
}
1.2.2 The Language of a Grammar
If the production rule is applied once, then we write a => p. When it is applied a number
G
♦
of times then we write a => p
G
Derivations are represented either in the
1. Sentential form OR 2. Parse tree form
1. 2.2.1 Sentential Form
Scanned by CamScanner
2 Theory of Comp. Scl. (MU-Sem. 5-Comp.) 1-8 Basic Concepts and Finite Automata
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 1 -9 Basic Concepts and Finite Automata
s
A
e 0 B
Scanned by CamScanner
Baslc Conce
Theory of Comp, Scl, (MU-Som. 5-Comp.) 1-1° Pts a n d Finite Automata
6
(e) B -> IB (0 B -> e
Fig. Ex. 1.2.2
Leftmost derivation of 1001 is shown by series of Figs. Ex. 1.2.2(a) to (f).
(ii) Parse tree for rightmost derivation of 1001.
s
s
A
A
1 I
0 B
0 B
s
S
A
A 1 I
0
0 B
E e
(k) B - > e
Fig. Ex. 1.2.2
Scanned by CamScanner
” Theory of Comp. Sci. (MU-Sem. 5-Comp.) 1-11 Basic Concepts and Finite Automata
0
A s 1 *— [Production S -> 0S1]
o///AS \
Fig. Ex. 1.2.3 : Derivation of 000111
Example 1.2.4 : Let G = (V, T, P, S) be the CFG having following set of productions. Derive
the string “aabbaa” using leftmost derivation and rightmost derivation.
S -> aAS I a, A -> SbA I SS I ba
Solution : v .
(i) Leftmost derivation
Leftmost derivation of aabbaa is being shown with the help of the parse tree.
1. S 2. s
/l\ /l\
a a S a A S
S->aAS
A
S b
co
co
(C
T
Scanned by CamScanner
CamScanner
Ifteory of Comp, Sci, (MU-Sem. 5-Comp.) Basic ConceptsandFjnj
(ii) Rightmost derivation
Rightmost derivation of aabbaa is being shown with the help of the parse tree.
1. S 2. S
a
S -> aAS
a
S 4. S S
a
A ? a A S a A S
b A a /l\. I
b s b A a
A->SbA b a a b a
A -> ba
4
r
Fig. Ex. 1.2.4(b)
S -> aAS -> aAa —> aSbAa -> aSbbaa -> aabbaa
Example 1.2.5 : For the grammar given below
Set of productions P
I F->(E)lalb
Start symbol = E
(i) Derivation in sentential form
[Using production E T]
[Using production T —> T * F]
[Using production T —> F]
[Using production F —» (E)]
[Using production E -» E + T]
[Using production E —> T]
[Using production T -> F]
Scanned by CamScanner
CamScanner
Theoty of Comp. Scl. (MU-Som. 5-Comp.) 1-13 Basic Concepts and Finite Automata
A
E + T
I I
T F
a
' T *
r
F b
'
- a
( E )
A
- + T
T F
II
F „b
I ■
a
Fig. Ex. 1.2.5 : Parse tree for (a + b) ♦ a + b ,
1 .3 Chomskey Hierarchy
MU ■ Dec. 2009, Dec. 2012, May 2013, May 2014, Dec. 2014.
M"Tiay2015, Dec. 2016, May 2017, Dec. 2017
A grammar is called Type 3 or regular grammar if all its productions are of the following
forms :
A —> e
Scanned by CamScanner
Theory of Comp. Scl, (MU-Sem. 5-Comp.) 1-14 Basic Concepts and Finite Automat
A —> a
A —> aB .
A —> B a
Where, a g Z and A, B e V.
A language generated by Type 3 grammar i s known as regular language.
A grammar i s called Type 2 or context free grammar if all its productions are of the
following form A -» a where A e V and a e ( V u T ) *.
V is a set of variables and T is a set of terminals.
The language generated by a Type 2 grammar is called a context free language, a regular
language but not the reverse.
A grammar i s called a Type 1 or context sensitive grammar if all its productions are of
the following form.
a ->P,
Where, P is atleast as long as a.
Example 1.3.1 : Write a set of production for the strings of the form a n b n c n .
Solution :
S aSBC I aBC
P = CB —> B C , aB —> ab, bB —> bb, ►
1
"■'SCBC)"" [by applying S —> aSBC, (n — 1 ) times]
1
“■’aBC(BC)"’ [by applying S -> aBC]
1
“■'abC(BC)"' [by applying aB -> ab]
Scanned by CamScanner
WF Theory of Comp. Sci. (MU-Sem. 5-Comp.) 1 -1 5 Basic Concepts and Finite Automata
Finite automaton or finite state machine can be thought of as a severely restricted model
of a computer.
• • Every computer has central processing unit; it executes a program stored in a memory.
This program normally accepts some input and delivers processed result.
• The word Automata is for automation. A system where energy and information are
transformed and used for performing some functions without direct involvement of men
is called automaton.
• Finite automata are also called a finite state machine.
• A finite state machine is a mathematical model for actual physical process. By
considering the possible inputs on which these machines can work, we can analyse their
strengths and weaknesses.
• Finite automata are used for solving several common types of computer algorithms.
Some of them are :
1. Design of digital circuits.
2. String matching.
3. Communication protocols for information exchange.
4. Lexical analyser of a typical compiler.
1.4.1 Working of a Finite Automata
Scanned by CamScanner
Basic Concepts and Finite Automata
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 1/16
CT
cr
CT
cr
CT
CT
CT
CT
CT
State (Q)
0
0
0
0
0
Fig. 1.4.2 : State transition of T-fllp-flop under the input 101110011
From the Fig. 1.4.2, it should be clear that every even number of l ’ s in input will make
the T-flip-flop toggle twice and thus it will be back to q 0 . Thus we can conclude the following :
1. If the initial state of flip flop is q 0 , it will come back to q0 if the input number contains
even number of l’s.
2. A single T-flip flop can be used as a machine for checking whether a binary number
contains an even number of l’s.
The machine starts with initial state
b a
q and after feeding the binary number, if
the machine is found to be in q 0 , the input
binary number contains an even number
of l’s. >q0 is the initial state
-q
Working of finite automata can be Finite §5 qj and q are final
understood with the help of an abstract Control states. Final state
is for answer
model of finite automata. It is shown in 'yes’
Fig. 1.4.3.
Fig. 1.4.3 : Model of a finite state machine
Operation of the finite automata is given below :
• Input string is fed to the machine through a tape. Tape is divided into squares and each
square contains an input symbol.
• The main machine is shown as a box. Machine could be in any of the internal states
(q<)> Ap Az* A3’ A4’ As)*
• Initially, the machine is in the starting (initial) state q0 . Reading head is placed at the
leftmost square of the tape.
• At regular intervals, the machine reads one symbol from the tape and then enters a new
state. Transition to a state depends only on the current state and the input symbol just
read.
5 (q P Ap => Aj
Machine transits from qj to on input A r
• After reading an input symbol, the reading head moves right to the next square.
Scanned by CamScanner
CamScanner
om
j_nTeor of_CQmp ScL2MU2§gnk ~ P0 1~17 Basic Concepts and Finite Automata
An alphabet is a finite, non empty set of symbols. Conventionally, the symbol E is used
:
for an alphabet. Common alphabet include :
1. E {0, 1}, the binary alphabets. «
2. E {A, B, ..., Z ) , the set of uppercase letters. :•••'•" , J-
3. E {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, the decimal alphabets.
f •' I ;' ■' < • ,;ti t H
4. E {0, 1, 2}, the ternary alphabet.
Scanned by CamScanner
Basic Conce
.T Theory of Comp. Sci. (MU-Sem. 5-Comp.) 1-18 Pts and
™te Automata
For example,
if Z = (0,1}
then 221 = (0, 1}
Z 2 = (00,01, 10, 11}
and £° = ( e )
The reversal of string co is denoted by co .
For example,
If co = xyz
t>
Then co = zyx.
A string v is a substring of a string co if and only if there are strings x and y such that,
co = x v y, where both x and y could be null
Every string is a substring of itself.
For example,
if co = ‘abedef is a string.
then *cde’ is a substring of co.
For each string co, the string co1 is defined as,
co = E, the empty string
1+ 1
co = co1 • co for each i > 0 where, i is a natural number.
For example,
if co - abb
then co = E
co1 = abb
2
co = co • co = abbabb
3 2
co = co • co = abbabbabb
The set of all strings over an alphabet £ is denoted by £*.
Z* = Z° U Z‘ U z2
E String of String of
length 1 length 2.
For example,
if Z = {0, 1}
then = { e , 0 , 1,00,01, 10, 11,000,001 }
Scanned by CamScanner
” Theory of Comp. Sci. (MU-Sem. 5-Comp.) 1-19 Basic Concepts and Finite Automata
1. 4.2.3 Languages
Finite automata are used for solving several common types of computer algorithms.
Some of them are :
(i) Design of digital circuit
(ii) String matching
(iii) Communication protocols 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 we L, where L i s a regular language.
Scanned by CamScanner
j* Theory of Comp. Scl. (MU-Som, 5-Comp,) 1-20 Barjlc Concepts and Finite Automata
Finite state machine is a more general form of finite automata. FSM can also generate
output. Finite automata that we have discussed so far have a limited capability of cither
accepting a string or rejecting a string. Acceptance of a string was based on the reachability of
a machine from starting state to final state. Finite automata can also be used as an output
device. 4, 1**1 ' I1 f • 4
Set of output alphabet O can be different from the set of input alphabet X
2. Moore machine : Output is associated with state.
X:Q-»O
Dec, 2009
Q. 1 Write note on Chomsky Hierarchy. (Section 1,3)
(5 Marks)
May 2010 ]
Q, 2 State applications of Finite Automata In brief. (Section 1,4.3) (3 Marks)
Scanned by CamScanner
_______ Basic Concepts and Finite Automata
Q.7 Write short note on : Chomsky Hierarchy of Grammar (Section 1.3) (5 Marks)
May 201 5
• - * <. • • •' ; r ‘ i r ‘ V •
Q.9 Explain with an example the Chomsky hierarchy. (Section 1.3) (5 Marks)
Dec. 2016
I May 2017 |
Dec. 2017
Chapter Ends...
□□□
Scanned by CamScanner
Finite Automata
Module 1
Finite automaton or finite state machine can be thought of as a severely restricted model
of a computer.
• Every computer has central processing unit; it executes a program stored in a memory.
This program normally accepts some input and delivers processed result.
• The word Automata is for automation. A system where energy and information are
transformed and used for performing some functions without direct involvement of men
is called automaton.
• A finite automata are also called a finite state machine.
• A finite state machine is a mathematical model for actual physical process. By
considering the possible inputs on which these machines can work, we can analyse their
strengths and weaknesses.
• Finite automata are used for solving several common types of computer algorithms.
Some of them are :
1. Design of digital circuits.
2. String matching.
3. Communication protocols for information exchange.
4. Lexical analyser of a typical compiler.
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-2 Finite Automata
Qi % Qo % % %
From the Fig. 2.1.2, it should be clear that every even number of l’s in input will make
the T-flip-flop toggle twice and thus it will be back to q 0 . Thus we can conclude the following :
1. If the initial state of flip flop is q0 , it will come back to q0 if the input number contains
even number of l’s.
2. A single T-flip flop can be used as a machine for checking whether a binary number
contains an even number of l’s.
The machine starts with initial state
Input
the machine is found to be in q0 , the input Tape
binary number contains an even number
of l’s.
Working of finite automata can be q o l s t h e initial state
understood with the help of an abstract q j a n d qjare final
Finite
Control states. Final state
model of a finite automata. It is shown in Is for answer
Fig. 2.1.3.
Scanned by CamScanner
2f fhe°ry of Comp. Sci, (MU-Sem, 5-Comp.) 2-3 Finite Automata
8 ( q ? Ajj q. . r ’ t
Machine transits from q.* to q.J on input A..
* * ' 'J <
• After reading an input symbol, the reading head moves right to the next square.
• This process is repeated again and again, i.e.
1. A symbol is read. ,. ( ; z -
2. State of the machine (finite control) changes.
’ * • i • * *I
An alphabet is a finite, non empty set of symbols. Conventionally, the symbol Z is used
for an alphabet. Common alphabet include :
1. ‘ Z {0, 1 }, the binary alphabets.
2. Z {A, B, ..., Z}, the set of uppercase letters.
3. Z { 0 , 1,2, 3,4, 5, 6, 7, 8, 9), the decimal alphabets.
4. Z {0, 1, 2), the ternary alphabet.
2.1. 2.2 Strings (words)
Scanned by CamScanner
Theory of Comp. Scl, (MU-Sem. 5-Comp.) 2-4 Finite Automata
A string may have no symbol at all, in this case it is known as an empty string and is
denoted by e.
The length of a string is number of positions for symbols in it. The standard notation for
the length of a string cd is I co I.
For example,
If cd = 01101
then I cd I = 5
Length of an empty string is zero.
i.e., I 8 1 = 0. '.
The exponential notation is used to express the set of all strings of a certain length.
= set of strings of length k, each of whose symbol is in £.
?
For example, ' • . v. <’■ .
if Z = {0, 1}
then S 1 . = {0, 1}
L 2 = {00,01, 10, 11} .
and Z° = {£} ■• ••
The reversal of string cd is1 denoted by cd .
' I ‘ -!
For example,
If cd = xyz
r—— R - n■' 4• ,
Then cd = zyx.
A string v is a substring of a string co if and only if there are strings x and y such that,
cd = x v y, where both x and y could be null
Every string is a substring of itself.
For example, - . . , .
if cd = ‘abcdef is a string.
then ‘cde’ is a substring of cd.
For each string cd, the string cd1 is defined as,
cd° = e, the empty string
cd1 + 1 = cd* • (D for each i £ 0 where, i is a natural number.
For example,
if co = abb
. o , . . •
then cd = e
CD1 = abb
2
cd = cd • cd = abbabb
3 2
cd = cd • cd = abbabbabb
j. • L ;iv;. f -j’T fi‘
Scanned by CamScanner
2 5
Theory of Comp, Scl, (MU-Sem, 5-Comp.) ’ Finite Automat
Z* = Z° U Z l U Z2
Finite automata are used for solving several common types of computer algorithms-
Some of them are :
(i) Design of digital circuit
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-6 Finite Automata
Scanned by CamScanner
Th
-X eory of Comp, sei, (MU-Sem, 5-Comp.) 2-7 Finite Auton
— states .
The Fig. 2.2.2 shows the state transition diagram of the transition function discussed in
this section.
Starting state
Scanned by CamScanner
V Theory of Comp. Scl, (MU-Sem. 5-Comp.) 2-8 Finite Automata
Therefore,
8*
(q 00101 10) => (q p e) and so the input 0010110 is accepted as q, is a final state.
Designing a DFA is like writing a program. In a program, the current state of a program
is given by :
1. Values stored in variables.
2. Current line of execution.
In case of a DFA, variables are not used. Everything is remembered through explicit
• states. Transition function of a DFA is similar to algorithm. While designing a DFA, one has to
concentrate on four issues.
Scanned by CamScanner
Finlte Autorr|
JieoryoLComp. Scl. (MU-Sem. 5-Compj 2-9 ___________ = ata
These are :
1. Number of states 2. Transition function |
3. Start state 4. Final states |
Number of states
Number of states depends on “what must be remembered as input symbols are read by a
DFA”
For example,
1. DFA to check whether a binary numbers has even number of 1 *s :
DFA has to remember whether the number of l ’ s seen so for is even or odd. It does not
have to count number of 1’ s. Let us look at a binary number given below :
0 1 1 0 1 1 0 0 1 1
Numberofl even odd even even odd even even even odd even
• Machine will have two states :
(i) One corresponding to even number of l’s, so far.
(ii) One corresponding to odd number of l’s, so far.
• These two states are necessary and sufficient to conclude whether a binary
number contains even number of l’s.
2. DFA to check whether a string over alphabets (a, b) contains a substring abb :
DFA has to search for substring abb in input stream. Input string is not stored in a
memory variable. Input is made one symbol at a time. As the substring is of length 3, it
may be necessary to remember the preceding two symbols.
• Preceding symbol is a. (a is the first symbol in abb).
• Preceding two symbols are ab. (ab constitutes first two symbols of abb).
• DFA has already seen the substring abb in the input string. Once the substring abb
is found in input string, this status will not change irrespective of what follow*
thereafter in the input string.
• Preceding symbols are neither a nor ab, and abb has not come earlier.
The machine will have four states, each standing for one of the four situations.
Transition function
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-10 Finite Automata
For example
1. Give transition function for a “DFA to check whether a binary number has even number
ofl’s”.
We have already seen that the corresponding DFA will have two states.
(i) State q 0 , indicating even number of l ’ s seen so far.
(ii) State q p indicating odd number of T s seen so far.
• 0 as next input will have no effect on number of l’s.
• 1 as next input will make the transition from q0 to q ( and from q t to q 0 . Thus, the
transition behaviour can be described using the Fig. 2.2.3.
8 (Qo>O) %
5 ( q 04 ) = > q i 0 0
8 (q p 0) => q }
8 (Qpl) —*> Qo
(a) Tabular form (b) Transition table (c) Transition diagram
Fig. 2.2.3 : Transition behaviour
2. Give transition function for a “DFA to check whether a string over alphabets (a, b)
contains a substring abb”. . •
We have already seen that the corresponding DFA will have four states.
1. State q p preceding symbol is a.
2. State q 2 , preceding two symbols are ab.
3. State q 3 , substring abb has already been seen in input string.
4. State q 0 , situations q p q 2 , or q 3 are not there.
State of the DFA after every input symbol for a sample input data is shown in Fig. 2.2.4.
Nothing of
relevance
Is seen. First a First a
It is looking f o r of a b b a b of a b b of a b b Entire abb Entire abb
first a . Is seen i s seen I s seen is seen is seen
Input
I __ b a a b b ' a
— b J q3 — ► q3
I— q 0 — ►qo —►qo Al ■ >q 2 Hi > 1 q2 A3
States
Scanned by CamScanner
jj Theoty of Comp. Scl. (MU-Sem. 5-Comp.) ■ 2-1 1
Scanned by CamScanner
CamScanner
2-12 Finite Automata
a b
Ao
a b b
2 3
a
Scanned by CamScanner
,W — ' , , Finite Automata
__£_Theory of Comp. Scl. (MU-Sem. 5-Comp.) _2-13
. q 0 is the starting state. Zero number of l ’ s implies that number of 1’s is of the
form 3n.
• q 0 is a final state. Machine will be in q 0 if number of l’s seen so far is multiple
of 3.
(b) Number of l ’ s is not multiple of three. Let L be a language over alphabets {0, 1 } such
that number of l ’ s is multiple of 3.
Thus,
L = [d)G {0, 1 }* I no. of l’s in co is multiple of 3]
Complement of L is given by :
L' = [cog {0, 1)* I no. of l ’ s in co is not multiple of 3]
DFA for L' can be obtained through following modifications on DFA for L.
1. Every final state of L should become a non-accepting state in L'.
2. Every non-accepting state of L should become a final state in L'.
o o
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-14 Finite Automata
(Qp 1) %
An input 0 in state q 2, will make number of 0’s even.
;
8 ( q 2, 0) => q 0 '
An input 1 in state q 2 , will make number of l ’ s odd.
S (q 2, 1 ) —
An input 0 in state q 3 , will make number of 0’s even.
5 (Q3J 0) Qj
An input 1 in state q 3 , will make number of l’s even.
8 (q 3, 0) => q 2
q0 is the starting state. An empty string contains even number of 0’s and even number
of l’s.
qgis a final state. q 0 stands for even number of 0’s and even number of l’s.
0 0 0
lo
Scanned by CamScanner
Finite Automata
0 1
Ho
” 9o 92
0 0 0 0 9i 93 9o
1
92 9o 93
Ha *
93 9i 92
Example 2.2.3 Design a DFA which accepts the odd number 1’s and any number of 0’s
over Z = {0,1}.
Solution :
The DFA must keep track of number of 1’s in the 0
r\°
string already seen by it. CA 1
• The number of 1’s seen could be even, state q 0 . •—
Scanned by CamScanner
2-16 . Finite Automata
q l0 - number of l*s is 3m
q | t - number of l’s is 3m + 1
o
0 0 0 0
1 -d o
J| \ ; 0 1
“ 00 9io
9oi 9n 9o2
%2 912 9oo
9io 920 Qn
Qn 921 912
910 930
• An 932
9 o 9oo 041
Scanned by CamScanner
Example 2.2.5 : Draw DFA for the following language over {0, 1 } :
(I) All strings of length at most five
(II) All strings with exactly two 1’s
(III) All string containing at least two 0’s.
(Iv) All strings containing at most two 0’s.
(v) All strings starting with 1 and length of the string i s divisible by 3.
Solution :
(i) All strings of length at most five
',0,1
0 1
*o*-*M*en
T
3, 3r
CT
CT
3z 32
CT
3j 3,i
CT
34 34
CT
3s 3S
3* 3+
30 3+
(b) State transition table
Fig. Ex. 2.2.5 : Final DFA for Example 2.23(1) with explicit failure state
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-18 Finite Automata
qi Qi
* o2 o2
0i
*
q2 q3 q3
*
o3 04 O4
o4*
*
q5
(d) State transition table
Fig. Ex. 2.23 : Final DFA for Example 2.2.5(i) without a failure state or
with failure transition omitted. .
Scanned by CamScanner
Finite Automata
First two 0’s will take the machine from q 0 to q 2. Thereafter, the machine remains in q,
as the string should contain at least two 0’s.
0 9i
*
q2
0,1 q* ■ %
(i) State transition diagram (j) State transition table
(V) All strings starting with 1 and length of the string Is divisible by 3 :
Step 1 : A machine M., representing a DFA accepting a string starting with 1 is given in
Fig. Ex. 2.2.5(k) :
M1 = ----►
”0
0,1 ’
Scanned by CamScanner
V Theory of Comp. Scl. (MU-Sem. 5-Comp.) 2-20 Finite Automata
m2 =
io
— Pb
0,1
0,1
Solution :
Scanned by CamScanner
Finite Automata
Theory of Comp, Scl. (MU-Som. 5-Comp.) 2-21
a b
n
b b
*lo)----► - -----fa) ----->
a.b
— a.b q,
CT
CT*
a
el
C
CT1
O*
CT
rp
q
a *
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-22 Finite Automata
q0 to q0 on input ‘b’ :
First character in ‘abb* is a.
q0 to qj on input ‘a’ :
qj is for preceding characters as ‘a*, first character of abb.
q t to q T on input ‘a’ :
An input of ‘a’ in state q t will make the preceding two characters as ‘aa’. Last a will
still constitute the first ‘a’ of abb.
q t to q 2 on input ‘b’ :
q 2 is for preceding two characters as ‘ab’ of ‘abb’.
q 2 to q t on input ‘a’ :
An input ‘a’ in q 2 will make the preceding three characters as ‘aba’. Out of the three
characters ‘aba’, only the last character ‘a’ is relevant to ‘abb’.
q 2 to q 3 on input b :
q 3 is for preceding three characters as ‘abb’.
q 3 to q3 on input a or b :
The substring ‘abb’ has been seen by the machine and a new input will not change this
status.
(c) All strings ending in abb
This can be seen as an extention of solution which is given in Example 2.2.6(b) as the
substring ‘abb’ should be at the end of the string. Transitions from q 3 should be modified to
handle the condition that the string has to end in ‘abb’.
b
L a Z 3 io
q 3 to qj on input a :
An input of a in q 3 will make the previous four characters as ‘abba’. Out of the four
characters as ‘abba’ only the last character ‘a* is relevant to ‘abb’.
q 3 to q0 on input b :
An input of b in q 3 will make the previous four characters ‘abbb’. Out of the four
characters ‘abbb’, nothing is relevant to ‘abb’.
Scanned by CamScanner
em. 5-Comp.) 2-23 Finite Automata
%
So
9i
*
92 9o
Scanned by CamScanner
Finite Automata
MU - Dec. 2007
Solution
o
O°
Scanned by CamScanner
Finite Auto t.
JT Theory of Comp. Scl. (MU-Som. 5-Comp.) 2-25
Example 2.2.10 : Design a DFA for set of strings over {a, b} in which there are at least
occurrences of b between any two occurrences of a.
Solution :
”0 a b
o c
* 73
Qi %
*
91 <t> <h
*
2 * Qo
(a) State transition diagram (b) State transition table
Fig. Ex. 2.2.10 : Final DFA (without explicit failure state) for Example 2.2.10
• An input *a’ in q 0 takes the machine from q 0 to q r Before the next ‘a’ can come, there
should be at least two b’s taking the machine from q t to q 2 and from q 2 to q 0 .
• An input *a’ in either qj or q 2 causes a failure.
q3 b of sequence ba
q4 —> ba of sequence ba
b
Transitions :
• Input a in q 0 takes the machine to q , as the first character ‘a’ of ‘ab’ is the preceding
character.
• Input b in q 0 takes the machine to q 3 as the first character ‘b’ of ‘ba’ is the preceding
character.
• Input ‘a’ in q] makes the preceding two characters as ‘aa’. Out of ‘aa’ only the
Scanned by CamScanner
CamScanner
o
o
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-26 Finite Automata
Input ‘a’ in q 2 makes the preceding two characters as ‘ba’. Machine enters the state q 4
which stands for previous two characters as ba.
Input b in q 2 makes the preceding two characters as ‘bb’. Out of ‘bb’, only the last
character ‘b’ is relevant to ‘ba’ and hence the machine enters the state q 3 .
Similar explanation can be given for q 3 and q 4 .
Example 2.2.12 : Design an DFA for set of all strings over {a, b} containing both ab and ba as
substrings.
Solution :
a
' b
a a
a a
b
•
Fig. Ex. 2.2.12 : DFA for Example 2.2.12
Machine takes the path q 0 - q , ~ q 3 - q 5> if the first substring is ab and the next substring
is ba.
Machine takes the path q 0 - q 2 - <1, - q 5 . if *6 first substring is ba and the next substring
isab. >
Example 2.2.13 : Design a FA that reads string defined over S = {a,b} and accepts only those
strings which end up in either ‘aa’ or ‘bb’.
Solution :
Scanned by CamScanner
T h e o r y o f Comp. Scl. (MU-Sem. 5-Comp.) 2-27 Finite Autorng ta
* a of sequence an is seen.
q 2 —> aa of sequence aa is seen.
q 3 —» b of sequence bb is seen.
q 4 —» bb of sequence bb is seen.
Example 2.2.14 : Design a DFA for set of all strings over {a, b} containing neither aa nor
as a substring,
Solution :
Machine enters the failure state q on seeing either aa or bb.
An input b is state q j will make preceding
two characters as bb.
An input a in state q 2 will make the
preceding two characters as aa.
Machine moves from q 1 to q 2 and q 2 to q j as
long as the input sequence contains a and b
alternatively.
Scanned by CamScanner
2-28 Finite Automata
0 0 0 VO
0 0 0
If the sum total of number of l ’ s before 0 and number of 1’s after 0 is 4, the machine
enters a failure state.
Meaning of various states :
q 0 —> number of l ’ s before 0 is zero.
q —> number of 1 ’s before 0 is one. — <' >
q 7 —> number of l ’ s before 0 is two. • . s
V vv
0 /2 /1 /2 ◄----- Number of 1's after
Scanned by CamScanner
? Theory of Comp. Scl. (MU-Sem. 5-Comp.) 2-29 Finite Automata
Example 2.2.17 : Design a D F A for set of all strings over {0, 1} s u c h that strings either begin
o r e n d (or both) with 01 .
Solution :
o
o
o o
Fig. Ex. 2.2.17 : DFA for Example 2.2.17
MU - Dec. 2006
Solution : 0
0 0
0
0
0
Scanned by CamScanner
Finite Automata
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-30
Transitions :
• An input 0 in q 4 will make the preceding three characters as 000, machine moves to
state q0.
• An input 1 in q4 will make the preceding three characters as 001, machine moves to state
<Ir
An input 0 in q 5 will make the preceding three characters as 010, machine moves to
state q 2.
An input 1 in q 5 will make the preceding three characters as O i l , machine moves to
state q r
An input 0 in q 6 will make the preceding three characters as 100, machine moves to
state q 4.
An input 1 in q 6 will make the preceding three characters as 101, machine moves to
state q 5.
An input 0 in q7 will make the preceding three characters as 110, machine moves to
state q 6 .
An input 1 in q 7 will make the preceding three characters as 111, machine moves in
state q7.
‘aaa’ or
Example 2.2.19 : Construct a DFA for set of strings containing either the substring
‘bbb’
Solution
*
b
b
b
Scanned by CamScanner
▼ Theory of Comp. Sci, (Mll-Sem. 5-Comp.) 2-31 „ Finite Au tomato
Step 2 : Complementing the DFA by reversing a non-final state to final state and a final
state to non-final state.
o
-1
\ o K 1 0
Example 2.2.21 : Design a DFA that reads strings made up of letters in the word ‘CHARIOT
,eC S S,ri
Multan : °‘"’” "° S ,lw
'‘“ d
A,H,I,O,T,R
Scanned by CamScanner
V Theory of Comp; Sci. (MU-Sem. 5-Comp.) 2-32 Finite Automata
'I • c H A R I O T
“No <11 % <lo <lo Ao Qo
Ql Qi <lo <12 Ao q0 9o <lo
<12 <li % Qo <lo Io <13
* m
cr q3 <13 <13 q3 q3 <13 q3
Example 2.2.22 : Design a FA that reads strings made of letter in the word 'UNIVERSITY’
and recognize these strings that contains the word UNITY as substring.
Solution
e,i,n,R,s,t,v,y
U \ X N
EJ.R.S.T.V.Y
E,N,R,S,T,V,Y
E.NJ.R.S.V.Y
E,I,N 1R t S I T,V
Scanned by CamScanner
q4 : First four characters UNIT of ‘UNITY’ arc the preceding four characters.
q5 : Entire ‘UNITY’ has been seen.
2.2.4.3 Examples of Divisibility
Remainder 1 0
next input 0 (10) 2 MOD 3 = (10)
Remainder 10
(101) 2 MOD 3 = (10)
next input 1
Scanned by CamScanner
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-34 Finite Automata
o
* 9o Qi
0
9> q2
q2 91 92
Example 2.2.24 : Design a DFA which can accept a ternary number divisible by 4.
Solution :
A ternary system has three alphabets.
S = {0,1,2}
Base of a ternary number is 3.
The running remainder could be :
(0) 3 0 —> associated state, q 0
(1) 3 1 -> associated state, qj
(2) 3 2 —> associated state, q 2
(10) 3 3 —> associated state, q 3
Ternary Decimal
Transition behaviour will depend on the current remainder (current state) and the next
input. These two will be used to get the next remainder (next state).
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-35
0 20 = > 6 M 0 D 4 => 2 (q 2)
3
1 21 = > 7 MOD 4 => . <q3 )
(q 2 ) 2
2 22 = > 8 MOD 4 => °(q 0)
1 _ _ _ _2_
to qt q2
xO o
2
b))— ► q0 qi
qa q0
0
0 q2 q.>
MU - Dec. 20171
Solution :
A ternary system has three alphabets
S = {0, 1 , 2 }
Base of a ternary number is 3.
Scanned
Scanned by
by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-36 Finite Automata
Decimal
2
o
—3—
Example 2.2.26 : Design a DFA which can accept a decimal number divisible by 3.
Solution:
A decimal system has 10 alphabets.
Z = (0, 1 , 2 , 3, 4, 5, 6, 7, 8, 9)
The running remainder could be
0 —> associated state, qQ . ..
1 —> associated state, qj
2 —> associated state, q 2
Transition behaviour will depend on the current remainder (current rate) and the next
input These two will be used to get the next remainder (next state).
• Next digit as 0, 3, 6, 9 will be mapped to the same next state.
• Next digit as 1, 4, 7 will be mapped to the same next state
• Next digit as 2, 5, 8 will be mapped to the same next state.
Thus it is sufficient to consider transition for just three inputs :
0 for (0, 3, 6, 9)
1 for (1,4, 7)
2 for (2,5,8)
f ;
’ r ■ ' .. •»; ■'.•'•‘ltd • ' ' ‘ •
Scanned by CamScanner
JjLl!l eory of Comp. Sei, (MU-Sem, 5-Comp.) j-3?
Finite Au
j2!
Transition behaviour is explained below :
Current remainder Next input Calculation of remainder Remainder
0 20 => 20 MOD 3 2 (q 2)
2 1 21 => 21 MOD 3 O(qo)
2 22 => 22 MOD 3 W
~»qo 9o 91 q2
2,5,8
q
i 91 q2 q«
q q
2 q2 q« i
(a) State transition diagram (b) State transition table
Fig. Ex. 2.2.26 : DFA for Example 2.2.26
MU - Mav 2007
Solution :
Let us assume that E = {0}
For unary number to be divisible by 3, the number of 0’s should be multiple of 3. The
FSM is given in Fig. Ex. 2.2.27
o
Fig. Ex. 2.2.27
Example 2.2.28 Design a Finite State Machine for divisibility by 5 tester of a given decimal
number.
Solution :
A decimal number will be divisible by 5 if the rightmost digit is either ‘O’ or ‘5’.
Scanned by CamScanner
2-38 Finite Automata
1,2,3,4,6,7,8,9
Fig. Ex. 2.2.28
(i)
X
y.z
y.z
o X
y
0
x,z
-o 0
0
x»y
x,y
(Hi)
Scanned by CamScanner
F n te A U t
y Theory of Comp. Sci. (MU-SeroS-Comp.) 2-39 _ _ ===== ------ ‘ ' °
That is, the language of DFA M is the set of strings accepted by M . The language of a
DFA is also known as regular language. 8*(q 0 , co) stands for a series of transitions starting
from q 0 .
Example 2.2.30 : Describe the language accepted by the deterministic finite automata shov
in Fig. Ex. 2.2.30.
a
a b
a,b
’a,b
Fig. Ex. 2.2.30
Solution
(i)
b
rig. r.x.
a,b
a,b a,b
Scanned by CamScanner
Finite Automata
Example 2.2.31 : Consider the following transition diagram in Fig. Ex. 2.2.31.
Test whether the string 110101 is accepted by the finite automata
represented by above transition diagram. Show the entire sequence of
states traversed.
0/ 10 . 1 jo. 10;- T r. c .
i T i J
;
V/ i -
A(q 2, 101)
4>(q 3, 0 1 )
=kq v i)
=>(q 0. e) => Qo •
String 1 10101 will be accepted as q 0 is a final state.
Scanned by CamScanner
=2 2X21S2!I!PL§2i- (MU-Sem. 5-Comp.) 2-41 --------------- '
Finite Auto
m
2.2.6 Equivalence of DFAs
Two finite automata M t and M2 are said to be equivalent if they accept the samc
language
i.e., L (Mj) = L (M 2 )
Two finite automata M t and M 2 are not equivalent over if there exists a co such that :
co G L (M0 and co £ L (M 2)
or
co (2 L (M0 and co e L (M
That is, one DFA reaches a final state on application of co and other reaches a non final
state.
Thus, if two DFAs are equivalent, then for every co G E* :
Equivalence of two DFA’s can be established by constructing a combined DFA for two
DFAs Mj and M 2 . Let the combined DFA for Mi and M 2 be M 3 .
where, Mj = (S, S, 8 s0 , FJ
and M 2 = (Q, S, S 2, q0 , F ) The combined machine M 3 can be described in terms
of M! and M 2 . • x
M 3 = (S x Q, x, 8 3 , < s0 , qo >, F 3 ) i
A state of M 3 is of the form < s it q ( >
Where,
Si G S of Mj and qj G Q of M 2 .
The transition function 8 3 is defined as :
83 (< Si, Qi >, a) = < 8] (Sj, a), S 2 (q it a) >
8 3 is a function from S x Q to S x Q.
The algorithm for generation of transition behaviour 8 3 of M 3 is given below :
1. Add the state < s h q0 > to M 3.
2. for every state < s-, q, > in M 3 .
{
if(< Sj, qj > has not been expanded)
{
for each alphabet a} g £
{
add a transition < 8 t (Sj, Hj), 8 2 (q p a to M 3
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-42 Finite Automata
Example 2.2.32 : Show whether the automata M j and M 2 in given F i g . Ex. 2 . 2 . 3 2 (a) a n d (b)
are equivalent o r not.
lc
‘ Id c
C lc
c
(a) M : (b) M 2
Fig. Ex. 2.2.32
Solution :
Construction of combined DFA M3
Step I s We start with the combined state < q p q 4 >, where q, is start state of M 1 and q4 is the
start state of M 2 . < q P q 4 > *s expanded and necessary transitions added.
8 3 (< q r q 4 >, c) => < 8 i (q P c). 8 2 (q 4> c) >
=><q P q4>
5
S 3 (< q P q 4 >- 8) => < i (q P d), 8 2 (q 4 , d) >
=> < q 2 > q 6 >
Fig. Ex. 2.2.32(c)
Scanned by CamScanner
Theory of Comp. Scl. (MU-Sem. 5-CompQ 2-43 Finite Antony
Step 2 :
d
4dyd c
Every state in M 3 has been expanded. The construction of combine DFA is over. During
the construction of M 3 , four states have been generated.
< q p q4 > : both qj and q4 are final states. • •
< 2’ 06 > :
both are non final states. ...
ky -----x -----(QT)y
(M;> (M,)
Fig. Ex. 2.2.33
Scanned by CamScanner
Scanned by CamScanner
5-Comp.) 2-44 , Finite Automata
Solution : 1
' I■ ■ii ■ ■
Two DFAs are equivalent if for any string (O e Z*, either the machines will be in a final
state or both the machines will be in a non-final state.
This can be established by constructing a combined DFA for Mi and M 2 .
Construction of combined DFA
In the Fig. Ex. 2.2.33(a), following subsets are generated.
1. {A, D} - Both A and D are accepting states.
2. (D, E ) - Both D and E are non-accepting states. .
3. {C, F} - Both C and F are non-accepting states.
4. {B, G} - Both B and G are non-accepting states.
Hence, machines Mj and M 2 are equivalent.
X '
{B.E} {C.F}
and M 2 = (Q, S, 6 2 , q 0 , F 2 )
be two arbitrary DFAs. To prove the various closure properties, we must show that there
is another machine : ,
M 3 such that L (M 3 ) = L (Mj) u L (M 2 )
M 4 such that L (M 4) = L (M n L (M 2)
M 5 such that L (M s ) = L (M - L (M2)
Scanned by CamScanner
Flnlt0 Aul0
- 12*?
and qj g Q of M 2 .
The transition function 8 C is defined as :
. § c (< s b q ( >, a) = < 8 j (sj, a), 8 2 (q., a) >
8 c is a function from S x Q to S x Q.
The algorithm for generation of transition behaviour 8 C of Mc is given below :
1. Add the state < s 0 , q 0 > to M c .
2. for every state < Si, q. > in M c .
{
if(< s,, q t > has not been expanded)
{
for each alphabet a1 G E
}
1
1
Deriving M 3 from M c (L (M3 ) = L (MJ u L (MJ)
M 3 can be derived from M c by selecting a set of states from M c as a set of final states
for M 3 .
Set of final states F 3 for M 3 = {< s it q t > e states of M c I s 4 e Fj v q ( 6 F 2 )
where Fj is set of final states for Mj and F 2 is set of final states for M 2 .
Deriving M 4 form M c (L (MJ = L (Mj) n L (MJ)
M 4 can be derived from M c by selecting a set of states M c as a set of final states for M4.
Set of final states F 4 for M 4 = {< s h q} > g states of M c I s, g Fj a q t e F 2 )
where, Fj is set of final states for M j and F 2 , is set of final states for M 2.
Scanned by CamScanner
5-Comp.) 2-46 . Finite Automata
M1 = M2 =
(a) DFA for strings ending in 01 (b) DFA for strings having
even number of 1’s.
. Fig. Ex. 2.2.34
Construction of combined DFA M c
Step 1 : We start with the state < s 0 , q0 >, where s0 is the initial state of Mj and q0 is the
initial state of M 2 . . ■ . ,;
< s0 » qo > is expanded and necessary transitions added. 2 ----->
8 (< s 0 , q 0 >, 0) => < 8 (s 0 , 0), 8 (q0 , 0) >
* 1
=> < si. q 0 > I
8 ( < s 0 , q<l >, 1) = > < 8 ( s 0 , 1), 8 ( q 0 , 1) >
=> < s 0 , q r > Fig. Ex. 2334(c)
Step 2 : State < s „ qo > and < s 0 , q, > are selected for expansion.
8 (< s 0 , q , >, 0) => < 8 (So, 0), 8 (q,, 0) >
=><s„q 1 >
8 (< Sj, q, >, 1) => < § ( s o. 1). 8 (q P 1) >
o
=> < s 0 . q 0 > o
Scanned by CamScanner
eor
Finite Aiitorr ,
■2_Ib y of Comp. Scl. (Mll-Sem. 5-Comp.) 2-47
Step 3 : State < qj > and < s 2 , qj > arc selected for expansion.
o
o
o
o
0
0
o Derived
by selecting
final states
In DFA of
Figure 2.2.33(f)
Fig. Ex. 2.2.34(g) : DFA for L (Mx) L (M2 )
Scanned by CamScanner
V Theory of Comp: Sci. (M U-Sem. 5-Comp.) 2-48 ' ' / Finite Automata
o
0 0
0
0
0
0
0
0
2.2.7.2 Complementation
It may happen that a DFA contains redundant states, i.e. states whose functions can be
handled by other states. Minimization of a DFA is a process of constructing an equivalent
DFA with minimum number of states,
Scanned by CamScanner
F i n i l e Autoni
_V Theory of Comp, Scl, (MU-Sem. 5-Comp.) 2-49 ata
• It may be possible (hat for a given DFA some of the states might be equivalent or they
arc not distinguishable.
• Equivalent states from an equivalent class or group. Every state from an equivalent class
exhibit the same transition behaviour.
• States of a DFA can be divided into a group of equivalent classes.
Equivalent states
Two slates qj and q.e Q arc said to be equivalent if they are not distinguishable
(1) If q. is an accepting slate and qj is non-accepting, then qj and qj are distinguishable.
(2) If q. is non-accepting and qj is accepting, then q. and qj are distinguishable.
(3) If there is string such that.
S*(Qi» x) e F and 8*(qj, x) £ F
or
8*(qj, x) g F and 8*(qp x) G F
then qj and q are distinguishable. .
K- equivalence : qj and q. are K-equivalent states if and only if no string of length < k
can distinguish them. If there exists a string of length k, which can distinguish q i and q j? the
states and qj are said to be K-distinguishable.
Algorithm for minimization divides states of a DFA into blocks such that :
1. States in a block are equivalent.
2. No two states taken from two different blocks are equivalent.
I
The first step is to partition the states
o
of M into blocks such that all states in ? 2)r —
a block are O-equivalent. This is 1
accomplished by placing :
1, Final states into one block.
2. Non-final states into another block.
States of the DFA in Fig. 2.2.8 can be divided into two blocks,
block 1 = set of non-final states
= C
ll’ 3* 5’ 6’ 9?)
J
Scanned by CamScanner
W Theory of Comp. Sei, (Mu-Sem. 5-Comp.) 2 . 50 Finite Automata
Input ‘O’ :
8 (q 0 , 0) qj {block 1, q x is in block 1 }
8 (q r 0) => q 6 {block 1, q6 is in block 1 }
8 (q 3 , 0) => q 2 {block 2, q 2 is in block 2}
8 (q 4, 0) => q 7 {block 1, q7 is in block 1 }
5 (q 5 , 0) => q2 {block 2, q 2 is in block 2}
8 (q 6 , 0) => q 6 {block 1, q 6 is in block 1 }
8 (q 7 , 0) => q 6 {block 1, q 6 is in block 1 }
On input 0, block 1, is successor of q0 , q p q q6 , q7 -
On input 0, block 2, is successor of q 3 , q5.
Input ‘ 1 ’ :
8 (q 0 , 1) q 5 {block 1 )
8 (q p 1) => q 2 {block 2 )
8 ( q 3, l ) = > q 6 { b l o c k l ) ’ '
8 (q 4 , 1 ) => q 5 {Mock 1}
8 (q3 , l ) = > q 6 {block 1} , ‘ ' ■
8 ( q 6 , l ) = > q 4 {block 1}
S ( q 7 , 1) => q 2 {block 2}
On input 1 , block 1 is successor of q0 , q3 , q 4, q5 , q 6-
On input 1, block 2 is successor or of q ( . q 7.
q 3, q 5 are distinguishable from, q0 , q P q4, q 6. q7 on input 0.
Scanned by CamScanner
Finite Aut
▼jTheoryofComp, Sci, (MU-Sem, 5-Comp.) 2-51 — - - orn
• The next step is to obtain the partition P 2 whose blocks consists of the sets of states
which are 2-equivalent, i.e. equivalent under any input sequence of length 2.
Checking (q 3 » q 5 ) for equivalence - block 1 1
8 (q 3 » 0) = q 2
5 y mapped to same block (q 2 )
(q5»°)= q 2
8 (qs» 1 ) = q 6 1 ’■ \
> mapped to same block (q 0 , q q 6 )
5 (05> 0 = q 6 J
q3 and q 5 are 2-equivalent. ;
8 ( q P o) = q 6 *1
ma
8 (q 7 > 0) = q 6 J PPed t0 same block
too* 04’ Ofi) '
8 ( q P l ) = q2 "1 I
> mapped to same block (q 2 ) ,
8 (q 7 » 1) = q 2 J
;1 ;
qj and q 7 are 2-equivalent. '
Checking (q 0 , q 4, q 6 ) for equivalence - block 13
8 (q 0) = q! [block 12] ,
8 (q4, 0) = q 7 [block 12]
8 (q 6 , 0) = q 6 [block 13]
8 (qo» 1) = q 5 [block 11]
5 (% 1) = q 5 [block 11]
8 ( q 6 , 1 ) = q4 [block 13]
q 6 is distinguishable from q 0 , q 4 on input 0/1 .
2-equivalence partitioning of Q r i
p2 = to3. <0r too- to 6 ) to 2 )
• The next step is to obtain the partition P 3 whose blocks consists of the sets of states
which are 3-equivalent, i.e. equivalent under any input sequence of length 3.
It can be seen easily that p 2 can not be portioned further.
p3 = P 2 to 3 * q 3 )» top q 7 ) too» q4) toe) to 2)
• If for some k, P k = P k + h the process terminates and P k defines the blocks of equivalent
states of the machine. . ,,
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-52 Finite Automata
The transition diagram for minimum state DFA for DFA of Fig. 2.2.8 is given in
Fig. 2.2.9. The process of drawing of minimum-state DFA is as below :
1. Start state is <q0 , q 4 >, since q0 was start state in Fig. 2.2.8.
2. Only accepting state is <q 2 >, since q 2 is the only accepting state of Fig. 2.2.8.
3. Since, there is a transition from q0 to q, on input 0 in Fig. 2.2.8, we add a
transition from <q 0 , q 4> to <q,, q 7> on input 0 in Fig. 2.2.9.
4. Since, there is a transition from q0 to q5 on input 1 in Fig. 2.2.8, we add a
transition from <q0 , q 4> to <q3 , q 5> on input 1 in Fig. 2.2.9.
5. Other transitions can be added in a similar fashion.
o
o,
0KX9
0 1 . ..
q0 q. q« ; .. .
q. 9o q2 ■' . -
q2 A3
★
q3 q3 q0
q< q3 q5
q6 q8 a4
q
q6 6 q«
q
Az e q3
Solution :
Step 1 : Finding 0-equivalence partitioning of states by putting final and non-fmal states
into independent block. (
Pq ~ Qp 5* 6’ 9?) 3)
block 1 block 2
Step 2 : Finding 1-equivalence partitioning of states by considering transition on ‘O’ and
transition on *1*.
Scanned by CamScanner
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-53 %
I- - - - - -*
Ao. A i ( A2 ,A4 , A5 ,Aa ,q 7
(A3) Transition o n 0
block 1 block 2
block 1 block 2
Transition onO
( Ao A1" A5 A© ) ( A2 A4) ( A7 ) (A3)
Transition on 1
p
2 = (q 0 » A6> (Qp Aj) (A2> A4> (a?) (%)
Step 4 : Finding 3-equivalence partitioning of states by considering transition on 0 and 1.
Transition onO
Scanned by CamScanner
Theory o£Cornp Sci. (MU-Sem. 5-Comp.) 2-54 Finite Automata
(q 2 . qJ (q 3 ) (q P q 5)
(q 3 ) * (q 3 ) (q 0 > q 6 )
(q 7 ) (q«> q 6 ) (q 3 )
(a) State transition diagram for minimum- (b) State transition diagram for minimum-
state DFA state DFA
Fig. Ex. 2.2.35
q3 2 q5
★
q2 . Ma
q3 q7
q. q5 q8
★
q5 q6 ■q,
q6 q7 q
i
q7 qe q2
*
q8 q0 q.
Solution :
Step 1 : Finding O-equivalence partitioning of states by putting final and non-final states
into independent blocks.
=
(Qq* M r 6* M7) (Qj* Mj» Mg)
block 1 block 2
Scanned
Scanned by CamScanner
by CamScanner
Theory of Comp. Scl. (MU-Som. 5-Comp.) 2-55
Transition onO
1( q 2 > As » Aa )
U
t t
?
On input 0, block 1 is successor of q0 , q3, q6.
On input 0, block 2 is successor of q p q q7. block 1
Block 2 is 1-equivalent
Pi = (Qo> <13- 96> (Qi> AL- q 7 ) (<i2> q
block 11 block 12 block 2
Step 3 : Finding 2-equivalence partitioning of states by considering transition on ‘0’ and
transition on T .
*— Transition onO
Scanned by CamScanner
2-56 Finite Automata
= =
?2 Pl (Qo» Av q 6 ) (Ap q 7) (q 2 » q$» which is final set of blocks of equivalent
classes.
Step 4 : Construction of minimum state DFA. ,
Non-deterministic finite automata can reside in multiple states at the same time. The
concept of non-deterministic finite automata is being explained with the help of transition
diagram (Fig. 2.3.1)
If the automata is in the state q0 and the next input symbol is 1, then the next state will be
either: ;
2 V
(1) qo " ( ) <lr ‘ x t
Thus, the move from q0 on input 1 cannot be determined uniquely. Such machines are
called Non-deterministic automata.
•' Non-deterministic finite automata can be in several states at any time. Thus, NFA can
guess about an input sequence.
• Every NFA can be converted into an equivalent DFA.
• An NFA can be designed with fewer states compare to its deterministic counterpart.
• • Due to non-determinism, NFA takes more time to recognize a string.
2.3.1 Definition of NFA x *
MU - Dec. 2006
Scanned by CamScanner
— Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-57 - Finite Autorr
'-X •1
Ao Ao q q
Ao Ao
°F/ ° Ao
{q o l
t°
{q o l
b
{ho.qi)
0 t 1
(Ao.qi.'M
0
I
KM
1
1
+ °
<t> I s a failure state {q 0 .q2>}
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-58 Finite Automata
{qj {qU
(b) State transition table
Fig. Ex. 2.3.1
Scanned by CamScanner
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 2-59 Finite Autorna
For reaching the final state q 4, from the start state q0 , a substring 0101 is required.
• Any string, containing a substring 0101 will be accepted by the above NFA. The
machine can remain in q0 , for the portion of string before 0101. The machine can ren
in q 4 for the portion of string after 0101.
Example 2.3,2 : Construct a NFA that accepts any positive number of occurrences of
various strings from the following language L given by,
L = {x g {a,b} I x ends with aab}
Solution :
a _____b
{q0’<li) %
Qi <12
q2 q3
<13
The language accepted by the NFA shown in Fig. Ex. 2.3.2 consists of string ending in
aab. A string of length n, ending in aab can be accepted by the NFA if :
1. The machine remains in q 0 for first n-3 inputs.
2. On last three inputs aab, the machine makes the transition as :
• To reach the final state, and to remain there, it is necessary that the last two characters of
the string should be 10.
• Thus the NFA accepts a string, if and only if it ends in 10.
Example 2.3.3 : Draw a non-deterministic finite automata to accept strings over alphabet
{0, 1}, such that the third symbol from the right end is 0.
Solution :
1
->q 0 {q0’ q i } {%}
Jo,
q> {q2 l {q2 l
(a) State transition diagram q2 {q3 ) (q3 l
*
q3 0
(b) State transition table
Fig. Ex. 233 : NFA for Example 233
• For the first n-3 symbols, the machine can remain in the start state q 0 .
• On seeing the third symbol from the right end as 0 (NFA can guess), it makes a mo' e
towards the final state q 3 .
Scanned by CamScanner
2-60 Finite Automata
Example 2.3.4 : Design a NFA to recognize the following sets of strings : abc, abd, aacd.
Assume that alphabet is {a, b, c, d}. Give the transition table and transition
diagram data.
Solution :
Machine can guess and take one of the
three paths as shown :
1. Path q 0 —> Qi q 2 —> q 3 for string
a b d
abc.
2. Path q 0 —> q 4 —» q 5 —> q 6 for string
a
abd. a d
3.
string aacd. Fig. Ex. 2.3.4 : State diagram of NFA
Table Ex. 2.3.4 : State table for NFA
b d
M
a
WA—>qn ' (q P q4> q7 l 0 * 0
q. 0 q2 0 0
0 0 q3 ' 0
0 0 0
0 q5 0 0
q$ 0 0 0 q6
0 0. .. .0’ 0
q q8 0 0 0
q. 0 0 q9 0
0 0. 0 Qio
0.' ■ 0 ■- <k-' ; <i>
Example 2.3.5 :
L = {x g (a, b, c}* : x contains exactly one b immediately following c} •
L = {x g {0, 1}* : x is starting with 1 and I x I is divisible by 3}
L = {x g {a, b}* : x contains any number of a’s followed by at least
one b}
a.b a,b,c
Solution : a,c
(i) L { x 6 {a, b , c ) * : x contains exactly one b 50— ♦— < 0
immediately following c )
r /b
Since, the problem is full deterministic in
nature; it will serve no purpose to design an NFA.
We can design a DFA and since every DFA is also
an NFA, a DFA can stand for both DFA and NFA. Fig. Ex. 2.3.5(a) : A DFA standing
for both DFA and NFA
Scanned by CamScanner
Finite Autor
<2/0,1
0
5= 0,1 >r-X ’1
wn —
Fig. Ex. 2.3.5(b) : A DFA standing for both DFA and NFA
(iii) L ( x e {a, b}* : x contains any number of a’s followed by at least one b}
a,b
Fig. Ex. 2.33(c) : A DFA standing for both DFA and NFA
Example 2.3.6 : Design a NFA for a binary number where the first and the last digits are
same
Solution :
0,1
i
o
Q q2
<12 {q2.q4}
q3 {q qj
0,1 5
Scanned by CamScanner
Theory ofComp. Sci. (MU-Sem, 5-Comp.) 2-62 Finite Automata
Example 2.3.7 : An NFA with states 1-5 and input alphabet {a, b} has following transition
table.
(a) Draw a transition diagram (b) Calculate 8*(1, ab)
(c) Calculate 8*(1, abaab) _________
q 6 (q, a) 8 (q, b)
1 {1.2} {1}
2 {3} {3}
3 {4} {4}
4 {5} <t>
5 {5}
Solution :
(a) Transition diagram is given in Fig. Ex. 2.3.7.
ab b
’
►— C4
a
= { 1 , 3 , 4, 5 }
Example 2.3.8 : Construct a FA for accepting L over {0,1} such that each 0 is immediately
preceded and immediately followed by 1 .
Solution :
0 1
. *
<!♦ qi
o o 02 Qi
q?
o <12 0| qi
0,1 q* q* 0|
Fig. Ex. 2.3.8
Scanned by CamScanner
Theory of Comp. Sci.(MU.Sem.5-Comp.) 2-63 ‘ Finite Autor
Example 2.3.9 : Construct a NFA that accept the set of strings in (0 + 1)* such that some
two 0’s are separated by string whose length is 4i , for some i >=0.
Solution :
(lo,i
o
o,1
0 1
(Oo’ *11}
{q 2 }
<h {q3 } {q3 l
<13 {qU > (qj
<14 {qj ■ (qj
*
<15 {q5 ) {q,}
(b) State transition table
Fig. Ex. 2.3.9 : NFA for Example 2.3.9
• The loop <h —> q 2 —* q 3 q 4 —> qi can absorb a string of length 4i.
• On receiving theO of the portion of the string containing ‘two 0*s separated by string of
length 4i, the machine makes a move to q, from q 0 . Then, it loops i times in
q , -> q 2 -» q 3 -» q 4 -> q r Then it gets a 0 and makes a move from q ( to q,.
Example 2.3.10 : Design NFA for the set of strings on the alphabet {0, 1} that start with oF
and end with 10.
Scanned by CamScanner
y . Theo _of_Cornp. Sci. (MU-Sem. 5-Comp.) 2-64 Finite Automata
Solution :
q
o. 4>
q
3
0,1
• A string 010 (starting with 01 and ending with 10) can be accepted through the path
<lo Qi q 3 04’
• For any other string, the portion between starting 01 and ending 10 can be absorbed by
the state q 2 .
4- (qo.q P q 2 ) .......; - -
These subsets form a power set on Q, which is given by 2 .
The procedure for finding whether a given string is accepted by the NFA, involves :
1. Tracing the various paths followed by the machine for the given string.
2. Finding the set of states reached from the starting state by applying the symbols of the
' 'string. .........4 ' ............ • 1 ‘ >
. f -f I ■ • •* . .
3. If the set of states obtained in step (2), contains a final state, the given string is accepted
by the NFA. '
The above procedure can be applied to any string by constructing a successor table.
o
8= 0i 8=
(Qo ’*11J
Scanned by CamScanner
V Theory of Comp. Scl. (Mll-Som. 5-Comp,) 2-G5 |r|nltQ Autor
M
Algorithm for construction of successor table for the NFA of Fig. 2.3.5 is being
explained, stepwise :
Step 1 : Starting state {q 0 } is the first subset.
0 - successor of q 0 is q 2, i.c. 8 (q 0 , 0) => q r
1 - successor of q 0 is i|), i.c. 8 (q l? 1) => (|>.
Fig. 2.3.6
0 1
o
{q 2 } 0
Fig. 2.3.7
J
Step 3 : A new subset {q0 , q, } is generated. Successors of the subset {q 0 , q ( ) are generated.
0 - successor of {q 0, q, ) = 8 ({q 0 , q t }, 0)
= 8 (q0 , 0) o 8 (q p 0)
= (q 2 ) o<l>= { q 2 )
1 -successor of {q 0, q t ) = 8 ({q 0 , q j , 1)
= 8 (q0 , 1) u 8 (q t , 1)
= 0 u ( q0 « q2 )
.= (q»> q 2 )
J
Scanned by CamScanner
W Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2 . 66
Finite Automata
0 1
0
(q 0 > a>1 {q 2 } { 0’ 2
Fig. 2.3.8
Step 4 : A new subset {q q 2 ) is generated successors of the subset {q0 , q 2 } are generated.
0 - successor of {q0 , q j = 8 ( { q0 , q 2 h ° )
= 6 (q0 , 0) U 8 (q 2 , 0)
= {q 2 } u {q 2’ 91} = {‘Jo’*51’
1 - successor of {q0 , q 2 } = 8 ( { q0 , q 2 }, 1)
= 5(0. I ) u 6 (q 2, 1) = <)» U {qo) = { Qo )
0..0
o
0,1
-Hqo) {q2 } <!>
{q2 J )q,. Ai J {%}
o
{q qj {q2 } (qo . q 2 )
Fig. 2.3.9
Scanned by CamScanner
CamScanner
FlnitQ Auto
jy__Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-67 ___ _ _ _ _:--------
oo o
0,1 “*No) {q 2 l
NJ No* mJ ( qo i
0 {q0* q j , (q 2 l I q0* q 2)
{q0 * q 2 ) N o )
( q0. q P qj No* q o l
Fig. 23.10
Since, a new subset is not generated the process of subset generation stops.
Step 6 : q2 is the final state in NFA of Fig. 2.3.5. Every subset containing q 2 should be takes
as a final state.
0 0
MU - Dec. 20lj!
Scanned by CamScanner
W TheoT fCgmp. Sei, (MU-Sem. 5-Comp.) 2-68 ______ Finite Automata
Proof: ’ i 1
o r 8 2 ({p., p 2 , . . . p k ) . a ) = U 5 , ( Pi , a) .,.(ii)
i=l ■■■■ - ..
also, 8 2 (q 0 ,con ) = {p p p 2, . . . pk ) ,..(iii)
from (i), (ii) and (iii) we get,
§
+ = 2(s 2 (q0 > “n). a)
.• = ({ppPj, . . . p k ) , a )
k
= U 6 1(p 1, a ) = 51(q 0 , s n + 1 )
i=l
Scanned by CamScanner
V Theory of Comp. Scl. (MU-Sem, 5-Comp.) 2-69 -----------:— —--------
Solution :
Table Ex. 2.3.11 : State transition table of DFA
0 1 . . J. ■ n? • t • ■ ' r
•
1 1
o
) -------*- ------ftp,q})------
1
o
o
v
0
o
7°
1 /
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-70 Finite Automata
0 1
->{p} {q. s} {q}
q* W {q. r}
r (s) {p}.
s* 4> {p}
Solution :
Step 1 : {p } is taken as the first subset.
0-Successor of { p } = 8 ({ p ), 0) = {q, s}
1-Successor of { p} = 8 ({ p ), 1) = { q }
0 1
Step 2 : Two new subsets {q, s) and {q) have been generated. Successors of (q, s) and {q}
are calculated.
8 ( { q , s } , 0 ) = 8 (q, 0) u 8 (s, 0) = {r) u (|) = {r}
8 ({q, s}, 1) = 8 (q, 1) u 8 (s, 1) = {q, r} u {p} = { p, q, r)
5 ({q}, 0) = {r}
5 ({q}, 1) = {q, r )
0 1
Scanned by CamScanner
Flnrte
y Theory of Comp, Sci, (MU-Sem, 5-Comp.) 2-71 —2 -— Autor
= ( q , s ) u {r) u {s}
= {q, r, s}
5 ({p, q, r), 1) = 8(p, 1) u 8 (q, 1) u 8 (r, 1)
= (q) {q> r} u (p)
= {p, q, r}
8 (( q, r), 0) = 8(q, 0 ) u 8 (r, 0)
-={r}u{s}
' = (r,s)
8 ({q, r ) , 1) = 8(q, 1) w 8 (r, 1)
= (q. r ) u {p}
= {p, q> r}
o ’ 1
->{p} {q>s} {q)
{q>s} {r}‘ {p, q. r )
(q) {r} {q,r}
{r} (s) {p}
{q. r) [r, s} {p, q. r )
\ r f s■■■
, | (p. q, r) {q> r, s )
{p. q. !■}
Step 4 : Three new subsets { s}, {r, s ) and {q, r, s} are generated. Their successors are
calculated.
8({s},0) = 0
1
8 ( { s ) , 1) = p
8 ( ( r , s},0) = {s)
8 ( { r , s ) , 1) = (p)
8({q, r,s),0) = (r,s)
8 ( ( q , r, s ) , 1) = (p, q, r)
0 1
~*{p) (q- s ) (q)
(q.s) (r). (p. q . r )
(q) (r.) (q.r)
(r) (s) {p}
Scanned by CamScanner
r Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-72 ____________________ Finite Automata
0 1
-»{p} {q.s} {ql
{q.s}* {r} (P.q.r)
{q}* {r} {q, r)
{r) (s}‘ {p}
■ {q.r}* {r, s) {p.q.r)
{p, q.r)* (q, r, s) {p.q.r}
{s}* 0 . {P)
... . {r.s}* {s} ■ Ip)
{q.r.s}* {r, s) {p.q.r}
11
0
0
0 0
0
Scanned by CamScanner
Finite Autorr
Theory of Comp. Scl. (MU-Som. 5-Comp.) 2-73
Example 2.3.13 : Convert Wallowing NFA to a DFA and Informally beth anoC
■ Dec. 20
....accep s. -............
0 1
(p. q) (p)
r
s
t
Solution :
Step 1 :
0-Successor of {p) = S ({p }, 0) = {p, q}
1-Successor of [ p ] = 5({p}, l ) = { p )
The new subsets {p, q ) is generated. Successors of {p, q} are calculated.
5({p, q } , 0 ) = 8 (p, 0) U 8 (q, 0)
= {p, q ) u (r, s}
8 ( { p , q ) , 1) = 8 ( p , l ) u 8 ( q , l)={p}u{t}
= {p, t}
Step 3 : Two new subsets (p, q, r, s) and {p, t) are generated. Their successors are
calculated.
= 8(p, 0) u 6 (q, 0) u 8 (r, 0) u 6 (s, 0)
Scanned by CamScanner
▼ Theory ofComp. Sci. (MU-Sem. 5-Comp.) 2-74 Finite Automata
Jp.q),
Example 2.3.14 : Construct a NFA that accepts a set of all strings over {a, b} ending in aba.
Use this NFA to construct DFA accepting the same set of strings.
P U - May 2014
Solution :
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 2-75 - Finite Autop
{ q 0 ’ qJ { 3o’ 3i } q2|
{ 3o’ 3 2 J { q0’ q p q 3 ) (q0 )
(b) State diagram of the DFA (c) State table of the DFA
Fig. Ex. 2.3.14
Example 2.3.15 : Construct a NFA a n d its equivalent DFA for accepting a language define:
over S = {0,1} such that each string h a s two consecutive zeroes followed
by 1 .
P,1
♦—®“-* —(s£
Fig. Ex. 2.3.15
Scanned by CamScanner
▼ Theory of Comp, Scl. (MU-Sem. 5-Comp.) 2-76 Finite Automata
0 1
- H Qo) as —>A \ B A 1 o
J- 0 0
{q<>.qi} as B C A }— » -( 0 ) »■
{qo’Qi’q:) as C C D
(q0 - q?) as D * D ; D
Example 2.3.16 : Construct an NFA an then equivalent DFA accepting strings over {0, 1},
whose every block of 4 consecutive symbols, contain at least 3 zeros.
Solution:
This problem is fully deterministic in nature. Technically, a DFA with 0 transitions not
mentioned can be called a NFA.
NFA is given in Fig. Ex. 2.3.16(a).
0 0
L, 1
0
V 0
0
0
From Fig. Ex. 2.3.16(a), it is clear that between any two 1’s there will be at least three
0’s. Thus every block of 4 consecutive symbols will contain at least three 0’s. Every state is a
final state as the failure cases have not been shown.
Above NFA can be converted into a DFA by introducing the dead state. DFA is given in
Fig. Ex. 2.3.16(b).
Example 2.3.17 : Construct a NFA and then equivalent DFA accepting strings over {0, 1),
which accepts the set of all strings of zeros (i.e, 0’s) and ones (i.e. 1’s) with
at most one pair of consecutive zeros and at most one pair of consecutive
ones (i.e. 1’s).
Solution :
This problem is fully deterministic in nature. Technically, a DFA with 0 transitions not
mentioned can be called as NFA.
Scanned by CamScanner
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 2-77 _Finite_Autorp a
0
o
o 0
0
0
0 0
1
r
/o 0
0
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-78 Finite Automata
Example 2.3.18 : Construct an NFA and then equivalent DFA accepting string over {0, 1}, for
accepting all possible strings of zero and ones not containing 101 as
substring.
Solution : This problem is fully deterministic in
nature as technically, a DFA with (f> transitions not
mentioned can be called as NFA.
o
Fig. Ex. 2.3.18(b) : A DFA
Example 2.3.19 : Construct a NFA and then equivalent DFA accepting strings over {0, 1}, for
accepting all possible strings of zeros and ones which do not contain 011
as substring.
Solution : p
This problem is fully deterministic in nature.
Technically, a DFA with (|> transitions not
0
mentioned can be called as NFA.
• State qj is for previous character as 0. Fig. Ex. 23.19(a) : A NFA
• State q 2, is for previous two characters as 01.
• An input of 1 in state q 2 will cause the machine to enter a failure state.
• ' NFA can be converted into a DFA by introducing an explicit failure state.
o
o
o
Fig. Ex. 2.3.19(b) ; An equivalent DFA
Scanned by CamScanner
Finite Aut
V Theory of Comp. Scl, (MU-Sem, 5-Comp.) 2-79 . _ .omata
Example 2.3.20 : Construct a DFA equivalent to the NDFA shown In Fig. Ex. 2.3.20 :
o
o
As
s ({q0 }, i ) = (q 0 » q j
Step 2 : > 3 ({q 3 }, 0) = { q 4 }
5 ( { q3 } , l ) = (|)
8({qo , q i h O ) = {q 3 }
SGqo’Qih i ) = {q0 »qi»q2 }
Step 3 : 5 ( { q j , i ) = ( q4}
5 ( ( q j , 1) = {q 4 )
— =
$ ((q0 > q P q 2 b o) (q3> q 2 » qd {qi* q.3» q .)
=
$ ({q0 » q P q 2 h i ) I qo’ q P q 2 )
Step 4 : d q 2’ q3’ q 4 }» d) = {q 2, q4 }
=
({q2 » q 3 > q i) {q2, q 4 }
Step 5 : b ({q 2, q 4 }, 0) = {q 2, q4 }
3 ({q2, q4 ), 1) = {q 2, q4 )
DFA is given in Fig. Ex. 2.3.20(a).
o
o
0
0.1
Fig. Ex. 2.3.20(a) : State diagram of the DFA for Example 2.3.20
Scanned by CamScanner
V Theory of Comp, Sei, (MU-Sem. 5-Comp.) 2-80 Finite Automata
Solution :
4
w* * ' • »
Transition diagram i s given in Fig. Ex. 2.3.21(a).
0 o
<>0
1 \*0?1 '1 1
Subset construction :
Step 1 : 8 ( { q j , 0) = ( q 2, q 3 }
8((q,},l) = {q,}
Step 2 : 8 ( { q 2, q 3 } , 0 ) = { q p q 2 }
8 ( { q 2 , q 3 } . 1) = { q , . q 2 }
Step 3 : 8({q p q 2 } , 0 ) = { q p q 2, q 3 }
8 ( { q P q 2 h 1) = { q P q 2 }
Step 4 : 8 ( { q p q 2 , q 3 ) . 0) = ( q p q 2> q 3 }
8((q P q 2. q 3 ) . D = {q,.q2)
1
Fig. Ex. 2.3.21(b) : Equivalent DFA
Scanned by CamScanner
Scanned by CamScanner
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 2-81 Finite Auton
Subset construction :
Step 1 : 8 ( { q1 } , 0 ) = { q2 , q 3 }
SaqJ,!) = {qt }
Step 2 : 8 ( { q2, q 3 } , 0 ) = { q t , qj
8(fa 2, q 3 }» ~ {qp q2 }
Step 3 : 8(fajy qjJrQ) “ faj }
8 ( { qp q 2 } , 1) = {qj
5
( { q P q 2, q 3 } ’ ° ) - fapMd
=
(fap q 2 > q 3 )> fap q 2 }
DFA is given in Fig. Ex. 2.3.22.
Example 2.3.23 : Let M = ([q 0 , qj, {0, 1}, 5, q 0 , {qj) be an NFA with :
=
(qo> {q-i}
=
5(q0 . 1) {qo» qi }
=
8(qi, 1) {qo» q j
Find the corresponding DFA.
Solution :
Subset construction :
Step 1 : 5
( ( % ) . 0) = ( q j
5
((%!> 0 = ( q0 > q j
Scanned by CamScanner
Finite Automata
Example 2.3.24 : Convert NFA shown in Fig. Ex. 2.3.24 to its corresponding DFA.
0
Step 3 : Two new subsets { q 0 , q j and
{ qJ are generated. Their
successors are calculated.
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-83 ■ Finite Auton)g[a
Step 4 : Two new subsets { q0 , q 2, qj and ( q0 , q P q5 ) are generated. Their successors arc
calculated.
{qo-'U
Scanned by CamScanner
V Theory Of Comp, Sei, (MU-Sem. 5-Comp.) 2-84 Finite Automata
Example 2.3.25 : Convert NFA shown in Fig. Ex. 2.3.25 to its corresponding DFA.
a
b
b a
a
a
Solution :
Step 1 : Starting state { 1 } is taken as the first subset. Its successors are calculated.
5 ({!}, a) = {2,5}
5({l},b) = 0
Step 2 : A new subset {2, 5} is generated. Its successors are calculated.
8 ({2, 5}, a) = 8 (2, a) u 8 (5, a)
= (|) kj (j) = <j)
{W
{2,5}
Example 2.3.26 : Convert the NFA shown in Fig. Ex. 2.3.26 to DFA.
b
—J a ) -----------/eT) — —»(M
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-85
Solution :
In some cases where number of states is large, it is convenient to derive subsets using
state transition table. State transition table for the given NFA is shown in Table Ex. 2.3.26(a)
Tabic Ex. 2.3.26 : State transition table for NFA of Example 2.3.26
a b
->A (B,D)
B 0 (C,E)
C* (E) (C)
D 4> (A)
E (C,D)
Step 1 : Starting state {A) is taken as the first subset. Its successors are calculated.
8 ({A}, a) = {B, D)
S({A},b) =
Step 2 : A new subset (B, D ) is generated. Its successors are calculated
8 ( ( B , D ) , a ) = 8 (B, a) u 5 (D, a)
= <j> O ([> = <[>
8({B, D ) , b ) = 8 (B, b) u 6 (D, b)
= {C, E ) u (A) = {A, C, E)
Step 3 : A new subset {A, C, E) is generated. Its successors are calculated.
8 ({A, C, E},a) = 8 (A, a) o 8 (C, a) u 8 (E, a)
= {B,D) u {E) u ( Cr D )
= (B,C, D, E)
8 ( ( A , C, E ) , b ) = 8 (A, b ) u 8 ( C , b ) u 8 ( E , b)
= <|>u (C) u<|> = {C)
Step 4 : Two new subsets (B, C, D, E) and (C) are generated. Their successors are
calculated.
8 ({B, C, D, E}, a) = 8 (B, a) u 8 (C, a) u 6 (D, a) u 8 (E, a)
= <|>u (E) u<|>u ( C , D ) = (C, D , E )
8 ({ B, C, D, E), b) = 8 (B, b) u 8 (C, b) u 8 (D, b) u 8 (E, b)
= (C, E) u (C) u (A) u <f>
= (A.C.E)
8 ((C), a) = (E)
8 ( ( C } , b ) = {CJ
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-86 Finite Automata
Step 5 : Two new subsets {C, D, E) and { E) are generated. Their successors are calculated.;
6({C, D, E ) , a ) •= (E) u <]> U (C, D} = {C, D, E)
8({C, D, E),b) = ( C ) u ( A ) U < | )
= (A,C)
8 ( { E ) , a ) = {C, D)
8((E),b) = 0
Step 6 : Two new subsets {A, C] and (C, D) are generated. Their successors are calculated.
8({C, D),a) = {E) u<|>={E}
8({C, D),b) = (C} u { A) = ( A, C)
8({A, C),a)’ = -{B, D ) u ( E } = (B,D, E)
8({A,C},b) = {C) = {C}
Step 7 : A new subset {B, D, E) is generated. Its successors are calculated.
8 ( ( B , D, E),a) = 0U<|)U{C, D} = {C, D}
8({B,D,E},b) = (C, E}u(A}u<t>
{A, C, E)
Every subset containing C is marked as final state. Final DFA is given in
Fig. Ex. 2.3.26(a).
b
b
(A.C.E1
a
b
a
{A,C})Y— <-
b
a,b b \aa
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-87
Solution :
NFA to DFA :
Step 1 : Writing transition from the state p. .
(Jpf) — T-CpJ!)
A| p p a.b
a,b
c {q}
8 ({q}, a) = 0, 8 ( { q } , b ) = <j>
8 ({p, q}, a) = 8 (p, a) u 8 (q, a)
= (p. q ) ’-'<!>={p.q}
8((p,q|,b) =8(p,b)u8(q,b)
= {q) u<|>= (q)
Scanned by CamScanner
CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp,) 2-88 Finite Automata
JwiT;
p.i
p.i
r® 0,1
r\
J Qi }
8({qo,qibO) = 8 ( q 0, O ) u 8 ( q ] ) O)
= {q0» Qi) 0 = {Qo Qi}
8 ({q0, q j , 1) = 8 (q0, l ) u 8 ( q 1} 1)
= ( qj {q0» qj = {q0, Qi}
e stands for a null symbol. A e transition allows transition on e (or no input), the empty
string. This implies that a machine can make a transition without any input.
• When finding the string described by a path containing arc with label e, the £ symbols
are discarded.
• The use of E-transition may simplify a transition graph by reducing number of labelled
arcs. ...
lt ■ \ ' ' J f. 1T (i
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 2-89
0
E
(a) Strings ending in 01 or 10 (b) Starting with zero or more 0’s followed by
zero or more J’s and ending in 01
S E
e
e
An NFA with E-move can be converted into an equivalent NFA without E-move. We can
always find the equivalence between systems with s-move and without E-move. This can be
understood with the help of an example.
N o ) ------*-
(a)
(C)
(ii)
Fig. 2.3.13 : NFA with e-move converted to NFA without e-move
If there is an e-move between any two states q, and q? then e-move can be removed a*
follows :
(1) All moves from qj should also originate from q p For example : If there is move from 4
to q k , then we must add a move on qj to q k.
(2) If q f is a final state then make as a final state.
Scanned by CamScanner
2-90 Finite Automata
Example 2.3.29 : Use direct method to find an equivalent NFA without e-move for the NFA
given below :
2
e
Fig.JEx. 23.29 : NFA with e-move
Solution :
Step 1 J Transitions from q 2 are also duplicated on q P qi is made a final state.
2
Fig. Ex. 23.29(b) : Final NFA without e-move after removing e-move between q 0 and qj
Scanned by CamScanner
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 2-91 Einite-Autgni
2.3.4.3 e-Closures
E-closures of a state q, is the set of states including q} where qj can reach by any number
of E-moves of the given non-deterministic finite automata.
E-closure of q, :
0
G e
E E
E s
Scanned by CamScanner
Theory of Comp, Sei, (MU-Sem. 5-Comp.) 2-92 Finite Automata
Scanned by CamScanner
>f Theory of Comp. Sci. (MU-Sem, 5-Comp.) 2-93 Finite Auton
Solution :
(a) Transition diagram is in Fig. Ex. 2.3.30(a).
a X b a
5 )— <
e
£ b
8(3, b) = 2
8*(l,ba) = (1,4,3)
2. 3.4.4 e-NFAtoDFA
Example 2.3.31 : Find an equivalent DFA for the e-NFA given in Fig. Ex. 2.3.31.
Ma
h— x!?) / — b
b
Fig. Ex. 2.3.31
Scanned by CamScanner
V Theory of Comp. Sei, (MU-Sem, 5-Comp.) 2.94 Finite Automata
Solution :
Step 1 : e-closure of states.
State E-closure Comment
q0 {qo» q q 2 » q 3 } There are E-moves from,q 0 to q h qi to q 2 , qj to q 3 .
qi (qi» q 2 > q 3 } There are E-moves from qi to q 2, q j to q 3
q2 • ( q2 ) There is no E-move from q 2.
q3 ( q3 ) There is no £-move from q 3.
= {q2 ) {q 3 > qJ = {q 2 »q 3, q 4 J
Step 3 : Two new subsets {q 2, q 3 , q 4 } and { q 0, q h q 2, q 3> q 4 } have been generated. Their
successors are calculated.
a-successor of { q 2, q 3> q 4 ) = e-closure (5(q 2, a) u 5(q„ a) u (q 4 , a)
= e-closure (<|> u {q 4 } U <|>) = e-closure (q 3 , q 4 )
= e-closure (q 3) o e-closure (q 4 )
CamScanner
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp,) 2-95 Finite Autom
u
= Uhl (Whl = (Wkl
b-successor of {q 2, q 3 , q 4 ) = (--closure (8(q 2, b) U S(q 3, b) u (q 4 , b))
= c-closurc ( {q 4 ) U ([) U (f))
= c-closurc (q 4) = { q 3 , q 4 )
a,b
13’
Step 4 : A new subset {q3 , q 4 ) has been generated. Successors of {q 3 , q 4 ) are calculated,
a-successor of {q 3 , q 4 ) = E-closure (S(q 3 , a) u S(q 4, b))
= E-closure ({q 4 ) u (()) =.E-closure (q 4) = {q,, q 4 )
b-successor of {q 3, q 4 } = E-closure (6(q 3, b) u S(q 4, b))
= E-closure (i|) u <)>) = <j)
No further subsets are generated. Every subset containing q 4 (final state i°
Fig. Ex. 2.3.31) is marked as final state.
a
a
a,b b
Scanned by CamScanner
CamScanner
V Theory of Comp, Sci. (MU-Sem. 5-Comp.) 2-96 Finite Automata
Step 2 : Two new subsets { p, q ) and (p, q, r} are generated. Their successors are calculated,
a-successor of ( p, q } = e-closure (8(p, a) u 8(q, a))
= e-closure ({p} u (q))
= e-closure ({p, q ) )
Scanned by CamScanner
"f Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-97 Finite Autom.,
------------------- ' ----------------------------------- -
= (p, q, r}
c-successor of {p, q, r} = c-successor { p, q) u c-successor of {r}
= {p, q, r} u e-closure (p) = {p, q, r}
No new subsets are generated. Each subset containing r (final state in given (e-NFA) is
marked as final state.
a
a,b,c
{p.qTn)
c {p, q , r ) *
(a) State transition diagram of final DFA (b) State transition table of final DFA
Fig. Ex. 2.3.32
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci (MU-Sem, 5-Comp.) 2-98 Finite Automata
Solution :
Scanned by CamScanner
CamScanner
Theory o f C o m p . Sci. ( M U - S e m . 5-Comp.) 2-99 Finite Automata
Example 2.3.34 : Design e-NFA for the following languages. Try to use e-transitions to
simplify the design
(a) The set of strings consisting of zero or more a’s followed by zero or
more b’s, followed by zero or more c’s. *
(b) The set of strings that consist of either 01 repeated zero or more time5
or 010 repeated zero or more times.
(c) The set of strings of 0’s and 1’s such that at least one of the last ten
positions is 1 .
Solution :
(a) The set of strings consisting of zero or more a’s followed by zero or more b’s, followe
by zero or more c’s.
Scanned by CamScanner
2-100 Finite Automata
E a b c
<10 2 A
<17)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-101
Solution :
Step 1 : Finding e-closure
State e-closure
qi { q i . q 2> q 3 )
q2 ( q 2 . q? )
03 ( q3 )
Step 2 : Initial state is taken as the E-closure of q P Transitions are written from it.
s ({qn q 2, q 3 }> = e-closure (8 (q, t 0) u 8 (q 2, 0) u 8 (qj, 0))
= E-closure (qj u 0 u (j>) = { q t , q 2, q 3 }
8 ({qp q 2 > q j ) , 1) = e-closure (8 (q h 1) u 8 (q 2, 1) u 8 (q3 , 1))
= E-closure (<j) u q 2 U p) = { q 2, q 3 }
8({qi, q 2. q j l . 2 ) = E-closure (8 (q ( , 2) u 8 (q 2 , 2) u 8 (q,, 2))
= E-closure ((|) u $ U q 3 ) = { q 3 }
{ q3 }
0,1
{ 2- 3 1 ({q3 0
2
Fig. Ex. 2.3.35(b)
Scanned by CamScanner
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-102 Finite Automata
Example 2.3.36 : Design an FSM to accept those strings having 101 or 110 as substring.
Solution : The required F S M is given below.
1.0
0
e 90
9oJ ((94
0
95 96
1.0
Fig. Ex. 2.3.36
• The path from q 0 —>qj —»q 2 ~»q3-*q4 is for the substring 101 .
• The path from q 0 —»q5 —>q6 —> q 7 —>q4 is for the substring 1 10.
No. of states. NFA has fewer number of states.. More, if NFA contains Q states
then the corresponding DFA will
have < 2 Q states.
Power NFA is as powerful as a DFA DFA is as powerful as an NFA
Finite automata that we have discussed so far have a limited capability of either
accepting a string or rejecting a string. Acceptance of a string was based on the reachability of
a machine from starting state to final state. Finite automata can also be used as an output
device.
• Such machines do no have final state/states.
Scanned by CamScanner
w
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-103
Machine generates an output on every input. The value of the output is a functio
* Of
current state and the current input.
Such machines are characterised by two behaviours :
1. State transition function (8)
2. Output function (X)
State transition function (8) is also known as STF.
Output function (X) is also known as machine function (MAF).
8:ZxQ-»Q
X : X x Q —> O [for Mealy machine]
X : Q —> O [for Moore machine]
There are two types of automata with outputs :
L Mealy machine : Output is associated with transition.
X : X x Q —> O
Set of output alphabet O can be different from the set of input alphabet Z
2. Moore machine : Output is associated with state.
X : Q —> 0
2,4.1 A Sample Mealy Machine
M U - Dec. 2005, Dec. 2010, Dec. 2017]
Input
a/0 Output
a/1
b/0
Scanned by CamScanner
Theoryo Comp Sci. (MU-Sem, 5-Comp.) 2-104 Finite Automata
<11 0 0
q2 I 0
q3 0 0
Fig. 2.4.3 : Output function for mealy machine of Fig. 2.4.1
<li qo/0 q 2 /0
q2 q</i q 3 /0
<h q 3 /0 q 3 /0
1
-----► Output
►Next state
Fig. 2.4.4 : State table depicting both transition and output
behavior of mealy machine of Fig. 2.4.1
Scanned by CamScanner
pay
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-105 Finite Auto
o
o
0
0 1 State Output
—>q0 e
"" 0 qi q2
q a
qi qi q2 '
q2 b
q2 qi q2
Fig. 2.4.6 : State transition function for Moore Fig. 2.4.7 : Output function for Moore machine
machine of Fig. 2.4.5
State
■ ■
Output
0 *
V
v'x ..
~ >qo Si q2 E
qi qi q2 a
q2 qt q2 b
Fig. 2.4.8 : State table depicting both transition and output
behavior of Moore machine of Fig. 2.4.5
Output in a Moore machine is associated with a state and not with transition.
Scanned by CamScanner
'Sfif ,
V Theo yj CompJSci. (MU-Sem. 5-Comp.) 2-106 Finite Automata
Example 2.4.1 : Give Mealy and Moore machine for the following :
From input S*, where S = (0, 1 , 2) print the residue dulo 5 of the input
treated as ternary (base 3). kMU B- May
A 2006, Dec. 2015
Solution :
(a) Mealy machine :
1/4
0/3
2/1
o/o
77 O’ 0/1 0/4 7-0
Qoj— ►— (qi)«7 ■■ -*■ — QU) J 2/4
1/1 2/3
v / 1/3
1/2
2/2
0/2
1/0
Fig. Ex. 2.4.1(a) : Mealy machine
In decimal In ternary
system system
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-107 Finite Autg
W0 <1?2 2
1
Fig. Ex. 2.4.1(b) : Moore machine
Example 2.4.2 : Draw the transition matrix and transition graph for a binary adder
1
0
1
Fig. Ex. 2.4.2(a) : Transition graph
( 0A ( 0A p A p A
loj llj l o j 11 J__________
c c
lo lo> 0 q 0, 1 q (), 1 qh 0 <— carry is 0
qi Qo» 1 0 Qi> 0 1 <— carry is 1
_________> Output
______> Next state
Fig. Ex. 2.4.2(b) : Transition matrix
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-108 ______ Finite Automata
Example 2.4.3 : Show MAF, STF, Transition graph and Transition matrix for a divisibility by
3 tester assuming ternary inputs
Solution :
A divisibility by 3 tester will have three states. These states will be based on running
remainder. The next remainder will be a function of current remainder and the next input. A
ternary system has three alphabets.
= {0, 1 , 2 )
q 0 = current remainder is 0
Qi = current remainder is 1
q 2 = current remainder is 2.
Next remainder as a function of current remainder (state) and the next input is given
below. ,
Current state (current remainder) Next input Next remainder (next state)
0 00 mod 1 0 = 0 q0
0(q 0) 1 01 mod 10 = 1 q]
2 02 mod 10 = 2 q2
(10)3 = (3) 10
0 10 mod 10 = 0 q0
1 (qi) 1 1 1 mod 10 = 1 qt
2 12 mod 1 0 = 2 q2
0 20 mod 1 0 = 0 q0
2 (q 2 ) 1 21 mod 10 = 1 qt
2 22 mod 1 0 = 2 q2
Machine will give an output 1 if the current remainder is 0, output will be 0 otherwise.
Scanned by CamScanner
CamScanner
Finite Antony
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-109
Current Inpul
-
2/0 J
0/1 state
' ■/> S'; J': ./-fa
1/0 2/0
1/0 q,/0 q 2 /0
q0 ) — 2 - >q0 q/i
2/0
qi q</i q,/0 q 2/0
0/1 1/0
q2 q</i q t /0 q 2/0
0/1
0 i 2 0 i 2
->q 0 i 0 0 -*q 0 q0 qi q2
qi i 0 0 qi q0 qi q2
q2 1 0 0 q2 qo qi q2
Example 2.4.4 : Design a Mealy machine that gives an output of 1 if the input string ends in
bab. ' :. .
Solution :
a/0 b/0
b/1 Qi qi.o
Example 2.4.5 : Design a Moore machine that gives an output of 1 if the input string ends
bab.
Scanned by CamScanner
CamScanner
y Theory of Comp. Sci. (MU-Sem.5-Comp.) 2-110 Finite Automata
Solution : ® b
Pq/O qi/0 q O
state
q2
i %
(b)
Example 2.4.7 : Design a Mealy machine to generate 1’s complement of a given binary
number.
Solution : 1/0
0 1
~>q0 q0 « i qo>o
(a) (b)
Scanned by CamScanner
Finite
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-111 Automat
Example 2.4.8 : Design a Moore machine for generating output 1 if input of binary sequent
1 is preceded with exactly two zeros.
Solution
Input Output
o 0 1
q2
0
q2
S
/ 1 q2
q3 qi
Example 2.4.9 : Design, a mealy machine for a binary input sequence such that if the
sequence ends with 100 the output is 1 otherwise output is 0.
MUADec 2006, May 2008, Dec, 2008
Solution :
0 1
0/0 1/0 1/0
qo q 9, o qi.o
Ao 32
1/0 0/0 q< q 2, 0 qi.o
~o7T q2 qo- 1 q o
(a) State diagram (b) State table
Fig. Ex. 2.4.9
Meaning of various states :
q0 - start state
qj - previous symbol is 1
q 2 - preceding two symbols are 10
A transition from q 2 to q0 will make the preceding three symbol as 100 and hence
output 1.
f
Example 2.4.10 : Design a mealy machine for incrementing the value of any binary num
by one. The output should also be a binary number with value one
than the given number.
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-112 Finite Automata
Solution :
When a binary number is incremented then trailing 1’s become 0’s and the first zero
immediately beloie trailing l’s become 1.
For example, if we add 1 to 0101 111 then the incremented value will be 01 10000.
0 10 1111
+ 1
0 110 0 0 0
The mealy machine is shown in Fig. Ex. 2.4.10.
0/0
1/0 1/1
Input should be applied, starting from the
0/1
->■-----( 4 i rightmost symbol.
Fig. Ex. 2.4.10
0 1
1 0
->qo q t , q 0.
qi qi. o qi. i
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-113 Finite Autor
Symbols State
20 — q 2o
21 --- 921
22 — 922
Scanned by CamScanner
"T Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-114 Finite Automata
Example 2.4.12 : Design a Mealy machine that will read sequences made up of vowels of
English language, it will give output in same sequences, except in cases
where a T follows ‘e’, it will be changed to ‘u’.
Solution : l/i
a/a
s. o/o e/e
J u u Current Next
state output
M state
<1 u
»
• Machine must convert every occurrence of ei into eu. In other cases, output will be same
as input.
• State q 3 implies that the preceding character is e.
• An input i in stat q r will generate an output u.
Example 2.4.13 : Give the Mealy and Moore machine for the following processes.
“For input from (0 + 1)*, if inp ’ • • - . . ...
; \ ; output y; otherwise output z”.
Solution :
(a) Design of Mealy machine :
1/z
fq2
1/z/
0/z
Mo)----- 0/y
0/z
1/x
0/z
Fig. Ex. 2.4.13(a) : State diagram of Mealy machine
The mealy machine must be designed to remember the preceding two symbols
particularly ‘11’ and ‘10’.
The state q 2 is for preceding two symbols as ‘ 11’.
The state q 3 is for preceding two symbols as ‘ 10’.
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-115 Finite Autonym
• An input of 0 in state q 2 will mean a sequence of 1 10 (hence the output y), and it wi|]
make preceding two symbols as 10 (q 3 stands for preceding two symbols as 10).
• Similar argument can be given for the state q r
(b) Design of Moore machine :
Example 2.4.14 : Design a Moore and Mealy machine for a binary input sequence such that if
it has a substring 110 the machine outputs A, if it has a substring 101
machine outputs B, otherwise outputs C.
Solution :
(a) Design of Mealy machine :
0/A.1/A
1/C
0/A
0/C 32 I----------
1/C
1/C X—
-+—C 31
0/C
1/B
0/C
0/8, 1/B
Fig. Ex. 2.4.14(a) : State diagram of the Mealy machine
Scanned by CamScanner
V Theory of Comp. Sei. (Mu-Sem, 5-Comp.) 2-116 Finite Automata
o
0
0,1
Fig. Ex. 2.4.14(b) : State diagram of the Moore machine
Solution :
Let the two numbers are A and B.
where, A = an , a n _j . . . . aj
B = b bn-j .... bj
Numbers are entered bitwise, starting from the least significant digit. If there is a
mismatch at the i th bit position then we can conclude the following.
A > B if ai > bj
A < B if a1 < bj
Machine generates the following output :
A > B ... +
A < B ... -
A = B ... 0
0 a Output
0
1 0
0 0
1 0
1
b.
1
0
0
0
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-117 ■ Finite Autp
Example 2.4.16 : Design a Moore Machine for checking divisibility by 3 of a given deci
number (residue of 3).
Solution :
The decimal system has 10 alphabet.
S = {0, 1 , 2 . 3 , 4 , 5 , 6 , 7 , 8 , 9 1
The running remained when a given decimal number is divided by 3 could be :
0 associated state is q0 .
1 —> associated state is q t
2 —> associated state is q 2 .
The required Moore machine is given Fig. Ex. 2.4.16.
2,5,8
0,3,6,9 _ _ ___ ____ 0,3,6,9
2,5,8
1.4,7 x—
— — ►— (qyo
1 .4,7 Si-
2.5,8
0,3,6,9
1.4,7
A Mealy machine can be transformed into an equivalent Moore machine so that both the
machines show the same output behaviour for any input sequence. Conversion process is beinj
explained with the help of a Mealy machine shown in Fig. 2.4.9.
1/1
Present (Next state, output)
o/o
state Input 0 Input 1
1/0 0/1
->P r. 0 q, 0
q p, 1 s, 0 1/0 /
0/1
/ 1/0
r q, i p. 1
s s, 1 r.O '0/1
Scanned by CamScanner
y Theory of Comp, Sci, (MU-Sem. 5-Comp.) 2-118 Finite Automata
r/0
0
o o
Fig. 2.4.9(c) : State diagram of Moore machine
Scanned by CamScanner
XSfflT
1
v Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-11 9
. . . . Transitions
...... ....... for
. . . ...Mealy
....... • Machine Equivalent transitions for Moore Machine
8 (p, 0) = r, 0 6 (p, 0) - r
8 (p, 1) = q, 0 8(p, l) = q0
q 0 is associated with output 0
8 (q, 0) = p, 1 8 (q 0 . 0) = p
8(qi,0) = p
8 (q, 1) = S, 0 8 (q 0 , 1) = s 0
8 ( q „ l ) = s0
8 ( r , l ) = p, 1 8 (r, 1) = p
8 (s, 0) = s, 1 8 (s0 , 0) = s 1
8 ( S | , 0) = s ,
Transition is duplicated for both s 0 and s P s, ' s |
associated with output 1.
8 ( s „ 1) = r
State transition table for the Moore machine is given in Fig. 2.4.9(d).
Scanned by CamScanner
V? Theory of Comp, Sci. (MU-Sem. 5-Comp.) 2-120 Finite Automata
->p r q0 1
q0 P So 0
qi P So 1
r qi p 0
s0 Si r 0
s 1
i Si r
Fig. 2.4.9(d) : State transition table
Step 3 : Moore machine of Fig. 2.4.9(c) produces an output 1 without any input. Thus, the
Moore machine gives an output 1 on a zero length sequence. But the Mealy machine
will produce no output for a zero length sequence.
To handle the above problem, we must split the start state p into two states :
pE - Giving no output
p! - Giving an output 1.
Final Moore machine is drawn in Fig. 2.4.9(e).
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-121
Example 2.4.17 : Construct a Mealy machine that accept strings ending in ‘00’ and *ii>
Convert the same to a Moore machine.
Solution :
(i) Construction of Mealy machine.
0/1
Current <N»t s M .,O„,p071
0/0 state Q
... ...■ 1
q„0 q 2. o
% 1/0 0/0
i/o qi qo 1 q 2>o
“ 2
q2 qb 0
1/1
(a) State transition diagram of the Mealy (b) State transition table of the Mealy machine
machine
The Mealy machine is shown in the Fig. Ex. 2.4.17. Interpretation of various statesis
given below.
q 0 = Initial state
qi = Preceding input is 0, 0 as next input will complete the sequence 00.
q 2 = Preceding input is I, 1 as next input will complete the sequence 11.
(ii) Conversion to a Moore machine :
Step 1 : Splitting of states
(i) There is no incoming line into the state q 0 . Output associated with q 0 will be e.
(ii) There are three incoming lines into the state q P
(a) q 0 to q, on input 0, with output 0.
(b) qj to q j on input 0, with output 1.
(c) q 2 to qj on input 0, with output 0.
Since there are two different outputs, we must split q 0 into two different states p0 W*
output 0 and pj with output 1.
(iii) Similarly, we must split q 2 into two different states s 0 with output 0 and s , with output !■
Scanned by CamScanner
CamScanner
Theory of Comp, Sci. (MU-Sem. 5-Comp.) 2-122 Finite Automata
0 Output
0
0
qo Po So
s
Po Pi o
0
Pl Pi So
So Po Si
Si Po Si
(c) State transition diagram of the Moore (d) State transition table of the Moore
machine machine
Fig. Ex. 2.4.17
8 (q b 1) = qi, 1 8 (p0 , 0) = p,
8 (Pi, 0) = P]
Transition is duplicated for both p0 and p t . p l is
associated with output 1
=
8 (Qi, 1) = q 2 > 0 5 (po* 0 So
5 ( p P l ) = s0
Transition is duplicated for both p0 and p P s 0 is
associated with output 0.
b (Q2’ 0) =
Qi» 0 8 (s0 , 0) = p0
8 (s, 0) = Po
Transition is duplicated for both s0 and s t . p0 is
associated with output 0.
5 5 (Sy, 1) = S|
(q 2 , 1) = q 2, 1
b (S|, 1 ) = S|
Transition is duplicated for both s 0 and s P s , is associated
with output 1.
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-123
. Convert it to Moore.
Example 2.4.18
g
Next
I
Present state a- 0 a=1
qi 1 q2 0
->qi
q4 1 q4 1
q2
q3 q2 1 q3 1
q4 q3 0 qi 1
Solution :
Step 1 : Splitting of states.
(i) Incoming lines into qj always give an output 1.
(ii) Incoming lines into q 2 give both 0 and 1 as output.
We must split q 2 into two different states p 0 with output 0 and p, with output 1.
(iii) Incoming lines into q 3 give both 0 and 1 as output.
We must split q 3 into two different states so with output 0 and Sj with output 1.
(iv) Incoming lines into q 4 always give an output 1 .
Step 2 : Drawing of an equivalent Moore machine.
\o,i
0,1
o o
o
1
1
Step 3 : Moore machine of Fig. Ex. 2.4. 18(a) produces an output of 1 without any input. B
the Mealy machine given in Example 2.4.18 does not produce any output for a
length input sequence.
To handle the above problem, we must split the start state q t into two states :
r e - Giving no output
Tj - Giving an output 1.
Scanned by CamScanner
Theory of Comp, Sci. (MU-Sem, 5-Comp.) 2-124 Finite Automata
0,1
o 0
1
Fig. Ex. 2.4.18(b) : State transition diagram for an equivalent Moore machine
•
Current state 0 1 Output
r
e n Po 8
n Po 1
Po q4 q4 0
Pl q4 q4 1
So Pi Si 0
. Sj pi Si 1
Fig. Ex. 2.4.18(c) : State transition table for an equivalent Moore machine
Example 2.4.19 : Convert the Mealy machine shown i n Fig. Ex.2.4.19 to M o o r e machine.
,b/0 a/0
a/1
s
()
b/1
Solution :
Step 1 : Splitting of states.
(i) Incoming lines into s0 give both 0 and 1 as output. In addition, s0 in an initial state and
hence an e-output condition should also be considered. s0 is split into three states :
pE with output 8,
p0 with output 0,
P j with output 1.
Scanned by CamScanner
7
v Theory of Comp, Sei, (MU-Sem, 5-Comp ) 2-125
(ii) Incoming lines into Sj give both 0 and 1 as output. We must split Sj into two diff er
states.
q 0 with output 0,
qj with output 1.
Step 2 : Drawing of an equivalent Moore machine.
b
a
a
b a a
b
b
Example 2.4.20 : Convert the Mealy machine shown in Fig. Ex.2.4. 20 to its equivalent Moore
machine.
L J b/1
a/1
<~>b/0
% -----------------►-
Xb/0
b/1 a/0
Scanned by CamScanner
jr Thegryo Comp Sci. (MU-Sem. 5-Comp.) 2-126 Finite Automata
B j with output 1.
(iii) Qi i s associated with two different outputs 0 and 1.
We must split q2 into two different states :
C o with output 0,
C j with output 1.
(iv) q 3 is associated with two different outputs 0 and 1. We must split q 3 into two different
states :
D o with output 0,
D j with output 1.
Step 2 : Drawing of an equivalent Moore machine.
b b
a
a a
b )b
c/i)
a
b a
b
b
Scanned by CamScanner
y Theory of Comp. Sci. (Mll-Sem. 5-Comp.) 2-127
->P s q 0
q q r 1
r r s 0
s s P 0
p, 0
(ii) ‘ Moving the output associated with q, which is 1 to transitions entering into state q, we
get:
q, i
p, o
(iii) Moving the output associated with r, which is 0 to transitions entering into the state f-
we get :
q» i
q q, 1
P.O
Scanned by CamScanner
sF Theoryofjgomp, Sei, (MU-Sem. 5-Comp.) 2-128 Finite Automata
(iv) Moving the output associated with s, which is 0 to transitions entering the state s, we
get:
0 1
->p s, 0 q, 1
q q, 1 r,0
r r.O s, 0
s s, 0 p, 0
Fig. 2.4.10(b) : A trivial Mealy machine
Step 2 : The trivial Mealy machine obtained in Fig. 2.4.10(b) can not be minimized further.
Final Mealy machine is given in Fig. 2.4.10(c) and 2.4.10(d).
0 1
o/o
— s, 0 q, 1
1/1 1/0 1/0
q q, 1 r.O (q j— —>— ( s
r r, 0 s, 0 0/0
0/1
s s, 0 P.O
1/0
(c) Transition table for final Mealy machine (d) Transition diagram for final Mealy machine
Fig. 2.4.10
bv
Solution :
Step 1 : Construction of a trivial Mealy machine by moving output associated with a state to
transitions entering that state, we get :
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-129
a
PqL_ a/0
x
b/0/
b/1
/ a/1
q2
a/0
b/0
(a)
“*Po r Po e
Pl r Po 1
Po Pi So 0
Pl Pi So 1
r Pi Pl 0
s0 Si r 0
S
1 Si r 1
Solution :
Step 1 : Construction of a trivial Mealy machine by moving output associated with a state
transitions entering that state, we get :
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-130 Finite Automata
(Nextstate, O u t p u t ) . .
® » ifes 0 1
Po r, 0 qo<°
Pl r,0 q0> 0
q0 Ph 1 s o, O
qi Ph 1 s 0 ,O
r qi» 1 Pi, 1
s0 s„ 1 r, 0
s Sp 1 r, 0
i
Fig. Ex. 2.4.22(a) : A trivial Mealy machine
Step 2 : Minimization of the trivial Mealy machine obtained in Fig. Ex. 2.4.22(a).
(i) 1 -equivalent partitions Pi based on outputs.
p
i = (Po- Pl) (Qo> s o- S 1) (r)
(ii) 2-equivalent partitions p 2 based on transitions.
s s
(p 0 -Pi) (qo'<ii' o' i) ( r)
t __ I _t ■<------ T ransition o n input 0
t __ Lt
I ______________]
Fig. Ex. 2.4.22(b)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Som. 5-Comp.) 2-131 Finite Autom
Example 2.4.23 : Consider the Moore machine described by the transition table given belo
Construct corresponding Mealy machine. ______
Present state Next State y Output
a= 0 a=0
Ai q2 0
-*11
q2 % 0
q3 A3 1
Solution :
Step 1 : Construction of a trivial Mealy machine by moving output associated with a state to
transitions entering that state, we get :
0 1
“*11 qP o q 2’°
q2 qPo q 3, l
qP o q 3, 1
Step 2 : Minimization of the trivial Mealy machine obtained in stepl.
States q 2 and q 3 are equivalent as they have the same output and transition behaviour.
The minimum state machine is given below :
0 1
-*11
qp0 q 2’°
( 2’ A 3 ) ~ Q2 q P° q 2J
Scanned by CamScanner
V Theory of Comp, Sei, (MU-Sem. 5-Comp.) 2-132 Finite Automata
a/0
a/1
b/1
Ao - - -CjaD371
b/OY lb/1
a/1
Minimization process is being explained step wise for the Mealy machine given in
Fig. 2.5.1.
Step 1 : Finding 1-equivalent partitions pj based on outputs.
Pj = (q0 * 92* q 4) (q i . q qs)
q q 2, q 4 have the same output behaviour i.e. 0, 1.
q p q 3, q5 have the same output behaviour i.e. 0, 0.
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-133
Transition o n 1.
Transition on 0.
Block 1 Block 2
Fig. 2.5.1(a)
1/1
0/0 1/1
0/0 /
0/0
1/0
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-134 Finite Automata
Example 2.5.1 : Find the equivalent partition and corresponding reduced machine in
standard form, for the following machine -
NS z
X=0 X=1
A F.O B, 1
B G, 0 A, 1 PS = Present state
. 0 B, 0 C, 1 NS = Next state
D C, 0 B, 1 Z = Output
E D, 0 A, 1 X = l/P
F E, 1 F, 1
G E, 1 G, 1
Solution :
Step 1 : 1 -equivalent partitions pj based on outputs
Pi = (A, B, C, D, E) (F, G)
Step 2 : 2-equivalent partitions P 2 based on transitions.
Transition on 1.
t
f
L_J
Transition on 0.
Transition o n 1.
i i ri n nt
(A, B)(C, D,E) (F, G)
Transition o n 0.
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-1 35
Scanned by CamScanner
Theory of Comp. Sci (MU-Sem. 5-Comp.) 2-136 ___________________ Finite Automata
May 2006
Q. 3 Design the finite state machine that compares two binary numbers to determine
whether they are equal and which of the two is larger. (Example 2.4.15) (8 Marks)
Q. 4 Given the Moore and Mealy machine for the following :
For the input from S*, where X = { 0, 1 , 2 } , print the residue modulo 5 of the input
treated as ternary. (Example 2.4.1) (10 Marks)
Dec. 2006
Q. 6 Give NFA and minimized DFA for the language over {0, 1} having all strings such that
the third symbol form the right end is zero.
(Similar to Example 2.2.18) (Note : Take 0 as 1 and 1 as 0) (12 Marks)
Q. 8 Compare Mealy and Moore machine. Design a Mealy machine to convert each
occurrence of substring abb by aba. S = { a, b }
(Section 2.5.1, Similar to Example 4.5.9) (10 Marks)
Scanned by CamScanner
V Theory of Comp. Scl, (MU-Som. 5-Comp.) 2-137
May 2007
Dec. 2007
Q. 12 Design the DFA which accepts set of strings such that every string containing 00 a$ 3
substring but not 000 as a substring. (Example 2.2.8) (8 Marks'
Q. 13 Design FSM that compares two binary numbers to determine whether they are equal
and which of the two are larger. (Example 2.4.1 5) (10 Marks)
May 2008
e a b c
->p '!> {P} {q} {r}
q {p) {q} {r} <l>
r* {q} {r} {p}
(a) Compute the e-closure of each state.
(b) Convert the automation to a DFA. (E sample 2.13.32) (10 Marks)
Q. 15 Design the DFA for the language, contains strings in which leftmost symbol differ from
rightmost symbol. X is given by {0, 1}. (Example 2.2.9) (10 Marks)
Q. 16 Design Moore machine to convert each occurrence of substring 1000 by 1001.
(Similar to Example 2.4.9) (10 Marks)
Dec. 2008
Q. 17 Design the DFA for the language, contains strings in which leftmost symbol differ from
rightmost symbol. Z is given by {0, 1). (Example 2.2.9) (10 Marks)
Q. 18 Compare and contrast Moore and Mealy machine. Design a Mealy machine to convert
each occurrence of substring abb by aba S = { a, b }.
(section 2.5.1, Similar to Example 2.4.9) (10 Marks)
May 2009
Q. 19 Give DFA accepting the following languages over the character (0, 1)
Scanned by CamScanner
Theory of Comp, Sei, (MU-Sem. 5-Comp.) 2-138 Finite Automata
Q. 20 Design a Mealy machine that accepts an input from (0 + 1)*, if the input ends in 101,
output A; if the input ends in 110, output B, otherwise C.
(Similar to Example 2.4.13) (5 Marks)
Q. 21 Write down minimization algorithm and using it find out the minimum state finite
automation equivalent to the transition table given below :
0 1
->A B E
B C F
C D H
D E H
E F I
F* G B
G H B
H I C
r A E
Dec. 2009
Scanned by CamScanner
CamScanner
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 2-139
Q. 26 Obtain DFA to accept strings of a’s and b’s with even no .of a’s and even no. of
(Example 2.2.2) (Note : Take 0 as a and 1 as b) (5
Q. 27 Give Mealy and Moore machine for input from S = { 0, 1 }, if the input ends in
output should be x, if input ends in 110, output should be y, otherwise output sho
be z. (Example 2.4.13) (10 Mari j
V.- ■ ■
Dec. 2010
(i) a set of all strings with odd number of one’s followed by even number of zeros
(ii) a set of all strings with which begin and end with different letters Z ={x, y, z}
(Example 2.2.29) (10 Marks)
Q. 29 Explain different types of machines and state at least one application of each
(Sections 2.4 and 2.4.1) (10 Marks)
May 2011
Q. 30 Differentiate between-
0 1
—>a b a
b a c
c d b
*d d a
e d f
f g e
g f g
h h d
(Similar to Example 2.2.35) (Note : Consider q 0 as a, q 1 as b and so on)(10 Mar
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-140 Finite Automata
Dec.....2011
______0
-» p (p. q) (p)
q (n sj {t}
r (p. r) {t}
*S (>
*t b q
May 2012
Q. 33 Compare and Contrast Moore and Mealy Machine. (Section 2.5.1) (5 Marks)
Dec. 2012
■'
Q. 34 Differentiate Moore and Mealy machine. (Section 2.5.1) (5 Marks)
Q.35 What is finite automation ? Give the finite automation M accepting (a,b)*(baaa).
(Section 2.1) (5 Marks)
Ans. :
The R.E. = (a, b)* (baaa), represents strings ending in baaa.
The FA is given below
a,b
—@ ® — MS) — M9
Fig. 1-Q. 35
Q 36 Differentiate between Moore and Mealy machine with proper example and usage carry
out conversion of Moore M/C to Mealy M/C. (Sections 2.5.1 and 2.4.6) (10 Marks)
May 2013
Q 37 Differentiate between Moore and Mealy machine with proper example and usage carry
out conversion of Moore M/C to Mealy M/C. (Sections 2.4.6 and 2.5.1 ) (1 0 Marks)
May 2014
Scanned by CamScanner
IMF
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-141 == Finite Autom
1
------— - I II 11
■ .... 'I = --
Q. 40 Compare and Contrast -Moore and Mealy Machine. Construct Moore Machine to fjnd
out the residue-modulo-3 for binary numbers.
(Section 2.5.1 and Example 2.2.23)
Ans. :
1
qi/1
Fig. 1-Q. 40
(For details, please refer to Example 2.2.3)
Q. 41 Construct NFA that accepts a set of all strings over {a, b} ending with “abb” Convert
this NFA to Equivalent DFA. (Similar to Example 2.3.14) (10 Marks)
Q. 42 State and prove equivalence of NFA and DFA. (Theorem 2.1) (5 Marks)
Q. 43 Design a DFA to accept strings over the alphabet set {a, b} that begin with ‘aa’ but not
end with ‘aa’. (10 Marks)
Ans. : [Hint : Refer as Example 2.2.6(A)]
a
<13
a
b
Q4
Fig. 1-Q. 43
Scanned by CamScanner
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-142 Finite Automata
Q. 4 4 Design a MOORE and MEALY machine to decrement a binary number. (10 Marks)
Ans. : [Hint : Refer as Example 2.4.1(A)]
One can decrement a binary by adding 1 1...1 (all l ’ s is 2’s complement of 1) to the
Mealy m a c h i n e : 1/1
0/1
0/0
Fig. 1-Q. 44
Fig. 2-Q. 44 1
May 2015
Q . 46 Design minimized DFA for accepting strings ending with 100 over alphabet (0,1).
(10 Marks)
A, At a2
A2 At
0 * %
Aj
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-143
q 3 to q t on input 1 :
oqr
characters as ‘1001’ only die last character ‘1’ is relevant to ‘100’.
q3 to q 0 on input 0 :
An input of 0 in q 3 will make die previous four characters ‘1000’. Out of the f Oll
characters ‘1000’, nothing is relevant to ‘100*.
Q, 47 Design Moore Machine to generate output A if string is ending with abb, B if sf r jr
ending with aba and C otherwise over alphabet (a, b). and convert it to meal?
machine. (10 Marks)
Ans.: [Hint : Refer as Example 2.4.9(A)]
Design of Moore machine
a a
b
\ a b
Fig. 1-Q. 47
Conversion into Mealy machine :
Step 1 : Construction of a trivial Mealy machine by moving output associated with a state to
transition entering into that state.
a b
qo q i . c q0. C
qi q i . c q2» c
q2 qi. b q3. a
q3 q i . c 9o» C
q4 q i . c
Step 2 : Minimization
The two states qj and q 4 can be merged into a single state, say q r
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-144 Finite Automata
a b
c
q0 lu C q 0, C
qi q„C q 2, C
q2 qb B q3, A
<13 qh C qp, c
The two state q 0 , q 3 can be merged into a single state, say q 0 .
a b
q0 q.-c qo.c
qi qi.c C|
q2 qi,B q». a
The final Mealy machine is
b/C a/C
b/C
q 0 )------>-------( q ? 2)
<< a/C
a/B
b/A
Fig. 2-Q. 47
a b
Fig. 1-Q. 48
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-145
Fig. 2-Q. 48
Since all 3 states in the NFA are final states, we can merge all 3 states
/. NFA - without 6 is
Fig. 3-Q. 48
Q. 49 Design the DFA to accept the language containing all the strings over
X = {a, b, c} that starts and ends with different symbols. (10 Marks)
Ans. : [Hint : Refer Example as Example 2.2.22(A)]
M ={Q. S. 8, q0 , F )
Q ={q 0 . q i . 02.03. O4.o5. q 6. o?)
S ={a, b, c )
q 0 =initial state
F ={<13. 05. 0 7 }
Ho
<>a,b
94
a,c
c,a
<45
. 49 : DFA
8 = Transitions are :
Scanned by CamScanner
CamScanner
V Theory of Comp, Sei, (MU-Sem, 5-Comp.) 2-146 Finite Automata
Fig. 1-Q. 51 : FA
Scanned by CamScanner
CamScanner
V" Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-147
May 2016
Q. 53 Design the DFA to accept all the binary strings over Z = {0, 1} that are beginning W jth
1 and having its decimal value multiple of 5. (10 Marks)
Ans. : [Hint : Refer Example as Example 2.2.22(C)]
Running remained is maintained through (he states q 0 , q j, q2, q 3’ q 4* the number starr
with 0, it is rejected. 0*.
* — Cq 3y— * — ( q 4)
35
0,1
Fig. 1-Q. 53 : DFA
q0 0 00 + 5 = 0(q 0 ) 0 1 + 5 = 1 (q,)
Qi 1 10 + 5 = 2 ( q 2) 1 1 + 5 = 3 (qj)
q2 10 100 + 5 = 4 (q„) 1 0 1 + 5 = 0 ( q 0)
q3 11 110 + 5 = 1 ( q j 11 1 5 = 2 (qj
0 10 110 10 0 0
! ___________| | | => 1010011000
I I
Complement No change
every bit
Fig. 1-Q. 54
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-148 Finite Automata
Fig. 2-Q. 54
b
e
State a
->q0 {q 0 . q<) { q j {}
qi { q 2} { qi.q2) {}
*q2 { q 0) { q 2} {qj
Ans. :
[Hint : Refer Example as Example 2.3.28(A)]
Fig. 1-Q. 55
e -closure of states :
Qo (Qo)
qi -> (Qi)
q 2 -» (Qi.qz)
NFA to DFA using direct method.
a
a,b
Fig. 2-Q. 5$
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-149
Dec. 2016
..........................
Q. 56 Design a DFA over an alphabet E = {a. b} to recognize a language in which every ‘a >.
followed by ‘b’ (5 Mark,’
Ans. : [Hint : Please refer example as Example 2.2.22(D)]
a,b
a a
Fig. 1-Q. 56
• If ‘a’ is followed by ‘a’ then the machine enters the failure state q0
• A ‘b’ immediately after ‘a’ takes the machine to the accepting state q0
Q. 57 Design a mealy machine to determine the residue mod 3 of a binary number.
(5 Marks)
Ans. : [Hint : Please refer Example as Example 2.4.20(A)]
q0 /1 V 0;
Fig 1-Q. 57
{ q0 . qj q< {}
qi . ( q 2) { qo q2} {}
*q2 { q0) { q 2} {q t )
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 2-150 Finite Automata
{q 0 .qd
{q 0 . q d
a,b
< Hd
{qo» q d
a,b r
(qi) fai . qaM
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-151
a
a
b
a
b
a,b
May 2017
Q. 59 Differentiate between NFA and DFA. (Section 2.3.5) (5 Marks)
Q. 60 Design FA to check divisibility by 3 to binary number. (Example 2.2.23) (10 Marks)
Q. 61 Design minimized DFA for accepting strings ending with 100 over alphabet (0, 1).
(Example 2.2.6(c), All strings ending in abb (take 0 as a and 1 as b)) (10 Marks)
Dec, 2017
Q. 62 Differentiate between DFA and NFA. (Section 2.3.5) (5 Marks)
Q. 63 Design a Finite State Machine to determine whether ternary number (base 3) is
divisible 5. (Example 2.2.25) { 1 0 Marks )
Q. 64 Write short note on Moore and Mealy Machine. (Sections 2.4.1 to 2.4.4) (5 Marks)
Chapter Ends..-
□□Q
Scanned by CamScanner
Regular Expressions
and Languages
Module 2
3.1 Introduction
MU - Dec. 2010
The set of strings accepted by finite automata is known as regular language. This
language can also be described in a compact form using a set of operators.
1) + , union operator
2) . , concatenation operator
An expression written using the set of operators (+, • , *) and describing a regular
language is known as regular expression. Regular expressions for some basic automata are
given in Fig. 3.1.1. '< _____• ______. ! _________ .
(a) R.E. = a
<n J ‘
1
— O) (a,b) R.E = a + b
Scanned by CamScanner
CamScanner
; Scl. 3-2 Regular Expressions ai
or a
aa
Scanned by CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 3-3 Regular Expressions and Languages
R* is also regular,
R* is also regular,
r+ is also regular.
Scanned by CamScanner
V Theoty of Comp. Scl. (MU-Sem. 5-Comp.) 3-4 Regular Expressions and Lang
(h) The regular expression for a set of strings over alphabet (a, b} starting with a and endjj)
in b is given by,
R.E. = a(a + b)*b
3
0 4. b) stands for (a + b) (a + b) (a + b) = aaa + aab + aba + abb + baa + bab + bba +
FA for Rj is given by o Ri
FA for R2 is given by
Scanned by CamScanner
1
Theory of Comp Sci. (MU-Sem. 5-Comp.) 3-5 Regular Expressions and Languages
2. FA for Rj • R 2 is given by
>O R1 o
3. FA for R* is given by
e e
OR
Finite automata for some primitive strings are shown in Fig. 3.2.1.
— where a; e Z
a
— where a e E
where a,b 6 2
ab —*o— ~o—
Fig. 3.2.1 contd...
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-6 Regular Expressions and Langi .
where a,b e E
rni or — +(( J
or
») or
Fig. 3.2.1
Example 3.2.1 : Write a note on Kleen’s theorem.
Solution : Kleen’s theorem says that if a language can be defined by any one of these three
ways;.;
Transition Diagram
(ii) Transition Table
(iii) Regular Expression
Then, it can be defined by other two ways.
Example 3.2.2 : Construct a finite automata for the regular expression
R = (0 + 1(01)*)*
Solution :
(0+1(01)*)*
Step 1
Step 2
1(01)*
c
Step 3
Scanned by CamScanner
V Theory of Comp, Sei, (MU-Sem. 5-Comp.) ___3-7 Regular Expressions and Languages
(01)
Step 4
Step 5
8 8
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-8 Regular Expressions and Langi ,
11
Fig. Ex. 3.2.4
(i) (a + ab)* :
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-9 RegularExpressions and Languages
Formal definition :
A regular expression over alphabet
q = (a 1? a 2 , . . . aj is defined recursively as follows :
1. An empty set ([> is a regular expression.
2. A null string e is a regular expression.
3. An alphabet a ] g Z is a regular expression.
4. If Tj and r 2 are regular expression then :
(a) Their concatenation ii • r2 is a regular expression.
(b) Their union rj + r2 is a regular expression.
(c) Closure of r b i.e. r* is a regular expression.
5. A regular expression is generated through a finite number of applications of above rules.
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-10
_____Regular Expressions and Lann,.,
eR = Re=R
•■■(3.3)
E* = E
••■(3.4)
(|>* = E
•••(3.5)
R+ R = R
-(3.6)
PQ + PR = P(Q + R)
■■•(3.7)
QP + RP = (Q + R)P
■••(3.8)
R*R* = R *
•••(3.9)
RR* = R*R •••(3.10)
(R*)* _ R *
•••(3.11)
E + RR* = R* •••(3.12)
(PQ)*P = P(QP)* ••■(3.13)
£ E
There is only one path from the start state to the final state, recognizing e • R • e = R-
Hence, 0+R= R
2. <t> - R = <>
A composite machine for <|) ■ R is given by
£
There is no way of reaching the final state from the start state.
<|)-R = <|>
3. e-R = R
FA for e is given by
FA for R is given by R CQ
Scanned
Scanned by
by CamScanner
CamScanner
Theory of Comp. Sci (MU-Sem. 5-Comp.) 3-11 Regular Expressions and Languages
An
-k 7
/Q
W
p, Q
[ Q
qi
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-12
Q
Behaviour of both {q 0 , q j and {q h q 2 } are
identical and they can be merged into a single state say Q
o
9o*
Example 3.3.1 : Give the examples of sets that demonstrates the following inequality. He-e
r1f r2 , r3 are regular expression :
(1) (2) r r r n
(3) r1 • r1 * r1 (4) r1 + (r2 • r3 ) (r1 + r2 ) • (r, • r3 )
Solution : Let u s make the following assumptions :
n = o r2 = 1 r3 = 1
Sr. R.E. L.HJS. of R.E. R.H.S. of R.E. Remark
No. i 1 •
1. q + e # 0 +e 0 L.H.S. RJLS.
2. n ri*ri 0.0 0 L.H.S. R.H.S.
3. Tl ‘i2 r2 Ti 0.1 1.0 L.H.S. R-H.5
4. n (r2 T 3 ) (ri + r 2 ) 0+10 (0 + 1) • 00 = 000 + 100 L.H.S. R.H.S.
fri • r 3 )
Example 3.3.2 : Prove that
e + 1*(011)‘(1*(011)*)* = (1 +011)*
Solution :
L.H.S. = e + l*(011)*(l*(011)*)*
= e + RR*, where R is taken as l*(01 1)*
= R* . . . [By Equation (3.12)1
= (l*(011)*)*
= (1+011)* ...[By Equation (3.1
= R.H.S.
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-13 Regular Expressions and Languages
Example3.3.3 :■.
(1 +00*1) ( 0 + 1 0*1)*(0
May 2006, Dec. 2007
Solution :
L.H.S. = (1 +00*1) + (1 +00*1) ( 0 + 10*l)*(0+ 10*1)
= R.H.S.
Example 3.3.4 : Prove or disprove the given regular expression (rs + r)* = r(sr + r)*
Solution :
e and rs are in (rs + r)*
but e and rs £ r (sr + r)*
Hence, . (rs + r)* * r (sr + r)*
Example 3.3.5 : If s = {aa, b) write all the strings in s* which are having length 4 or less, also
say the following is true or false.
(i) (s +)* = (s*)* (ii) (s +)+ = s + (ill) (s*f = (s +f
Scanned by CamScanner
CamScanner
Theory of Comp. Sei, (MU-Sem, 5-Comp.) 3-14 Regular Expressions and Lan.
Example 3.3.7 : Find a regular expression corresponding to each of the following subsets of
{0, 1}*
(1) The language of all strings containing exactly two 0’s.
(2) The language of all strings containing at least two 0’s.
(3) The language of all strings that do not end with 01 .
Solution :
(1) Strings containing exactly two 0’s
R.E. = 1*01*01*
(2) Strings containing at least two 0’s
R.E. = 1*01*0(1+0)*
(3) Strings not ending i n 01.
(10* 4- 111)*
o
5o. -►-Uli
Scanned by CamScanner
Scanned by CamScanner
Theory of Comp, Sci. (MU-Sem. 5-Comp.) 3-15 Regular Expressions and Languages
e
0
5o
Mi
(q?
E E
o 1
qo)—►
0
0
0 0
Example 3.3.9 : Construct an NFA to accept the language represented by a*b*c*. Convert
the NFA to its equivalent DFA.
Solution :
Step 1 : RE to NFA
a
b‘c‘
Scanned by CamScanner
CamScanner
Theory of Comp. Sei (MU-Som. 5-Comp.) 3-10 Regular Expressions and Langu.
Step 2 : NFAtoDFA o
a,b
I Ja,b,c
ab*a
(aa)
a a
a
6 w
n5 )
He
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-17 Regular Expressions and Languages
Scanned by CamScanner
CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp,) 3-18 Regular Expressions and Larig,
(11+00)
11+00
1/
do
0t
Scanned by CamScanner
V Theory of Comp, Sci. (MU-Sem. 5-Comp.) 3-19 Regular Expressions and Languages
(b) D F A f o r ( l l l + 100)*0
(111+100)
111+100
111+100
100 0
0 0 0,1
MU - Dec. 2009
Solution :
(a) FA for 10 + (0 + 11)0*1
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem, 5-Comp.) 3-20
o
.0
( (10)* + 11 1)*+ o
tE E
£ ,£
(10)*
Scanned by CamScanner
y Theory of Comp. Sci. (Mll-Sem. 5-Comp.) 3-21 Regular Expressions and Languages
00
L-x11
9 J
(01+10) (00+11)* (01+10)
oT
j/ 1/
(9i) q2 — o
t 0, 1\ 1 98
00+11
9a 94
0
1 1
0
9g
Example 3.3.14 : For each of the following regular expression, draw a finite automata
recognizing the corresponding language.
1. (1 + 10 + 110)*0 2. 1(01 + 10)* + 0(11 + 10)*
3. (010 + 00)*(10) 4. 1(1 + 10)* + 10(0 + 01)*
Solution :
1. F. A. f o r ( l + 1 0 + 110)*0
- (1+10+1 10)*0z (1+10+110)
1+10+110
0
0
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-22 Regular Expressions and Langu;
0(11+10)*
(01+10)
(11+10)
01+10
E
E
£
11+10
0.
£
0'S
0
E /V-A
(010+00)*10
Scanned by CamScanner
CamScanner
jy Theory of Comp, Sei, (MU-Sem, 5-Comp.) 3-23 Regular Expressions and Languages
010+00
o8
o o
10(0+01)
(1+10)’
(0+01 )
0
8
E
0
0+01
0
0
Scanned by CamScanner
Theory of Comp. Scl. (MU-Som. 5-Coinp.) Regular Expressions and Langu
Example 3.3.15 : Convert the following R.E. to DFA. (R.E. to NFA with e-moves to DFA}
A * A '
(ab + ba) aa(ab -i- bn)
Solution :
The t'-NFA for (ab 4- ba)* is given by
a a
b
b a b a
8 8
g a 6
—
a
a
b
ci,b
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-25 Regular Expressions and Languages
(i) The set of all strings over {0, 1 } without length two.
£ + ( 0 + 1) + (0 + l ) ( 0 + l ) ( 0 + l ) ( 0 + 1)*
(ii) L = {a n bra I (n + m) is even )
((aa)*ab + bb) (bb)*
(iii) L = {<o e (a, b)* I (number of a’s in id) mod 3 = 0 )
(b + ab*ab*a)*
(iv) L = { a " b ra l n > = 4 , m < = 3 )
aaaaa* [6 + b + bb + bbb]
Example 3.3.19 Construct a n NFA with e-moves for the R E 1 0(0 + 0 1 + 01 1 0)’
M U ■ M a y 2009
Scanned by CamScanner
Theory of Comp. Sci, (MU-Sem. 5-Comp.) 3-26 Regular Expressions and Lan™.
'' —sSfe
Solution :
_io(o+oi+ono))
(0+01+0110)-
1
0+01+0110
0
£
E E E
->
E
i£
0 £
0 £ £
Example 3.3.20 : Construct NFA from following RE and convert into minimized DFAM
(i) (0 + *(1*(00 + 11)
* (iii) (a + b)*aba MU - May 2007, Dec. 2008. Dec. 2009, May 2011
Solution :
(i) ( 0 + ])*(00+ 11)
RE to NFA ( o n > * (00+11)
(0+1)
Scanned by CamScanner
Scanned by CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 3-27 Regular Expressions and Languages
00
11
0,1 0
do
0 0
4
0 ti
rrrs/i
{do>d2 } ) — *—
1
Fig. Ex. 3.3.20(c) : RE to NFA
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-28
NFA to DFA
{q }
<7)0, 1
{Mo)
{ q2 ) 0,1
(a+b) a
a,b
£ a
a b
►— — ►
NFA to DFA
b a
Wo-Mi)
b
Fig. Ex. 3.3.20(0 : NFA to DFA
j i f j j j u !■ ■»»*. I. J . ■ ......... ■ } I u ■ III nil i t.-i-
(a + b)‘ a b a ( a + b)
a,b a,b
a
CamScanner
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-29 Regular Expressions and Languages
a b
MU -May 2014
(00+11)* (10)*
/ e \
6
\
OOz-x \
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-30
After elimination
of state q 1
After elimination
of state q 3
Ao
2.
After re-writing
of a.b, b.a in R E form
%
a ab + ba
Ao
a b
After elimination
Ao
8
R.E = a(ab + ba)
Aa
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-31 Regular Expressions and Languages
R2
The equivalent R.E. for machine shown in Fig. 3.4.4
Example 3.4.1 : Find a regular expression for the given two state generic machine.
b
Solution :
Neither state q 0 nor state qj can be eliminated as both are final states. Effect of the loop
between q and qj can be moved either to state q () or state q .
Moving the effect of loop between q 0 and qi on state q 0 , we get a machine as shown in
Fig. Ex. 3.4.1(a).
The above machine has two final states. The machine can be divided into two machines,
first with q 0 as final state and second with q L as final state. Machine after division is shown in
Fig. Ex. 3.4.1(b).
a + bc*d a + bc*d a + bed
Scanned by CamScanner
Scanned by CamScanner
V Theoiy o f Comp. Sci. (MU-Sem. 5-Comp.) 3-32 Regular Expressions and Lanqu-
b b
a
ib
Solution :
(i) Machine is redrawn after the effect of loop between state 1 and 2 is transferred to
state 1.
a + bb*b
/. R.E. = (a + bb*b)*b*b*
Machine is redrawn after the effect of loop between state 2 and 3 is transferred •
state 2.
( Aa+bb
a ,b
1 n— — ft 2 )
R.E. = e + (a + b) (a + bb)*b + a
I
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-33 Regular Expressions and Languages
Example 3.4.3 : Find R.E. corresponding to T.G. shown in Fig. Ex. 3.4.3.
'-i/X \
M )o
Solution :
0+1
00*(0+1)
q
3.
Scanned by CamScanner
3-34
0
9l q3
93
©
Fig. Ex. 3.4.5(b)
I
Scanned by CamScanner
JTheory of Comp. Sci. (MU-Sem. 5-Comp.) 3-35 Regular Expressions and Languages
Example 3.4.6 : Find a regular expression for the given three state machine shown in
Fig. Ex. 3.4.6.
Ho
f
*fez (fez)
Fig. Ex. 3.4.6(a) : Moving the effect of loops on state qj
d(b+ea*d+fc*g)*f
Example 3.4.7 : Find the regular expression for the DFA’s, shown in Fig, Ex. 3.4.7.
o a
90
0 9l 0
qo.
q2j
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-36 Regular Expressions and Langu
Solution :
(i) Machine is redrawn after the effect of loop between q 0 and q j is transferred to q 0 .
• 0(0+1 n 0+1
—kt- -£
Fig. Ex. 3.4.7(a)
R.E. = (0(0 + 1)*1)*0(0 + 1)*
(ii) Machine is re-drawn after the effect of loop between q 0 and q j is transferred to q0
b+ab _ a ,b
0+11 X—\
( J 0 ’1
KM
1+00
There are two paths from Q1 and q 3 [0 + 1 1], There are two paths from q 2 to q 3 [1 + 00].
R.E. = [0(10)*(0 + 11) + 1(01)* (1 + 00)](0 + 1)*
\ b
a I
Tb
q 2j
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-37 Regular Expressions and Languages
—>{q0 }* a
(qo> q J (q2)
{ q2} { q<i} <l>
a
{qo-qJ* {q<> q J {q 0 . q 2 )
b
( q0. q 2 ) * ( q0 . q j { q 2}
q2 p
Fig. Ex. 3.4.8(a) : An equivalent DFA
Step 3 : Minimization of DFA obtained from Fig. Ex. 3.4.8(a).
Behaviour of { q 0 } and { q 0 , q 2 ) are identical as their a-successors and b-successors are
same. Merging the two states { q 0 } and { q0 , q 2 ) , we get, the minimum state DFA as shown in
Fig. Ex. 3.4.8(b).
{ q 0 , Qi) = -> A
* B C
{ q0 » q i ) = B * B A
{ q2 ) = C A
b
Fig. Ex. 3.4.8(b) : An equivalent minimal DFA
Step 4 : Writing of regular expression for DFA derived from Fig. Ex. 3.4.8(b).
• There are two loops on A.
1. Loop through A -> C -» A on input ba
2. Loop through A -> B -> B -> . . . . -» B -» A on input aa*b
a
3. There are two final states. Q-
A n equivalent machine without a loop is a 41'7
ba
shown in Fig. Ex. 3.4.8(c). r“\
\_>aa‘b
Fig. Ex. 3.4.8(c) : An equivalent machine without a loop
Scanned by CamScanner
V Theory of Comp. Scl. (MU-Sem. 5-Comp.) 3-38 Regular Expressions and Langua(>
Regular expression for die machine shown in Fig. Ex. 3.4.8(c) is given by :
R.E. = {aa*b + ba)* + (aa*b + ba)*aa*
I _ _ _ _) k J
Corresponding Corresponding
to state A to state B
= (aa*b + ba)*[e + aa*]
= (aa*b + ba)*a* . . . [By Equation (3.12)]
= (a*ab + ba)*a* . . . [By Equation (3.10)]
= L.H.S.
Example 3.4.9 : Prove or disprove the following for regular expression r, s and t.
(a) (rs + r)*r = r (sr + r)* (b) s(rs + s)*r = rr*s (rr*s)*r
(c) (r + s)* = r* + s*
Solution :
(a) (rs + r)*r = r(sr + r)*
This can be proved by drawing a F.A. for (rs + r)*r
8
>-
rs
X q1
r/
Ao
Scanned by CamScanner
CamScanner
Regular Expressions and Languages
The regular expression for the above FA is given by r(sr 4- r)* = R.H.S.
Hence, it is proved.
(b) s(rs + s)*r = rr*s (rr*s)*r
L.H.S. is starting with s, whereas R.H.S. starts with r. Hence, disproved.
(c) (r 4- s)* = r* + s*
The expression rs belongs to (r + s)* but it docs not belong to r* 4- s*, hence disproved.
Example 3.4.10 : Find the regular expression corresponding to each of the following subset
of {0,1}*
a) The language of all strings not containing the substring 000.
b) The language of all strings that do not contain the substring 110.
c) The language of all strings containing both 101 and 010 as substring.
Solution :
(a) The language of all strings not containing the substring 000.
R.E. can be written by drawing an equivalent FA and then writing a regular expression
for it.
DFA for strings not containing 000 is given by,
0 o
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Cornp.) 3-40 Regular Expressions and Language
1
I 0 o
0
2
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-41 Regular Expressions andj-anguages
Ho 0
J 0 ’1
33))
/. R.E. = b * + b*aa*
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-42 Regular Expressions and LanguaQ
Solution :
(i) S = (0, 1) odd number of 1’s in strings.
In some cases it is more convenient to draw a DFA for the given language and then an
equivalent regular expression can be written for it.
Step 1 : DFA for the given language
o <~A0
—
1
Fig. Ex. 3.4.13(a) : DFA for strings having odd number of l ’ s
0+01+001
Fig. Ex. 3.4.13(d) : An equivalent Frk for DFA of Fig. Ex. 3.4.13(c)
Scanned by CamScanner
• TheoryofComp Sci. (MU-Sem. 5-Comp.) 3-43 Regular Expressions and Languages
a b
Scanned by CamScanner
Theory of Comp. Sei. (MU-Sem. 5-Comp.) 3-44
he
Solution :
(a) = {co e E* I co has at least one pair of consecutive 0’s }
The equivalent FA is shown in Fig. Ex. 3.4.15(a).
o
—£ Mi
1
Fig. Ex. 3.4.15(b)
The effect of loop between q 0 and q, can be move to q 0 . A new machine is obtained and
shown in Fig. Ex. 3.4.15(c).
<40 Hi
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-45 Regular Expressions and Languages
The effect of loop between qj and q 2 can be removed by moving its effect both on
qj and q2- A new machine is shown in Fig. Ex. 3.4.15(e).
XJ01
10
Cj
Fig. Ex. 3.4.15(e) : FA after elimination of loop
o'
ds
The effect of loop between q0 and q b between q 2 and q 3 are removed and FA is redrawn
as shown in Fig. Ex. 3.4.15(g).
A R.E. = (00)*1(1D*
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-46 Regular Expressions and Languaa
Ao
DFA corresponding to e-NFA of Fig. Ex. 3.4.16(b) is given in Fig. Ex. 3.4.16(b).
{Qo»Ai}
0,1
There are two paths between the start (q 0 , q 1 ) and the final state {q 4 }
1. First path is represented by
R.E. = 1
2. Second path is represented by
R.E. = 0 ( 0 + 10)* 11
The equivalent regular expression for FA is shown in Fig. Ex. 3.4.16(c) is,
R.E. = 0 ( 0 + 10)* 11 + 1
Hence the formula is proved.
Scanned by CamScanner
'Ziff
V Theory of Comp. Sci (MU-Sem. 5-Comp.) 3-47 Regular Expressions and Languages
MU - Dec. 2010
Let P, Q and R be regular expressions on Z. Then, if P does not contain 8, the equation
R = Q + RP ...(3.16)
has a unique solution given by,
R = QP* ...(3.17)
Proof : Let us verify whether QP* is a solution to the equation.
R = Q + RP
On substitution of QP* for R in Equation (3.16),
R = Q + RP = Q + QP*P
= Q*(8 + P*P)
= QP*
Thus the Equation (3.16) is satisfied by R = QP*. We still do not know whether QP* is a
unique solution or not.
To establish uniqueness, we expand
R = Q + RP
= Q + (Q + RP) P
= Q + QP + RP2
= Q + QP + (Q + RP) P 2
= Q + QP + QP2 + RP3
= Q + QP + QP 2 + . . . + Q P ' + RP' + 1
= Q ( e + P + P 2 + . . . + p ' ) + R pi + 1 ...(3.18)
here, i is any arbitrary integer.
Let us take a string (i) G R I length of cd = k.
Substituting k for i in Equation (3.18)
R = Q (e + P + P 2 + . . . + P k) + R pk + l
Since P does not contain e, w is not contained in RPk + 1 as the shortest string generated
by RPk * 1 will have a length of k + 1.
Since co is contained in R it must be contained in Q(e + P + P 2 + . . . + p k)
Conversely, if (0 is contained in QP* then for some integer k it must be in
Q{e + P + P 2 + . . . + P }, and hence in R = Q + RP.
Scanned by CamScanner
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-48 Regular Expressions a
, Let us consider the FA of Fig. 3.4.6. The set of strings accepted by this FA will be
described by a path.
Starting from q 0 and ending q 2.
• A path from q0 to q 2 will be reached through q P
• These strings will end in ‘a’ with a prefix generated during coming to q r
• If the set of strings generated by coming to q! from q 0 is q j and the set of strings
generated by coming to q 2 from q0 is q 2 .
The set q 2 can be written as
q2 = <11* ...(3.19)
Similarly, the state qj can be reached from
(1) q 2 on a,
(2) q t on b,
(3) q 0 on a.
Thus qi can be expressed in terms of q0 and q 2 .
qi = q 0 * + q i b + q 2 a ...(3.20)
Similarly, the state q 0 can be reached from :
(1) q0 on a,
(2) q j o n b ,
(3) Without any input as q0 is a start state.
Thus q 0 can be expressed in terms of q 0 , qj and e.
q0 = q o a + q , b + e ...(3.21)
• These three Equations (3.19, 3.20, 3.21) can be solved with the help of Ardens
theorem.
• The set represented by q 2 will be the solution for the FA of Fig. 3.4.6. q 2 is a fi11
state.
I
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-49 Regular Expressions and Languages
= qo a + q,(b+aa) ...(3.22)
Equation (3.22) is of the form
R = Q + RP with Q = q 0 a,
R - Qi and P = (b + aa)
and its solution is given by,
R = QP*
Thus, q t = q o a ( b + aa)* ...(3.23)
2. Substituting the value of q j from Equation (3.23) into Equation (3.21)
q 0 = q o a + q o a (b + aa)*b + 8
= 8 + q 0 (a + a (b + aa)*b) •. . (3.24)
The Equation (3.24) is of the form
R = Q 4- RP from Q = e, R = q 0 and P = (a + a (b + aa*)*b)
R = QP*
Thus, q 0 = 8 • (a + a (b + aa)*b)*
= (a + a (b + aa)*b)* '• ' . ...(3.25)
3. Substituting the value of q 0 from Equation (3.25) into the Equation (3.23).
qj = (a + a(b + aa)*)*a(b + aa)* ...(3.26)
4. Substituting the value of q! from Equation (3.26), into the Equation (3.19).
q2 = ( a + a(b + aa)*)*a(b + aa)*a •
Thus the R.E. representing the FA of Fig. 3.25 is given by :
R.E. = (a + a(b + aa)*)*a(b + aa)*a
Example 3.4.18 : Prove that the following F.A. accepts strings with equal number of 0’s and
1’s; such that each prefix has at most one more 0 than 1’s or at most one
more 1 than 0’s.
0
Ho
9— ►— (2 0.1
Scanned by CamScanner
V Theory of Comp. Scl. (MU-Sem. 5-Comp.) 3-50 Regular Express!*
Solution : The state q 3 is a dead state. It is not required to equate for state q 3.
<lo = e + <
q, = q o O
q 2 = qo'l ■••(Q18.3)
Substituting values of q , and q 2 from Equation (Q18.2) and (Q18.3) in Equation (Q18.] j
q 0 = e + q o O l + q o lO
= e + q 0 (01 + 10) .-.(Q18.4)
The Equation (QI 8.4) is of the form R = Q + RP with Q = e, R = q 0 and P = (01 + 10)
and its solution is given by R = QP*
.-. q 0 = E (01 + 10)* = (01 + 10)*
Observations :
1. The regular set accepted by the given FA will have equal number of 0’s and l’s as the
R.E. = (01 + 10)* implies 01 or 10 coming zero or more times.
2. The prefix of regular set accepted by the given FA is
(a) The set represented by q j which is (01 + 10)*0. Number of 0’s is one more
than l’s.
(b) The set represented by q 2 which is (01 + 10)*1. Number of l’s is one more
than 0’s.
Example 3.4.19 : Consider the following transition diagram, convert it to an equivalent regular
expression using Arden’s theorem.
Solution : The set of equations for regular sets represented by the states A, B and C are given
below :
A = e + A1 + B1
B = A0 4- CO
C = Bl ...(3)
Rewriting Equation (1),
A = (e + B l ) + A l , it is of the from R = Q + RP with R = A ; Q = (e + B l ) and P = 1
its solution is given by R = QP*
.-.A = (e + B l ) l * . - ( 4)
Scanned by CamScanner
Substituting, tile value of A from Equation (4) and the value of C from
Equation (3) in Equation (2).
B = (e + B l ) l * 0 + B10
= 1*0 + BI 1*0 + BIO
= l*0 + B ( l l * 0 + 10)
From Arden’s theorem,
B = 1*0(11*0+10)*
Substituting the value of B from Equation (5) in Equation (3).
C = 1*0(11*0+ 10)*l
Since state C is a final state, the regular expression represented by C describes the FA
given in the Example 3.4.18.
R.E. = 1*0(11*0 + 10)*l
Example 3.4.20 : Find the regular expression for the set strings recognized by the FA shown
in Fig. Ex. 3.4.20. Use Arden’s theorem.
2
Q,
o
0,1
Fig. Ex. 3.4.20
Solution :
The set of equations for regular sets represented by the state q 0 , q, and q 2 are given
below.
q0 = e + qi l + q 2 (0 + l ) ...(1)
qt = qi 2 + q oO ...(2)
q 2 = OiO + Mol •••(3)
Substituting q 2 from Equation (3) in Equation (1)
q 0 = e + (Jil + ((JiO + q o l ) (0 + 1)
= e + q j + q 100 + q 101 + q o l (0 + 1)
= E + <11(1 + 00 + 01) + q o l (0 + 1)
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-52
Example 3.4.21 : State Arden's theorem and use it to construct a regular expression
_ ____
corresponding to the <following automata ______________yaMU ji 1-- -
t . u _ M-i .Dec.
fw 2010
0
q2
- e + q t 0 + q 2 010 + q 2 00
Scanned by CamScanner
3-53
Now,
5324 1
q 4 = q3 =q 2 oi
= [o + 1 (1 + oi i)* (oioi + oo)]* i ( i + o i i ) * oi
Which is the required RE.
3.5 FA Limitations
Some languages are regular. There are other languages which are not regular. One can
neither express a non-regular language using regular expression nor design finite automata for
it.
Scanned by CamScanner
r Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-54 Regular Expressions and Langiinf[(j
Pumping lemma gives a necessary condition for an input string to belong to a regular se(
Pumping lemma docs not give sufficient condition for a language to be regular.
Pumping lemma should not be used to establish that a given language is regular.
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
placed into less than n holes, some holes have to have more than one pigeon in
Similarly, a string of length > n when recognized by a FA with n states will see some
states repeating.
3.6.1 Definition of Pumping Lemma
MU - Dec. 2007, May 2009, Dec. 2009, May. 2010, Dec. 201?
a
b
do)- - » ( qi ► J— ►— (da) — *— « q 4
q , i s repeating
Input
— a Z b c a\ b c b
do “►di —►cl 3 -►dg —* ( 1i —> q 2 -*q “*q
3 4
States
I
Scanned by CamScanner
Theory of Comp.JSci. (MU-Sem. 5-Comp.) 3-55 Regular Expressions and Languages
As the input abcabcb takes the machine through the loop qi —> q2 “ * Q3 “ > 1°°P
can repeat any number of times. In terms of abcabcb, we can say that if abcabcb is accepted by
FA then every string in a(bca)*bcb will be accepted by the FA of Fig. 3.6.1. The portion bca
is input during the loop.
Thus, if abcabcb is accepted by the FA then abcabcb can be written as xyz, with
x = a
y = bca
z = bcb.
• Length of abcabcb is > n
• xy z for every i > 0 or a(bca)’bcb for every i > 0 will be accepted by the FA of
Fig. 3.6.1.
3.6.3 Proof of Pumping Lemma
MU - Dec. 2007, May 2009, May 2010,
UM— BMay 2012, Dec. 2016
Scanned by CamScanner
V Theory of Comp. Scl. (MU-Sem. 5-Comp.) 3-56 Regular Expressions and Language
9. Since y is a portion relating to loop, it can repeat any number of times. The same js
shown in Fig. 3.6.3.
Mo
10. From the Fig. 3.6.3 it is clear that if xyz e L then x y z will also be accepted by the
machine for every i > 0
3.6.4 Application of Pumping Lemma
• Pumping lemma should be used to prove that given set is not regular.
• Pumping lemma should be applied to a problem in steps as given below.
Step 1 : To prove that L is not regular, we proceed with assumption that L is regular and it is
accepted by a FA with n states.
Step 2 : Now, we choose a string w e L such that I co I > n.
— which co to select is very important and it is the key to solution using
pumping lemma.
Step 3 : co is written as xyz,
With I xy I < n
and Iy I > 0
Now we must find an i such that xy'z g L. This will contradict the assumption that L
is regular and hence we can conclude that L is not regular.
— which i to select such that xy‘z g L is very important.
201 201
Solution :
Step 1 : Let us assume (hat L is regular and L is accepted by a FA with n states
Step 2 : Let us choose a string
n , ti
oj = a b
I co I = 2n > n
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-57 __________Regular Expressions and Languages
Example 3.6.2 : Using pumping lemma for regular sets. Prove that the language.
L == {a m b n I m > n} is not regular. MU - Dec. 2011
Solution
Step 1 : Let us assume that L is regular and L is accepted by a FA with n states.
Step 2 : Let us choose a string w = a pbq such that
p+q > n and p=q+1
Let us write w as xyz with
and I xy I < n
y could take any of the given forms :
1. a 1 . . . . case I
2. a’bj . . . case II
3. bJ . . . case III
Step 3 : We want to find i so that x y z 2 L and we must consider all the three cases.
Case I :
*
We can take i = 0 with xyz = xz = a p " i bq
Since p = q + 1 and i > 0 therefore p - i is not greater than q.
xy°z £ L
Scanned by CamScanner
Theory of Comp. Scl. (MU-Som. 5-Comp.) 3-58 Regular Expressions and Langua
Case II :
x y ' z for i = 2 is given by
p • I i . I I I I I <1 - I
= a a b a b lv
= a p b' a 1 b'1 £ L
Case III :
= a p b q " j + nJ
= a 1' b q 4‘ ( n “
Since p = q + 1. n > 1 and j > I, it is clear that p is not greater than q + (n - l)j and
n
hence xy z e L.
Thus in all the three cases we get a contradiction. Therefore, L is not regular.
Example 3.6.3 : Using pumping lemma for regular sets. Prove that the language
L = {O'l a I i > 0} Is not regular. iWnnMETOwfl
MU . May 2011
Solution :
Step 1 : L e t u s assume that L is regular and L is accepted by a FA with n states.
Step 2 : Let us choose a string co = a ' b 2 n
Icol = 3 n >n
Let u s write w a s xyz, with
ly I > 0
and I xy I < n
Since I xy I < n, y must b e of the form a r I r > 0.
Since I xy I < n, x must be of the form a\
11 n
N o w , co = a b~ can be written as
a r n-s-r. 2n
a a a b
x y z
n + r . 2n
= a b
2i
S i n c e r > 0, a n + ’ b 2n is not of the form a 1 b 2i . Therefore, xy‘z £ L . H e n c e by contraction
we can say that the given language is not regular.
Scanned by CamScanner
y Theory of Comp, Sei, (MU-Sem 5-Comp.) 3-59 Regular Expressions and Languages
Example 3.6.4 : Using pumping lemma for regular sets prove that the language.
L = {0m1 n0 m + n I m £ 1 and n £ 1} is not regular.
Solution :
Step 1 . Let us assume that L is regular and L is accepted by a FA with n states.
Step 2 : Let us choose a string.
n
— n i 4- n
I co I = 2 (m + n ) > n
Let us write co as xyz with
Iy I > 0
and I xy I < n.
Since I xy I < n, y must be of the form 0 r I r > 0
Since I xy I < n, x must be of the form 0 s.
Now,
cd = o n i mo m + n can be wr itten as
os o
x y z
MU - May 2010
Solution :
Step 1 : Let us assume that L is regular and L is accepted by a FA with n states
Step 2 : Let us choose a string
co =
Q)
cd R "*---- t r o m
R
COCO
I co I = 2n + 2 > n
Scanned by CamScanner
v Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-60
ends in (n) a’s. Therefore. xy‘z £ L. Hence by contradiction, we can say that the
given language is not regular.
Example 3.6.6
M U - Dec. 2006, Dec. 2010
Solution
Step 1 : Let us assume that the given language is regular and L is accepted by a FA with a n
states.
Step 2 : Let us choose a string
n n
co = a Kb a ub
(0 co from ano
I cd I = 2n + 2 > n
Let us write co as xyz with
lyl > 0
and I xy I < n
Since I xy I < n, x must be of the form a8.
Since I xy I < n, y must be of the form a’ I r > 0.
Now,
n. n. s r n-s-r. n
a) = a ba b = a a a ba b
x y z
Scanned by CamScanner
V Theory of Comp, Sci. (MU-Sem. 5-Comp.) 3-61 Regular Expressions and Languages
n+r n
=— a« iba ib
Since r > 0, a n + rba n b is not of the form cocoR as the number of a’s in the first half is
n + r and in the second half is n.
Therefore, xy z 2 L. Hence by contradiction, the given language is not regular.
Example 3.6.7 : Use pumping Lemma to show whether or not the following language is
regular : L = {cocoa) I co g {a,b)*}
Solution :
Step 1 : Let us assume that L is regular and L is accepted by a Finite Automata with n states.
Step 2 : Let us chose a string
co = a n ba n ban b
I co I = 3n + 3 > n
Let us write co as xyz, with
lyl > 0
and I xy I < n
Since, I xy I < n, y must be of the form a r I r > 0
Since, I xy I < n, x must be of the form a s.
Now, a n ba°ba n b can be written as
a r n.-8-r n n
a a a ba ba b
Solution
Step 1 : Let us assume that the given language is regular and L is accepted by a FA with n
states.
Step 2 : Let us choose a string co = a p, where p is a prime and p > n.
I co I = I a p I = p > n
Let us write w as xyz with
lyl > 0
and I xy I < n
Scanned by CamScanner
v Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-62
as I y | = | am I = m
Example 3.6.9 : Using pumping lemma for regular sets, prove that the language,
j2
L = {a I i > 1} is not regular.
Solution :
Step 1 : Let us assume that the given language L is regular and L is accepted by a FA with n
states.
2
Step 2 : Let us choose a string co = a n .
I co I = I a n I = n2 > n
Let us write co as xyz with
lyl > 0
and I xy I < n
Step 3 : We will try to prove that xy z is not of the form a by showing that I xy z I lies
between the square of two consecutive natural numbers.
i.e. n2 < I xy 2 z I < (n + I )2
A number lying between the square of two consecutive numbers can never be of the
form i 2 .
Let us find the length xy 2 z
2
I xy z I = I xyz I + I y I
= n + (> 0) as the length of y > 0 and length of xyz is n2.
I xy 2 z I > n2 ...(1)
Again, I xy 2 z I = I xyz I + I y I
Since the length of I y I < n as I xy I < n we can say that
1 xy z I < n b n
2 2
or, I xy z I < n +n + n + 1 [n + 1 is added on the right of the inequality]
or, I xy 2 z I < ( n + 1) 2 '
Scanned by CamScanner
CamScanner
Theoryo[Comp. Sci. (MU-Sem, 5-Comp.) 3-63 Regular Expressions and Languages
Example 3.6.10 : Show that the language L (a"ba n I n > 0) Is not regular.
MU - D e c . .2009; Dec. 2011
Solution :
Step 1 : Let us assume that L is regular and L is accepted by an FA with n states.
Step 2 : Let us choose a string
n, n
co = a ba
Icol = 2n + 1 > n
Let us write co as xyz, with
ly I > 0
and I xy I < n
Since, I xy I < n, y must be of the form a r I r > 0
Since, I xy I < n, x must be of the form as.
Now, a n b n can be written as :
srn-s-r. n
a a a . ba
Step 3 : Let us check whether x y z for i = 0 belongs to L.
xy z = a (a ) a ba
n-r . n
= a ba
Since, r > 0 the string a n ” rban £ L. ...
Hence by contradiction we can say that the given language is not regular.
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-64 Regular Expressions and
do
Example 3.6.13 : Are the following sets regular ? (Using pumping lemma) Prove the same :
MU -May 2007
Solution :
(a) anb im with m, n > 0 is regular. The corresponding FA is shown in Fig. Ex. 3.6.13 :
b
90 Al
b b
2m
If an operation on regular languages generates a regular language then we say that the
class of regular languages is closed under the above operation. Some of the important clostf*
1. Union
2. Difference
Concatenation
Scanned by CamScanner
3-65 Regular Expressions and Languages
Intersection
Complementation
Kleene star
7. Transpose or reversal.
To prove the closure property; we must show that there is another machine M 3 which
accepts every string accepted by either Mj or M 2 and no other string. The construction M 3 is
quite simple as shown in Fig. 3.7.1.
s
0. %
F
m2
8
*— (Ao
M3
Fig. 3.7.1 : M , is constructed such the L(M,) = L(M ( ) u L(M: )
R = S o Q u (r0 )
H = FuG
53
Scanned by CamScanner
CamScanner
Theory of Comp. Sei, (MU-Sem, 5-Comp.) 3-66 Regular Expressions and LangtiaQ
L(M 3) = L ( M , ) U L ( M 2)
Let Mi = ( S , E , 8 b s 0 , F )
To prove that closure property under concatenation, we must show that there is
another machine M 3 such that L(M 3 ) = L(M t ) • L(M 2 ). The construction of M 3 is shown in
Fig. 3.7.2.
s
o. Ao,
M.
q
o
(O
m3
Fig. 3.7.2 : M 3 is constructed such that L(M3 ) = L(M t ) • L(M2 )
Scanned by CamScanner
CamScanner
) 3-67 Regular Expressions and Languages
3.7.3 Regular Language is Closed under Kleene Star
Let M — {Q, S, 8, q0, F) be the given automata. To prove the closure property under
complementation, we must show that there is another machine M which accepts L(M) where
L(M) = L(M) = L*-L(M)
I I
Given Machine after
machine complementation
If M is a deterministic finite automata then M can be constructed by interchanging final
and non final states of M.
/. M = (Q, S, 8, q0, Q - F)
3.7.5 Regular Language Is Closed under Intersection
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-68
Let Lj and L 2 are two regular languages. The difference Lj - L 2 is the set of strings
that are in language Lj but not in L 2 . Construction of a composite automata f Or
L(Mj) - L(M 2) is explained in Chapter 2. Thus regular languages are closed under difference.
3.7.7 Regular Languages are Closed under Reversal
Reversal of a language L is obtained by reversing every string in L. Reversal of a
o
language L is represented by L .
For example,
if L = {aab, abb, aaa), then
Lr = (baa, bba, aaa}
Let M j = (Q, Z, 8, q 0 , F) be the given automata. To prove the closure property under
R
reversal, we must show that there is another machine M 2 which accepts L(Mj) .
or L(M2) = LCMj/
M 2 can be constructed from M | by :
1. By reversing every transition in M,.
2. Start state of Mj is made the only final state.
3. A new start state s0 is added with e-move to every final state of M P
Example 3.7.1 : Construct a FA accepting every string over alphabet {0, 1} ending in 01 or
10. Find the transpose of the machine or create a reverse machine.
Solution :
0
9 l ) -----►
Ao
c
93
9o S
0J
E
93 96
Scanned by CamScanner
1
Lj = { a n bn I n > 1 )
We know from the application of pumping lemma that Lj is not regular. Language Lj is
subset of language described by (a 4- b)*.
Thus a subset of a regular language need not be regular.
(ii) Every regular language has a regular proper subset.
(1) If the language is as empty language then it will have no proper subset. Hence the
above argument is not true for an empty language.
if L = <|> then it has no proper subset
(2) If L is {e) then b is its proper subset. A language L = (|) is a regular language.
(3) For any other language, let us consider a word ODj 6 L where 0)j is given by aj
a 2 . . . a n , we can always construct a finite automata recognizing a language Lj
where Lj = (tOj}. Thus the language has a proper subset.
Example 3.7.3 : For each statement below, decide whether it is true or false, if it is true,
prove it, all parts refer to language over the alphabet {a, b}
(a) if L1 c L2 and L1 is not regular, then L2 is not regular
(b) if L1 c L2 and L2 is not regular, then L1 is not regular.
(c) if Lt and L2 are non regular then L1 u L2 is non regular.
Solution :
Let us assume that following languages.
L a = { a n b n I n > 1 ) , L a is not regular.
L b = { m l 0 J G {a, b)*) L b is regular. .
L c = { a b ) , Lc is regular.
(a) If Lj c L 2 and Lj is not regular, then L2 is not regular.
Answer = False.
L a c L b , L a is not regular but L b is regular.
(b) If Lj c L 2 and L 2 is not regular, then L, is not regular.
Answer = False.
L c c L a , L s is not regular but Lc is regular.
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-70
-s—
e
Thus, L* is regular
Example 3.7.5 : Let L b e a language. It is clear from the definition that L + c L*. Under what
circumstances are they equal ?
Solution :
Let us assume that L is a language with e belonging to it.
L+ = L L * = (e + L,)L*
where L! is a language obtained from L after deleting e from it.
L+ = e L * + L 1 L* = L* + L l L*
= L*, as L[ L* G L*
+
Thus, L will be equal to L* if e is a member of L.
________________________ ______________________________________________________________ _____
Example 3.7.6 : Are the following True or False. Support your answer with proofs or counts
examples.
(i) If LL,1 u L 2 is regular and L 1t is regular then L 2 is regulai
regular.
(ii) If L i • L 2 is regular and L , is regular then L 2 is regular.
(iii) If L* is regular, then L is regular.
Scanned by CamScanner
Regular Expressions and Languages
Solution :
(i) If Lj u L 2 is regular and L ( is regular then L 2 is regular.
— We can always construct a combined
F A f o r M ( L 1 u L 2 ) - - M ( L 1)
M(L 2) is same as M(L, u L 2) - M ( L j )
L 2 is regular (True)
(ii) If Lj • L 2 is regular and Lj is regular then L 2 is regular.
- True [If L 2 is not regular then L ( ■ L 2 can not be regular]
(iii) If*L* is regular, then L is regular.
-True
Existence ot an FA tor L* will trivially imply existence of an FA for L.
•• F = { Fn )
The set of states Q = { q 0, q b ..., q n }
The Finite Automate M T accepting L T can be derived from the given Finite Automata M.
Steps for the same are given below :
1. q n will become the start state in M .
2. q 0 will become the final state in M T .
3. For every transition 8(q,,a) = qj (where a g S ) , a transition 8(q ? a) = q t is added to
mt.
• It is clear that M T is FA and the language of MT must be regular.
• If co is accepted by M, i.e. 8* (q 0 ,co) = q n then coT will be accepted by M T by tracing the
reverse path. Therefore, 8*(q n,co) = q 0.
There are a number of decision problems related to finite automata and regular
languages. We have algorithms to answer various questions concerning regular languages.
A problem that has a decision algorithm is called decidable. A decision algorithm
terminates with yes or no answer. Some ol the decision problems are given below :
1. Is a regular set empty ? - Emptiness property.
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-72 Regular Expressions and Language
Scanned by CamScanner
Theory of Comp. Sei. (MU-Sem. 5-Comp.) 3-73 Regular Expressions and Languages
3.10 Applications of RE ; -t •
3.10.1 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 for a + b
2. [0 - 9] Stand for a digit from 0 to 9
3. [A - Z] Stands for an upper-case letter
4. [a-z] Stands for a lower-case letter
5. [0 - 9A-Za - z] Stands for a letter or a digit.
The grep utility in UNIX, scans a file for the occurrence of a pattern and displays those
lines in which the given pattern is found.
For example :
$ grep president emp.txt
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-74
It will list those lines from the file cmp.txt which has the pattern “president”. The
pattern in grep command can be specified using regular expression.
6. * matches zero or more occurrences of previous character.
7. • matches a single character.
8. [ A pqr] Matches a single character which is not a p, q or r.
A
9. pat Matches pattern pat at the beginning of a line
10. pat $ Matches pattern at end of line.
Example :
(a) The regular expression [aA] g [ar] [ar] wal stands for either “Agarwal” or ‘agrawal”.
(b) g* stands for zero or more occurrences of g.
(c) $grep “A • ♦ thakur” emp.txt will look for a pattern starting with A. and ending with
thakur in the file emp.txt.
• Lexical analysis is an important 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
Keywords, identifiers and operators are common examples of tokens.
The UNIX utility lex can be used for writing of a lexical analysis program. Input to kx
is a set of regular expressions for each type of token and output of lex is a C program for
lexical analysis.
Dec, 2005
Q. 1 Explain how can pumping lemma be used to test if a language is regular or not.
(Section 3.6) (1o Marks)
Q. 2 Write short note on Myhill-Nerode theorem. (Section 3.9) (5 Marks)
Scanned by CamScanner
CamScanner
V Theory Of Comp. Sci.(MU-Sem.5-Comp.) 3-75
Regular Expressions and Languages
May 2006
Q. 3 Convert the following regular expression to NFA with e transitions.
Q. 4 Show that
(1 + 00*1) + (1 + 00*1) (0 + 10*1)*(0 + 1.0*1) = 0*1(0 + 10*1)*
(Example 3.3.3) (4 Marks)
Q. 8 Using pumping lemma prove that the given language is not regular.
(i) L, = {coco I co € {0, 1}* ) (Example 3.6.6)
(ii) L 2 = {O’l' I i > 1} (Example 3.6.1) (10 Marks)
(Note : Take ‘a’ as *0’ and ‘b’ as T).
Scanned by CamScanner
▼ ' Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-76 Regular Expressions and Langua y.
Q. 15 State and prove pumping lemma for regular languages and prove that L pr consisting Of
all strings of 1’s whose length is prime is not regular.
(Sections 3.6, 3.6.1 and 3.6.3, Similar to Example 3.6.8) (10 Marks)
May 2008
Dec. 2008
May 2009 -
Q. 21 Construct an NFA with e-moves for the regular expression 10(0 + 01 + 0110)*
(Example 3.3.19) (6 Marks)
Q. 22 State the pumping lemma for regular language. (Sections 3.6, 3.6.1 , 3.6.3) (5 Marks)
Dec. 2009
Q. 23 Design Finite Automata from 1(01 1)*0 RE. (Similar to Example 3.3.13) (5 Marks)
Q. 25 State and explain pumping lemma for regular languages. Using pumping lemma, show
that the following languages are not regular. (Sections 3.6, 3.6.1 and 3.6.2)
(i) L = {1n I n is prime number) (Example 3.6.8)
(ii) L = {0 n 10n I n > 0) (Example 3.6.10) (10 Marks)
(Note : Take ‘0’ as ‘a’ and ‘1’ as ‘b’)
Q. 26 Explain decision properties for regular languages. (Section 3.8) (10 Marks)
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-77 Regular Expressions and Languages
MiitWO
Q. 27 Obtain a regular expression such that L (R) = { co I coR e { 0, 1 }* ) with atleast three
consecutive zeros. (Example 3.6.5) <5 M a r k s )
Q. 30 Using Pumping Lemma theorem, show that following grammars are not regular: -
(i) L = { a n bn I n > 0 } (Example 3.6.1 ) (5 Marks)
(II) L = { cocoR I co e (0,1) * } (Example 3.1.5) (5 Marks)
—•OU-
Fig. Q. 32
(10 Marks)
Q. 33 Using pumping lemma prove that the following languages are not regular
(i) L1 = (coco I co e {0,1)*} (Example 3.6.6) (5 Marks)
(II) L2 = { 0‘l‘ 11 > = 1} (Example 3.6.1) (5 Marks)
Q. 34 List and explain decision properties of regular languages, Explain the test for checking
emptiness of a regular language. (Section 3.8) (1 0 Marks)
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-78 Regular Expressions and Lan™.
Q. 35 Give and explain the formal statement of Pumping Lemma for regular languages and
use it to prove that the following language is not regular
L = a n b 2n I n> 0} (Similar to Example 3.6.3) 0 0 Marks)
Q . 38 Using pumping Lemma, show that following grammars are not regular : (10 Marks)
(i) L = {an ba n I n > 0} (Example 3.6.10)
(ii) L = {o' 1 j I i £ j } (Similar to Example 3.6.2}
May 2012
Q. 39 State and prove the Pumping Lemma for Regular Language. (5 Marks)
(Section 3.6.3)
May 2013
Q. 44 Give the formal definition of R E and design a DFA corresponding to the regular
expression.
(a + b)* aba (a + b)* (Section 3.3) (5 Marks)
Ans. :
35
The language associated with the R.E. (a + b)*aba(a + b)* = strings with “aba
substring.
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-79 Regular Expressions and Languages
b
Fig. Q. 44
Q.47 What is Myhill-Nerode theorem ? Explain necessary of it. (Section 3.9) (5 Marks)
Q. 48 Using pumping lemma prove that the following language is regular or not
L = {a n b n | n > = 1} (Example 3.6.1) (5 Marks)
May 2014
Q. 51 Give and explain the formal statement of Pumping Lemma for regular languages and
use it to prove that the following language is not regular :
L = {aV | rt > = 1}
(Section 3.6/1 and Similar to Example 3.6 J ) (10 Marks)
Scanned by CamScanner
Theory of Comp. Sci. (Mll-Sem. 5-Comp.) 3-80 R e g u l a r Expressions and Lanqi
(10)*
1 0
E 8
Fig. 1-Q. 54
G -closure of states
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-81 Regular Expressionsand Languages
Fig. 2-Q. 54
p . Dec. 2015 kj
Q. 60 State and explain pumping lemma for regular languages. Using pumping lemma prove
that the language L = (0T | n 0} Is not regular.
(Sections 3.6 to 3.6.3, Example 3.6.1) (10 Marks)
Scanned by CamScanner
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 3-82
State diagram
0
•0
Scanned by CamScanner
▼ Theory of Comp. Sei, (MU-Sem. 5-Comp.) 3-83 Regular Expressions and Languages
a,b
a
▼ o-
w
a/
r /b
.96 b
9q
Q. 65 Define Regular Expression. Design R.E. for strings ending In consecutive 1’s over
E = {0, 1). (Section 3.3) (5 Marks)
Q. 66 Give and explain formal definition of Pumping Lemma for Regular Language and
prove that following language is not regular.
L = { a n b n | n >= 1 } (Section 3.6.1 and Example 3.6.1) (10 Marks)
Scanned by CamScanner
Theory of Comp. Sei. (MU-Sem. 5-Comp.) .3-84 Regular Expressions andLang lp
Chapter Ends.-
□□0
Scanned by CamScanner
Regular Grammar (RG)
Sms?*
Regular Grammar
The language accepted by finite automata can be described using a set of productions
known as regular grammar. The productions of a regular grammar are of the following form :
A —> a
A e
1 f> ! 1
Where, a e T a n d A , B e V. •• ' - .
A language generated by a regular grammar is known as regular language.
A regular grammar could be written in two forms : - \
1. Right-linear form ‘J. t •-
2. Left-linear form. . . ...? . .
Left-linear form :
A left linear regular grammar will have productions of the following form :
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 4-2 Regular Gramm.
Solution
Transition system for (ba)* is given by :
We can write left linear grammar and the right linear grammar form the transition
systems.
Right linear grammar :
, ' * . , . * I I /
S —> baS I £
Left linear grammar :
S -» Sba I £
Every DFA can be described using a set of production using the following steps :
Let the DFA, M = (Q, £, 5, q 0 , F)
Where, A, B, C, D eV
3. Creating a set of productions P.
3.1 If q e F then add a production
S -» £ to P.
3.2 For every transition of the form,
0 —.—Q
a _
Scanned by CamScanner
4-3 Regular Grammar (RG)
AO Hl
—♦
Solution :
Step 1 : Renaming of states, we get the DFA shown in Fig. Ex. 4.2. 1 (a).
IS! A)
where,
The set of variables V = (S, A, B, C}
The set of terminals T = ( a , b }
The start symbol = S
Scanned by CamScanner
Scanned by CamScanner
Theory, of Comp. Sci. (MU-Sem. 5-Comp.) 4-4 Regular G
Example 4JL2 : Give a right linear grammar for the DFA shown in Fig. Ex. 4.2.2.
o
o
0,1
Fig. Ex. 4.2.2
Solution :
Step 1 : State D is a dead state and it can be removed. The FA after deletion of dead state D
is shown in Fig. Ex. 4.2.2(a).
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-5 Regular Grammar (RG)
Solution :
Re-writing the production we get
S —>bB
B - ■» bC I b
B —»aB
C —> a
Step 1 : Adding transitions corresponding to every production, we get
a.b
Scanned by CamScanner
1
Step 3 : States {S}, {A}, {B}, {C,F}, and {A, F ) are renamed as q 0 , q p q 2 , q and a dead
state q 4 is introduced to handle <(> - transitions. The resulting DFA is shown in
Fig. Ex.4.2.4(c) :
Scanned by CamScanner
V Theory of CompjSci. (MU-Sem. 5 . Comp-) 4 .7
Regular Grammar (RG)
May 2009
Solution :
A new final state F is being introduced to handle productions like,
Step 3 : A dead state is added to handle ( transition. The resulting DFA is shown in
a
a b
b
g)a,b
a.b
b
b
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-8
Following- steps are required to write a left linear grammar corresponding to a DFA.
Interchange starting state and the final state.
2. Reverse the direction of all the transitions.
Write the grammar from the transition graph in left-linear form.
Example 4.3.1 : Give a left linear grammar for the DFA shown in Fig. Ex. 4.3.1 .
a
a
Fig. Ex. 4.3.1
Solution :
Step 1 : Interchanging the starting state and the final state and reversing the direction of
• ’I - • '
b b b f b
a
S Ba I Ab, A —> Sb I Ca I a
B S a l Cb I b, C —> Bb I Aa.
Scanned by CamScanner
Theory of Comp, Scl. (MU-Sem. 5-Comp.) 4-9 Regular Grammar (RG)
Example 4.3.2 : Give a left linear grammar for the DFA shown in Fig. Ex. 4.3.2.
a
Solution : '
Step 1 : The given DFA contains three final states S A, and B. We can draw an equivalent
DFA with a single final state D by adding e - transitions from S to D, A to D and
B to D.
a
a x
tS b
Step 2 : Interchanging the starting state and- the final state and reversing direction of
transitions, we get • /
b
a
S
tS b
Scanned by CamScanner
y Theory of Comp. Scl. (MU-Sem. 5-Compj___4 J 0
Every left linear grammar can be represented using an equivalent DFA. Following steps
are required to draw a DFA for a given left linear grammar.
• 1. Draw a transition graph from the given left linear grammar.
2. Reverse the direction of all the transitions.
3. Interchange starting state and the final state.
4. Carry out conversion from FA to DFA.
Example 4.3.3 : Construct DFA accepting the regular language generated by the left linear
grammar given below.
S -> Ca I Bb
C->Bb
B -> Ba I b
Solution :
Step 1 : Draw a transition graph from the given left linear grammar.
[0
Scanned by CamScanner
Theory qfComp. Sci. (MU-Sem. 5-Comp.) 4-11 Regular Grammar (RG)
Step 2 : Reverse the direction of transitions and interchange starting state and the final state.
b /b
__ / a,b
------a dead state Is is
.---------------------added
added to handle
a
»b <|> - transitions
Example 4.3.4 : Construct DFA accepting the language generated by the left linear
grammar given below.
S -> B1 I AO I CO
B - » B 1 11
A—»A1 I B1 I CO IO
C —» AO
Solution :
Step 1 : Draw a transition graph from the given left linear grammar
o
0 0
0
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-12 Regular Grammy
Step 2 : Reverse the direction of transitions and jnterchange starting state and the final state
0
o o
o
o
o
o
o
o
o,1
f
Fig. Ex. 4.3.4(c) : DFA
Scanned by CamScanner
y Theory of Comp. Scl. (MU-Sem. 5-Comp.) 4-13 Regular Grammar (RG)
Every right linear grammar can be represented by an equivalent left linear grammar.
Conversion process involves drawing of an intermediate transition graph. Following steps are
required :
1. Represent the right linear grammar using a transition graph. Mark the final state as
■ ®
2. Interchange the start state and the final state.
3. Reverse the direction of all transitions.
4. Write left linear grammar from the transitions graph.
Example 4.4.1 : Convert the following right linear grammar to an equivalent left-linear
grammar.
<n co co O co
-» bB I b
aB
Solution:
Step 1 : Conversion of right linear grammar to transition system.
— ♦— Ccj
Step 2 : Interchanging the start state with the final state and receiving direction of transitions,
we get :
b
a
i
Step 3 : Writing of left linear grammar from the transition system, we get :
S —> b I Bb I Ca
B —» B a l b
C ->Bb
Scanned by CamScanner
Theory of Comp. Scl. {MU-Sem. 5-Comp.) 4-14 Regular Gram.
Example 4.4.2 : Write an equivalent left linear grammar from the given right linear gramm
S->0AI1B
A-»0CI 1AI0
1B 11A I 1
C -> 0 I OA
Solution :
Step 1 : Transition system for the given right linear grammar is as shown ;
Fig. Ex. 4.4.2(a).
0
B’
Step 2 : Interchanging the start state with the final state and reversing direction of transitions,
we get . ... i ' / • .
o
o
o
6
Step 3 : Writing of left linear grammar from the transition system, we get :
S -> CO I AO I B l
A -> A l I CO I B l 10
B —> B l 1 1
C —> AO.
—----- — ____________________________________ __
Example 4.4.3 : For right linear grammar given below, obtain an equivalent left linear
grammar. r . ... , , .
S 10A I 01
A -» 00A I 1
Scanned by CamScanner
V JFheory_of_Comp. Sei, (MU-Sem. 5-Comp.) 4-15 Regular Grammar (RG)
Solution :
Step 1 ; Transition system from the given right linear grammar is as shown in
Fig. Ex. 4.4.3(a).
00
10
S.
01 v1
Step 2 : Interchanging the start state with the final state and reversing direction of transitions,
01
Step 3 : Writing of left linear grammar from the transition system, we get
S - > A l I 01
A -> A00 I 10
Every left linear grammar can be represented by an equivalent right linear grammar.
The conversion process involves drawing of an intermediate transition graph. Following steps
are required.
1. Represent the left linear grammar using a transition graph. Mark the final state as ©
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-16 Regular Grammars
Solution :
Step 1 : Transition system for the left - linear grammar is as shown in Fig. Ex. 4.5.1(a):
o
Step 2 : Interchanging the start state and the final state and changing direction of all
transitions, we get :
0 0 >>
ml • ■+-— /Sy -----♦—
1
Fig. Ex. 4.5.1(b)
Step 3 : A right linear grammar can be written from the transition system. A set of
productions is given below ;
S -»1BIOA
A ->0CI 1 A I 0 , ,
B —> IB I 1AI 1
C -»OCIO <
Scanned by CamScanner
TheoryofComp. Sci. (MU-Sem. 5-Comp.) 4-17 Regular Grammar (RG)
Example 4.5.2 : Construct the right linear grammar corresponding to the regular expression
R=(0+1)1*(1 +(01)*)
Solution :
Step 1 ; The given expression can be represented using a transition system as shown in
Fig. Ex. 4.5.2.
0+1
T) — ——
BT
s- 01
Fig. Ex. 4.5.2
Step 2 : Writing of right linear grammar from the transition system, we get :
S —> 0A I 1A, A —> 1AI 1 I B, B —>01B I £
The £ - transition. B —> £, makes both A and B nullable. The.£-transitions, B —» £ is
removed and the resulting productions are given below.
S —>0AI 1 A 1 01 b ■■ .
A —> 1A I 1
B - OlBIOl '
Example 4.5.3 : Write an equivalent right recursive grammar for the given left > recursive
grammar : S -> S 1 0 I 0
Solution :
Step 1 : Transition system for the left-linear grammar is shown in Fig. Ex. 4.5.3(a) :
10
o
Step 2 : Interchanging the start state and the final state and changing direction of all
transitions, we get :
Step 3 : A right recursive grammar can be written from the transition system. A set of
productions is given below :
S —> OX I 0
• X ->10X110
Scanned by CamScanner
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 4-18 Regular Grammars
1
......
Example 4.5.4 : Construct the right linear grammar corresponding to the regular expresS jOn
Solution :
The R.E. ( 1 + (01)*) l * ( 0 + 1) can be represented using a transition system as shown
Fig. Ex. 4.5.4.
1+(01)
e £
01
Fig. Ex. 4.5.4
Writing of right linear grammar from the transition system, we get :
S —> 1BIA
A ->01AIB
B 1BI0CI1CI0I1
Removing unit productions S—>A and A—»B :
S —> 1BI01AI1BI0CI1CI0I1
A —> 01AI1BI0CI1CI0I1
B —> 1BI0CI1CI0I1
Example 4.5.5 : Draw NFA accepting the language generated by grammar with
productions :
S -> abA I bB I aba
A —> b I aB I bA
B -> aB I aA
Solution :
Step 1 : Transitions system corresponding to S —> abA I bB I aba is given by
ab
aba
A new state F Is added
as a final state
Scanned by CamScanner
Scanned by CamScanner
Sei* (MU-Sem. 5-Comp.) 4-19 Regular Grammar (RG)
Step 2 : Transitions for A -> b I aB I bA and B —> aB I aA are added to the transition graph
A)s
ab
tv a
s )------*"----- <3
> aba
Step 3 : Transitions on ab and aba are expanded with resulting NFA as shown in
Fig. Ex. 4.5.5(c).
'A
'CT
B
b
F.
Example 4.5.6 : Construct right linear and left linear grammar for the language
L = {an bm , n > 2, m £ 3}
Solution :
Step! : ADFAforL = { a n bm I n > 2 , m > 3} is shown in Fig.Ex. 4.5.6(a) :
a b b
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-20 Regular Grammar .
Step 3 : Left linear grammar can be written after making the following modification in
Step 4 : Left linear grammar can be written from the transition graph.
A —> Ab I Eb
E—>Db
D—>Cb
C —> Ca 1 Ba ...
B —> a . <'
Example 4.5.7 Construct left linear and right linear grammar for the language
0*(1 (0 + 1))* .
Solution : '\ .
' ■ ; •; .. ' f
Stepl : We will first draw a transition diagram from the given regular expression.
1
.
0,1
(expanding 1(0+1)]
Step 2 : A right linear grammar can be written from the transition graph.
S —> 0 S I A I e
A -> I B
B —> 0A 1 1A 10 1 1
Removing the unit production, S —> A, the resulting grammar is given below :
S —> OS I I B I e
A-> IB
B —> 0A I 1A I 0 1 1
CamScanner
Scanned by CamScanner
Regular Grammar (RG)
Step 3 • Left linear grammar can be written after making the following modifications :
1. Interchange start state and the final state
2. Reverse direction of transitions.
The resulting transition diagram with above modifications is shown in Fig. Ex. 4.5.7(b).
0,1
Step 4 : A left linear grammar can be written from the transition diagram
S —> B 0 I B 1 I A I e
B — SI
A AOIO
Removing the unit production, S —> A, the resulting grammar is given below.
S —> BO I B l I AO I 0 I E
B -+S1
A —> A 0 I 0
Example 4.5.8 Construct left linear and right linear - grammar for the language
(0+1)*00(0 + 1)*
Solution : .. • ‘ ‘
Stepl : We will first draw a transition diagram from the given regular expression.
0+1
oo
Step 2 : Right linear grammar, from the transition graph can be written as given below :
S ->OSI1SIOA
A —> OB 1 0 Right linear grammar
i 1
. i , ' ’ j .>•.
B -> OB I I B 1 0 1 1
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-22
Step 3 : Left linear grammar can be written after making the following modifications :
1. Interchange start state and the final state
2. Reverse direction of transitions.
Resulting transition diagram with above modifications is shown in Fig. Ex. 4.5.8(b) •
&■' 0 o
Step 4 : A left linear grammar can be written from the transition graph. It is given below :
S —> SO I S I I AO
A —> BO I 0
B —>BOIB1 1 1 1 0
MU - May 2009
Solution :
The given expression can be represented using a transition system as shown below :
oo
(01+10)
e E
E
00
oo
01+10 01,10
E £
E e
00 00
Scanned by CamScanner
CamScanner
TheoryofComp. Sci. (MU-Sem. 5-Comp.) 4-23 Regular Grammar (RG)
01,10
,10 01,1
oo
<Joooo
Fig. Ex. 45.9(b)
oo
oo
Fig. Ex. 4.5.9(c)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-24
Interpretation of language
.Step 1 : We will first draw a transition diagram from the given regular grammar.
Scanned by CamScanner
y Theory of Comp. ScLjMU-Sem. 5-Comp.) 4-25 Regular Grammar (RG)
Language . A string of length 0 or more ending in either ab or b’s but never containing
aba as a substring.
Example 4.5.11 . Describe the language generated by the following grammar.
S aA I bC I b
A -> aS I bB
B —> aC I bA I a
C -» aB I bS
Solution :
A language can be understood easily from a regular expression or finite automata.
Step 1 : We will first draw a transition diagram from the given regular grammar.
s A B
f’
Language generated : From the transition diagram, it clear that any path from S to C will
involve :
1. Even number of a’s ' 1 i;. ’
2. Odd number of b’s
Thus the language is, ...
L={tt)G{a, b}*lco contains even number of a’s and odd umber of b’s}.
Example 4.5.12 : Describe the language generated by each of these grammar and justify
your answer with the example string derive from the grammar of the
productions given below.
’ (1) S -> aSalbsblaAblbAa
A -» aAalbAblalble s
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-26
Solution :
(1) It generates a string in which there is an ‘a’ and if it is rewritten as ‘b’ then the string
will become a palindrome as shown in Fig. Ex. 4.5.12(a).
s
a S a
b S b
a A b
A
a A a
A
b A b
I .
e
Fig. Ex. 45.12(a)
The string abaabbabba is in L(G). If the third character ‘a’ is made b then the modified
string abbabbabba is a palindrome.
(2) S -> bTlaTle
A—»aslbs “"‘dzf
The FA representing the grammar is shown in a,b
Fig. Ex. 4.5. 12(b). ' Fig. Ex. 4.5.12(b)
Thus the grammar generates the following language :
L(G) = (cog{a,b)* I Icol is even)
Example 4.5.13 : Find the CFL associated with CFG
S - > 0 Q 11P
P —» 0 I OS I 1PP
Q-»1 I1SI0QQ
Solution :
This is a case of indirect recursive. S is related back to itself through P or Q.
s
0Q 1P
10S
OOQQ 11PP
Scanned by CamScanner
5-Comp.) 4-27 Regular Grammar (RG)
Q.1 Construct right linear grammar and left linear grammar for the language (ba)*.
Q. 2 Find the equivalent DFA accepting the regular language defined by the right linear
grammar given as :
S -> aAl bB
A -> a A I bC I a
B aBIb
C bB (Example 4.2.5) (7 Marks)
Q. 3 Construct left linear and right linear grammar for the regular expression.
(((01 + 10)*11)*00)*. (Example 4.5.9) (6 Marks)
Chapter Ends...
□ □□
Scanned by CamScanner
Context Free
Grammars (CFG)
Module 3
• We have seen that every finite automata M accepts a language L, which is represented
by L(M).
• We have seen that a regular language can be described by a regular expression.
• We have seen that there are several languages which are not regular.
1. L l = {ap l p i s a prime} is not regular.
2. L2 = { a n2bn I n > 0} is not regular.
i . ;
3. L3 = {a I i > 1 } is not regular.
.4. L 4 = {wwl w e {a, b}*) is not regular.
5. L5 = {ww r I w g {a, b}*) is not regular.
• We have seen two ways for representing a language :
1. Using finite automata.
2. Using a regular expression.
If a language is non-regular, it cannot be represented either using a FA or using
a regular expression. Hence there was a need for representing such languages.
Grammar is another approach for representing a language.
1. In this approach, a language is represented using a set of equations.
2. Equations are recursive in nature.
a set of terminals.
Scanned by CamScanner
Theory.of Comp. SciJMU-Sem. 5-Comp.) 5-2 Context Free Grammars (CFG)
■HM 1
sSFat ............................................
Equivalent
No. - " . ..... .. • ■
grammar
1. X —> a
2. X—>aX
cr
X-»b
3. © X —> aX
X—> e
Scanned by CamScanner
CamScanner
” Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-3 Context Free Grammars (cp.
Now, let us consider the grammar given below and see if it can
generated by the DFA of Fig. 5.1.2.
•••(4-2)
First by writing X as aX and then by replacing X on the right hand side with an e.
X as aX X
X as e —► s
Fig. 5.1.3
First by writing X as aX, secondly by replacing X on RHS with aX and finally replacing
X on RHS with E.
X
X as aX a
X
X as aX a
X
X as e ■■■► g
Fig. 5.1.4
• Thus the language generated by the set of productions {X -> aX, X -> e} is
L = {e, a, aa, ...}
L = {On l n l n > 1)
Scanned by CamScanner
▼ Theory of Comp. Sci. (MU-Sem. 5-Cornp.) 5-4 Context Free Grammars (CFG)
We know from pumping lemma that the above language is not regular; but this language
can be represented using a set of productions. It will be worthwhile to understand the recursive
nature of the above language.
X
I I
a a n--1 b r>-1 b
I |
X
A
a X b Writing X as aXb
A
a X b Writing X as aXb
b Writing X as ab
3 3
Fig. 5.1.5 : Generation of a b
Scanned by CamScanner
CamScanner
eo ofComp SciJM Sem. 5-Comp.) 5-5 Context Free Grammars
T is a set of terminals.
P is a set of productions.
S is a special variable called the start symbol S e V.
A production is of the form
V, —> Oj where Vj e V and oq is a string of terminals and variables.
5.2.1 Notations
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-6 Context Free Grammars (CFG)
Several productions of <noun> and <verb> can be merged together and the set of
productions can be re-written as :
P = { <sentence> —> <noun> <verb>,
<noun> —> Mohan I Soham 1 Ram,
<verb> —> eats I plays I reads
j ■.
If the production rule is applied once, then we write a => p. When it is applied a number
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 5-7 Context Free Grammars
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-8
Scanned by CamScanner
_jrheory of Comp, Sci, (MU-Sem. 5-Comp.) 5-9
Solution :
(i) Parse tree for leftmost derivation of 1001.
B
e 0 B
e 0 B
0 B
(d)B->0B
S
o
0 B
o
o
o
(g) S -> A 1 B (h) B -> 0B (i) B —> 0B
Fig. Ex. 5.2.2
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-10 Context Free Grammars (CFG)
UJ
£
(j) B - > I B (k)B-»e Fig. (fl: A - > e
Fig. Ex. 5.2.2
Rightmost derivation of 1001 is shown by a series of Figs. Ex. 5.2.2(g) to (Z).
It may be noted that the final parse trees in Fig. Ex. 5.2.2(f) and (Z) are identical.
A
0 s 1 *— [Production S -> 0S1J
01
Fig. Ex. 5.2.3 : Derivation of 000111
Solution :
0) Leftmost derivation :
Leftmost derivation of aabbaa is being shown with the help of the parse tree.
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-1 1
CD
CD
T
CD
co
cd
(U
t
4
/l\
s
a A
S —► aAS
A SbA b a
00
A -+ ba
aj
T
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-12 Context Free Grammars (CFG)
E
A + T
I IT F
A I'
T * F b
( E )
T F
F b
a
Fig. Ex. 5.2.5 : Parse tree for (a + b) ♦ a 4- b
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-13 Context Free Grammars (Cfq.
Solution :
- Dec. 2Q
u
0
Leftmost derivation : Rightmost derivation :
S —> OB [Production S —> OB] [Production S —> OB)
—» OOBB [Production B —> 0 B B ] —» OOBB [Production B —» OBB]
—»OO1B [Production B —> 1] —> 0 0 B l S [Production B —> IS]
-> 0011S [Production B —> I S ] —>00B10B [Production S -» OB]
-» 0 0 1 1 OB[Production S -> OB] —>00B101S [Production B -> IS]
—> 0 0 1 101S[Production B —> I S ] —> 00B1010B [Production S —> OB]
-> 0 0 1 1010 B [Production S -> 0B] -» 00B 10101 [Production B —» 1]
0 0 1 1 0 101[Production B - » 1] —> 00110101 [Production B —» 1]
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp ) 5-14 Context Free Grammars (CFG)
i c t s
Scanned by CamScanner
1
Parse tree : s
e
Fig. 5.2.1 : Parse tree for aaaa
a S
S—>aS [Using production S —> aS]
aaS [Using production S aS] aS
—> aaaS [Using production S -» aS]
a
aaaa [Using production S aS] $
a
Fig. 5.2.2 : Parse tree for
(3) Language L = { b, ab, aab, aaab, . . . }
The productions required to generate the above languages are :
S —> aS
S -4b
The production S -> aS can generate one or more a’s. The production, S -» b is f°f
termination of recursion and thus generating zero or more a’s followed by ab.
Example : Generation of the string aaab.
Sentential form :
S —> aS [Using production S —» aS]
—> aaS [Using production S —> aS]
Scanned by CamScanner
Theory of Comp, Sei, (MU-Seni 5-Comp.) 5-16 Context Free Grammars (CFG)
a S
a S
a S
b
Fig. 5.2.3 : Parse tree for aaab
The DFA M =
Scanned by CamScanner
V Theory of Comp. Sci, (MU-Sem. 5-Comp.) 5-17
Parse tree :
s
a s
b S
b S
a S
Scanned by CamScanner
CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5 . 18
Parse tree :
s
a S b
a S b
a S b
8
'3-3
Fig. 5.2.5 : Parse tree for aaabbb or a' b
a S b
a S b
a b
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp .) 5-19 Context Free Grammars
abababa — a palindrome
— a palindrome derived
after removing first and
last b's
Sentential form :
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem, 5-Comp ) 5-20 Context Free Grammars (CFG)
parse tree : s
a
AS a
b
AS b
a
AS a
b
Fig. 5.2.8 : Parse tree for abababa
a
AS a
b
AS b
a
AS a
b b '•
Fig. 5,2.9 : Derivation tree for ababbaba . .f ;
Scanned by CamScanner
CamScanner
Theory of Comp. Scl. (MU-Som. 5-Comp.) 5-21
1
Fig. Ex. 5.2.7
Leftmost derivation :
S —> OB -» OOBB -> 001B -> 001 IS —> 001 10B —> 001 101S -> 001 1010B -» 001 10101
Rightmost derivation :
S —> OB —> OOBB -> OOBIS (X)B10B -> 00B101S —> 00B1010B -> 00B10101
001 10101
5.2.3. 1 Union Rule for Grammar
If a language L, is generated by a grammar with start symbol S t and L 2 is generated by 3
grammar with start symbol S 2 then the union of the languages L t u L2 can be generated wid1
start symbol S, where
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-22 Context Free Grammars (CFG|
S Si I S 2
Example : Let the languages L j and L 2 are given as below :
L
i = { an l n > 0 ]
U = { bn I n > 0 }
Productions for L j are :
51 —> aS I a
Productions for L 2 are :
5 2 —» bS I b
Then the productions for L = Lj o L 2 can be written as :
S -»SjlS 2
S j —» aS I a
S 2 —> bS I b
Example 5.2.8 : Give a context free grammar for the following language.
0(0+1)*01(0 + 1)*1 ‘
Solution :
The ( 0 + 1 ) * can be generated by the following productions.
S! - > O S j l 1 S j l e
Scanned by CamScanner
CamScanner
V Theory of Comp, Scl, (MU-Soin. 5-Comp.) 5-23 Context Freo Grammars (CFQ)
Using the product rule, we can write a set of productions for the language
0 ( 0 + l)*01(0 + 1)*1
P= ( S-+0S,01S l l
S , —> (>S,I lS|le
)
Where,
Set of variables V = (S, S | )
Set of terminals T = ( 0 , 1 )
Start symbol = S
Example 5.2.9 : Construct the context free grammar corresponding to the regular
expression.
R = (0 + 1)1*(1 +(01)*)
Solution :
Grammar for (0 + 1) is given by
Sj —> 0 I 1 [union rule]
Grammar for 1* is given by
52 — y 1S2I8
Grammar for (1 + (01)*) is given by
5 3 —> 1 I S 4 where S 4 stands for (01)*
5 4 —> 0 1S 4 1 £
R = ( 0 + 1)1*(1 +(01)*)
P = { S - » S 1S 2S 3
S] —> 0 I 1
52 —> 1S21 £
5 3 -> I I s 4
5 4 —> 01S4 1e
)
where,
Set of variables V = (S h S 2, S 3 , S 4 )
Set of terminals T = (0, 1)
Start symbol = S
Scanned by CamScanner
Theor ofComp ci, (MU-Sem. 5-Comp.) 5-24
1
Example 5.2.10 : Give the CFG for L = {a'b , i <> j <. 2i, i > 1}
Solution :
equal equal
number of number of
a’s and c’s b’s and c's
A string of the form ac‘ can be generated with the help of the following productions :
S —> aSc
S ->X
Recursion is terminated by the production S —> X, where X gives a string of the
form bV.
X — bXc
X -> be
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-25
Thus, the set of productions for the above language are given by :
P = { S —» aSc I X
X —> bXc I be
P a r s e tree :
a S c ----
b X c
b c
(1) = {a'bJ c k I i = j + k}
(2) L2 = {a1 b1 c k I j = i + k}
(3) L3 = {a‘b1 c k I i = j or j = k)
Solution :
(1) Lj = {a'bj c k I i = j 4- k}
Outer a’s should be matched with c’s then the remaining a’s should be matched with b’s-
S —> aSc I aXc
X -> aXb I ab
j k
(2) L 2 = {a'b c I j = i + k}
Scanned by CamScanner
CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-26 Context Free Grammars (CFG)
a’s and b’s arc equal B’s and c’s are equal
(generated by X) (generated by Y)
S —» XY
X —> aXb I ab
Y —> bYc I be
(3) L3 = { a ‘ bj c k I i = j or j = k ]
S —> A B [i=j]
S —> CD [j = k]
Example 5.2.13 : Give the context free grammar for the following languages.
(a) (011 + 1 ) * (01)*
(b) O' i' + k 0 k where i, k >= 0
Solution :
(a) CFG for (011 + 1 ) * (01)*:
)
where,
The set of variable V = ( S , X , Y )
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-27 Context Free Grammars
X Y
Let us assume that 0T is generated by the variable X.
X = 0X1 le
Let us assume that l k0 k is generated by the variable Y.
Y = 1 Ylle
The set of productions P is given by :
P = { S->XY
X—>0Xlle
Y —» lYOIe
}
Where,
The set of variable V = { S, X, Y )
The set of terminals T = {0, 1 }
The start symbol = S
S —> XY
X —> aXblab [X generates a* bl and
Y
Y -> cY Ic generates c j ]
S —» XY | X generates a1 and
X -> aXIa Y generates bJJ ]
Y -» bYc Ibc
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 5-28 Context Free Grammars (CFG)
Side ways 0)
Thus, the set of protections for the above language are given by :
P = {S —> (S) I SS I e)
Where, Set of variables V = {S}
Set of terminals T = ((,)}
Start symbol = S
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-29 Context Free Grammars (cp
■n ■
Example 5 . Z 1 7 : Give CFG for all strings with at least two O’s.E = {0, 1}
/ W>EE
MU ■ Dec.
Solution :
The regular expression for the above language is given by :
RE = 1*01*0(0+1)*
Let us assume that 1* is generated by the variable X.
/. X -> I X I e
Let us assume that (0 + 1)* is generated by the variable Y.
Y —> 0Y I 1Y I e
Using the concatenation rule, the set of productions for 1*01*0(0 + 1)* can be written as :
P = { S —> X0X0Y
X - > IX I e
Y —> 0Y I I Y I e
}
Where, Set of variables V = {S, X, Y}
Set of terminals T = {0,1}
Start symbol = S.
Example 5.2.18 : Give CFG for set of odd length strings in {0, 1}* with middle symbol T.
Solution :
A string generated by the above language could be shown as given below :
String of length n from {0, 1 ) * t 1 f String of length n from {0, 1 }* |
The hidden recursion can be understood from the fact that deletion of first and last
character will not change the basic nature of the string.
i.e. If S is a string of the given form then S are :
OSO, 0S1, ISO, 1S1.
Thus, the set of productions for the above language is given by :
P = {
S->OSOIOS1 I ISO I LSI I I
Example 5.2.19 : Give CFG for set of even length strings in {a, b, c d)* with two midd|0
symbol equal. ’ J
Scanned by CamScanner
5-Comp.) 5-30 Context FreeGramrnar CFG)
Solution :
A stting generated by the above language could be shown as given below :
aa
bb
String of length n from {a, b, c, d ) * String of length n from {a, b, c, d}
1
' ' Ldd
The hidden recursion can be understood from the fact that deletion of first and last
character will not change the basic nature of the string.
i.e., if S is a string of the given form then S are :
aSa, aSb, aSc, aSd, bSa, bSb, bSc, bSd, cSa, cSb, cSc, cSd, dSa, dSb, dSc, dSd.
Thus, the set of productions for the above language is given by :
P = {S —» aSa I aSb laSc I aSd I
bSa I bSb I bSc 1 bSd I
cSa I cSb I cSc I cSd I
dSa I dSb I dSc I dSd I
aa I bb I cc l d d
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-31 Context Free Grammars
Z(say)
n n
Where, b c is generated by Z (say)
Z = bZc I E
Thus the set of productions for the above language are given by :
P ={ S —» aSd I X I Y
X -» aXc I Z
Y bYd I Z
Z bZc I £
Solution : The production S —» aSc can generate equal number of a’s and b’s.
Thus the set of productions for the given language is :
P ={ S -> aSc I X
X -> bX I b
The production S —> X, terminates the recursion. The variable X can venerate one of
more b’s. °
where, Set of variables V = (S, X)
Set of terminals T = {a, b)
Start symbol = S.
E x a m p l e 5.2.22 : Give CFG for L = {x I x contains equal number of a’s and b’s}.
Solution :
Let us assume that there are three variables S, A and B.
where,
S generates a string with equal number of a’s and b’s.
A generates a string in which number ol a’s = 1 4- number of b’s.
Scanned by CamScanner
V Theory of Comp, Sei, (MU-Sem. 5-Comp.) 5-32 Context Free Grammars (CFG)
b a
Scanned by CamScanner
1
v Theoty of Comp. Sei. (MU-Sem. 5-Comp.) 5-33 Context Free Grai
} .
Where, Set of variables V = {S, B}
Set of terminals T = {a,b).
Start symbol = S.
A -> aA 18 . i-
B -> bB I 8
} ■ ■ '■
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5 -34 Context Free Grammars (CFG)
B ° = cB I e o
Now, we can write productions for the given language L using the rule of union.
L = L1 U L2
P = {
S-*SjlS 2
S ] - >AB
A —♦ aAb I e
B -> cB I e
s2 -*xyb
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-35 Context Free Grammars
X aX I e
Y -> bYc I e
}
where, The set of variables V = (S, S h S 2 , A, B, X, Y}
The set of terminals T = {a, b }
The start symbol = S.
L=|0 1 0 f j 0 1 00 f
/ Y X
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-36 Context Free Grammars (CFG|
Example 5.2.30 . Given the context free grammar for the following languages :
(a) L = {a n b2n | n > 1} (b) L = {a " b " ,n > m}
Solution :
L = { a n b2n l n > 1}
S —» aSbb I aabbbb
(b) L = {am b n I n > m}
S -> aSb I B
B bB I b
Example 5.2.31 : Give CFG for the following language : The set of odd length strings in
{a, b} whose first, middle and last symbols are all same.
Solution : A string belonging to the language will have any of the two forms, given below.
Form 1 . a a string of length n over {a, b}* a a string of length n over {a, b}* a
I I -------------------------------- ---------I L— _ _ _________:_______I I
Generated by X
Form 2 : b a string of length n over {a, b}* b a string of length n over {a, b}* b
I | | | ; I 1
Generated by Y
Generated by X
Scanned by CamScanner
V Theory of Comp. Scl. MU-Som Corr2j__jjZ,
The h i d d e n recursion in X c a n be understood from the fact that deletion of first and]
character w i l l not change t h e b a s i c nature o f X .
A X i s a string o f the g i v e n form then X arc :
X —» aXa I aXb I bXa I bXb
Thus, the set of productions for t h e above l a n g u a g e i s given by,
P = ( S -> Xb
X -» aXa I aXb I bXa I bXb I a
)
The production X —» a, terminates the recursion with a as the middle character of X.
Set of variables V = { S , X )
Set of terminals T = { a , b }
Start symbol = S
(iii) The set of all string over alphabet {a, b) with exactly twice as many a’s
as b’s.
S e t o f variables V = ( S , X )
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem.5-Comp.) 5-38 Context Free Grammars (CFG)
• The variable X generates a string having equal number of a’s and b’s. Group of excess
a’s over b’s are generated by S -productions.
Where
Set of variables V = { S,X,A,B }
Set of terminals T = {a,bj
Start symbol = S
• • •I <
Scanned by CamScanner
1
Theory of C o m p . Sci. ( M U - S e m . 5 - C o m p . ) 5-39
X —> bX I aY
Example 5.2.36: Ol
'' < V; J ’C
MU - May 2010
Solution :
Productions for the required language are as follows.
Scanned by CamScanner
CamScanner
Theory,of Comp, Sci. (MU-Sem. 5-Comp.)
5-40 Context Free Grammars (CFG)
A
E + E
' A
E + E
LU --------- tG
LU --------- XI
+
CC
b a
(a) : (b)
Fig. 5.3.1 : Parse trees considered for ambiguity
• The grammar 5.8, generates a + b + a in two different ways. The two derivations are
shown in Fig. 5.3.1(a) and (b).
• The first derivation (Fig. 5.3.1(a)) says that (b + a) is evaluated first and then the
evaluated value is added to a. Thus the right side + operator gets a precedence over the
left side + operator. The expression a + b + a is treated as (a + (b + a)).
• The second derivation (Fig. 5.3.1(b)) says that a + b is evaluated first and then the
evaluated value is added to a. Thus the left side + operator will get precedence over right
side + operator. The expression a + b + a is treated as ((a + b) + a).
Removing ambiguity : There is no general rule for removing ambiguity from CFG.
Removing ambiguity from a grammar involves rewriting of grammar so that there is only one
derivation tree for every string belonging to L(G) i.e., language generated by grammar G.
Ambiguity from the grammar
E —> E + E l a l b _ .
can be removed by strictly assigning higher precedence to left side + operator over ri ht
side + operator. This will mean evaluation of an expression of the form a + b + a„ from left to
right. This property can be incorporated in the grammar itself by suitably modifying the
grammar.
• • Parse tree of Fig. 5.3.1 (b) is based on left to right evaluation.
• Left to right evaluation in Grammar (5.8) can be enforced by introducing one more
variable T. Variable T cannot be broken by + operator.
• An unambiguous grammar for the Grammar (5.8) is given in Grammar (5.9)
Scanned by CamScanner
Theory of Comp, Sci. (MU-Sem. 5-Comp.) 5-41 Context Free Grammars (ec
E + T
a
Fig. 5.3.2 : A parse tree for a + b + a using an unambiguous grammar
* * i i • . « * • * u-.«
In general,
1. Sometimes we can remove ambiguity from a grammar by hand.
2. There is no algorithm for either deletion of ambiguity or for removing ambiguity.
3. Some context free languages are inherently ambiguous. They have only
ambiguous CFG. . „ .
An example of inherently ambiguous language is
Where,
Lj = {anb mcmd n 1 n, m > 0} ;
D, = {anbnc mdm I n, m > 0}
Example 5.3.1 : Consider the grammar
E E + E I E * E I (E) 11
I —> a I b
a) Show that the grammar is ambiguous.
b) Remove ambiguity.
Solution : There are two problems in the given grammar
a) Both + operator and * operator are at the same precedence level.
b) An expression can be evaluated either left to right or right to left.
These problems can be removed by introducing more variables, each representing
expressions that share a level of “binding strength.”
• A variable T is an expression that cannot be broken by + operator. Let us call it a term
A term can be expressed as product of one or more factors.
• A variable F is a factor that cannot be broken by + operator or * operator. A factor is
(a) Identifiers
(b) Literals
(c) Parenthesized expressions
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-42 Context Free Grammars (CFG)
A parse tree for a + b * a using both ambiguous and unambiguous grammar is shown in
E + E E ♦ E E ♦
I
A
E ♦ E
A I
E + E I
A
E + E
a a
Fig. Ex. 5.3.1(a) and Ex. 5.3.1(b) are using the ambiguous grammar.
Fig. Ex. 5.3.1(c) is using an unambiguous grammar.
The grammar given in the example is an ambiguous grammar as there are two different
parse trees for the expression a 4- b * a (shown in Fig. 5.22(a) and 5.22(b)).
Example 5.3.2 : Give an ambiguous grammar for if-then-else statement and then re-write an
equivalent unambiguous grammar.
Solution :
The dangling else is a well known problem in programming language. If statement in
C-language can be written in any of the two forms .
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-43
We can substitute the condition x > y with C.and S for a = a + 1. With the$
substitutions, if statement in two forms can be written as :
For the case of understanding, a reserve word ‘then’ has been introduced.
A nested if statement can be written with two interpretations. It is shown in
Fig. Ex. 5.3.2.
f Ci then If C 2 then S , else s 2
b i > a i .
if C i then
{
if C 2 then if C 2 then
S
else } 1
j s2. else
S
2
____________
Fig. Ex. 5.3.2 : Two interpretations of a nested if-statement
Scanned by CamScanner
Scanned by CamScanner
jj* Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5 -44 Context Free Grammars (CFG)
I C t S e S
/AJ i C t S a
c
2 a , a c2 a
(a) (b) ,
Fig. Ex. 5.3.3 : Two derivations for icxtic2 taea
Removing ambiguity : The ambiguity from the grammar (5.11) can be removed by
binding dangling else with inner if - statement. We will introduce two more variables.
U —> For if - statement without else part.
M —> For if - statement with else part.
An unambiguous grammar is given below :
. . . ■'
(Grammar 5.12)
The working of grammar (5.12) can be understood with the help of derivation tree for
icjti taea. It is shown in Fig. Ex. 5.3.3(c).
- s
/K
i C t
Ct m
Cg a a
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-45
Solution :
Let us consider derivation of the string (( ))( ). Two derivations are shown in
Fig. Ex. 5.3.4(a) and 5.3.4(b).
S s •
co
(b)
Fig. Ex. 5.3.4 : Two different derivations for (( ))( ).
Since, there are two different derivation for the string (( ))( ) the grammar is ambiguous.
An unambiguous grammar is given by
S —> S T I T
T ->(S)IO. .
Sideways parentheses can be generated by S —> ST, where T is atomic as far as sideways
generation is concerned. It forces left to right generation of balanced parenthesis.
Nesting of parenthesis is generated by T —> (S).
Solution :
An unambiguous grammar is given below.
E E + T IT [+ has lowest priority with L —> R associativity]
T —> T * F I T / F I F [* and / has higher priority over + with L —> R associativity]
A
F -» F G IG [ A has higher priority over * and I with L —> R associativity]
G —> - H I H [unary - has the highest priority]
H —» a I b I c I d I e I f I (E) [to handle brackets and identifiers]
A
Derivation tree for (a + b) d / e + (- f)
Scanned by CamScanner
CamScanner
5-46
T / F
A
F G H ( E )
G H e
( E ) . G
E + T H
F (5
G H
II '
H b
a
Fig. Ex. 5.3.5 : Derivation tree for (a + b) * d / e + (- 0
The given CFG is ambiguous as we can have two different derivation trees for the string
a
h. Derivation trees are shown in Fig. Ex. 5.3.6.
Scanned by CamScanner
Context Free Grammars (cp
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-47
S S
a a b
b
(a) (b)
Fig. Ex. 5.3.6 : Two derivations for ab
s
CO
C0
CO
s
0)
(a) (b)
Fig. Ex. 5.3.7 : Two derivations for a 7
Scanned by CamScanner
Scanned by CamScanner
Theory of Comp. Sci.£MU-Sem, 5-CompQ 5 -4 8
Context Free Grammars (CFG}
Solution :
(i) Two parse trees for aab are given in Fig. Ex. 5.3.8.
era*
fis
00
J*
tn
w
o’
3
□
(ii) The set of productions resemble productions for if-statement. Thus the ambiguity can be
removed in a similar way through introduction of two variables U and M with resulting
productions is :
S —> U I M
U -> aS I aMbU
M —» aMbM I e
It will be worthwhile to draw the parse tree and from the parse tree, one can easily write
kft most and right most derivation.
Left most derivation :
S ->aB -> aaBB -» aaaBBB -> aaabBB
aaabbB -> aaabbaBB -» aaabbabB -> aaabbabbS
aaabbabbbA —> aaabbabbba
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-49
a B
A A
a B B a B B
b b b b s
a
Fig. Ex. 5.3.9
(iv) The grammar is ambiguous as we can draw two parse trees for aababb :
S
S
a B B
b
I A
b a B B
b b
b
(a) (b)
Fig. Ex. 5.3.9
Scanned by CamScanner
5X===========2L= §22 £2 pJ__ 2= : ===== 22 L= ========
(ii) Rightmost derivation
S -» bA -» bbAA -> bbAaS -> bbAaaB
bbAaabS -> bbAaabbA -> bbAaabbaS
-» bbAaabbabA -» bbAaabbaba -> bbaaabbaba
(iii) Parse tree for bbaaabbaba
b' A
Scanned by CamScanner
CamScanner
(iii) Parse tree S
o' B
0'»
/w
B B
0'
s
0'
1
Fig. 5.3.9(d)
Example 5.3.10 : In each case, show that the grammar is ambiguous, and find the equivalent
unambiguous grammar
(a) S-> SS I a I b . (b) S-> ABA, A -» aA I s, B bB I e
(c) S-» aSb I aaSb I e
Solution:
(a) S-»SS I a I b is ambiguous
Two parse trees can be generated for the string aab.
S
s s S
S S a s s
a b
Unambiguous grammar :
S-> aS I bS l aa I ab I ba I bb
(b) s—>ABA, A —>aA le, B->bBle
Two parse trees can be generated for the string aa
s .
x s
/ \
A B A /V\
/ \ A B A
... A II]
| a e a
a
Fig. Ex. 5.3.10(b)
Scanned by CamScanner
CamScanner
Theory of Comp, Sei, (MU-Sem. 5-Comp.) 5-52 Context Free Grammars (CFG)
Unambiguous grammar :
S —> aS I bXe [String of only a’s will be generated by S- aS I e]
X —> bX I y
Y -> aY I e
(c) S—>aSb I aaSb I e is ambiguous
We can draw two production trees for aaabb
S S
S b a a S b
A
a a S b a S b
F - E E - F
F b
b a i
Scanned by CamScanner
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 5-53
(b) The production E —> F — E, makes the evaluation from right to left which is normally
allowed.
Thus the ambiguity can be removed by deleting the product the production E —> p
The resulting unambiguous grammar is :
E —> E - F I F
F -> a l b
Example 5.3.12 : Test whether the following grammars are ambiguous. "
1. S -> 0S1S 1 1S0S I e
■ 2. S -» AA .
. A-»aAblbAale
Solution :
1. S->OS1SI ISOS I e
The given grammar is ambiguous as we can have two different parse trees for the
string 0101.
s s
; 1
0 S 1 S O’ S • 1- ' s
s u o i o i b u S
£ E £ c
(a) (b)
Fig. Ex. 5.3.12
2. S —> AA . '*"•
A —» aAb I bAa I e - »». ■ , , . .. •
The given grammar is ambiguous. We can derive two different parse trees for the
string abab.
A
y
A • /l\ &
aAb
e £
e
(d)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-54 Context Free Grammars (CFG)
A grammar written in a simple form is easy to analyze. Certain restrictions are imposed
on simplified grammar. Simplification of CFG involves transforming CFG into an equivalent
form that satisfies certain restrictions on its form. A CFG can be simplified by eliminating :
Useless symbols. 2. e - productions.
3. Unit productions.
5.4.1 Elimination of Useless Symbols
A grammar may contain symbols and productions which are not useful for derivation of
strings. Two types of symbols are useless :
1. Non - generating symbols. - .
2. Non reachable symbol. *•
5.4.1. 1 Non-generating Symbols
There are two rules for finding a set of generating symbols for the given grammar,
1. Every symbol in T (terminal) is generating.
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-55 Context Free Grammars (C j
Two symbols A and C are added to the set of generating symbols using the Rule 2,
which says if there is a production of the form A —> a and every symbol in a is generating
then A is generating.
Step 3 : Symbol S is generating as there is a production S —» CA with both C and A as
generating.
-— *&a,b,A,C h— »C a,b,A t C;S|B
. C -4 b
, Set of generating
symbols
Thus the symbol B is a non-generating symbol as it does not belong to the set of generating
symbols.
Grammar in Example 5.4.1 can be simplified by deleting productions with the useless
symbol B. Simplified grammar is given below :
S ->CA
A —> a
C -»b
Example 5.4.2 : Eliminate non-generating symbols from the grammar.
1. P = {S -» aAa, A -> Sb I bCC, C -> abb, E -> aC}
2. P = {S -> aAa, A -> Sb I bCC I DaA, C -> abb I DD, E -> aC, D -» aDA)
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Som. 5-Comp.) 5-56 Context Free Grammars (CFG)
Solution :
Same as the
abb
bCC
aC
abb
non-
Non-reachable symbols can be located with the help of a dependently graph. A variable
X is said to be dependent on S if there is a production
S - * a t x “2 (s)— <------
z
The dependency graph is shown in Fig. 5.4.1. S-> a 1 xa2 '-
Fig. 5.4.1 : Depending Graph
Scanned by CamScanner
Theory of Comp. Sci. (Mll-Sem. 5-Comp.) 5-57 Context Free Grammars (CEg)
A-»Sb
"" ~~~~ bCC ✓—■s. E —> aC
-------» — ■ QAj -------► yCy -------------kHz
-
S -> aAa .
• From the production S —> aAa, A is dependent on S. It is shown using a directed edge
from S to A.
• From the production A —» bCC, C is dependent on A. From the production A —> Sb, S is
dependent of A.
• From the production E —> aC, C is dependent on E.
There is no path from S to E, E is non-reachable.
i/ Step 2 : Simplification of grammar :
Grammar can be simplified by deleting every production containing the non-reachable
symbol E. Simplified grammar is given below :
Pj = {S—»aAa, A—> S b l b C C , C - » a b b }
A, D and E
are non-
reachable
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Som. 5-Comp.) 5-58 _____________ Context Free Grammars_(CFG)
A —>bBB
(s) ----->-----(a) ----♦----(b) is non- A - > bBB
©
3. S - » a S | A B Every Same as the
\ V
©
A->bA; symbol is original
(©)
grammar
0 /\
B-»AA reachable
//
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-59 Context Free Grammars (CFG)
ramm
written
Productions derived
and nullable as
► e. S is null making one more
>ol on the symbols null.
Scanned by CamScanner
V Theory of Comp. Sei, (MU-Sem. 5-Comp.) 5-60 Context Free Grammars (CFG)
S - > ABA
f S,A,B )
S->B
B -» bB I b I £
A -> aAA I e
B -> bBB I e
Scanned by CamScanner
CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 5-61
Solution :
Step 1 : Find nullable variables
S - > AB
f S,A,B
A aAA can be written as A —> aA r By making one or more nullable symbols as null.
. 'S-»ABIAIB
p> A —> aAA I aA I a "
. B -> bBB I bB I b ,
Scanned by CamScanner
CamScanner
____________ Context Free Grarnmars_(CFG2
The piocedure lor finding G 1 (an equivalent grammar without unit productions) is as
follows :
• The technique is based on expansion of unit production until it disappears. This
technique works in most of the cases. This technique does not work if there is a cycle of
unit productions such as
Aj -> A 2 , A 2 A 3 , A 3 -> A 4 and A4 -> A!
* The steps for elimination of unit production are as follows :
Step 1 : Add all non-unit production of G to G P
Step 2 : Locate every pair of variables (Aj , Ajj such that Aj => Aj.
’' G
Step 3 : From pairs constructed in step 3, we can construct a chain like
Ai~> A 2 .. . . Aj —> a, where Aj —> oc is a non unit production.
Each variable A, to Aj will derive a.
Example 5.4.7 : Eliminate unit productions from
P= { S -> ABA I BA I AA 1 AB I A I B
A -> aA I a
B -> bB I b
Step 2 : Locate every pair of variables (Aj , Aj> such that Aj => Aj
■ G •
Productions
. J •. B —» bB 1 b
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-63 Context Free Grammars (CEq
Productions
Pair (S, A) S — aA 1 a
Pair (S, B) S -> bB 1 b
Thus,
' S - > AB A 1 BA 1 AA 1 AB 1 aA 1 a 1 bB 1 b "
>
Pj = ’ A —> aA 1 a
IB bB l b
P1 _ J F - » ( ET*) F
T
L
< I -» a I b I la I lb 110 I I I ,
*
Step 2 : Locate every pair of variables (A, , A such that Aj => Aj
Scanned by CamScanner
CamScanner
2? Theory of Comp. Sei. (MU-Sem. 5-Comp.) 5-64 Context Free Grammars (CFG)
Productions
Pair (E, T) E —> T*F
Pair (T, F) T->(E)
Pair (F, I) F—> a l b l l a l i b HO I I I
Pair (E, F) E->(E)
Pair (E, I) E —> a 1 b 1 la 1 lb 110 III
Pair (T, I) T—> a l b l l a l i b HO III
Thus, the final grammar is given by
r
E —> E + T I T*F I (E) I a I b I la I lb 110 I I P
J T —» T*F I (E) I a I b I la I lb 110 I II I
| F (E) I a I b I la I lb 110 1 II
<I a I b I la I lb 110 I I I
Example 5.4.9 : Simplify the following grammar
S -> ASB I e
A -> aAS l a
B —> SbS I A I bb
Solution : Following sequence should be followed to simplify a grammar.
1. Eliminate to E - productions from G and obtain G P
2. Eliminate unit productions from Gj and obtain G 2.
3. Eliminate useless symbols from G 2 and obtain G3 .
Step 1 : Eliminate E - productions from G and obtain G P
B — bb
______B - » b b ________________
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-65
A derives aAS I aA I b
All the three variables S, A and B are both reachable and generating. Hence the grammar
G 2 does not have useless symbols.
S -> ASB I AB
— G2 - A —» aAS I aA I a "
B -> SbS I Sb I bS I b I bb I aAS I aA I b .
B-»S IA
Solution :
AS
Step 1 : Eliminate E-production from G
(given grammar) and obtain G P Set A -> C, C is nullable and hence A
Scanned by CamScanner
CamScanner
jTheory of Comp. Sci. (MU-Sem. 5-Comp.) 5-66 Context Free Grammars (CFG)
and S —> B —» S
• ’ i • * ' * ♦1
S ->OAOIOOI1B1 I 11 IBB ;
A —> 0A0 1 00 I 1 B 1 I 1 1 I B B
B —> 0A0 I 00 I 1B1 I 1 1 I B B <-
Solution : F
Following sequence should be followed to simplify a grammar :
1. Eliminate E-productions from G and obtain G P
2. Eliminate unit productions from G j and obtain G 2 .
3. Eliminate useless symbols from G 2 and obtain G 3 .
Step 1 : Eliminate E productions from G and obtain G h
Grammar does not have E-productions.
/. Gj = G
Scanned
Scanned by
by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-67
Step 2.1 : Add all non unit productions of the given grammar G j to productions
P 2 of G 2 .
P2
*
Step 2.2 : Locate every pair of variables (Aj , Aj} such that Aj => Aj
Gi
p2 = s
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem, 5-Comp.) 5-68 Context Free Grammars (CFG)
Two symbols {S, A} are reachable. Other symbols {B, C, D, E ) are non-reachable.
Final grammar G 3 with productions P 3 is obtained by deleting useless symbols.
P3
a,b,d,c,S,A
CamScanner
Scanned by CamScanner
V Theory of Comp. Scl. (MU-Sem, 5-Comp.) 5-69 Context Free Grama
• »
1. Eliminate e-productions, unit productions and useless symbols from the grammar.
2. Every variable deriving a string of length 2 or more should consist only of variables.
i.e. every production of the form A —> a with I a I > 2, a should consists only of
variables. • ■
Example : Consider a production A —> V 1V 2 aV 3 bV4 .
CamScanner
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-70 Context Free Grammars (CFG)
Example : Consider a production A -> X , X 2 . . ,X„ where n > 3 and as X / s are variables.
A —> X j C |
cL x2c2
Cn-2 —> X n _ i X n
each with two variables on the right.
P2 -b
!b —> b
1 Corresponding to S —> C a AC b B
■2 Q3
B
C a —> a
Scanned
Scanned by
by CamScanner
CamScanner
Theory of Comp. Sci. (Mll-Sem. 5-Comp.) 5-71 Context Free Grammars (CFG)
M U - Dec. 2015
Solution :
Step 1 : Simplify the grammar
Step 1.1 : Eliminate E productions Nullable symbols = {P, Q , S }
P and Q are null able as P —> E and Q —> £. S is also nullable as S —> PQP with every
symbol on the right as nullable.
Eliminating £-productions, we get a set of productions Pi
[ S -> PQP I PQ I QP I P I Q ]
P j = S P -> OP I 0 f
IQ->1QI1 J
Step 1.2 : Eliminate unit productions :
There are two unit productions S —> P and S —> Q.
Eliminating unit productions from P 1? we get a set of productions P 2
'S PQP I PQ I QP I OP I 0 I I Q I 1 I PP ?
P SP OP I 0 ►
2=
.Q>IQ I1 >
Step 2 : Every symbol in a, in a production of the form A —> a with I a I > 2 should be a
variable.
This can be done by adding two productions
Co - 0
- C j —> 1
S -» PQP I PQ I QP I PP I C 0 P I 0 I C j Q I 1
P -> CqP I 0
S - >PQP S-»PC 2
C 2 r-> QP
S-»PQ S->PQ
S-»QP S—>QP
S->PP S->PP
S - » C 0P S -> C 0 P
Scanned by CamScanner
- - - -—
Original product ion Equivalent productions in CNF
s —> 0
S->0
S->C,Q S —> CjQ
S-» 1 S-> 1
P -» C n P 10 P -» C0 P 10
Q- CjQI 1 Q->C,QI1
C o —> 0 _______Cq 0 _____________
C,->1 Cj-»1 _____
Solution : The given grammar is not in CNF. Following productions are not allowed in CNF.
S bA I aB
A bAA I aS
B —> aBB I bS
Scanned by CamScanner
CamScanner
5-73
Step 3 :
cb b _________ c b —> b
B -» C b C 2 1C b S I b
C 2 — BB
a
b
Example 5.5.4 : Design a CNF grammar for the set of strings of balanced parenthesis.
Solution :
A context free grammar for balanced parentheses is given by
P = S->SS
Pl = I s —» s s
Conversion of Pj in CNF can be done in two steps.
Step 1 : Addition of two productions C t —> ( and C 2 —» ) will change the set of
productions to :
S -> CiSC i I c 1c 2 .1
p2
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-74 Context Free Grammars (CFG)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 5-75 Context Free Grammars (CFpj
Scanned by CamScanner
CamScanner
__Thgo t omp Sci (MU-SeiTi. 5-Comp.) 5-76
A —> ACJSCJa
Step 2 :
1 0
■I
■ Z=
■
■'a.
B
1
■■ _
5“
... !
i
S —> C a A S - > C aA
A-> ACJSCJa A —> ACa 1SCa 1 a
Scanned by CamScanner
Solution
Step 1 : Removing e-production.
The symbol B is nullable. The null production is removed. The resulting set of
productions is,
A —> aBb I bBa I ab I ba
B —> aB I bB I a I b
Step 2 : Every symbol in a, in productions of the form A —» a where I a I > 2 should be a
variable.
This can be done by adding two productions :
C a -> a
Cb — b
The set of productions after above substitution is :
• A —» C a B C b I C b B C a I C a C b I C b C a
B -> C a B I C b B I a I b
C a —> a
Cb b
Step 3 : Finding an equivalent CNF
Sr. No. Original prosecution Equivalent productions in CNF
1. A —> C a B C b A —> C a C b Q —» B C b
2. A->C b BC a A —> C b C 2 , C 2 —> B C a
3. A —> C a C b A —> C a C b
4. A —> C b C a A —» C b C a
5. B—» C a B B->C a B
6. B —> C b B B->C b B
7. B —> a B —> a
8. B —> b B->b
9. C,->a C a —>a
10. Cb ->b c b -»b
(Ssg
iWSSW
IWi«s
> ■ ■' ' -w: ..
Sic'
Scanned by CamScanner
CamScanner
MU ■ May 2012
Solution :
Step 1 : Eliminate g -production
Nullable set = {D, B )
The resulting set of productions without G -production is :
S -> ABC I AC I BaB I aB I Ba I a
A —> aA I BaC I aC I aaa
B bBblbbla
C -> CA I AC
Step 2 : The grammar does not have unit productions
Step 3 : Removing useless symbols.
{C } is a non-generating symbol. It is eliminated and the equivalent grammar is
written.
S —> BaB I aB I Ba I a - ,
A —> aA I aaa . • .;••• ? ’ : . .■ ■ ;\
B -> b B b l b b l a
The symbol {A} is non-reachable. It is eliminated and the equivalent grammar is
written.
S —> BaB I aB I Ba I a
B —> bBb I bb I a
Step 4 : Putting grammar in CNF . . . ’ '
Replacing ‘a’ by X and ‘b’ by Y, we get
S -> BXB IXB I B X I a - ;■<
B —» YBY I YY I a
X -> a, Y - > b
Now, the above set of productions can be written in CNF
S-—> BAj IXB I B X I a
A j -> XB
B —> Y A J Y Y I a
A 2 -> BY
' X a
Y -» b
| PU - May 2014
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-79
Solution :
1. The non-terminals {S, A, B) are nullable. Null productions are removed. The resulting
grammar is :
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 unit productions, we get
S -> ABA I BA I AB I AA I aA I bA I a I b I bB I aA
A —> aA I bA I a I b
B —> bB I aA I b I a
3. Every symbol in a, in production of the form A —> a where I a I > 2 should be a
variable. .
This can be done by adding two productions.
1
C a —> a .
C b —>b
The set of productions after the above changes is :
s _> ABA I BA I AB I AA I C a A I C b A I a I b I C b B I C aA
A C aA I C bA I a I b
B —> C b B I C a A 1 b I a
C a —>a, C b —> b
Finding an equivalent CNF.
Original production. “ Equivalent productions in CNF
S —> ABA S —> A C p C j —> BA
B —> C b B I C aA I b I a B —» C bB I C a A I b I a ________________ __
Ca a Ca -»a
C b ->b Cb -»b
Scanned by CamScanner
CamScanner
Theory of Comp. Sci (MU-Sem. 5-Comp.) 5-80 Context Free GrammarsjCFG)
A production of the form A —> Aa is called left recursive as the left hand side variable
i i
appears as the first symbol on the right hand side.
Language generated by left recursive grammar :
Scanned by CamScanner
CamScanner
Context
Theory of Comp, Sci. (MU-Sem. 5-Comp.) 5-81 T r e e Grammars (c
ba , baa , . . . ba ,
n
, ca , caa , . . . ca
i, 010, 01010, .
10B I 10
1. Eliminate e-productions, unit productions and useless symbols from the grammar.
2. In production of the form A —> X 1 X 2 . . . Xi . . . Xn , other than X H every other symbol should
be a variable. Xj could be a terminal.
Example : Consider a production A —> V 1 V 2 aV 3 bV4
Terminal symbols a and b can be removed by rewriting the production ’
A V j V2 aV 3 bV4 as
. A -> and adding two productions.
C a —> a
C b —> b
Thus, at the end of step 2 all productions must be of the forms.
*• fS -» aXSY I YSX I b ♦ • •
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (Mll-Sem. 5-Comp.) 5-82 Context Free Grammars (CFG}
Solution :
Step 1 : Grammar is already in a simple form without :
1. E-productions.
2. Unit productions. •/.
3. Useless symbol.
We can proceed for renaming of variables, Variables S and A are renamed as A t and A 2
respectively. The set of productions after renaming becomes :
At A 2A2
At a Productions after renaming
• , r
A 2 —> AjAj
A 2 —> b
Step 2 : Every production of the form A( -» AjOt with i > j must be modified to make i 5 j.
A 2 - production A 2 —> Aj A j should be modified.
We must substitute A 2 A 2 1 a for the first A,. We should not touch the second A t of A , A b
A 2 —> A2A2Ai
[A AAl A aV 1
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-83
Scanned by CamScanner
CamScanner
Theory of Comp, Sci. (MU-Sem. 5-Comp.) 5-84 Context Free Grammars (CFG)
T-»T*FIF
F —> (E) I a ;
Solution :
Step 1 : Grammar does not contain £-productions and useless symbol.
Grammar has unit productions.
We first eliminate unit productions :
E T and T —> F are two unit productions with the chain E —> T —> F
Non unit productions are taken as it is :
E —> E + T
T —> T * F
F —> (E) I a *•
Productions for the following pairs are added :
(E, T) => {E —> T * F}
(E,F) => {E —> (E) I a)
(T,F) => { T - (E)la) ,• . •
The resulting set of production is :
E —> E + T I T * F I (E) I a
T -» T * F I (E) I a
F -» (E) I a
Step 2 : Bringing every productions to the form A -> aa, where a is a string of variables.
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-85 Context Freg Grammars (CFq-,
Ai —» AjA2 A3 I A 3 A4 A5 1 (AjA 6 I a
A3 —> A3 A4 A5 | (AjA 6 I a
B3 —> A4 A5 B3 1 A4 A$
Aj —> (AjA 6 1 a
A 2 —> +
a 4 -» *
a 6 —> )
Scanned by CamScanner
mp
Theory . ScujMU-Sem. 5-Comp.) 5-86 - , Context Free Grammars (CFG)
SteP 6
* canteen n S A3 A4 As
’ ’ and A
6 316 i n GNE
Productions for A p B j and B 2
can be converted to GNF.
A2 —> +
A
3 (A Bj I a B3 1 (AjA6 I a
Already in GNF
A4 —> *
A
s (AjA 6 I a
A« —» )
Aj - > A 3 A4 A5 B1 . ■
V I Substituting
B 1 ->4-A3 B 1 | ± A 3
+forA 2
B A
3 4A 5 B 3 I A 4 A 5
I Substituting *forA 4
| B a -►* A 5 B 3 1 * A 5 ~|
Scanned by CamScanner
J JhggfygLCgmp Sci, (MU-Sem. 5-Comp.) ■ 5-87
A3 —> ( A , A6 I a
A6 -» )
A! -> (A,A 6 B 3 A 4 A 5 B , I aBjA 4 A 5 B 1 I (A I A 6 A4 A 5 B I I aA 4 A 5 B j I
( A , A 6 B , 1 a B , I (A,A6 B 3 A4 A 5 1 aB 3 A 4 A 5 I (A|A 6 A 4 A 5 1 aA 4 A 5 I (A ( A 6 1 a
B| —> + A 3 B ] I + A 3
B3 —> * A 5 B 3 1 *A 5 .
A 2 — A3A] l b
A3 — AjA2 l a
Step 2 : Every production of the form A t —> Aj a with i > j. must be modified to make i < j.
A 3 — production A 3 —» Aj A 2 should be modified
JJ A t is changed as per A j —» A 2 A 3
A 2 A 3A 2 - ,
■ U a 2 is changed as per A 2 —> A3AjI b
A3AjA 3 A 2 I bA 3 A 2 .
The resulting set of productions is :
A i —> A 2 A 3
A2 A3AjI b
A 3 — A 3 A]A3 A 2 1 bA 3 A 2 1 a
Step 3 : Remove left recursion
A3 Production has left recursion. Left recursion from A 3 - production can be
removed through introduction of B 3 - Production
A 3 —~y bA 3 A 2 B 3 1 aB 3
B 3 —> AjA3A2B3 I AjA3A2
The resulting set of production is
A j —>A2 A 3
Scanned
Scanned by CamScanner
by CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-88 Context Free Grammars (CFG)
A2 —>A3 A] I b
A 3 -> bA 3 A 2 B 3 1 aB 3 1 bA 3 A 2 1 a
B 3 -» A 1 A 3 A 2 B 3 1 A]A 3 A 2
Step 4 : A 3 - productions are already in GNF
A 2 - production can be converted to GNF with the help o f A 3 — productions.
A2 > A 3A | I b
U Substituting bA 3 A 2 B 3 1 aB 3 1 bA 3 A 2 I a for A 3 .
A 2 —> b A 3 A 2 B 3 A j I aB 3 A ! I bA 3 A 2 A j I a A , I b
A | —> bA 3 A 2 B 3 A 1 A 3 1 aB 3 A j A3 1 bA 3 A 2 A A 3 1 aAjA 3 1 bA 3
Scanned
Scanned by
by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-89 Context Free Grammars (CFG)
M U - May 2011
Solution :
Step 1 : Making every symbol oilier than the first symbol (in derived string a in A —» a)
t as a variable :
Variables C b is substituted for b with resulting set of productions give as :
S —> AB
A —> BSB I BB I b
B —> aAC b I a , C b —> b
Step 2 : The variables S, A, B and C b are renamed as A }, A2 , A3 and A4 respectively. The
resulting set of productions is given below.
Ai —> A 2 A 3
A2 —> A 3A i A 3 I A 3 A3 1 b
A3 —> aAjA 4 I a
A 4 —> b
Step 3 : Convert to CFG
Given production Equivalent Production
inGNF
A4 - > b —► A4 - > b - -
u
Substituting A3 > A2 -> a A! A 4 A1 A3 1 aA •
A 2 -> A 3 A 3
A 2 -> b —► Ag -> b
A<| —> A 2 Ag
JL K A A ■
Substituting A2 y A-| -> a A 1 A4 A1 A 3 A3 1 a A i A 3 A3
" r | aA-(A4 A1 A3A3 1 aA 3 A3 1bA 3
Scanned by CamScanner
y Theory of Comp, Sei, (MU-Sem. 5-Comp.) 5-90 Context Free Grammars (CFG)
Example 5.5.15 : Convert the following grammar to Greibach Normal Form (GNF),
Step 1 : Modifying productions to ensure that on R.H.S, other than the first symbol every
other symbol must be a variable.
X —> a
Y —> c
S —> BS I AX
A —> bY
B ->AY
Step 2 : Remaining of variables.
The variables S, A, B, X, Y are renamed as A b A 2 , A3 A 4 , A 5
A 4 —> a
A 5 —> c
Aj A 3 Ajl A 2 A4
A 2 -> b A 5 ... ■
A3 A 2 A5
Productions for A 4 , A 5 , A 2 are in the required form. Productions for A t and A 3 can
A 1 -> A3A1 1 A2 A 4
Substituting bA 5 A5 for A3
and bAg for A2
A1 -* bA5 A5 A i | bA 5 A4
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 5-91 Context Free Grammars (CFG)
Step 1 : Making every symbol other than the first symbol (in derived string a in A —> a)
1
as a variable :
Vanable C b is substituted for b with resulting set of productions given as :
S —> AB AC b I aC b
B -> ABA I a
A —» a I b
C b -» b
Step 2 : Substituting A —> a I b , in S —> ABAC, and B -> ABA, we can convert
productions in GNF. The fmal set of productions is given by :
S aBAC b I bBACb I aC b ' .
B —> aBA I bBA I a
A —> a I b
Cb -> b
Solution :
Scanned by CamScanner
CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 5-92 Context Free GrammarsjCFG)
s ->aAB I bBB I bB
The required get of productions in GNF is :
S -> aAB I bBB I bB
A —» aA I bB I b
Substituting | a for A 2
A 3 —> A 3 A 2 A 4 1 a A 4 | A 3 A 2 Ag | aA 2
Scanned by CamScanner
CamScanner
Qr
___Th£ y of Comp. Sci. (MU-Sem. 5-Comp.) 5-93
B 3 ~ * A 2 A 4 B 3 IA2 A 2 B 3 IA2 A 4 IA 2 A 2
The resulting set of production is
A j —> A 2 A 3 laA 4
A 2 — A 3 A 2 la
A2 A 3 A 2 la
Aj —> A 2 A 3 laA 4
MU - . 2006
Scanned
Scanned by
by CamScanner
CamScanner
Theory ofComp Sci. (MU-Sem. 5-Comp.) 5-94 Context Free Grammars (CFG)
Solution :
:
Step 1 Other than (he first symbol on the RHS of every production, every symbol must
be a variable.
We can make the substitution X for b.
The resulting set of productions after the above substitution is :
S —> SSIaSXlaX
X— >b
Step 2 : Removing left recursion from s-production, we get :
S -> aSXSJaXSjIaSXlaX
Sj - SSJS
X ->b .
Step 3 : Sj-productions are not in GNF. They can be brought to GNF by substituting S.
S -> aSXSJaXSJaSXlaX
S j -> aSXSjSJaXSjSJaSXSJaXSJaSXSjaXSjIaSXlaX
X ->b
Scanned by CamScanner
CamScanner
M U - M a y 201 1
Solution :
A j —> A 2 A 2 1 1
A 2 —> A 1 A 1 I 1
Step 2 : Every production of the form A t —> Aj a with i > j must be modified to make i< j .
A 2 - production, A 2 —» A i A ] should be modified. We m u s t substitute A 2 A 2 I 1 for
the first A P
A 2 —> A 2 A 2 A |
[A 2 —» A! A J =>
A 2 —» l A j
Ai —> A 2 A 2 I 1
A 2 —> 1 A j B 2 I 1 B 2
B2 > A 2 A] B 2 I A 2 Aj
. The resulting set of productions i s :
Ai ~ > A 2 A 2 I 1
A 2 —» 1 A t B 2 I 1 B 2 I 1 A j I 1
B 2 — A 2 A] B 2 1A 2 Ai '
Step 4 : A 2 - productions are in GNF.
A j and B 2 productions can be converted to GNF with the help of A 2 - productions.
I 1 Aj B 2 1 1 Aj B 2 Aj I 1 B 2 Aj I 1 Aj Aj I 1 A t
Scanned
Scanned by CamScanner
by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-96
2,
u x y— — z
x is generated by T 2
v is generated by Tj
u is generated by T
Tj excluding T 2 can be repeated any number of times.
This will yield a string of the form uvxyz where i > 0.
Scanned by CamScanner
-X_ Theory of Comp, Sci. (MU-Sem, 5-Comp.) 5-97 Context Free Grammars (CFq
M U - May 2008
Solution :
1. Let us assume that L is CFL.
2. Let us pick up a word w = a n b nc n where the constant n is given as per the pumping
lemma.
3. w is rewritten as uvxyz.
Where I vxy I < n and v • y £ £ i.e., both v and y are not null.
‘ 4. From pumping lemma, if uvxyz G L then uvbcy1 z is in L (G) for each i = 0, 1, 2, . . .
There are two cases :
Case I : vy contains all three symbols a, b and c.
If vy contains all three symbols a, b and c then either v or y contains two symbols.
The exact ordering of a, b and c will be broken in uv 2xy 2 z and hence
uv2 xy 2z £ L (G)
Case II : If vy does not contain three symbols a, b and c then uv 2 xy2 z will have unequal
number of a’s, b’s and c’s and hence uv2 xy 2z £ L (G).
Hence, it is proved by contradiction.
Scanned by CamScanner
CamScanner
V' Theory j f C o m p . Sci. (MU-Sem. 5-Comp.) 5-98 Context Free Gramrnars(CFG)
3. to is rewritten as uvxyz where, Ivxyl < n and v.y e, both v and y are not null.
4, From pumping lemma, if uvxyz e L then u v x y z is in L(G) for each i = 0, 1,2....
There are two cases :
Case I ; vy contains all three symbols a,b and c. If vy contains all three symbols a,b, and c
then either v or y contains two symbols. The exact ordering of a,b and c will be
1broken
1 * 2 2 in uv xy z and hence uv9xy
9 z g L(G).
Case II : If vy does not contain three symbols a,b and c then uv 2 xy 2z will have either :
(a) Unequal number of a and b
(b) Count of either a or b can be increased from the count of c.
Hence, proved by contradiction.
Example 5.6.4 : A = (a n I n > 1} is context free. If so, enumerate some members of the
equivalent CFL.
Solution :
2
L = { a n I n > 1 } is not a context free language
Proof that L is not a CFL :
It can be proved using a contradiction. Let us assume that L is a context free language.
1. Let n be the constant as per the pumping lemma.
2. . Let us choose a word w = a
3. w is rewritten as uvxyz.
Where I vxy I < n and v • y e, both v and y are not null.
I uvxyz I = I a n I = n
2
I uv2 xy2 z I = I uvxyz I + I vy I > n
And l u v2 xy2 z l < n2 + n [aslvyl<n]
< n2 + 2n + 1 o
< (n + 1)2 ..
I uv2 ny2 z I is a square for every i, uv’xyz G L. But there is no square between n 2 and
(n + l ) 2 . This is a contradiction. Therefore, L is not a CFL.
Example 5.6.5 : Prove that the following language is not a context free language.
L = {a n bmc p I 0 5 n<m<p)
Solution :
1. Let us assume that L = { a W l 0<n<m<p} is CFL.
2. Let us pick up a word to = a nbn * *cn * , where the constant n is given as per the pumping
lemma.
Scanned by CamScanner
CamScanner
Theor
-T y ot Comp, Sci, (MU-Sem, 5-Comp.) 5-99 Context Free GrammarsjCF
3. co is rewritten as uvxyz, where Ivxyl < n and v.y , both v and y are not null.
4. From pumping lemma, if uvxyzeL then uv'xy'z is in L(G) for each i = 0,1,2...
There are two cases :
Case I : vy contains all three symbols a,b and c. If vy contains all three symbols a,b and c ,
then either v or y contains two symbols the exact ordering of a,b and c will be
broken in uv 2xy 2 z and hence uv2 xy 2z e L(G).
Case II : If vy does not contain three symbols a,b and c then the count of the missing
symbols/symbol will be less than or equal to the other symbols/symbol in uv xy2z.
Similarly, the count of the missing symbols/symbol will be more than or equal to the
other symbol/symbols in uv°xy°z.
Thus the sequence n < m< p in a n bmc p can be made to violate either for i = 0 or
i = 2 in u v x y z .
Hence, it is proved by contradiction.
Example 5.6.7
i 'f t'
Solution :
1. Let us assume that L is CFL
2. Let us pick up a word co = 0 n l n 2 n 3 n, where the constant n is given as per the pumping
lemma.
Scanned by CamScanner
CamScanner
Theory of Comp, ScL (MU-Sem. 5-Comp.) 5-100 Context Free Grammars (CFG)
co is rewritten as uvxyz where Ivxyl < n and v-y e i.e. both v and y are not null.
From pumping lemma, if uvxyz e L then uv'xy'z is in L(G) for each i = 0,1,2,...
There are two case :
v
Case I J y contains three symbols. These three symbols could be 0,1,2 or 1,2,3.
The exact ordering of 0,1, 2, 3 will be broken in uv 2xy 2z and hence uv xy z gL(G)
Case II : If vy does not contain three symbols then uv 2xy 2 z will have either unequal num
of 0’s and 2’s or unequal number of l’s and 3’s. Hence, uv xy z £ L (G).
Thus, proved by contradiction.
In this section, we will consider some general properties of CFL. These properties can be
classified in two groups :
1. Closure properties
2. Algorithmic properties (Decision properties)
Scanned by CamScanner
CamScanner
. Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-101
A new language L is constructed by combining the two grammars G1 and G2 into one
grammar G that will generate the concatenation of the two languages.
• A new start symbol S is added to G.
• A new production is added to G.
• S —> S t S 2
The start symbol S will generate a string w of the form :
w = WjW2, where Wi e Lj and w 2 e L 2 .
The grammar G can be written as :
Scanned by CamScanner
CamScanner
e
==L=== =j£i = Sem_5-Com£2_ 5-102 Conte)<t Free Gra ( E91
s -» sSj
S —> E ’ . ,
The pi eduction S —> SS j I e will generate a string w* where we L P
The grammar G can be written as :
G
= (VhT PjUIS- SSJe} )
5.7.1-4 CFL i s not Closed under Intersection
Theorem :
Theorem : • .
Scanned by CamScanner
CamScanner
V' Theory of Comp, Sci. (MU-Sem. 5-Comp.) 5-1 03
Proof :
This theorem can be proved through contradiction.
Let us assume that CFL is closed under complementation.
If Lj is context-free then L is also context-free.
If L * is context-free then L"2 is also context-free.
Now, Lj n L 2 can be written as ( L, u L'2 ) , which should also be a context-free.
Since, L 1 n L 2 is not guaranteed to be context-free, our assumption that CFL is closed
under complementation is wrong.
5.7.1 .6 Intersection of CFL and RL
Theorem :
If L is a CFL and R is a regular language, then R n L is a CFL.
Proof :
Let us assume that L is accepted by a PDA
Ml = (Qi.Lp r i5 8 1( q l t z„ F,)
and R is accepted by a FA
M 2 = (Q2, So,8 2, q 2, F 2)
We can combine M t and M2 into a single PDA M = (Q, E, F, 8, q, z, F). The PDA M
will accept a string w if it accepted by the PDA M t and FA M 2 both executing in parallel.
The construction of M is given below :
Q = Qi x Q 2, the Cartesian product of states of and M2
X = Z1
r r
= i
q = (Qi» Q2)
z = z}
F = Fi x F 2
Scanned by CamScanner
y Theory of Comp. Sei, (Mu , Sem 5 . Comp ) Context Free Grammars (CFG)
* Since, M 2 will read one symbol at a times, it requires I u I steps to reach the state p 2 from
Q2-
Theorem :
MU - Dec. 2012
There are algorithms for testing :
1. Whether the language generated by a CFG is empty i.e. Is L(G) = 0 ?
2. Given a grammar G and a string w, is w e L(G) ?
The method of finding whether a string w belongs to L(G) is known as parsing. There
are two types of parsing :
1. Top-down parsing
2. Bottom-up parsing
In top-down parsing, we try to derive w, starting from the start symbol S.
In bottom up parsing, we try to reduce the word w into the start symbol S .
Scanned by CamScanner
CamScanner
Theory of Comp, Sci. (MU-Sem. 5-Comp.) 5-105 Context Free Grammars (CFG)
May 2006
Q. 6 Proof L = {ap I p is prime} is not context free (Ei cample 5.6.1) (5 Marks)
Q. 7 Consider the following grammar
S ASB I e
A -> AAS I a
B -» SbS I A I bb
Put the above grammar in CNF. (Example 5.5. 7) ' (10 Marks)
Dec. 2006
May 2007
Scanned by CamScanner
Theory of Comp, Sci (MU-Sem. 5-Comp.) 5-106 Context Free Grammars (CFG)
Dec. 2007
Q. 1 3 Prove that L = {0111 213* I i >= 1 and j > = 1 } is not context free.
(Example 5.6.7) ( 8 Marks)
May 2008
Q. 14 Prove that L = {an b n cn I n >= 1} is not context free. (Example 5.6.2) (5 Marks)
Dec. 2008
Q. 17 State and prove pumping lemma for context free languages. (Section 5.6) (10 Marks)
Q. 18 Explain GNF with suitable example. (Section 5.5.2) (10 Marks)
Q. 19 Design a grammar for accepting an even palindrome over Z = {a,b}
(Section 5.2.3) (10 Marks)
May2009
Q. 21 Let G = {V.T.P.S} be the CFG having following set of productions. Derive the string
“aabbaa” using leftmost derivation and rightmost derivation.
S -» aAS I a, A -> SbA I SS I ba. (Example 5.2.4) (5 Marks)
Scanned by CamScanner
Scanned by
w
*r Theory of Comp. Sei, (MU-Sem. 5-Comp.) 5-107
Dec. 2009
(Example 5.2.33(iv))
(10 Marks)
May 2010
fDec<i201Q|
Q. 31 Let G b e the grammar. Find the leftmost derivation,
rightmost derivation and parse tree
for the string 00110101.
G: S -> 0B I 1A
A -> 0 I OS 1 1AA
B —> 1 1 1 S 10BB (Example 5.2.7)
(10 Marks)
Scanned by CamScanner
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-108 Context Free Grammars (CFG)
May 2011
Dec. 2011
Scanned by CamScanner
Scanned by CamScanner
— Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-109
May 2012
Q. 42 Prove that it is undecidable whether Context free grammar is ambiguous.
(Section 5.3)
Q. 43 Begin with the grammar :
S -> ABC I BaB
A —> aA I BaC I aaa
B bBb I a I D
C CA I AC
Eliminate g Productions.
Dec. 2012
Q. 46 Give and explain ambiguous context free language. (Section 5.3} (5 Marks)
Q. 47 Let G be the grammar
S -> aB I bA
A -» a I aS I bAA
B —» b I bS I aBB
Find the leftmost derivation, right most derivation and parse tree for the string
"bbaaabbaba". (Example 5.3.9)
(10 Marks)
Q . 48 Write short notes on : Closure property and decision algorithm for CFL.
(Sections 5.7.1 and 5.7.2) (20 Marks)
Scanned by CamScanner
y Theory of Comp. Sei (MU-Sem. 5 . Comp } 5 . 11Q
May* _______
...
2013■
Q. 49 What is ambiguous CFG ? Give one example of ambiguous CFG.
(Section 5.3) (5 Marks)
q. 50 Let G be the grammar, find the leftmost derivation, rightmost derivation and parse tree
forth© string 00110101.
S - > 0 B I 1A
A -» O I O S I 1AA
B
-> 1 I 1 S I 0BB (Example 5.3.9) (Note : take ‘O’ as ‘a’ and ‘1’ a s ‘b’)
Q. 51 Explain C N F and GNF with example. (Sections 5.5.1 and 5.5.2) (10 Marks)
May 2014
(iv) Check if the above grammar is ambiguous (Section 5.2.7) (10 Marks)
Q. 56 Obtain leftmost derivation, rightmost derivation and derivation tree for the string
“cccbaccba". The grammar is
Scanned by CamScanner
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-11 1 Context Free Grammars (CFG)
Zl\
S S b
to
tn
Fig. 1-Q. 4
Left most derivation Right most derivation
S SSa S -> SSa
—> SSaSa —» SSSba
—> cSaSa -> SScba
—> cSSbaSa —> Sccba
ccSbaSa —> SSaccba
—> cccbaSa —> SSSbaccba
cccbaSSba —> SScbaccba
' —■> cccbacSba Sccbaccba
—> cccbaccba —> cccbaccba
May 2015
S -> ASB I a I bb
A-> aSA I a
B -> SbS I bb
Ans. : [Hint : Refer as Example 5.5.20(A)] (10 Marks)
S-»ASBIalbb
A -> aSA I a
B —> SbS I bb
Scanned by CamScanner
Theory of Comp. Sci. (Mll-Sem. 5-Comp.) 5-11 2 Context Free Grammars (CFG)
Converting to CNF :
Re-writing the grammar, we get,
S -> ASB l a i V 1V l
A —> V 2S A I a
B -> SVjS I V 1V l
Vj -> b
V2 -».a
Now, re-writing each production in its equivalent CNF form, we get,
Productions CNF forms
S —> ASB S -> AV 3, V3 -» SB
S—a S—a
S - > V , V, S - > V j V1
A —> V 2 SA 1 a S —> V 2 V4, V4 —> SA
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-113 Context Free Grammars (CFG)
• i:>;
Dec.
.
2015
.... : .
Q. 59 Consider the following grammar G = (V, T, P, S), V = {S, X, Y}, T = {a, b} and
productions P are
S -» XYX
X -> aXle
Y -> bYle
Convert this grammar in Chomsky Normal Form (CNF).
(Similar to Example 5.5.2) . (1 0 Marks)
Q. 60 State and prove pumping lemma for context free language.
(Section 5.6) (10 Marks)
Q. 61 Consider the following grammar G = (V, T P, S), V = (S, X), T = {0, 1} and productions
P are
• S - > 0 10X11 01S1
X->0XX1l1S
S is start symbol. Show that above grammar is ambiguous. (5 Marks)
Ans. : [Hint : Refer as Example 5.3.12(A)]
A grammar is said to be ambiguous grammar if the language generated by the grammar
contains some strings that has 2 parse trees.
Ex. : Let us consider the given grammar
S 0 I 0 X 1 I01S1
X -> 0XX1 I IS
where, S is the start symbol.
A string 01001 1 is generated by the given grammar.
The grammar generates the string 010011 in 2 different ways. The 2 deviations are
shown in Fig. l(a)-Q. 61 and Fig. l(b)-Q. 61. As the same string has 2 different parse trees.
The given grammar is ambiguous grammar.
s
0 X
I
0
I
a
(a) (b)
Fig* I-Q. 61
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 5-114 Context Free Grammars (CFG)
Si—’-
May 2016
q. 62 State and prove pumping lemma for context free languages.
(Section 5.6) (10 Marks)
S -» a S b I aX
X —> X a I S a I a
S -> aSb I aX
X ‘ —> Xa I S a I a
Substituting C a for a, C b for b, A1 for S and A2 for X. .
A-j — a A1 C b I a A2
A2 -> A 2 C a I A! C a I a
c. '
cb -> b
Ca —> a, C b —> b
• A, —> a A 1 Cb laA 2
A2 A , C a A 3 I a A 3 I A, C a I a
a3 Ca a3 I a2
Re-writing productions in GNF from
A! a Af C b I a A 2
a A 1 C b C a A 3 1 a A 2 C a A 3 1 a A 3 1 a A1 C b C a I a A 2 C a I a
A3 a A 3 1 a A'j C b C a A3 1 a A2 C a A3 1 a A3 1a A! C b C a I a A 2 C a I a
Ca —>
Cb —> b
Scanned by CamScanner
Scanned by CamScanner
Thgory of Comp. Sei, (MU-Sem. 5-Comp.) 5-115 Context Free Grammars (CFG)
Dec. 2016
■
S -> AA I a
May 2017
Dec. 2017
Scanned by CamScanner
V Theory of Comp, Sei, (MU-Sem. 5-Comp.) 5-116 Context Free Grammars (CFG)
Chapter Ends...
□□□
Scanned by CamScanner
CamScanner
Pushdown Automata
(PDA)
Module 4
Informally, pushdown automata can be viewed as finite automata with stack. An added
stack provides memory and increases the capability of the machine.
A pushdown automata can do the followings :
1. Read input symbol [as in case of FA]
2. Perform stack operations.
2.1 Push operation
2.2 Pop operation
2.3 Check empty condition of a stack through an initial stack symbol.
2.4 Read top symbol of stack without a pop.
3. Make state changes.
PDA is more powerful than FA. A context-free language (CFL) can be recognized by a
PDA. Only a subset of CFL that are regular can be recognized by finite automata.
• A context free language can be recognized by PDA.
• For every context-free language, there exists a PDA,
• The language of PDA is a context-free language.
n
Example : A string of the form a b" can not be handled by a finite automata. But the
same can be handled by a PDA.
• Any machine recognizing a string of the form a" b", must keep track of a’s [first half of
a”b"] as number of b’s must be equal to the number of a’s.
•' First half of the string can be remembered through a stack.
Scanned by CamScanner
y Theory of Comp, Sci (MU-Sem. 5-Comp.) 6-2 Pushdown Automata (PDA)
b Input
Finlto slate
control
Stack
• As the machine reads the first half of a n bn, it remembers it by pushing it on top of the
stack. As shown in Fig. 6.1.1, after reading first 5 a’s, the stack contains 5 a s.
• While reading the second half of the input string consisting of b’s, the machine pops out
an ‘a’ from the stack for every ‘b’ as input.
• After reading 5 b’s, input will finish and the stack will become empty. This will indicate
that the input string is of the form a11 bn .
• The machine will have two states q0 and q r
State q 0 - while the machine is reading a’ s
State qj - While the machine is reading b’s. ,
While in state qp an input ‘a’ is not allowed and hence there is a need for two states.
L Current state
2. Current input
• ‘ >
3 Top symbol of the stack.
A transition in PDA can be shown as a directed edge from the state q ( to q.. While
moving to state q jt the machine can also perform stack operation. A transition edge from q L to
q should be marked with current input, current stack symbol (topmost symbol of the stack)
and the stack operation. It is shown in Fig. 6.1.2.
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 6-3 Pushdown Automata (PgA)
Stack operation
Input
symbol
Stack
a, any (b,a) symbol
push (a) (b,a) Pop (a)
POP (a)
Stack
operation
• The state q 0 will keep track of the number of a’ s in an input string, by pushing symbol
‘a’ onto the stack for each input ‘a’. A second state q , is used to pop an ‘a’ from the
stack for each input symbol ‘b’. Finally, after consuming the entire input the stack will
become empty.
MU - Dec. 2016
A pushdown automata M is defined as 7-tuple :
M = (Q, Z, r, 8, q0 , z 0 , F)
Where,
Q = The set of states
E = Input alphabet
T = Stack symbols
8 = The transition function is a transition form Q x (X u e) x T to Q x T*
q0 = q 0 G Q is the initial state
Scanned by CamScanner
CamScanner
y Theory of Comp. Sci. (MU-Sem, 5-Comp.) 6-4 Pushdown Automata (PDA)
z =
o An initial stack symbol
Transition function in detail :
Q x (Z u e) x r to Q x r*
Where
Q x ( I u e ) x P implies that a transition is based on :
• 1. Current state
2. Next input (including e)
3. Stack symbol (topmost)
and
Q x T* implies that :
1. The next state could be any state belonging to Q.
2. It can perform push, pop or no-operation (NOP) on stack.
In general, we can have the following types of transition behaviours.
1. Read input with no-operation on stack :
8(q 1 , a, b) = (q2 , b)
2. Pop operation :
8(q 1 , a , b ) = (q2 , e)
Stack symbol (top most)
is replaced with e
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-5 Pushdown Automata (PDA)
The above transition will erase the stack symbol (topmost). Replacing, b with 8 amounts
to erasing b from the stack top.
3. Push operation :
S t q p a . b ) = (q2 , ab)
1 1
-----------Replacing the stack
symbol (top most) b with
ab will push a symbol
a onto the stack
The above transition will perform a push operation. It will push ‘a’ onto the stack.
Replacing *b’ with 4ab’ amounts to push xa’ onto the stack.
The three operations for stack are shown in Table 6.2. 1.
Table 6.2.1 : Transitions rules for stack operations
transition
top
No - operation
top
top
- operation
— top
Push - operation
Scanned by CamScanner
y Theory of Comp. Sei, (MU-Sem, 5-Comp.) 6-6 Pushdown Automata (PDA)
Note ♦ Failure transitions are not shown as it is a standard practice, ______ ___________
The transition rules mentioned above can be written in two different forms.
Form 1 Form 2
8(q 0 , a, z 0 ) = (q Q, a 2() )
((q 0 . azo), (q0 , az0 ))
8(q 0 , a, a) = (q Q, aa) => ((q 0 , a, a), (q 0 , aa))
8(q 0 , b, a) = (q p g) => ((q 0 , b, a), (q p e))
8(q p b, a) = (q p g) => ((q p b, a), (q p e))
=
(Qp Zo) (q 2 , Zo) ((q p S, z 0 ), (q 2, z0 )) ,
Stack
contents
Stack
Scanned by CamScanner
CamScanner
Sr* No.
abbb, aaz(
bbb, aaaz(
, bb,
Scanned by CamScanner
CamScanner
i_(MU;Sem. 5-Comp.) 6-8 Pushdown Automata (PDA)
q2 — 4 z0
Example 6.3.1 : Suppose the PDA M = ({q0 , q j , {a, b, c}, {a, b, Zo}, 8, q 0 , Zq,{qj) has the
following transition function.
1. 8(q 0 , a , e) = (q0 , a) 2. 8(q 0 , b, e) = (q 0 , b)
3. 8(q 0 , c , e) = (q p e) 4. 8(q p a, a) = (q p e)
5. 8(q p b, b) = (q p e)
Show the acceptance of abbcbba by the above PDA through an
instantaneous description.
Solution :
(Rulel)
(q 0 , abbcbba, z0 ) ----------> (q 0 , bbcbba, azo)
(Rule 2)
---------t* (q 0 , bcbba, baz 0 )
(Rule 2)
----------* (q 0 » bbaz0 )
(Rule 3)
----------> (q p bba, bbaz0 )
(Rule- 5)
----------> (q p ba, baz0 )
(Rule 5)
----------> (q p a, az0 )
(Rule 4)
----------> (q P e, z0 )
Since, the machine is in a final state q p the string abbcbba is accepted.
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-9 Pushdown Automata (PDA)
PDA by
final state
Let the PDA, M = (Q, E, T, 8, q0 , z0 , F) then the language accepted by M through a final
state is given by :
. L(M) = w l (q0 , w, z0 ) => (q p 8, a) ►
Where the state q, E F. a, the final contents of the stack are irrelevant as a string is
accepted through a final state.
Let the PDA, M = (Q, S, T, 8, q 0 , z0 , <» then the language accepted through an empty
stack is given by :
* I I
L(M) = w I (q0 , w, Zo) => (q e, e) -
I M
Where q 1 is any state belonging to Q and the stack becomes empty on application of
input string w.
Example 6.4.1 : Give a PDA to accept the language
L = {0n1 m I n £ m )
1. Through empty stack’.
2. Through final state.
Scanned by CamScanner
CamScanner
y Theory of Comp, Sci. (MU-Sem. 5-Comp.) 6-10 Pushdown Automata (PDA)
Solution :
Algorithm :
o, Zo |OZo 1,0|£
0, 0|00
£ z
. olz 0
Fig. Ex. 6.4.1(b) : Transition diagram for acceptance through a final state
1.
Oz
8(q 0 , 0, z 0 ) = (q 0 » o) i. 8(q 0 , 0, z0 ) = (q O’ Oz o)
5. 3(q l,Zo)
=
(qp z o) 5. 8(q 1, 1, Zo) =
(qp zo)
P
e 6. 8(q p e, Zo) =
6. 8(q,, e, Zo) = (q P ) (q 2. zo)
Fig. Ex. 6.4.1(c) : State transition ru Fig, Ex. 6.4.1(d) : State transition rules
for acceptance through an empty for acceptance through a final state
stack
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-11 Pushdown Automata (PDA)
(q„ 1 1. oz.)
(Kok 4 > . , .
----------> (q P i> zo)
(Rule 5)
----------> (q p e, Zo)
(Rule 6)
— ------> (q p 8, 8)
Case II : Acceptance through final state :
(Rule 1)
(q 0 , 00111, z0 ) - --------$ (q 0 , 01 1 1, 0z0 )
(Rule 2) '
- --------> (q 0 , 111, 00z 0 )
(Rule 3)
- --------4 ( q p l l , O z o )
(Rule 4)
----------> (q P 1, z 0 )
(Rule 5)
---------->(q p 8 , z 0 )
(Rule 6)
----------> (q 2, 8, z0 )
S(q 0 , a, a) = (q 0 , aa)
Scanned by CamScanner
CamScanner
2, An a should be popped for every b as input till the end of input.
S(q0, b, a) = (q p e)
S(q P b, a) = (q p e)
3, Additional m - n a’s should be popped from the stack.
8(q p e, a) = (q p e )
4, Finally y the symbol z0 should be popped out to make the state empty .
8(q p £, z0 ) = (q p e)
Transition diagram is given in Fig. Ex. 6.4.2 and transition table in Table Ex. 6.4.2
Table Ex. 6.4.2
8(q 0 ,a,z0 ) = (q 0 ,az0)
8(q 0,a,a) = (q 0,aa)
8(q 0,b,a) = (q p e)
a,a/aa b,a/e
"x e,a/e 8(q p b,a) = (q p e).
Sfe,z 0/ €
8(q p e,a) = (q P e)
b,a/e
8(q p e,z 0 ) = (q p e)
M = ( { q 0, q J ’ t a ’ b } ’ b’ Z ° J ’ q°’ z ° ’ f q 2
Example 6 4 3 - Design a PDA for accepting the set of all strings over {a, b} with an equal
' ' ’ n u m ber of a’s and b’s. The string should be accepted both by
(!) Final state (2) Empty stack.
Scanned by CamScanner
P u s h d o w n Automata (PDA)
Theory of C o m p . Sei, (MU-Sem. 5-Comp.) 6-13
Solution :
• Stack will be used to store excess of a’s over b’s or excess of b s over a s out of input
. seen so far.
• The status of stack on input abaabbbbaa is shown in the Fig. Ex. 6.4.3.
a a Input
* ab aabbbb
I II I II 1 1 " II 1 1 I II, 1 1 J 1 1 b 1 1 I
Stack
Fig. Ex. 6.4.3 : Stack preserving excess of a’s over b’s o r b’s over a s
Algorithm :
to make
the stack
empty
e 2
’ olz o
(a) Transition diagram for the PDA accepting (b) Transition diagram for the PDA accepting
through an empty stack through a Anal state
Scanned by CamScanner
CamScanner
V Theory of Comp, Sci. (MU-Sem. 5-Comp.) G-14 Pushdown Automata (PDA)
8 (q 0 , a, z 0 ) = (q 0> az0 ) az
8 (q0 , a, z„) “ (q 0 * o)
S(q 0 , b, z 0 ) = (q 0 , bz0 ) 8(q 0 , b, z0 ) =
(q<p b z o)
5(q 0 , a, b) = (q 0 , e) 8(q 0 , a, b) =
(q 0’
6(q 0 , b, a) = (q 0 , e ) 8(q 0 , b, a) = (q<r e )
8(q 0 , a, a) = (q 0 , aa) 6(q (), a, a) = (qir aa)
8(qQ, b, b) = (q 0 , bb) 8(q 0 , b, b) = (q 0 , bb
)
8(q o ,E,Zo) = (q 0 , e) 8(q 0 , E, z0 ) =
(q z
o)
Fig. Ex. 6.4.3(c) : Transition rules for the Fig. Ex. 6.4.3(d) : Transition rules for the
PDA accepting through empty stack PDA accepting through final state
Fig. Ex. 6.43 (a to d) shows .state transition behaviour of PDA of Example 6.43.
The PDA accepting through empty stack is given by :
M = ({q 0 } {a, b} {a, b, z0 }, 8, q 0 , z0 , 0)
Where, 8 is given in Fig. Ex. 6.4.3(c).
The PDA accepting through final state is given by :
M = ({q0 , q j , {a, b}, {a, b, z 0 }, 8, q 0, z 0 , { q j ) ,
Where, 8 is given in Fig. Ex. 6.4.3(d).
!1 n
Example 6.4.4 : Design a PDA to accept (ab) (cd) .
MU - May 2007
Solution : To solve this problem, we can take a stack symbol x. For every ‘ab’, one x will be
pushed on top of the stack. After reading (ab)n, the stack should contain n number of x’s.
These x’s will be matched with (cd)n. For every ‘cd’ one x will be popped.
The transitions for the PDA accepting through an empty stack are given in Fig. Ex.6.4.4.
a,x/x
c,x/x b,x/xx
o,x/x
d,x/e
Scanned by CamScanner
CamScanner
f JTheory of Comp. Sci. (MU-Sem. 5-Comp.) 6-15 Pushdown Automata (PgM
S(qb, a, z 0 ) = (q h z0 )
8(q 0 , a , x ) = (q b x)
8(q ls b, z0 ) = (q 0 , x z0 )
8(q b b, x) = (q0 , xx)
8(q 0, c, x) = (q2, x)
8(q 2 , d, x) = (q 3 , £)
8(q 3 , c, x) = (q 2 , x)
ample 6,4.5
MU - ■. 2005
Solution :
To solve this problem, we can take a stack symbol x. For every ‘bdb\ one x will be
pushed on top of the stack. After reading (bdb)", the stack should contain n number of x’s.
These x’s will be matched with c’s.
The transitions for the PDA accepting through an empty stack are given in Fig. Ex.6.4.5.
b,x/x d,x/x
biZj/ Q d.ZQ/Zo
c,x/e b,x/xx
b.Zj/xZo
c,x/g
8,Zq/g
Scanned by CamScanner
CamScanner
5 Theory of Comp, Sci. (MU-Sem. 5-Comp.) 6-16 Pushdown Automata (PDA)
I ll iL i| Il ||dp|MM| || p i || | L i r |
I ?o 1 1 ,z o I
q
[miI
q
z
q
° 1 1 z ° I L d I— I 1— J L_d
q q q q q
Izd
q
L_d
q
I z ° 11 z ° 11
q q q
11 z ° 1 1 z ° I ■*
q q
stack
s
q0 i i ° ’ ’ ' ° ' ° ’ ° ° 1 ° o * ’a‘e
Scanned by CamScanner
y Theory of Comp. Sei, (MU-Sem. 5-Comp.) 6-1 7
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp,) 6-1 8 Pushdown Automata (PDA)
(Rule 9)
----------> (q0, aabaab, z0)
(Rule r IT \
---------> (q p abaab, z0)
(Rule 7)
---------> (q0, baab, xz0)
(Rule 5)
---------> (q0, aab, z0)
(Rule 1)
---------> (q p ab, z0 )
(Rule 7)
• • ---------> (q0, b, xz0)
(Rule 5)
---------> (q0, e, z0)
(Rule 13)
---------> (q2, 8, z0)
Example 6.4.7 : Give the transition table for PDA recognizing the following language
L = { a n x l n > 0 and x e {a, b}* and I x I < n }
Solution :
Algorithm :
• Sequence of initial a’s should be pushed onto the stack in state q0 .
• For every .symbol in x, an ‘a’ should be erased from the stack.
1. Push the first ‘a’ using the transition 8 (qO, a, zO) = (qO, azO)
2. Push subsequent a’s using the transition 8 (qO, a, a) = (qO, aa).
3. On the first ‘b’ as input, the machine will transit to q1 with a pop operation using the
transition
8(q 0 , b, a) = (q p 8).
4. On subsequent a’s or b’s in x, an ‘a’ should be erased from the stack.
8(q p a, a) = (q p 8)
8(q p b, a) = (q p e)
5. After the end of input string, contents of stack should be erased one by one.
. 8(q p e, a) = (q p E)
8(q p E, z0 ) = (q P e) • ,
The PDA is given by :
M = ({q0 » Qi}> b}, {a, Zq}, 8, q , Zq, 0)
Where, 8 is given below :
8(qQ, a, Zq) = (q0, a z o)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) — — ■I - ■
6-19 Pushdown Automata (PDA)
Scanned by CamScanner
CamScanner
g Theory of Q P ci (MU-Sem 5-CQmp.) 6-20 Pushdown Automata (PDA)
Solution :
Algorithm :
1. Foi eveiy input symbol ‘a’, a symbol x is pushed onto the stack.
2. Foi eveiy input symbol ‘b’, a symbol x is pushed onto the stack.
3. Foi eveiy input symbol ‘o’, x is erased from the stack.
(i) PDA accepting through final state is given by
M = c
( K , qi,q 2 >q3 M a A ), {X, z0 ),q 0 , z 0, 6, {q3 ))>
Where the transition function 5 is :
1. 3(q 0 , a, z0 ) = (q0 , xz0) [x is pushed for the first a]
2. 8(q 0 > a, x) = (q0 , xx) [ x is pushed for every subsequent a]
3. 8(q 0 , b, z0 ) = (q p xz0 ) [First b without a’s]
4. 8(q 0 , b, x) = (q p xx) . [First b after a’s]
5. 3(q P b, x) = (q p xx) [Subsequent b’s]
6. 8(q P c, x) = (q2, e). [x is erased for the first c]
7. 8(q 2, c, x) = (q2, s) [x is erased for subsequent c’s]
8. S(q 2, e, z0) = (q3, i 0 ) [Accept through q3 ]
9. 8(q 0 , E, z0 ) = (q3, Zq) [Accept a null string]
Scanned by CamScanner
V Theory of Comp, Scl. (MU-Sem. 5-Comp,) G-21 Pushdown Automata (PDA)
Solution :
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 first n input symbols.
• Status of the stack and state of the machine is shown in Fig. Ex. 6.4. 11. Input applied is
abbebba.
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Coinp.) 6-22 p u sh d own Automata (PDA)
Input
b c
bl I b
b b
Stack
State
Ao % % <11 <11 Al Al
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-23 Pushdown Automata (PDA)
In every transition of DFA, a stack operation Zq/z0 is added. z0 /z0 implies no-stack
operation.
The PDA accepting strings ending in abb through final state is given by :
M
= ( {Qo’ Qi’ 2’ As)’ ( a > b )>{zol.8, q0 , z 0> {q3 })
Where the transition function 5 is given below,
8(qo< b, z0 ) = (q0, z0) •
8(Qq* a * (qp z0 ) -
8(q 1; a, Zo) = (q p Zo) ■
8(q r b, z0 ) = (q2, z0 )
S(q2 > a, Zg) = (q p Zg)
8(q 2, b, z0 ) = (q3, z0 )
3(q r a,Zo) = (q p Zq)
S(q3, b, z0 ) = (q0 , z 0 )
o>
2. 8(q 0, ( , ( ) = (q 0, ( ( )
3. 8(q 0, ( , [ ) = (q 0, ( [ )
Scanned by CamScanner
Pushdown Automata (PDA)
4. 5(q 0 > Zq) = (q0 . [zq) Push the opening bracket ‘[’
5. 8(q 0 , [, ( ) = (q > [( )
0
6. 5(q 0 . [. [ ) — (q > it )
0
8. 8(q 0 , ], ] ) . = (q 0. e)
Solution :
1. Algorithm :
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-25 Pushdown Automata (PDA)
8 ( q0 , l , x ) = ( q „ x x)
8 (q„ 1, x) = (q h x x)
8 ( q „ 0 , x) = (q2, e )
8 ( q 2, 0, x) = (q 2, e )
8 (q2, 6 , Zo) = (q2, e ) [Accept through an empty stack]
Example 6.4.15 : Define PDA and construct PDA for the grammar :
h E -» E + E I E - E l (E) I id MU - Dec. 2011
Solution :
The equivalent PDA, M is given by :
M = ({q}, { + , - , ( , ) , id}, { + , - , ( , ) , id, E}, 8, q, E, <|))
Where 8 is given by :
8 (q, e , E) = {(q, E + E), (q, E - E), (q, (E)), (q, id)}
8 (q, +, +) = {(q, £ ) )
- .. 8 (q, -, -) = {(q, e ) }
8(q,(,() = {(q,e)} '
8 (q, ), ) ) - { (q, e ) }
8 (q, id, id) = {(q, e ) }
Scanned by CamScanner
Theory of Comp, Sei, (MU-Sem. 5-Comp.) 6-26 Pushdown Automata (PDA)
M U - Dec. 2007
Solution :
There is no way of finding the middle position of a string by a PDA, therefore the
middle position is fixed non-deterministically.
1. First n characters are pushed onto the stack, where n is non-deterministic.
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 :
8(q 0, a, e) => (q0, a)
8(q0 , b, e) => (q0, b)
(b) It is a middle character - go for matching of second half with the first half.
8(q0, a, e) => (q p e)
5(q0, b, £) => (q p 6)
The status of the stack and the state of the machine is shown in the Fig. Ex. 6.5.1. Input
applied is ababa.
• Left child -> current input is taken as the middle character
• Right child -> cunent input is not a middle character.
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-27 Pushdown Automata (PDA)
z
o
a
After first z
o
input 'a' Ho
b
After second a
input b z
Hi o
a
After third ni b
input a a
z
o q0
T
a
After fourth z b
o:
input b Al a
b
a
ni z
o no
After fifth :
a
input a b
a
. O'
ni
fail
Fig. Ex. 6.5.1 : Processing of string by the PDA. String is taken as “ababa"
L> e - indicates that irrespective of the current stack symbol, perform the
transition.
b, e) => {(q p e), (q0 , b)}
8(q p a, a) => {(q P e)J
8(q p b, b) => {(q p e))
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-28 Pushdown Automata (PDA)
Starting state = q 0
Initial stack symbol = z0
Example 6.5.2 : Design a PDA for detection of even palindrome over {a, b}.
MU - Dec. 2005, May 2006, May 2007, May 2016
Solution :
|a b a [ [aa |
R AR AR
w W W WWW
There is no way of finding the middle position by a PDA; therefore the middle position
is fixed non-deterministically.
1. First n characters are pushed onto the stack, n is non-deterministic.
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, whose
previous character is same as itself should be considered for two cases :
(a) It is first character of the second half.
- Pop the current stack symbol using the transitions :
5(q 0 , a, a) => (q p e)
§
(q0 >
Scanned by CamScanner
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-29 Pushdown Automata (PDA)
Scanned by CamScanner
Theory of Comp, Sei, (MU-Sem. 5-Comp.) 6-30 Pushdown Automata (PDA)
Input
'a' is part
of w
The transition rule for 8(q 0, a, a), must consider the three cases :
1. Input ‘a’ is part of w of the palindrome.
2. Input ‘a’ is middle character of wawR
3. Input ‘a’ is the first character of wR.
The transaction, 8(q 0 ,a,b) => { (q0> ab), (q v b) }
Scanned by CamScanner
'r Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-31 Pushdown Automata (PDA)
Machine has to non-deterministically figure out which of the two conditions will be
satisfied by the string :
1. i j, by entering the state qj on first input.
2. j k, by entering the state q 2 on first input.
8(q 0 , a, z0 ) = { (q p aa), (q2 , z 0 )}
In state
8(q 2* b, q) = { (q 4, bZg)}
8(q 4 , b, b) = ((q 4, bb)J
8(q 4, c, b) = f(q4 , e))
8(q 4 > e. b) = ( (q r > e) }
8(q f , b , e) = ((q f , e)}
. ■
CamScanner
Scanned by CamScanner
Theory of Comp. Scj._(MU-Sem, 5-Comp.) 6-32 Pushdown Automata (PDA)
\ PDA by PDA by
empty:
\ stack
From a given CFG G = (V, T, P, S), we can construct a PDA, M that simulates the
leftmost derivation of G . .
The PDA accepting L(G) by empty stack is given by :
M = ({q}, T, V u T , 8, q, S, 0) [M is a PD A for L(G)]
Where 8 is defined by :
1. For each variable A G V, include a transition,
8(q, e, A) =>{(q, a) I A —> a is a production in G }
2. For each terminal a g T, include a transition
8(q, a, a) =>((q, £))
CamScanner
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6J33 P hdown Automat PDA)
B->0S I IS I 0 .
I
i Test if 0 1 0 is in the language MU - May 2006, May 2 0 1 1 , May 2012
Scanned by CamScanner
CamScanner
V Theory of Comp. Scl. (MU-Sem, 5-Comp.) 6-34 Pi ighrlown Automata (PDA)
8(q, 0, 0) = (q,.E)
__________ — > (q, 000, BBB)
8(q, e, B) = (q, 0)
________— > (q, 000, OBB)
8(q, 0, 0) = (q, 8)
> (q, 00, BB)
8(q, e, B) = (q, 0)
______________ > (q, 00, OB)
8(q, 0, 0) = (q, 8)
---------- -------------------------------> (q, 8, 8)
Scanned by CamScanner
CamScanner
y Theory of Comp. Sci, (MU-Sem. 5-Comp.) 6-35 Pushdown Automata (PDA)
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-36 Pushdown Automata (PDA)
8(q, e, S) = (q, 4)
-----------------------------------> (q, 4, 4)
8(q, 4, 4) = (q, e)
-----------------------------------> (q, e, 6)
Example 6.6.6 : Convert the following expression grammar to PDA
I —» a I b I la I lb 110 I II
E 1 1 E * E I E * E I (E) M U - Dec. 2008
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-37 Pushdown Automata (PDA)
<Xj = Xj pj , State of
the PDA = q
Base case : i = 0 :
Scanned by CamScanner
CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 6-38 Pushdown Automata(PDA)
Induction step :
a s0,
We have to show that the correspondence is preserved for j - i + I
correspondence for otj is assumed.
Without the loss of generality, we can assume that the given grammar is in GNF.
If (i + I)th input symbol is a{ + ( and the first variable of ft is Vj then the top of the sta
contains Vj. The variable Vj can be expanded using the production.
Vi => a i + i y
Similarly, the top symbol of the stack can be replaced with a i + j y and then a, + j can b e
popped out using the transition
S (q, ai + i, a i + i) => (q, £)
Thus, there is one-to-one correspondence between the strings otj and the configuration of
PDA. Thus a string w G L(G) will be accepted by the PDA M.
6.6.2 Construction of CFG from PDA
We can find the Context Free Grammar G for any PDA, M such that
L(G) = L(M)
i.e., we can construct an equivalent CFG for a PDA. ''
The variables of the CFG, so constructed will be of the form :
[pXq], where p, q G Q and X G T ■-
Let the PDA is given by : . .. \ -
M = (Q, X, T, 5, q0 , z, 0) . -
Where, z is the initial stack symbol.
Then an equivalent CFG is given by
G = (V, Z, P, S) where . / .
V = {S, [pxq] Ip, q e Q a n d X G T}
ar,
Example : If Q = {Qo» Qi) d F = (a « ft z} then the possible set of variables in the
corresponding CFG is given by :
1. S
2. [q o a q o l . h o a
qi)>[qi a(
Jo].[qi a qi]
b
3- [q o q o i.[qo b q i M q i b
qoi*[qi b
qii
4
- [qo z qo].fao z
qil-[qi z
qo].[qi z
qi]
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-39 Pushdown Automata (PDA)
[q t q] - > a [ q j q]
3. For each transition of the form
SCq,, a, B) => (q? C f C 2)
Where,
(a) q,. qj G Q
(b) a belongs to (E u e)
(c) B, Cj and C 2 belongs to r ‘ .
then for each p l t p 2 G Q, we add the production
B C2
[q, pj a p 2] [p 2 ' p j
Scanned by CamScanner
CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-40 Pushdown Automata (PDA)
[q Z q ] - + 1 [q x
p] [p Z q]
Z x
[q Pl —> 1 [q q] [q Z p]
[q Z Pl —> 1 [q X
p] [p z p]
Step 3 : Add productions For 8(q, 1, x) => {(q, xx) }
X X
[q q] —> 1 [q q] [q x q]
[q x ql -> i [q x
p] [p x q]
X X
[q p]->l[q q] [q X p]
x x
[q p] —> i [q p] [p x p]
Step 4 : Add productions for 8(q, e, x) {(q, e)}
x
[q q] —■> e
Step 5 : Add productions for 8(q, 0, x) => {(p, x)}
X X
[q q] 0[p q]
X X
[q p]-»0[p p]
Step 6 : Add productions for 8(p, 1, x)=> {(p, £))
[p X p] —> 1
Step 7 : Add productions for 8(p, 0, z) => {(q, z)}
Z
[p q] -> 0 [q Z q]
[p Z p] —> 0 [q Z p]
: Renaming of variables :
BM.Newname
A
N
B
&
1
|
C
N
*T5
I
1
1
D
N
C.
2*
1
1
1
E
X
wD
F
*
IdD
1
1
1
Scanned by CamScanner
<ar
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-41 Pushdown Automata (PDA)
x G
[p q]
x H
[p p]
The set of productions can be written as :
S-»AIB
A 1EA I 1FC
B —> 1EB I 1FD
E—> IEE I 1FG
F - » 1EFI 1FH
E —> 8
E- OG
F—» 0 H -(/
C—» 0 A
D—>0B
Step 9 : Simplification of grammar
Symbol G does not come on the left side of the production, hence it can be eliminated.
The equivalent set of productions is :
S—» A I B
A -» 1EA I 1FC
B —> 1EB I 1FD
E-»lEEIe
F - > 1EFI 1 F H I 0 H
H—> 1
C—>0A
D—>0B
xz(
Scanned by CamScanner
CamScanner
V Theory of Comp, Scl, (MU-Sern. 5-Comp.) 6-42 Pushdown Automata (PDA)
Solution :
Step 1 : Add productions for the start symbol
s 0
—> [qo q<>]
z
s
-> Cq0 ° q i ]
Step 2 . Add productions for 8(q 0 , 1, z 0 ) = { (q0 , xZq))
[qo Z ° q 0] 1 [q 0 X
q 0 ] [q 0 Z ° q 0 ]
0 x
[qo qol —> i [q 0 q i] fai z ° q 0 ]
0 x
[q 0 q d —> i [q 0 qo] [q« Z ° q j
Z v 7 *
[qo 0 qJ -» i Iq0 q j [qi 0 q j
r
• . . • ( . | / * ♦. . L <•
Step 3 : Add productions for 8(q 0 , 1, x) => {(q0 , xx))
[qo x qJ -> i [q 0 x
qo l [q 0 x q J _ ../ j —
x X
[q 0 qj 1 [q 0 q,] [q, x q,]
Step 4 : Add productions for 8(q 0 , 0, x) => {(q b x)}
[qo x q J -» 0
[qi x
qJ
Step 5 : Add productions for 8(q 0, e, z0 ) = {(q p e)}
r
• ■ ' ■ ’ ' •
0 e
[q 0 qd —* ' ;■■■ ■■■ • -
Step 6 : Add production for 8 , 1, x) => {(q,, e)}
x 1
■ [qi qJ ->
Step 7 : Add productions for 8(q„ 0, z0 ) => { (q0 , Zo)}
7
[qi ° q o ] =: > 0 [qo fl®]
[q ( z ° q J => 0 0 z ° qd ' ■■
> ■ i -
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-43
1'
Solution :
Step 1 : Add productions for the start symbol.
[qo a q i ] - > a [ q 0
a
q 0 ] [ q o a qi]
Fq0 a q J —» a too a q d [ q / q J
Step 3 : Add productions for 8(q 0, b, a) = {(q b £)}
Scanned by CamScanner
Theory _o[Comp 1 _Sci. (MU-Sem. 5-Comp.) 6-44 Pushdown Automata (PDA)
Solution :_________
'Sr.No _____- PDA transition Corresponding productions
1. Productions due to start symbol S. Zn ,
s —> [q0 Qq]
Zn .
S - > [q0 q j
2. S(q 0 » £, Zo) = (q b 6)
[qp q J —> £ ___________________
3. 8(q 0. 0, Zq) = (q0 , Ozo)
[q0 z ° qo l -> 0
(qo 0 qoJ [qo 0
qol
[q0 0 qJ i tq0 1 qo l [q 0 ° q j
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-45 Pushdown Automata (PDA)
Simplification of grammar :
With the above substitutions, the resulting set of productions can be written as :
S-> AIB
B->e
A -> OEA1I OFC • . •
B -> OEB I OFD
E —> OEE I OFG
F—>0EFI0FH -
, E —> 1IE I 1JG
;
F—» 1 I F I U H ‘ :
1
I -> III I UK . '
J -» I D I 1JL
J-»0 ■
L —> 0 t
H-»0
D —> E '
1. Removing E-productions :
Nullable set = {D, B, S )
E-productions are removed with resulting set of productions as given below :
S—> A I B ■.
• A -> OEA I OFC
: • .• • ‘ • i '
B -» OEB I OFD I OE I OF
E -> OEE I OFG I HE 11JG
F - » 0 E F I 0 F H I 1IFI 1JH
H->0
I-> i n I UK
J IDIULIO
L 0
Scanned by CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 6-46 Pushdown Automata (PDA)
B-»0F
F —> OFH I 1JH
H—» 0
J -> 1JL I 0
L —> 0 * Fig. Ex. 6.6.11
3. The unit production S —> B should be removed. The set of productions after elimination
• of the unit production S B is given below :
S -> OF
F —» OFH I UH
H—» 0
1JLI0 '
2. Sequence of input b’s should be skipped. These b’s will have no effect on the stack.
5(q 0 ,b,a) = (q p a)
8(q p b,a) = (q p a)
3. Initial a’s which are on the stack should be matched with the trailing a’s in the input. An
‘a* should be popped for every ‘a’ as input till the end of input.
8(q p a,a) = (q 2, e)
Scanned by CamScanner
CamScanner
Wgr
Theory of Comp. Scl. (MU-Sem, 5-Comp.) 6-47 Pushdown Automata (PDA)
6(q 2,a,a) = (q 2, e)
4. Finally, the symbol z0 should be popped but to make the stack empty.
S(q 2, e,z 0 ) = (q 2, e)
Transition diagram and the transition table are given below.
a t ZQ/az0 a,a/e
a a
_b,a/a
S-Hq qo]
S-Hq qJ
s-nq qj
Step 2 : Add productions for 8(q 0 ,a,z0 ) = (q Q,az0 )
[q 0 20 qj -> a
fa© a [q0 20 q0 ] , .
[q 0 20 q©! a
too a q 2 J
[q qj- atq/qj [q qj , .
>a
[qo qj - [qo a qJ fai qj ■ . •
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (h1U-Sem 5 Com <n_
tq
oZ °‘li]- >
a[q o
a
q 2 ] [q qj
z
tqo l - a f q / q j [q 2 Z ° q 2]
Step 3 : Add productions for 8(q 0 ,a,a) = (q0 ,aa)
fa/qj a [ qo a q 1] [q/qj
[q 0 a q 2l a [ q0 a q 2] [q 2 a q 2]
Step 4 : Add productions for 8(q 0,b,a) = (q p a)
[qo a q o l -* b
lqi a qol
[q/qJ btq/qi]
[qi a q o l -» b t q / q o ]
[qi a q 2] b f q qj
Step 6 : Add productions for 8(q p a,a) = (q2,e)
[qi a q 2i a
[q 2 a q 2J -* a
Scanned by CamScanner
V Theory of Comp. Scl. (MU-Som. 5-Comp.) 6-49 ____________ Pushdown Automata (PDA)
Example 6.6.13 : Design a PDA and then corresponding CFG for the language that accepts
the simple palindrome.
L = {xcxR I x g {a,b}*}
Solution :
Transitions for the PDA are given by :
8(q 0 , a, z) = (q0 , az)
8(q 0 , b, z) = (q0> bz)
6(q 0> c, z) = (q 0, e)
8(q 0 , a, a) = (q0 , aa)
8(q 0 , a, b) = (q 0 , ab)
8(q 0 , b, a) = (q 0 , ba)
8(q 0 , b. b) = (q,j, bb) :
8(q 0 , c, a) = (q p a)
8(q 0 , c, b) = (q b) .
8(q p a, a) = (q p e) .. '
8(q p b, b) = (q p e)
8(q p e, z) = (q p e)
• z is the initial stack symbol.
• qo is the initial state.
• String is accepted through an empty stack.
Productions for the corresponding CFG are given below :
Step 1 : Add productions for the start symbol. .
S - > [q o z q o ]
S - > [ q o z qJ
Step 2 : Add productions for 8(q 0 , a, z) = (q 0 , az)
[qo z q o ] —> a [q o a q 0 ] [q 0 Z q0 ]
IqO Z qo) —> a [q o a qj [q, Z q0 ]
Scanned by CamScanner
CamScanner
___________ Pushdown Automata_(PDA
[qo a q 0 ] -» a [q0 a q 0 ] [q 0 a q 0 ]
[qo q o l - » a [ q o q 0] [q 0 q 0 ]
[qo b qol a [ q o a q 1 ] [q ( b q 0 ]
[qo b q j a [q o a q 0 ] [q o b Q|]
[q o b q j a [q o a q j [q qj
Step 6 : Add productions for 8(q 0 , b, a) = (q 0 , ba)
[qo a Qol b
[Qo b Qo] [Qo a Qo]
[Qo a Qo] “ > b (Qo Qi] [Qi Qo] > . •<;{.; ' !
- •<.
[q o a q o ] ~» c [Qi Qo]
[qo iW qJ
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-51 Pushdown Automata (PDA)
[qo q o ] - > c [ q i q o]
r b 1 cr
b ,
[q0 q J -* fai qJ
a
fai QiJ “> a
b
[qi qiJ->b
Step 12 : Add production for 8(q p e,z) = (q p E)
r z . . J , .
[qi qJ —> £
Step 13 : Add productions for 8(q 0 ,b,z) = (q 0,bz)
z b
b z
[q 0 qd -> lqo qol too qol
[q o z q o l b r qo b qi i [ qi Z
q0] ’ - ; :
■
s
s
■
*
1 z
0
s —> [qo qj
Scanned by CamScanner
Scanned by CamScanner
o-uomp.) 6-52 __________________ Pushdown Automata
Q'n Mn
— _ _ _PDA transition CoirospondinH productions
2. S (q0 , 1, z0 ) = (q 0 , xz0 )
[q» z ° qJ -* 1
foo x qo] [qo 0
qd
[qo z °qo] -* 1
foo x qi] foi 0 < i<J
z v Zm
[Qo ° 9i] “ * 1 Mo Qo] Mo Qi]
7 r v Z
foo 0 q J —* 1 foo q J foi 0 q J
3. 8(q 0 , 1, x) = (q 0 , xx)
[qo x qol —> 1 [qo x qo] [qo x qol
Scanned by CamScanner
Pushdown Automata (PDA)
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 6-53
E—» 0 G
F—>0H
C—» 0 A
D—>0B
1. Removing e-production.
Nullable set = (S, A)
e-productions are removed with resulting set of productions as given below .
S—> A I B
A—> 1EAI1FCI I E
B -» 1EB I 1 FD
C -> OA I 0
D—>0B
E—>1EEI 1 F G I 0 G
F —> 1EH 1 1FH I OH
H—> 0 ■ ■ . . ../■
2. Removing non-generating symbols
| C — 0, H - » 0
■ ( ) '•
F -> OH
•’ (i,O.1.AC,F,|{|
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem, 5-Comp.) 6-54 Pushdown AutomatajPDA)
A—> 1FC
C -» OA I 0
F -> 1FH I OH
H—> 0
The language accepted by the PDA is given by :
L = {1010 1 0 1n £ 1} u e
Example 6.6.15 : Show that if a language L is accepted by a PDA then there exists a CFG
generating L.
Solution :
Proof :
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-55 Pushdown Automata (PDA)
T = {a)
A = {((S, a, e), (S, a)), ((S, b, e), (S, a)), ((S, a, e), (F, e)), ((F, a, a),
(F, e)), ((F, b, a), (F, e))}
Solution :
It recognizes a string of the form (a + b) nl a(a + b) n I n < m and n, m > 0
• The middle character ‘a’ is determined non-dctcrministically.
• For every symbol from the first (a + b) m of (a + b)m a (a + b) n an ‘a’ is pushed onto the
stack using the given two moves.
((S, a, e),(S,a)),((S,b,e),(S,a))
• The middle a of the input string (a + b) ma (a + b) n takes the PDA to state F. ‘a’ is fixed
non-deterministically.
• For every symbol from (a + b) n of (a + b) m a (a + b)n an a is erased from the stack in
state F.
In a DPDA there is only one move in every situation. A DPDA is less powerful than
NPDA. Every context free language cannot be accepted by a DPDA. For example, a string of
the form ww can not be processed by a DPDA. The class of a language a DPDA can accept
lies in between a regular language and CFL.
A DPDA is defined as :
M = (Q, Z, T, 8, q 0 , z0 , F), where
8(q, a, x) has one move for any q e Q, X e T and a e Z.
6.7.1 Regular Language and DPDA
We can always design a DPDA for a regular language. A DPDA can be designed for
regular language in two steps :
Step 1 : Construct an equivalent DFA for the given regular language.
Step 2 : For every transition 8(q., a) = qj in FA (where q p qj c Q and a e E), we can write an
equivalent transition for DPDA,
8(q p a , z 0 ) = {(q., z0 )}.
The move for PDA involves neither a Push nor a Pop operation.
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-56 Pushdown Automata (PDA)
1
Fig. Ex. 6.7.1 : Construction of DFA
Step 2 : The DPDA is given by :
M = ({q0 , q t , q 2 }, {0, 1 }, (0, 1, 1, z0 }, 5, q0 , z0 , {q0 })
where the transition function 8 is :
(9o’ 0’ z o) ~~ o* z o)
(Qo’1* z o) ~~ z
o)
8(q p 0, z0 ) = (q2, z0)
(Qp 1» z0) — (q0, Zq)
( 2’ 0’ Zq) — (Qp Zq)
S(q2 » zo ) = (q2, z0 )
Example 6.7.2 : Show that if L is accepted by a PDA in which no symbols are removed from
the stack, then L is regular.
Solution : Every regular language is accepted by some FA. Every transition of an FA can be
converted into a PDA move without any push or pop operation.
Let there be a transition 8(q p a) = in the FA. Where,
q., G Q and a g X.
The FA move 8 , a) = qj can be converted into an equivalent PDA move as given
below :
8(q p a, z0) = {(q? z0 ))
This move for the PDA involves neither a Push nor a Pop operation.
Example 6.7.3 : Prove “Let L be a language accepted by deterministic PDA, then the
complement of L, can also be accepted by a DPDA”.
Solution :
Let the DPDA for the given language L is
M = (Q, S, T, 8, q 0, z0 , F)
We can construct M' from M, such that M' accepts L'.
M' = (Q, S, r, 8, q 0 , Zo , Q-F)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 6-57 Pushdown Automata (PDA)
PDA is a machine for CFL. A string belonging to a CFL can be recognized by a PDA.
PDA is extensively used for parsing. PDA is an abstract machine; it can also used for giving
proofs of lemma on CFL.
6.9 Parsing , .
We can always find a method for determining whether a particular string is generated by
a CFG. Parsing a string is nothing but finding a derivation of the string in the given grammar
G. A great deal of work has been done in finding an efficient algorithm for parsing. These
algorithms depend on specific properties of the grammar.
PDA is a machine for CFG. A PDA can be used for parsing. A PDA can be enhanced to
record its moves, so that the sequence of moves leading to an acceptance of the string can be
remembered.
Scanned by CamScanner
■ Sci. (MU-Sem. 5-Comp.) 6-58
a b
(x)
The leftmost leaf, labelled a, matches the first symbol of input string abb. Input pointer
is advanced to the next symbol b of abb.
Step 3 : Now, X is expanded using X -> ab and if it fails to generate remaining input
symbols, we backtrack and try the next production X -» b.
The first tree will not generate the input string abb. The leftmost symbol ‘a’ of subtree,
rooted at X does not match the next input symbol ‘b’. We must backtrack and try the next
. production X -> b, as shown in Fig. 6.9.2.
• A recursive-descent parser may enter an infinite loop.
CamScanner
Scanned by CamScanner
V Theory of Comp. Sei. (MU-Sem. 5-Comp.) 6-59 Pushdown Automata (PDA)
Scanned by CamScanner
'MF
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 6-60 Pushdown Automata (PDA)
//The function match ( ) will increment the current position pointer for input string by 1.
Function corresponding to T = F T,
TO
{ F();
T r ( );
}
Function corresponding to T, = *F T, I E
LO
{ if(input [position] == '*’)
( match();
F();
T
■■ t<);
}
}
... ■■
if(input[position] == ‘C’)
{ match( ); •
E();
if (input[position] == *)’)
match( );
• • •> else
error =1;
}
else
if(input [position] == a’ ] [ input [position] == ‘b’)
match( );
)
// main program
char input [30];
int error = 0, position = 0;
void main( )
{
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-61 Pushdown Automata (PDA)
In bottom up parsing, the source string is reduced to the start symbol of the grammar.
The bottom up parsing is also known as shift-reduce parsing.
• A shift reduce parser constructs a parse tree by beginning at the leaves and then working
up towards the root.
• A shift reduce parser can be constructed using a stack.
Implementation of shift reduce parser
Scanned by CamScanner
CamScanner
y Theory of Comp, Sci, (MU-Sem. 5-Comp.) 6-62 Pushdown Automata (PDA)
•
Solution :
Sr. No. Stack Input Action
1. empty id + id *id Shift
2. id + id * id Reduce by F —> id
3. F + id * id Reduce by T —> F
4. T + id * id Reduce by E —» T
5. E + id * id Shift
6. E+ id * id Shift
7. E + id * id Reduce by F id
10. E + T* id Shift
n. . E + T * id ■— Reduce by F id
13. E +T — Reduce by
14. E — Accept
Adding multiple stacks to a PDA enhances its power. A PDA with two or more stacks
can accept any language that a Turing machine can accept. A PDA with two stacks is shown in
Fig. 6.10.1.
input Accept / reject
stack stack
A *
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-63 Pushdown Automata (PDA)
A post machine can simulate functioning of a luring machine. Hence a turing machine
and a post machine have equal power.
A post machine is more powerful than a PDA. A string of the a n bnc n can be handled by a
post machine but it can not be handled by a PDA.
An NPDA is more powerful than a DPDA. A string of the form coco can be handled by
an NPDA but it cannot be handled by a DPDA.
DPDA is more powerful than FA. A string of the form a n bn can be handled by a DPDA
but it cannot be handled by FA.
Dec. 2005
May 2006
Q.5 Construct the PDA equivalent to the following context free grammar :
S -» 0BB
B OS i 1S I 0
Test whether 01 04 is in language. (Example 6.6.4) (10 Marks)
D
Q.6 Design the PDA that will recognize the language L = WW : W is in {a, b}* i.e. even
length palindrome over S = {a, b} (Example 6.5.2) (10 Marks)
Dec- 2006
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-64 Pushdown Automata (PDA)
■ .. ■.
_
May 2007
________' _______________ _
Dec. 2007
Q.10 Design the PDA to accept language containing all odd length palindromes,
S = {0, 1}. (Example 6.5.1) (10 Marks)
Q.11 Give the CFG generating the language accepted by the following PDA :
M = ({ q 0 , q,}, {0, 1), {Zo, x), 8, q0 , z 0 , 0) when 8 is given below :
8(q0 , 1 , Zq) = {(q 0 , xz0)J
8(q 0 , 1 , x) = {(q 0 , xx)}
8(q 0 , 0, x) = { ( q 1 , x ) }
8(q 0 , e, Zo) = {(q 0 , e)}
8(q1 , 1 . x ) = { ( q1 ,e)}
Sfa,, 0, Zq) = {(q0 , z 0)} (Example 6.6.9) (5 Marks)
May 2008
Dec. 2008
Q.13 Prove the variations and equivalence of the push down automata
May 2009
Q. 15 Design a PDA for CFL that checks the well formedness of parenthesis i.e. the
language L all “balanced" string of two types of parenthesis say “( )” and “[ Trace the
sequence of moves made corresponding to input string (([ ])[ ]).
(Example 6.4.13) (8 Marks)
Scanned by CamScanner
V Theory of Comp. Scl. (MU-Sem. 5-Comp.) 6-65 Pushdown Automata (PDA)
Dec. 2009
May 2010
May 2011
Pec- 2011 |
Scanned by CamScanner
y Theory of Comp. Sci; (Mu . Seni 5 . Comp ) 6 Pushdown Automata (PDA)
Ma
I y 2012
Q. 26 Design PDA to accept each of the following languages
' mn ’ n -
m
(Example 6.4.10, Refer 0 as a and 1 as b)
(ii) (0 1 0 0 I m , n i 1) (Example 6.4.14) (10 Marks)
Dec. 2012
Q. 28 Explain PDA and power of PDM. (Sections 6.1 and 6.12) (10 Marks)
Q. 33 Design PDA that checks for well-formed parentheses. (Example 6.4.13) (10 Marks)
Transitions :
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-67 Pushdown Automata (PDA)
01 f [ 10 | , t0 1 0| }0 1 Op |Q0| [00 |
A R 4 R AR
ww wTw wTw
Centre Centre Centre
There is no way of finding the middle position by a PDA; therefore the middle position
is fixed non-deterministically.
1. First n characters are pushed onto the stack, n is non-deterministic.
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, whose
previous character is same as itself should be considered for two cases :
(a) It is first character of the second half.
- Pop the current stack symbol using the transitions :
SCqo, 0, 0)=> (q p e)
5(q 0 , t l ) = > (q,,E)
L— Must be identical
(b) It belongs to first half.
- Push the current input
8(q 0 , 0, e)=>(q0 , 0) ■
8(q 0 , 1, e)=>(q0 , 1)
Scanned by CamScanner
Theory of Comp, Sei, (MU-Sem. 5-Comp.) 6-68 Pushdown Automata (PDA)
May 2015
Q. 36 Construct PDA accepting the language L =.{a n ,b n ln > 0} (Section 6.1) (10 Marks)
Q. 37 Write short note on : Difference between PDA and NPDA. (Section 6.5) (5 Marks)
Dec. 2015
Scanned by CamScanner
CamScanner
>r Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-69 Pushdown Automata (PDA)
Transitions
|j| May 2 0 1 6 I
Q. 39 Design PDA to check even palindrome over L = (0, 1} (Example 6.5.2) (10 Marks)
Dec. 2016 I
Scanned by CamScanner
CamScanner
$ Theory of Comp. Sei, (MU-Sem, 5-Comp.) 6-70
Chapter Ends. . . .
□ □□
Scanned by CamScanner
Turing Machine (TM)
Module 5
2. Pushdown machine
3. Post machine
These machines have no control over the input and they cannot modify their own inputs.
Turing machine is a writing machine; it can modify its own input symbols. Turing machine is
more powerful than a pushdown machine. Power of various machines is shown below :
■ ti , _ ‘ .
Scanned by CamScanner
CamScanner
V Theory of Comp, Sci.(MU-Som.5-Comp ) 7-2 Turing Machine (TM|
• The tape is divided into squares. Each square holds a single symbol.
Initially the tape contains two finite blocks of 1’s separated by finite block of
blanks. The machine should delete the block of blanks between two blocks
of 1’s.
Solution :
Let us call the first block of 1’s as and the second block of 1’s as cd2 .
There are three blanks between and co2 . These three blanks will be deleted in three
cycle.
Each cycle will consist of following steps :
Step 2 : Head moves to first ‘B* between (Dj and co2 and replaces it by 1 .
Step 3 • If some B’s are yet to be deleted then head comes back to first 1 of
These cycles of computation are shown in Fig. Ex. 7.1.1(c).
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-3 Turing Machine (TM)
11 u n i 1
The transition behaviour of the taring machine is shown in Fig. Ex. 7.1.1(d).
1/B.R B/1JR.
— ►—
B/B,L
B/B3
1/1, L
>5
Arc between q 0 and qj is marked as (1/B, R). It implies that the square being scanned
contains 1 and it will be replaced by B, then the head will move to right cell.
1/B
head moves to
Input replacement right cell
symbol symbol ,
Scanned by CamScanner
y Theory of Comp Sci. (MU-Sem. 5-Comp.) Turing Machine (TM)
7-4
B B 1 1 1 1 B B B 1 1 1 B B
1 1
B/1»R
B B B 1 1 I 1 B B 1 1 IBB- [Replace first B
V' j
with 1 and move
right]
B B B 1 1 1 1 B B 1 1 1 B B [skip B and
B/B,L
go to left
cell]
<13
[On first B
B B B 1 1 1 1 B B 1 1 1 B B
before 1 ’s make
a right move]
B/B,R
Scanned by CamScanner
Theory of Comp. Scl. (MU-Som. 5-Comp.) 7-5 Turing Machine (TM)
Example 7.2.1 : Design a turing machine that erases all non-blank symbols on the tape,
where the sequence of non-blank symbols does not contain any blank
symbol B in-between.
Solution :
The turing machine M is given by :
M = (Q, Z, r, 8, q 0 , B, F)
where
Q = {q0 >qib
Z = {a, b ) ,
r = {a, b, B ) , . ;
q 0 = is the initial state,
B = is blank symbol.
F = {q j } is a final state or ‘Halt state’.
The transition function 8 is defined below :
S(qo, a ) = (qo> R) [erase *a’ and move right]
b) = (q0 , B, R) [erase ‘b’ and move right]
8(q 0 , B) = (q b B, N) [stop in state qj
The transition function can also be given in a tabular form as shown in Fig. Ex. 7.2.1(a).
__________a ________b B
->q 0 (qo»B,R) (qo,B.R) (q b B,N)
*x»
Scanned by CamScanner
7-6
Turing Machine (TM)
a/B,R
b/B,R
I B/BJxT
MU - D e c . 2009, May 2 0 1 1 , M a y 2 0 1 3
Solution :
3 3
Let us try to understand the design process with the help of the input string a b .
There are three a’s and three b’s. Three a’s will be matched with three b’s in three
0
a ovf w a b | b| y| b| :a l b H 2 nd Cycle
>0 b
HTFTx Ia I b
I y
I y I B| I B] x | X y 7.1 a 3 rd Cycle
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-7 Turing Machine (TM)
a b x y B
(qi. x, R) — (q 4> y. N) —
<£ (Qi, a, R) (q„ b, R) (q 2 , y, L) (q 2 > B, L)
91 (q 3, y, L) ■ ■— ' — . —
93 fe. a, L) (q 3. b, L) (q 0 , x, R) —
q4 94 94 94 94 q4 <— Halting state
Fig. Ex. 7.2.2(c) : Transition table
Meaning of Various States :
q0 - Leftmost a is replaced by x.
9i - State q! locates the last b by skipping a’s and b’s and taking a left turn on first
:
B ory. ? ■ • ,•
q2 - Rightmost b is replaced by y.
93 - State q3 locates the first a by skipping a’s and b’s while moving left and taking a
right turn on first x from right side and entering state q 0 .
q4 - It is halt state for a valid string.
Acceptance :
In case of a valid string, the head will be exposed to y in state q 0 .
Rejections:
1. An input symbol ‘b* in state q 0 implies that number of b’s are more than a’s.
2. An input symbol ‘a’ in state q 2 implies that number of a’s are more than b’s.
Example 7.2.3 : Draw a transition diagram for a turing machine accepting the following
language. L = { a b I i < J)
Solution : Solution to this example follows from Ex. 7.2.2. In Fig. 7.2.2(b), if the machine
finds a string of only b’s in state q 0 then number of a’s is less than number of b’s in a' b*.
Scanned by CamScanner
V Theory of Comp, Sci. (MU-Sem. 5-Comp.) 7-8 Turing Machine (TM)
a/a
Ob/b, R
a/x, R W B/B, L
yq)— *— ---------*—
y/y. l
v b/b, R b/y, L
x/x, R
b/b, L
b/b, R a/a, L
v y/y, n
B/B, N
Halt state
Examole
P
7.2.4 A turing machine is represented in Fig. Ex. 7.2.4 show the processing
' sequence for the input string aaabbb.
' b/b,R
a/x,R b/b>l zTX
y/yj,”.
b/yJL
vy/yJ4
x/x,R
b/b J,
a/ajy
Scanned by CamScanner
Turing Machine (TM)
7 Theory of Comp. Sci . (MU-Sem. 5-Comp.) 7-9
Solution : a a a b b b B |- x a a b b b B j - x a a b b b B
c
q0 l* D
|-xaabbbB|-xaabbbB|-xaabbbB
q3 q3 q3
|-xaabb yB|-xaabbyB
q3 qo
|-xxabb yB[-xxabbyB|-xxabbyB
•. t t ■ t
qi qi qi
|-xxabbyB|-x x a b by B |-xx aby y B
qi q2 • •
|-xxabyyB|-xxabyyB|-xxabyyB
q? q3 qo • ■
|-xxxbyyB|-xxxbyyB|-xxxbyyB
qi
q0
[ Accept]
Scanned by CamScanner
Theory of Comp. Sei. (MU-Sem. 5-Comp.) 7-10 Turing Machine (TM)
Solution :
1) 110B: B1 1 OB H B01 0B [ As per (So , 1 , O,S0 ,L) ]
So So
PBB010B
on the left then the tape head
halt with a crash ]
Halt
2) 0011B :
B O O 1 1 B H- B 0 0 1 1 B [ As per ( So , 0, 0,S.| ,L) ]
s0 s1
The machine will fail at this point. There is no transition for input B in state Sj
3) 0101B :
B0101BF B001 1 B [ As per ( So , 0, 0,S 1 ,L) ]
So Si
This machine will fail at this point. There is no transition for input B in state S
Example 7.2.6 : Design a right shifting taring machine over alphabet {0,1}
Solution :
A right shifting turing machine will shift the input string, right by 1 place.
Algorithm :
Right shifting should start from the rightmost character. Each character is shifted right
starting from right end and working towards left end.
Cycles of computation for an input string 1010 are shown in Fig. Ex. 7.2.6 :
[ starting from the
initial character,
the head moves to
last character]
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-1 1 Turing Machine (TM)
B/B,L
1/B,R B/1JL
B/BX
v B/B,N
0 ________1 B
(q o ,O,R) (q 0 ,l,R) (qi.B.L)
' ■ q2 . - ; (q4.0,L) s
<13 (qU’L)
}l
- I D T<
(qi,B,L)
q4
*
q5 Os Os <15
Fig. Ex. 7.2.6(b) : Transition table
Scanned by CamScanner
CamScanner
y T h s o r y of C o m p , Sci. ( M U - S e m . 5-Comp.) 7-12 Turing M a c h i n e (TM)
<14
0 1 --- J
J ----L_
b| 1 | i | 0 | 0 | #
Input
' Output
Solution : « ’' . I r - . • t ..
1/ljt
Qo/O,R
0/xJ<
S) — t B/0,L
g|r\#/#,L
.% i/ix
B/1,L 0/0,L
l/y,R
1/1 ,R
O/OJt >
v
x/0»R
y/ljt
< i
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-13 Turing Machine (TM)
0 1 # B X y
(q 2.y.R) (q 4 ,#.N) — — —
Qo (qi.x.R)
(q 3 ,0,L) — —
q t (qi.o.R) (qi.l.R) (qi,#.R)
q 2 (q 2,0,R) (q 2, 1.R) (q 2># R) (q 3 .i.L) — —■
q0 91 Qi Qi Qi
|-B x 1 1 # 0 | - B x 1 1 # 0 H B x l l # 0 [ - B x l l # 0
T t t .... ?
93 93 91■ ' 93
|-B 0 1 1 # 0 | - B 0 y # 0 | - B Oy 1 # 0 p B Oy 1 # 0 B
c
9o 92 2 9i
| - B Oy 1 # OB | - B Oy 1 # 0 1 | - B 0 y 1 # 0 1
92 93 93
|-B0yl#01 | - B 0 y l # 0 1 | - B 0 11 # 01 B01 y# 0 1B
qj 93 9o 92
|-B01y#01B |-B0 1y #01B|-B01y#01B
t T ?
92 92 92
|-B01y#011 {-BOly #01 1 {-BOly #01 1
93 93 93
f-B01y#011 |— B 0 1 1 # 0 1 1 I - B 0 1 1 # 0 1 1
T T ?
9s 9o 94
Fig. Ex. 7.2.7(c)
Scanned by CamScanner
Turing Machine (TM)
Theory of C o m p . Sci. (MU-Sem. 5-Comp.) 7-14
head
Step 1 : Head is moved to the rightmost symbol
head
Step 2 : We create another copy of aba in the reverse order. It will require 3 cycles as the
length of aba is 3. In each cycle, the input symbol ‘a’ and b are renamed as x.
Step 3 :
st v>y'.W
After 1 cycle B b
After 2 cycle x a
After 3 cycle x
x/B,R x/x,L
a/a.R
b/b,R
. z. \a/a,N x/x,R
b/b,N \
He
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-15 Turing Machine (TM)
B a b b B l - B a b b B I- B a b b B l - B a b b B l - B a b b B
t T T T T
Qo <lo <lo Ho , 3i
l-BabxB l-BabxbB I- B a b x b B |- B a x x b B
T T f T
<13 <14 ' <11 33
|- B a x x b B |- B a x x b B |- B a x x b b B | - B a x x b b B
T t T T
<13 33 <14 34
|- B x x x b b a B (-BxxxbbaB |- B x x x b b a B
T ' . T ■' f
34 , 34 . ..... ,.3i
I- B x x x b b a B l - B x x x b b a B 1-BxxxbbaB
T . t T
3i 3i <b
l-BB BBbb aB
t
q6 - Halt
Fig. Ex. 7.2.8(a)
Scanned by CamScanner
Theor of c
V y °rnp. Sci. (MU-Sem. 5-Comp.) 7-16 Turing Machine (TM)
x/x,R
b/x,L
b/bjy
a/x,L x/x>L
b/x,R b/b,R
x/x,R
B/BJR
vB/B,N
a b X B
—>
Qo (QiXR) (q 2 ,X,R) (q 0 ,X,R) (q 4 ,B,N)
qo
|-BxxbaB|-BxxbaB|-BxxbaB|-BxxxaB
q0 Qo qo q2
Fig. Ex. 7.2.9(c) Contd,.,.
Scanned
Scanned by
by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-17 Turing Machine (TM)
|-BxxxxB|-BxxxxB|-BxxxxB|-BxxxxB
T c
T T T
q.i h q.i qi
|-BxxxxB|-BxxxxB|-BxxxxB|-BxxxxB
f T t T
q0 qo qo
|- B x x x x B |- B x x x x B
Ml Ma 012 a 015 a 0
Solution :
In each cycle, the left-most *)’ is written as X, then the head moves left to locate the
nearer ‘(’ and it is changed to X .
The cycles of computation are shown below.
Input string is assumed to be (00)0-
Cycle No. Tape
f
Initial ■ B (00)0 B
1. • • B(XX())()B
2. B (XXXX)() B
3. B XXXXXXO B
4. B XXXXXXXX B .
x/x L ____(
)/xJ> Qi (q 0 .GR) (q h x,L) (q 0 ,x,R) (q 2>B,L)
q0
Qi (qo-x >R) (q t ,x,L)
q2 (q 2 ,x,L) (q3,B,R)
*
q3 q3 11
— --------M Halting
/
B/B.R state
Halt state
Fig. Ex. 7.2.10(a) : State transition diagram Fig. Ex. 7.2.10(b) : State transition table
Scanned by CamScanner
Theory of Comp. Sci (MU-Sem. 5-Comp.) 7-16 Turing Machine (TM)
,0
Bxxxxxx()B|-Bxxxxxx()B|-Bxxxxxx()B
t T T
qo qo ■ <10
BxxxxxxxxB|-BxxxxxxxxB
q0 Jo
Fig. Ex. 7.2.10(c) Contd...
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-1 9
| - B x x x x x x x x B | - B x x x x x x x x B |- B x x x x x x x x B
q 2 Q2 q2
|-BxxxxxxxxB|-BxxxxxxxxB|-BxxxxxxxxB
T t T ,
Q2 2 42
|-BxxxxxxxxB|-BxxxxxxxxB|-BxxxxxxxxB
c C
Q2 2 2 • .
l-BxxxxxxxxB
q3
Fig. Ex. 7.2.10(c)
b
a a
b/b>R
b/bX
Fig. Ex. 7.2.11(b) : TM obtained from the DFA of Fig. Ex. 7.2.11(a)
The Turing machine M is given by :
M = (Q, S, r, 5, q0 , B, F)
Where, Q = {q0 , q b q2, q3 )
;
S = (a,b) ' -s’
r = {a,b,B)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-20 Turing Machine (TM)
a/aj<
b/b,R b/a,L
a/a>R
— ►— Qi)— >- 3'
a/a,R a/aj<
b/a ,L
a/a,R
B/B,N
B/B,N
B/B,N
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 7-21 Turing MachinejTM)
BbbabbaabbabB|-BbbabbaabbabB|-BbbabbaabbabB
qo Co co -
|— B b b a b b a a b b a b B | - B b b a b b a a b b a b B | — BbbabaaabbabB
c
i c2 ' Q3
|-BbbaaaaabbabB|-BbbbaaaabbabB
£ Q5
|-BbbbaaaabbabB|-BbbbaaaabbabB|-BbbbaaaabbabB
t - T "
06 Ao - , Ai
|-BbbbaaaabbabB|-BbbbaaaabbabB
|-BbbbaaaabaabB|-BbbbaaaaaaabB
|-Bbbbaaab aaabB|-Bbbbaaabaaab B
q5 q6
|-Bbbbaaab aaa bB|-BbbbaaabaaabB
.J q>
|-BbbbaaabaaabB|-BbbbaaabaaabB
I <£
Fig. Ex. 7.2.12(c)
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-22 ■ Turing Machine (TM|
1
Fig. Ex. 7.2.13(a) : DFA for L
1/1, R B/B, L
1/1, R
q3jj Halting state
Solution :
The turing machine will look for every occurrence of the string 110.
• State q 2 is for previous two symbols as 11.
• Next symbol as 0 in state q2, will initiate the replacement process to replace 110 by 101 .
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-23
0/0,R
c
Replacement Vc e
1 - 0 1 0 1 1 0 1 B J- 01 01 1 01 B
t
Al A2
0 1 0 1 1 1 1 B |- 0101011B .
As A4
I-010111B J- 0 1 0 1 1 1 B I- 010111B
t t
<lo 1 5 (halt)
Fig. Ex. 7.2.14(a)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-24
example 7 . 2 . 1 5 . Design and write out in full a turing machine that scans to the rigl
finds two consecutive a’s and then halts over the language of {a,b}.
Solution :
a a
b
Fig. Ex. 7.2.15(a)
Example 7.2.16 : Design a Turing machine with no more than three states that accepts the
Solution: i- \
This problem can be solved by a DFA. A problem that can be solved by a DFA can also
be solved by a TM.
Stepl: DFA for (a(a + b)*)
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-25 Turing Machine (TM)
y/y,R b/b,R
Solution
x/x,R
z/z>L
y/y,R b/b,L
y/yJ-
a/a yL
y/y>R W ---*“
z/zjl B/B,N
Fig. Ex. 7.2.17(a) : Transition diagram
a b c X y z B
(qi.x.R) — — — (q 4 .y,R) — —
q2 — (q 2 ,b,R) (q 3 >z,R) — — (q 2 ,z R) —
Scanned
Scanned by
by CamScanner
CamScanner
Theoryof-Comp Sej. (MU-Sem. 5-Cornp.) 7-26 Turing Machine (TM)
BaaabbbcccB|-BxaabbbcccB|-BxaabbbcccB
t T t.
Mo q[ qi
|-BxaabbbcccB|-BxaaybbcccBf-Bxaaybbccc
42
Mi q2
|— B x a a y b b c c c B | - B x a a y b b z c c B | - B x a a y b b z c c B
t t
q2 M3 M3
|— B x a a y b b z c c B | - B x a a y b b z c c B | - B x a a y b b z c c B
f t t
q3 qj Ma
|-BxaaybbzccB|-BxaaybbzccB|-BxxaybbzccB
q3 co Mi
BxxaybbzccB|-BxxaybbzccB|-BxxayybzccB
t t '
q, . Mi M2
q2 M2 ■ ’ Ms
ayybzzcB|-BxxayybzzcBpBxxayybzzcB
X C3 M3
|-BxxayybzzcB|-Bxx
q, Mo
|-BxxxyybzzcB|-BxxxyybzzcB|-BxxxyybzzcB
qt . Mi Mi
BxxxyyyzzcB|-BxxxyyyzzcB|-BxxxyyyzzcB
q2 Mz M2
zz
Bxxxyyyz B|-BxxxyyyzzzB|-BxxxyyyzzzB
ch . Ma M3
B XXX yyyzzzB|-BxxxyyyzzzB|-BxxxyyyzzzB
t t t
q3 Ms Ms
Fig. Ex. 7.2.17(c)
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-27 Turing Machine (TM)
Solution :
A palindrome can have one of the following forms :
1. coco R
9
R
coaco
R
cobco
a/aJR.
b/bjt
a/BJv
B/B,N a/a>L
% b/bX
B/B,R
Scanned by CamScanner
CamScanner
jr Theoryof Comp. Sci. (MU-Sem. 5-Comp.) 7-28 Turing Machine (TM)
. where,
T = {a, b, B)
The transition function 5 is given in Fig. Ex. 7.2.18(a)
q 0 = initial state
B = blank symbol
qo qi 03 03
|-Bbb abbaB |-BbbabbaB |-Bbb a b b a B |-B b ba b b a B
q 3 03 03 03
q2
t- q-t 04 ‘
t 04
t I. ■ . V "
1-BbabbB |-Bb ab bB|-BbabBB |-B babB
q4 q$ ■ 07 07
|-BbabB|-Bb abB f-Bbab.B l-BBa bB
Gy 7 -0 C2
i i i
Ffg. Ex. 7.2.18(a)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-29 Turing Machine (TM)
Example 7.2.19 : Draw a transition diagram for a Turning machine accepting the following
language.
L = The language of all non-palindromes over {a,b}
Solution : The solution follows from Example 7.2.18.
In Fig. Ex. 7.2.18, if the tape does not contain ‘a’ in the state q5 or the tape does not
contain ‘b’ in state q 6 then it is a non-palindrome.
The required TM is given below :
a/a, R
b/b, R
a/a, R
—»—
b/b, R
a/a, L
b/b, L
Halt
a/a, R
■— ►—
b/b, R a/a, R
b/b, R
B/B, R
Example 7.2.20 : Design a TM which recognizes words of the form cococo over alphabet {a, b}.
Solution :
Initial HIs a to
After 1 st cycle ¥ • y b v n •■ w 4n In
1 15
After 2 nd cycle B x y y X a 2 | B
rd *
' y' y ' Y
After 3 cycle Im mlwblfe Kb 2o 1n
| B
Scanned by CamScanner
CamScanner
wjgr
V Theory of Comp, Sci. (MU-Sem. 5-Comp.) 7-30 Turing Machine (TM)
Initial x
_ _nd ,
After 2 cycb
After 3 rd cycle B
Step 3 : After step 2, the tape contains coco, where the first co is a string over {x,y} . I n this
step first co is matched with 2 nd co.
Situation of the tape is shown below :
Initial
After 1 cycle
After 2 cycle b;
rd ,
After 3 cycle
Scanned by CamScanner
X Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-31 Turing Machine (TM)
a/a3 a/a3
b/b3
a / l , L >4
----► ( 4 )
b/23 <<
Step 1 2/2,L
y/y,R ______________________
x/x3
1/1,N
B/B3
2/2,N
a/a,L
b/bJL x/x3
x/x3 y/y3
y/y3 a/a3
a/aJL b/bj<
b/b,L
x/x>L
y/yX
2/b>L
$ — -X-
B/B3
1/1>N
2/2>N
y/B3 Wl/13
<>2/23
2/2 < x/x3
B/B3
Fig. Ex. 7.2.20 : Transition diagram
MU - Dec. 2010
Solution :
o
Step 1 : First, we can design an FA to recognize a
string staring with 0 and ending in 1 1. IT 1
Scanned
Scanned by
by CamScanner
CamScanner
Al
A turing machine can be used for computation. It can perform, several operations
including :
1. Addition
2. Subtraction
3. Multiplication
1
4. Division >
Computation of a function f can be represented as
The unary number system is often used while computing a function using a Turing
hi The unary system uses only one symbol. This symbol is represented as 0.
Representation of some decimal numbers in unary system is given below :
Unary Number
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-33 Turing Machine (TM)
.1000
S 1010011000
z
Complement no change
every bit
Algorithm :
B/BJt
B/B>R
0 1 B
~ >q0 (q o .o,R) (Ao»1’R) (qi,B,L)
Ai (qi,o,L) (<h,l,L) (q 3 ,B,R)
A2 (<32,1,L) (Qz 0.L) (q 3 ,B,R)
* Halting state -
A3 Aa A3 A3
Fig. Ex. 7.3.1(b) : Transition table
The Turing machine M is given by :
M = (Q, Z, r, 5, q 0 , B, F)
Where,
Q — { A0’ Ah A2» A3)
E = {0, 1}
r = {o, 1, B )
The transition function 8 is given in Fig. Ex. 7.3.1(a) or (b),
q 0 = Initial state
B = Blank symbol
Scanned
Scanned by
by CamScanner
CamScanner
Turing Machine (TM)
q0 qo
I - B 0 1 0 11 0 1 0 0 0 B H B 0 1 0 11 0 1 0 0 0 B I - B 0 1 0 1 1 0 1 0 0 0 B
i qo
| - B 0 1 011 0 1 0 0 0 B ( - B 0 1 0 11 0 1 0 0 0 B p B 0 1 0 1101000B
qi
q> qi
| - B 0 1 0 11 0 1 OOOB | - B 0 1 0 1 1 1 1 OOOB
q2 q2
|-B0101011000B|-B0100 Oil OOOB I - B 0 1 1 0 0 11 OOOB
q2 i q2
| - B 0 0 1 0 0 11 OOOB B 1 0 1 0 0 11 OOOB [ - B 1 0 1 0 0 11 OOOB
c? q2 q?
Example 7.3.2 : Design a TM to find the 1’s complement of a given binary input.
Solution :
l’s complement of a binary number can be computed by complementing every bit of the
given binary number.
0 i B
0/1, A
-J/0,R
q<> (qo* i . R ) (q0. o. R) (q P b, L)
W B/B,L
qi Qi q! -Halting state
q*
Scanned by CamScanner
V' Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-35 Turing Machine (TM)
lliI
Illi I
11
Solution :
The working of the TM is being explained with subtraction of 3 from 5.
In unary system, 5 is represented as 00000.
In unary system, 3 is represented as 000.
In unary system, 0 is represented by a blank tape.
Subtraction will require several cycle. In each cycle :
1. - Leftmost 0 is erased ■ j -
2. Rightmost 0 is erased.
Situation of tape after each cycle is shown below :
Initial
•Bl
,cd
O
p
%
-
o
B’
H itg ...
03,.
.E.
B
O
o
w
_rd , .
B 1
o'
o
After 3 cycle B |B
Transition diagram and transition table are given in Fig. Ex. 7.3.3(a) and (b).
#/#£
0/0,R
0/B,R B/B,L
0/B,L
#/BJl B/B,R
0/0,L
#/#L
#/0,N
0/B,R — *— «3
B/B,N ,
Scanned by CamScanner
CamScanner
Theory of Comp. Sci.(MU. Spm 5-Comp.) 7-36 Turing Machine (TM)
0 # B
cfc • *11 St
|-B B 0 0 0 0 # 0 0 0 B | - B BO 0 0 0 # 0 0 0 B | - B B 0 0 0 0 # 0 0 0 B
Qi , 91 c,i
|-BBOOOO#OOOB|-BBOOOO#OOOB|-BBOOOO#OOOB
9i 9i q,
l-BB 0000#OOOB|-BOOOO#OOOB|-BOOOO#OOBB '
t ; t •
91 9i q3
Scanned by CamScanner
CamScanner
Theory of Comp. Sci, (MU-Sem, 5-Comp.) 7-37 Turing Machine (TM)
|-B0000#00B|-B0000#00BH-B0000#00B[-B0000#00B
T ? T T
Q3 <13 <b <13
(-B 0 0 0 0 # 0 0 B | - B 0 0 0 0 # 0 OB | - B 0 0 0 0 # 0 0 B | - B 0 0 0 0 # 0 0 B
qa . qj q0
[-BBOOO#OOB|-BBOOO#OOB|-BBOOO#OOB|-BOOO#OOB
J t t T
qi qi qi 9i
HB000#00B|-B0 00#0 0B|-B000#00BPB000#00B
q< qi <i
f-BOOO#OBB|-BOOO#OB|-BOOO#OB|-BOOO#OB
i i i J
|-B000#0B|-B000#0BPB000#0B|-BB00#0B
q3 93 q0 ct
|-B 0 0 # 0 B | - B 0 0 # 0 B | - B 0 0 # 0 B B 0 0 # 0 B | - B 0 0 # OB
Qi qi qi 9i <h
Example 7.3.4 : Design TM to compare two numbers, which will produce the output L if first
number is less than the second number, output G if first is greater than the
second number and E otherwise.
Scanned by CamScanner
Sei, (MU-Sem. 5-Comp.) 7-38 Turing M a c h i n e ( T M )
Solution :
#/#,R
0/0, R
O/B.R J O/B,L
Mb— *— q
2.
m > n
m n- #/B,R O/B,L
B/B.R #/B,L
0/0, L
#/#,L
m < n O/L.R
\ ( O/B.R
mJo/b,l
B/B,N
B/G.N
m = n
' B/E.N
Halting state
An input # in state q2 means m > n. Everything is erased from the tape and an alphabet G
is written on the tape.
A # followed by 0 in state q0 means m < n. L is written on the tape.
A # followed by B(blank) in state q0 means m = n. E is written on the tape.
The Turing machine
B
M = ({q0 , 9i> q 2> 93- 94- 9s- 96- 9?)- {°1- {0, #. > G - L, E}, 8, q0 , B, {q7 })
The transition function 8 is specified using the transition diagram.
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-39 Turing Machine (TM)
Initially
1* 0 fo: to
o
o
0 ;:0 B 0 0 0 B
x x
head on the . head o n the
rightmost bit of x rightmost bit of x
x x y
st
After 1 cycle 0
head
X y
wN # |
nd $
After 2 cycle 1D 44
#| 0 I 0 I 0 B | BJ 0 1 0 | 0 | # | # | # | # I B
1 T
head
' head
Final result
waHl __ |b|o|o|o|o|q | o | o | b
#’sare erased #'s are rewritten
as 0's •_
Scanned by CamScanner
CamScanner
jr Theory of Comp Sci. ( M U - S e m , 5-Comp.) 7-40 Turing Machine (TM)
x+y
B/B,R
0/0, L
B/B,L X
XB/B.L
y-x #/#,L
0/0,R q4 ) j#/o,l
B/B,N
0/0, N
The Turing machine M = ({q0, q p q2, q3, q4, qs, q6h {0}»{ » 4o» fae))
Where 8 is specified through the transition diagram.
Simulation of machine : ,
(i) x = 2,y = 2 ......
l - B O O O O B HBOrtOOB | - B 0 # # 0 B | - B 0 # # 0 B
f T ,t • T! -
QO ,' <51- .:..... % «J3 ■ ■
|-BO##OB|-BOOOB|-BO##OB
T
q3
T
tfe
t ■
q0
|-B###0B |- B # # # 0 B |- B # # # 0 B
t /• T. T T
di. . 9i ' Qi Qi
|-B ####B |-B#OOB l-BOOOB RB#000B
. t T ? T
14 94 04 <14
l-BOOOOB l-BOOOOB
f, T ' I . p .J ' . It
04
q6 -halt '
(ii) x = 0, y = 2 . . , .
x = 0, will be represented by.blanks and y = 2 will be represented by two 0’s.
BOOBl-BOOBl-BOOB
T T T ,' ■
‘Io % <16 -halt ' »
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-41 Turing Machine (TM)
5 Product will be
stored here
1x5 = 5
3 x 5 = 15
4/0
B/0,L
#/#,N
0/0, L
#/#X
Scanned by CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 7-42 Turing Machine (TM)
l-BxO#xO#B |-BxO#xO#B
t t
Qa Qa
|- B x 0 # x 0 # B |- B x 0 # x 0 # OB
T T
q3
|-Bx0#x0#0B |-Bx0#x0#0B
? T
q4 q«
|-Bx0#00#0B |-Bx0#0x#0B
T - T
q2 - . <b
|-Bx0#0x#0B |-Bx0#0x#0B
q3 <£> .
|-BxO#Ox#OOB |-Bx0#0x#00B
t T
. • <14 . .
|-Bx0#0x#00B |-Bx0#C0#00B
T f
q4 q2 - • ■ ■.
|-Bx0#00#00B |-Bx0#00#00B |-Bx0#00#00B
T T T
q3 qs q5 '
|-BxO#00#OOB |-Bx0#00#O0B |-B0G#00#00B
t t T
• qj <b qo
|-B0x#00#00B |-B0x#00#00B
T T
q> qj
Fig. Ex. 7.3.6(a) Contd...
CamScanner
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-43 Turing MachinefTM)
|-BOx#xO#OOB|-BOx#xO#OOB
t T
O3 Os
|-BOx#xO#OOB|-BOx#xO#OOB
T T
<13 ■ <13
|-B0x#x0#00B|-B0x#x0#000B
T T
<13 <14
I- B 0 x # x 0 # 0 0 OB | - B 0 x # x 0 # 0 0 0 B
T ?
<14 q4
I-B0x#x0#000B|-B0x#x0#000B
t ■
<14 Of : '
I- B 0 x # 0 0 # 0 0 OB I- B Ox # 0 x # 0 0 0 B
T . ;■ t '
<k q3
l - B O x # 0 x # 0 0 OB | - B Ox # 0 x # 0 0 0 B
T u.; . t • < p-;. - '•
q3 %
| - B 0 x # 0 x # 0 0 OB |- B Ox # 0 x # 0 0 0 B
T . f/ t
qj . ■ qj
|-BOx#Ox#OOOOB|-BOx#Ox#OOOOB
T ... t
04 04
|-B0x#0x#0000B|-B0x#0x#0000B
f..'.; ■■■ ■ T- -
q4 , 04
Ibox#ox#oooob|-box#oo#oooob
T t ,
q4 04
|-box#oo#oooob|-box#oo#oooob|-box#oo#oooob
T ■ 't ■■ ••• f
q? Os
|-box#oo#oooob|-boo#oo#oooob|-boo#oo#oooob
t 1" f ' V '
Os Oo <k resufo
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-44 Turing Machine (TM)
Cycle 1 B x 00 # 0 0 0 # 000
#/#,R #/#,L
x-sO/O.R >0/0, L
#/#,R
x/0,R Copy TM
zS O/O.R
KLJ#/#,r
#/#,R
B/#,L 0/0, R 0/0, R
To copy #
at the right 0/x.R CJ #/#,R X/0.R CJ
end
I 0/0, L |B/B,R
i
I #/#,L x/0,L
: #/#,n
is
! <Jo/o,l
„ #/#,l ' Multiplication TM ;
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-45 Turing Machine (TM)
Example 7.3.8 : Design a TM to compute remainder and quotient when a unary number is
divided by another unary number.
Solution :
o/o z/zjl
z/OJL
0/x,R y/B>L
— >—
x/B>L
0/BJL
0/zJL
’" a “t is ay
'■ , . EtHaiEEEHaaBB
Initial situation of tape for 5 -s- 2 is J 7 ,
shown in Fig. Ex. 7.3.8(a). 2 5 quotient will be stored here.
Fig. Ex. 73.8(a)
1 , Initially BOOflOOOOOflB
2. BOO#zzOOO#B [2 is subtracted from 5]
3. BOO#yyOOO#OB [quotient is incremented by 1]
4. B 0 0 # y y z z Q # 0 B [2 is subtracted from 3]
5. BOOflyyyyOflOOB [quotient is incremented by 1]
6. BxO#yyyyz#OOB [2 can not be subtracted from 1]
7 1
- • I
BOflOOB
remainder quotient
Fig. Ex. 7.3.8(b)
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-46 ■ Turing MachineJTM)
If y cannot be subtracted from x then z’s become remainder.
Working of TM for 5 + 2 is shown in Fig. Ex. 7.3.8(c)
BOO#00000#B|-Bx() #0 00 00 #B|-B xO # 0 0 0 0 0 # B
Ao q, Ai
|-Bx0#0000O#B|-BxO#z0000#B|-Bx0#z00OO#B
q
, A2 Ab s
|-Bx0#z0000#B|-B00#z0000#B|-B0x#z0000#B
T T q
t
Qo 9o '
|- B 0 x #z 0 00 0 #B |-B Ox #z 0 0 00 #B |-B Ox # z z 0 00 #B
T T T
. q2 9z %
I- B 0 x#z z 0 0 0 #B F B 0 x# z zO 00 #B |-B 0 0 #z zO 0 0 # B
. •T t t
’ 93 9a 9o
|-B00#zz000#B|-B00#yz000#B|-B00#yy000#B
T T
J q
9< 94 4
I- BOO# y y 0 0 0 # B | - B 0 0 # y y 0 0 0 # B |-B 00 #y y 0 00 #B
T T T
q
94 ' ■ 94’ . *
| - B 00 #y y 0 00 #B|-B 00 #y y 00 0 # 0B |-B 00 #y y 00 0# 0B
T T t
94 As
|-BOO#yyOOO#OB|-BOO#yyOOO#OB|-BOO#yyOOO#OB
t T t
9s As As
|_BOO#yyOOO#OB|-BOO#yyOOO#OBl-BOO#yyOOO#OB
t T t
q5 Qs Qs
|-BOO#yyOOO#OBbBOO#yyOOO#OB|-BOO#yyOOO#C
ft t
cfc Ms Mo
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-47 Turing Machine (TM)
i.— ■ ; = —
|-BxOflyyOOOflOB|-BxO#yyOOOflOB BxOflyyOOOflOB
■t ' t T
<h 91 92
|-BxO#yyOOOflOB|-BxO#yyOOOflOBl-BxOflyyzOO#OB
T t c
T
q2 q2 b
l - B xO fly y zO 0 # OB I- B x 0 # y y zO 0 # 0 B l-B xO fly y z 0 Ofl OB
t T T
Qi q> %
| - B x O # y y z O O # OB |- B O O f l y yzOO flOB
T T
q2 q>
|-B0x#yyz00#0Bl-B0x #yyz00fl0B|-B0x flyyzOOflOB
T - T T
% Oz 02 .
% ; o2
l - B 0 x # y y z z 0 # OB l-B Ox fly y z z O # OB
T T ’ -
;% . Oa
|- B 0 x fly y z z 0 # OB | - B Ox # y y z z 0 # 0 B
■T T ■. ■ '
q3 0)
l - B 0 x # y y z z 0 # OB l - B 0 0 fly y z z 0 # OB •
T ■ T
01 Oo
|- B 0 0 fly y z z 0 # 0 B | - B 0 0 fly y z z 0 # OB '
T - t
O« . -04 ;
04 04
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-48 Turing Machine (TM)
|-BOO#yyyyO#OOB|-BOO#yyyyO#OOB|~BOO#yyyyO#OOB
QS
<15 <lj
#OOB
|-BOO#yyyyO#OOB|-BOO#yyyyO#OOB|-BOO#yyyy°
T t f
q5 q5 45
l-BOO#yyyyO#OOBl-BOO#yyyyO#OOB|-BOO#yyyyO#OOB
t t t
qj
q5 q5
I- B 0 0 # y y y y 0 # 0 0 B |- B 0 0 # y y y y 0 # 0 0 B |- B 0 0 # y y y y O f
4.5 ■
T . n
45 On remainder quotient
|-BxO#yyyyO#OOB|-BxO#yyyyO#OOB|-BxO#yyyyO#OOB
Qj <13 1 .
| - B xO # y y y y z # 0 0 B | - B x O # y y y y z # 0 0 B
.. . r • /’
|-BxO#yyyyz #00B|-B00#yyyyz#00B
T
q, ’ Qo ’ , f.
|-B0x#yyyyz #00B|-B0x #yy yz #0 0B
q2 Qi q2
l-BOx #yyyyz #00B|-B0x #yyyyz #00B
t t
qz q2
Fig. Ex. 7.3.8(c) Contd...
Scanned by CamScanner
Machine
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-49 Turing (TM)
| - B 0 x fly y y y z # 0 0 B I- B 0 x flyyyyOflOOB
T T
qs
| - B 0 x fly y y BOflOOBl-BOx fly yBBOflOOB
t T
Os q«
|-BOx#yBBBO#OOB|-BOx#BBBBO#OOB
T T
q6 q»
l - B 0 x B B B B B 0 # 0 OB l-B OB B B B B BO # 0 OB
T■ T
9s 9s
|-BBBBBBBB0#00B | - B B B B B B B B 0 # 0 OB
t t
q6 q7 remainder quotient
B/1JM
0 i . B
State q 0 moves the head to the first blank on the right side.
■»
Scanned by CamScanner
Theoryof Comp. Sci. (MU-Sem. 5-Comp.) 7-50 Turing Machine (TM)
I— B 0 1 0 1 0 1 1 1 B |- B 0 1 0 1 0 1 11B|— B 0 1 0 1 0 1 1 1 B I — B 0 1 0 1 0 1 1 1 B
T T t T
T % 9o • 9o
I-BOIOIOI'UBI-BOIOIOI 1 1 B I— B 0 1 0 1 0 1 1 IB
T . T t
I-B01010110BI-B01010100BI-B01010000B
T T T
Qi 41 41
l-BOl 01 1000B
9a
Fig. Ex. 7.3.9(c)
Example 7.3.10 Design a TM to add two unary numbers. . MU - May 2011, Dec. 2016
Solution :
Addition of two unary numbers can be performed through append operation. To add two
numbers 5 (say (00 and 3 (say (D?) will require following steps :
Initial configuration of tape :
3(oj 2 )
While every ‘0’ from co, is getting appended to <o2, *0* from co, is erased. co2 contains 8
0’s, which is sum of 5 and 3.
Scanned by CamScanner
CamScanner
.V Theory of Comp. Sci. (MU-Sem, 5-Comp.) 7-51 Turing Machine (TM)
0/0
O/B.R
#ZB,R
D/B,R
0 # B
“ 0 (q B.R) (q3,B,R) ■—
qi (qi,o,R) (q1(#,R) (q2 ,0,L)
(q 2,0,L) (qjAL) (q 0 -B,R)
* Halting state
q3 qj <h q3
Fig. Ex. 73.10(b) : Transition table
S = {0,#}. ■ . , <
r= .
8 = Transition function is given in Fig. Ex. 7.3.10 (a), (b)
q 0 = initial state
r
B = blank symbol
F = {qs), halting state.
Example 7.3.11 : Design a TM to find the GCD (greatest common divisor) of two unary
numbers.
Solution:
Two numbers x and y are stored on the tape without any separator. Head is positioned
on the last digit of x. 1 1
• - • • . < - * * ■ ■ <, . ,
• Algorithm for finding GCD is based on repeated subtraction of x from y if y > x or y
from x if y < x. ultimately either x or y will become 0.
• If x becomes 0 then y is GCD
Scanned by CamScanner
Th e Q | 7 of C°mp. Sei. (MU-Sem. 5-Comp.) 7-52 Turing Machine (TM)
if(x > 0)
printf (“ % d”,x);
else
printf (“ % d”,y) ;
Situation of tape for x = 6 and y = 4 is shown in Fig. Ex. 7.3.11 :
1st cycle : Subtraction is carried out through several cycles. In each cycle, the last ‘0’ of ‘x’ is
written as x and the first ‘0’ of y is written as Y.
1. BOOOOO X Y 000 B - - -
2. B0000XXYY00B \ . .
3. B000XXXYYY0B
4. BOO XXXX YYYY B
I ______II--------J ' ■'
Since, x is greater than y, y will end first. . , ■
At this point Y’s are erased and X’s are written as 0’s
B 00 0000 B
9o
2 nd cycle :
1. B0XY000B
BOX
2. BXXYY00B
BX2
B00 00B
Qo
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-53 Turing Machine (TM)
At this point, X’s are erased and Y’s are written as 0’s.
B 00 00 B
9o
rd
3 cycle :
B0XY0B
2. BXXYYB
Since, y is equal to x, both x and y, have ended at the same time. At this point, x’s are
erased and Y’s are written as 0’s. The tape contains the final result.
B00B
I I
result
0/Y,L
0/0 J,
/b/bx
a® x/xx
Y/YX
B/B,R
------KX
b/bx
XZB3
Y/0,R
Scanned by CamScanner
Theor of C o m Turing Machine (TM)
y y P- Sci. (MU-Sem, 5-Comp.) 7-54
q0 = Initial state
B = Blank symbol
F = {q6 h halting state
Working of TM for GCD (6, 4) is shown in Fig. Ex. 7.3.1 1(b) :
6 is represented as 000000
4 is represented as 0000
t 6 4
Initial configuration BOOOOOO' 'oooo'b head is positioned on the last digit of 6.
T
Qo
B0000000000B I- B 0 0 0 0 0 xO 0 0 OB |- B 0 0 0 0 0 x y 0 0 0 B
t f T
q0 <ii.
l-B 00 00 Oxy 0 00 B |-B0000xxy000B l-B 0 0 0 0 x x yO 0 0 B
T T ■ t
1 q
Vqo q> >
| - B 0 0 0 0 x x y 0. 0 0 B |- BO 0 0 0 x x yy 0 0 B | - B 0 0 0 Ox x y y 0 0 B
T T ' T
q
Qi % 2
HBOOOOxxyyOOBl-BOOOOxxyyOOBl-BOOOOxxyyOOB
t T T
q2 q2
|-BOOOxxxyyOOB|-BOOOxxxyyOOB|-BOOOxxxyyOOB
f • T T
q, qi qi
|-BOOOxxxyyOOB|-BOOOxxxyyOOB|-BOOOxxxyyyOB
t T T
qi qi q2
|-BOOOxxxyyyOB|-BOOOxxxyyyOB|-BOOOxxxyyyOB
T t t
q2 q2 q2
|_BOOOxxxyyyOB|-BOOOxxxyyyOB|-BOOOxxxyyyOB
t t T
q2 q q0
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-55 Turing Machine (TM)
|-BOOxxxxyyyOB|-BOOxxxxyyyOB|-BOOxxxxyyyOB
T T t
Qi Qt 9i
|-B00xxxxyyy0B|-B00xxxxyyy0B|-B00xxxxyyy0B
T • ! T T
91 91 91
|-BOOxxxxyyyOB|-BOOxxxxyyyyB
9i t
l-B00xxxxyyyyB|-B00xxxxyyyyB|-B00xxxxyyyyB
T T T
92 92 92
J-BOOxxxxyyyyB|-BOOxxxxyyyyB|-BOOxxxxyyyyB
T T T
92 92 ' 92
|-BOOxxxxyyyyB|-BOOxxxxyyyyB|-BOxxxxxxyyyyB
t t ' ' T •
9z - 9o 9i
1-BOxxxxxyyyyBl-BOxxxxxyyyyBl-BOxxxxxyyyyB
l - B O x x x x x B h B 0 x x x x O B | - B O x x xO OB
T T t
q«
■ ' -I X y ..
n r— l
l~BO x x O 0 0 B | - B 0 x 0 0 0 OB | - B 0 0 0 0 0 OB | - B 0 0 0 0 0 OB
T ? T t ,
q
< 9i
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-56 Turing Machine (TMj
H B 0 xOO 0 0 B |- BO x y 0 0 OB |- B 0 xy 0 0 OB
T t t
ii q2 q2
l~BO x y 0 0 0 B |- B x x y 0 0 OB B x x y 0 0 OB l~B x x y 0 0 OB
l-BxxyyOOB|-BxxyyOOB|-BxxyyOOB
t ' (
T T
Qi b
|-BxxyyOOB|-BxxyyOOB|-BxyyOOB|-ByyOOB
T T T t
q2 qj ■ • q3 • is
X y
.I- B 0 y 0 0 B ,|- B 0 0 0 0 B | - B 0n 0n 0 OB
T T t
qj q5 q0 ■
|-B0x00B|-B0xy 0B|-B OxyOB |-B0xy0B
t t
ii q2 %
B x x y 0 B |- B x x y 0 B | - B xx y OB l - B x x y y B
t T. f i A '.-.
qi . . qi .. . l i ... .. iz
|-BxxyyB|-BxxyyB|-BxxyyB|-BxxyyB
T . T. « 7- T .',T-
li '■ ' q2 ' ' q2‘ q5
| - B x y y B |-B yy B | - B 0 y B | - B 0 0 B |-B00B
T -T T T . t •
qs q3’ q5 ■ q5 ' q6
p p
Result = 00, which is 2.
Fig. Ex. 7.3.11(b)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-57 Turing Machine (TM)
Any number n satisfying the condition 25 n < 26 can be written as 1XXXXX (where X
stands for either 1 or 0).
log2 (1XXXXX) can be calculated by erasing the most significant bit 1 and renaming
other bits as ‘O’. Unary representation of 5 is 00000.
0/0,R
0/B,R 1/OJR.
U4 1/B,R B/B.LZBX
@
Halting
state
Fig. Ex. 7.3.12(a) : Transition diagram
0 1 B
Ij MU - Dec. 2008 |
Solution : It is assumed that n is represented in unary system.
Factorial of n can be calculated through repeated application of :
1. Multiplication
2. Copy
Operations.
Algorithm is being explained with the help of example.
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) ' 7-58 Turing Machine (TM)
Algorithm for | 3 .
Initial configuration
0#000#B B ...
n
Cycle 1 :
1. Multiplication
0#000#000B...
Product
2. Copy n 1, i.e . 2 0 #0 0 0 #0 00 #0 0
nx1 n-1
Cycle 2 :
1. Multiplication 0#000#000#00#000000
#/#,N
0/0, R
0/x,R 0/x.R
<!y —
x/0,R / \
#/#,L
#/#,R B/O.L
x/0,R
,0/0, L
#/#,L
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-59 Turing MachineJT
Subroutine to copy n - 1 :
0/0, R
x->#/#,R
0/x
q iR
o )-------- --------( q 9 )
x/x,R „B/0,L
'#/#,R 0/0, L
w #/#,L
q ,0/0, R
n #/#,R
B/B.L
0/#,L
o/o, .
#/#,L
0/0, L
#/#,R
(22)
Fig. Ex. 7.3.13(c)
MUj Dec jOOS, Dec. 2008, Dec. 2009, Dec. 2010, May 2014,
May 2015. May 2017
In a standard Turing machine, the tape is semi-infinite. It is bounded on the left and
unbounded on the right side. Some of the extensions of turing machine are given below :
1. Tape is of infinite length in both the directions.
2. Multiple heads and a single tape.
3. Multiple tapes with each tape having its own independent head.
4. K-dimension tape.
5. Non-deterministic turing machine.
7.4.1 Two-way Infinite Turing Machine
In a standard turing machine number of positions for leftmost blanks is fixed and they
are included in instantaneous description, where the right-hand blanks are not included.
In the two way infinite Turing machine, there is an infinite sequence of blanks on each
Scanned by CamScanner
Z £2 _2L2£ P2_§cMMU-Sem. 5-Comp.) 7-60 Turingj achine CTM
side of the input string. In an instantaneous description, these blanks are never shown.
H) H2
Fig. 7.4.1 : A Turing machine with two heads
The transition behavior of 2-head one tape Turing machine can be defined as given
below :
8 (State, Symbol under Symbol under H 2 ) = (New state, (S b M , (S 2 , M2))
Where,
S j is the symbol to be written in the cell under H P
M j is the movement (L, R, N) of H P . '' :;
S 2 is the symbol to be written in the cell under H 2 .
< ?
M 2 is the movement (L, R, N) of H 2 . ‘
Tape 2:
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-61 Turing Machine (TM)
Example 7.4.1 : Construct a two-tape taring machine to recognize words of the form cooxd
over alphabet {a,b}.
Solution : Let the initial string be placed on tape 1 and tape 2 may contain all blanks. Initial
configuration for co = abb is shown in Fig. Ex. 7.4.1(a).
Tape 1: B a b b a b b a b b B B By-
Th,
Tape 2: B B ... ..................................
Step 1: This step will copy the last co from cococo on tape 1 to tape 2 in reverse order.
• H] is advanced by two places while re-writing a as x and b as y.
• H, is moved to the last character and the last character from tape 1 is copied to tape 2,
(a/a,R) (B/B JT)
(b/b,R) (B/B.N)
(a/B,L)(B/a,R)
--------. ----------
(b/B,L)(B/b,R)
Hj
Fig. Ex. 7.4.1(c) : Last co from tape 1 is copied as coR to tape 2
Scanned by CamScanner
Theory of Comp, Sei. (MU-Sem. 5-Comp.) 7-62 Turing Machine (TM)
(c/c,N) (D/B,R)
(c/c,N) (n/a,L)
(c/c,N) (b/b,L)
<14
<15
Step 3 : Two sets of xyy are matched with bba in the reverse order.
(x/B,R) (a/ajt)
(x/B>R)(a/a,R) (y/B>R)(b/b,R)
(y/B,R) (b/b,R)
(c/e,N) (B/B,L)
r
Fig. Ex. 7.4.1(e) : Two sets of xyy are matched with bba in the reverse order
Example 7.4.2 : Construct a two-tape taring machine to convert an input co into cocoR .
Solution :
Initially, tape 1 contains (0 and the tape 2 is blank String w is assumed to be abb.
B a H b b ® -a
t
Hl
B B B — -......... . . .
I
H,
Fig. Ex. 7.4.2(a) : Initial configuration
Scanned
Scanned by
by CamScanner
CamScanner
Turing Machine (TM)
Tr Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-63
(B/B.N) (B/B,L)
H2
Step 2 : Contents of tape 2 is copied to tape 1, while moving towards left in tape 2 and
moving towards right in taped.
(B/aJ<) (a/aj.)
(B/bJt) (b/bj.) / J.
(B/BJM) (B/B.N)
a | b] b
i.-.
■■
•; r. ■
H 3
THz
' . •d . Fig. Ex. 7.4.2(c): Contents of tapes after step 2 .
(B/BJJ)(B/B,L)
— — *---------------
(B/B JI) (B/B.N) NS
s
■, »4 *, *' ♦ ■ t jr * * 4\
CamScanner
Scanned by CamScanner
V Theory of Comp. Sci, (MU-Som. 5-Comp.) 7-64 Turing Machine (TM)
a/x>R
The transition behaviour for state q 0 for TM of Fig. 7 .4.3 can be written as
5 (qo> a ) = a x
{(Qo» »,R) (Qi» » R)}
Example 7.4.3 : Construct NDTM to recognize words of the form coco over alphabet {a,b}
Solution : . •> -
First character of the second co in coco can be located non-deterministically.
1. TM skips the first co and re-writes every a as x and every b as y for every a, b belonging
to first co of coco. Contents of tape after step 1 are shown in Fig. Ex. 7.4.3(b).
co
<lo
h
(b)
Fig. Ex. 7.4.3
2. Head is sent back to the first character and then first half is matched with the second half
while matching the two co’s every character of second co is re-written as z.
• A cycle through q 2 -> <14 Qz matched x with a. ,
• A cycle through q 2 -> q4 Az matched y with b.
Scanned by CamScanner
CamScanner
war
y Theory of Comp. Sci. (Mll-Sem. 5-Comp.) 7-65 Turing Machine (TM)
x/x,R
y/y,I<
z/z,R
x/x,L x/B,R
y/yX
z/z,L
B/B,R x/xX
y/y>L
y/B,R x/xJR
z/z sR y/y,R
z/z>R
z/z,R
B/B>R
B/B,N
z/zJj
— ►“ --------- x/x,L
B/BJ< y/y>L
y/B,R
yAz/zJR
x/x,R
y/yjt
z/zj<
z/zJR
B/B,R
B/B,N
a
Fig. Ex. 7.4.3(d) : NDTM for oxo
Scanned by CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 7-66 Turing MachinegMl
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-67 Turing Machine (TM)
State q n is represented by 0 n+ \
First symbol can be represented by 0,
Second symbol can be represented by 00 and so on.
Two elements of a tuple representing a move arc separated by 1.
• Two moves are separated by 11.
Execution by UTM : We can assume the UTM as a 3-tapc turing machine.
1. Input is written on the first tape.
2. Moves of the TM in encoded form is written on the second tape.
3. The current state of TM is written on the third tape.
The control unit of UTM by counting number of 0’s between l ’ s can find out the cunent
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 information from the tape 2 :
1. Next state 2. Next symbol to be written
3. Move of the head.
Based on this information, the control unit can take the appropriate action.
MU - May 2017
The Turing machine is a general model of computation. Any algorithmic procedure can
be solved by G computer can also be solved by a TM. Problems computed by a computer or a
TM are also known as partial recursive functions. Some enhancements to TM made the
Church-Turing thesis acceptable. These enhancements are :
1. Multi-tape 2. Multi-head
3. Infinite tapes 4. Non-determinism.
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.
Scanned by CamScanner
(MU-Senri. 5-Comp.) 7-68 Turing Machine (TM)
1VH|Td 2oT2, D e c . 2 0 1 6
Solution :
A linear bounded automata(LBA) restricts the length of the tape. The LB A is important
as the set of context-sensitive languages is accepted by it.
A linear bounded automata is same as a non-deterministic Turing machine except in the
followings , (1 , . . . . . - , -
1. There is a special symbol < (say), marking the left end of the tape.
2. There is a special symbol > (say), marking the right end of the tape.
3. The head is not allowed to move beyond these end-markers. ,
4. The head is not allowed to change these end-markers.
An LB A can be denoted by :
M = (Q, E, T, 8, q 0, F, <, >) ; , .
Where < and > are symbols in S, the left and right end-markers. Q, S, T, 8, q 0 and F are
same as for a non-deterministic TM.
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-69 Turing Machine (TM)
Q. 2 Design a Turing machine that can accept the set of all even palindromes over
alphabet {0, 1 }. (Example 7.2.1 8) (Note : ‘0’ as ‘a’ and ‘1’ as ‘b’) (8 Marks)
Dec. 2006
Q. 4 Prove the variations and equivalence of Turing machine. (Section 7.4) (10 Marks)
May 2007
Dec. 2007
Q. 8 Design the turing machine to find the value of log 2 n where n is any binary number and
a prefect square. (Example 7.3.12) (10 Marks)
Q. 9 Write note on ‘Multiple Turing machine’. (Sections 7.4.2 and 7.4.3) (5 Marks)
May 2008
Q. 11 Design a Turing Machine which recognizes the language L = {anb n c n I n > 1}.
Q. 13 Design TM which will recognize strings containing equal number of 0’s and 1’s.
(Example 7,2.9) (Note : ‘0’ as ‘a’ and ‘1’ as ‘b’) (10 Marks)
Scanned by CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 7-70 Turing Machine (TM)
Dec. 2008
i May 2009?
Dec. 2009
Q . 20 Design a T M to subtract two numbers (e.g. m and n are two integers and m - n is to be
evaluated) assume m > n. (Example 7.3.3) (10 Marks)
Q. 21 Design a TM to accept language {0 n 1 n I n > 0}
(Example 7.2.2) (Note : ‘0’ as ‘a’ and ‘1’ as ‘b’) (10 Marks)
May 2010
i . St >" '■* t
Q. 23 Design TM that replaces all occurrences of "111” by "101" from sequence of 0’s and
1’s. (Example 7.2.14) (10 Marks)
Q. 24 Write short note on : Universal Turing Machine (Section 7.5) (5 Marks)
Dec. 2010
Q. 25 Explain different types of machines and state at least one application of each
(Section 7.1) (10 Marks)
Q. 26 Design a Turing machine to accept the language given by a regular expression
0(0+1 )*11. (Example 7.2.21) (10 Marks)
Q. 27 Write short note on : Variants of a Turing Machine (Section 7.4) (6 Marks)
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-71 Turing Machine (TM)
May 2011
Q. 29 Design a Turing machine that computes a function f(m, n) m + n for the addition of 2
integers. (Example 7.3.10) (10 Marks)
Dec. 2011
J--' ‘ C . j<. , ;
;
May 2012
Dec. 2012
(10 Marks)
Q. 40 Write short note on : Variants of Turing Machine (Section 7.4)
(5 Marks)
Scanned by CamScanner
y” Theory o£Comp. Sci. (MU-Sem. 5-Comp.) 7-72 Turing Machine (TM)
Dec. 2014
Q. 41 Design Turing machine as generator to add two binary numbers and hence simulate
for 1 10 + 10”. (Hint : Assume two way infinite tape.) (10 Ma s)
Ans. : [Hint : Refer Example as Example 7.2.14(A)]
This problem can be solved using a 3-tape Turing machine.
First machine T1 stores the first binary number. Second machine T2 stores the second
binary number. Third machine T3 stores the result.
The Turing machine will have 3 states :
q 0 - previous carry as 0
qi - previous carry as 1
q2 - Halting state : ..
(0, 0, L) (0, 0, L) (B, 0, L) (1, 1, L) (0, 0, L) (B, 0, L)
(1, 1 , L ) (0, 0, L)(B, 1 , L ) I (1,1, L) (B,B,L) (B, 0, L)
?
(0, 0, L ) ( l , 1,L)(B,.1,L) (0, 0, L ) ( 1 , 1 , L ) (B, 0, L)
(B, B, L) (0, 0, L) (B, 0, L) (B,B,L)(1, 1 , L ) (B, 0, L)
(0, 0, L) (B, B, L) (B, 0, L)
(B, B, L) (1, 1, L) (B, 1 , L ) H
(1, 1, L) (B, B, L) (B, 1 , L ) j
Fig. 1-Q. 41
Simulation for 1 1 0 + 1 0
State q0
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-73 Turing Machine (TM)
T
State q0
T
State qj
State qj
B B B 1 0
-
B B 1 1 0
B 1 0 0 0
T
q 2 (Halt)
Q. 42 Design a Turing machine as acceptor for the language {a n bm I n, m > 0 and m > n}.
a/x,R B/B.L
-w- .
y/y.L
b/b,R b/y,L
y/y,R x/x,R
b/blL
b/b,R Rl) a/a.L
y/y,N
B/B.N
Scanned by CamScanner
CamScanner
JC- Theor
V o f Comp. Sci. (MU-Sem. 5-Comp.) 7-74 Turing Machine (TM)
May 2015
Q. 43 Construct turning machine that accepts the string over S = {0, 1} and converts every
occurrence of 111 to 101. (10 Marks)
Ans. : [Hint : Refer Example as Example 7.2.13(A)]
Halting state
B/B.L / \B/B,L
0/0, R y
B/B.L \
1/1,R
Ao ---------[<12,
1/1,R
0/0, R
1/1 ,L
1/0, H
Fig. 1-Q. 43
r = {0,1, B}
5 = Transition function is shown using the transition diagram
Dec. 2016
Q 46 Design Turing machine to accept the language L = set of string with equal number of
ci s sod b p )
Q 48 Explain variations and equivalences of Turing machine. (Section 7.5.2) (10 Marks)
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-75 Turing Machine (TM)
May 2 0 1 6
a/a,R
b/b,R
B/B.L ~
a/x,R
b/Y,L
X/X.R b/b,L
a/a,L
B/B,L
B/B.L
Fig.l-Q.50
M = (Q, Z, r, 5, q 0 , B , F)
Where,
S = {a, b }
r = {a, b , X , Y, B )
, ;
q0 = initial state
B = Blank symbol'
F = { q4 } : ~
Q. 52 Design a Turing machine that computes a function f(m, n) = m + n i.e. addition of two
integers.(Example 7.3.10) (10 Marks)
Scanned by CamScanner
y Theory of Comp. Sci (MU-Sem. 5-Comp.) 7-76 Tu ring Machine (TM)
May 2017 |
Dec. 2017
S = (a, b).
Q. 57 Design a Turing Machine that recognizes palindrome string where
(10 Marks)
(Example 7.2.18)
Chapter Ends...
□□□
nw
Scanned by CamScanner
Undecidability
Module 6
Scanned by CamScanner
CamScanner
Theory of Comp Sci. (MU-Sem. 5-Comp.) 8-2 Undecidability
Example 8.1.1 : Show that for two recursive languages L1 and L2 , each of the following is
recursive.
•1 ' 1
*~2
Solution :
coeL*
Scanned by CamScanner
Undecidability
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 8-3
It should be clear that M 3 decides L, U L 2. As both L j and L 2 are turing decidable, after
a finite time both M j and M 2 will halt with answer “Y” or “N”. The machine M 3 is activated
after M ( and M 2 are halted. The machine M 3 halts with answer “Y” if 0) £ L | or co e L 2, else
M3 halts with output “N”.
Thus U L 2 is turing decidable or L! u L 2 is recursive.
(ii) L 1 n L2 is recursive
Let die turing machine Mj decides
Lj and M2 decides L 2. If a word wgL, then
returns “Y” else it returns “N”. I
<ogS*
Similarly, if a word (D G L 2 then M 2 returns
“Y” else it returns “N”.
Let us construct a turing machine M 4
as shown in Fig. Ex. 8.1.1(b). Fig. Ex. 8.1.1(b) : A turing machine for n L2
• Output of machine Mj is written on the tape of M 4 .
• Output of machine M 2 is written on the tape of M 4 .
• The machine M4 returns “Y” as output, if both outputs of M j and M 2 are “Y”; otherwise,
M 4 returns “N”.
It should be clear that M 4 decides Lj n L 2 . As both Lj and L 2 are turing decidable, after
a finite time both M, and M 2 will halt with answer “Y” or ‘N”. The machine M 4 is activated
after M, and M 2 are halted. The machine M 4 halts with answer “Y” if cog Li and cdg L 2 , else M 4
halts with answer “N”. ' .
- ;-1 ? .•’ i -
(iii) L 1 is Recursive
coeS
Mi
Scanned by CamScanner
CamScanner
Theory of Comp. S C j. (MU-Sem. 5-Comp.) 8-4 Undecidability
Step 3 : a’s on tape 1, b’s on tape 2 and c’s on tape 3 are matched.
5(qs,(a,b,c)) = (q 5 ,(a,b,c),(L,L,L)) . ' .
8(q 5 ,(B,B,B)) = (q 6,(B,B,B),(N,N,N))
q 6 is a halting state.
If the state q 6 is reached then the string is in the language, {anb nc n }.
If at any stage, the TM does not have any move then the string cog L'
Thus a string of the form a nb nc n is turing decidable.
Example 8.1.3 : If and l_2 are two recursive languages and if L is defined as :
L = {co I cd is in and not in L2 , or co is in L2 and not in Lv } Prove or
disprove that L is recursive.
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 8-5 Undecidability
Solution :
Let the turing machine M t decides L, and
M 2 decides L2. If a word co 6 L h then Mj returns cogZ*
* “Y” else it returns *‘N”. Similarly, if a word co e L 2
then M2 return “Y” else it returns “N”.
Let us construct a Turing machine M 3 as
shown in Fig. Ex. 8.1.3. Fig. Ex. 8.1.3
Output of machine is written on the tape of M3 .
Output of machine M 2 is written on the tape of M 3 .
The machine M 3 returns “Y” as output, if one of the outputs of M h or M 2 is “Y” and
other is “N”.
• The machine M 3 returns “N” as output, if the outputs of both Mj and M 2 are either “Y”
or “N”.
It should be clear that M 3 decides L. As both L! and L 2 are recursive, after a finite time
both Mj and M 2 will halt with answer “Y” or “N”. The machine M 3 is activated after and
M 2 are halted. Finally, the machine M 3 will halt with answer “Y” or “N”.
Example 8.1.4 : Prove the theorem.
“A language is recursive if and only if both it and its complement are
recursively enumerable.
Solution : Let us consider a recursive language L. Let the turing machine M j accepts L and
M 2 accepts L'.
If a word coeL then Mj returns “Y” and the M,
machine M 2 returns either “Y” or it loops forever. , ooeS
Similarly, if a word cog L then M 2 returns “N” and
the machine Mj returns either “N” or it loops forever. M.
Let us construct a Turing machine M 3 as shown in
Fig. Ex. 8.1.4. Fig. Ex. 8.1.4
• Output of machine Mj is written on the tape of M 3 .
• Output of machine M 2 is written on the tape of M 3 .
• The machine M 3 returns “Y” if a value “Y” is found on its tape.
• The machine M 3 returns “N” if a value “N” is found on its tape.
It should be clear that if toeL then at least Mj will halt with answer “Y”. Similarly, if
0)C L then, at least M2 will halt with answer “N”
Thus, if a language and its complement are recursively enumerable then the language is
recursive.
If a language is recursive then its complement is also recursive. A recursive language is
subset of a recursively enumerable language. Every recursive language is recursively
enumerable.
Hence, proved.
Scanned by CamScanner
2 £ T h e o r y of Comp. Sci. (MU-Sem. 5-Comp.) 8-6 Undecidability
Scanned by CamScanner
wSf
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 8-7 Undecidability
Example 8.2.2 : Show that an infinite recursively enumerable set has an infinite recursive
sub set.
Solution :
Let n b n 2, n3 be a recursive enumeration of an infinite set S. For each r there must
be, in this sequence, a later nj > nj.
Let ■ mi = nj,
m2 = , the first nj greater than n!
m 3 = Hj , the first nj beyond n, greater than a .
3 2 2
l
l
l
i
I
Scanned by CamScanner
V Theoryof Comp. Sei, (MU-Sem, 5-Comp.) 8-8 Undecidability
Example 8.2.3 : Show that if S is uncountable and T is countable than S-T is uncountable.
Solution :
We can prove this by contradiction.
Suppose S-T is countable set
S T
“ = {CO b Q)2 ...(On )
where, = a n , a 12 .
W = a
2 21» a 22. ....
i
i
i
i
= a
nl» a n2....
Example 8.2.4 : Show that the set of languages L over {0,1} so that neither L nor L' is
recursively enumerable is uncountable.
Solution :
To prove that the set of languages over {0,1} that are not recursively enumerable is
uncountable, we will first prove that the set of recursively enumerable languages is countable.
Proof that set of recursively enumerable language is countable
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 8-9 Undecidability
Example 8.2.5 : Prove that “The set of real numbers, R, is not countable.
Solution :
Let us define a set S.
S = {x I (x g R) and (0 < x < 1))
Now, the proof can be given in two steps :
1. By proving that S and R have the same cardinality.
2. The set S is not countable.
These two steps will imply that R is not countable.
Step 1 : Proof that S and R have the same cardinality
Let R be the set of all positive real numbers. Let f : R S be defined by f (x) = —*
for x e R. Obviously the range of f is in S.
y
Further, for any ye S, we have x = j . Thus f is one-to-one and onto. Therefore, the
set S and the set R have the same cardinality.
Step 2 : The proof that the set S given by S = {x | x g R and 0 < x < 1} is not countable.
We can prove this by contradiction.
Suppose S is a countable set. —
;
S = {COj, co2 .....con } '
where, . CDj — a j a 2 a 3
W = a
2 22 &23 ...........
W a a
n ~ nl n3............. ‘
Assumptions
L Each C0j is distinct
2. ajj G [0,l,2,3,4,5,6,7,8,9] • . ..
Using the principle of diagonalization, we can find a string cok = x n x22 x33 such that
cok g S .
Xji = ajj + 1, if Ojj < 5
= 4, if aH . > 5
Now, it should be clear that :
(ok will differ from (Di at the first place
tok will differ from co2 at the second place
Scanned by CamScanner
V Theory of Comp. Sei, (MU-Sem, 5-Comp.) 8-10 Undecidability
3 Regular X -> aY 1 a 1 £ 1 Ya FA
where,
X,YeVandae T
Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 8-11
8.4 Un-decidability
MU - May 2013"
A problem is said to be decidable if there exists a Turing machine that gives the correct
answer for every statement in the domain of the problem. Otherwise, the class of problems is
said to be un-decidable. These two statements are equivalent :
1. A class of problems is un-decidable.
Scanned by CamScanner
Step 1 • Let us assume that the halting problem of a Turing machine is solvable. There exists
a machine H/say). H j takes two inputs :
1. A string describing M.
2. An input co for machine M.
Hj generates an output “halt” if Hj determines that M stops on input co; otherwise H
outputs “loop”. Working of the machine Hj is shown below.
M
halt
W H
loop
Step 2 : Let us revise the machine Hj as H 2 to take M as both inputs and H 2 should be able to
determine if M will halt on M as its input. Please note that a machine can be
described as a string over 0 and 1.
M halt
H2
loop
Step 3 : Let us construct a new Turing machine H 3 that takes output of H 2 as input and does
the following :
. . 1. If the output of H2 is “loop” than H3 halts.
2. If the output of H 2 is “halt” than H 3 will loop forever.
Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 8-13 Undecidability
Example 8.4.1 : Show that the following problem is un-decidable. “Given a Turing machine
T,T halts on every input string”.
Solution : This is also known as totality problem
This can be proved by showing, that the halting problem is reducible to the totality
problem.
• That is, if an algorithm can solve the totality problem, it can also solve the halting
problem. Since, no algorithm can solve the halting problem; the totality problem must
also be unsolvable.
Reduction step (from halting problem to totality problem) :
For any Turing machine M and input 0), we construct Mj :
1. Mj takes an arbitrary input, ignores it, and runs M on to.
2. M, halts on all inputs if and only if M halts on input to.
Thus, Mj halts on all inputs also tells that M halts on input cd, which is solution to the
halting problem. Hence, the totality problem is un-decidable.
8.4.2 Un-decidability of Post Correspondence Problem :
MU - May 2006, Dec. 2006, May 2007, May 2008, Dec. 2008, May 2009,
BH M ay 2010, Dec. 2010, May 2011, Dec. 2011, May 2012, May 2016
Definition of post correspondence problem (PCP) : Let A and B be two non-empty lists of
strings over E. A and B are given as below : .V;
A = {Xj, x2, x3 . . . x k } -
B !
= {yi»y2,y3
We say, there is a post correspondence between A and B if there is a sequence of one or
more integers i, j, k . . .m such that :
The string Xj Xj. . . xm is equal to yj . . . ym .
Example : Does the PCP with two lists :
A = {a, aba , ab) and ;
B = {a3, ab, b)
have a solution ?
We will have to find a sequence using which when the elements of A and B are listed,
will produce identical strings.
The required sequence is (2, 1, 1 , 3)
A 2 Aj A] A3 = aba a aab = ab a b
B2 Bj Bi B3 = aba 3 a 3 b = aba6 b 1
• . ' » ’i i* i y 11
Scanned by CamScanner
” Theory of Comp, Sci. (MU-Sem. 5-Comp.) 8-14 Undecidability
Example 8.4.2 : Determine the solution for following Instance of PCP. MU - Dec. 2007
List A List B
1 ..........
col xl
1 01 0
2 110010 0
3 1 1111
4. 11 01
Solution :
The PCP has a solution. The required sequence is (1, 3, 2, 4, 4, 3)
co1(O3co2 cd4(D4co3 = 01111001011111
x1 x 3 x 2 x4 x4 x 3 = 01111001011111
Solution :
A 2 and A 3 differ from B 2 and B 3 at the first of place. Therefore, we must pick Aj and B j
Sequence String
(1) (Aj = 10) (B t = 101)
The next string to be picked up must be A3 and B 3 . Any other sequence will not lead to a
solution.
Strin
Sequence 8
(1>3) (A,A3 = 10101) (B,B 3 =101011)
The next string to be picked up must be A3 and B3. Any other sequence will not lead to a
solution. ...
S,rin
Sequence 8
(1 3 3) (A!A3 A3 = 10101 101) (B1 B3 B 3 = 101011011)
There is only choice of next string. This choice is A, and B 3 . This does not lead to a
solution. The PCP has no solution.
Solution: ... .
The post correspondence problem can be used to prove the un-decidability of whether a '
given CFG is ambiguous. ’
CamScanner
Scanned by CamScanner
v Theory of Comp. Sei. (MU-Sern. 5-Comp.) 8-15 Undecidability
Consequently, if G AB is ambiguous, then the post correspondence problem with the pair
(A, B) has a solution. Conversely, if G AB is unambiguous, then the post correspondence cannot
have a solution.
Scanned by CamScanner
y Theory of Comp. Sci (MU-Sem, 5-Comp.) 8-16 Undecidability
If there exists an algorithm for solving the ambiguous problem, then there exists an
algorithm for solving the post correspondence problem. But, since there is no algorithm for the
post correspondence problem, the ambiguity of CFG problem is unsolvable.
Example 8.4.5 : Prove that the blank tape halting problem is un-decidable.
Solution :
The halting problem of Turing machine can be reduced to blank-tape halting problem.
Given a Turing machine M, determine whether or not M halts if started with a blank tape. This
is un-decidable.
Reduction :
Suppose, we are given some Turing machine M and some string co. We first construct
from M a new machine M1 that starts with a blank tape, writes co on it, then positions itself on
the first symbol of co in starting state q0 .
After this, Mj will halt on a blank tape if and only if M halts on co.
Conclusion :
Since, the halting problem of the Turing machine is un-decidable, the same must be true
for the blank tape halting problem. . <
Example 8.4.6 : Let G! and G2 be context free grammars, and r be a regular expression.
Then the following are un-decidable :
(a) 1(0 n L(G2 ) = <t> (b) L(G1) = L(G2)
(c) 1(60 = L(r) (d) L(G1 ) g L ( G 2 )
(e) L(R)cL(G1 )
Solution :
(a) L(Gt) n L(G2 ) = <|> is un-decidable :
Proof :
Let Gj be a grammar that generates
L(Gj) = {coCcoR such that co e Z* and C £ S )
x
Let P G a member of P will be of the form (Uj Vj)
Let G 2 be a grammar that generates
• R R R
L(G2 ) = { U) u2 . . . un Cv n v|1 _ 1 ... Vj. I n S 1 and(Ui, e P}
• Gj and G2 can be constructed easily.
• L(G1) n L ( G 2) = {<oCcoR such that co is match of P)
L(Gj) n L(G2) is the set of solutions to this instance of PCP. The intersection is empty if
and only if there is no solution.
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 8-17 Undecidability
If there is an algorithm for finding whether L(G|) n L(G 2) is 0 then there exists an
algorithm for solving the post correspondence problem. But, since there is no algorithm for the
post correspondence problem there is no solution for
L ( G j ) n L ( G 2) = < ?
(b) L(Gj) = L(G 2 ) is un-decidable :
Proof :
Un-decidability of L(Gj) = L(G2 ) will imply un-decidability of similar unsolvable
problem about Turing machine.
Assumptions :
L(Gj) = L(G 2 ) is decidable
Construction of TM for G1 and G2 .
We can design a TM for L(Gj) and similarly for L(G 2). We can design a Turing machine
Mj for L(Gj) as follows :
1. Given an input string co, machine Mj saves the string co on its tape.
2. Machine Mi can produce all strings over the alphabet of Gj in a lexicographic order.
3. Whenever M j produces a string derivable by LCG , it compares the last string in
derivation with the saved string co. M j halts if they are same otherwise Mj continues to
generate later strings. - ....
Obviously, L(Gj) is Turing acceptable. If a language is Turing acceptable then it must
be the output language of some Turing machine.
Let us assume that L(Gj) is acceptable by M j and L(G 2 ) is acceptable by M 2 .
From the assumption, L(Gj) = L(G2)
For every string co, e L(Gi), co, should be in L(G2) and for every string C0j e (0,
should be in LCGj).
co, e L(Gj) is similar to whether Mi accepts (Oj and
(Dj 6 L(G 2 ) is similar to whether M 2 accepts co,.
Solvability of above problem contradicts the halting problem of Turing machine.
(c) L(G X) = L(r) is un-decidable :
Proof :
Let the grammar G t and the regular expressions r is defined over the alphabet
Let the language L(G]) is the output language of the Turing machine M P
Scanned by CamScanner
Theory ot Comp, Sci. (MU-Sem, 5-Comp.) 8-18 Undecidability
8.4.3 Rice’sTheorem
MU - Dec. 2012, May 2013, May 2014, May 2015, Dec. 2015,
BM TMav2016, Dec. 2016, May 2017, Dec 1_2017_
“Every property that is satisfied by some but not all recursively enumerable language is
un-decidable”. Any property that is satisfied by some recursively enumerable language but not
all is known as nontrivial property. We have seen many properties of R.E. languages that are
un-decidable. These properties include :
1. Given a TM 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 ?
The Rice’s theorem can be proved by reducing some other unsolvable problem to
nontrivial property of recursively enumerable language.
“Let o be a class of languages that is effectively closed under concatenation with regular
sets and union, and for which L = S* is un-decidable for any sufficiently large fixed S. Let P
be any non-trivial property that is true for all regular sets and that is preserved under a, where a
is single symbol in S. Then P is un-decidable for a”.
Greibach theorem can be used to prove that many problems related to CFG are un-
decidable.
Scanned by CamScanner
CamScanner
Theory of Comp. Sci, (MU-Sem. 5-Comp.) 8-19 Undecidability
2. Partial function :
• Some initial functions are taken as primitive recursive functions. These initial functions
are
1
1. Zero function ■
2. Successor function
3. Projection function
• A function derived from combination/composition of primitive recursive functions is
primitive recursive. ... r
The zero function Z is defined by Z(x) = 0
The successor function S is defined by S(x) = x + 1
The projection function u" is defined by
n
U. (xj,x2,...xn) = Xi
The projection functions for selecting 1th element from a tape containing n elements.
Increment it by 1
Scanned by CamScanner
Theory of Comp, Sci. (MU-Sem, 5-Comp.) 8-20 Undecidability
Thus the function f is obtained by applying composition and recursion finite number of
times to initial functions. Hence, f is primitive recursive.
8.6 University Questions a n d Answers
Dec. 2005
■ ............................................ - ■ — - -
May 2006
Dec.2006
May 2007
•; • -:v' ..
Q.7
-r'
xl
1 01 0
2 110010 0
3 1 1111 :
4 11 01
(Example 8.4.2) (5 Marks)
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 8-21 Undecidability
Q. 8 Write notes on :
(i) Halting problem (Section 8.4.1)
(ii) Greibach Theorem (Section 8.4.4) (10 Marks)
Q. 9 Write notes on :
(i) Post correspondence problem (Section 8.4.2)
(ii) Halting problem (Section 8.4.1) (10 Marks)
Q. 12 Explain the un-decidability of PCP ? Does PCP with following two list :
;
- A = (10, 01 1, 101) and B = (101, 1 1, 01 1) have a solution ? Justify your answer.
(Section 8.4.2, Example 8.4.3) (6 Marks)
Q. 14 Define post correspondence problem and prove that PCP with two lists
3 3
x = { b, bab , ba } and y = { b , ba, a } have a solution.
(Sections 8.4.2) (Note : a and b are interchanged) (10 Marks)
Scanned by CamScanner
8-22 Undecidability
Doc. 2010
_________
Q. 15 Write short notes on :
(a) Post Correspondence Problem (Section 8.4.2)
(b) Recursive and recursively enumerable languages (Section 8.1) (10 Marks)
May 2011
D
°°- g011
Q. 17 Write short notes on :
(a) Post correspondence problem (Section 8.4.2)
(b) Halting problem (Section 8.4.1) (10 Marks)
May 201 2
Dec. 2012
MSZM13
Q. 23 (5 Marks)
Scanned by CamScanner
Theory of Comp. Sci. (Mll-Sem. 5-Comp,) 8-23 Undecidability
May 2015
_________
Q. 24 Compare recursive and recursively enumerable languages. (Section 8.1) (5 Marks)
Q. 25 Write short note on : Rice theorem. (Section 8.4.3) (5 Marks)
Dec. 2015
— _____—
Q. 26 State and prove the halting problem. (Section 8.4.1) (5 Marks)
Q. 27 Compare recursive and recursively enumerable language. (Section 8.1) (5 Marks)
Q. 28 State and prove Rice’s theorem. (Section 8.4.3) (10 Marks)
Dec 2016
-
Q. 32 Write detailed note on : Recursive and Recursively Enumerable Languages.
(Section 8.1) e (10 Marks)
Chapter Ends.,.
□□□
Scanned by CamScanner
Appendix A
Theory of Computer
Science
Now, an b n ” 1
can be written as
• r
a a
Scanned by CamScanner
V Theory of Comp. Scl. (MU-Sem. 5-Comp.) A-2 Appendix
Ans. :
Step 3 : Let us check whether xy z for L = 2 belongs to L.
(i) Left most derivation :
2 s r 2 n s r n 1
x y z = a (a ) a - - b -
S -> ICtSeS [using S -> iCtSeS]
3 2r n s r n 1
=a a a - “ b “
-> IbtSeS [using C —> b ]
1
sa 'b""
—> IbtaeS [usings -» a]
n + r 1
Sincer>0, a ~ L.
-> IbtaeiCtS [using S-4- iCtS]
Hence, by contradiction, w e can say that the
-> ibtaeibts [using C -> b]
given language Is not regular.
-> Ibtaeibta
Q. 3(a) Construct PDA accepting the language
L = {a 2” b n I n O). (10 Marks) (ii) Rightmost derivation :
Ans. :
S -> ICtSeS [using S —> iCtSeS]
1. For every pair of a's one x is pushed o n to the stack
-> ICtSeiCtS [using S -> iCtS]
2. For every b, one x Is popped o u t from t h e stack.
-> iCtSeiCta [using S —> a]
3. Finally the stack should contain t h e initial stack symbol
—> iCtSeibta [using C - > b ]
Zo-
Transition table (5) —> iCtaeibta [usingS—> a]
4. 5(q b a , x ) = (q 0, x x ) //l\V\
5. 8(q 0, b , x )
6. 8(q 2 , b, x)
- (q 2 , e )
= (q 2, e )
Tbi ‘ a
a
lets
7. 3(q 2, €, Zo) = (q 2, e )
ba
Accepting through empty stack
Fig. 1-Q. 3(b)
Thus, the PDA M = ({q 0, q b q 2 }, {a, b), {x, Zq), 6, q 0, Zo, {<t>})
Q. 3(b) Consider the following grammar (10 Marks) (iv) The grammar can be shown to be ambiguous by
drawing two different derivation trees for the string
S—> I C t S 11C t S e S I a
Ibtibtaea as shown in Fig. 2-Q. 3(b)
C—>b
Scanned by CamScanner
Appendix
A —> B | S
°- 1 0,1
Qo)------►-
B -> b |e
Ans. : 0
1. Nullable set of symbols = (B, A)
Re-writing grammar after removing e-production,
we get, , Fig. 1-Q. 5(a)
A -> Bls
0
B -> b 0,1
_ 0
2. Re-writing grammar after removing unit productions 1
0,1
(A —> B, A -> S), we get
S -> ASlSA |ASA | aB l a
A —> b | AS ISA |ASA | aB l a
Fig. 2-Q. 5(a)
B —> b
Q. 5(b) Construct Moore and Mealy Machine to convert
3. Every symbol i n a, in production of the form A —> a each occurrence of 100 by 101. (10 Marks)
where | a I > 2 should be a variable. This can be done
Ans. :
by adding the production
1. Mealy Machine
Ci —> a o/o 1,1
1/1 0/0
The set of productions become,
1/1
S -> AS I SA I ASA |CiB l a
A —> b I AS ISA |ASA I CiB I a
B -> b 0/1
Ci a Flg.1-Q. 5(b)
Ci —> a
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) A-4 Appendix
Q. 6(a) Write short note on Closure properties of Context Q. 6(c) Write short note on Rice’s Theorem.
Free Language. (Section 8.4.3) (2.5 Maries)
(Section 5.7.1) (2.5 Martes) Q. 6(d) Write short note on Moore and Mealy Machine
Q. 6(b) Write short note on Applications of Regular (Section 2.4) (2.5 Marks)
expression and Finite automata. Q. 6(e) Write short note on Universal Turing Machine
(Section 3.10) (2.5 Martes) (Section 7.5) (2,5 Martes)
□□□
Scanned by CamScanner
• • 1
*
MU
Microprocessor
Harish G. Narula
Theory of Computer Science
Dilip Kumar Sultania
coming soon
Head Office
ISBN : 978-93-89. 9-95-3
lony, Aranyeshwar Corner,
91-20-24221234, 91-20-24225678
like us at:
o®oo©o
Tech knowledgepublications
Scanned by CamScanner