50% found this document useful (2 votes)
2K views589 pages

TCS Book

Uploaded by

Navnath Auti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
2K views589 pages

TCS Book

Uploaded by

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

2019

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

Dilip Kumar Sultania


B.Tech.(hons.) Computer Science and Engineering
LET. , Kharagpur.

TTM MO46A Price? 4 5 5 / -

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.

First Edition : August 2018


Second Revised Edition : July 2019 (TechKnowkdge Publications)

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.

Printedaty 37/2, Ashtavinayak Industrial Estate, Near Pari Company,


'' i , : * *"'Narhe, Pune, Maharashtra State, India. Pune -411041

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.

[CSC504] (FID : MO46) (Book Code : MO46A)

(Book Code : MO46A)

Scanned by CamScanner
Preface

My Dear Students*
\ ' * I ' .’ ' X VA’ i * 1 •- * r> ii ' Up c V•
»' < i • * »■ 4.

I nm extremely happy to come



out with this edition of
“ Theory of Computer Science” for you. The topics within the chapters have been

arranged in a proper sequence to ensure smooth flow of the subject.

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.

I am thankful to the staff members of TechKnowledge Publications and others


for their efforts to make this book as good as it is. I have jointly made every possible
efforts to eliminate all the errors in this book. However if you find any, please let me
know, because that will help me to improve further.

I am also thankful to my family members and friends for patience and


encouragement.

- Dilip K u m a r Sultania
□□□

(Book Code : MO46A)

Scanned by CamScanner
Syllabus

Theory of Computer Science


(Mumbai University) _______

Course Code Course Name Credits

CSC504 Theory of Computer Science 4

Course objectives :

1. Acquire conceptual understanding of fundamentals of grammars and languages.

2. Build concepts of theoretical design of deterministic and non-deterministic finite automata and
push down automata. ’ r ■. •

3. Develop understanding of different types of Turing machines and applications.

4. Understand the concept of Undecidability.


■ - • .< -■ ....

Course outcomes : On successful completion of course learner will be able to :

1. Identify the central concepts in theory of computation and differentiate between deterministic and

nondeterministic automata, also obtain equivalence of NFA and DFA.

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.

4. Design pushdown automata to recognize the language.

5. Develop an understanding of computation through Turing Machine.

6. Acquire fundamental understanding of decidability and undecidability.

(Book Code : MO46A)

Scanned by CamScanner
Prerequisite : Discrete Mathematics

Module Unit ■■jg; -r . . . 1


5
'- ■ No.
W No. T
*
________ _ _ _ _

w
1.0 Basic Concepts and Finite Automata 09 03
1.1 • Alphabets, Strings, Languages, Closure properties.

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

1.2 • Deterministic Finite - Automata (DFA) and


Nondeterministic Finite Automata (NF A) : Definitions,
transition diagrams and Language recognizers

• NF A to DFA Conversion

• Equivalence between NF A ’ with and without


e- transitions

• Minimization of DFA

• FSM with output: Moore and > Mealy machines,


■ 1
Equivalence

• Applications and limitations of FA

________________________ (Refer chapters 1 and 2)


2.0 Regular Expressions and Languages
06 02
2.1 • Regular Expression (RE)

• Equivalence of RE and FA, Arden‘ s Theorem

2.2


RE Applications

Regular Language (RL)

Closure properties of RLs


1
• Decision properties of RLs

• Pumping lemma for RLs (Refer chapter 3)

____ __________ • I
(Book Code : MO46A)

Scanned by CamScanner
tuns
■ ? 5s f
Bl i i
Theory Tutorial
s ' '
H»-

3.0 Grammars 08 03

3.1 • Grammars and Chomsky hierarchy.

3.2 • Regular Grammar (RG)

• Equivalence of Left and Right linear grammar

• Equivalence of RG and FA

3.3 ' Context Free Grammars (CFG)

• Definition, Sentential forms, Leftmost and Rightmost


derivations, Parse tree, Ambiguity.

• Simplification and Applications.


/
• Normal Forms: Chomsky Normal Forms (CNF) and

• Greibach Normal Forms (GNF).

• CFLs - Pumping lemma, Closure properties

(Refer chapters 4 and 5)

4.0 Pushdown Automata(PDA) 04 01


X
4.1 • Definition, Transitions .Language of PDA

• Language acceptance by final state and empty stack

• PDA as generator, decider and acceptor of CFG.

• Deterministic PDA , Non-Deterministic PDA

• Application of PDA. (Refer chapter 6)

(Book Code : MO46A)

Scanned by CamScanner
W T>


Module Topic. 1 heory Tutonal.;

°’ Hrs.
""

5.0 Turing Machine (I'M) 09 03

5.1 • Definition, Transitions

• Design of TM as generator, decider and acceptor

• Variants of TM : Multitrack, Multitape


r » 1 . j n > r* • > • . ..
• Universal TM.
Af j r . , ■* r
• Equivalence of Single and Multi Tape TMs
i
Applications, Power and Limitations of TMs
‘ ■'*' ■ ’ . •'< v I m . - ’ • . . . ■■■a

• Context Sensitivity and Linear Bound Automata.

(Refer chapter 7)

6.0 Undecidability
----------------- •< ~ - ,-r 03 01
6.1 • Decidability and Undecidability

• Recursive and Recursively Enumerable Languages.


i
• Halting Problem

• Rice‘s Theorem i
*
• Post Correspondence Problem
*• » 1, , 1

_________________________________ (Refer chapter 8)

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.1 Basics of Languages ......... ............................................................................... .................................

1.1.1 Kleene Closure ................... ; ............................................................................................

1.1.2 Recursive Definition of a Language .................. ...................................................... 1"4

1.1.3 Prefix and Suffix of a String ................. ........ 1’5

1.2 Grammar (Context Free) ................................. 1’5

1.2.1 Notations .........;... ..........'................... •......................... 1"

1.2.2 The Language of a Grammar ...... ....; ....... ................ ................................... I -7

1. 2.2. 1 Sentential Form ........... A....................................................................... I -7

1.2.2.2 Parse Tree . ................. ......; ......... l"9

1.3 Chomskey Hierarchy . ..... ...... .......................... •.................................. 1-13

1.3. 1 Type 3 or Regular Grammar ...... ..... ........ ............... ........................ I -13

1.3.2 Type 2 or Context Free Grammar .: ............ A....:......... 1-14

1. 3.3 Type l or Context Sensitive Grammar .............. ;... .................................................... 1-14

1.3 .4 Type 0 or Unrestricted Grammar ...............: ................;............................................ 1-15

1 .4 Finite Automata (FA) .......... .................. A...... ...... .......................................... l - l 5

1.4.1 Working of a Finite Automata . .......... 1-15

1.4.2 Some Important Terms ....................... ...; ........... 1-17

L4.2.1 Alphabet ..................................... 1-17

L4.2.2 Strings (words) ................................................. ;........................................................... 1-17

1.4.2.3 Languages ...................... ........................................................ ..................................... I -1 9

1.4.3 Application of Finite Automata ................................................................................. 1-19

1. 5 Finite State Machine (FSM) .......................................................................................................... I -20

1.6 University Questions and Answers *................................................................................... I -20


t . • Chapter Ends........................................................................................................................... . . l - 2 l

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2
_ _Table of Content*

Chapter 2 : Finite Automata 2-1 to 2-151


2.1 Introduction to Finite Automata ............................................................... _
............................. z-i
2.1.1 Working of a Finite Automata ...........................................
........................... £2
2.1.2 Some Important Terms ......................

2.1.2.1 Alphabet ............................

2.1. 2.2 Strings (words) ....................... . ._

2. 1.2.3 Languages ............................ . ....

2.1.3 Application of Finite Automata ........ ...........


2.2 Deterministic Finite Automata (DFA) .......
2.2.1 Definition of a DFA ................
2.2.2 Representation of a DFA ......................

2.2.3 Designing a DFA ..................


2.2.4 Examples on DFA ..............

2.2.4.1 Examples on Counting of Symbols


» •- 2.2.4.2 Examples on Substring ..............

2.2.4.3 Examples of Divisibility ................


2.2.5 Language of DFA ...........................
2.2.6 Equivalence of DFAs ................ . . .
■ 2.2.7 Closure Property of Language Accepted by a DFA ................
........................ 2-44
2.2.7. 1 Union, Intersection, Difference ...........

2.2.12 Complementation ......................

2.2.8 Minimization of DFA ..................

2.2.8. 1 Algorithm for Minimization DFA’s ........

2.3 Non-deterministic Finite Automata .....................

2.3.1 Definition of NFA ................................

2.3.2 Processing of a String by NFA .................

2.3.3 NFA to DFA Conversion ...................................................

Scanned by CamScanner
V Theory of Comp. Sei, (MU-Sem. 5-Comp.) 3 Table of Contents

2.3.4 NFA with E-Transitions 2-88

2.3 .4.1 Equivalence of e-NFA and NFA 2-89

2.3. 4.2 The Formal Notation for an e-NFA 2-90

2.3 .4.3 s-Closures 2-91

23.4.4 e-NFA to DFA 2-93

2.3.5 Difference between NFA and DFA 2-102

2.4 Finite Automata as Output Devices ...................................... 2-102

2.4.1 A Sample Mealy Machine . .................................................. 2-103

2.4.2 Formal Definition of a Mealy Machine 2-104

2.4.3 A Sample Moore Machine 2-105

2.4.4 Formal Definition of a Moore Machine .......................................................... 2-1 06

2.4.5 Conversion of a Mealy Machine into a Moore Machine ................................... 2-1 17

2.4.6 Conversion of a Moore Machine into a Mealy Machine 2-126

2.5 Minimization of a Mealy Machine ................................................ 2-132

2.5.1 Difference between Moore and Mealy Machines ......................... 2-135


. r

2.6 University Questions and Answers ................................................. 2-136


• Chapter Ends 2-151

Module 2

Chapter 3 : Regular Expressions and Languages 3-1 to 3-84


3.1 Introduction ....3-1
3.2 Finite Automata Representing a Regular Expression 3-4
3.2.1 Composite Finite State Automata ........................................................................... 3-4
3.3 Determination of Regular Expression 3-9
3.3.1 Language Generated by a Regular Expression 3-9
3.3.2 Basic Properties of Regular Expressions
:
3.4 DFA to Regular Expression ...........................................*
3.4.1 State/Loop Elimination Process . ..............................................
3.4.1. 1 A Generic One State Machine
3.4.1.2 A Generic Two State Machine ............................................. 3-30

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

3.6.4 Application of Pumping Lemma .............. .................................................................. 3.5$

3.7 Closure Properties of Regular Language ......... 3. 4

3.7.1 Regular Language is Closed under Union ..........................................


....................... 3-65
3.7.2 Regular Language is Closed under Concatenation ............................
....................... 3-66
3.7.3 Regular Language is Closed under Kleene Star . ........ 3-67
3.7.4 Regular Language is Closed under Complementation .....................
....................... 3-67
3.7.5 Regular Language is Closed under Intersection ; ............. ......... 34:7
3.7.6 Regular Languages are Closed under Difference ..............................
...................... 3-68
3.7.7 Regular Languages are Closed under Reversal ...
3.8 Decision Properties of Regular Languages

3.9 Myhill - Nerode Theorem ............ - ..............

3.10 Application of RE .......................

3.10.1 R.E. in Unix ................


* • .
3.10.2 Lexical Analysis ..........

3.11 University Questions and Answers •

• Chapter Ends ...........................

Module 3 1
__ 1

Chapter 4 : Regular Grammar (RG)


4-1 to 4-27
4. 1 Regular Grammar .......................................
.4-1
4.2 DFA to Right-Linear Regular Grammar ..
.4-2
4.2. 1 Right Linear Grammar to DFA
4-4

Scanned by CamScanner
Theory of Comp. Scl. (MU-Sem. 5-Comp.) 5 Table of Contents

4.3 DFA to Left-Linear Grammar ...................................................... 4-8


4-1 0
4.3.1 Left Linear Grammar to DFA .......................... .

4.4 Right Linear Grammar to Left Linear Grammar ........................... ........................................... 4-12

4.5 Left Linear Grammar to Right Linear Grammar ......................................... 4-15

4.6 University Questions and Answers ..................... 4-27


• Chapter Ends............................................................ ••................. 4-27

Chapter 5 : Context Free Grammars (CFG) ; ' 5-1 to 5-116

5.1 An Example to Explain Grammar ................................................................ .............................. o-i

5.2 Context Free Grammar ............................................................................................................... 5-5


5.2.1 Notations ........... 5-5
5.2.2 The Language of a Grammar . .......... 5-6
5.2.2. 1 Sentential Form ........ .......... ......... 5-7
•5.2.2.2 Parse Tree ......................................... 5-8
5.2.3 Writing Grammar for a Language .............. 5-14
5.2.3.1 Union Rule for Grammar . ...................................................................................... 5-21
5.23.2 Concatenation Rule for Grammar ......... .............. .................................... 5-22

5.3 Ambiguous Grammar ....................................... ...... ......... ................... ..................... 5-39

5.4 Simplification of CFG ...............................................................................................................


5.4.1 Elimination of Useless Symbols .............................. ............................................

5 .4. 1 .1 Non-generating Symbols .........................................................................................

5.4.1.2 Non-reachable Symbols ..................... ....................................................................

5.4.2 Elimination of e-productions ..............................•........................•........................

5.4.3 Elimination of Unit Productions .......................... ................................. ................

5.5 Normal Forms for CFG ................................................... 5-69

5.5.1 Chomsky Normal Form (CNF) ..................... 5-69

5-69
5.5. 1.1 Algorithm for CFG to CNF Conversion ........
5-79
5.5.2 Greibach Normal Form (GNF) .....................

5. 5.2.1 Removing Left Recursion ............................. 5-80

5.5.2.2 Algorithm for Conversion from CFG to GNF 5-81

Scanned by CamScanner
V Theory of Comp. Sd, (MU-Som. 5-Comp.) 6 Table of Contents

5.6 Pumping Lemma for CEO „ 5.

5*7 Properties of Context-free Languages 5-100

5.7.1 Closure Properties 5-100

5.7. 1.1 CFL is Closed under Union 5-100

5. 7.1.2 CFL is Closed under Concatenation ............................ 5-101

5.7.1.3 , CFL is Closed under Klcene Star 5-101

5.7. 1.4 CFL is not Closed under Intersection 5-102

5.7.1 .5 CFL is not Closed under Complementation 5-102

5.7.1.6 Intersection of CFL and RL 5-103

5.7. 1.7 CFL is Closed under Reversal 5-104

5.7.2 Algorithmic Properties (Decision Properties) 5-104

5.8 University Questions and Answers ....................................................... 5-104


• Chapter Ends 5-116

Chapter 6 : Pushdown Automata (PDA) 6-1 to 6-70

6. 1 Introduction to Pushdown Automata (PDA) ..................................................................... 6-1

6.2 The Formal Definition of PDA . ................................................... $.3

6.3 Instantaneous Description of a PDA ......................................................................... 6-6

6.4 The Language of a PDA .................................................... 5.9

6.4.1 Acceptance by Final State

6.4.2 Acceptance by Empty Stack 5.9


J e

6.5 Non-deterministic PDA (NPDA) 6-25

6.6 Pushdown Automata and Context Free Language 6-32

6.6.1 Construction of PDA from CFG 6-32

6.6.2 Construction of CFG from PDA ...................................................... 6-38

6.7 Deterministic Pushdown Automata (DPDA) 6-55


I
6.7. 1 Regular Language and DPDA 6-55

Scanned by CamScanner
Theory of Comp. Scl, (MU-Sem. 5-Comp.) 7 Table of Contents

6.8 Application of PDA ..................................................................................................................... 6-57

6.9 Parsing .......................................................................................................................................... 6-57


7
6.9.1 Top-Down Parsing ...........................................................

6.9.2 Bottom-Up Parsing ..................................................................... $-61

6.10 Multistack DPDA ................................................................. ...................................................... -62

6.1 1 Power of Various Machines ............................................................................. -63

6.12 University Questions and Answers .................................................. 6-63


• Chapter Ends.. . .................................................................................................................... 6-70

Chapter?: Turing Machine (TM) 7-1 to 7-76

7.1 Introduction to Turing Machine ...................................................................................................7-1


!
...... * . . ,. . . . . 1• * k I J

7.2 The Formal Definition of Turing Machine .................................................................................. 7-4

7.2. 1 A String Accepted by TM ........................................................... 7-6

7.2.2 Instantaneous Descriptions for Turing Machines ...................................................... 7-8


1
............. t ... .'I

7.3 Turing Machines as Computer of Functions ............................ 7-32

7.4 Extension of Turing Machine ..................................................................................................... 7-59

7.4.1 Two-way Infinite Turing Machine .............................. 7-59

7.4.2 A Turing Machine with Multiple Heads ................................................................. 7-60

7.4.3 Multi-Tape Turing Machine .................................................................................... 7-60

7.4.4 Non-deterministic Turing Machine ............................ 7-64

7.5 Universal Turing Machine .......................................................................................................... 7-66

7.5.1 Church-Turing Hypothesis ....................................................................................... 7-67

7.5.2 Power of Various Machines ..................................................................................... 7-67

7.6 Linear Bounded Automata .......................................................................................................... 7-68

7.7 University Questions and Answers 7-68


• Chapter Ends...................................................................................................................... 7-76

Scanned by CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 8 Table of Contents

Chapter 8 : Undecidability 8-1 to 8-23

8J Recursive and Recursively Enumerable Language ........................................................................ g.j

8.1.1 Turing Acceptable Language ......................................................;................................ g . |

8.2 Enumerating a Language .............................................................. ...................................................

8.2.1 Finite and Infinite Sets ................ g.g

8.3 Compare Type 0, Type 1, Type 2 and Type 3 Grammars ........................................................... 8-10

8.4 Un-decidability .................................................. ................................................. 8-11

. 8.4.1 Halting Problem of a Turing Machine .................... ........... 2_i t

8.4.2 Un-decidability of Post Correspondence Problem ..........................

8.4.3 Rice’s Theorem ......................... .................


- ................................... ' •
8.4.4 Greibach Theorem ......... ....................
8.5 Partial Recursive Function :............................................... o ,o
......................... . r-. • .. (i . f . •
8.6 University Questions and Answers . .............................. o

• Chapter Ends... . ...... ......................... ;....... ■ t


• Appendix A : Solved University Question Paper of Dec. 2018..............
............. A-l to A-4

□□□

Scanned by CamScanner
Basic Concepts and
*
Finite Automata
I ' » ' 1

Module 1

1 .1 Basics of Languages .

A subset of strings over an alphabet is a language.


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 alphabet.
2. Z{A,B,...Z), the set of uppercase letters.
3. Z{ 0, 1 ,2, ...9 ) , the decimal alphabet,
A string is a finite sequence of symbols from the alphabet. For example .
1. 101 10 is a string from binary alphabet.
2. 5920 is a string from decimal alphabet.
3. ‘STAMP’ is a string from roman alphabet.
A string having no symbol is known as an empty sting. An empty string is denoted by e.
• The exponential notation is used to express the set ot all strings ot a certain length.
L k = set of strings of length k, each of whose symbol is in Z.
For example,
if Z = {0, 1)
then Z 1 = {0, 1)
3
Z 2 = {00,01, 10, 11)
and Z° = {e)
o

• The reversal of string cd is denoted by co .

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

Thus, a language can be specified by :


L = {cd G E* I (D has die given property)
Some examples of language are :
1. A language of all strings, where die string represents a binary number.
{0, 1,00,01, 10, 1 1 , . . . }
2. The set of strings of 0’s and l’s with an equal number of each.
{e, 01, 10, 0011,0101, 1010, 1100, . . . )
3. The set of strings of 0’s and l’s, ending in 11.
{11,011, 111,0011,0111, 1011, ...}
4. E* is a language for any alphabet E.
5. 0, the empty language, is a language over an alphabet.
6. {e), the language consisting of only the empty string, is also a language over any
alphabet.
1.1.1 Kleene Closure

Given an alphabet E, the Kleene closure of E is a language given by


2
Z* = Z Z'uZ ....................... '.
I I \
E Strings of Strings of
length 1 length 2
For example :
1. If E = {x), then
E* = {e, x, xx, ....)
2. If E = {0, 1 ) , then
E* = {E, 0, 1,00,01, 10, 11 )
If L is a language, then L* is the set of all finite strings formed by concatenating words
from L. Any word can be used any number of times including zero time. £ is a member of L*.
Example : If L = {aa, b) then
L* = (e, b, aa, bb, bbb, baa, aab, ...)
It may be noted that a always appears in pair.
• If E = 0 (an empty language)
Then,
Z* = (e)

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
+

epsilon but L* will always contain an epsilon.


‘If L itself contains an epsilon then there will be no difference between L + and L*.
1.1.2 Recursive Definition of a Language

A language over an alphabet £ can be described recursively. A recursive definition has


three steps :
1. Specify some basic objects in the set.
2. Specify the rules for constructing more objects from the objects already known.
3. Declaration that no objects except those constructed as given above are allowed in
the set.
Example : Let us try to define a language of positive integers
L = {1,2,3 }
The object 2 can be derived from 1 by adding 1 to it.
The object 3 can be derived from object 2 by adding 1 to it.
Thus, if 1 is taken as the base object then we can derive any positive integer from it
Rules are given below :

Rule 1 : 1 is a positive integer


Rule 2 : If x is a positive integer, then so is x + 1.

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

A prefix of a string is a substring of leading symbols of the string.


Example : The prefixes of the string “abc” are { e , a, ab, abc)
A suffix of a string is a substing of trailing symbols of the string.
Example : The suffixes of the string “abc” are { e , c, be, abc)
1 .2 Grammar (Context Free) • •
A context free grammar G is a quadruple (V, T, P, S),
where,
V is a set of variables.
T is a set of terminals.
P is a set of productions.
S is a special variable called the start symbol S G V.
A production is of the form
Vj -» cq where Vj e V and oq is a string of terminals and variables.

Scanned by CamScanner
Theory of Comp, Sci, (MU-Sem, 5-Comp.) 1-6 Basic Concepts and Finite Automata

1 .2.1 Notations

1. Terminals are denoted by lower case letters a, b, c . . . or digits 0, 1, 2 . . .. etc.


2. Non-terminals (variables) are denoted by capital letters A, B, . . . , V, W, X . . .
3. A string of terminals or a word w 6 L is represented using u, v, w, x, y, z.
4. A sentential form is a string of terminals and variables and it is denoted by a, p, y etc.
CFG explained through an example

Let us consider English sentences of the form :


1. Mohan eats. 2. Soham plays. 3. Ram reads.
The first word of in the above sentences is a noun and the second word is a verb. A
sentence of the above form can be written as
<sentence> <noun> <verb>
Here, noun can be replaced with Mohan, Soham or Ram and <verb> can be replaced
with reads, plays or eats. We can write :
<noun> —> Mohan
<noun> —> Soham
<noun> —» Ram
I <verb> —» eats
<verb> —> plays
<verb> -» reads
In the above example :
1. <sentence>, <noun> and <verb> are variables or non-terminals.
2. Mohan, Soham, Ram, eats, plays and reads are terminals.
3. The variable <sentence> is the start symbol as a sentence will be formed using the
start symbol <sentence>.
Formally, the grammar can be written as :
G = (V, T, P, S), where
V = {<sentence>, <noun>, <verb>)
T = {Mohan, Soham, Ram, eats, plays, reads)
P = (<senlence> —><noun> <verb>,
<noun> —> Mohan,
<noun> —> Soham,
<noun> —> Ram,
<verb> —> eats,
<verb> —> plays,
<verb> —» reads
)

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

Every grammar generates a language. A word of a language is generated by applying


productions a finite number of times. Derivation of a string should start from the start symbol
and the final string should consist of terminals.
If G is a grammar with start symbol S and set of terminals T, then the language of G is
the set
*
L (G) = w I w G T* and S => w >
G

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

Let us consider a grammar given below :


S -»A1B (Production 1-1)
A —> OA I E (Production 1-2)
B —» OB I IB I £ (Production 1-3)
where, G is given by (V, T, P, S)
with, V = [S,A, B)
T = (0, 1)
P = (Productions 1-1, 1-2 and 1-3)
S = Start symbol
Let us try to generate the string 00101 from the given grammar.
S -> A1B [Starting production]
—►0A1B [Using the production A —> OA]

Scanned by CamScanner
2 Theory of Comp. Scl. (MU-Sem. 5-Comp.) 1-8 Basic Concepts and Finite Automata

-> 00A1B [Using the production A ->0A]


—> 00 IB [Using the production A —» e]
—> 0010B [Using the production B —> OB]
—> 00101B [Using the production B —> I B ]
—» 00101 [Using the production B —» e]
Thus the string 00101 e L(G).
In sentential form, derivation starts from the start symbol through a finite application of
productions.
A string a derived so far consists of terminals and non-terminals.

S => a I a e (V u T)*
G
• A final string consists of terminals.
• In left sentential form, leftmost symbol is picked up for expansion.
• In right sentential form, rightmost symbol is picked up for expansion.
• A string can be derived in many ways. But we restrict ourselves to :
1. Leftmost derivation.
!
2. Rightmost derivation. ' •
In leftmost derivation the leftmost variable of a (sentential form) is picked for
expansion.
In rightmost derivation the rightmost variable of a (sentential form) is picked for
expansion.
Example 1 .2.1 : For the grammar given below
S - > A1B
A —> 0A I e
B —> 0B I I B I e
Give leftmost and rightmost derivation of the string 1001
Solution :
(i) Leftmost derivation of 1001 (Leftmost variable is picked up for expansion.)
S —> A1B [Derivation starts from the start symbol]
-> I B [Using the production A -> e]
-» 10B [Using the production B -> 0B]
-> 100B [Using the production B —> 0B]
—> 1001B [Using the production B IB]
—> 1001 [Using the production B —> e]

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 1 -9 Basic Concepts and Finite Automata

(ii) Rightmost derivation of 1001 (Rightmost variable is picked up for expansion)


S —> A1B [Derivation starts from the start symbol]
-> A10B [Using the production B -» OB]
—> A100B [Using the production B —> OB]
-> A1001B [Using the production B —> IB]
A1001 [Using the production B —> e]
—> 1001 [Using the production A —» e]
1.2.2.2 Parse Tree
A set of derivations applied to generate a word can be represented using a tree. Such a
tree is known as a parse tree. A parse tree representation gives us a better understanding of :
1. Recursion 2. Grouping of symbols
A parse tree is constructed with the following condition :
1. Root of the tree is represented by start symbol.
2. Each interior mode is represented by a variable belonging to V.
3. Each leaf mode is represented by a terminal or e.
A string generated by a parse tree is seen from left to right.
Example 1.2.2 : For the grammar given below
S—>A1B
A 0A I e
B—>0BI 1 B l e
Give parse tree for leftmost and rightmost derivation of the string 1001.
Solution :
(i) Parse tree for leftmost derivation of 1001.
s

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

Rightmost derivation of 1001 is shown by a series of Figs. Ex. 1.2.2(g) to (Z)


It may be noted that the final parse trees in Fig. Ex. 1.2.2(f) and (Z) are identical.

Scanned by CamScanner
” Theory of Comp. Sci. (MU-Sem. 5-Comp.) 1-11 Basic Concepts and Finite Automata

Example 1.2.3 : For the grammar given below S -> 0S1 I 01


Give derivation of 0001 1 1 .
Solution :
1. Derivation in sentential form
S —> 0S1 [Using production S —> OS1]
S —> 00S 1 1 [Using production S —> OS1]
S — >000111 [Using production S —> 01]
2. Derivation using parse tree
S ■<— [Production S -> 0S1]

0
A s 1 *— [Production S -> 0S1]

— [Production S -> 01]

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

Fig. Ex. 1.2.4(a)


S -> aAS —> aSbAS —> aabAS aabbas -> aabbaa

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

Give the derivation of (a + b) * a + b.


Solution : For the grammar given in Example 1.2.5,
Set of variables V = {E, T, F )
Set of terminals Z = { + * ( ) a b )

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 + b) * a + b [Using production F —> a I b]


(ii) Derivation using parse tree f

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 can be classified on the basis of production rules. Chomsky classified


grammars into the following types :
1. Type 3 : Regular grammar
2. Type 2 : Context free grammar
3. Type 1 : Context sensitive grammar
4. Type 0 : Unrestricted grammar.
1 .3.1 Type 3 or Regular Grammar

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.

1 .3.2 Type 2 or Context Free Grammar

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.

1.3.3 Type 1 or Context Sensitive Grammar

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 :

The set of productions is given by :

S aSBC I aBC
P = CB —> B C , aB —> ab, bB —> bb, ►

A string of the form a n b n c n can be generated as given below.

1
"■'SCBC)"" [by applying S —> aSBC, (n — 1 ) times]
1
“■’aBC(BC)"’ [by applying S -> aBC]
1
“■'abC(BC)"' [by applying aB -> ab]

” - 1 abB n " l C n [by applying CB ■—> BC, n — 1 times ]


n. n — 11
ib bC [by applying bB —> bb several times]
n n-1
i b
L LbcC
[by applying bC —» be] • .
it. n n
[by applying cC —> cc several times ]

Scanned by CamScanner
WF Theory of Comp. Sci. (MU-Sem. 5-Comp.) 1 -1 5 Basic Concepts and Finite Automata

1.3.4 Type 0 or Unrestricted Grammar


MU - May 2013
Productions can be written without any restriction in a unrestricted grammar. If there is
production of the a -» 0, then length of a could be more than length of 0.
• Every grammar also is a Type 0 grammar.
• A Type 2 grammar is also a Type 1 grammar
• A Type 3 grammar is also a Type 2 grammar.

1 .4 Finite Automata (FA)


MU - Dec. 2012

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

Let us consider a T-flip flop.


A T-flip-flop has :
Serial _ _T
1. One input denoted by T, Input

2. Two outputs denoted by Q and Q.


3. It has two distinct states, defined by
the logical values of Q. The flip-flop Clock
is in q0 state when Q = 0 and it is in q t
state when Q = 1. Fig. 1.4.1 : A T-flip flop as a finite automata

Scanned by CamScanner
Basic Concepts and Finite Automata
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 1/16

4. A value of 1 applied to its input changes its state from q 0 to q , or from q, to q 0 .


A T-flip-flop can be used to check whether an input binary number contains an even
number of l’s. Let us assume that a binary number 101110011 is applied to input T of
flip-flop. Initial state of the flip-flop is q0 (i.e. Q = 0) __________

Input (T) 101 1 10011


- ■- .■ -

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

• This process is repeated again and again, i.e.


1. A symbol is read.
2. State of the machine (finite control) changes.
3. Reading head moves to the right.
• Eventually, the reading head reaches the end of the input string.
' ‘ t i v. ■ .t | , . -*7
• Now, the automaton has to say ‘yes’ or ‘no’. If the machine ends up in one of a set' of
finial states (q 2, q 3 ) then the answer is ‘yes’ otherwise the answer is ‘no’.

1.4.2 Some Important Terms


1. 4.2.1 Alphabet , ... .

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.

1.4.2.2 Strings (words) , ... 0 ... .... .

It is a finite sequence of symbols from.the alphabet.


For example,
1 101 10 is a string from binary alphabet.
5920 is a string from decimal alphabet.
‘STAMP’ is a string from Roman alphabet.
• 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 co is I co I.
For example,
If co = 01101
then I co I = 5
Length of an empty string is zero,
i.e., I E I = 0.
• The exponential notation is used to express the set of all strings of a certain length.
E k — set of strings of length k, each of whose symbol is in E-

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

A subset of strings over an alphabet E is a language. In other words, any subset of E* is


a language.
If £ is an alphabet, and L £ E*, then L is a language over E.
For example,
Li = [cd g {0, 1 }* I cd has an equal number of 0’s and 1’s]
i.e., Lj is a language over alphabets {0, 1 }, having equal number of 0’s and l’s.
.-. Lj = {£, 01, 10, 0011, 0101, 1100, 1010, ...}
Thus, a language can be specified by :
L = {cd g E* I co has the given property)
Some examples of language are 1

1. A language of all strings, where the string represents a binary number.


{0,1,00,01,10,11,...}
♦ -
2. The set of strings of 0’s and l’s with an equal number of each.
{e, 01, 10, 0011, 0101, 1010, 1100, ...}
3. The set of strings of 0’s and l’s, ending in 11.
{11, 011,111,0011,0111,1011,...)
4. E* is a language for any alphabet E.
5. 4>, the empty language, is a language over an alphabet.
6. {£}, the language consisting of only the empty string, is also a language over any
alphabet.
1 .4.3 Application of Finite Automata
MU May ?O1O

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

1 -5 Finite State Machine (FSM)


M U ■ Doc. 2010

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

• Such machines do not have final statc/states.


• Machine generates an output on every input. The value of the output is a function of
current state and the current input.
Such machines are characterised by two behaviours :
1. State transition function (8)
2. Output function (X)
1
State transition function (8) is also known as STF.
Output function (X) is also known as machine function (MTF).
8 z X x Q —> Q
X : X x Q —> O [for Mealy machine]
X : Q —> O [for Moore machine]
There are two types of automata with outputs :
1. Mealy machine : Output is associated with transition.
X : X x Q -> O
• * * '• ‘4 . ’’ / ’ * J1 , •

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

1 .6 University Questions and Answers

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.3 What is Finite Automata? (Section 1.4) (2 Marks)

Q. 4 Explain Chomsky Hierarchy with languages used, forms of productions in grammars


and accepting device. (Section 1.3) (5 Marks)

Q. 5 Explain Chomsky Hierarchy for formal languages. (Section 1.3) (5 Marks)

Q. 6 Define the following term : Unrestricted grammar (Section 1.3.4) (2 Marks)

Q.7 Write short note on : Chomsky Hierarchy of Grammar (Section 1.3) (5 Marks)

Q.8 Give 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

Q. 10 Write detailed note on : Chomsky Hierarchy. (Section1.3) (10 Marks)

I May 2017 |

Q.11 Explain Chomsky Hierarchy. (Section 1.3) (5 Marks)

Dec. 2017

Q . 1 2 Explain Chomsky Hierarchy. (Section 1.3) (5 Marks)

Chapter Ends...
□□□

Scanned by CamScanner
Finite Automata

Module 1

2.1 Introduction to Finite Automata


M U - Doc. 2012

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

Working of a Finite Automata

Let us consider a T-flip flop.


A T-flip-flop has :
Serial
1. One input denoted by T. Input

2. Two outputs denoted by Q and Q.


3. It has two distinct states, defined by
the logical values of Q. The flip-flop Clock
is in q 0 state when Q = 0 and it is in qj
state when Q = 1. Fig. 2.1.1 : A T-flip flop as a finite automata
‘ 4. A value of 1 applied to its input changes its state from q0 to q T or from qj to q0.
A T-flip-flop can be used to check whether an input binary number contains an even
number of l’s. Let us assume that a binary number 101110011 is applied to input T of the
flip-flop. Initial state of the flip-flop is q0 (i.e. Q = 0) . :
10 1110 0 11

Qi % Qo % % %

Fig. 2.1.2 : State transition of T-flip-flop under the input 101110011


* ■» ■ ■ -

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.

Fig. 2.1.3 : Model of a finite state machine

Scanned by CamScanner
2f fhe°ry of Comp. Sci, (MU-Sem, 5-Comp.) 2-3 Finite Automata

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<r q P q 2 » q 3 » % q 5)-
• 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. ' , , .

i- ■ ■■

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

3. Reading head moves to the right.


• Eventually, the reading head reaches the end of the input string.
• Now, the automaton has to say ‘yes’ or ‘no’. If the machine ends up in one of a set of
finial states (q2, q3) then the answer is ‘yes’ otherwise the answer is ‘no*.

2.1.2 Some Important Terms


2.1 .2.1 Alphabet r ,

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)

It is a finite sequence of symbols from the alphabet.


For example :
1101 10 is a string from binary alphabet.
5920 is a string from decimal alphabet. J
h i- r;
‘STAMP* is a string from Roman alphabet.

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

• The set of all strings over an alphabet Z is denoted by Z •

Z* = Z° U Z l U Z2

£ String of String of length 2.


length 1
For example,
if Z = {0, 1}
then Z* = { £ , 0 , 1 , 0 0 , 0 1 , 1 0 , 1 1 , 0 0 0 , 0 0 1 , . . . . }
2.1 .2.3 Languages

A subset of strings over an alphabet Z is a language. In other words, any subset of 2* is


a language.
If Z is an alphabet, and L £ Z*, then L is a language over Z.
For example,
L! = [co G {0, 1 }* I (0 has an equal number of 0’s and T s ]
i.e., Li is a language over alphabets {0, 1 }, having equal number of 0*s and l’s.
/. Lj = {£, 01, 10, 0011, 0101, 1100, 1010, ...}
Thus, a language can be specified by :
L = {co 6 Z* I co has the given property}
Some examples of language are :
1. A language of all strings, where the string represents a binary number.
{0, 1 , 0 0 , 0 1 , 10, 11, ...}
2. The set of strings of 0’s and l ’ s with an equal number of each.
{e,01, 10,0011,0101, 1010, 1100, . . . )
3. The set of strings of 0’ s and 1’ s, ending in 11 .
{11,011,111,0011,0111,1011,...}
4. Z* is a language for any alphabet Z.
5. 0, the empty language, is a language over an alphabet.
6. {e), the language consisting of only the empty string, is also a language over any
alphabet.

2.1 .3 Applications of Finite Automata


MU - May 2010. May 20 <1

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

(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 wg L, where L is a regular language.

2,2 Deterministic Finite Automata (DFA) __________


The word “deterministic” refers to the fact that the transition is deterministic. There is
only one state to which an automaton can transit from the current state on each input. The
word “finite” implies that number of states are finite. A finite automata consists of five parts :
1. A finite set of states, represented as Q.
2. A finite set of alphabet, represented as Z-
3. An initial state, represented as q 0 .
4. A set of accepting states. An accepting state or final state is for ‘yes’ answer. It is a
subset of Q and is represented as F.
F£ Q [F is subset of Q]
5. A next state function or a transition function. Next state depends on the current state and
the current input. It is a function from Q x £ to Q. It is represented as 5.

2.2.1 Definition of a DFA


M U -Dec. 2 0 0 6 , May 2010

A deterministic finite automata is a quintuple.


M = (Q, Z, 8, q 0 , F), where
Q is a set of states.
Z is a set of alphabet.
q0 e Q is the initial state,
F c Q is the set of final states, and 8, the transition function, is a function from Q x £
to Q.

2.2.2 Representation of a DFA

Let the machine M be a deterministic finite automata.


M = {Q, Z, 8, q0 , F ) , where
=
Q {Qo* q J
Z = (0, i )
q0 is the starting state
F = {qj.

Scanned by CamScanner
Th
-X eory of Comp, sei, (MU-Sem, 5-Comp.) 2-7 Finite Auton

and 8 is the transition function as given below :


8 ( q o ,O)=>qo .
6 (q0> l ) => q t
7 ;
8 ( q p oj=> q i • - ; /
5
(q P l ) = > q 0 .
The above representation of transition function is not very readable. Conventionally
there are two representations for transition function :
1. State transition table. ,
2. State transition diagram.

1. State transition table


The Fig. 2.2. 1 shows the state transition table of the transition function discussed in this
section.
0 W — Input alphabets.
~»q0 qo qi -> before q 0 indicates it is the starting state
qf <11 q» * indicates a final state.
• * * I I JI . tV • .■' > ■

— states .

Fig. 2.2.1 : State transition table

In state transition table, there is a row for every state q t e Q.


In state transition table, there is a column for every alphabet A g £.
Starting state is marked as
4
• Final state is marked as
2. State transition diagram ' ’

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

Language of the above DFA


• It is easy to see that machine remain in same state on input 0.
• The machine transitions from q 0 to q, and q, to q0 , when the input is 1.
• Machine will be in state qj (final state), after reading odd number of l’s.
Thus we can conclude that the DFA accepts a string if and only if the number of l’s is
odd.
The language L accepted by M, the given DFA, is the set of all strings having odd
number of l’s.
Or, . . i

L ( M ) = {cog { 0 , 1 } * I to contains odd number of l’s)


Simulating the functioning of the above DFA for a given input
If M is given an input 00101 10, its initial configuration is (q0 , 00101 10).

(q 0 , 00101 10) => (q 0 , 0101 10) [input 0 in state q0 ]


8 . ■
=> (q0, 10110) [input 0 in state q0 ]
8 ■ • • - ■ *' 1
=> (q p 01 10) [input 1 in state q0 ]
1
? ’ •■ ■
'5
=> (q p 110) [input 0 in state qj].

=> (q0, 10) [input 1 in state q ( ]


6
=> (q p 0) [input 1 in state q 0]
8 ' '
=> (q n e) [input 0 in state q j
i ; *■ » > • t

Therefore,
8*
(q 00101 10) => (q p e) and so the input 0010110 is accepted as q, is a final state.

2.2.3 Designing a DFA

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

Transition function gives the next-state, depending on :


1. Cunent state 2. Current input
A transition function is problem specific and it depends on the problem.

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

Initial First a First a ab o f abb Entire a b b


Nothing o f
state of a b b of abb I s seen is seen
relevance
Is s e e n . i s seen I s seen
It Is looking for
first a .
Fig. 2.2.4: States of DFA

Scanned by CamScanner
jj Theoty of Comp. Scl. (MU-Sem. 5-Comp.) ■ 2-1 1

Transition behaviour is described using Fig. 2.2.5..


8 (q 0 ,b) => q 0
5
(q 0’a ) =>
8 (q P a) => q ,
b a a,b
8 (q p b) => q 2 o
8 (q 2,a) => qj
8 (q 2,b) => q 3
8 (q 3,a) => q 3 * nothing First ab abb
of a is seen is seen
8 (q 3 ,b) => q 3 relevance

(a) Tabular form (b) Transition table (c) Transition diagram


. Fig. 2.2.5 : Transition behaviour

Machine transits from q 0 to q j on input a, as the first symbol a of abb is seen.


Machine remains in q, state on input a, as the previous two symbols aa will still make
:
the first symbol a of abb. • ■j . - :i ;
Machine transits from q, to q 2 on input b, as ab of abb is seen.
Machine transits from q 2 to q 3 on input b, as entire abb is seen.
Machine transits from q 2 to qj on input a, as the previous three symbols aba will still
make the first symbol a of aba. ' ■ : .. -
Starting state and final states :
In Fig. 2.2.3(c), q 0 is both the initial state and final state as :
1. Zero number of 1’ s is even number of 1’ s.
2. Machine will have “yes” answer for even number of 1’s. q 0 is final state. Machine in
Fig. 2.2.3(c) is redrawn in Fig. 2.2.6 with initial and final states marked.
0 1
0 0
*
0

Fig. 2.2.6 : Final DFA for Fig. 2.2.3

In Fig. 2.2.5(c), q 0 is initial state and q 3 is the final state.


1. Initially, we have nothing that is relevant to substring abb. Therefore, q 0 is initial state.
2. Machine goes to state q 3 after seeing the substring abb. Therefore, q is final state.
Machine in Fig. 2.2.5 is redrawn in Fig. 2.2.7 with initial and final states marked.

Scanned by CamScanner
CamScanner
2-12 Finite Automata

a b

Ao

a b b

2 3
a

Fig. 2.2.7 : Final DFA for Fig. 2.23(c)

2.2.4 Examples on DFA


2.2.4.1 Examples on Counting of Symbols
Example 2.2.1 : Give deterministic finite automata accepting the following language over
the alphabet {0, 1}.
(a) Number of 1’s is multiple of 3.
(b) Number of 1 ’s not multiple of 3.
Solution : -
(a) Number of l’s is multiple of 3. * - -
Number of l’s seen, so far by the machine can be written as :
(i) 3n
(ii) 3n + 1
(iii) 3n + 2 .. , .
Corresponding to three cases mentioned above, there will be three states.
State Qq - no. of l’s, so far is 3n
State - no. of l’s, so far is 3n + 1
State q 2 - no. of l’s, so far is 3n + 2
An input of 1 will cause a transition from :
q to q p if the machine is in q 0.
qj to q 2, if the machine is in q r
q 2 to q if the machine is in q 2.
An input of 0 will not cause any transition.
0 1
->Ao Ao Ai
Ao q2
Ai ‘ q * a2
1 Qi a2 Ao
(a) Transition diagram (b) Transition table
Fig. Ex. 2.2.1 : Final DFA for Example 2.2.1(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

(c) Transition diagram (d) Transition table


Fig. Ex. 2.2.1 : Final DFA for Example 2.2.1(b)

(ip MU - May 2010


Solution :
(a) Number of l ’ s is even and number of 0’s is even.
At any instance of time, we will have following cases for number of 0’s and number of
l ’ s seen by the machine.
Situations
State
Number of 0’s Number of l ’ s
Even Even % .
Even Odd <11
Odd Even <12
Odd • Odd <13

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-14 Finite Automata

An input 0 in state q 0 , will make number of 0’s odd.


8 (q 0 , 0) => q 2
An input 1 in state q 0 , will make number of l ’ s odd.
8 (q 0 > 1) => q ,
An input 0 in state q p will make number of 0*s odd.
8 (q P 0) => q 3
An input 1 in state q p will make number of l’s even.

(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

(a) Transition diagram (b) Transition table


Fig. Ex. 2.2.2 : Final DFA for Example 2.2.2(a)

(b) Number of 1’s is odd and number of 0’s is odd.


In solution of Example 2.2.2(a), the state q , stands for odd number of 0’s should be
declared as final state.

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

(c) Transition diagram (d) Transition table .


Fig. Ex. 2.2.2 : Final DFA for Example 2.2.2(b)

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

• The number of 1’s seen could be odd, state q P 1


The required DFA is given in Fig. Ex. 2.2.3. Fig. Ex. 2.2.3
Example 2.2.4 : Design a DFA for a set of strings over alphabet {0, 1} such that the number
of 0’s is divisible by five, and number of 1’s divisible by 3.
Solution : At any instance of time, we will have following cases for number of 0’s.
Case 1 - 5n
Case 2 - 5n + 1
Case 3 — 5n + 2
Case 4 - 5n + 3
Case 5 - 5n + 4
Number of 0’s should be divisible by 5.
Similarly, there will be three cases for number of 1’s.
Case 1 - 3m
Case 2 - 3m + 1
Case 3 - 3m + 2
Number of 1’s is divisible by 3.
Thus, depending on number of 0’s and 1’s there will be 5 x 3 = 15 cases.
Let us represent a state of the DFA under consideration as q where i can take a value
from 0 to 4 depending on number of 0’s seen so far :
q O j - number of 0’s is 5n
q j . - number of 0’s is 5n + 1
q 2j - number of 0’s is 5n + 2
q 3. ~ number of 0’s is 5n + 3
q 4j - number of 0’s is 5n + 4

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

(a) State transition diagram

J| \ ; 0 1

“ 00 9io

9oi 9n 9o2

%2 912 9oo

9io 920 Qn

Qn 921 912

912 922 9io

910 930

921 131 922

922 932 920

9ao Q40 431

• An 932

932 942 930

9 o 9oo 041

<141 9oi 942

942 902 940

(b) State transition table


Fig. Ex. 2.2.4 : Final DFA for Example 2.2.4

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

These are the valid strings :


String of length 0 - accept through q 0 .
String of length 1 - accept through q r
String of length 2 - accept through q 2.
String of length 3 - accept through q 3.
String of length 4 - accept through q 4 .
String of length 5 - accept through q 5.
String of length > 5 should be rejected through a dead state or a failure state. A failure
state is designated as q .
0,1

',0,1

(a) State transition diagram

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

It is not necessary to mention the failure/dead state explicitly. If a transition is not


mentioned, it is taken as a failure transition. Thus, the solution can be given with out explicit
failure state.
0,1 0,1

(c) State transition diagram

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

(ii) All strings with exactly two 1 ’s


0
0 0 0
t 1 ► 1

(e) State transition diagram (0 State transition table


Fig. Ex. 2.2.5 : Final DFA for Example 2.2.5(H)

First 1 takes the machine from q0 to q r


Second 1 takes the machine from q, to q 2.
q 2 is a final state.
A 1 in q 2 takes the machine to a failure state q .

Scanned by CamScanner
Finite Automata

(Hl) All string containing at least two 0’s


0 1
Qi 90
0,1
1
0 k 0 qi <12
*
q2 <12 92

(g) State transition diagram (h) State transition table


Fig. Ex. 2.2.5 : Final DFA for Example 2.25(ili)

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.

(IV) All strings containing at most two 0’s


These are the valid strings :
A string not containing 0 - accept through q 0 .
A string containing one 0 - accept through q r
A string containing two 0’s - accept through q 2.
Another occurrence of 0 in state q 2 will cause a transition to dead state.
0 1
9t

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 ’

A 0 as the first input takes the machine to a dead state


Fig. Ex. 2.2.5(k)

Scanned by CamScanner
V Theory of Comp. Scl. (MU-Sem. 5-Comp.) 2-20 Finite Automata

Step 2 : A machine M 2, representing a DFA accepting a strings starting having length


divisible by 3 is given in Fig. Ex. 2.2.5(/) : .

m2 =

Fig. Ex. 2.2.5 (0


To design the required machine, we can combine M! and M 2 .
• Merge q t of M j with q j of M 2 .
• Initial input is handled by M h subsequent inputs are handled by M 2.

io

— Pb
0,1
0,1

(m) State transition diagram (n) State transition table


Fig. Ex. 2.2.5 : Final DFA for Example 2.2.5(v)

2.2.4.2 Examples on Substring

Solution :

(a) All strings starting with abb

• First input as ‘b’ will take the machine to a failure state.


• First two inputs as ‘aa’ will take the machine to a failure state.
• First three inputs as ‘aba’ will take the machine to a failure state.
• First three inputs as ‘abb’ will take the machine to a final state.

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) State transition diagram (b) State transition table


Fig. Ex. 2.2.6 : Final DFA for Example 2.2.6(a)

A DFA without explicit failure state is given in Fig. Ex. 2.2.6(a).

(c) State transition diagram (d) State transition table


Fig. Ex. 2.2.6 : Final DFA for Example 2.2.6(a), without a failure / dead state

(b) All strings with bb as a substring


The machine will have fours states :
Slate q 0 - It is the starting state and indicates that nothing of relevance to complete
‘abb’ has been seen.
State qj - preceding character is ‘a’ and ‘bb’ is required to complete ‘abb*.
State q 2, - Preceding characters are ‘ab’ and ‘b* is required to complete ‘abb?
State q 3 , - Preceding characters are ‘abb* and the substring ‘abb’ has been seen by
the machine.
a b
Qi %

— a.b q,
CT

CT*

a
el
C

CT1
O*
CT

rp

q
a *

(e) State transition diagram (0 State transition table


Fig. Ex. 2.2.6 : Final DFA for Example 2.2.6(b)

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

(g) State transition diagram (h) State transition table


Fig. Ex. 2.2.6 : Final DFA for Example 2.2.6(c)

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

E x a m p l e 2.2.7 : Design DFA for a language of string 0 and 1 that i

(I) Ending with 1 0 (II) Ending with 1 1


(III) Ending with 1
Solution :
I) Ending with 10
0 1

%
So
9i
*
92 9o

(a) State transition diagram


Fig. Ex. 2.2.7 : DFA for string ending with 10

Meaning of various states :


Oq : Starting state, nothing of sequence 10 is seen.
q 1 : ‘ T of sequence 10 is seen.
q 2 : Final state for strings ending with 10.
II) Ending with 11
0 1
Cq 0j 5b 9i '
9i 9o
*
92 9o
(b) State transition diagram
Fig. Ex. 2.2.7 : DFA for string ending with 11
Meaning of various states :
q : Starting state, nothing of sequence 1 1 is seen.
q: : First T ’ of sequence T T is.seen
q 2 : Final state of strings ending with ‘ I T
III) Ending with 1
0 1
9o Qi
*
0 •Qi 9o Qi
(c) State transition diagram
Fig. Ex. 2.2.7 : DFA for string ending with 1
The DFA will be in state q p whenever the preceding symbol is 1.

Scanned by CamScanner
Finite Automata

MU - Dec. 2007
Solution

Fig. Ex. 2.2.8 : Final DFA for Example 2.2.8

A sequence of 00 takes the machine from q 0 to q 2.


q 2 is a final state.
A ‘0’ input in q 2 will cause a substring 000.
An input of 1 in state q 2, causes a transition from q 2 to q 3 . If starting from q 3 , a substring
‘000’ is detected, machine goes to the failure state q .

MU - May 2008, Dec. 2008


Solution :
Machine will end in the final state q 2 if the leftmost symbol is 0 and the rightmost
symbol is 1.
Machine will end in the final state q4 if the leftmost symbol is 1 and the rightmost
symbol is 0.
A transition from q 0 to q ( is for ‘0’ as the first symbol.
A transition from q 0 to q 3 for ‘1’ as the first symbol.
o
o
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.

• All the three states are ‘accepting states’.


____________________________________________________ 'Jai*
Example 2.2.11 : Design a DFA for set of all strings over {a, b} ending in either ab or ba.
0
Solution : a
Meaning of different states :
qo -» starting state
a
q t —» a of sequence ab
q 2 —» ab of sequence ab a

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

character ‘a’ is relevant to ‘ab’ and hence the machine requires in q ,


. Input ‘b’ in q, makes the preceding two characters as ‘ab’. Machine enters the state 4
which stands for previous two characters as ab.

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 :

Fig. Ex. 2.2.13

Meaning of different states :


q 0 -> starting state

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.

Fig. Ex. 2.2.14 : DFA for Example 2.2.14


Example 2.2.15 : Design a DFA for set of all strings over {a, b) such that each a in co is
immediately preceded and immediately followed by a 'b’.
Solution :

(a) State transition diagram (b) State transition table


Fig. Ex. 2.2.15 : DFA for Example 2.2.15

Example 2.2.16 : Design


bols contains at least two 0's.
MU - M a y 2009
Solution : To solve this problem, we must maintain a count of ;
1. Number of l * s before 0.
2. Number of 1 *s after 0.

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

q 9 —> number of l ’ s before 0 is three.


q 5 -> number of l ’ s before 0 is one and after zero is 1. ->
q —> number of l ’ s before 0 is one and after zero is 2.
q g —> number of l ’ s before 0 is two and after zero is 1.
An input 0 in q q 5 , q 6 , q7 , q8 , q 9 will make number of l ’ s after 0 as number of l ’ s
before 0. y \ 1 1

3 0 2 1 — Number of 1's before

V vv
0 /2 /1 /2 ◄----- Number of 1's after

After and After and After and


before are before are before are
same same same
Fig. Ex. 2.2.16(a)

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

Path qg - qj - q2 is for strings beginning with 01 .


Final state q5 is for strings ending in 01.
State q3 - Nothing relevant to ‘01’ is seen.
State q4 - First character 0 of 01 is the preceding character.
State q5 - preceding two characters are 01 .

MU - Dec. 2006
Solution : 0

0 0
0

0
0

Following 4 combinations for the preceding three characters are accepting


100,101,110,111.
Path q0 -> q] q2 04 is for 100.
Path q 0 -> qj q2 -> q5 is for 101.
Path qo -» qj -» q3 -> q6 is for 1 10.
Pathq0 —> qj q3 —> q7 is for 111.
State number is same as the value of preceding three inputs.

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

Meaning of various states :


q0 —> Starting state.
q 1 -» previous character is a of ‘aaa’,
q 2 —> previous two character are ‘aa’ of ‘aaa’.
q4 -» previous character is b of ‘bbb’.
q3 -> previous two character are ‘bb’ of ‘bbb’.
q 3 —> substring ‘aaa’or ‘bbb’ is seen.

Scanned by CamScanner
▼ Theory of Comp. Sci, (Mll-Sem. 5-Comp.) 2-31 „ Finite Au tomato

• An input ‘b’ in q t or q 2 moves the machine to q 4 as b is the first character ot the


sequence bbb.
• An input ‘a* in q 0 , q 4 or q 5 moves the machine to qj as ‘a’ is the first character of the
sequence aaa.
Example 2.2.20 : Construct a DFA for accepting a set of strings over alphabet {0,1} not
ending in 010.
Solution :
Above DFA can be constructed in two steps :
1. DFA for strings ending in 010.
2. By taking complement of DFA derived in step 1; make every final state as non-final
state and non-finals state as final state.
Step 1 : DFA for accepting strings ending in 010.

Fig. Ex. 2.2.20(a) : DFA for strings ending in 010

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

(b) State transition table


Fig. Ex. 2.2.21 : DFA for Example 2.2.21

Meaning of various states :


q : Starting state.
qj : First character C of ‘CAT’ is the preceding character.
q 2 : First two characters CA of ‘CAT’ are the preceding two characters.
q3 : entire ‘CAT’ has been seen.

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

’ Fig. Ex. 2.2.22

Meaning of various states


qo : Starting state
q! : First character U of ‘UNITY’ is the preceding character.
q 2 : First two characters UN of ‘UNITY’ are the preceding two characters. •
: First three characters UNI of ‘UNITY* are the preceding three characters.

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

M Dec 005 a 4 a 0l7j


Solution :
A binary number is divisible by 3, if the remainder when divided by 3 will work out to
be zero. We must device a mechanism for finding the final remainder.
• . We can calculate the running remainder based on previous remainder and the next input
• The running remainder could be :
0 —> associated state, q0
1 —» associated state, qj
2 —» associated state, q 2
• Starting with the most significant bit, input is taken one bit at a time. Running remainder
is calculated after every input
The process of finding the running remainder is being explained with the help of an
example.
Number to be divided : 101 101
Binary number
to be divided by 3

Next input is 1 (1) 2 M O D 3 = (1).

Remainder 1 0
next input 0 (10) 2 MOD 3 = (10)

Remainder 10
(101) 2 MOD 3 = (10)
next input 1

Remainder 10 (101) 2 MOD 3 = (10)


next Input 1

Remainder 1 0 (100) 2 M O D 3 = (1)


next Input 1
1 1
Remainder 1
(11) 2 M O D 3 = (0) 2
next Input 1
x x
Fig. Ex. 2.2.23(a)

Scanned by CamScanner
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-34 Finite Automata

The calculation of next remainder is shown below,


Previous remainder Next input Calculation of remainder Next remainder
0 0
(%) o 00 % 3 =>
*32 0 1
(%) 1 01 % 3 =>
1 10
(Qi) 0 10 % 3 =>
1 (qj) 1 11% 3 => .' °'<q <P
1
10 (q 2 ) o 100 % 3 =>
. 10 (q 2 ) 1 101% 3 ■ => 10 (q 2 )

Binary Binary decimal Binary

o
* 9o Qi
0

9> q2
q2 91 92

(b) State transition diagram (c) State transition table


Fig. Ex. 2.2.23 : DFA for Example 2.2.23

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

Transition behaviour is explained below :


Remainder
Current remainder Next input Calculation of remainder
0 (q 0 )
0 00 : =>0MOD 4 =>
0 1 01 : => 1 M 0 D 4 => 1 (q,)
(Qo)
02 : =>2 MOD 4 => 2 ( q 2)
2
3
0 10 = > 3 M 0 D 4 => faj)
(q t ) i 1 ‘ T 1 => 4 MOD 4 => 0 (<Jo>
1
2 12 => 5 MOD 4 => (q>)

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)

0 100 = > 9 M O D 4 => Ifai)


(q 2 ) 10 1 101 =>10 MOD 4 => 2 ( q 2)
2 102 => 11 MOD 4 => 3 (q 3 )

Ternary Ternary Decimal Decimal

1 _ _ _ _2_

to qt q2
xO o
2
b))— ► q0 qi

qa q0
0
0 q2 q.>

(a) State transition diagram (b) State transition table


Fig. Ex. 2.2.24 : DFA for Example 2.2.24

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

The running remainder could be :


(0) 3 = 0 —> associated state, q0
(1)3 = 1 —> associated state, qj
(2) 3 = 2 —» associated state, q 2
(10)3 = 3 —» associated state, q 3
(11)3 = 4 —> associated state, q 4
t

Decimal

2
o

—3—

Fig. Ex. 2.2.25

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 00 => 0 MOD 3 O(qo)


0 7 01 => 1 MOD 3 i (q,)
2 02 => 2 MOD 3 2 ( q 2)

0 10 => 10 MOD 3 l(q.)


11 => 11 MOD 3 2 ( q 2)
2 12 => 12 MOD 3 o(qo)

0 20 => 20 MOD 3 2 (q 2)
2 1 21 => 21 MOD 3 O(qo)
2 22 => 22 MOD 3 W

0,3,6, 9 2,5,8 0,3, 6, 9 0,3, 6, 9


b (0, 3, 6, 9) ( 1 , 4 , 7) (2, 5, 8)

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

The required DFA is given in Fig. Ex. 2.2.28.


1,2,3,4,6,7, 8,9 0,5
o 0,5

1,2,3,4,6,7,8,9
Fig. Ex. 2.2.28

. 2006, . 2009 . 2010


Solution

(i)
X
y.z
y.z
o X
y
0
x,z
-o 0
0
x»y
x,y

Fig. Ex. 2.2.29(b)


Fig. Ex. 2.2.29(a)

(Hi)

Fig. Ex. 2.2.29(c)

2.2.5 Language of DFA

The language of a DFA M = {Q, 8, q0 , F) is denoted by L (M) and is defined by :


L (M) = {co I 5*(q 0 , co) is in F}

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.

• There is no path from q 2 to qj (final state) or from -----


q 3 to q r Thus q 2 to q 3 are dead states.
• The DFA can be redrawn after elimination of q 2 b
andq 3. Fig. Ex. 2.230(b)

• The language of the DFA can be defined as given below :


L = {co e {a, b}* I co starts and ends with a and a, b alternate}

a,b

a,b a,b

Fig. Ex. 2.2.30(c)

Scanned by CamScanner
Finite Automata

q 4 is dead state. The DFA can be redrawn after elimination of q 4.


_a
b
a

Fig. Ex. 2.2.30(d)

• A string ‘b’ of length 1 is accepted through the path q0 -» q 3 .


• A string having 1 or more a’s followed by a ‘b’ is accepted through the path.
• Qo Qi ••• 2

The language of the DFA can be described as given below :


L = {co e {a, b } * I co consists of 0 or more a’s followed by a ‘ b ’ } .

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 -

Fig. Ex. 2.2.31

Solution: (q 0»110101) =>(q p 10101)

z ' A(q 0 , 0101)

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

“Lt nnai state, simultaneously. _____________________


ion " “ ““ " “- *
Testing equivalence of two DFAs

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

and M 2 are equivalent if :


for every state < s b q. > in M 3
{ both Sj and q ; are final states
or
both Sj and qj are non-final states

M i and M 2 are not equivalent if :


for any state < Sj, q t > in M 3
{ Sj is a final states and q ( is a non-final state

Sj is a non-final state and q 4 is a final state

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 :

Fig. Ex. 2.2.32(d)


Step 3 : State < Qp q 7 > is selected for expansion. c

Fig. Ex. 2.2.32(e) M


Step 4 : State < q 2, q 5 > is selected for expansion. ,C

d
4dyd c

Fig. Ex. 2.232(f) M3

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

Thus, we can infer that Mj and M 2 are equivalent.

Example 2.2.33 : Find out whether M 1 and M2 are equivalent.


,y
tfE.

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}

Fig. Ex. 2.2.33(a)

2.2.7 Closure Property of Language Accepted by a DFA

The class of languages accepted by a deterministic finite automata is closed under :


(a) Union (b) Intersection
(c) Difference (d) Complementation.
The closure property can be established by constructing an appropriate finite state
automata in each case. . , \ ;
2.2.7.1 Union, Intersection, Difference

Let Mj = (S,.S, 6i, s0 , Fj) . • ,


i-j . ‘t

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*?

We will first construct a combined machine Mc » simulating the transition behaviour of


both. Mj and M 2. Then, we will derive M 3 , M 4 or M , from M c .
The combine machine M c can be described in terms of M | and M 2 .
Me- = (S x Q, 8 C, <s 0 , q 0 >, F c )
A state of is of the form < s h qj > where,
SjGSofMp

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

add a transition < 8j (s i5 a , 8 2 (qj, a s) > to M c .

}
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

Deriving M s form M c (L (M s ) = L (M,) - L (M2 ))


M 5 can be derived from M c by selecting a set of states from M c as a set of final states
for M 5 »
Set of final states F 5 from M 5 = {< Sj, q{ > G states of M c I s, g F! n qj g F 2 }
where, Fj is a set of final states for M, and F 2 is a set of final states for M 2.

Example 2.2.34 : Suppose, we have two machines M 1 and M 2 .


: For accepting a set of strings over alphabet {0, 1} ending in 01.
M 2 : For accepting a set of strings over alphabet {0, 1} containing even
number of 1’s.
Design a DFA for the following :
(i) L ( M 1) u L ( M 2) (ii) L ( M 2) a L ( M 2 ) (iii) L ( M 1) - L ( M 2)
. Solution :

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

8 (< s l f q 0 >, 0) => < 8 (s„ 0), 8 (q0 , 0) >


= > < S | , q0 > o
8 (< s „ q 0 >, 1) = > < 8 ( s lf 1), 8 (q0 , d ) >
=> < s 2, q , > Fig. Ex. 2.234(d)

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.

6 (< s h q , >, 0) => < 8 ( S1 0), 8 (q, 0) >

o
o

o
o

Fig. Ex. 2.2.34(e)


Step 4 : State < s 2 , q 0 > is selected for expansion.
o
o
'2>

0
0

Fig. Ex. 2.2.34(0

Finding a DFA for L (MO u L (M2)

The set of final states of the required DFA is given by :


Every < s i( q > such that
Sj e final -states of Mj or Qi e states of M ;
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 )

Finding DFA for L (MO n L (M2)

The set of final states of the required DFA is given by :


Every < s it q s > such that S| e final states of Mi and q t e final states of M 2

Scanned by CamScanner
V Theory of Comp: Sci. (M U-Sem. 5-Comp.) 2-48 ' ' / Finite Automata

o
0 0

0
0
0

Fig. Ex. 2.2.34(h) : DFA for L (MJ n L (MJ

Finding DFA for L (MJ - L (M 2 )

The set of final states of the required DFA is given by :


every < s it > such that s, e final states of Mj and qj £ final states of M 2 .
0
0 0

0
0
0

Fig. Ex. 2.2.34(i) : DFA for L (MJ - L (MJ

2.2.7.2 Complementation

Let Mj = (Q, E, S, q0 , F) be an arbitrary DFA. To prove the closure property


complementation, we must show that there is another machine M 2 such that,
M 2 (L) = (Mj(L))'
M 2 can be constructed from of Mj by making the following changes to M P
1. Make every final state M t of as a non-final state.
2. Make every non-final state of M, as a final state.
Machine M 2 can be defined as :
(Q. E. 8, Q - F)
2.2.8 Minimization of DFA

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.

2.2.8.1 Algorithm for Minimization DFA’s

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.

Fig. 2.2.8 : DFA considered for minimization

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

block 2 = set of final states.


= (q 2 )
O-equivalence partitioning of Q,
P
o ~ 3, q 4 » q5 , q6 , q 7) (q 2)
• The next step is to obtain the partition Pi whose blocks consists of the set of states which
are 1-equivalent, i.e. equivalent under any input sequence of length 1.
Checking (q 0 , q n q 3 , q 4 , q 5 , q 0 , q 7 ) for 1 -equivalence

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.

q ( , q 7 are distinguishable from, q0 , q 3 , q 4, q 5. q6 on *n Put 1 '


/, 1 -equivalence partitioning of Q,
p ) = (q 3, q 5 ) (q,,q7 ) fad’ <14> q«) faz*
f T ■■ T T•
block 11 block 12 block 13 block 2

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

Checking (q 1t q 7) for equivalence - block 12

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

Fig. 2.2.9 : Minimum state DFA equivalent to Fig. 2.2.8

2.2.35 : Construct the minimum state automate equivalent Io g».n

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

On input 0, block 1 is successor of q 0 , q p q 5 , q 6 , q 7 .


On input 0, block 2 is successor of q 2 , q 4 .
A q 2, q 4 are distinguishable from q 0 , q t q 5 , q 6 , q7

(Ao , Ai , A2 , A4 , A5 , Ar , A 7) (Aa\ Transition on 1

block 1 block 2

On input 1 , block 2 is successor of q 7 .


On input 1, block 1 is successor of q QT q f q 2T q 4r q 5 , q6 .
q 7 is distinguishable from q 0 , q p q 2 , q 4 , q 5 , q 6 .

Pl = (q 0 > Qp q 5’ <U ( <14) (<b> fas) -


Step 3 : Finding 2-equivalence partitioning of states by considering transition on ‘O' and
transition on ‘T.

Transition onO
( Ao A1" A5 A© ) ( A2 A4) ( A7 ) (A3)
Transition on 1

block 10 block 11 block 1 2 block 2

On input 1, block 11 is successor of q p q 5 .


On input 1, block 10 is successor of q0 , q 6 .

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

(Ao > Ae) ( qi . as ) ( a2 » <14) ( 9 7 ) ( « h )


Lt Lt *— Transition on 1

Scanned by CamScanner
Theory o£Cornp Sci. (MU-Sem. 5-Comp.) 2-54 Finite Automata

Blocks can not be divided further.


p
P3 = 2 = (q 0 - q 6 ) ( q , , q 5 ) (q 2 , q 4 ) (q 7 ) (q 3 ) which i s final set of blocks of equivalent
classes.
Step 5 : Construction o f m i n i m u m state DFA.
0 1
-*(q 0. q 6) (q P q 5) (Mo» Me)
<qo 6>
(q P q$) (Mo» M$) (q 2 . q 4 )

(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

Example 2.2.36 : Construct the minimum-state DFA equivalent to given DFA.


4 MU - May 2009
0 1
-*q 0 q. 4

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

Step 2: Finding 1-equivalence partitioning of states by considering transitions on ‘O’


onT.

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

On input 1, block 1 is successor of q 0, q3, q 6. ■


On input 1, block 2 is successor of q p q q7.
States q0, q3, q6 are distinguishable from q p q q 7.

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

' .. ' ' ■

Blocks can not be divided further.

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

(a) State transition table (b) State transition diagram


for minimum state DFA for minimum state DFA
Fig. Ex. 2.2.36 : Final DFA for Example 2.2.36

2.3 Non-deterministic Finite Automata

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

A Non-deterministic finite automata is a 5-tuple.


M = (Q. S, 5, q0 , F)
where,
q = A finite set of states
£ = A finite set of inputs .
5 = A transition function from Q x £ to the power set of Q i.e.to 2 Q .

Scanned by CamScanner
— Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-57 - Finite Autorr

q 0 = q 0 G Q is the start I initial state


F = F G Q is a set of final / accepting states.
The NFA, for Fig. 2.3.1, can be formally represented as,
Qj}*{0* 1 } ,8, q 0 ,{ q 2 J)
where, the transition function 8 is given below in Fig. 2.3.2.
0 1
(q0 ) {q 0 > q J
<11 (Q2’5
*
{q2 l . {q 2 )
Fig. 2.3.2 : A transition table for NFA in Fig. 2.3.1

2.3.2 Processing of a String by NFA

A string co e is accepted by NFA M if 8*(q 0 , co) contains a final state.


Let us see how the string 011010 is processed by the NFA given in Fig. 2.3.3.

Fig. 2.3.3 : A sample NFA


a path leading to final state

'-X •1
Ao Ao q q
Ao Ao
°F/ ° Ao

{q o l


{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>}

Fig. 2.3.4 ; States reached while processing of 011010

From the state q 0 , after input 0, resulting state is q 0 .

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-58 Finite Automata

» From the state q0 , after input 1, resulting states arc q0, q r


An input 0 in q0, generates two paths :
1 1
t Qo o 2. q0 -> q.
On input 01 1010, the automata can take any of the four paths :
1 0
1 -A L> a x * x 0 .
1- Qo * % ~ 9i -> -> -> <1*
[Once in (j) state (dead state) machine will remain in $ state]
0 1 1
• 0 1x0
■ 2. q0 q0 9i ~ > q 2 0
„ 0 110 10
9q ~ * q0 q0 q0 9 0 —* 9i q2
0 110 10
4. q0 q0 q0 9 0 -> q0 9 0 q0
Out of the four paths, the path number 3 takes the machine from start state to the final
state on input 01 1010. q2 is a final state.
A string is accepted by non-deterministic finite automata, if there exists a path that takes
the machine to a final state.
Thus, 01 1010 will be accepted by the NFA of Fig. 2.3.3.
• The language accepted by the above NFA consists of string ending in 10. A string of
length n (I co I = n), ending 10 can be accepted by the above NFA if :
1. The machine remains in q0 for first n-2 inputs.
2. On last two inputs 10, the machine makes the transition as shown below :
1 0
qo qi 92
• To reach the fmal state, and to remain there, it is necessary that the last two characters of
the string should be 10.
• Thus the NFA in Fig. 2.3.3 accepts a string, if and only if it ends in 10.
Example 2.3.1 : Draw a non-deterministic automata to accept strings containing the
substring 0101.
Solution :

~A0,1 . n 0,1 ->q 0 (Qo- Qi ) {q0 )


{q2 J
(a) State transition diagram {qj

{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

Fig. Ex. 23.2

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 :

9 o — — ►Al — *92 — -►93

• 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

(ii) L { x € {0, 1}* : x is starting with 1 and I x I is divisible by 3}

<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

aDFA sorined be caMedaa NFX

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

(a) State transition diagram (b) State transition table


Fig. Ex. 2.3.6
If the starting and ending digits are 0 then NFA will end in q 4 by making a move to q:
from q , on first 0 thereafter it will wait non-deterministically in q 2 and on last 0 it will
enter the final state q*.
If the starting and ending digits are 1 then NFA will end in q 5 by making a move to q3
from q ( on first 1 thereafter it will wait non-deterministically in q 3 and on last 1 it will
enter the final state q 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

Fig. Ex. 2.3.7 : State transition diagram

(b) ' 8 * ( l , a b ) = 5 ((8(1, a)), b)


= 8 ( { 1 , 2 ) , b) = 8 (1, b) u 8 (2, b ) ‘ .
= { 1 } u { 3 } = {1, 3 }

(c) 8*(1, abaab) = 8 * ( { 1 , 2 } , baab)


= 8 * ( { 1 , 3 ) , aab)
= 8 * ( { 1 , 2, 4 ) , ab) = 8 ( { 1 , 2, 3, 5 } , b)

= { 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

• If the starting symbol is 0, string is rejected by entering the failure state q .


A loop through qi q2 q, ensures that every 0 is preceded and succeeded by 1.
An input 0 in state q 2 will mean two consecutive 0’s. The string is rejected by entering
the failure state q .

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

(a) State transition diagram


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

Fig. Ex. 2.3.10 : NFA for Example 2.3.10

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

2.3.3 NFA to DFA Conversion

The NFA to DFA conversion is based on subset construction. If a non-deterministic


finite state machine consists of three states.
i-e- Q = (Qo- q P q 2 )
The machine could be in any of the following states :
1- 0
■ 2. (q,,) , (q , (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

Fig. 2.3.5 : NFA to be converted to DFA

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

Step 2 : A new subset {q 2 ) is generated. Successor of the subset {q 2 ) are generated.


0 - successor of {q 2 } are {q0 , qj ) .
1 - successor of {q2 } are {q0 }.
1

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

0 0 - ■> {AoI {q 2 } <!>

{q2 } (qo’AJ (q0 }

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

{q 0 >q2 } (q© * Ap q 2 ) {q0 }

Fig. 2.3.9

StepS: A new subset ( q q p q 2 ) is generated successors of the subset {q0 , q p q 2 } are


generated.
=
0 — successor of {q q P q 2 ) $ Qi* 2)’
= 8 ( { qc , q 1 } 0 ) u 5 ( q 2, 0 ) .
= {q2 J (qo. q j = {q0 - Qi- q2 )
1 -successor of (qo,q l t q 2 ) = 8 ({q q p q 2 }, 1)
= 8 ({q0 . q,}, 1) 5 (q 2 . 1)
u
= {q0 - q 2 ) (q0 ) =

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

Fig. 2.3.11 : Final DFA after marking of accepting states

MU - Dec. 20lj!

Scanned by CamScanner
W TheoT fCgmp. Sei, (MU-Sem. 5-Comp.) 2-68 ______ Finite Automata

Proof: ’ i 1

This theorem has two parts :


1, If L is accepted by a DFA M 2 , then L is accepted by some NFA M P
2. If L is accepted by an NFA M b then L is accepted by some DFA M 2 .
First part can be proved trivially. Determinism is a case of non-determinism. Thus a
DFA is also an NFA.
Second part of the theorem is proved below :
Construct M 2 from M , using subset generation algorithm as explained earlier. We can
1
prove the theorem using induction on the length of co.
• ; . .< • | ■ • i ■ ■ <I )
Base case : Let co = £ with I co I = 0, where I co I is length of co.
Starting state for both NFA and DFA are taken as q0 . When co = £, both DFA and NFA
will be in q 0 . Hence, the base case is proved.
Assumption : Let us assume that both NFA and DFA are equivalent for every string of
length n. We must show that the machines Mi (NFA) and M 2 (DFA) are equivalent for strings
of length (n + 1). ■’
Let con + i = cona, where con is a string of length n and con + j is a string of length (n + 1). ‘a’
is an arbitrary alphabet from Z.
w
8 2(q 2, con) = 8 2 (q 0 , n)> where 8 2 is transition function of DFA (M and 8 , is transition
;
function of NFA (M x). < ----- ~
If the subset reached by NFA is given by
{p p p 2 , . . . p k } i . 'j
k
' •.
then, / • 8 2 (q0 , con + 1 ) = U 8 2(p i, a) ; ,..(i)
i = 1
k

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

Thus, the result is true for I co I = n + 1, hence it is always true.

Scanned by CamScanner
V Theory of Comp. Scl. (MU-Sem, 5-Comp.) 2-69 -----------:— —--------

Example 2.3.11 : Convert to a DFA the following NFA :


0 1
-*p {p. q) {p}
q {r} {r}
r (s) <l>
s* {8} {8}

Solution :
Table Ex. 2.3.11 : State transition table of DFA
0 1 . . J. ■ n? • t • ■ ' r

—>{P) {p.q) , tp) A new subset {p, q ) is generated.


{p. q ) . { p . q . r } (P.r) Two new subsets {p, q, r} and {p, r} are generated
{p> q. r ) { p . q . r , s ) {p.r) A new subset {p, q, r, s} is generated
{p. r ) {p.q.s} Anew subset {p, q, s} is generated
{p,q, r,s}* { p . q . r , s ) , {p, r, s} A new subset {p, r, s } is generated
{p.r.s}* {p, q, s} (p.s) A new subset {p, s } is generated
{p.s)* {p.q.s} {p. s )

1 1

o
) -------*- ------ftp,q})------

1
o
o

v
0

o

1 /

Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-70 Finite Automata

Subsets are generated recursively.


Eveiy subset is selected for generation of successor subsets.
Every subset containing s (final state of the given NFA) should be marked as a final
state.

Example 2.3.12 : Convert to a DFA the following NFA.

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

->{pj (q. «) {q}

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

- {p} {q, s} {q}

{q.s) {r} (p> q . r )

{q) {r} {q.rl

Step 3 : Three new subsets {r }, {p, q, r) and { q, r} are generated.


Their successors are calculated.
5({r},0) = {s}
3({r}, 1) = (p)
8 ({p, q, r}, 0) = 8(p, 0 ) u 8 (q, 0) u 8 (r, 0)

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

■ {q<r) {r, s) (P.q.r)


{p. q. r) (q, r, s) (p.q.r)
(s) <l» (P)'
{r, s) (s) {p}
(q. r, s) . {r, s) {p.q.r)
Step 5 : Every subset containing q or s is declared as a final state. Final DFA is shown in
Fig. Ex. 2.3.12.
Table Ex. 2.3.12 : State transition table of DFA

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)

= (q) U {t) U { t ] u <|>


= (P. t)
5 ( { p , t ) , 0 ) = 8(p, 0) u 8 (t, 0)
= ( p . q l U<|>= { p , q |
3 ( { p , t ) , 1) = 8(p, 1) u 8 (t, 1)
= Ipl u<|>= (p)
s or t is marked as a
state.
Informal Description: Strings over (0, 1 ) with second digit from the end is 0.

Scanned by CamScanner
▼ Theory ofComp. Sci. (MU-Sem. 5-Comp.) 2-74 Finite Automata

Jp.q),

—>{P) (p. q ) (p)

Ip.q) {p> q. (p.t)


(p, q, r, s ) * Ip.q. (p.t) [p.q.r.sL (P.t)

{P.tj* Ip.q) Ip)


(a) State table (b) State diagram
Fig. Ex. 2.3.13 : Final DFA for Example 2.3.13

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 :

Fig. Ex. 2.3.14(a) : Non-deterministic finite automata for Example 2.3.14

Non-determinism should be utilized to full extent while designing an NFA. A string of


length n, ending in aba can be recognized by the NFA given in Fig. Ex. 2.3.14(a). First n-3
characters can be absorbed by the state q0 by making a guess. On guessing the last three
characters as aba, the machine can make a transition from q0 to q3 .
NFA to DFA conversion :
Step 1 : {q0 } is taken as first subset
a-successor of {q0 ) = 8( q 0 , a) = {q0 , q j
b-successor of (q 0 ) = 8( q 0 , b) = {q0 }
Step 2 : A new subset { q0 , q j} is generated. Successors of { q0 , q, } are calculated.
8 ( {q 0 » qi ) > a ) = S(q0 , a) u 8 (q p a)
= (q 0 il U ( i> = {q0’ q J
8 ( { q0 , q j » b ) = 8(q 0 , b) u 8 (q p b)
= =
tq0 } {q 2 ) K q2 i
Step 3 : A new subset {q0 , q 2 ) is generated. Successors of (q0 , q,) are calculated.
8 ({q 0 > q 2 h a ) = 8(q 0, a) u 8 (q2, a)
=
iqo» q J 3)
=
{qo» qp q3 l
s ( { q 0. q 2 h b ) = S(q0’ b ) u 8
(q b
) = (qo l U(
t* = 1%)

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 2-75 - Finite Autop

Step 4 : A new subset {q 0 , q t q 3 } is generated. Successors of { q 0 , q ) are calculated.


8 ({q0 » q al* a) = 8(q 0 , a) U 8 (q P a) U 8 (q 3 , a)
=
, '• = ( q 0 , q j O 0 tJ 0 31
8 ( { q 0 , qjq 3 ) , b ) = 8(q 0 , b) U 8 (q P b) U 8 (q 3 , b)
=
= ( q 0 ) U {q 2 ) ( 3<j’ 3 2 1
No, new subset is generated. Every subset containing q 3 is marked as a final state.
a b
b
■ {3ol { 3o» 3i J

{ q 0 ’ qJ { 3o’ 3i } q2|
{ 3o’ 3 2 J { q0’ q p q 3 ) (q0 )

{ 3o» 3 r q 3 1 {q0 . q j f 3o’ 92!

(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

A string of length n, having two consecutive zeroes followed by 1 can be recognized bj


the NFA. Symbols before 001 can be absorbed by the state q 0 by making a guess. O n guessing
001, the machine can make a transition from q 0 to q 3 .
NFA to DFA conversion :
0 1

3o) ( qo. q> ) {q 0 ) A new subset { q0 , q j is generated

{ q0 . q>) {qo.q1.q2) ( qo l A new subset { q0 , q p q } is generated

{qo.q1.q2) {qo.q1.q2) ( q0 , q 3 } A new subset { q 0 , q 3 } is generated


{q 0 . q 3 ) * {q 0 . q i . q 3 ) ( q 0’ q 3 ) A new subset { q0 , q p q 3 } is generated
(qo.q1.q3)* ( 3o» q p q 2 * q 3 } ( qo* q 3 ) A new subset { q 0 , q p q 2 , q 3 } i s generated

<30’ 3i» 32’ 33)* { qo.q1.q2. q 3 ) {qo- q 3 )


Transition behaviour ol ( q 0 , q 3 ) , {q 0 , q ( , q 3 } and ( q 0 , q p q 2 , q 3 ) is identical. Therein
these three states can be merged into a single state with resultant DFA as shown
Fig. Ex. 2.3.15(a) :

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

(a) A NFA (b) A DFA


Fig. Ex. 2.3.16

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

NFA is given in Fig. Ex. 2.3.17(a).

0
o

Fig. Ex. 2.3.17(a) : A NFA

Meaning of various states :


q 3 = A pair of 1 1 has been seen
04 = A pair of 00 has been seen
Qio» 12 A pair of 00 and a pair of 1 1 have been seen
NFA in Fig. Ex. 2.3.17(a) can be converted into a DFA by introducing an explicit dead
state.

o 0

0
0

0 0

1
r
/o 0
0

Fig. Ex. 2.3.17(b) : A DFA

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.

• State q 1 is for previous character as 1. o


• State q 2 is for previous two characters as 10. Fig. Ex. 2.3.18(a) : A NFA
• 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. The
equivalent DFA is given in Fig. Ex. 2.3.18(b).

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

Solution : Construction of subsets :


Stepl: 5 ({qo h 0) = (q 3 )

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

Example 2.3.21 : Let m = ({q„ q 3 , q 4) , (o, 1}, 5, { q j , { q 3 }) is NFA where 5 is given as :


5
far. 0) = {q 2 , q 3 )
5
(An 1) = {q t }
5
(q 2 . 0) = {q„ q 2 }
5
(Q2 . 1) = { q „ q 2 }
=
(qa> 0) (q 2 )
8 (q 3 > 1) = {q p q 2)

its equivalent DFA, show all intermediate steps also.


i . f . 1 ‘ * ’

Solution :
4
w* * ' • »
Transition diagram i s given in Fig. Ex. 2.3.21(a).

0 o

<>0
1 \*0?1 '1 1

Fig. Ex. 2.3.21(a) : Transition of the given NFA

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)

Equivalent DFA is given in Fig. Ex. 2.3.21(b).


o

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

Example 2.3.22 : M ({q 1t q 2, q3 }, {0, 1), 5, q n {q3}) is a non-deterministic finite automate


where 8 is given by.
8(qi, 0) = {q2, q 3} 8fa 1( 1) -fad
S(q2) o) = fan q 2) S(q2, 0) = (j)
=
8(q3 » 0) = {qg} s(q3 > 1) fap q 2 }
Construct an equivalent DFA.
Solution :

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.

Fig. Ex. 2.3.22 : Equivalent DFA

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

Step 2 5 ({Qj}, 0) = (j) o


8 ({q, }. 1) ■= {q 0 , q , } 0
5
( ( q0 > qi ) o) = ( q j
S d q o . q J , 1) = (q0 , q,}
An equivalent DFA is given in Fig. Ex. 2.3.23.
Fig. Ex. 2.3.23 : An equivalent DFA

Example 2.3.24 : Convert NFA shown in Fig. Ex. 2.3.24 to its corresponding DFA.

Fig. Ex. 2.3.24


Solution :
Step 1 : q 3 and q 6 have identical NFA and they can be merged into a single state q 3 .

We start with the subset { q 0 )


0- successor of {q0 } = {q0 > qj
1-successor of { q j = {q0’ Qj
Fig. Ex. 2.3.24(b)
0

0
Step 3 : Two new subsets { q 0 , q j and
{ qJ are generated. Their
successors are calculated.

Fig. Ex. 2.3.24(c)

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.

Fig. Ex. 2.3.24(d)


Step 5 : Two new subsets ( q0 , q p q3 , q 5 ).and { q0 , q 2, q3 , qj are generated. Their
successors are calculated.

{qo-'U

Fig. Ex. 2.3.24(e)

Step 6 : Following states can be combined into a single state.

Fig. Ex. 2.3.24(f) : Final DFA

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

Fig. Ex. 2.3.25

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)

8 ({2, 5},b) = 8 (2, b) o 8 (5, b)


= <|)U{1} = {1}
A subset containing 5 is marked as a final state. The equivalent DFA is given in

{W

{2,5}

Fig. Ex. 2.3.25(a) : DFA for Example 2.3.25

Example 2.3.26 : Convert the NFA shown in Fig. Ex. 2.3.26 to DFA.

b
—J a ) -----------/eT) — —»(M

Fig. Ex. 2.3.26

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

Fig. Ex. 2.3.26(a) : Final DFA for Example 2.3.26

Example 2.3.27 Convert the following NFA to its equivalent DFA.


a
a,b a,b CJ

Fig. Ex. 2.3.27

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

Fig. Ex. 2.3.27(b)

Step 2 : Writing transitions from the states {p, q} and {q}.

A| p p a.b

a,b
c {q}

Fig. Ex. 2.3.27(c)

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)

Example 2.3.28 : Convert the following NFA into equivalent DFA


M = ({ q 0 > Qi}>{ 0, 1}, 8, q 0 , {q,}) where
8 (q0 , 0) = {q0 , q,}, 8(q 0 , 1) = {q,}, 8 (q (1 0) = <•>
8 ( q „ 1) = (q0 . q,}
Solution : The state transition table for the above NFA is as given below :
0 1
->qo (q0 . q J {qJ
*
C
11 (q 0 . q J

Scanned by CamScanner
CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp,) 2-88 Finite Automata

Fig. Ex. 2.3.28

Step 2 : Transitions from q.

JwiT;
p.i

Fig. Ex. 2.3.28(a)

Step 3 : Transitions from {q0 , qj }

p.i

r® 0,1
r\

J Qi }

•r Fig. Ex. 2.3.28(b)

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}

2.3.4 NFA with E-Transitions

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

(c) Recognizing reverse words ‘while’ or ‘else’


Fig. 2.3.12 : Few sample e-NFAs

2.3.4. 1 Equivalence of e-NFA and NFA

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.

Fig. Ex. 2.3.29(a) : e-move between q 3 and is removed

Step 2 : Transition from q] are also duplicated on q 0 . q0 is made a final state.


0

2
Fig. Ex. 23.29(b) : Final NFA without e-move after removing e-move between q 0 and qj

2.3.4.2 The Formal Notation for an e-NFA

A non deterministic finite automaton M with e-transition is given by :


Where, Q = A finite set of states
Z = is an alphabet
q 0 = q 0 6 Q is the initial/start state.
F = F c Q is a set of final/accepting states
Q
8 = A transition function from Q x ( E u {e}) to the power set of Q i.e. to 2 .
The transition function 8 also includes transitions on e. The NFA given in Fig. 2.3.14(a)
can be formally represented as :
M = ({q h q 2, q 3 ), {1,2, 3 } , 8 , q t , {q3 })
where 8 is defined by the transition table/diagram in Fig. 2.3.14(b).
e 1 9 3
2
■"*11 (q 2 ) (q.) .0
E
q 2 faa) <i> {q 2 }
*
<13 <l> {q 3 )
(a) (b)
Fig. 2.3.14 : An NFA used for formal notation

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

• E-closure of a state q b includes q P


• Set of states reachable from q ( on E-move.
• Set of states reachable from existing states in E-closurc, using E-move, and so on.
e

0
G e
E E

E s

Fig. 2.3.15 : NFA considered for calculation of E-closures

E-closures of various states in Fig. 2.3.15 are given below :


e-closure of q 0 = {q0 , q „ q 2 , q 4> q7 )
There are E-moves from q 0 to q b
q0 to q?>
qi to q 2 ,
Qi to q4 .
E-closures of qj = (qj, q 2, q 4 }
There are E-moves from q, to q 4,
qi to q 2 .
E-closure of q 2 = {q 2 }
There is no E-move from q 2 .
e-closure of q 3 = (q 3> q 6 , q 7, q „ q 2 , q 4 |
There are E-moves from q 3 to q 6 ,
q 6 >° q 7.
qe'oq,.
q, to q 2,
q , t o q 4.
e-closure of q 4 = { q4 )

Scanned by CamScanner
Theory of Comp, Sei, (MU-Sem. 5-Comp.) 2-92 Finite Automata

There is no E-move from q 4.


e-closure of q 5 = { q 5 , q s , q „ q (> q 2, q< )
There are e-moves from q 5 to q 6 ,
q<, to q 7,
q6 t o q |t
qi to q 2,
q i t o q 4.
e-closure of q 6 = {q 7, q „ q 2 , q4 )
There are E-moves from q 6 to q7,
q6 t o q p
qi to q 2,
qi to q4.
E-closure of q 7 = { q7 )
There is no s-move from q7.
E-closure of various states are summarised below :
State E-closure
q0 — {q 0 > q p q 2’ q 4 » q?)
q! — (qn q 2 » qa)
q2 —
q3 — {qs* q6» q?» qp q 2 » qa)
q4 — {q 4 }
q5 — ( q5 > q 6, q?» qp q2 > q 4 }
q6 — (q?» qp q 2» q4 )
q7 — (q?)
Examole 2.3.30 : A transition table is given for another with NULL with seven states..
q 8(q, a) 8(q, b) 8(q, 0)
1 {5} 0 {4}
2 {1} 0 0
3 {0} {2} 0
4 0 {7} {3}
5 0 0 {1}
6 0 {5) W
7 {6} 0 0
(a) Draw a transition diagram. (b) Calculate 8*(1, ba)

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

Fig. Ex. 2.3.30(a) : State transition diagram

(b) Calculate 8*(1, ba)


e - closure of 1 is
{1.4,3}

8(3, b) = 2

a 8(2, a) = 1 and £ - closure


of 1 i s { 1 , 4, 3 }

Fig. Ex. 2.3.30(b)

8*(l,ba) = (1,4,3)
2. 3.4.4 e-NFAtoDFA

8-NFA to DFA conversion is also based on subset construction as in case of NFA io


DFA. E-closure is calculated for every state in the subset. That is the subset is extended by
e-closure of every state in the subset.
Let us compute the extended transition (8 e ) for state q 2 on input 0 for the e-NFA in

8 E(q 2,0) = E-closure (q 3 ) = (q 3 , q 6. q 7, q „ q,. q 4 )

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.

q4 {q-o q 3 ) There is an E-move from q 4 to q 3 .


Step 2 : E-closure of the initial state q 0 is taken as the first subset.
E-closure (q 0 ) = {q 0 , q b q 2 , q 3 }
a-successor of {q 0 , q p q 2 , q 3 } = E-closure (8(q 0 , a) u 8(q b a) u S(q 2 , a) u S(q 3 , a))
= E-closure (<j) u {q 0 , q 4 J u 0 u { q4 })
= E-closure ({ q0 , q 4 }) = E-closure { q0 } u E-closure (q 4 )

= {q 0 , Qu q 2 > q3) ( q 3 , qJ = {q 0 , q q q3> q 4 )


b-successor of {q 0 , q l t q 2, q 3 } = E-closure (8 (q 0 , b ) u 3 (q„ b) u 8(q 2 , b) w S(q 3 , b))
= E-closure ({ q 2 } u 0 {q 4 } u 0)
= E-closure ({q 2 , q 4 }) = E-closure (qj u E-closure (q 4 )

= {q2 ) {q 3 > qJ = {q 2 »q 3, q 4 J

Fig. Ex. 2.3.31(a)

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-successor of {q 0 , q h q 2, q 3 , q 4 ) - a-succcssor of (q 0 , q h q 2, q 3 ) U a-successor {q 4 }

= t O’ c l h A l ’ Q3» 4 ) = {Ao» Q i ’ A2» Q3’ 4}

b-successor of {q 0 , q b q 2, q 3, q 4 ) = b-succcssor of (q 0 , q h q 2, q 3 } U b-successor of {q4}

= { q 2 iA3> A4) 0= {92 3,94}


a

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

Fig. Ex. 2.3.31(c) : Final DFA

Scanned by CamScanner
CamScanner
V Theory of Comp, Sci. (MU-Sem. 5-Comp.) 2-96 Finite Automata

Example 2.3.32 : Consider the following e-NFA


e a b c
->P <l> {p} {q) w
q (p} {q} {r}
r*(q) ' {r} <!’ {p}
(a) Compute the e-closure of each state.
(b) Convert the automaton to a DFA.
Solution :

(a) e-closure of states.

State e-closure Comment


,p {p } There is no e-move from p
q { p, q ) There is an e-move from q to p.
r { p, q, r } There are e-moves from r to q, q to p.

(b) Conversion of NFA to DFA

Step 1 : e-closure of the initial state p is taken as the first subset


e-closure (p) = {p}
a-successor of {p} = e-closure (8(p, a))
= e-closure (p) = p
b-successor of { p j = e-closure (8(p, b))
= E-closure (q)
• = {p, q}
c-successor of {p) = e-closure (8(p, c))
= e-closure (r) = { p, q, r }

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

= e-closure (p) u e-closure (q)

= {p} U {p, q) = {p, q)


b-successor of (p, q) = e-closure (8(p, b) u 8(q, b))
= e-closure ({q) U {r})
= e-closure (q) u e-closure (r)

= {P» q) {p, q, r} = {p, q, r}


c-successor {p, q) = e-closure (8(p, c) u (q, c))
= e-closure ({r} u 0) = {p, q, r}
a-successor of {p, q, r} = a-successor of {p, q} u a-successor of {r}
= {p, q} o e-closure (r)

= {p, q} u {p, q, r} = (p, q, r )


b-successor of { p, q, r) = b-successor of (p, q} u b-successor of {r}

= (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

Example 2.3.33 : Consider the following e-NFA.


e a b c
->p {q. r) q (q) {r}
q 0 (p) {p. q) {r}
r* 0 0 0 0
(a) Compute the e-closure of each state.
(b) Convert the automation of a DFA.

Scanned by CamScanner
CamScanner
V Theory of Comp. Sci (MU-Sem, 5-Comp.) 2-98 Finite Automata

Solution :

(a) E-closure of states

State E-closure Comment

P {p, q, r) There is an E-move from p to {q, r}


q {q} There is no E-move from q.
r {r} There is no E-move from r.
(b) Conversion of NFA to DFA

Step 1 : E-closure of the initial state p is taken as the first subset.


E-closure (p) = {p, q, r)
Successor of { p, q, r} are given by :
a-successor of {p, q, r} = E-closure (8(p, q, r}, a))
= E-closure (8(p, a) u 8(q, a) u 8(r, a))
= E-closure (0 o { p } u 0)
= E-closure ({p})
= {p, q> r )
b-successor of {p, q, r) = E-closure [8({p, q, r,}, b)]
= E-closure (8(p, b) u 8(q, b) U 8(r, b))
= E-closure ({q} U {r} u <t>)
= E-closure ((q, r})
= E-closure (q) u E-closure (r)
= {q) {r}
= (q,r)
c-successors of {p, q, r} = E-closure (8({p, q, r ) , c))
= E-closure (8(p, c) o (q, c) u 8(r, c))
= E-closure ({ r } u { p, q } u 8)
= E-closure ({p, q, r})
= E-closure (p) u E-closure (q) u e-closure (r)
= {p, q>r) u l q ) {r} = {p, q, r}
Step 2 : A new subset (q, r) is generated and its successors are calculated.
a successor of (q, r) = E-closure (8 (q, r}, a))
= E-closure (8(q, a) u 8(r, a))

Scanned by CamScanner
CamScanner
Theory o f C o m p . Sci. ( M U - S e m . 5-Comp.) 2-99 Finite Automata

= E-closure ({p) u (f>)


= s-closure (p)
= (p. q, r)
b-successors of (q, r) = E-closure (8({q, r), b))
• = E-closurc (8(q, b) u 8(r, b))
= e-closure ({r} u <}))
= E-closure ({r)) = (r)
c-successor of {q, r ) =• e-closure (8((q, r}, c))
= e-closurc (8(q, c) u 8(r, c))
= e-closure ({ p, q } u <]))
= {p, q, r}.
Step 3 : A new subset {r} is generated successor of { r } are ()).
Every subset containing r is marked as a final state. Final DFA is given in
Fig. Ex. 2.3.33.
a,c
a b c
b
->{p, q . r } * (p, q . r } {q.r} {p. q.r}
a,c b {q, r}* {p, q . r ) {r} {p. q. r)
{r}* 0
0 o 0
(a) State diagram (b) State table
Fig. Ex. 2.3.33 : Final DFA for Example 2.3.33

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

a ~ *qo toll (q<>} 4>


b
9i (qz) {q,)
* 0 {q 2 }
2

Meaning of various states :

q 2 ~> 92 will generate zero or more c’s


(b) The set of strings that consist of either 01 repeated zero or more times or 010 repeated
zero or more times.
E 0 1

1 ~ »qo {qi> q 3 l 4> 0


*
Qi ■ - {q 2 l
q2 0 (q.l
* <J>
q3 0 {q4 )
q4 <i> ! qs )
4 q5 (qa)

(a) State diagram (b) State table


Fig. Ex. 2.3.34 : e-NFA for Example 2.3.34(b)
is
• Path q 0 —> 9i -> 92 9i - • 92 9i f° r generation of zero or more
representation of 01.
• Path q 0 —> 93 94 “ 95 •••• “ > 93 ’ s f° r generation of zero or more representation
ofOlO.
(c) The set of strings of 0’s and l’s such that at least one of the last ten positions is 1.
0,1

<10 2 A

<17)

Fig. Ex. 2.3.34(c) : NFA for Example 2.3.34(c)

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-101

State q t is for accepting strings with last character is 1.


Similarly, states q 2 to q 10 arc for accepting strings whose second position to iq*
position from the end contain 1.

Example 2.3.35 : Convert the following NFA-e to its equivalent DFA.


o

Fig. Ex. 2.3.35

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 }

Fig. Ex. 2.3.35(a)

Step 3 : Transitions from { q 2, q 3 ) and { q 3 ) are calculated.


0,1,2

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.

2.3.5 Difference between NFA and DFA


|;MUgMa 2007, Dec. 2009fMay 2QmMff 2CW,
BMay7015?Maya0l6, May 2017, Dec. 2017

Parameter NFA DFA


Transition Non-deterministic. Deterministic

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

Design Easy to design due to Relatively, more difficult to


non-determinism. design as transitions are
deterministic.

Acceptance It is difficult to find whether w e L as It is easy to find whether w e L as


there are several paths. Backtracking is transitions are deterministic.
required to explore several parallel
paths.

2.4 Finite Automata as Output Devices


MU -Dec; 2010

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 ia/0 a/0


b/o b/0

b/0

Fig. 2.4.1 : State diagram of a Mealy machine

State transition function (8) (or STF) :


a b
>c
- lo q0 qi
qi q0 q2
q2 qo q3
q3 . q3 q3
Fig. 2.4.2 : State transition function for Mealy machine of Fig. 2.4.1

Scanned by CamScanner
Theoryo Comp Sci. (MU-Sem, 5-Comp.) 2-104 Finite Automata

Output function (X) (or MAF) :


a b
“*<lo 0 0

<11 0 0

q2 I 0

q3 0 0
Fig. 2.4.3 : Output function for mealy machine of Fig. 2.4.1

State table for both 8 and X (both STF and M AF) :


A b
q</o q/O

<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

• An arc from state q, in a mealy machine is associated with :


1. Input alphabet e Z
2. An output alphabet G O.
• An arc marked as ‘a/0’ in Fig. 2.4.1 implies that :
1. a is in input
2. 0 is an output.
• State transition behavior and output behavior of a mealy machine can be shown
separately as in Fig. 2.4.2 and 2.4.3; or they can be combined together as in •
Fig. 2.4.4.
2.4.2 Formal Definition of a Mealy Machine
MU - Dec. 2005, Dec. 2017

A mealy machine M is defined as .


M = {Q,S, 0 , 8 , X, q0)
Where,
Q = A finite set of states.
X = A finite set of input alphabet

Scanned by CamScanner
pay
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-105 Finite Auto

O = A finite set of output alphabet


6 = A transition function Z x Q —» Q
X = An output function Z x Q O
q0 = q 0 e Q is an initial state.

2.4.3 A Sample Moore Machine


MU- Dec.2di

o
o
0

Fig. 2.4.5 : State diagram of a Moore machine

State transition function (8) : Output function (O)

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 table for both 8 and X :


In put ■ ■: : /

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

2.4.4 Formal Definition of a Moore Machine


MU- Dec. 2017

A Moore machine M is defined as :


M = (Q, L, O, 6, X, q0 )
• where, Q = A finite set of states
S = A finite set of input alphabet
O = A finite set of output alphabet
8 = A transition function L x Q —» Q
X = An output function Q —> O
q 0 = Qo G Q is an
initial state.

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

Meaning of various states is :


q 0 - Running remainder is 0
qj - Running remainder is 1
q 2 - Running remainder is 2.
• q 3 - Running remainder is 3 = (10) 3
q 4 - Running remainder is 4 = (1 1) 3

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

Solution : The machine will have two states :


q0 - corresponding to previous cany as 0
q 1 - corresponding to previous carry as 1.
• Machine will transit from q 0 to q, if the previous carry is 0 and the next carry is 1.
• Machine will transit from q, to q 0 if the previous carry is 1 and the next carry is 0.
• Next input will be one of the followings :
m m pA m
loj’ 11 J’ l o J ’ l i J
• Sum of carry will be calculated in a usual way.
Input Output (Sum)
0 0
1
1
o 0 1
1 0l
b
o 1

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

(a) State transition diagram (b) State transition table

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

(c) MAF (d) STF


Fig. Ex. 2.4.3

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/0 a/0 x— qi.o


-> ------( 2

b/1 Qi qi.o

a/0 q2 qo,O qi. i


(a) State diagram (b) State table
Fig. Ex. 2.4.4

Meaning of various states :


q0 - Start state
qj - Previous character is b
q 2 - Preceding two character are ba
A transition from q 2 to q, will make the preceding three characters as bab and hence tl*
output 1.

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

(a) State diagram (b) State table


Fig. Ex. 2.4.5 : Moore machine for the Example 2.4.5
Meaning of various states :
q 0 - Start state
qi - Previous character is b
q2 - Preceding two character are ab
q3 - Previous character is b
A transition from q 2 to q 3 will make the preceding three characters as bab hence the
output 1.
Example 2.4.6 : Design a Moore machine for the 1’s complement of binary number.
Solution :

i %
(b)

Next input as 0, sends the machine to state q 1 with output of 1. Complement of 0 is 1.


Next input as 1, sends the machine to state q0 with output of 0. Complement of 1 is 0.

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

Meaning of various states :


q0 - initial state, associated with output 0
q, - preceding symbol is 0 of sequence 001, associated with output 0.
q 3 - preceding two symbols as 00 of sequence 001, associated with output 0.
q t - preceding three symbols as 001 of sequence 001, associated with output 1.

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

mple 2.4.11 : Design a Moore machine for the following problem :


Input alphabet - { 0 , 1 , 2}
Output alphabet - {a, b, c}
If the sum of 3 consecutive number is 0, 1 x 4; output a.
If the sum is 2, 3; output b.
If the sum is 5, 6; output c. MU - May 2007
Solution :
Sum of three consecutive symbols will depend on :
1. Preceding two symbols
2. Next input.
Preceding two symbols could be :
Symbols State
00 “ c
loo
01 Qoi
02 02
10 Qio
11 - Qu
12 - 9l2

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-113 Finite Autor

Symbols State
20 — q 2o
21 --- 921
22 — 922

The next state and the output is given below :


1. An input 0 in state 9 00 will make sum as 0 and next state as q(K)
2. An input 1 in state q00 will make sum as 1 and next state as q01
3. An input 2 in state q00 will make sum as 2 and next state as q02
4. An input 0 in state q01 will make sum as 1 and next state as q 10
5. An input 1 in state q01 will make sum as 2 and next state as q H
6. An input 2 in state 9 01 will make sum as 3 and next state as q 12

25. An input 0 in state q 22 will make sum as 4 and next state as q 20


26. An input 1 in state q 22 will make sum as 5 and next state as q21
27. An input 2 in state q 22 will make sum as 6 and next state as q
State transition table,
0 1 2
~ >
Qoo q0(/a q0 /a q02/b
q Oi qi</a q H /b q, 2/b
902 q2(/b 921/b q 22/a
Q10 qVa qoi/b ql>2/b
qn qi(/t> q H /b q, 2/a
qi2 qa/b q 2 i/a q 22/c
q20 qot/b q<i|/b qo2/a
q 2i qi</b q ( l /a q )2/c
q 22 Q2(A q,,/c q 22/c

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
»

qo- a qpe q 0, o q0 > u


a/a
o/o
l/u qi q0, a qi,e q0 > u q0 , o qo’ u
u/u
(a) State diagram (b) State table
Fig. Ex. 2.4.12

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

Fig. Ex. 2.4.13(b) : State diagram of the 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

Meaning of various states :


q 0 - Start state
qj - Preceding character is 1
q 2 - Preceding two characters are 11 and it needs a 0 to complete the sequence 110
q 3 - Preceding two characters are 10 and it needs a 1 to complete the sequence 101.

Scanned by CamScanner
V Theory of Comp. Sei. (Mu-Sem, 5-Comp.) 2-116 Finite Automata

(b) Design of Moore machine :


0,1

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

Fig. Ex. 2.4.15 : State diagram of the Mealy machine

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

Fig. Ex. 2.4.16

2.4.5 Conversion of a Mealy Machine into a Moore Machine :

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

(a) State table (b) State diagram


Fig. 2.4.9 : Mealy machine considered for conversion into an equivalent Moore machine

Step 1 : Splitting of states :


It may be necessary to split some of the states of a Mealy machine to get an equival
Moore machine. Splitting is based on incoming lines into a state and the associated outp111
We must split a state to handle different output values associated with incoming lines.

Scanned by CamScanner
y Theory of Comp, Sci, (MU-Sem. 5-Comp.) 2-118 Finite Automata

• There aie two incoming lines into the state p.


(i) From q to p on input 0, giving an output of 1.
(ii) From r to p on input 1, giving an output of 1.
In every case output is 1 and hence splitting is not required.
• There are two incoming lines into the state q.
(i) From p to q on input 1, giving an output of 0.
(ii) From r to q on input 0, giving an output of 1.
Since there are two different outputs, we must split q into two different states q0 with
output
0 and q t with output 1.
split Qc/O - output 0
1
Q ---- q t / l - output 1
• There are two incoming lines into the state r.
i) From p to r on input 0, giving an output of 0.
ii) From s to r on input 1, giving an output of 0.
In every case output is 0 and hence splitting is not required.
• There are two incoming lines into the states S.
(i) From q to s an input 1, giving an output of 0.
(ii) From s to s on input 0, giving an output of 1.
Since there are two different outputs, we must split s into two different states. s0 with
output 0 and Sj with output 1.
split syO - output 0
s i---- S ] / i output 1
Step 2 : Drawing of an equivalent Moore machine.

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

• Every transition into q with output 0 is directed to q 0 .


• Every transition into q with output 1 is directed to q P
• Every' transition into s with output 0 is directed to s 0 .
• Every transition into s with output 1 is directed to s P
• Every transition from q is duplicated both from q 0 and q P
Every transition from s is duplicated both from s 0 and s ( .

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

Transition is duplicated for both q 0 and q 1

8 (q, 1) = S, 0 8 (q 0 , 1) = s 0

8 ( q „ l ) = s0

Transition is duplicated for both q 0 and q t . s0 is


associated with output 0.
8 (r, 0) = q, 1 8 (r, 0) = q ,

qi is associated with output

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, l) = r,0 8 (s0 , 1) = r

8 ( s „ 1) = r

Transition is duplicated for both s0 and Sp______

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

Present Next state


Output
state o 1
■ _________________ ■ • ' „

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

Present Next state


Output
o
Iww
state SliJI
r/0 r e
“>Pe q0
0 r 1
Pl q0
q0 Pi s0 0
0
Qi Pi So 1
r qt Pl 0
0 0
s S
o 1 r 0
s
Si i r 1

(e) State diagram (f) State table


Fig. 2.4.9 : Final Moore machine

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

Fig. Ex. 2.4.17

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

Transition in Mealy machine Corresponding transitions in Moore machine


3 ( q 0 , 0) = q „ 0 5 (q 0 , 0) = p 0, p0 is associated with output 0.

6 (qo, l ) = q 2 > ° 5 (q 0 , 1) = s 0 , s 0 is associated with output 0.

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

State Output State Output

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

Fig. Ex. 2.4.18(a) : Moore machine

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

Final Moore machine is drawn in Fig. Ex. 2.4.18(b) and 2.4.18(c).

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

Fig. Ex. 2.4.19

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

Fig. Ex. 2.4.19(a) : Moore machine for Example 2.4.19

• Transitions for s0 are duplicated for pe, p0 and p P


• Transitions for Sj are duplicated for q0 and q ( .

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

Fig. Ex. 2.4.20


Solution :
Step 1 : Splitting of states.
(i) q0 is associated with three different outputs (on the basis of incoming lines) E, 0, 1.
must split q0 into three different states :
Ae with output Ej .... (q0 is a start state)
Ao with output 0,
Aj with output 1.

Scanned by CamScanner
jr Thegryo Comp Sci. (MU-Sem. 5-Comp.) 2-126 Finite Automata

(ii) Qi i s associated with two different outputs 0 and 1.


We must split q j into two different states :
B o with output 0,

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

Fig. Ex. 2.4.20(a) : Mealy machine for Example 2.4.20

2.4.6 Conversion of a Moore Machine into a Mealy Machine :


MU -Dec, 2012, May 2013

A Moore machine can be transformed to a corresponding Mealy machine in two steps.


These steps are :
1. Construction of a trivial Mealy machine -
By moving output associated with a state to transitions entering into that state.

Scanned by CamScanner
y Theory of Comp. Sci. (Mll-Sem. 5-Comp.) 2-127

2. Minimization of the trivial Mealy machine obtained in step 1.


These two steps are being explained with the help of a Moore machine shown in
Fig. 2.4.10(a). ____________________
Next state
Present state Output
0 1

->P s q 0

q q r 1
r r s 0
s s P 0

Fig. 2.4.10(a) : Moore machine considered for conversion

Step 1 : Construction of trivial Mealy machine.


(i) Moving the output associated p, which is 0, to transitions entering into state p, we get :

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

Example 2.4.21 : C h a n g e t h e given Moore machine into Mealy machine.

bv

Fig. Ex. 2.4.21

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)

Step 2 : Minimization of Mealy machine obtained in Fig. Ex. 2.4.20(a).


(i) 1-equivalent partitions P] based on outputs
p
i = (q») (Qi) (q 2, q 3 )
(ii) 2-equivalent partitions P 2 based on transition,
q 2 is mapped to block (qj on input 0.
qj is mapped to block (q2 , q 3) on input 0.
/. Behavior of q 2 is different from q .
p
2 = (q 0 ) (q,) (q 2) (q 3 )
Mealy machine given in Fig. Ex. 2.4.20(a) is the final Mealy machine.
Example 2.4.22 : Change the given Moore machine into Mealy machine.

Present state Next state Output


0

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

On input 0, q 0 and qj are mapped to blocl- (Po, Pl)


On input 0, s 0 and S! are mapped to block (q0 , q 1( s 0 , Sj)
r. Behavior of q 0, qi is different from s0 , s P
p
2 = (Po> Pi) (qo» Qi) ( s o> s i) (0
Further partitioning is not possible. The minimum state Mealy machine is given in
Fig. Ex. 2.4.22(c) and 2.4.22(d) _______________
■ ___VI
(Next state, Output)
Present state 0/0
.. . 0 1
vo 0/1
(Po. Pl) = -*10 q 3 , o q i . o (Aq) — (qn — <— -fas

(q 0 , qi) = q i qo. 1 q 2, 0 0/1


r1 / 0 1/0
(SO, S]) = q 2 q2> i q3, o
(r) = q 3 Ah 1 q0 , i
->0/1
(c) State transition table of Mealy machine

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

Example 2.4.24 : Convert the following Moore machine to Mealy machine.


Input
State a b Output
q0 qi q3 1
qi q3 qi 0
q2 q0 q3 0
q3 q3 q2 1
Solution :
Step 1 : Construction of trivial mealy machine by moving output associated with states on
incoming transitions.
... z j"-: ; /
Input
State a b
qo q/o q?>
qi q 3/ l q,/()
q2 q</i q 3/ l
q3 q 3/ l q 2/o

Scanned by CamScanner
V Theory of Comp, Sei, (MU-Sem. 5-Comp.) 2-132 Finite Automata

Step 2 : Minimization of trivial mealy machine.


State arc grouped on the basis of output.
(q0 ) (q» q?) (q 2)
The transition behaviour of q, and q, arc different under the input ‘b’. Hence the states
qj and q3 are distinguishable.
The final mealy machine is same as the trivial mealy machine and it is given
Fig. Ex. 2.4.24.
b/o

a/0
a/1
b/1
Ao - - -CjaD371
b/OY lb/1
a/1

Fig. Ex. 2.4.24

2.5 Minimization of a Mealy Machine :

Minimization of a Mealy machine is almost similar to minimization of a DFA


(Discussed in Chapter 3).
• In a DFA, initial grouping is based on accepting and non-accepting states.
• In a Mealy machine, initial grouping is based on outputs.
(Next state, Output)
Current state
■ . ■ , 1
liio i Oil 0

>q0 q 4. o q3- 1
Qi q5>° q3. o
q2 q4>° qi* i
q3 qj.o qi.o
q4 q2. o q5. 1
q5 qi.o q2. o

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

Step 2 : Finding 2-equivalent partitions P2 based on transitions.

Transition o n 1.

Transition on 0.
Block 1 Block 2

Fig. 2.5.1(a)

Behaviour of q5 is different from q | and q3 . 1-successor of q5 is block 1. l-successor


of qj and q3 is block 2.
p
2 = (qo.q2.q4) (qi. q3 ) (qs)
Block 1 Block 2 Block 3
Step 3 : Finding 3-equivalent partitions P3 based on transitions.
1-successor of q0 and q 2 is the block (q p q 3)
1-successor of q4 is the block (q5).
Behaviour of q4 is different from q 0 and q?.
p
3 = (q0 > q?) (q 4) (qi. q3 ) (qs)
Further division is not possible. The minimum machine is drawn in Fig. 2.5.1(b)
and 2.5.1(c).
(Next state, Output)
Present state
0 1
(qo> q 3 ) a s Po Pl.0 P"?* J
(q4 ) as p ( Po.0 p3 . 1
(q„ q 3 ) as p2 P3.0 P2.0
(q5 ) as p3 P2.0 Po. 0
Fig. 2.5.1(b) : State table for minimum state Mealy machine

1/1

0/0 1/1
0/0 /
0/0
1/0

Fig. 2.5.1(c) : State diagram for minimum state Mealy machine

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.

Fig. Ex. 2.5.1(a)

On input 0, A, B are mapped to block (F, G)


On output 0, C, D and E are mapped to block (A, B, C, D, E)
.*. Behavior of A, B is different from C, D, E
P 2 = (A, B) (C, D, E) (F, G)

Step 3 : 3-equivalent partitions based on transition.

Transition o n 1.
i i ri n nt
(A, B)(C, D,E) (F, G)

Transition o n 0.

Fig. Ex. 2.5.1(b)

Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-1 35

p, = (A, B) (C) (D, E) (F, G)

(A, B) (C) (D, E) (F. G)


t
t
Transition o n 0.

On input 0, D is mapped to block (C)


On input 0, E is mapped to block (D, E)
Behaviour of D is different from E. .
P4 = (A. B) (C) (D) (E) (F, G)
Further division is not possible.
The minimum state Mealy machine is drawn in Fig. Ex. 2.5.1(e).
(Next state, Output)
Current state
0 1 ->1/1
o/o CJ1/1
(A, B) = q0 q4. 0 q0 , 1
(C) = q. q0 > o q„ 1 \ X.1/1
(D) = q 2 q,. 0 q0, 1 1/1 o/o \i/i X 0/1

(E) = q3 92» 0 9o’ 1


(F. G) 33 q4 9 1) — <-----( Q 2n
Q3» i q4 > i 0/0 0/0
(d) State transition table (e) State transition diagram
Fig. Ex. 2.5.1 : Minimum state Mealy machine for Example 2.5.1

2.5.1 Difference between Moore and Mealy Machines :

Sr. No. Mealy machine Moore machine


1. Output is associated with transition Output is associated with state. _
2. There is an equivalent Moore There is an equivalent Mealy machine f°r
machine for every Mealy machine. every Moore machine. __
3. A mealy machine can be A Moore machine has often a larger
implemented with a fewer number number of states compare to the
of states. corresponding Mealy machine. _______

Scanned by CamScanner
Theory of Comp. Sci (MU-Sem. 5-Comp.) 2-136 ___________________ Finite Automata

Sr. No. Mealy machine Moore machine __


4. The transition function is given by The transition function is given by
x Q —> Q. The output function is Xx Q Q. The output function is given
given by, Z x Q -> O by, Q —> O
5. A mealy machine is seldom It is easy to implement a Moore machine
implemented either using circuit or using circuit or program.
program.

2.6 University Questions and Answers :

Q. 1 Design a divisibility by four tester FSM for Binary numbers.


(Similar to Example 2.2.23) (5 Marks)
Q. 2 Write short note on Mealy machine. (Sections 2.4.1 and 2.4.2) (5 Marks)

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. 5 Define formally a deterministic finite Automata DFA and Nondeterministic Finite


Automata. (Sections 2.2.1 and 2.3.1) (8 Marks)

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. 7 Design DFA that accepts the following language :


(i) Set of all strings with odd number of 1’s followed by even number of 0’s S = {0, 1}.
(ii) Set of all strings which begin and end with different letters S = {x, y, z}. .
(Example 2.2.29) (10 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

Q. 9 Distinguish between NFA and DFA. (Section 2.3.5) (5 Mar|<5j


Q. 10 Design a FSM for testing divisibility by three. (Example 2.2.27) (5 Markjj
Q. 11 Design a Moore machine to change all vowels to
(Similar to Example 2.4.11) (5 Mark,)

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

Q. 14 Convert the following e-NFA to minimized DFA.

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

(i) The set of all strings with three consecutive zeroes


(ii) The set of all strings ending in 00. (Similar to Example 2.2.16) (4 Marks)

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

(Similar to Example 2.2.36, Refer q 0 as A, q! as B, q 2 as C, q 3 as D and so on)


(10 Marks)

Dec. 2009

Q. 22 Distinguish between NFA and DFA. (Section 2.3.5) (4 Marks)


Q. 23 Construct DFA accepting the following language : Strings ending with 1 1 0 or 1 1 1 .
(Example 2.2.29) (5 Marks)

Q. 24 Define DFA and state applications of Finite Automata in brief.


(Sections 2.2.1 and 2.1.3) (5 Marks)

Q. 25 Obtain a grammar to generate the language L = { 0 n 12n I n > 0) }. (5 Marks)


Ans. :
Productions for the required language are as follows.
P ={S —» 0 S l l l e )
CFG for the above language is ({S), {0, 1 ], P, S)

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

Q. 28 Design a DFA to accept

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

(i) NFA and DFA (Section 2.3.5) (5 Marks)

(ii) Moore and Mealy Machines. (Section 2.5.1)

Q. 31 Find a minimum state finite automata equivalent to the following automata

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

Q. 32 Convert the following NFA to a DFA (Example 2.3.13) (5 Marks)

______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

Q. 38 Differentiate between NFA and DFA. (Section 2.3.5) ( 5 Marks)


Q. 39 Give applications of Finite Automata. (Section 2.1.3) ( 2 Marks)

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

A string not starting w i t h aa w i l l reach the dead state .


A string starting w i t h aa w i l l reach the state q 2.
A string starting w i t h aa and not ending i n aa will be either in q 4 or q 5 .
The DFA is given by,
M
= ({Qo. Q). q j . q 4. {a. b l , 8, q 0 , { q 4, q 5 } )

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

,(q0 - Previous carry as 0, qj - Previous carry as 1)


i.e., all trailing 0’s are written as 1 and the first 1 is written as 0.
o
Moore machine :
o 0

Fig. 2-Q. 44 1

May 2015

Q . 45 Differentiate between NFA and DFA. (Section 2.3.5) (5 Marks)

Q . 46 Design minimized DFA for accepting strings ending with 100 over alphabet (0,1).
(10 Marks)

Ans. : [Hint : Refer as Example 2.2.3(A)]


All strings ending in 100 :
The substring ‘100’ 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 ‘100’.
1
1 0
>l
- la A( Ao
40

A, At a2
A2 At
0 * %
Aj

(a) State transition diagram (b) State transition table


Fig. 1-Q. 46

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

Q. 48 Convert following g-NFA to NFA without g . (5 Marks)

a b

Fig. 1-Q. 48

Ans. : [Hint : Refer Example as Example 2.3.37]

To convert e -NFA to NFA without G


Step 1 : To remove e transition from q state to r state, we do following
(a) Duplicate transitions of r state on q state
(b) Since r is the final state, we make q as well as the final state.

Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-145

Step 2 : To remove e transition from p state to q state do following :


(a) Duplicate the transitions of q state on p stale
(b) Since q is a final state we make p as well as the final state.
Thus, the NFA is : b

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 :

& (Qo’ a ) — 2 $ (q6* c ) —** 96


b 5
8 (Oo> ) => q 4 (q 3 , a) => q 2

Scanned by CamScanner
CamScanner
V Theory of Comp, Sei, (MU-Sem, 5-Comp.) 2-146 Finite Automata

8 (q0 , c) => q 6 8 (q 3. 8) => q 3


8 (q 2, a) => q 2 8 (q3 , c) => q 3
8 (q 2, b) => q3 8 (q5. a) => q5
8 (q 2> c) => q , 8 (q 5 > b) => q4
8 (q 4 . a) => q 5 8 (q 5, c) => q 5
8 (q 4 , c) => q 5 8 (q 7, a) => q 7
8 (q 4, b) => q4 8 (q7, b) => q 7
8 ( q 6 , a) => q 7 8 (q7, C) => q 6
5 (q 6 , b) => q7
Q. 50 Design Moore Machine for the input from (0, 1 , 2 ) * which print the residue modulo 5 of
the input treated as ternary number. (Example 2.4.1) (10 Marks)
Q. 51 Convert the following grammar into finite automata. (5 Marks)
S -> aX I bY I a I b
X -» aS I bY I b
Y -> aX I bS
Ans. : [Hint : Refer Example as Example 2.2.22(B)]
The above grammar can be converted to FA as follows :
For every non terminating symbol we consider it as a different state
M = {Q,2,8,S,F}
a
Q = {S,X,Y}
S = {a, b}
S = initial state
b
F = {X, Y )

Fig. 1-Q. 51 : FA

8 : Transition functions are :


5 (S, a) => X
5 (S, b) => Y
8 (X, a) => S
8(X,b) => Y
8(Y,a) => X
5 ( Y , b ) => S

Scanned by CamScanner
CamScanner
V" Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-147

May 2016

Q. 52 Differentiate between NFA and DFA. (Section 2.3.5) (5 Marks)

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

Reminder calculation for finding the next state


State Binary value of the state SfeO) 8 (q„ 1)

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

q4 100 1000 - 5 = 3 (q 3 ) 1001 + 5 = 4 (q 4)


The operator + is for reminder.
Q. 54 Design mealy machine to find out 2's complement of a binary number. (10 Marks)
Ans. : [Hint : Refer Example as Example 2.4.7(A)]
2's complement of a binary number
2’s complement of a binary number can be found by not changing bits from right end till
the first ‘1’ and then complementing remaining bits. For example, the 2’s complement
of a binary number 0101 101000 is calculated as given below :

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

The required mealy machine is given below.


The input is entered from right to left.
0/1
1/1

Fig. 2-Q. 54

Q. 55 Convert the following NFA to an equivalent DFA (10 Marks)

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

The transition graph of the given NFA is :


e

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

State q0 is for the running reminder as 0.


State qj is for the running reminder as 1.
State q 2 is for the running reminder as 2.
Output 1 indicates divisibility by 3
Output 0 indicate that the number is not divisible by 3.
r. Required R.E. = (0 + 1 (1 +01)* 00)*
Q. 58 Convert the following NFA to an equivalent DFA
State a b 6

{ 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

Ans. : [Hint : Please refer example as Example 2.3.28(A)]


6 - closure of states
State G - closure
q0 { q»}
qi ( qj
92 ( qi . q 2 )
Constructing DFA using the direct method
Step 1 : Transitions for the state {q0 }

{q 0 .qd

Step 2 : Writing transitions for the state {q t }

{q 0 . q d

a,b
< Hd

Step 3 : Writing transitions for the state {q 0 , qj }

{qo» q d

a,b r
(qi) fai . qaM

Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 2-151

Step 4 : Writing transitions for the states { q b q 2 ) and ( q0 , q , , q 2 )

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.

These operators are :

1) + , union operator

2) . , concatenation operator

3) * , star or closure 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. '< _____• ______. ! _________ .

— ‘ ‘ Language I Reguiar ■expression;!


{,£} R.E. = e
1

©

(a) R.E. = a
<n J ‘
1

— O) (a,b) R.E = a + b

Fig. 3.1.1 contd...

Scanned by CamScanner
CamScanner
; Scl. 3-2 Regular Expressions ai

Automatons Language •sion

or a

aa

Fig. 3.1.1 : Examples on regular expression


If Rj and R 2 are regular expressions then :
Rj + R2 is also regular,
Rj • R 2 is also regular,

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.

[R+ stands for one or more occurrences of R J


• 0* stands for a language in which a word contains zero or more 0’s.
• ( 0 + 1 ) * stands for a language in which a word cu contains any combination of 0 s and
l’s and I o) I > 0.

Example 3.1.1 : Write regular expression for the following languages.


(a) The set {1010}
(b) The set {10, 1010}
(c) The set {e, 10, 01}
(d) The set {e, 0, 00, 000, ...}
(e) The set {0, 00, 000, ...}
(f) The set of strings over alphabet {0, 1} starting with 0.
(g) The set of strings over alphabet {0, 1} ending in 1 .
(h) The set of strings over alphabet (a, b} starting with a and ending in b.
(i) The set of strings recognized by (a + b)3
Solution :
(a) Regular expression,
R.E = 1010 stands for the set {1010}
(b) Regular expression,
R.E. = 10 + 1010 represent the set { 10, 1010)
(c) The set {e, 10, 01 } is represented by the regular expression,
R.E. = e + 1 0 + 01
(d) The set {£, 0, 00, 000, . . . } is represented by the regular expression,
R.E. = 0*
(e) The set {0, 00, 000, . . . } is represented by the regular expression,
R.E. = 0 +
R.E. = 00*
00* represents 0{ E, 0, 00, 000, . . . } = {0, 00, 000, . . . }
(f) The set of string starting with 0 is given by 0 followed by any combination of 0, 1.
R.E. = 0 ( 0 + 1 ) *
(g) The regular expression for a set over alphabet {0, 1 } ending in 1 is given by :
R.E. = ( 0 + 1)*1

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 +

3.2 Finite Automata Representing a Regular Expression

We can always construct an e-NFA for recognizing a set of strings represented by a


regular expression.
e-NFA can be converted directly into a DFA
OR
e-NFA can be converted into an NFA without e-moves which can be converted into a
DFA.
A regular expression can be converted to an e-NFA through recursive application of the
operations +, • , * on alphabet X, (|> and e.

Recursive nature of regular expressions :

A regular expression R = (1 + 0(10)*)* can be written as R = P*, where P = 1 + 0(10)*


P can be written as P t + P 2 , where Pj = 1 and P 2 = 0(10)*.
P 2 can be written as P 3 • P 4 , where P 3 is 0 and P 4 is (10)*.
P 4 can be written as P*, where P 5 is 10.
P5 can be written as P 6 • P 7 , where P 6 is 1 and P 7 is 0.
From the recursive nature of regular expression, we can conclude that :
If FAs for two regular expressions Rj and R 2 are given and if we can construct
composite FA for
(1) R j + R2
(2) Rj • R2
(3) R*
Then we can construct finite state automata (FA) for any regular expression.
3.2.1 Composite Finite State Automata

Let us assume the existence of FAs for regular expressions R t and R 2.

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

Each FA is assumed to have one final state.


FA for Rj + R2 is given by

2. FA for Rj • R 2 is given by

>O R1 o

3. FA for R* is given by

e e

OR

FA for Rf can also be shown as s

Finite automata for some primitive strings are shown in Fig. 3.2.1.

String Finite Automata


e

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

String Finite Automata

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

Fig. Ex. 3.2.2

E x a m p l e 3.2.3 : Show that P*(QP*)* = (P + Q)


Solution :

A finite automata recognizing P*(QP*)* is given by :

8 8

Naming the states, we get the Fig. Ex. 3.2.3.

Fig. Ex. 3.2.3 : A finite automata for P*(QP*)*

e-move from q0 to qi can be removed by of


1. Duplicating transitions of qj on q 0
P Q
AJ ’
2. Making q0 as a final state.
e-move from q 2 to qi can be removed by
1, Duplicating transitions of qj on q 2
2. Making q 2 as a final state.
Fig. Ex. 3.2.3(a) : An equivalent finite
automata without e-moves

Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-8 Regular Expressions and Langi ,

Since all the three states q 0 , q j and q 2 are final


states (in Fig. Ex. 3.2.3(a)), they can be merged into a
single state.
Fig. Ex. 3.2.3(h) : An equivalent ])p

Thus both expressions P*(QP*)* and (P + Q)* can be recognized by equivalent Dp


and hence we can infer that P*(QP*)* = (P + Q)*

Example 3.2.4 : Prove or disprove the following for a regular expression.


(r*s*)* = (r + s)*
Solution :
Step 1 : e-NFA for (r*s*)*

11
Fig. Ex. 3.2.4

Step 2 : e-NFA to DFA


e-closure of q 0 = (q0 , q , , q 2 )
e-closure of q, = {q0 , q h q 2 )
e-closure of q 2 = {q 0 , qj. q 2 }
The equivalent DFA is given shown in Fig. Ex. 3.2.4(a).

Fig. Ex. 3.2.4(a)

Step 3 : The regular expression for the DFA


= (r + s)*
= R.H.S.

Example 3.2.5 : Describe in English the language represented by following expressions .


(i) (a + ab)* (ii) (a + b)*a (a + b)*
(iii) (a*ab*ab*) + b* (iv) a + b*c+ •
Solution :

(i) (a + ab)* :

—» The language is “Every b is preceded by an a”.

Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-9 RegularExpressions and Languages

(ii) (a + b)* a (a + b)* :


—> The language is “The string contains at least one a”.
(Ill) (a*ab*ab*) + b* :
—> The language is “The string contains at least two a’s or a string of only b s
including null string”.
(Iv) a+b*c+ :
—> The language is “One or more a’s followed by any number of b’s followed by one
or more number of c’s”.

3.3 Determination of Regular Expression


MU - Dec. 2010, May 2013, Dec. 2017

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.

3.3.1 Language Generated by a Regular Expression

If r is a regular expression then the language generated by r is given by L(r).


Example :
If n = 0 + 1 then L(ti) = {0, 1 }
If r2 = 0* then L(r 2) = { e, 0, 00, 000, . . . }
If r3 = (0 + l ) * then L(r3 ) = {e, 0, 1, 00, 01, 10, 11, ...}

3.3.2 Basic Properties of Regular Expressions


Some of the basic identities for regular expressions are given below. P, Q and R are
regular expressions.
(f) + R = R ...(3.1)
(|).R = R.(j) = (|) ...(3.2)

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)

(P + Q)* = (P*Q*)* = (P* + Q*)* ...(3.14)

(P*Q)* = e + (P + Q)*Q ...(3.15)

Proof for basic properties :


1. 0 +R= R
Machine for (J) + R is given by

£ 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

Composite FA for E • R is given by or©

Scanned
Scanned by
by CamScanner
CamScanner
Theory of Comp. Sci (MU-Sem. 5-Comp.) 3-11 Regular Expressions and Languages

This machine accepts R and only R.


e - R = R.
4. e + RR* = R*
L.H.S. = e + RR*
= e + R{e + R + RR + RRR + . . . )
= e + R + RR + RRR + . . .
= R* = R.H.S.
5. (PQ)*P = P(QP)*
L.H.S. = (PQ)* P
= {e + PQ + (PQ) 2 + (PQ) 3 + . . . ) P
= {e + PQ + PQPQ + PQPQPQ+ ...} P
= {P + PQP + PQPQP+ ...}
= P { £ + QP + QPQP + ...}
= P (QP)* = R.H.S.
6. (P*Q)* = e + (P + Q)*Q
Finite automata for (P*Q)* is given by :

An

E-move can be removed by duplicating transitions of q t on q0.

-k 7
/Q
W

Finite automata for E + (P + Q)*Q is given by

p, Q
[ Q
qi

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-12

An equivalent DFA is being constructed using direct method


P
r>P
p Q
q q
— o,Ai O) ► C{ i 1 j — ►

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*

Fig. 3.3.2 : FA for e + (P + Qj*Q


DFAs for (P + Q)* given in Fig. 3.3.1 and e + (P + Q)*Q given in Fig. 3.3.2 are sane.
.-. (P*Q)* = e + (P + Q)*Q.

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)

= (1 +00*1) [e + ( 0 + 10*l)*(0+ 10*1)] ... [By Equation (3.7)]

= (1 + 0 0 * l ) ( 0 + 10*1)* . [By Equation (3.12)]

= [(e + 00*) 1] ( 0 + 10*1)* ...[By Equation (3.8)]

= 0*1(0+ 10*1)* . [By Equation (3.12)]

= 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

Solution : We have to find all strings of length 4 or less in (aa + b)*


(i) String of length 0 = {8}
(ii) String of length 1 = {b}
(iii) String of length 2 = {aa, bb], taking either ‘aa’ one time or b two times.
(iv) String of length 3 = { bbb, baa, aab}, taking either b 3 times or b and aa one time each
with permutations.
(v) String of length 4 = { aaaa, bbbb, bbaa, aabb, baab ),
taking aa two times,
taking b four times,
taking aa one time and b two times with permutations.
/. Required strings are {e, b, aa, bb, bbb, baa, aab, aaaa, bbbb, bbaa, aabb, baab}
(i) (s +)* = (s*)* - True, both (s +)* and (s*)* contain 8
(ii) (s +) + = s + - True, both (s+) + and s+ do not contain e
(iii) (s*) + - (s + )* - True, both (s*)+ and (s + )* contain 8.

Scanned by CamScanner
CamScanner
Theory of Comp. Sei, (MU-Sem, 5-Comp.) 3-14 Regular Expressions and Lan.

Example 3.3.6 : Find all possible regular expression L g {a, b}*


(a) the set of all strings ending in b.
(b) the set of all strings ending in ba.
(c) the set of all strings ending neither in b nor in ba.
(d) the set of all strings ending in ab
(e) the set of all strings ending neither in ab nor ba.
Solution :
(a) The set o f all strings ending i n b.
R.E. = (a + b ) * b
(b) The set o f all strings ending in ba.
R.E. = (a + b)*ba
(c) The set o f all strings ending neither in b nor i n ba.
R.E. = (a + b)*aa + a + e
(d) The set of all strings ending in ab
R.E. = (a + b)*ab
(e) The set o f all strings ending neither in ab nor ba.
R.E. = e + a + b + (a + b)*(aa + bb)

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.

R.E. = (1 4- 0)*(00 4- I I 4- 10)


--------------------- ------------- -- ---------------------- -'
Example 3.3.8 : Construct NFA with e-moves for R.E. = 0.1[ (10* + 1 11)* + 0 ]*1. Convert
it to DFA using direct method of conversion from NFA with e-moves to DFA'
Solution : The e-NFA is being constructed recursively as shown in Fig. Ex. 3.3.8.

(10* 4- 111)*

o
5o. -►-Uli

Fig. Ex. 3.3.8 Contd

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

Fig. Ex. 3.3.8 : e-NFA for Example 3.3.8

DFA is being constructed from e-NFA. It is shown in Fig. Ex. 3.3.8(a).

0
0

0 0

Fig. Ex. 3.3.8(a) : DFA for Example 3.3.8

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‘

Fig. Ex. 3.3.9(a)

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

Fig. Ex. 3.3.9(b)

Solution : Step 1 RE to NFA


b(aa)*b
b(aa)*b + ab*a

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

Step 2 : NFA to DFA


- e-closure of states :
State e-closure
q0 (qo)
qi {qi. q 2. qJ
q2 (q 2- qJ
q3 {q3 }
q4 {qj
q5 { q 3’ q6» q?)
q6 {q 6 > q 7 )
q7 {q7 }

Fig. Ex. 3.3.10(b)

Example 3.3.11 : Which of the following are true ? Explain.


(i) baa g a*b*a*b* (iii) a*b* n b*c* = 0
(ii) b*a* n a*b* = a* u b* (iv) abed g (a (cd)*b)’
Solution :
(i) baa G a*b*a*b*
a *b*a*b* = (e + a + aa + ...) (e + b + ...) (e + a + aa + ...) (e + b + bb + . . . )

By taking e-from the first set,


b-from the second set
aa from the third set
and £ from the fourth set
We can say that, baa G a*b*a*b*. Answer is True.
(ii) b*a* n a*b* = a* u b*
b*a* = (£ + bb*) (£ + aa*) ...[From Equation (3.12)]

Scanned by CamScanner
CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp,) 3-18 Regular Expressions and Larig,

e + aa* + bb* + bb*aa*


(E + aa*) + (e + bb*) + bb*aa*
a* + b* + bb*aa*
Similarly, a*b* =• a* + b* + aa*bb*
Taking intersection of equations (QI 1.1) and (QI 1.2)
b*a* n a*b* = (a* 4- b* + bb*aa*) (a* + b* + aa* bb*)
= a* + b* = a* o b*
Answer is True.
(iii) a*b* n b*c* = (|)
Both a*b* and b*c* contain the common string b*.
Answer is False.
(iv) abed G (a(cd)*b)*
A string belonging to (a(cd)*b)* can not end in cd.
Answer is False.
Example 3.3.12 : For each of the following draw DFA of following regular expressions :
(a) (11+00) (b) (11 1 + 100)*0
Solution :
(a) DFA for (11 +00)*

(11+00)

11+00

1/

do

0t

Fig. Ex. 3.3.12(a)

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

Fig. Ex. 3.3.12(b)

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

Fig. Ex. 3.3.13

(b) FA for 01 [((10)* + l l l ) * + 0]*l


[((io)* + iiir+o r
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

(c) FA for [ 00 + 11 + (01 + 10) (00 + 11)*(01 + 10)]

00
L-x11
9 J
(01+10) (00+11)* (01+10)

oT
j/ 1/
(9i) q2 — o
t 0, 1\ 1 98

-+»- 01+10 fas)


95
01+10 0
0

00+11
9a 94

0
1 1
0
9g

Fig. Ex. 3.3.13(b)

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;

2. F.A. for 1(01 + 10)* + 0(11 + 10)*


1(01+10)*
1(01+10)*+0(11+10)*

0(11+10)*

(01+10)

(11+10)

01+10
E

E
£

11+10

0.
£

0'S

0
E /V-A

3. F.A. for (010 + 00)*(10)

(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

Fig. Ex. 3.3.14(c)

4. F.A. for 1(1 + 10)* + 10(0 + 01)*


1(1+10)
1(1+10)*+10(0+01)

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

Fig. Ex. 3.3.15(a) : e-NFA for the given example

DFA using the direct approach is shown in Fig. Ex. 3.3.15(b).

a
a
b

ci,b

Fig. Ex. 3.3.15(b) : An equivalent DFA

Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-25 Regular Expressions and Languages

Example 3.3.16 : What language is represented by Regular expression. (((a a)b) u b)


Solution : The regular set (((a*a)b) u b) can be written as
RJE. = (a*a)b + b = (a*a + e)b
= [From Equation (3.12)]
Thus the language represented by the regular expression is “zero or more a s followed
by a ‘b’ ”.

Example 3.3.17 : Consider the two regular expression


r = 0* + 1*, s = 01* + 10*+ 1*0 + (0*1)*
a) Find the string corresponding to r but not to s .
b) Find the string corresponding to s but not to r.
c) Find the string corresponding to both r and s .
Solution :
a) Find the string corresponding to r but not to s.
00 is in r but not in s
b) Find the string corresponding to s but not to r.
01 is in s but not in r
c) Find the string corresponding to both r and s.
11 is both in r and s.

>, Dec. 2007, May 2008


Solution :

(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

0 £

0 £ £

Fig. Ex. 3.3.19

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)

Fig. Ex. 3.3.20(a) : RE to NFA contd...

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

Fig. Ex. 3.3.20(a) : RE to NFA


NFA to DFA
0

0 0

4
0 ti
rrrs/i
{do>d2 } ) — *—

Fig. Ex. 3.3.20(b) : NFA to DFA


01
(ii) (01* + 1 ) 01*4-1
RE to NFA Cj ~~~ *(( .

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

Fig. Ex. 3.3.20(d) : NFA to DFA

(iii) (a + b)* aba


RE to NFA (a+b)* a b a

(a+b) a

a,b
£ a

a b
►— — ►

Fig. Ex. 3.3.20(e) : RE to NFA

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-

ign a DFA corresponding to the regular expression (a+b) * aba(a+bf


MU - D e c . 201°
Solution :
Give regular expression : (a + b)* aba (a + b)*.
Step 1 : RE to NFA (a + b ) ‘ aba (a + b)

(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

e -moves can be eliminated as the adjacent nodes do not contain a loop.

Step 2 : NFA to DFA using the direct method


b a,b

a b

MU -May 2014
(00+11)* (10)*

/ e \

6
\
OOz-x \

3.4 DFA to Regular Expression


There are two popular approaches for constructing a regular expression from FA.
(1) Through state/loop elimination
(2) Arden’s theorem.

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-30

3.4.1 State/Loop Elimination Process

Every state q, F (set of final states) can be eliminated if q s is not a start


state
Elimination of a state involves writing of regular expression as label on arcs.
For example,
After elimination
of state q 1
a
1. Ao >-

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

3.4.1.1 A Generic One State Machine

The regular expression for strings accepted by a


one state generic machine is R*.

3.4.1 .2 A Generic Two State Machine


A regular expression for a two sate generic
machine can be written easily after elimination of loop
between q0 and q R. R3 r\R 2
• The effect of the loop can be moved either to
state q0 or q P \ao) Aw

• Even after elimination of loop between q () and


q b a path from start state (q 0) to final state (qj)
should be maintained.

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-31 Regular Expressions and Languages

Machine after moving the effect of loop to state q0 :


Starting from the start state q 0 , one can come back to q0 cither on Rj,
or R 3 R*R 4 R-| + 3 2 4

R2
The equivalent R.E. for machine shown in Fig. 3.4.4

R.E. = (Rj + R 3 R* R 4 )*R 3 R* Fig. 3.4.4 : Effect of loop is moved to state q0

Machine after moving the effect of loop to state q! :

Starting from the state q b one can come back to qj either on R 2 .


or R4 R*R 3
The equivalent R.E. for machine shown in
Fig. 3.4.5 can be written easily. It is given by :
R.E. = R*R 3 (R 2 + R 4 R*R 3 )* Fig. 3.4.5: Effect of loop is moved to state qj

Example 3.4.1 : Find a regular expression for the given two state generic machine.
b

Fig. Ex. 3.4.1 : A two state generic machine

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

R.E. corresponding to final state q 0 = (a + bc*d)


R.E. corresponding to final state qi = (a + bc*d) be*
The final, regular expression will be obtained by taking union of R.E. for state q 0 and q P
.-.Final R.E. = (a + bc*d)* + (a + bc*d)*bc*
= (a + bc*d)*[e + be*]

Scanned by CamScanner
Scanned by CamScanner
V Theoiy o f Comp. Sci. (MU-Sem. 5-Comp.) 3-32 Regular Expressions and Lanqu-

Example 3.4.2 : Convert the automata shown in Fig. Ex. 3.4.2 to RE


a a

b b
a

ib

Fig. Ex. 3.4.2

Solution :
(i) Machine is redrawn after the effect of loop between state 1 and 2 is transferred to
state 1.
a + bb*b

Fig. Ex. 3.4.2(a)

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

Fig. Ex. 3.4.2(b)

R.E. = e + (a + b) (a + bb)*b + a

for state 1 for state 3

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

Fig. Ex. 3.4.3

Solution :

Reducing the given T.G.

0+1

Fig. Ex. 3.4.3(a)

Now, eliminating the state q2 , we get,

00*(0+1)
q
3.

Fig. Ex. 3.4.3(b)

Machine is redrawn after the effect of loop between q! and q3 is transferred to q P


00*(0+1)1

Fig. Ex. 3.4.3(c)

Final R E. = 1 [00*(0 + 1)1]* 00* ( 0 + 1)

Scanned by CamScanner
3-34

Example 3.4.4 : Find R.E. for shown in Fig. Ex. 3.4.4.

0
9l q3

Fig. Ex. 3.4.4


Solution :
R.E. for state qj = 0*
R.E. for state q 2 = 0*11*
R.E. for state q3 = 0*11*0(0 4-1)*
/. Final RE = 0* 4- 0*1 1* 4- 0*1 1*0(0 4-1)*
-------------------------------------------------------------------------------------------------------------------
Example 3.4.5 : Consider the transition diagram shown in Fig. Ex. 3.4.5. Convert it *■
equivalent RE.
0

93

Fig. Ex. 3.4.5


Solution : Machine in redrawn after the effect of loop between Oo’O3»O2 is transferred to q0.

Fig. Ex. 3.4.5(a)


Now, removing the loop between q() and q 2,

. 11(01)‘0 4- 1 (01 )’0

©
Fig. Ex. 3.4.5(b)

Final RE = [1 l(01)*0 4- l(01)*0|*

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

Fig. Ex. 3.4.6


Solution : The state q 1 is at the crossing point of three loops :
1- Qi to qj on input b.
2. Loop between qj and q0 on input ea*d
3. Loop between qj and q 2 on input fc*g.
The effect of these three loops can be transferred to state qf, the machine after this effect
is shown in Fig. Ex. 3.4.6(a).
a d b+ea*d+fc*g

f
*fez (fez)
Fig. Ex. 3.4.6(a) : Moving the effect of loops on state qj

The state qj can be eliminated.

d(b+ea*d+fc*g)*f

Fig. Ex. 3.4.6(b) : After elimination of state qj

Now, the regular expression can be written as,


R.E. = a*d(b + ea*d + fc*g)*fc*

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

Fig. Ex. 3.4.7(b)


R.E. = (b + ab)*aa(a + b)*
(iii) Machine is redrawn after the effect of loop between q j and q 2 is transferred to both q.
and q ? .
io

0+11 X—\
( J 0 ’1

KM

1+00

Fig. Ex. 3.4.7(c)

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

Example 3.4.8 : Prove the identity given below :


(a'ab + ba)’a‘ = (a + ab + ba)*
Solution : We can start with construction of NFA for (a + ab + ba)* and then convert it in'0’
DFA.
Step 1 : (a + ab + ba)* to NFA. An equivalent NFA is given in Fig. Ex. 3.4.8
a

\ b
a I
Tb

q 2j

Fig. Ex. 3.4.8 : An equivalent NFA

Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-37 Regular Expressions and Languages

Step 2 : NFA to DFA. An equivalent DFA is given in Fig. Ex. 3.4.8(a).

—>{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

Fig. Ex. 3.4.9


Moving the effect of the loop on [q n, q p q ? ), we get,

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

Fig. Ex. 3.4.10

Moving the effect of loops on state q0 we get,


1+01+001

R.E. = (1 4-01 4- 001)*(e 4- 0 4- 00)


(b) The language of all strings that do not contain the substring 110.
R.E. can be written by drawing an equivalent FA and then writing a regular expression
for it.
DFA for strings not containing 110 is given by,
o
% 1—

Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Cornp.) 3-40 Regular Expressions and Language

Moving the effect of loop on state q o , we get,

Fig. Ex. 3.4.10(c)

R.E. = (0 + 10)* + ( 0 + 10)*l + ( 0 + 1 0 ) * l l l *


(c) The language of all strings containing both 101 and 010 as substring.
R.E. = (0 + l)*101(0 + l)*010(0 + 1)* + (0 + l)*010(0 + 1)* 101 (0 + 1)*

Example 3.4.11 : Write RE for the following L = {0,1}


(i) At most 1 pair of consecutive ‘0’ or 1 pair of consecutive ‘1’.
(ii) Without any double letter.
Solution :

(i) At most 1 pair of consecutive ‘0’ or 1 pair of consecutive ‘1’ :


A finite automata with at most 1 pair of consecutive 0’s is shown in Fig. Ex. 3.4.11.

1
I 0 o
0
2

Fig. Ex. 3.4.11


R.E. for strings with at most 1 pair of consecutive 0 ' s is
(1 +01)* [£ + 0 + 00(11*0)* + 00 (11*0)* 11*]
R.E. for strings with at most 1 pair l’s is
( 0 + 1 0 ) * [e + 1 + 11 (00*1)*+ ll(00*l)*00*]
Required R.E. is:
(1 +01)* [E + 0 + 00 (11*0)* + 00 (11*0)* ! ! * ] + (() + 10)*
[e + 1 + 11 (00*1)* + 11 (00*l)*00*]
(ii) Without any double letter :
A Finite Automata without any double letter is shown in Fig. Ex. 3.4.11(a).

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-41 Regular Expressions andj-anguages

Ho 0

Fig. Ex. 3.4.11(a)

.-. R.E. = e + 0(10)* [e + 1] + 1 (01)* [e + 0]

Example 3.4.12 : Write RE for the following languages :


(i) The language of all strings that contains at least one occurrence of
each symbol in Z = {0,1}
(ii) Z = {a, b} such that ab is not a substring of any strings.
Solution :
(i) The FA for language o f all strings containing at least one occurrence of each symbol in
Z = {0, 1 } is shown in Fig. Ex. 3.4.12 :

J 0 ’1

33))

Fig. Ex. 3.4.12

The RE can be written from the DFA.

/. R.E. = b * + b*aa*

Example 3.4.13 : Write a R.E. for the following


(I) L = (0, 1) odd number of 1 ’s in strings
(ii) S = {0. 1} Triple 0 must never appear in strings.
(iii) Identities in C language.

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

An equivalent FA with effect of loop between q 0 and qj moved to q0 .


0+10*1 x-x 0
1
—-X%) W))
Fig. Ex. 3.4.13(b) : An equivalent machine of DFA given in Fig. Ex. 3.4.13(a)

Step 2 : Writing of R.E. from Fig. Ex. 3.4. 13(b).


R.E. = (0 + 10*l)*10*
(ii) Z = {0, 1 } Triple 0 must never appear in string.
Step 1 : Drawing an equivalent DFA.
•>1
o
i —£ Qi >-
v0
0.1
T
Fig. Ex. 3.4.13(c) : DFA for strings not having 000
An equivalent FA with effect of loops moved to qo is shown in Fig. Ex. 3.4.13(d)

0+01+001

Fig. Ex. 3.4.13(d) : An equivalent Frk for DFA of Fig. Ex. 3.4.13(c)

Step 2 : Writing of R.E.


R.E. = (1 + 01 + 001)*(e + 0 + 00)
(iii) Identifiers in C language.
R.E. = (A + B + . . . + Z + a + b + z) (A + B . . . + Z + a + . . . z + 0 + ...9)*

Scanned by CamScanner
• TheoryofComp Sci. (MU-Sem. 5-Comp.) 3-43 Regular Expressions and Languages

Example 3.4.14 : Represent the following using regular expressions.


(i) S = {a, b, c} the language such that “any number of a’s followed by
any member of c’s”
(ii) If L (r) = {0, 2, 201 , 21 , 01 1 , 21 1 , 01 1 1 , ...} then what is r ?
(iii) If L (r) = {00, 010, 0110, 01110,...} then what is r ?
(iv) Languages defined over S = {a, b} has to have the strings beginning
with ‘a’ and not have two consecutive a’s. Write the regular expression
for the same.
Solution :
(i) R.E. = a*c*
(ii) L(r) = {0,2,01,21,011,211,0111,...}
r = 0 + 2 + 01 + 2 1 + 0 1 1 + 2 1 1 + 0 1 1 1 + . . .
= 0 ( E + 1 + 11 + 111 + . . . ) + 2 ( e + 1 + 11 + . . . ) = 01* + 21*
r = (0 + 2)1*
(iii) L(r) = {00,010,0110,01110,...}
R.E. = 0(l)*0
(iv) DFA for the given language can be drawn as shown in Fig. Ex. 3.4.14(a).

a b

Fig. Ex. 3.4.14(a)

Moving the loop between q j and q 2 and q, we get :


bb*a
a

Fig. Ex. 3.4.14(b)

R.E. corresponding to q j = a (bb*a)


R.E. corresponding to q 2 = a (bb*a) bb*
.’. Combined R.E. = a (bb*a)* + a (bb*a)*bb*
= a (bb*a*) + [e + b 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

Fig. Ex. 3.4.15(a)


R.E. = (0 + l)*00 (0 + 1)*
(b) L, = {co g X* I co has no pair of consecutive zero)
The equivalent FA is shown in Fig. Ex. 3.4.15(b)

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

Fig. Ex. 3.4.15(c) : FA alter the effect of loop moved to q 0

R.E. for state q 0 = ( 1 + 0 1 ) *


R.E. for state q, = ( l + 0 l ) * 0
The final R.E. = (1 + 01)* + (1 + 01)*0 = (1 + 01)*[e + 0]
(c) L 3 = {co G E* I co is a string with either 1 preceding a 0 or 0 preceding 1 }

The equivalent FA is shown in Fig. Ex. 3.4.15(d).

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-45 Regular Expressions and Languages

Fig. Ex. 3.4.15(d)

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

A R.E = e + 1(01)* + 0(10)*


(d) L 4 = {cd G Z* I cd consists of an even number of 0’s followed by an odd number of 1’ s. }
The equivalent FA is shown in Fig. Ex. 3.4. 15(f)

o'

ds

Fig. Ex. 3.4.15(f)

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*

Example 3.4.16 : Prove the formula (00*1) 1 = 1 + 0(0 + 10) 11


Solution :
The formula can be proved by drawing an equivalent DFA.
DFAfor (00*l)*l

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-46 Regular Expressions and Languaa

Ao

Fig. Ex. 3.4.16(a) : An equivalent e-NFA

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

Fig. Ex. 3.4.16(b) : An equivalent DFA

FA after moving the effect of loop between { q 2 ) and { q 3, q 0 , qj } is redrawn as shown in


Fig. Ex. 3.4.16(c).
0+10

Fig. Ex. 3.4.16(c) : Affect of loop is moved to { q2 }

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

Example 3.4.17 : Obtain a regular expression such that L ( R ) = { W I W e { 0, 1 }* } with


atleast three consecutive zeros.
Solution :
R.E. = (0 + l)*000 (0 + 1)*

3.4.2 Arden’s Theorem

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

3.4.2.1 Application of Arden's Theorem

A finite automata can be represented using a system of equations :


a

Fig. 3.4.6 : A transition diagram to be converted into a set of equations

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

Solving of equations using Arden’s theorem :


1. Substituting for q 2 in Equation (3.20). q 2 is given by Equation (3.19)
q j = q 0 a + q ! b + q t aa

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

To use Arden’s theorem, the FA should not contain e-moves.

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

Fig. Ex. 3.4.18

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.

Fig. Ex. 3.4.19

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)

From Arden’s theorem,


q 0 = [e + q / 1 + 00 + 01)] (1(0 + 1))* ...(4)

Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-52

Putting the value of q 0 from Equation (4) in Equation (2),


qi = q , [2 + (e + q L( l + 00 + 01)] (1(0 + l))*0 '
= (1(0 + l))*0 + q,[2 + (1 + 00 + 01) (1(0 + 1))*0]
From Arden’s theorem,
q, = (1 + ( 0 + l))*0[2 + ( l + 0 0 + 01) (1(0+ l))*0]*
Putting the value of q t from Equation (5) in Equation (4)
q0 = [e + (1 (0 + 1) )*0[2 + (1 + 00 + 01)
(1(0+ 1) )*0]*(l + 0 0 + 01)] (1(0+1))*
The R.E. describing the machine is given by,
R.E. = q0 + q, [both q0 and q! are final states]
= [e + (1 (0 + l))*0[2 + (1 + 00 + 01) (1(0 + 1) )*0]
(1 + 00 + 01)] (1(0 + 1))* + (1(0 + 1))*
0 [2 + (1 + 0 0 + 01) (1(0+ l))*0]

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

Fig. Ex. 3.4.21


Solution :
Step 1 : Expressing the automata as a set of equations.
Qi
<h
% = q2 °
<h
Step 2 : Solving of equations
Putting the values of q 3 and q 4 (from Equations (3) and (4)) in Equations (1) and (2).

- e + q t 0 + q 2 010 + q 2 00

Scanned by CamScanner
3-53

= e + q , 0 + q 2 (010 + 00) ...(5)


q 2 = q,l+q 2 l +q,ll
= q1 l+q 2 l+q 2 011
...(6)
' = qJ+qjd+OU)
From Equation (6)
q 2 = q , 1 ( 1 +011)* ...(7)

From Equations (5) and (7)


qt = G + q 1 O + q 1 l (1 +011)* (010 + 00)
= G + q J O + 1 ( 1 + 0 1 1 ) * (010 + 00)]
q j = [ 0 + 1 ( 1 + 0 1 1 ) * (010 + 00)]* ...(8)

From equations (7) and (8)


q 2 = [ 0 + 1 (1 +011)* (010 + 00)]* 1 (1 +011)* ...(9)

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

1. FA cannot handle the following types of languages.


a) Context Free Language
b) Context Sensitive Language
c) Recursively enumerable language
2. FA cannot be used for computation.
3. FA is not allowed to modify its own input. A taring machine can modify its own input.
4. FA does not have memory to store variables. Therefore, it cannot be used for solving of
general problems.
5. FA can be used for a subset of language, which is regular.

3.6 Pumping Lemma for Regular Language


MU - Dec. 2005, Dec. 2007, May 2009, Dec. 2009, May 2013, Dec. 2016

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?

Let L be a regular language and M = (Q, Z, 8, q0 , F) be a finite automata with n-states.


Language L is accepted by m. Let co e L and I col > n, then co can be written as xyz, w here
(i) Iy I> 0
(ii) I xy I < n
(in) xy z G L for all i > 0 here y1 denotes that y is repeated or pumped i times.
3.6.2 Interpretation of Pumping Lemma
MU -Dec. 2009
a

a
b
do)- - » ( qi ► J— ►— (da) — *— « q 4

Fig. 3.6.1 : FA considered for interpretation of pumping Iemma

Let us consider the FA of Fig. 3.6. 1


No. of states = 5 (q 0 to q 4 )
Let us take a string co with I co I > 5, recognized by the FA.
co = abcabcb
co - abcabcb, the machine will transit through various states as
shown in Fig. 3.6.2.

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

Fig. 3.6.2 : Transitions of FA on input abcabcb

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

1. Suppose L is a regular language.


2. Suppose M be a DFA with n states, such that DFA accepts the given regular language L.
i.e. L = L(M), Language of M is same as the given regular language. •
3. Let us consider a string cd e L | I cd I > n.
String CD can be written as
aj a 2 a 3 . . . a,., with m > n.
4. Let us assume that states of M are given by q0 , q h q 2, . . q n - 1 with q0 as a starting state
and q n _j as a final state.
5. Let us assume that after feeding the first i characters of the word cd = aj a 2 . . . a m,
machine will be in a state q.
8*(q 0 , aj a 2 , . . . aj) = q
6. As the word cd is fed through the machine M, the machine will go through the various
states as :
q0 rj r2 Qn-i <— state
aj a2 • am input
Since the length of cd, I cd I > n it is not possible for the machine to move through distinct
states.
7. Let us assume that q and q are same. There is a loop from q to q.
8. The string cd can be divided into three parts.
1. x = Portion before loop = aj a 2 . . . a,
2. y = Portion of loop = ai + 1 ai + 2 . . . aj
3. z = Portion of loop = a j + 1 aj + 2 • • • am

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

Fig. 3.6.3 : Behaviours of machine M on input x y z for i > 0

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

Let us write w 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 the form a s.
Now, a n bn can be written as

Step 3 : Let us check whether xy’z for i = 2 belongs to L.


3
xy 2 z = a s (a r) 2a n ‘ s ’ r b"
= a s a 2r a n - s ‘ r b"
= a s + 2r + n - s - r b n
n+r ,n
= a b
Since r > 0, number of a’s in a" + r b n is greater than number of b’s. Therefore, xy 2 z £ L.
Hence by contradiction we can say that the given language is not regular.

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 :

xy'z = a 1' (b 1)” b q 1

= 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

Step 3 : Let us check whether xy'z for i = 2 belongs to L.


2 s / i\2 n _ $ _ (■ 2n
xy z = a (a ) a b
s 2r n-s-r , 2n
= a a a b
s + 2r + n - 8 - r | 2n

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

Step 3 : Let us check whether xyz for i = 2 belongs to L.


xy2 z = oW -’lV
_ qH + r | rriQm + n

Since r > 0, o n + r l mOm + n is not of the form


• oW
2
Therefore, xy z g L. Hence by contradiction, we can say that given language is not
regular.

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

Let us write w as xyz with


lyl > 0 •
and I xy I < n
Since I xy I < n, x must be of the form a\
Since I xy I < n, y must be of the form a r I r > 0.
Now, II 3
ro = a bba = a a a bba

Step 3 : Let us check whether xy’ z for i = 2 belongs to L.


2 $ 2r n - s - r. . n n + r. . n
xy z = a a a bba = a bba
Since r > 0, a n + r bba n is not of the form <ocoR as the strings starts with (n + r) a’s but
■*)

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

Step 3 : Let us check whether x y z for i = 2 belongs to L.


xy z = a a a ba b

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

Step 3 : Let us check whether xy lz for i = 0 belongs to L.


0 s n-s-r , n. n, n-r, n, n.
xy z = a a ba ba b = a ba ba b
Since r > 0, a n rba n ba n b is not of the form cococo. Hence by contradiction, we can say that
the given language is not regular.

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

We can assume that y = a’n for m > 0.


Step 3 : Length of x y z can be written as given below :
* i 1
I xy’z I = I xyz 1 + 1 y I = p + (i - 1 ) m

as I y | = | am I = m

Let us check whether P (i - 1) m is a prime for every i.


For i = p + 1, p + (i - 1) in = P + P ]n = P ( 1 + m).
P (1 + m) is not a prime as it has two factors p and (1 + m) and
Ip I > L
I1 + m I > 1
So xyp + z g L . Hence by contradiction the given language is not regular.

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

From the two inequalities (1) and (2)


n < I xy z I < (n + I )2 .
Thus xy z £ L. Hence by contradiction, the given language is not regular.

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.

Example 3.6.1 1 : Prove L = {(ab) n ak : n > k, k > 0} is not regular.


I MU -May 2006, May 2008
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
co - (ab) a
I co I — 2 ( n + l ) + n = 3n + 2 n
Let us write to as xyz, with
ly I > 0
and I xy I < n
The string xy will contain a maximum of n symbols from (ab)".
Step 3 : In the string x y z with i = 0, at least one ‘a* or atleast one ‘b’ will be erased from
(ab) n + 1 of (ab)" + V . This will lead to one of the following situations :

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-64 Regular Expressions and

1. Number of a’s in (ab)n is equal to number of a’s in a k of (ab)na k.


2. x y z will not be of the form (ab)na k.
Therefore, xy°z g L.
Hence, this is proved by contradiction.
2n
Example 3.6.12 : Is L = {a I n £ 1) regular ?
Solution :
The given language is regular as we can draw an equivalent FA.
[From Kleene’s theorem]
The FA is given in Fig. Ex. 3.6. 12. q0

do

Fig. Ex. 3.6.12 : FA for a 2n

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

3.7 Closure Properties of Regular Language

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.

Regular Language is Closed under Union

Let M j = (S, L, 8 b s0 , F) and

M 2 = (Q, E, § 2, q0 , G) be two given automata.

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

Machine M 3 is constructed to accept L(Mj) u L(M 2).

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

Machine M 3 can non-deterministically choose either M, or M 2. Therefore,

L(M 3) = L ( M , ) U L ( M 2)

3.7.2 Regular Language is Closed under Concatenation

Let Mi = ( S , E , 8 b s 0 , F )

and M 2 = (Q, S, 8 2, q 0 , G) be two given automata.

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 )

M 3 is constructed by adding e-move from every final state of M j to start state of M 2.


Machine M 3 is given by :
M 3 = (R, Z, 8 3 , s 0 , G) where
8 3 = 6] u 8 2 u ( E-move from every final state of M l to start state of M 2 }
Machine M 3 recognizes L(Mj) • L(M 2) by going non-deterministically from the
state of Mi to start state of M 2 .

Scanned by CamScanner
CamScanner
) 3-67 Regular Expressions and Languages
3.7.3 Regular Language is Closed under Kleene Star

Let Mj - (Q, X, 8, q0 , F) be the given automata. We can construct a non-deterministic


finite automata M2 such that L(M2) = L(M ( )*. The construction of M2 from Mj is shown in

Fig. 3.7.3 : M 2 is constructed such that L(M2 ) = LfMj)*

M2 is constructed as given below :


(a) A new start state s0 is added with an E-move from s0 to q0.
(b) A new final state f 0 is added with E-moves from every state of F to f 0, An E-move is
added from s0 to f 0 as s is a member of L(Mj)*.
Machine M 2 = ( Q u {s0, f 0 }, X, 8, s0, {f 0 })
Machine can accept a string G L(Mj) and resume back from the start state q0 through the
E-move from f 0 to q0. Thus accepting L(M1)*.
3.7.4 Regular Language is Closed under Complementation
• r

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

If Li and L 2 are two regular languages, then

Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-68

I n L j = ((Lj n L 2)')' = (L, u L2 / = S* - [(Z* - LJ u (X - L J ]


Closeness under intersection follows directly from closeness under union and
complementation.

3.7.6 Regular Languages are Closed under Difference

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

Fig. Ex. 3.7.1(a) : An NFA accepting strings ending in 01 or 10


If L is a language representing strings ending in 01 or 10. A machine for L R is shown in
Fig. Ex. 3.7.1(b). 0 1
9i 95
e

9o S
0J

E
93 96

Fig. Ex. 3.7.1(b) : An NFA accepting L R

Scanned by CamScanner
1

5-Comp.) 3-69 Regular Expressions and Languages

Example 3.7.2 : Explain your answer in each of the following :


(i) Every subset of a regular language is regular.
(ii) Every regular language has a regular proper subset.
Solution :
(i) Every subset of a regular language is regular.
A subset of a regular language need not be regular. Let us consider a language

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

(c) If Lj and L 2 are non-rcgular then Lj u L2 is non-regular.


Answer = True.
We will not be able construct a FA for Lj u L 2 if L, and L2 are non-regular.

Example 3.7.4 : Let L be any subset of 0*. Prove that L* is regular.


Solution :
It is given that L is any subset of 0*.
DFA of L is given by :

Fig. Ex. 3.7.4 : DFA for L

L* can be considered to be regular if we can construct an FA for L*. FA for L* is given


in Fig. Ex. 3.7.4(a).

-s—
e

Fig. Ex. 3.7.4(a) : F/V for L *

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.

E x a m p l e 3.7.7 : If L is regular, prove LT is also regular.


S o l u t i o n : Let us assume L = L(M) for some FA = (Q,Z,8,q 0 ,F)

For simplicity, we can assume that F contains only one state q n.

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

3.8 Decision Properties of Regular Languages


MU > Dec. 2009, Dec 2010. May 2017

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

2. Whether a finite automata accepts a finite number of strings ? - Finiteness property.


3. Whether a finite automata accepts an infinite number of strings ? - Infiniteness property
In addition to above decision problems, we can formulate a number of other decisiOri
problems. Some of them are :
1. Given a regular expression R and a string co, does co belong to L(R) ?
2. Given two FAs M , and M 2 , is L(Mj) = L(M2 ) ?
3. Given two FAs M j and M 2 , is L(Mj) subset of L(M 2 ) ?
4. Given an FA M, is M a minimum state FA accepting L(M) ?

Decision Algorithm for emptiness :


• Finite automata will fail to accept any string if it does not have a final state.
• Finite automata will fail to accept a string if none of its accepting states is reachable
from the initial state.
• We can determine the emptiness of language accepted by an FA by calculating Q k, the
set of states that can be reached from q 0 by using strings of length k or less.
{qj ifk = O
. ( Qk - 1 u ( ft ( cb a ) ) I A <= Qk - 1 a n < a G X ) if k > 0
• We can go on computing the Q k for each k > 0 until one of the two cases arise :
1. Q k contains a final state.
The language is not empty.
2. Qk = Qk-i
The language is empty as the final states are not reachable from q 0 .

Decision algorithm for finiteness I infiniteness :


The set of strings accepted by a finite automata M with n states is finite if and only if the
finite automata accepts only strings of length less than n.
The set of strings accepted by a finite automata M with n states is infinite if and only if H
accepts some string co such that n < I co I < 2n.
From the pumping lemma we know :
1. If co with length of co > n is accepted by M then co can be written as xyz.
2. For every, i xy’z will be accepted by M.
We can always design an algorithm to generate all strings over Z with length between fl
and 2n. If any of these strings is accepted by M then L(M) is infinite else L(M) is finite.

Scanned by CamScanner
Theory of Comp. Sei. (MU-Sem. 5-Comp.) 3-73 Regular Expressions and Languages

3,9 Myhill - Nerode Theorem


MU - Dec. 2005, May 2006, Dec. 2006, M a y 2007,
■■May 2008, Dec. 2008, Dec. 2012, May_2013_
Given a language L, two strings x and y are said to be in the same class if for all possible
strings z either both xz and yz are in L or both are not.
The Myhill-Nerode theorem says :
1. A language L divides the set of all possible strings into mutually exclusive classes.

3. If the number of classes L creates is finite, then L is regular.


In finite automata, each state can be thought of as creating a class of strings. Two strings
are said to be in the same class if they both trace a path from starting state q 0 to some state qj
(say).
■ !
• Number of strings is infinite. ,
• Number of states in an FA is finite.
Many strings when applied to the FA will end up in the same state. Each state of FA can
stand for a class of strings.

3.10 Applications of RE ; -t •

M U - Dec. 2012, May 2014, May 2015, Dec. 2017

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.

3.10.2 Lexical Analysis :

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

3.1 1 University Questions and Answers ______

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.

R = (1(00) 1 + 01*0) (Example 3.3.10) (4 Marks)


(Note : Take ‘a’ as *0' and ‘b’ as ‘1’).

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. 5 Write R E for the following languages


(i) S = {0, 1} containing all possible combination of 0’s and 1’s but not having two
consecutive 0’s. (Example 3.4.15(b))
(ii) The set of all string over {0, 1} without length two. (Example 3.3.18(1)) (6 Marks)

Q. 6 Prove L = { (ab) n a k : n > k, k > 0 } is not regular. (Example 3.6.11) (5 Marks)

Q. 7 Write short note on Myhill-Nerode theorem. (Section 3.9) (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).

Q. 9 Write short notes on closure properties of regular language and Myhill-Nerode’s


theorem. (Sections 3.7 and 3.9) (10 Marks)

Q, 10 Design a NFA to accept (a + b)*aba convert it to a reduced DFA.


(Example 3.3.20(iri)> (10 Marks)

Q. 11 Use pumping lemma to show that the following is regular.


L = { x I x = 0 n 1 2m , n > 0, m > 0} (Example 3.6.13) (8 Marks)
(Note : Take ‘0’ as ‘a’ and ‘ V as ‘b’).

Q. 12 Write short note on Myhill-Nerode theorem. (Section 3.9) (5 Marks)

Q. 13 Show that 0(0 + 1)* + (0 + 1)*00(0 + 1) = (1*0)*(01*)*


(Similar to Example 3.3.3) (4 Marks)

Scanned by CamScanner
▼ ' Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-76 Regular Expressions and Langua y.

Q. 14 Write RE for following languages.


(I) L = {an b m : (n + m) is even}
(ii) L = {to e (a, b)‘ : (number of a’s in co) mod 3 = 0}
(Example 3.3.18(11), (III)) (5 Marks)

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

Q.16 Write regular expression for the following languages.


(i) L = {an b m I n >=4, m <= 3}
(ii) L = {co I Icol mod 3 = 0, cog {a, b}*} (Example 3.3.18(111), (lv)) (5 Marks)
n k
Q.17 Prove L = { (ab) a 1n > k, k >= 0 } is not regular. (Example 3.6.11) (5 Marks)
•* ■ •• ■ ■
Q.18 Write short note on Myhill-Nerode theorem. (Section 3.9) (5 Marks)

Dec. 2008

Q.19 Construct a NFA for the RE (01 * + 1) and convert it to DFA.


(Example 3.3.20(ii)) (10 Marks)

Q. 20 Write short note on Myhill-Nerode theorem. (Section 3.9) (5 Marks)

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. 24 Construct NFA from (0 + 1)*(00 + 1 1 ) and convert into minimized DFA.


(Example 3.3.20(1)) (1 0 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. 28 Construct reduced DFA for Language represented by


(ab/ba)* aa (ab/ba)* (Similar to Example 3.3.20) (1 0 Marks
)

Q. 29 State and Prove Pumping Lemma theorem for regular languages.


(Sections 3.6.1 and 3.6.3) (5 Marks)

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)

Q. 31 What is a regular expression? Give formal definition of a regular expression. Design a


DFA corresponding to the regular expression (a+b) * aba(a+b)*
' (Sections 3.1 , 3.3 and Example 3.3.21 ) (1 0 Marks)

Q. 32 State Arden's theorem and use it to construct a regular expression corresponding to


the following automata
'.:0

—•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. 36 Convert the following regular expression to NFA with e -transitions :


R = (1 (00)* 1 + 01* 0)* (Example 3.3.10) (Note : 0 as a and 1 as b) (5 Marks)

Q. 37 Give the statement of Pumping Lemma for regular Languages.


(Section 3.6.1) (2 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)

Q . 40 Write short note on application areas of R.E. (Section 3.10) (5 Marks)

Q. 41 Give the statement for pumping Lemma for regular languages.


(Section 3.6.1) (2 Marks)

Q. 42 Using pumping lemma, check whether a n b n is regular or not. (10 Marks)


(Example 3.6.1)

Q. 43 Write short note on : Myhill Nerode Theorem. (Section 3.9) (5 Marks)

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

DFA for strings with aba as substring.

b
Fig. Q. 44

Q. 45 Define the following term : Pumping lemma.


(Section 3.6) (3 Marks)

Q. 46 Give the closure properties of regular languages.


(Section 3.7) (5 Marks)

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. 49 Give applications of Regular Expression. (Section 3.10) (3 Marks)

Q. 50 Give formal definition of Regular expression. Give R.E. for following :


(i) Set of all strings over {1, 0} that end with 1 and has no substring 00.
(ii) Set of all strings over {1, 0} with even number of 1’s followed by odd number of
0’s. (Similar to Example 3.4.1 5(d)) (5 Marks}
Ans. :
(i) ( 1 + 0 1 ) * ( 1 + 01)

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)

Q. 52 Construct an NFA with epsilon transition for the following RE,


(00 + 11)* (10)*
(Example 3.3.22) (5 Marks)

Q. 53 State and explain any 5 closure properties of regular languages,


(Section 3.7) (5 Marks)

Scanned by CamScanner
Theory of Comp. Sci. (Mll-Sem. 5-Comp.) 3-80 R e g u l a r Expressions and Lanqi

Q . 54 Convert (0 + g ) ( 1 0 ) * ( g + 1 ) Into NFA w i t h g -moves a n d h e n c e o b t a i n a D F A .


( 1 0 Marks)

Ans. : [Hint : Refer Example as Example 3.3.8(A)]

Step 1 : RE to NFA for (0 + g ) ( 1 0 ) * ( g 4- 1)

(10)*

1 0
E 8

Fig. 1-Q. 54

(Note : States have been removed.)

Step 2: g -NFA to DFA

G -closure of states

Qo-> {Q0.q2.q3) . qi—*{qi)


q2-»(q2.q3 }. q3—>(qj}
The DFA using the direct method i s given below.

Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 3-81 Regular Expressionsand Languages

Fig. 2-Q. 54

Q. 55 Write short note on : Applications of regular expression. (Section 3.10) (5 Marks)

Q. 56 Convert (0 + e) (10)*(s + 1) into NFA with e-moves and obtain DFA


(Q. 2(b) of Dec. 2014) (10 Marks)

p . Dec. 2015 kj

Q. 57 Prove that Language L = {0n | n for n = 0, 1, 2, ....} is not regular.


(Example 3.6.8) 4. (5 Marks)

Q. 58 Write regular expression for the following languages.


(i) Language containing all the strings in which every pair of adjacent a’s appears
before any pair of adjacent b’s over the alphabet Z = {a, b}.
(ii) Language containing all the strings in which all possible combination of a’s and b’s
is present but strings does not have two consecutive a’s, over the alphabet
Z{a,b}. (5 Marks)
Ans. : [Hint : Refer Example as Example 3.4.15(e)]
(i) Regular expression for given language
{ { { a | b | a* | a+ b | ba+ } • (aabb) + { a+ b | a+ } ) * | { a | b | a* | a* b | b a+ } ‘ {aabb} + |
{ a | b | a* | a* b | ba+ } ) . ...

Q. 59 Show that language L - (0 1 11Is prime number} Is not regular.


(Example 3.6.8) (5 Marks)
KiWiaSI
Dec. 2016
■ ■■ .

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

Q. 61 Draw a state diagram and construct a regular expression corresponding to thQ


following state transition table. _______ (10 Marks)
State 0 1
->*q. qi q2
q2 q3 q2
q3 q< q 2
Ans. : [Hint : Please refer Example as Example 3.4.17 (A)]

State diagram
0
•0

R.E. form state diagram

Step 1 : Removing loop between q 2 and q 3 we get

Step 2 : Removing the main loop, we get

Q. 62 State and explain decision properties of regular languages.


(Section 3.8) (10 Marks)

Q. 63 Using Pumping Lemma prove that following language is not regular :


L {0m 1m + 1 1m > 0}. (Example 3.6.1) (10 Marks)

Q. 64 Construct NFA for given regular expressions : (10 Mark®!

(I) (a + b)*ab (il) aa(a + b)*b


(iii) (aba)(a + b)‘ (iv)(ab/ba)*|(aa/bb)‘

Scanned by CamScanner
▼ Theory of Comp. Sei, (MU-Sem. 5-Comp.) 3-83 Regular Expressions and Languages

Ans. : [Hint : Refer Example as Example 3.2.3(A)]


(i) (a + b)*ab : NFA

a,b
a

(ii) aa(a + b)*b : NFA

(iii) (aba) (a + b)* : NFA

▼ o-

(iv) (ab|ba)*|(aa|bb)* : NFA

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

Q. 67 Convert (0 + e) (10)*(e + 1 ) into NFA with e-moves and obtain DFA.


Ans. : [Hint : Refer Example as Example 3.2.4(A)]
Step 1 : NFA for the given expression :

Step 2 : 6-closure of states :


Qo ~ * ( Qo> Qi> Qa )
Qi -> { q t. q 3 1
q 2 -» { <h )
03 ( 03 )
Step 3 : DFA using direct method :

Q. 68 Write short note on Applications of Regular expression and Finite automata.


(Section 3.10) (5 Marks)

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

Right-linear form : , ■>


A right linear regular grammar will have production of the given form.
A -> a
Variable B in A -> aB is the second symbol on the right

Left-linear form :

A left linear regular grammar will have productions of the following form :

Variable B in A -> Ba is the first symbol on the right.


e

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 £

4,2 DFA to Right-Linear Regular Grammar

Every DFA can be described using a set of production using the following steps :
Let the DFA, M = (Q, £, 5, q 0 , F)

Let the corresponding right linear grammar


G = (V,T,P,S)
1. Rename q 0 e Q as S e V, relating start state of M with starting symbol of G.
2. Rename states of Q as A, B, C, D,

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 _

add a production B —> aC, where C is a non-accepting state.

Scanned by CamScanner
4-3 Regular Grammar (RG)

add two productions B -> aC, B -> a, where C is an accepting state.


Example 4.2.1 : Give a right linear grammar for the DFA shown in Fig. Ex. 4.2.1 .

AO Hl

—♦

Fig. Ex. 4.2.1

Solution :
Step 1 : Renaming of states, we get the DFA shown in Fig. Ex. 4.2. 1 (a).

IS! A)

Fig. Ex. 4.2.1(a)

Step 2 : Set of productions are given by :


f S aA I bB 1
J A —> aS I bC I b I
P =
| B —> bs I aC 1 a |
„ C -> aB I bA J

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

Fig. Ex. 4.2.2(a)

Step 2 : The set of productions are given below.


D r A —> e, \ - Start state is a final state
A—> 0 B I 0 - Transition from A to B on 0
B —> 1C - Transition from B to C on 1
C -» OB I 0 - Transition
Transiti from C to B on 0

where, Set of variables V = {A, B, C}


Set of terminals T = { 0 , 1 }
Start symbol = A.

4.2.1 Right Linear Grammar to DFA

Every right linear grammar can be represented using a DFA


• A production of the form
A —> aB
Will generate a transition (a) for the DFA
• A production of the form A —> aB I a will generate a transition (A>
provided every transition entering B terminates in B.
• A Production of the form A —> e will make A a final state.

Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-5 Regular Grammar (RG)

• An independent production of the form A —> b, will generate a transition

Where, F is a new state and it should be a final state.


Example 4.2.3 : Convert the following right-linear grammar to an equivalent DFA.
S-»bB
B->bC
B -> aB

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

Fig. Ex. 4.2.3(a)

Step 2 : Adding a state E to handle ( transitions, we get the final DFA

a.b

Fig. Ex. 4.2.3(b): Final DFA

Scanned by CamScanner
1

Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-6 Regular Gramm

Example 4.2.4 : Convert following RG to DFA


S—>0AI1B, A -> O C H A IO,
B - > 1B 1 1A 1 1, C -» 0 I OA.
Solution : A new final state F is being introduced to handle productions like,

Step 1 : Adding transitions corresponding to every production, we get the FA shown jn


Fig. Ex. 4.2.4(a).

Fig. Ex. 4.2.4(a)

Step 2 : Drawing an equivalent DFA, we get

Fig. Ex. 4.2.4(b)

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

Fig. Ex. 4.2.4(c) : Final DFA

Scanned by CamScanner
V Theory of CompjSci. (MU-Sem. 5 . Comp-) 4 .7
Regular Grammar (RG)

? equivalent DFA accepting the regular language defined by the


ear grammar given as : ,
< I bB,

May 2009
Solution :
A new final state F is being introduced to handle productions like,

Step 1 : Adding transitions corresponding to every production, we get the FA shown in


Fig* Ex. 4.2.5(a),

Fig. Ex. 4.2.5(a)

Step 2 : Drawing an equivalent DFA, we get :

Fig. Ex. 4.2.5(b)

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

Fig. Ex. 4.2.5(c)

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-8

4.3 DFA to Left-Linear Grammar

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

transitions, we get a transition graph as shown in Fig. Ex. 4.3.1(a).

b b b f b
a

Fig. Ex. 4.3.1(a)

Step 2 : Writing an equivalent left linear grammar, we get :

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

Fig. Ex. 4.3.2

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

Fig. Ex. 4.3.2(a)

Step 2 : Interchanging the starting state and- the final state and reversing direction of
transitions, we get • /

b
a
S
tS b

Fig. Ex. 43.2(b)

Scanned by CamScanner
y Theory of Comp. Scl. (MU-Sem. 5-Compj___4 J 0

Step 3 : Writing an equivalent left linear grammar we get :


S ->D
S -> A
S —>B
C —>Ba
B —> C b l A b
A —> Aa I Da I Ca I a
D —> B b l D b l b
Step 4 : Removing unit productions, the resulting productions are :
S -> Cb I Ab I Aa I Da I Ca I a I Bb I Db I b
. C — Ba •
B CblAb
A —> Aa I Da I Ca I a
D —> B a l D b l b
4.3.1 Left Linear Grammar to DFA

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

Fig. Ex. 4.3.3 : Transition graph

D is an accepting state, It is added for the production B —> b.

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

Fig. Ex. 4.3.3(a)


Step 3 : Conversion from FA to DFA
a
b

__ / a,b
------a dead state Is is
.---------------------added
added to handle
a
»b <|> - transitions

Fig. Ex. 4.33(b): DFA

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

Fig. Ex. 4.3.4(a)

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

Fig. Ex. 4.3.4(b)


1
. > .' . ... ' • y
Step 3 : Conversion from FA to DFA.

o
o
o
o

o,1
f
Fig. Ex. 4.3.4(c) : DFA

4.4 Right Linear Grammar to Left Linear Grammar

Fig. 4.44 : From right linear to left linear grammar

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

Fig. Ex. 4.4.1(a)

Step 2 : Interchanging the start state with the final state and receiving direction of transitions,
we get :

b
a
i

Fig. Ex. 4.4.1(b)

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’

Fig. Ex. 4.4.2(a) : Transition graph

Step 2 : Interchanging the start state with the final state and reversing direction of transitions,
we get . ... i ' / • .

o
o

o
6

Fig. Ex. 4.4.2(b)

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

Fig. Ex. 4.4.3(a) 1


* • ’ • I • * ; * ’ * . ‘ i ' ■ , ' *•

Step 2 : Interchanging the start state with the final state and reversing direction of transitions,

01

Fig. Ex. 4.4.3(b)

Step 3 : Writing of left linear grammar from the transition system, we get
S - > A l I 01
A -> A00 I 10

4.5 Left Linear Grammar to Right Linear Grammar

Fig. 4.5.1 : From left linear grammar to right linear grammar

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

2. Interchange the start state and the final state.


3. Reverse the direction of all transitions.
4. Write right - linear grammar from the transition graph.
Example 4.5.1 : Write an equivalent right linear grammar from the given left-linear grarr J?
S CO I AO I B1
A —> A1 I CO I B1 I 0
B->B1 I 1

Solution :
Step 1 : Transition system for the left - linear grammar is as shown in Fig. Ex. 4.5.1(a):
o

Fig. Ex. 45.1(a)

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

Fig. Ex. 4.5.3(a)

Step 2 : Interchanging the start state and the final state and changing direction of all
transitions, we get :

Fig. Ex. 4.5.3(b)

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

Fig. Ex. 4.5.5(a) ; Transition graph

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

Fig. Ex. 4.5.5(b)

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.

Fig. Ex. 4.5.5(c)

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

Fig. Ex. 4.5.6(a)

Step 2 : Right linear grammar can be written from the DFA.


A - > aB
B aC
C -» aC I bD
D —> bE
E bF I b
F _> bF I 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

1. Interchange start state and the final state


2. Reverse direction of transitions.

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

Fig. Ex. 4.5.7(a) : Transition graph for given RE

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

Fig. Ex. 4.5.7(b)

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

Fig. Ex. 4.5.8(a) : Transition graph for RE

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

Fig. Ek. 4.5.8(b)

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

Fig. Ex. 4.5.9(a)

Scanned by CamScanner
CamScanner
TheoryofComp. Sci. (MU-Sem. 5-Comp.) 4-23 Regular Grammar (RG)

Removing e — transitions, we get :


01 ,10

01,10
,10 01,1

oo
<Joooo
Fig. Ex. 45.9(b)

Writing of right linear grammar we get,


S->OOS I 11AI01B I 10B Ie
A—> 11AI01B I 10BI00S
B—>01BI 10BI H A
For writing of left linear grammar, we interchange the start state and the final state and
change direction of all transitions. The resulting transition system is given by :
01,10

oo

oo
Fig. Ex. 4.5.9(c)

Writing of left linear grammar we get,


S —» SOO I A00 I £
A A l l I B l l IS i l
B —> B01 I 10B I SOI I S10 I A01 I A10
Example 4.5.10 : Describe the language generated by the following grammar.
S -» bS I aA I e
A — aA I bB I b
B-»bS
Solution :
A language can be understood easily from a regular expression. Thus the above problem
should be solved as shown in Fig. Ex. 4.5.10 :

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 4-24

Interpretation of language

Fig. Ex. 4.5.10(a)

.Step 1 : We will first draw a transition diagram from the given regular grammar.

Fig. Ex. 4.5.10(b) : Transition graph


1
S is a final state, as S —> £
B is a final state, as A —> bB I b ; .
Step 2 : A regular expression can be written easily by moving the effect of loop
S->A-»B SonS. The resulting transition diagram is shown in
Fig. Ex. 4.5.10(c).

Fig. Ex. 4.5.10(c)

.'. R.E.= (a + aa*bb)*[E + aa*b]

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’

Fig. Ex. 4.5.11 : Transition graph for RE


. " I .

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

(2) S -> bTlaTle


T->aSlbS i -

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)

There are the following relations :


S—>01S I IOS 101 I 1 0 I 0 0 Q Q I l i p p
• Strings generated by S have equal number of 0’s and 1 ’s.
• P generates those strings, where number of O’s i s one more than number of Ts.
• Q generates those strings, where number of T s is one more than them number of O’s.

*■ < CFL associated with the given CFG

= { w g { 0 , 1 } * I w contain equal number of O’s and l ’ s ) ,

4.6 University Questions and Answers

Q.1 Construct right linear grammar and left linear grammar for the language (ba)*.

(Example 4.1.1) (1 0 Marks)

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

5.1 An Example to Explain Grammar


• ' '■ ■. ' ■' ■

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

5. Finite automata have a set of transitions; a grammar has a set of equations


(productions).
Table 5.1.1 : A set of DFA represented using an equivalent set of productions

■HM 1

sSFat ............................................

Equivalent
No. - " . ..... .. • ■
grammar
1. X —> a

2. X—>aX
cr
X-»b

3. © X —> aX
X—> e

The production X —> a, should read as X produces a or X derives a or X gives a.


The production X —> aX is a way of defining a string containing one or more a’s.
If X stands for a set of strings containing one or more a’s then a can be expressed in
terms of X. The concept is shown below in Fig. 5.1.1.
X
I-------------1 A string of 6 a’s can be generated by X
a a a a a a string of 5 a’s can be generated by X.
. X

Fig. 5.1.1 : Recursive definition of X —> aX

A string of 6 a’s = a followed by a string of 5 a’s.


-
I------- - - -1 I- -------—I
Or X = aX
Every recursive definition has a base case or termination case.
Let us consider the DFA given below in Fig. 5.1.2.

Fig. 5.1.2 : DFA taken as an example

The language of the DFA is given below,


L = (e, a, aa, aaa, )

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)

The empty string e can be generated by the production X—>e

A string “a” can be generated as shown in Fig. 5.1.3.

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

A string “aa” can be generated as shown in Fig. 5. 1,4

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

The production X —> 8, is for termination of recursion.


A non regular language can be expressed using a set of productions :

Let us consider a language

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.

1. A string 0, 1 e L [Base case or termination case]

2. a n b n can be written as a(a n " ’bn " *)b


If, a b is generated by X then a11 ~ 1 bn ~ 1 can also be generated by X.

X
I I
a a n--1 b r>-1 b

I |
X

Now, we can say that,


X —> aXb
i.e. If we have a string of die form a b , after removing the first a and the last b, the
string remains in the form a” b n.
Thus the language,
L = {anb n I n > 1 } can be represented using
1. X aXb ...(4-3)
2. X 4 ab ...(4-4)
The production X —> aXb can recursively generate a string a n b n and the second
production X ab is used for termination of recursion.
Let us try to generate a string a 3 b3 using the two productions (4-3) and (4-4).
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

Conclusion : Grammar can be used to represent both.


1. Regular languages 2. Non-regular languages

Scanned by CamScanner
CamScanner
eo ofComp SciJM Sem. 5-Comp.) 5-5 Context Free Grammars

5.2 Context Free Grammar _______________

A context free grammar G is a quadruple (V, T, P, S),


where,
V is a set of variables.
___ • ' I ’ ■

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

1. Terminals are denoted by lower case letters a, b, c . . . or digits 0, 1 , 2 . . . . etc.


2. Non- terminals (variables) are denoted by capital letters A, B, . . . , V, W, X . . .
3. A string of terminals or a word w e L is represented using u, v, w, x, y, z.
4. A sentential form is a string of terminals and variables and it is denoted by a, p, y etc.
* * ‘ f • • . . \ » i I * I .

CFG explained through an example :

Let us consider English sentences of the form :


1. Mohan eats. 2. Soham plays. 3. Ram reads.
The first word of in the above sentences is a noun and the second word is a verb. A
sentence of the above form can be written as
<sentence> —> <noun> <verb>
Here, noun can be replaced with Mohan, Soham or Ram and <verb> can be replaced
with reads, plays or eats. We can write
<noun> —» Mohan
<noun> —> Soham
<noun> —> Ram
<verb> —» eats
<verb> —> plays
<verb> —> reads *
In the above example :
1. <sentence>, <noun> and <verb> are variables or non-terminals.
2. Mohan, Soham, Ram, eats, plays and reads are terminals.
3. The variable <sentence> is the start symbol as a sentence will be formed using
start symbol <sentence>.

Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-6 Context Free Grammars (CFG)

Formally, the grammar can be written as :


G = (V, T, P, S), where
V = {<sentence>, <noun>, <verb>)
T = {Mohan, Soham, Ram, eats, plays, reads}
P = {<sentence> —> <noun> <verb>,
<noun> —> Mohan,
<noun> —> Soham,
<noun> -> Ram,
<verb> -» eats,
<verb> —> plays,
. j 1 • .■ ’ ■ \ ' *
<verb> -» reads
. . • }.
S = <sentence>
,f
>• /1 ‘ * / F' »* * 1
‘ * >• ' - *

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

5.2.2 The Language of a Grammar

Every grammar generates a language. A word of a language is generated by applying


productions a finite number of times. Derivation of a string should start from the start symbol
and the final string should consist of terminals.
If G is a grammar with start symbol S and set of terminals T, then the language of G is
the set
*
L (G) = w I w e T* and S => w ‘
G .

If the production rule is applied once, then we write a => p. When it is applied a number

of times then we write a => p


G

Derivations are represented either in the


•» > , .
1. Sentential form
■ , , »
OR ■ .
2. Parse tree form "«

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 5-7 Context Free Grammars

5.2.2.1 Sentential Form

Let us consider a grammar given below :


S —> A1B (Production 4-5)
A —> OA I e (Production 4-6)
B —> OB I I B I e (Production 4-7)
where, G is given by (V, T, P, S)
with, V = {S, A, B )
T = {0,1)
P = {Productions 4-5, 4-6 and 4-7}
S = Start symbol
Let us try to generate the string 00101 from the given grammar.
S —> A1B [Starting production]
—> 0A1B [Using the production A —> OA]
—> 00A1B [Using the production A —> OA]
—> 00 I B [Using the production A —> e]
—> 001 0B [Using the production B —> 0B]
—> 00 10 I B [Using the production B —> IB]
—>00101 [Using the production B —» e]
Thus the string 00101 e L(G).
In sentential form, derivation starts from the start symbol through a finite application of
productions.
A string a derived so far consists of terminals and non- terminals.
*
S => a I a e (V u T)*
G
• A final string consists of terminals.
• In left sentential form, leftmost symbol is picked up for expansion.
• In right sentential form, rightmost symbol is picked up for expansion.
• A string can be derived in many ways. But we restrict ourselves to :
1. Leftmost derivation.
2. Rightmost derivation.
In leftmost derivation the leftmost variable of a (sentential form) is picked f°f
expansion.
In rightmost derivation the rightmost variable of a (sentential form) is picked
expansion.

Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-8

Example 5.2.1 : For the grammar given below


S->A1B
A -> OA I e
B -> OB I 1B I e
Give leftmost and rightmost derivation of the string 1001 .
Solution :
(i) Leftmost derivation of 1001 (Leftmost variable is picked up for expansion.)
S —> A1B [Derivation starts from the start symbol]
—> I B [Using the production A —> e]
—> 10B [Using the production B —> OB]
—> 100B \ [Using the production B OB]
—> 100 I B [Using the production B —> IB]
—>1001 [Using the production B —> e]
(ii) Rightmost derivation of 1001 (Rightmost variable is picked up for expansion)
S —> A1B [Derivation starts from the start symbol]
—> A10B [Using the production B —> OB]
—> A100B [Using the production B OB]
—> A 100 I B [Using the production B IB]
—> A 1001 [Using the production B e]
—>1001 [Using the production A e]
5.2.2.2 Parse Tree
A set of derivations applied to generate a word can be represented using a tree. Such a
tree is known as a parse tree. A parse tree representation gives us a better understanding of :
1. Recursion 2. Grouping of symbols
A parse tree is constructed with the following condition :
1. Root of the tree is represented by start symbol.
2. Each interior mode is represented by a variable belonging to V.
3. Each leaf mode is represented by a terminal or e.
glj'jpg generated by a parse tree is seen from left to right.

Example 5.2.2 : For the grammar given below


S - > A1B
A -> OA I e
B—> OB I 1B I e
Give parse tree for leftmost and rightmost derivation of the string 1001 .

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

Fig. Ex. 5.2.2


Leftmost derivation of 1001 is shown by series of Figs. Ex. 5.2.2(a) to (f).
(ii) Parse tree for rightmost derivation of 1001 .
S

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.

Example 5.2.3 : For the grammar given below S —> OS1 I 0 1


Give derivation of 0001 1 1 .
Solution :
1. Derivation in sentential form
S —> 0S1 [Using production S —» 0S1]
S —> 00S11 [Using production S OS1]
S —> 000111 [Using production S —> 01]
2. Derivation using parse tree.
S •<— [Production S -> 0S1]

A
0 s 1 *— [Production S -> 0S1J

■*— [Production S -> 01]

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

Fig. Ex. 5.2.4(a)

S —> aAS — a S b A S — aabAS — aabbas —> aabbaa


(ii) Rightmost derivation :
Rightmost derivation o f aabbaa i s being s h o w n w i t h the h e l p of the parse tree.
s

/l\
s
a A
S —► aAS

A SbA b a
00

A -+ ba
aj
T

Fig. Ex. 5.2.4(b)


S —> a A S —> a A a —> a S b A a —> a S b b a a —> aabbaa

Example 5.2.5 : For the grammar given below


E —> E + T I T, T T * F I F, F -> (E) I a I b
Give the derivation of (a + b) * a + b.

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-12 Context Free Grammars (CFG)

Solution : For the grammar given in Example 5.2.5,


Set of variables V = {E, T, F)
Set of terminals X = {+,*, (,), a, b}
[ E—» E + T I T ,
Set of productions P = j T - » T * F I F , >
I F —> (E) I a I b ,
Start symbol = E
(i) Derivation in sentential form
E -4 E + T [Using production E —» E 4- T]
—> T + T- [Using production E —> T]
—» T * F + T [Using production T —> T * F]
—> F * F + T [Using production T —> F]
—» (E) * F + T [Using production F —> (E)]
-4 (E + T) * F + T [Using production E —» E + T]
(T + T) * F + T [Using production E —> T]
-» (F + F) * F + F [Using production T —4 F]
—4 (a 4- b) * a 4" b [Using production F —» a I b]
(ii) Derivation using parse tree :

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.

Example 5.2.6 Consider the grammar O


S- 0BI1A
A->0l0SI1AA
B-1I1SI0BB

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]

(iii) Parse tree

Fig. Ex. 5.2.6

sample 5.2.7 : Consider the following grammar


S iCtS I ICtSeS I a

For the String Ibtibtaea’ find the following :


(I) Leftmost derivation (ii) Rightmost derivation
(Iii) Parse Tree (iv) Check if the above grammar is Ambiguous
PU - May 20(1
Solution :

(I) Leftmost derivation : (II) Rightmost derivation :


S — iCtS [using S —> iCtS J S -> iCtS [using S —> iCtS]
—> ibtS —> iCtiCtSeS
—> ibtiCtSeS [using S —> iCtSeS] —> iCtiCtSea [using S -> a]

Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp ) 5-14 Context Free Grammars (CFG)

(i) Leftmost derivation : (il) Rightmost derivation :


—> ibtibtSeS [using C —> b] . -> iCteCtaea [using S -> a]
-> ibtibtaeS [using S -> a] -> iCtebtaea [using C -> b]
______-> ibtibtaea [using S a] -> ibtebtaea [using C -> b]
(iii) Parse T r e e :

i c t s

Fig. Ex. 5.2.7

5.2.3 Writing Grammar for a Language


M U - Dec. 2005; Dec. 2008

Productions for an infinite languageare written recursively. A production is called


recursive if its left side variable occurs on its right side.
For example :
(1) S —> aS is recursive
(2) S —> b I aA
A—> c I bS is indirectly recursive
as, S - aA—>abS [S gives A and A gives S]
A —> bS —> baA [A gives S and S gives A]
Thus a recursive grammar can be written either using recursive productions or using
indirectly recursive productions. Grammar for some basic languages is given below.
(1) Language L = { e, a, aa, . . . }
The productions required to generate the above language are :
S — aS
S ->e
The production, S —» £ is taken as E is a member of L.
The production, S aS, can generate one or more a’s.
Example : Generation of the string aaaa.
Sentential form :
$ > a g [Using production S —> aS]
> aa s [Using production S —> aS]
-> aaaS [Using production S -> aS]
aaaaS [Using production S -» aS]
-> aaaa [Using production S -> e]

Scanned by CamScanner
1

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

Parse tree : s

e
Fig. 5.2.1 : Parse tree for aaaa

(2) Language L = {a, aa, aaa, . . . )


The productions required to generate the above language are :
S —» aS
S —> a
The production S —> aS can generate one or more a s. The production, S —>a is for
termination of recursion.
Example : Generation of the string aaaa
Sentential form : Parse tree :

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)

aaaS [Using production S —> aS]


—> aaab [Using production S —> b]
parse tree :

a S

a S

a S

b
Fig. 5.2.3 : Parse tree for aaab

(4) Language , L = { w e {a, b}*}


The above language can also be written as
L = { £, a, b, aa, ab, ba, bb, . . . }
Thus any string that can be derived from the alphabet [a, b} belongs to L. This language
can also be generated by :
r\a,b

The DFA M =

or by the regular expression (a + b)*.


The productions required to generate the above language are :
S ->aS
S —> bS
S
The two productions S aS, S -> bS can generate any number of a’s or b’s with the
next character as either a or b. The production S -> e is for termination of recursion. Since e is
also a member of L, S E is taken for termination of recursion.
Example : Generation of the string abba
Sentential form :

S ->aS [Using production S —> aS]


—> abS [Using production S —> bS]
—> abbS [Using production S —> bS]
—> abbaS [Using production S -> aS]
—> abba [Using production S e]

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

Fig. 5.2.4 : Parse tree for abba

(5) Language L = {e, ab, aabb, . . ., a"b n )


The above language can also be described as a language of words in which even’ word
consists of any number of a’s followed by equal number of b’s.
The productions required to generate the above language are :
S — aSb
S —> E
The recursion involved in the above language is explained below.
! S
aaaabbbb
I _____I
S
Both the strings aV and a’b 3 are generated by S . a 4 b4 can be written as aa’b’b and thus
we have the recursive relation S -» aSb.
The production, S -> aSb can generate any number of a’s followed by equal number of
b’s. The production S -» e is for termination of recursion. Since e is a member of the give”
language, S —> E is taken for termination of recursion.
Example : Generation of the string aaabbb,
Sentential form :
S -» aSb [Using production S -> aSb]
-> aaSbb [Using production S -> aSb]
-> aaaSbbb [Using production S —> aSb]
—> aaabbb [Using production S —> e]

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

(6) Language L = {ab, aabb, . . . , a"bn }


The productions required to generate the above language are :
S —> aSb
S —» ab
The production, S —» aSb can generate a string of the form a n b n. The production S —» ab
is for termination of recursion.
Example : Generation of the string aaabbb
Sentential form :

S —> aSb [Using production S —> aSb]


—> aaSbb [Using production S aSb]
—> aaabbb [Using production S ab]
Parse tree :
s

a S b

a S b

a b

(7) Language L = { w e {a, b ) * Iw is a palindrome of odd length}


• String ‘a’ is palindrome of odd length.
• String ‘b’ is a palindrome of odd length

Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp .) 5-19 Context Free Grammars

String abababa is palindrome. The palindrome nature of abababa can


understood as given shown in Fig. 5.2.7.

abababa — a palindrome

babab — a palindrome derived


after removing first and
last a's

— a palindrome derived
after removing first and
last b's

b — a palindrome of odd length.


It is a termination
case for recursion

Thus a palindrome can be generated recursively using the two productions :


S —> aSa
S —> bSb
Since, we are considering odd length palindromes, S should terminate in either ‘a’ or ‘b’
and the productions are :
S —> a, S —y b
Thus the productions required to generate the above language are :
S —> aSa
S —> bSb
S —> a
S -»b
Or, in short, it can be written as
S aSalbSblalb
Example : Generation of the string abababa.
* t *•

Sentential form :

S —» aSa [Using production S -> aSa]


—> abSba [Using production S -> bSb]
—> abaSaba [Using production S -> aSa]
—> abababa [Using production S —> b]

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

(8) Language L = { w e {a, b}* I w is an even length palindrome with I w I > 0}


The productions required to generate the above language are :
S —> aSa
S —> bSb
S ->bb
S —> aa
The productions S -» aa, S -> bb are for termination of recursion. It may be noted that
smallest strings generated by the above grammar are aa and bb. The two productions S > aSa
and s > b s b can generate a palindrome of arbitrary length.
Example : Generation of the string ababbaba
Sentential form :
g > aSa [Using production S -» aSa]
abSba [Using production S -> bSb]
_> abaSaba [Using production S -» aSa]
_> ababbaba [Using production S -» bb]

Parse tree or derivation tree :


o

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

(9) Language L = { w G (a, b) * I w is a palindrome of either even length or odd length w


ith|
w I> 0)
The productions required to generate the above language arc :
S —> aSa I bSb I aa I bb I a I b
S —» aa or S —> bb are for termination of recursion for even length palindromes.
S —> a and S —» b arc for termination of recursion for odd length palindromes.
S —> aSa or S —> bSb can generate a palindrome of an arbitrary length.
E x a m p l e 5.2.7 : Let G be t h e grammar. Find the leftmost derivation, rightmost derivatiorT
and parse tree for the string 001 1 0 1 0 1.
G: S-»0BI1A
A —> 0 l 0 S I 1 A A
B —> 1 I 1 S I 0BB MU ~ Dec. 2010
Solution :
Parse tree f o r the string 00110101

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

5.2.3.2 Concatenation Rule for Grammar

If a language Lj is generated by a grammar with start symbol S t and L 2 is generated by a


grammar with start symbol S 2 then the concatenation (product) of the languages L] • L2 can be
generated with start symbol S, where
S ->SjS 2
Example : Let the languages L, and L 2 are given as below :
Lj = { a n I n > 0}
L 2 = {b n l n > 0 }
Productions for Lj are :
S j = aS I a *
Productions for L 2 are :
S 2 —> bS I b
Then the productions for L = Lj • L 2 can be written as :
S -> S A
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 £

Using the concatenation rule, we can write a set of productions for

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 :

A string of L has to meet the following requirements


• A finite number of a’s followed by a finite number of b’s.
• Number of b’s should be at least as many as number of a’s.
• Number of b’s should not be more than twice the number of a’s.
The set of productions for the above language are given by :
P = {
S —» aSb [Number of b’s = Number of a’s ]
S — > aSbb [Number of b’s = 2 x Number of a’s]
S —> ab I abb [Termination cases]
}
where, Set of variables V = {S}
Set of terminals T = { a, b }
Start symbol = S.
Example 5.2.11 : Give CFG for
L = {a l b’cq ; i + j = q; i, j > 1}
Solution :

A string of the form a‘bjc q with i + j = q can be written as


+j
aW
i j j i
a b c c

equal equal
number of number of
a’s and c’s b’s and c's

Fig. Ex. 5.2.11

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

where, Set of variables V = {S, X}


Set of terminals T = {a, b}
Start symbol = S .
Working of the above grammar can be understood with the help of the following
example.
Example : Generation of the string aabbbccccc

P a r s e tree :

a S c equal number of a’s and c’s

a S c ----

b X c

equal number of b’s and c’s


b X c

b c

Fig. Ex. 5.2.11(a) : Parse tree for aabbbccccc


Example 5.2.12 : Find context free grammars generating each of these languages

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

The string a' b J c k can be written as


11 i uk k
a b be

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]

A —> aAb 1 ab [equal number of a’s and b’s


B CB 1 c followed by some c’s]

C —> aC I a [Some a’s followed by equal


D —> b DC I be number of b’s and c’s]

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

Let us assume that (01 1 + 1)* is generated by the variable X .


.-. X —> 0 1 1 X I I X I e
Let us assume that (01)* is generated by the variable Y.
.-. Y -> 0 1 Y I e
Using the concatenation rule, (he set of productions for (01 1 + 1)* (01)* can be written
as :
P = { S-> XY
X —> 0 1 1 X l l X l e
Y->01Yle

)
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

The set of terminals T = { 0 , 1 )


The start symbol = S
(b) CFG for 0* 1 l + k 0 k 1 1, k >= 0
The language L can be written as :
L= {0l I 1 l k 0 k )

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

Example 5.2.14 : Show that the languages,


(1) L) = {a’b'c1 1 i.j >= 1} and
(2) L2 = {a'b’c’ I i j >= 1} are context free languages.
Solution : These languages can be shown to CFL by writing the required productions in each
case.
Productions for L 1 = {a'b'c1 1i, j >= 1} :

S —> XY
X —> aXblab [X generates a* bl and
Y
Y -> cY Ic generates c j ]

Productions for L* = {a't c1 I i, j >= 1} :

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)

Example 5.2.15 : Give CFG for L = {oW I j > I + k}


Solution :

A string of the form 0 1’0k with j > i + k can be written as


i i + k-hp n k . ,
0n 11 0 with P > 0
Qil
= | j Q j | i k o fc|
X Y Z
A string 0 1 l p 1 0 can be represented as concatenation of three strings.
1. OT generated by X.
2. l p with P > 0, generated by Y
k k
3. 10 generated by Z.
Thus, the set of productions for the above language are given by :
P = { S—» X Y Z
X —> 0X1 I 8
Y —> 1Y I 1
Z-> IZOIe
}
where, Set of variables V = {X, Y, Z )
Set of terminals T = {0, 1 }
Start symbol = S

Example 5.2.16 : Give CFG for matching parenthesis.


Solution :
• A production of the form
S ->(S)
can generate nested parenthesis.

• A production of the form


S ->SS
will allow parenthesis to grow sideways.
Nesting : ((()))

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

The production S -> I, terminates the recursion with 1 as a middle character,


where, Set of variables V = (S)
Set of terminals T = {0,1}
Start symbol = S.

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

Where, Set of variables V = {S}


Set of terminals T = {a, b, c, d}
Start symbol = S

Example 5.2.20 : Give CFG for L = {a m bnc pd q I m + n = p + q}


Solution :
There could be three possibilities on number of a’s and number of d’s.
Case I : m > q, a m b n c p dq can be written as
q rn-q n p ,q
a a bed
| ______I
X

Where, a m ' q bnc p is generated by the variable X (say).


I J 1+1 I J 1 I
a b c =a b c c
The string X is of the form U_i

Productions for X can be written as given below :


X —> aXc I Z
Z —> bZc I e
Case I I : m < q , a m b nc pd q can be written as

am ' > o p d q ~'"| d m Where, b"cpd‘‘ m


is generated by the variable Y (say).
Y

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-31 Context Free Grammars

The string Y is of the form bl+lo' d1 = b1 b1o1d1

Productions for Y can be written as given below :


Y —» bYd I Z
• Z —> bZc I £
m n p q
Case III : m = q, a b c d can be written as
m . n n ,m
abed

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 £

Where, Set of variables V = {S, X, Y, Z}


Set of terminals T = {a, b, c, d ]
Start symbol = S.
n m n
E x a m p l e 5.2.21 : Give CFG for L = {a b c I m, n > =1}

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 generates a string in which number of b’s = 1 + number of a’s.


We can write the set of production using indirect recursion by relating the three variables
S, A and B,
• The relation between S and B is given by S —> aB,
B represents a string in which number of b’s is one more than a’s. If we pretend an ‘a’ to
‘B’» both a’s and b’s will become equal.
• Similarly, the relation between S and A is given by
S —> bA
A is related to S by
A —> aS, removing an ‘a’ from a string represented by A will render both a’s and b’s
equal. '.
A is related to A by
A —> bAA, one b and two A’s on the right hand side will mean number of a’s one more
than number of b’s.
Similarly,
B —>bS
and B —> aBB
Thus the set of productions for the above language are given by :
P ={ S -> aB I bA
A -» aS I bAA I a
B -» bS I aBB I b
}
where, Set of variables V = {S, A, B}
Set of terminals T = {a, b)
Start symbol = S.
The behaviour of productions can be understood with the help of the following example
Example : Generation of the string aababb
s

b a

Fig. Ex. 5.2.22 ; Parse tree for aababb

Scanned by CamScanner
1
v Theoty of Comp. Sei. (MU-Sem. 5-Comp.) 5-33 Context Free Grai

Example 5.2.23 : Give CFG for strings in ab*

Solution : Let us say that b* is generated by variable B.


.’. B ->bB I e
The set of productions for the above language can be written using the rule
‘ f
0(
concatenations.
P ={ S —> aB
*
B -> bB I e , I

} .
Where, Set of variables V = {S, B}
Set of terminals T = {a,b).
Start symbol = S.

Example 5.2.24 : Give CFG for strings in a*b*.


Solution:
Let us assume that a* and b* are generated by variable A and B respectively.
A —>aA I e
B —>bB I 8
The set of productions for the above language can be written using the rule of
concatenation.
P ={ S —> AB ; . z

A -> aA 18 . i-
B -> bB I 8
} ■ ■ '■

Where, Set of variables V = {S, A, B)


Set of terminals T = {a, b}
Start symbol = S.
T
Example 5.2.25 : Give CFG for L = { wcw I w e {a, b}*}
Solution :

L is a language of odd length palindromes with middle character as c.


The set of productions for the above language is given by :
S -> aSa I bSb I c
Where,
Set of variables V = {S )
Set of terminals T = {a, b, c)
Start symbol = S.

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5 -34 Context Free Grammars (CFG)

Example 5.2.26 : Give CFG for L = {a n bm I n * m).


Solution . A string belonging to L can have either of the two forms given below :
1. aaV where j > 0
i I i
or 2. a’bJ b w h e r e j > 0
Let us assume that a is generation by A and b is generated by B.
The set of productions for the above language is given by :
P ={ S -> aSb I A I B
A —> aA I a
B —» bB I b
}
Where, Set of variables V = {S, A, B}
Set of terminals T = {a, b )
Start symbol = S.

Example 5.2.27 : Give CFG for the language L = {a n bmc k I n = m or m < k)


Solution : The language L is union of two languages Lj and-L2 .
where, Lj = { an b mc k I n = m} = {anb nc k }
L 2 = {an b mc k I m < k} = {an bmc m + p I p > 0 ) \ '
= { an b mc mc p I p > 0 )

The productions for languages Li = {anbnc k } are given as : *


S 1 = AB, where A generates a n bn and B generates c .
- • - A = aAb I e .. . . • • 1
O , > ' . \ . 4 ~ * • -

B ° = cB I e o

The productions for language {L2 = aVcV I p > 0 ) are given as


n
c XYB where X generates a and Y generates b c .
X->aX Ie
Y -> bYc I e

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.

Example 5.2.28 : Give CFG for the language L = {0 m1 n0 m * n I m, n > = 0}


Solution : The language L can be written as :

L=|0 1 0 f j 0 1 00 f

/ Y X

Let us assume that 0 m is generated by a variable X and l n0 n is generated by a variable Y


The set of productions for the given language is given by :
P = { S—>XYX
X —> OX I e
Y 1Y0 1 e
}
where, The set of variables V = {S, X, Y}
The set of terminals T = {0, 1 }
The start symbol = S .
Example 5.2.29 : Give CFG for the language
L = {0a 1 b 2 c I a - c = b}
Solution :
The string 0 a l b2 c with a - c = b
Can be written as 0 c + b1b2 c

The set of productions for the given language is :


P ={ S->0S2IX [X generates a string of the form 0 h l b ]
X~>0Xlle
}
Where, Set of variables V = (S, X )
Set of terminals T = {0, 1 }
Start symbol = S.

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

The set of productions for the given language is :


P ={ S —> aXa I bYb
X —» aXa I aXb I bXa I bXb I a
Y —> aYa I aYb I bYa I bYb I b
} .
Where, Set of variables V = {S, X, Y)
Set of terminals T = {a, b)
Start symbol = S.

Example 5.2.32 : Write a CFG for the following languages


L = {uacob I u, to are in {a, b)* and I u I = I co I }
Solution : A string generated by the above language could be shown as given below :

String of length n from (a, b)* a , String of length n from (a, b } ‘ b

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

Example 5.2.33 : Find CFG for generating

(iii) The set of all string over alphabet {a, b) with exactly twice as many a’s
as b’s.

MU - Dec. 2006, May 2009, Dec. 2009


Solution :
(i) String containing alternate sequence of 0 ’ s and 1’s, E = { 0 , 1 }
Since, any binary number will satisfy the condition of alternate sequence of 0’s and 1’s,
the language L = (0 + 1 ) *
The set of productions are :
S —> OS I I S I E
.-. CFG G = - ( { S } , { 0 , 1 }, { S b S I I S I G }, S)
(ii) The string containing no consecutive b ’ s but a ’ s can be consecutive.
The set of productions for the given language L are :
P = {
S —> aS I bX I b 1 e
X -> aSla

These production can easily be written from the FA for S


the above language. The FA i s shown i n Fig. Ex. 5 . 2 . 3 3 .

Fig. Ex. 5.2.33

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)

Set of terminals T = {a, b)


Start symbol = S
set
(iii) The °f all strings over alphabet (a, b) with exactly twice as many a’s as b’s.
The C F G G = (V, T, P, S )
Where V = {S} ’
T
= {a,b}
P
= (S aSaSbS I aSbSaS I bSaSaS I e}
S = Start symbol
(iv) Language having number of a’s greater than number of b’s.
The set of productions for the grammar are given by :
P = { . ' '
S —> SaS I aSS I SSa I a I aX I Xa
X —> aB I bA ' "■ 1 ’ •
A -> aX I bAA I a
B —> bX I aBB I b

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

Example 5.2.34 : Write CFG for the language


S = {a, b} number of a’s is a multiple of 3.
Solution : We can design a DFA for strings in which number of a’s is multiple of 3.

Fig. Ex. 5.2.34

The set of productions can be written from the DFA :


S -» bS I aX I e

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.35 : Consider the following grammar :


S aSa I bSb I aa I bb
Show that
1 . L(G) has no strings of odd length.
2. Any string in L(G) is of length 2n, n > 0
3. The number of strings of length 2n is 2 n.
Solution :
1. Each production from (S —> aSa I bSb I aa I bb) has two terminals on the right side.
Terminals are always generated in pair. Therefore, L(G) has no strings of odd length.
9 Smallest string generated by S is either aa or bb with length 2. The language of the given
grammar is a set of even length palindromes. Therefore, any string in L(G) is of length

3. Let us take a string of length 2n (say S 2n )


= a a
S 2n $2n-2 ' $2n-2

Thus, we can write a recurrence relation for S 2n


S 2n = 2 x S 2n 2 = 2 2 S 2n y = 2 n - 1 x S 2 = 2"

Example 5.2.36: Ol
'' < V; J ’C
MU - May 2010
Solution :
Productions for the required language are as follows.

CFG for the above language is ({ S }, {0, 1 }, P, S )

5.3 Ambiguous Grammar


MU, - M a y 2007, Dec. 20' 2009. Dec. 2J
2012. Dec. 201

A grammar is said to be ambiguous if the language generated by the grammar contai


some string that has two different parse trees.
Example : Let us consider the grammar given below :
...(Grammar 5.8)

A string a + b + a is generated by the given grammar.

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)

l-»alb . •••(Grammar 5.9)


A production of the form E -» E + T provides a binding. E + T, implies that E must be
evaluated first before an atomic T can be added to it. E can be broken down in E + T
but T cannot be broken further. This ensures higher precedence to left side + operator
over right side + operator.
Parse tree in Fig. 5.3.2 is based on unambiguous Grammar (5.9), for the string a + b + a

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)

An unambiguous grammar is given below :


E —» E + T I T
T —> T * F I F (Grammar 5.10)
F -> (E) I a I b

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

(a) Using ambiguous (b) Using ambiguous (c) Using unambiguous


grammar grammar grammar

Fig. Ex. 5.3.1 : Parse trees for a + b * 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

A nested if statement can have two interpretations.


Example 5.3.3 : An ambiguous grammar for if statement may have dangling else problem.
Solution :
An unambiguous grammar should attach the dangling else with inner if-statement.
An ambiguous grammar for if-statement is given below :

The above grammar can be written in short as :

Where, i stands for if,


t stands for then,
and e stands for else
rent
Example : Grammar (5.11) can be shown to be ambiguous by drawing two diffe
derivation trees for the string i tk taea. Two different derivations are shown in
Fig. Ex. 5.3.3(a), (b).

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

Fig. Ex. 5.3.3(c) : Derivation tree for iqticjtaea

* Outer if-statement is without else part.


Inner if-statement is with else part. _ .,
Example 5.3.4 : Show that the CFG given below. Which generates all strings of balanced
parentheses is ambiguous. Give an equivalent unambiguous grammar.
S -» SS I (S) I e

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

Example 5.3.6 : Is the following CFG ambiguous ?


S -> aB I ab
A -> aAB I a
B -> ABb I b
If so, show multiple derivation trees for the same string;
Solution :

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

Example 5.3.7 : Is the following CFG ambiguous ?


G = ({S, A}, {a, b}, P, S)
where, P consists of
S — aAS I a
A —> SbA I SS I ba

Solution : The above grammar can be proved to be an ambiguous grammar by derivin


string a7 in two different ways. Derivation of a i s shown in Fig. Ex. 5.3.7.
S

s
CO
C0
CO

s
0)

(a) (b)
Fig. Ex. 5.3.7 : Two derivations for a 7

Example 5.3.8 : Consider the grammar having productions :


S —> aS I £
S -> aSbS
The grammar Is ambiguous.
(i) Show in particular that the string aab has two parse trees.
(ii) Find an unambiguous grammar for the same.

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

MU - Dec. 2009, Dec. 2012, May 201 3 1


Solution :
(A) For string “aaabbabbba”

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

(ii) Right most derivation :


S—»aB—>aaBB—>aaBaBB—>aaBaBbS—»aaBaBbbA
- aaBaBbba —»aaBabbba—>aaaBBabbba->aaaBbabbba—>aaabbabbba
(iii) Parse tree :
s

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

(B) For string "bbaaabbaba"

(i) Leftmost derivation


S —> bA —> bbAA —> bbaA —> bbaaS
-> bbaaaB —> bbaaabs —> bbaaabbA
—> bbaaabbas —> bbaaabbabA —> bbaaabbaba

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

Mg. B.X. b.J.9 (c)

(C) For the string 00110101 ’

(i) Leftmost derivation


S —> OBB —> 00BB —> 001B —> 0011S ■
->00110 B >001101S->0011010B
->00110101
(ii) Rightmost derivation
S -> OB -> OOBB -> 00B1S -> 00B10B
-> 00B101S -> 00B1010B -> 00B10101
->001110101

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

Fig. Ex. 53.10(c)


Unambiguous grammar :
Given language is of the form
L= { a bj I i > j andi, j > 0 }
S —> aSb I X
X -»aXlE
Example 5.3.11 : Consider the grammar
G = {V = {E, F}, T = {a, b, -}, E, P}
where P consists of rules :
E —> F - E, F —» a, E —> E - F, F —> b, E F
(a) Show that G is ambiguous (b) Remove the ambiguity.
Solution : The production rules can be re-written as given below :
E —> F - E I E - F l F
F alb
(a) The grammar can be shown to be an ambiguous grammar by drawing two different
derivation trees for the string a — b.

F - E E - F

F b

b a i

Fig. Ex. 5.3.11 : Two derivations for a - b

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)

5.4 Simplification of 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

A symbol X e V (Set of variables) is a generating symbol if X => w,

where, w E T* i.e. every variable must generate a string of terminals.


Example : Grammar <■ - ‘ f . ....
S - Aa I Bbl a l b
A —» Aa I a . . . Grammar (4-14)
B ■“ — bB
Requires simplification as the symbol B is non-generating. Only production for B is
B —> bB
and it cannot generate a string of terminals. The Grammar (4-14) can be simplified by
deleting every production containing the useless symbol B. A simplified grammar is given
in (4-15).
S —> A a l a l b
A —> Aa I a ...Grammar (4-15)
A grammar containing a non-generating symbol Vj should be simplified by deleting
every production containing the non-generating symbol Vj.

Finding 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

2. If there is a production A —> oc and every symbol in cc is generating, then A is generating


Where, a e (V + T)*-
A symbol not in a set of generating symbols is said to be non-generating.
Example 5.4.1 : Find non-generating symbols in the grammar given below
S-» ABICA
B -» BCIAB
A —> a
C-> aBIb.
Solution :
Step 1 : Every terminal is generating
-------- Set of generating
symbols

Step 2 : A and C are generating as A —> a and C —> b.

ajbT) A -> a 0s Set of generating


S C -» b
symbols

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-

5.4.1 . 2 Non-reachable Symbols

A symbol X is reachable if it can be reached from the start symbol S.


i.e. if S a and a contains a variable X then X is reachable.
eting every

Finding non-reachable symbols :

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)

• We must draw a dependency graph for all productions.


• If there is no path from the start symbol S to a variable X, then X is non-reachable.
Example 5.4.3 : Eliminate non-reachable symbols from the given grammar.
/ P = {S —> aAa, A —> S b I bCC, C —> abb, E —> aC}
Solution :
Step 1 : Drawing of dependency graph :

A-»Sb
"" ~~~~ bCC ✓—■s. E —> aC
-------» — ■ QAj -------► yCy -------------kHz
-
S -> aAa .

Fig. Ex. 5.4.3 : Dependency Graph

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

Example 5.4.4 : Eliminate non-reachable symbols from the grammar :


1 . P = {S -> aBa I BC, A -> aC I BCC, C -» a, B bCC, D -» E, E —> d}
2. P = {S aAa, A -> bBB, B -» ab, C -> aB}
3. P = {S —> aS I AB, A -> bA, B -» AA}
Solution :

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)

Grammar Dependency graph Non-


reachable gram

______ ___________________________ symbols

2. S-»aAa S -> aAa A->bBB Symbol C S — aAa

A —>bBB
(s) ----->-----(a) ----♦----(b) is non- A - > bBB

B-»ab reachable B —>ab


C -> a B ‘
C — aB

©
3. S - » a S | A B Every Same as the
\ V

©
A->bA; symbol is original
(©)

grammar
0 /\

B-»AA reachable
//

5.4.2 Elimination of e-productions .


A productions of the form A —> e, is called a null production or e-production. For every
context free grammar G with e-productions, we can find a context-free grammar Gj having no
e-productions such that
L ( G1} = L ( G ) - { e }
The procedure for finding G t (equivalent grammar without E-productions) is as follows :
Step 1 : Find nullable variables
Step 2 : Addition of productions with nullable variables removed
Step 3 : Remove e-productions.
Step 1 : Find nullable variables : A symbol X is nullable if
1) There is a production of the form X —> e.
2) If there is a production of the form X -» a and all symbols of a are nullable.
Example : In the grammar with productions S —> ABA, A —> aA I e, B bB I e
A is nullable as there is a production A —» e
B is nullable as there is a production B —> e
S is nullable as there is a production S -> ABA
With both A and B as nullable.
/. The set of nullable symbols = {S, A, B}
Step 2 : Addition of productions with nullable variables removed.
An E-production has an effect. This effect must be added as productions to the existing
grammar before E-productions are removed.

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-59 Context Free Grammars (CFG)

Productions compensating the effect of e-productlons :


For each production of the form A —> ot h create all possible productions of the form
A —» where oc2 is obtained from a , by removing o n e or more occurrences of nullable
variables.
Example :

ramm

written

Productions derived
and nullable as
► e. S is null making one more
>ol on the symbols null.

Step 3 : Remove E-productions from the grammar obtained in step 2.


1. {S—> a S l a }
2. {S ABAI AB I AA I BA I A I B )
3 { s -> aS I a l AB I S -> A I S -> B}

Scanned by CamScanner
V Theory of Comp. Sei, (MU-Sem. 5-Comp.) 5-60 Context Free Grammars (CFG)

Example 5.4.5 : Remove E-productions from the g


S -> ABA, A —> aA I e, B -> bB I e
Solution :
Step 1 : Find nullable variables

S - > ABA

f S,A,B )

A and B are nullable as A —> £ and B —> e.


S is nullable as A and B are nullable and there is a production S —> ABA, with every
symbol on the right as nullable.
.-. Nullable set of symbols = {A, B, S}
Step 2 : Addition of productions with nullable variables removed.
r
S ABA
S-»AB

S —> ABA can be written as S AA

S->B

A —> aA can be written as '


B—>bB
B —» bB can be written as

f S -> ABA I AB I BA I AA I A I B I e '

B -» bB I b I £

Step 3 : Remove £ — productions


The final set of productions is given by
fS ABA I AB I BA I AA I A I B "
>
=
B —> bB I b

Example 5.4.6 : Remove E-productions from the given grammar.

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 and B are nullable as A —» E and B —> E.


S is nullable as A and B are nullable and S —> AB.
a Nullable set of symbols = {S, A, B}
Step 2 : Addition of production with nullable variable removed.

S —> AB can be written as By making one or more nullable symbols as null.

A aAA can be written as A —> aA r By making one or more nullable symbols as null.

' B -> bBB '


B —» bBB can be written as < B —> bB r By making one or more nullable symbols as null.

Step 3 : Remove 8-productions


/. Equivalent set of productions.

. 'S-»ABIAIB
p> A —> aAA I aA I a "
. B -> bBB I bB I b ,

5.4.3 Elimination of Unit Productions

A production of the form

It is known as a unit production. A and B are variables.


For every context free grammar G with unit productions, we can find a context free
grammar G j having no unit productions such that
L(G) = L(Gj)

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

Solution : Let Gi with productions Pi is an equivalent grammar without unit productions.


Step 1 : Add all non-unit productions of the given grammar to P b
S —> ABA I BA I AA I AB
Pi A —» aA I a r
B —» bB I b

Step 2 : Locate every pair of variables (Aj , Aj> such that Aj => Aj
■ G •

1. (S, A) [Due to unit production S A]


2. (S, B) [Due to unit production S —> B]
Step 3 : Unit production S —> A can be removed by expanding A.
A derives aA I a.
Unit production S -> B can be removed by expanding S —> B. B derives bB I b.
Set of final productions for G , are given below :

Productions

From step 1 S -> ABA 1 BA 1 AA 1 AB


A —> aA 1a

. 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

Example 5.4.8 : Eliminate unit productions from the grammar


P= { E E+T I T , T -> T*F I F,
F -> (E) 11
I -> a I b I la I lb 110 I II
} ‘ .
Solution :
Let the grammar G p with productions Pi is an equivalent grammar without unit
productions.
Step 1 : Add all non-unit productions of the given grammar to Pj
r
E E+T

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

1. (E.T) [Due to unit production E —» T]


2. cr.F) [Due to unit production T —> F]
3. (F, I) [Due to unit production F —> I]
4. (E, F) [Due to (E, T) and (T, F)]
5. (E.I) [Due to (E, T), (T, F) and (F, I)]
6. (T, I) [Due to (T, F) and (F, I)]
Step 3 : Unit productions are removed as shown below :
Productions
From step 1 E-»E+T
T->T*F
F—>(E)
I —> a 1 b 1 la 1 lb 110 1 I I

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

Grammar withe production (G)


____________ _______________
S—>ASB S -> ASB
S —» AB ] By making symbol S as null.
S -> e S-»e
A -> aAS A —> aAS
A -> a A ] By making symbol S as null_________
A—> a
B—>SbS B -» SbS
B -> Sb 1 By making one or more S null
B — bS |

B — bb
______B - » b b ________________

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-65

Final G] , after deletion of E-productions is given below :


' S -» ASB I AB
Gi = * A —> aAS I aA I a
„ B -> SbS I Sb I bS I b I A I bb ,

Step 2 : Eliminate unit productions from G j and obtain G 2 .


The grammar G! contains a unit production B —> A.

A derives aAS I aA I b

can be written as B -» aAS I aA I b

G 2 , after elimination of unit productions from Gj is given by :


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 I a

Step 3 : Eliminate useless symbols from G 2 and obtain G 3 .

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 .

Example 5.4.10 : Simplify the following grammar :


S -> 0A0 I 1B1 IBB

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

of nullable symbols are as shown in


Fig. Ex. 5.4.10:
B A, A i s nullable and hence B

$ B 0 , B Is nullable and hence S

Fig. Ex. 5.4.10

Scanned by CamScanner
CamScanner
jTheory of Comp. Sci. (MU-Sem. 5-Comp.) 5-66 Context Free Grammars (CFG)

The grammat after removal of null production is given below :


S —> 0A0 I 00 I I B 1 I 1 1 | B 1 B B
- Grammar G j
\a-»c,b->sia,c->s
Step 2 : Following u n i t productions are there in G P
S —> B , A —» C , B —> S , B -> A , C —> S
There i s a chain.

and S —> B —» S
• ’ i • * ' * ♦1

These chains are cyclic in nature of each is terminating in S .

The grammar without unit productions is given below :


" S —» 0A0 I 00 I 1 B 1 I 11 I B B 'I v -...
I A->0A01001 1B1 I 11 IBB [ ' - ' -
| B 0A0 I 00 I 1 B 1 I 1 1 I B B [
< C -> 0A0 I 00 I 1 B 1 I 1 1 I B B J .
Step 3 : The symbol C is not reachable and hence it can be deleted. The set of final
productions is given below ? — ...... -

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

Example 5.4.11 : Simplify the following grammar


S —>Ab
:
A -» a
B —> C I b •
C->D
D->E
E -> a 1 • *

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

1. (B, C) [Due to unit production B —> C]


2. (C, D) [Due to unit production C —» D]
3. (D, E) [Due to unit production D E]
4. (B,D) [Due to (B, C) and (C, D)]
5. (B, E) [Due to (B, C), (C, D) and (B, E)]
6. (C, E) [Due to (C, D) and (D, E)]
Step 2.3 : Unit productions are removed through expansion.
From Step 2.1 S —> Ab, A —> a, E —> a

Pair (B, C) B —> a [there is a chain B —> C —> D —» E — a]

Pair (C, D) C —a [there is a chain B —> C —> D —» E a]

Pair (D, E) D- a [there is a chain B—>C >D—>E—>a]

Pair (B, D) B —> a [there is a chain B C—> D E a]

Pair (B, E) B—a [there is a chain B—> C D—>E—>a]



Pair (C, E) C a [there is a chain B —> C —> D —> E a]
IS

p2 = s

Step 3 : Elimination of useless symbols :


• Every symbol, in the given grammar is generating.
• Reachable symbols can be located by drawing a dependency graph.
S->Ab

Fig. Ex. 5.4.11 : Dependency graph

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

Example 5 . 4 . 1 2 : Find a reduced grammar equivalent to


S -> aC I SB
A —> bSCa
B -> a S B I bBC
C —> aBC I ad
Solution :

Set of generating symbols can be found as shown in Fig. Ex. 5.4.12 :

C —> a d [a and d are generating and hence C]

S -> a C [a and C are generating and hence S]

A -> bSCa [symbols a,b,S,C are generating and hence A]

a,b,d,c,S,A

Fig. Ex. 5.4.12

/. Non generating symbols = {B}


Thus the productions after elimination of non-generating symbols is :
• S —».aC
Pi A bSCa *
C — ad

The set of reachable symbols can be found by drawing a dependency graph.


S —> aC /•<.
-------— — <c

A -> bSCa A -> bSCa

Fig. Ex. 5.4.12(a) : Dependency graph

CamScanner
Scanned by CamScanner
V Theory of Comp. Scl. (MU-Sem, 5-Comp.) 5-69 Context Free Grama
• »

The symbol {A} is non-reachable.


Thus the final set of productions after elimination of non-rcachable symbols is :
S —y aC
P2 =
I C -> ad '

5.5 Normal Forms for CFG

Productions in G, satisfying certain restrictions are said to be in normal form.


There are two normal forms for CFG.
1. Chomsky Normal Form (CNF)
2. Greibach Normal Form (GNF)
5.5.1 Chomsky Normal Form (CNF)
MU - Dec. 2009, Dec. 2010, May 2013, May 2014
A context free grammar (CFG) without 8-production is said to be in CNF if every
production is of the form :
1. A —> BC, where A, B, C G V.
2. A —> a, where A G V and a G T.
The grammar should have no useless symbols.
• Every CFG without E-productions can be converted into an equivalent CNF form.

5.5.1 .1 Algorithm for CFG to CNF Conversion

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 .

Terminal symbols a and b can be removed by rewriting the production


A -> V]V 2 aV3 bV 4 as
A -> V,V 2C a V3 C bV 4
And adding two productions
C a -> a
and C b -> b
3. Every production deriving 3 or more variables (A -» a with I a I > 3) can be broken
down into a cascade of productions with each deriving a string of two variables.

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.

The production A -> X j X 2 . . . X n should be broken down as given below :

A —> X j C |

cL x2c2

Cn-2 —> X n _ i X n
each with two variables on the right.

Example 5.5.1 : Find the C N F equivalent to S -> aAbB, A -» aA, B -> bB I b.


Solution :
Step 1 : Simplification of grammar.
The grammar is already in a simple form without
1. e-productions. / 2. Unit-productions. 3. Useless symbols.
Step 2 : Every symbol i n a, in a production of the form A —» a where I a I > 2 should be a
variable :
This can be done by adding two productions.
Ca ->a
and C b —> b - - .■
The set of productions after the above changes is :

P2 -b

!b —> b

Step 3 : Convert to CNF

1 Corresponding to S —> C a AC b B

■2 Q3

B
C a —> a

S —> C a C j , C j —>AC2 , C 2 —> C b B


A —>Ca A, B —> C b B I b, C a —> a
C b —> b . .

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

Step 3 : Convert to CNF :


Original production Equivalent productions in CNF

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 _____

The set of final productions in CNF is :


r
S -> PC 2 I PQ I QP I PP I C 0 P I 0 I CjQ I 1
C 2 —>QP
P -» C o P I 0
Q- qQH
C o —> 0
Iq-H J
- - - - ■ ■ ' ' ... . .

Example 5.5.3 : Check whether the given grammar is in CNF


S-bAiaB
A-»bAA|aSla
B -> aBB I bS I b
- If it is not in CNF, find the equivalent CNF. MU - Dec. 2010

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

Finding a n equivalent CNF :


Step 1 : Simplification of grammar - already simplified
Step 2 : 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.
C a —» a
and C b —> b
The set of productions after the above changes is
' S -» C b A I C a B
A -> C b AA I C a S I a
B -> C bB B I C bS I b
< C a —> a , C b —> b

Scanned by CamScanner
CamScanner
5-73

Step 3 :

S —> C b A 1C aB S —> CbA 1 C a B


A -» CbAA A —> CbC,
C , - » AA
A —> CaS 1a A —> CnS 1a
B -> CbBB B —> C bC2
C2 — BB
B —> CbS 1 b B —> CbS 1 b
Ca —> a Ca - > a

cb b _________ c b —> b

The set of final productions in CNF is :

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

where the set of productions

P = S->SS

An equivalent set of productions without e-productions can be written as

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)

Step 2 : Finding an equivalent CNF :


Production in CFG Equivalent productions in CNF
s -> c ( sc 2 S C|C2
c, -> sc 2
s -> qCj S —> CjC2
S->SS s-»ss
C ( C,->(
C2 —> ) C2 ->)
The set of final productions in CNF is
r
S -» CjC3 1CjC2 I ss"
* S SC
< 2.
C!->(
< c 2 —> )
Example 5.5.5 : Convert the following grammar to CNF.
S —>Aba, S —>aab, B—->Ac
Solution :
Step 1 : Symbol B is non-reachable. The symbol can be deleted. The grammar after
simplification is :
S—»Aba I aab • '
Step 2 : Every symbol is a, in a production of the from A —>a with lai > 2 should be a
variable :
This can be done by adding two productions
Ca —> a 1
and Cb —> b
The set of productions after above changes is :
S -» AC bC a I CaCaCb ,J
Step 3 : Convert to CNF
S —> ACj
Cl -> C bc a
s -» c ac 2
. c 2 -» C ac b

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 5-75 Context Free Grammars (CFpj

Solution : First of all, the grammar must be simplified.


Step 1 : Removing null productions.
Nullable set = {A}
Null productions are removed with the resulting set of production as :
S -» AACDIACDICD
A —» aAblab
C -> aCla
. A —> aDalbDb
Step 2 : Removing non-generating symbol
Symbol S and D are non-generating.
Since, the starting symbol itself is non-
it is an invalid grammar. Fig. Ex. 5.5.6

B SbS I A I bb MU - May 2006, May 2009, May 2010, Dec. 2011


Solution :
Step 1 : Simplification of grammar
Symbol S is nullable.
After removing e-productions, the set of productions is given by
S —> ASB I AB
A —> AaS I Aa I a
B —> SbS I Sb I bS I b I A I bb .
Unit production B —> A is removed, the resulting set of productions is given by
S —> ASB I AB
A —> AaS I Aa I a
B —> SbS I Sb I bS I b I AaS I Aa I a I bb
Step 2 : Every symbol in a, in productions of the form A —>a where lai > 2 should be a
variable.
This can be done by adding two productions :
C aa —> a
and C b —> b
The set of productions after the above changes is
S -> ASB I AB
A -» AC aS I A C a l a
B -> SC b S I SC b l C bS I b I AC aS I AC„I a I C bC b
C a -> a
C b —> b

Scanned by CamScanner
CamScanner
__Thgo t omp Sci (MU-SeiTi. 5-Comp.) 5-76

Step 3 : Finding an equivalent CNF


— — Q-'K' 113 1 production Equivalent productions in CNF _
S —> ASB S —> ACj
C , —> SB
S - » AB S —> AB '
A —> AC aS A —> AC2
“> c as
A —> AC a A —> AC a
A —> a A —> a
B -> S C b S b-»sc3
c 3 -> c bs
B—> S Cb I C b S l b S—» S C b I C bS l b
B -» AC aaS • B —> ACj
B —> AC a 1 a 1 C bC b B - > A C a | a | C bC b
C a —» a Ca a
c b '->b Cb b

Example 5.5.8 : Convert the given grammar to CNF


S aSBlaA
A -» AalSala
Solution :
Step 1 : Every symbol in a, in production of the form A —> a where loci > 2 should be a
variable. •
This can be done by adding the production
C a -> a
is :

A —> ACJSCJa

Step 2 :
1 0

■I

■ Z=


■'a.

B
1
■■ _

5“
... !
i

S —> C aSB S -» CjC,


C , ->SB

S —> C a A S - > C aA
A-> ACJSCJa A —> ACa 1SCa 1 a

C a —» a __________ _______ C a —> 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

S -> BA I AB I AA I C a A I C b A I a S -> BA I AB I AA I C a A I CbA I a t b


I b I C b B I C aA I C bB I C aA
A —> C a A I C b A I a I b A —> C a A I C b A I a I b

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

5.5.2 Greibach Normal Form (GNF)


MU - Dec. 2005JVlay 2007, Dec. 2008, Dec. 2009.
Mi— MMiyjav2013. May 2014. May 20J7.

A context free grammar G = (V, T, P, S) is said to be in GNF if every production is °


the form :
A -> aa,
Where, a g T is a terminal and a is a string of zero or more variables.

Scanned by CamScanner
CamScanner
Theory of Comp. Sci (MU-Sem. 5-Comp.) 5-80 Context Free GrammarsjCFG)

The language L (G) should be without e.


• Right hand side of each production should start with a terminal followed by a string of
non-terminals of length zero or more.
5,5.2. 1 Removing Left Recursion

Elimination of left recursion is an important step in algorithm used in conversion of a


CFG into GNF form.
Left recursive grammar :

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 :

Let us consider a CFG containing productions of the form


A —> Aa . . . [Left recursive]
A —> P . . . [For termination of recursion]
The language generated by above production is :
A —> Aa [From production A —> Aa]
—> Aaa [From production A —> Aa]
Aaaa [From production A —> Aa]

—> Aa n [From production A —> Aa]


n
—> Pa [From production A —» P]

Right recursive grammar for Pan :

A right recursive grammar for pa n can be written as :


A —> pB I P [where B generates a string a n, production A —» P is
for termination of recursion]
B —> aB I a
Thus a left recursive grammar
A —> Aa I P
can be written using a right recursive grammar as ;
A -> PB Ip Right recursive grammar
B — aB la
Example : A number of examples are given below for removing left-recursion.

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

5.5.2.2 Algorithm for Conversion from CFG to GNF

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.

(a) A —> ot (b) A — a


(c) A —> aa

Where, *a* is a terminal and a is a string of non-terminals.


3. Rename variables as A l t A 2 , A 3 . . . A n to create A-productions.
Example : Consider a grammar given below

*• fS -» aXSY I YSX I b ♦ • •

Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (Mll-Sem. 5-Comp.) 5-82 Context Free Grammars (CFG}

The variables S, X and Y can be renamed as A b A 2 and A 3 respectively. Then the


productions become
A j —> aA 2 AjA 3 1 A3AjA2 I b [A-productions]
.4 Modify the productions to ensure that if there is a production A, > Aj ot then i shoul
be < j. If there is a production Aj —> AjCC with i > j. then we must generate productions
substituting for Aj.
5, Repeating step 4, several times will guarantee that for every production A, —> Aja, i - J-
6, Remove left recursion from every production of the form A k —> A kct B-productions
should be added to remove left recursion.
7, Modify Aj-productions to the form A, —> act, where a is a terminal and a is a string of
non-terminals.
8. Modify B-productions to the form Bj —» aa, where a is a terminal and a is a string of
non-terminals.

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

The resulting set of productions is :


Aj —> A 2 A2 1a
A 2 —> A2 A 2A ] I aAj I b
Step 3 : Removing left recursion :
The A 2 - productions Ao —> A2A2AjI aA t I b contains left recursion. Left recursion from
A 2-production can be removed through introduction of Reproduction.
A2 aAjRn I bB 2
B 2 —> A2AjB2 I A2Aj
The resulting set of productions is :
A i - > A 2A2 1a
A 2 -» aAjB2 I aB2 I aAj I b
B2 > A 2A 1 B2 1 A2Aj
Step 4 : A 2 - productions are in GNF.
Ai and B2 productions can be converted to GNF with the help of A 2-productions.
A 2 —» aAjB2 1 bB2 1 aAj I b . . . in GNF
Ai —> A 2 A 2
U Substitute aA t B2 1 bB 2 I aAj I b for first A 2
A j — aAjB2 A2 1 bB2 A 2 I aA A2 I bA2
Aj—> a . . . in GNF
Now, for B2 - Production
B 2 —> A 2 A t B 2
V Substitute aAjB2 1 bB2 I aA 1 1 b for the first A 2
B 2 —> aAjB 2 A|B 2 1 bB 2 AjB 2 1 aAiAjB2 1 bAjB2 [
B 2 —* A 2 A]
U Substitute a Ai B 2 | b B 2 1 a A! I b for the first A 2
B 2 -> a A|B;A, I bB2 A, I aA, A, I bAj

The final set of productions is :


A 2 —► aAjB 2 1 bB 2 1 aAj I b
' A! -> aAjB2 A2 1 bB 2 A2 | aA t A 2 1 bA 2 1 a
B2 -» aA 1B2 A I B 2 1 bB2 AiB2 I aA 1 A 1 B2 1 bA I
a AiB2AjI bB2 A | I aAi Aj I bAi

Scanned by CamScanner
CamScanner
Theory of Comp, Sci. (MU-Sem. 5-Comp.) 5-84 Context Free Grammars (CFG)

where, Set of variables V = (A h A2, B2)


Set of terminals T = (a, b)
Start symbol = Aj
Set of productions P = Given above. -
Example 5.5.1 2 : Find a grammar in GNF for the given 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.

We can make the following substitutions : A for +, B for * and C for )


The resulting set of productions after the above substitutions is :
E -> EAT I TBF I (EC I a
T -» TBF I (EC I a
F —> (EC I a
A -> +, B -> *, C -») ,

Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-85 Context Freg Grammars (CFq-,

Step 3 : Renaming of variables :


The variables E, A, T, B, F, C are renamed as A h A 2 , A 3 , A 4 , A 5 , A 6 .
The resultant set of productions after the above substitutions is :

Ai —» AjA2 A3 I A 3 A4 A5 1 (AjA 6 I a
A3 —> A3 A4 A5 | (AjA 6 I a

A5 —> (AjA 6 I a, A 2 —> +, A 4 —> *, A 6 —> )


Step 4 : Every productions of the form Af Aj oc with i > j must be modified to make i < j.
This step is not required as the productions are in the required form.
Step 5 : Remove left recursion.
A t production and A3 production have left recursion. . ,
• Left recursion from A j-production can be removed through introduction of
B j - production. ’ -

Ai —> A3 A4 A5 Bj I (AiA6 Bj | aBj


Bi A 2 A 3B j | A 2A 3
• Left recursion from A 3 -production can be removed through introduction of
B3 - production - .<
:
A 3 —> (A 1A 6B3 1 aB3 ; .
B3 —> A4A5 B3 1 A4 A5

The resulting set of productions is :


Aj —> A3 A 4A 5 Bj I (A Bj I aBj | A3 A 4A5 1 (A!A 6 1 a
Bj —> A2 A3 Bj I A 2 A 3
A3 —> (AjA6 B3 I aB3 1 (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 . ■

______ Substituting (A! A6 B3 1 a B3 I (A, A6 I a for first A 3


Ai -> (A,A 6B3 A4A5 B, I aB3 A4 As B, I (A B, I aA4 A5 B t
A
i -> (A,A6 B! I aBj ...in GNF
A-i > A3 A4 A5 • •

II Substituting (A 1A6 B3 I aB3 1(A I a for the first A3


Ai -> (A t A6 B3 A4A 5 I aB 3 A4 A5 1(A t A6 A4 A5 1aA A; |
Ai -» (Ai A « l a . . . in GNF
B A
! 2A 3B 1 IA 2A 3

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

The final set of productions is :


A2 -» +
A3 —> (A1A6 B3 1aB3 | (A|A 6 1 a
A4 -> *

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 .

E x a m p l e 5 . 5 . 1 3 : Give the G N F for following CFG


S —» AB
A BS I b
B- SAIa ■
Solution :

Step 1 : Renaming of variables :


The variables S , A and B are renamed as A I ( A 2 and A 3 respectively.
The resultant set of productions after above substitutions is :
A j —> A 2 A 3

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 1 — production can be converted to GNF with the help of A 2 — productions .


1
A j —> A 2 A 3 • ■ ■
Substituting bA 3 A 2 B 3 A 1 1 aB 3 A j I bA 3 A 2 A 1 I aAi I b for A 2

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

B 3 - productions can be converted to GNF with the help of A j - productions.


B 3 — A A 3 A 2 B 3 1 AjA3A2
___________U Substituting bA 3 A 2 B 3 A j A 3 1 aB I bA 3 A 2 A t A 3 1 aA,A 3 1 bA 3 for A ]
B 3 —> bA 3 A 2 B 3 A 1 A 3 A 3 A 2 B 3 l aB 3 A | A 3 A 3 A 2 B 3 1 bA 3 A 2 A ] A3 A 3 A 2 B 3 1______________i
aA 1 A 3 A 3 A 2 B 3 1 bA 3 A 3 A 2 B 3
B 3 —> bA 3 A 2 B 3 A 1 A 3 A 3 A 2 l aB 3 A]A 3 A 3 A 2 1 bA 3 A 2 A 1 A 3 A 3 A 2 I aAjA 3 A 3 A 2 1 bA 3 A 3 A 2

The set of final productions in GNF is :


A j —> bA 3 A 2 B 3 A j A3 1 aB 3 A 1 A 3 1 bA 3 A 2 A , A 3 1 aAjA 3 1 bA 3
A 2 -» bA 3 A 2 B 3 A j I aB 3 A j I bA 3 A 2 A t I aAJ b
A 3 — bA 3 A 2 B 3 1 aB 3 1 bA 3 A 2 1 a
B 3 — bA 3 A 2 B 3 A i A 3 A 3 A 2 B 3 '
I aB 3 A [ A3 A 3 A 2 B 3
I bA 3 A 2 A 1 A 3 A 3 A 2 B 3
. I aAjA 3 A 3 A 2 B 3
I bA 3 A 3 A 2 B 3
I bA 3 A 2 B 3 A j A3 A 3 A 2
I aB 3 A | A 3 A 3 A 2
I bA 3 A 2 A 1 A 3 A 3 A 2
I a A j A3 A 3 A 2
I bA 3 A 3 A 2

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

A3 - > a A j A4 | a ► A3 —> a A-j A4 | a


a 2 -> a 3 a 1 a 3 ...

u
Substituting A3 > A2 -> a A! A 4 A1 A3 1 aA •

A 2 -> A 3 A 3

Substituting A 3 > A2 -> aA1 A 4 A3 1a A3

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

The final set of productions is :


Ai —> aAiA4 AiA3 A3 1aA t A 3 A3 1 aAjA4 A3 A3 1aA 3 A 3 1 bA3

Scanned by CamScanner
y Theory of Comp, Sei, (MU-Sem. 5-Comp.) 5-90 Context Free Grammars (CFG)

A 2 -> aA 1 A 4 A l A3 I aA,A3 1aAjA4 A 3 1aA 3 1 b


A 3 —> aAjA4 I a
A 4 —> b.

Example 5.5.15 : Convert the following grammar to Greibach Normal Form (GNF),

S ~* BS, S -* Aa, A -> be, B -> Ac


Solution :

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

JJ Substituting bA5 for A2


" A 3 -> bA5 A5 ||

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)

.’. The final set of productions is given by


A 1 —> bA5 A 5 Aj lbA5 A 4
A2 —> bA
A3 -> bA5 A5
A 4 —> a
A5 —> c

Example 5.5.16 : Find GNF of the grammar given below


S -> ABAb I ab
B -> ABA I a
A -> a I b
Solution :

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

Example 5.5.17 : Find the GNF equivalent to the CFG


S - » AB
A -> aA I bB I b

Solution :

A-productions A —> aA I bB I b are in GNF


B-production B -» b is in GNF . . j
S-production S -> AB is not in GNF.

Scanned by CamScanner
CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 5-92 Context Free GrammarsjCFG)

It can be brought into GNF through substitution.


S - > AB
U ■ ■ '■

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

. Example 5.5.18 : Find a GNF grammar equivalent to the following CFG.


S ->BA I ab
B -» AB I a
A -> Bb I BB
Solution :
Step 1 : Bring every production to the form A —> aa or A —» a, where a is a string of non-
terminals. '.'
We can make the substitution D for b in the production A —» Bb and S —> ab
S -» BA I aD
B —> AB I a
A BD I BB
D —> b • • :
Step 2 : Remaining of variables.
The variables S,B,A and D are renamed as A! , A2 , A 3 and A 4 respectively.
A| A 2 A 3 1 aA 4
A 2 — A 3 A2 I a
A 3 —> A 2 A4 1 A 2 A 2
A 4 -» b
Step 3 : Every production of the form A, —> Aj a with i > j must be modified to make i < j.
A 1 -> A 2 A 3 | a A 4
A2 A 3 A 2 1a
A 3 -> A 2 A 4 1 A 2 A 2 - not In the required form

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

Step 4 : Removing left recursion.


A 3 production has left recursion. Left recursion can be removed through introduction of
B 3 -production.
A3 aA 4 B 3 laA 2 B 3 laA 4 laA 2

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

A3 aA 4 B 3 laA 2 B 3 laA 4 laA 2

B 3 —> A 2 A 4 B 3 IA2 A 2 B 3 IA2 A 4 IA2 A 2


Step 5 : Productions for A 3 are already in GNF. Productions for A I; A 2 and B 3 can be
converted to GNF.

A 3 —> aA 4 B 3 laA 2 B 3 laA 4 laA 2

A2 A 3 A 2 la

U Substituting a A4B 3 laA 2 B 3 laA 4 laA2 for A 3


•j \ ■■ ■- ■ >

A 2 —> aA 4 B 3 A 2 l aA 2 B 3 A 2 laA 4 A 2 laA 2 A 2 la

Aj —> A 2 A 3 laA 4

■U Substituting A 2 with its productions

Aj aA 4 B 3 A 2 A 3 laA 2 B 3 A 2 A 3 l aA 4 A 2 A 3 laA 2 A 2 A 3 laA 3 laA 4

Substituting A2 with Its productions

B 3 —> aA 4 B 3 A 2 A 4 B 3 laA 2 B 2 A 2 A 4 B 3 laA 4 A 2 A 4 B 3 laA 2 A 2 A 4 B 3 laA 4 B 3 l

aA 4 B 3 A 2 A 2 B 3 laA 2 B 2 A 2 A 2 B 3 laA 4 A2A 2 B 3 laA 2 A2B 3 laA 2 B 3

laA 4 B 3 A 2 A 4 laA2B 2 A2A 4 laA 4 A 2 A 4 laA2A2A 4 laA 4

laA 4 B 3 A2 A2Ia A2B 2 A2A2Ia A 4 A 2 A2IaA2 A2A 2laA2

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

Example 5.5.20 : Convert the following gramm

BHMl Ma OT6 a 012


Solution :
Simplification of grammar /•
The unit production x -> y is removed, the equivalent set of productions is given by :
S - XYIIO
X ->00X11X1
Y -> 1X1
The symbol X is non-generating.
The set of productions after elimination of X is given by :
S —> 0, it is in GNF
r.
-i... MU - Dec. 2005
Solution :
' The grammar can be brought to GNF through simple substitutions C a -> a and C b -» b.
S —> aSC a I bSC b I C
C a -> a
C b —> b

Scanned by CamScanner
CamScanner
M U - M a y 201 1
Solution :

Step 1 : Renaming o f variables by substituting A j for S and A 2 for A .

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

The resulting set of productions i s :

Ai —> A 2 A 2 I 1

A 2 —> A 2 A 2 AjI 1 AjI 1


Step 3 : Removing left recursion :
The A 2 — production contains left recursion. Left recursion can be removed through
introduction of B 2 - production.

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.

A 2 —> 1 AjB 2 1 1 B2 11 AjI 1


A j -> 1 A j B 2 A 2 1 1 B 2 A 2 1 1 A j A 2 1 1 A 2 1 1

B 2 -> 1 A j B2 AjB 2 1 1 B 2 AjB 2 I 1 A| Aj B 2

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

S Pumping Lemma for CFG ______


U - Dec. 2008, Dec. 2015, May 201
Let G be a context free grammar. Then there exists a constant n such that any string
wG L (G) with I w I > n can be rewritten as w = uvxyz, subject to the following conditions .
1. I vxy I < n, the middle portion is less than n.
2. vy e, strings v and y will be pumped.
3. For all i > 0, uv y’z is in L. The two strings v and y can be pumped zero or more times.
Proof :
Let us assume that the grammar
G is given by (V, T, P, S).
0(G) denotes that largest number of symbols on the right-hand side of a production in P.
In pumping lemma, it is a requirement that the constant n should satisfy the following
condition :
n > 0(G)1V " T I
Let us take a string w e L (G), such that I w I > n. Let us construct a parse tree T with
root as S. The parse tree T generates w with smallest number of leaves.
The tree T will have a path length of at least I V - T I + 1. This path will have
I V - T I + 2 nodes with the last node labelled as terminal and remaining non-terminals.
Fig. 5.6.1 shows paths in detail.
s

2,

u x y— — z

Fig. 5.6.1 : Paths in the parse tree

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

Example 5.6.1 ; Prove that the language '

L = {a p I p is a prime) is not context free language. . ;


tanssEEanjgg
Solution :
1. Let us assume that L is a CFL.
2. Let n be the natural number for L, as per the pumping lemma.
3. Let p be a prime number greater than n. Then z = a pG L. We can write z — uvxyz.
4. By pumping lemma uv°xy°z = uxz g L. Therefore,
I uxz I is a prime number.
Let us assume that I uxz I = q.
Now, let us consider a string uvq xyq z,
The length of uvq xyq z is given by :
I uv q xyq z I = q + q (I v I + I y I), which is not a prime with q is a factor.
Thus, uvq xy q z G L. This is a contradiction.
Therefore, L is not a context free language.

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.

Example 5.6.3 : Prove that L = {a’b1 I j £ i}is not a CFL.


Solution :
1 Let us assume that L is CFL.
2 Let us pick up a word (D = a b c , where n is a constant given as per the pumping lemma*

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.6 : Prove that the language


L = {cocolco is in (0 + 1)*} is not a CFL.
Solution :
1. Let us assume that L = { cocolco is in (0 + 1)* } is a CFL.
2. Let us pick up a word co = 0 n l n0 n l n , where the constant n is given as per the pumping
lemma.
3. . co is rewritten as uvxyz, when lvxyl< n and v.y /G .
4. From pumping lemma, if uvxyz e L, then uv'xy'z is in L for each i =0,1,2...
5. vxy must be in one of the following forms :
0’,l j,0 i l k, l j0 k
Case I : vxy is of the form O’ - Two sets of 0’s will be unequal in uv°xy°z.
Case II : vxy is of the form i - two sets of l’s will be unequal in uv°xy°z.
i k i k
Case I I I : vxy is of the form Crl or VO -Two set of 0’s or two sets of l ’ s will be
,.oo
unequal m uv xy z.
Thus the string uv°xy z does not belong to L. 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.

5.7 Properties of Context-free Languages ; —— -

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)

5.7.1 Closure Properties . ____


2015, Dec. 201

A context free language is closed under following operations :


1. Union , ,
2. Concatenation
3. Kleene star
Context free language is not closed under intersection.
The intersection of a context-free language with a regular language is a context free
language.
The CFL is not closed under complementation.
The CFL is closed under reversal.

5.7.1.1 CFL is Closed under Union


Theorem :
context-free languages, then L t u L 2 is a context free language.
If Lj and L 2 are
Proof :
Let Lj be a CFL. It is generated by a context free grammar C l = (V h T t , P b S0.
Similarly, L 2 is another CFL generated by a context-free grammar G 2 = (V2 , T 2 , P 2 , S 2 )
We can combine the two grammars G j and G 2 into one grammar G that will generate the
union of the two languages.
♦ a new start symbol S i s added to G.

Scanned by CamScanner
CamScanner
. Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-101

• Two new productions are added to G.


S - > St Q.;
S —> s 2 , .
The grammar G can be written as :
’ • G = (V1 u V 2 u{S},T 1 u T 2, P 1 u P 2 u{S- S 1 I S2 } , S )
S can generate a string of terminals either by selecting start symbol of G j or start
symbol S 2 of G 2 . Thus, S can generate a string from Lj or from L 2 .
.*. L(G) = Lj u L 2

5.7.1. 2 CFL is Closed under Concatenation


Theorem :
If L I and L2 are context-free languages, then LjL2 is a context-free language.
Proof :

Let Li be a CFL with the grammar G] = (Vp T b P h S . -


Let L, be a CFL with the grammar G 2 = (V 2 , T 2 , P 2 , S 2) . :

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 :

G= (V 1 u V 2 u{S),T 1 u T 2, P I u P 2 u{S->S 1S 2 },S)


5.7.1 .3 CFL is Closed under Kleene Star
Theorem :

If L is a context-free language, then L is a context-free language.


Proof :
Let Lj be a CFL with the grammar Gj = (V b T b P b S,)
A new language L is constructed from L h which is L* .
i-e. L = L*
• A new start symbol S is added to the grammar G of L.
• Two new productions are added to G.

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 :

Context-free languages are not closed under intersection.


Proof :
Let us consider two context-free languages Lj and L 2.
Where,
Lj = { a n b nc m I n, m > 0 }
L 2 = { a m b nc n I n, m > 0 } • '< .■ ' '* /
The language Lj is a CFL with set of productions given below :
S —> AB
A —> aAb I £ ‘ ‘ .
B -> cB I E
The language L 2 is a CFL with set of productions given below : ~
S AB
■ A aA I E x .
B —» bBc I £
• A string oc E L j contains equal number of a’s and b’s.
♦ A string m2 E L 2 contains equal number of b’s and c’s.
A string (0 e L ( n L 2 will contain equal number of a’s and b’s and equal number of b’s
and c’s. • ■ . •
Thus, Lj n L 2 = { a n b nc n I n > 0 ). From pumping lemma for CFL, a string of the form
an bnc n cannot be generated by a CFG.
Therefore, the class of context-free languages is not closed under intersection.
5.7.1,5 CFL is not Closed under Complementation

Theorem : • .

The set of context-free languages is not closed under complementation.

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

The transition function 8 is defined as :


8 ((qi, q 2)» u, P) = ((P|, p2), y) [ transition for M ]
if and only if
5](q|,u, P) = (pj,y) [ transition for Mi ]
*
u
and «h' > (P2. e)

. When M passes from state (q h q 2) to state (p b p2). M! passes from state q! to p P

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-

Thus M i s a PDA for intersection of L(M,) and L(M2 ).


L(M) = L(M,) n L(M2 )
5.7.1.7 CFL is Closed under Reversal

Theorem :

If L is a context-free language, then so is L R.


Proof :
Let us assume that L - L(G) for some context-free grammar G = (V, T, P, S)
A grammar generating reverse L is given by
g r = (V,T,Pr , S )
*R
P can be obtained from P by reversing the right hand side of the production.
If A —> a is a production in P then / •
A —> a is a production in P .

5.7.2 Algorithmic Properties (Decision Properties)

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 .

5.8 University Questions and Answers

' Dec. 2005

Q. 1 Use pumping lemma to show the following : „


m p S
a" b c is not context free for n £ 0, m = 2n and p = m + n.
(Similar to Example 5.6.2)

Scanned by CamScanner
CamScanner
Theory of Comp, Sci. (MU-Sem. 5-Comp.) 5-105 Context Free Grammars (CFG)

Q- 2 Design a GRAMMER for accepting Even Palindrome over Z = (a, b).


(Section 5.2.3) (8 Marks)
Q. 3 Design an unambiguous grammar to accept arithmetic statements having terminals
a
s a (power), and ‘id’. (Example 5.3.5) ' (10 Marks)
Q. 4 Convert the following CFG to CNF :
S - aSa I bSb I c. (Example 5.5.21) (10 Marks)

Q. 5 Write short note on GNF. (Section 5.5.2) (5 Marks)

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)

Q. 8 . Convert the following grammar into GNF L*’* ' - - ■


S-»XY1l0
X -> 00X I Y
Y - » 1X1 (Example 5.5.20) (10 Marks)

Dec. 2006

Q. 9 Find CFG for generating


(i) Strings containing alternate sequence of 0’s and 1’s, Z = {0, 1}
(Example 5.2.33(i))
(ii) Strings over Z = {a, b} with atleast two a’s (Example 5.2.17, Refer ‘a’ as 0 )
(10 Marks)

Q.10 Convert the given grammars to GNF


S -> SS I aSb I ab (Example 5.5.19) (10 Marks)

May 2007

Q. 1 1 Convert the following to CNF :


S —> aSb I bSa I ab I ba I e (Similar to Example 5.5.9) Marks)

q, 12 Write short notes on :


(a) GNF (Section 5.5.2)
(b) Ambiguity resolution (Section 5.3) (15 Marks)

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)

Q. 15 Convert the following grammar in CNF


S —> AACD
A -> aAb I e
C — aC l a
D -» aDa I bDb I e (Example 5.5.6) (10 Marks)

Q. 16 Convert the following grammar in GNF


S —> AA I 0
A -» SS I 1 (Example 5.5.1 0) (Note : ‘0’ as ‘a’ and ‘1 ’ as ‘b’) (10 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)

Q. 20 Write short note on Ambiguity resolution. (Section 5.3) (5 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)

Q. 22 Given a CFG G, find G' in CNF generating L(G) ■ e.


S -> ASB I s ,
A —> AaS I a
B -» SbS I A I bb (Example 5.5.7) (1 o Marks)
Q. 23 Write short note on ambiguity resotutlon. (Section 5.3) (4 Marks)

Scanned by CamScanner
Scanned by
w
*r Theory of Comp. Sei, (MU-Sem. 5-Comp.) 5-107

Q. 24 Give context free grammar generating the following sets.


(i) The string containing no consecutive 'b's but ‘a’s can bo consecutive.
(ii) The set of all string over alphabet {a,b} with exactly twice as many a's as b’s,
(Example 5.2.33(H) and (ill)) (4 Marks)

Dec. 2009

Q» 25 Explain CNF and GNF with example. (Sections 5.5,1 a n d 5.5.2)


(6 Marks)
Q. 26 Let G be the grammar
S -» aB I bA
A a I aS I bAA
B b I bS I aBB
Find leftmost derivation, rightmost derivation and parse tree for the strinq
“bbaaabbaba”. (Example 5.3.9) (10 Marks)
Q. 27 Write CFG for language having number of a’s greater than number of b’s.

(Example 5.2.33(iv))
(10 Marks)

May 2010

Q. 28 Obtain a grammar to generate the language L = { 0 n 12n I n > 0) }


(Example 5.2.36)
(5 Marks)
Q. 29 Convert the following grammar in Chomsky Normal form -
S -> ASB I e .
B -» SbS I A I bb
A -> AAS I a (Similar to Example 5.5.7)
(10 Marks)
Q. 30 Show that the following grammar is ambiguous?
S - > aSbS
S —> bSaS
S -> e (Example 5.3.12(1), Refer ‘0’ as ‘a’ and ‘1’ as ‘b’)
(10 Marks)

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)

Q. 32 Convert the following CFG to CNF


S —> bA I aB
A -> bAA I aS I a
B aBB I bS I b (Example 5.5.3) (5 Marks)
Q. 33 Write short note on : Chomsky Hierarchy (Section 5.5.1) (6 Marks)

May 2011

Q. 34 Reduce the following grammars to GNF


‘ S-»AB
A -> BSB I B B I b (Example 5.5J4) (5 Marks)

Q. 35 Reduce the following grammars to GNF


B —» aAb I a
S -> AA I 1
A -> SS I 1 (Example 5.5.22) (5 Marks)

Q. 36 Convert the following grammars to CNF


A -> aBb I bBa
B -> aB I bB I e (Example 5.5.9) (10 Marks)

Dec. 2011

Q. 37 Write a short note on Ambiguity Resolution. (Section 5.3) (5 Marks)

Q. 38 Consider the grammar:


S- OB I 1A
A —> 0 I OS I 1AA
B-»1I1SIOBB
For the string 001 10101 find the following :
(i) Leftmost derivation
(ii) Rightmost derivation
(iii) Parse tree. (Example 5.2.6) (10 Marks)

Q. 39 Convert the following grammar into CNF :


S -> ASB I e
A -» AaS I a
B SbS I A I bb (Example 5.5.7) (10 Marks)

Q. 40 Convert the following grammar in GNF :


S AA I 0
A SS 11(Example 5.5.11) (Note : take 0 as a and 1 as b) (8 Marks)

Scanned by CamScanner
Scanned by CamScanner
— Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-109

Q. 41 Write short note on : Closure properties of context free language.


(6 Marks)

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.

Eliminate any useless symbols in the resulting grammar.


(iv) Put the resulting grammar into Chomsky Normal Form.
(Example 5.5.10) (14 Marks)
Q. 44 Prove that L = {a n I n is prime) is not context free. (Example 5.6.1) (6 Marks)
Q . 45 Convert the following grammar into Greibach Normal Form.
S—>AB1 I 0
• A -> 00A I B
B —> 1A1 (Example 5.5.20, Refer ‘X’ as ‘A’ and ‘Y’ as ‘B’) (10 Marks)

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

Q. 52 Explain C N F and G N F with example. (Sections 5.5.1 and 5.5.2) (5 Marks)

Q. 53 State a n d prove closure properties of Context Free Languages.


(Section 5.7.1) (5 Marks)

Q. 54 Consider the following grammar : ■


S —> iCtS I iCtSeS I a
C —> b
For the String ‘ibtibtaea’ find the following :
(i) Leftmost derivation
(ii) Rightmost derivation
(iii) Parse Tree z

(iv) Check if the above grammar is ambiguous (Section 5.2.7) (10 Marks)

Q. 55 Convert the following Grammar to CNF form :


S->ABA
A aA I bA I e
B —> bB I aA I © (Example 5.5.10(A)) (10 Marks)

Q. 56 Obtain leftmost derivation, rightmost derivation and derivation tree for the string
“cccbaccba". The grammar is

S —> SS a I SSb I c (10 Marks)

Scanned by CamScanner
Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 5-11 1 Context Free Grammars (CFG)

Ans. : [Hint : Refer as Example 5.2.3(A)]


s
Derivation tree :
s a

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

Q. 57 State and Explain closure properties of Context Free Lanauaoe


a
(Section 5.7.1) '
(5 Marks)
Q. 58 Convert following grammar to CNF and GNF.

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

B —> S VjS I Vj V 1 B - > S V5 , V5 -» Vj S


B V1 V 1
Vj-»b V b
V 2 —» a V 2 —>a
Converting to GNF :

Step 1 : Substituting symbols, we get,


S-> ASBIalbXj
A —> a S A I a -
B -> S X 2 S I b Xj
X b
X 2 —» a
Step 2 : Re-writing production in GNF :
Productions CNF forms
X,-»b
(1) X1 -> b
(2) X2 —> a X2 - > a
(3) A — aSA 1 a A — a S A 1a
(4) S - A S B I a l b X , S -> aSASB 1aSB [substituting A]
S — a 1 b Xj
(5) B —> SX 2 S I bX S -» aSASBX 2S 1 aSBX 2S 1aX 2S 1 b X t X2 S
• [substituting for S ]
S-»bX

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)

q. 63 Consider the following grammar G = (V, T, P, S) , V = {S, X}, T = {a, b} and


productions P are

S -» a S b I aX

X —> X a I S a I a

Convert this grammar in Greibach Normal Form (GNF). (1° Marks)


Ans. : [Hint : Refer Example as Example 5.5.22(A)]
Given set of productions

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

Removing left recursion form A2 production, we get

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

Q. 65 Convert the following CFG to GNF (10 Marks)

S -> AA I a

A —> SS I b (Example 5.5.11)

May 2017

Q. 66 Explain Pumping Lemma for CFG. (Section 5.6) (5 Marks)

Q. 67 Write short note on : Greibach Normal form. (Section 5.5.2) (5 Marks)

Dec. 2017

Q. 68 Write short note on Closure properties of Context Free Language.


(Section 5.7.1) (5 Marks)

Q. 69 Consider the following grammar : (10 Marks)


S -> I C t S I I C t S e S l a
C -> b ~
For the string ‘ibtibtaea’ find the following :
(i) Leftmost derivation
(ii) Rightmost derivation
(iii) Parse tree
(iv) Check if above grammar is ambiguous.
Ans. : [Hint : Refer Example as Example 5.3.4(A)]
(i) Leftmost derivation
C->b S -> i C t S
S=> i C t S e S => i b t S e S ======>
C—>b S —> a
ib tiC tSeS I b 11b t $ e S=>i b 11b t a e S
=>i b t i b t a g a

(ii) Rightmost derivation


S—>a S —> i C t S
S IC tSeS~ ICtSea
S_ >a
i C 11C t S e a — > I C 11C t a e a
n —* b C—>b
— > i c 11b t a e a .=> i b t i b t a e a

Scanned by CamScanner
V Theory of Comp, Sei, (MU-Sem. 5-Comp.) 5-116 Context Free Grammars (CFG)

(iii) Parse tree


S

(iv) It is an ambiguous grammar due to laughing if problem.


Q. 70 Reduce following grammar to GNF. (10 Marks)
S —> AB
A -> BSBIBBIb
B —> alaAb
(i) S -> 01SI01
> S -> 10SI10
S -» OOle
Ans.: [Hint : Refer Example as Example 5.5.22(A)]
Removing e -production, we get, '
S -> 01SI01I10SI10I00
It can be converted into GNF in an easy way by introducing two production
X 1 and Y -> 0 ’ '
/. Productions in GNF are
. s _> 0XSI0XI1YSI1YI0Y
X -» 1
Y -» 0

Chapter Ends...
□□□

Scanned by CamScanner
CamScanner
Pushdown Automata
(PDA)
Module 4

6.1 Introduction to Pushdown Automata (PDA)


MU - Dec. 2005, 'Dec. 2012, May 2015

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

Fig. 6.1.1 : Stack after reading the first half of a 5 b5

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

• A transition in PDA depends on :

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)

Input symbol, Stack symbol (top most)

Stack operation

Fig. 6.1.2 : A transition from q. to qj

A PDA uses three stack operations :

3) Nop operation, it does nothing to stack.


The language { an bn I n > 1 } can be accepted by the PDA of Fig. 6.1.3.

Input
symbol

Stack
a, any (b,a) symbol
push (a) (b,a) Pop (a)
POP (a)
Stack
operation

Fig. 6.1.3 : PDA for a nbn

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

6.2 The Formal Definition of PDA

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

F = F £ Q is the set of final states

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 :

A transition function is a mapping from

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)

------- Stack symbol


— ----------------------------b i s replaced
with b i.e. no
v I stack - operation
Current Input
state; symbol

• Left hand side of the transition, 5 (q p a, b) implies :


qj is the current state,
a is the current input,
b is the stack symbol (topmost).
Right hand side of the transition, (q2 » h) implies that
[The machine enters a new state q 2 . The top symbol of the stack , which is b is replaced with b.]
Thus, the transition 5(q P a, b) = (q 2, b) does not modify a stack,

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

Example : Transition function for a PDA accepting a language


L = ( w e {a, b}* I co is of the a n b n with n £ 1 ), can be givgen in various forms :
1. Using a set of rules.
2. Using graphical representation (transition diagram)
Using a set of equations :

8(q 0 , a, z0 ) = (q 0 , az0 ) [First a of a nbn is pushed onto the stack]


8(q 0, a, a) = (q0 , aa) [Subsequent a’s of a n b" are pushed one by one onto the stack.]
8(q 0 , b, a) = (q p e) [On Seeing the first b, the machine will make a move to q t with a POP]

Scanned by CamScanner
y Theory of Comp. Sei, (MU-Sem, 5-Comp.) 6-6 Pushdown Automata (PDA)

8(q P b, a) — (q p e) [For each subsequent b, a POP operation is performed]


8(q P £, Zo) - (q 2, zo) [Alter the end of input string, machine will transit to a final state]

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

Using graphical representation (transition diagram) :


a,z0 (az 0
a , a|aa b,a|s

Fig. 6.2.1 : Transition diagram

6.3 Instantaneous Description of a PDA


Instantaneous behaviour is useful in showing the processing of a string by PDA.
3 3 r*
Processing of the input string a b by the PDA of Fig. 6.2.1 is shown in Fig. 6.3.1.

Current Input • stack


Stack state string contents

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)

Sr. No. Graphical representation Tuple format


_ ’'lifeSife .... Qtistuntuneoug behaviour) (instantaneous behaviour)
8.
Th fa ~bTb I b l " ( 2’ A))
iL

q2 — 4 z0

Fig. 6.3.1 : Processing of a V by the PDA of Fig. 6.2.1


The PDA for recognizing a string of the form a n b n can be described as:
M
- (( <a >b h {a>b( z0 }, 8, q0 , z0 , { q2 })

Q , the S, input f, Stack Transition Start Set of


set of symbols symbols function state final
states as given I states
above
Initial
stack
symbols

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)

6.4 The Language of a PDA


MU - Dec. 2008
A language L can be accepted by a PDA in two ways :
1. Through final state.
2. Through empty stack.
It is possible to convert between the two classes.
1. From final state to empty stack.
2. From empty stack to final state.

PDA by
final state

Fig. 6.4.1 : Equivalence of two PDAs

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

6.4.2 Acceptance by Empty Stack

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 :

1. Sequence ol 0’s should be pushed onto the stack in state q 0 .


S(q 0 » 0» Zq) = (q 0 , Oz0 ) [Push the first 0]
S(q 0 , 0, 0) = (q 0 , 00) [Push subsequent 0’s]
2. A ‘0 should be popped for every 1 as input till the stack becomes empty.
8(q 0 , 1 , 0 ) =• (q p £) [pop on first 1 and change the state to q {]
5(q p 1 , 0 ) = (q p e) [Pop on subsequent 1 as input till every 0 is erased from the stack]
Subsequent l ’ s (m - n) will have no effect on the stack.
8 (q p 1, z 0 ) = (q p z0 )
Finally, the symbol Z o should be popped out to make the stack empty.
8 (q p e, z 0 ) = (q p e) , .
This step is required if the language is to be accepted through an empty stack.
Transition behaviour of the PDA is shown in Fig. Ex. 6.4.1 (a to d)
1,0|£
O,zo |Ozo
0|0
Q°' ° w.
qp
Fig. Ex. 6.4.1(a) : Transition diagram for acceptance through an empty stack

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)

2. 8(q 0 . 0, 0) = (q 0 , °°) 2. 8(q 0 . 0, 0) = (q o , ° 0 )


= 3. =
3. 5(qo. 1. 0) £) 8(q 0 , 1 , 0 ) (q P s)
4. 8(q 1, 0)
=
(qp £) 4. 8(q P 1, 0) = (q P e)
P

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

The PDA accepting through an empty stack is given by :


M — ({Qo* 1 )» 10* 1* z ol> Zo. 0 )

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-11 Pushdown Automata (PDA)

Where 8 is given in Fig. Ex. 6.4.1(c).


The PDA accepting through final state is given by :
M
= ({%’ <11’ q 2 M0. 1 ) . {0, 1, Zo), 8, q 0 , z 0 , {q 2 } )
Where 6 is given in Fig. Ex. 6.4.1(d).
Example : Processing of string 00111 by the PDA
Case I : Acceptance through empty stack.

(q, 00 11 1, Z.) (q(), 01 1 1, Oz.)

' 3(q.,ll 1 .00z.>

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

Example 6.4.2 : Let L = {a m bn I n < m}. Construct,


i) PDA accepting L by empty store.
ii) PDA accepting L by final state.
Solution :
j) Algorithm (Accepting L by empty store)
1, Sequence of a*s should be pushed onto the stack in state q 0 .
z az
8(q 0 , o> “ (Ao* o>

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)

Fig. Ex. 6.4.2

M = ({ q 0, q }, {a, b}, {a, b, z0 ), 8, q0, z0, 0)


ii) Accepting L through final state
Following modifications are required to accept through a final state.
In stepS, when the input ends with stack containing m-n a’s, the machine should enter
the final state.
Table Ex. 6.4.2(a)
8(q 0 ,a,z0 ) = (q 0,az 0 )
8(q 0 ,a,a) = (q0 ,aa)
8(q 0 ,b,a) = (q p e)
8(q p b,a) = (q p s)
8(q p e,a) = (q 2 , e)

Fig. Ex. 6.4.2(a)

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 :

At any point of time, there could be one the six situations :


1. Stack contains z 0(topmost) and the input is a
8(q 0, a, Zq) = (q0 , az 0 ) [Extra a is pushed]
2. Stack contain z 0 (topmost) and the input is b
8(q 0 , b, z 0 ) = (q 0 , bz0 ) [Extra b is pushed]
3. Stack contains a(topmost) and the input is a.
8(q 0 , a, a) = (q 0, aa) [Excess a’s will increase by 1]
4. Stack contains a(topmost) and the input is b
8(q 0 , b, a) = (q 0 , e) [Excess a’s will decrease by 1]
5. Stack contains b(topmost) and the input is a.
8(q 0 , a, b) = (q 0, £) [Excess b’s will decrease by 1]
6. Stack contains b(topmost) and the input is b.
8(q 0 , b, b) . = (q 0 , bb) [Excess b’s will increase by 1]
The PDA is shown in Fig. Ex. 6.4.3(a,b).
b z
. ol bz o
a , z 0 |az 0
a ,b|a
«» Xq I d X Q b ,a|e
a, b | e a ,a|aa
b ,a | e b ,b|bb

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

Fig. Ex. 6.4.3

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

Fig. Ex. 6.4.4

Scanned by CamScanner
CamScanner
f JTheory of Comp. Sci. (MU-Sem. 5-Comp.) 6-15 Pushdown Automata (PgM

PDA accepts through the final state q 4.


The PDA M = {Q, S, T, 8,q 0 , z 0 , F )
Where,
Q = (qo,qi, Q2> )
S = {a, b, c, d}
r = {x, z 0 }
The transition function 8 is given by,

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)

3(q 2 > Zo) = . (q 4 , z o)


q 0 is initial state,
Zq is initial stack symbol.
Set of final states F = { q 4 }

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

Fig. Ex. 6.4.5

Scanned by CamScanner
CamScanner
5 Theory of Comp, Sci. (MU-Sem. 5-Comp.) 6-16 Pushdown Automata (PDA)

• A cycle through q 0 —-> q J —> q 2 > q{) (races a group of bdb.


The PDA M = { Q , S , r , 8 , q 0, z 0,<|)}
Where,
Q = {qo>qi,q 2>q3 ), S = { b , d , c ) , r = { x , z 0)
q0 is the initial state, z0 is initial stack symbol.
The transition function 8 is given by,
8(q 0 , b, z0 ) = (q (, z0 )
8(q 0 , b, x) = (qj, x)
8(qj, d, z0) = (q2, z0 )
8(q 1, d, x) - (q2, x)
8(q 2, b, zo) = (q0 , xz 0 )
8(q2, b, x) = (q0 , xx)
8(q 0 , c, x) = (q3, e)
8(q 3, c, x) = (q3, e)
8(q 3, E, z0 ) = (q_3, E) Accept through empty stack.

Example 6.4.6 : Construct pushdown automata for the following language :


L = {the set of strings over alphabet {a, b} with exactly twice as many
a’s as b's}
Solution :
To solve this example, we can take three stack symbols :
r = (x, y, Zo ) -■ .
Where, x stands for 2 a’s,
y stands for a ‘b’,
z0 is initial stack symbol.
• Stack will be used to store excess of x’s over y’s or excess of y’s over x’s.
• Since a ‘x’ will be pushed onto the stack after 2 a’s, after first ‘a’ the machine will
transit to q j to remember that one ‘a’ has already come and the second ‘a’ in q 1 will
complete 2 a’s.
• Status of the stack and the state of the machine is shown in Fig. Ex. 6.4.6. Input applied
to the machine is abaabbaaaaabaab.
a haabbaaaaabaa.b ◄----- Input

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

Fig. Ex. 6.4.6 : Status of stack for input abaabbaaaaabaab

Scanned by CamScanner
y Theory of Comp. Sei, (MU-Sem. 5-Comp.) 6-1 7

Implementation of PDA using final state :


Let the PDA, M = {Q, £, T, 5, q0 , z 0, F)
Where,
Q = {q0 ’ 9i» q 2 l* 22 = { a * b}» r = {x, y, z0 }
q D is initial state, z() is initial stack symbol.
F
= {q 2 )
Transition function 8 is given by :
1. 8(q 0 , a > z o) = (q P z o) "1 First input could be either a or b
2. 8(Qo, b, Zo) = (qo> y z o) -J
3. 8(q 0 . a, x) = (q P x) [goes to q t to remember first a of aa]
4. 8(q 0 , a, y) = (q P y) [goes to q! to remember first a of 2 a’s]
5. 8(q 0 , b, x) = (q 0. e ) [Excess x is removed]
6. 8(qo, b, y) = (q 0 > yy) [Excess y increases by 1]
7. 8(q P a, zo) = (q<r xz0 ) [Push a x for 2 a’s]
8. 3(q p a, x) = (qo> xx) [Push a x for 2 a’s]
9. 8(q P a, y) = (q 0. £ ) [Two a’s will remove a ‘y’]
10. 8(q p b, zo) =■ (q P y z o) [Excess y is saved]
11. 8(q p b, x) = (q P e) [Excess x is removed] .
12. 8(q r b, y) = (q P yy) [Excess y increases by 1]
13. 8(qo, e, z 0 ) = (q 2 > z o) [goes to final state to accept the string]
Example : Processing of string abaabbaaaaabaab by the PDA
(Rule 1)
(q 0 , abaabbaaaaabaab, z0 ) ----------> (q p baabbaaaaabaab, z0 )
(Rule 10)
(q p aabbaaaaabaab, yz0 )
(Rule 9)
(q0 , abbaaaaabaab, z0 )
(Rule 1)
(q p bbaaaaabaab, Zq)
(Rule 10) z c
----------> (q p baaaaabaab, yz0 )
(Rule 12)
----------> (q p aaaaabaab, yyz0 )
(Rule9) / k u X
----------> (q 0 , aaaabaab, yz0 )
. (Rule 4) z , ,
----------> (q p aaabaab, yz0 )

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)

8(q 0 , a, a) = (q0 , aa)


8(q 0 , e , z 0 ) = (q 0 , e ) [To accept a null string]
8(q 0 , b, a) = (q p 6)
8(q p a, a) = (q p E) “I
8(q p b, a) = (q p E) erase, everything from (he stack
8(q P e, a) = (q p e) j
8(q p E, Zo) = (q p E)

Example 6.4.8 : Let L = { a n bnc md m I n, m Z 1} find a PDA that accepts L.


Solution :
Algorithm :

1. Sequence of a’s should be pushed onto the stack.


2. For every b as input, an ‘a’ should be erased from the stack.
3. Sequence of c’s should be pushed onto the stack.
4. For every d as input, a ‘c’ should be erased from the stack.
The PDA is given by :
M - ( [ q0 , q p q 2 , q 3 ), [ a , b , c, d ) , {a, c, z 0 } , 8 , q o ,Zo, <|>)

Where the transition function 5 is given below :


8(q 0 , a , z 0 ) = (q 0 > az o) [Push the first a]
Stq , a, a) = (q 0 , aa) [Push remaining a’s]
8(qo, b, a) = (q p £) [Erase an ‘a’ on first b]
8(q p b, a) = (q p £) [Erase remaining a’s on subsequent b’s]
8(q p c, z 0 ) = (q 2, cz 0 ) [First c is pushed]
S(q 2 , c, c) = (q 2, cc) [Subsequent c’s are pushed]
8(q 2 , d, c) = (q 3 , £) [On first d, machine transits to q 3 with a pop]
8(q 3 , d, c) = (q 3 , e ) [For every d, a ‘c’ is erased]
S(q 3 , e, Zo) = (q 3 , e) [String is accepted through empty stack]

Example 6.4,9 : Let L = { a b Jc k 11, J, k £ 0 and 1 + j = k }


(i) Find a PDA accepting through final state.
(ii) Find a PDA accepting through empty stack.

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]

(ii) PDA accepting through empty stack is given by,


M = ( [q0, q p q2 ), {a, b, c), [x, z0 },q0 , z 0 , 8, $ ) ,

where the transition function 8 is :


1. 8(q 0 , a, z0 ) = (q0, xz0)
2. 8(q 0 , a , x ) = (q0 , xx)
3. 8(q 0 , b, z0 ) = (q P xz0)
4. 8(q 0 , b, x) = (q P xx)
5. 8(q p b,x) = (q P xx)
6. 8(q p c, x) = (q 2. e)
7. S(q 2, c, x) =■ (q2 > e)
8. 8(q 2, e, z0) = (q2. e) [Stack is made empty]
9. 8(q 0 , e, z0) = (q0 , e) [Accept a null string]

Scanned by CamScanner
V Theory of Comp, Scl. (MU-Sem. 5-Comp,) G-21 Pushdown Automata (PDA)

Example 6.4.10 Consiruct a PDA sampling { sW I m, n > 1) by mill aoro.

Solution :

Algorithm :

1. The sequence of a’s should be pushed onto (he stack in state q 0


(Qo* a » z o) = (Ho* a z o)
8(q 0, a, a) = (q0 , aa)
2. On first b, the machine moves to qj and remains there for b’s. b’s will have no effect on
the stack.
3. For every ‘a’, an ‘a’ is erased from the stack.
The PDA accepting through empty stack is given by
M = ({q0 , q P q 2 h (a, b), {a, z0 ) , 8 , q 0 , z0 , <|> )
Where the transition function 8 is :
1. 8(q 0 , a , z 0 ) = (q0 , az0 ) [First ‘a’ is pushed]
2. 8(qo, a, a) = (q0 , aa) [Subsequent a’s are pushed]
3. b, a) = (q p a) [Input symbolsz b’s are skipped]
4. 8(q,,b, a) = (q p a)
5. 8(q P a, a) = (q2, e) [An a is erased on first a of last a’s]
6. 8(q 2, a, a) = (q 2, £) [An a is erased on subsequent a’s of last a’s]
7. 8(q 2 , e, z0 ) = (q 2 , e ) [Accepting through empty stack]

Example 6.4.1 1 : Design a PDA for accepting a language


L = ( W c W I W e {a,b) ]
MU - May 2008, May 2010, May 2011
Solution :
WT stands for reverse of W. A string of the form WcWT 'is an odd length palindrome
with the middle character as c.
Algorithm :

If the length of the string is 2n + 1, then the first n symbols should be matched with the
last n symbols in the reverse order. A stack can be used to reverse the 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

Fig. Ex. 6.4.11 : A PDA on input abbcbba .

The PDA accepting through final state is given by


M = f {m. a.. a , l . (a. b. c l . (a. b. Zn). 5, q„, Zn,{q,)!

Where the transition function 8 is given below :


1. = (q0 > a ) First n symbols are pushed onto the stack
8(q 0 , b, E) = (q0 > b )
3. 8(q 0 , c, e) = (q P e) [State changes on c]
4. 8(q p a, a) = (q P e) Last n symbols are matched with first n
symbols in reverse order
5. 8(q p b, b) = (q P e)
6. 8(q p 8, Zo) = (q2, Zo) [Accepted through final state]
A transition of the form 8(q0, a, e) = (q0 , a) implies that always push a, irrespective of
stack symbol.
Example 6.4.12 : Design a PDA to check whether a given string over {a, b} ends in abb.
Solution :
Strings ending in abb form a regular language. A regular language is accepted by DFA. '
A PDA for a regular language can be constructed in two steps :
1. Design a DFA.
2. Convert DFA to PDA by appending no-stack-operation to every transition in DFA.
Step 1 : DFA for the given language is :

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-23 Pushdown Automata (PDA)

Step 2 : From DFA to PDA

Fig. Ex. 6.4.12(a) : A PDA constructed from DFA

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>

and Trace the sequence of moves made corresponding to input string


(([])[ i). ,
Solution :
The transition function of the PDA is given below :

1. 8(q 0 , (, z0) = (q 0, (z c ) Push the opening bracket ‘(’

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

7. 8(q 0 , ), ) ) = (q . e) POP an opening bracket for a closing bracket.


0

8. 8(q 0 , ], ] ) . = (q 0. e)

9. 8(q<), Zq) (qp Zq) J Accept through a final state.


Simulation of PDA for the input string (([])[])
Rule 1
(q0’ ((□)□)» zo) ---------> (q0> (□)□), ( z0 )
Rule 2
--------> (q0, EDO), (( z0 )
Rule 5
---------> (q0, ])[]), [(( z0)
Rule 8
---------> (q0, ) []), (( z0)
Rule?
---------> (q0, []), ( z0 )
Rule 5
— ------> (q0 , ])> [( z o)
Rule 8
--------* (q0 >)>(zo)
Rule 7
(qtr zq)
Rule 9
--------> (q p 6, z0 )

Solution :
1. Algorithm :

(a) An x is pushed for every leading 0 of the string 0™ 1" 0 n' +


(b) An x is pushed for every 1 in the input of the string 0 m 1" 0 n> + n.
(c) x’s on the stack are matched with trailing 0’s in the input of the string 0 m l n 0 m + ",
2. Transition function 8 :
x z
8 (qo» 0, Zq) — (q 0. o)
x x
8 (q0 , 0, x) = (q0. )

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

6.5 Non-deterministic PDA (NPDA)


MU - Dec. 2005, May 2010, Dec. 2011,
Hi\/iay2013, May 2015, Dec. 2017

There are two types of push down automata :


1. DPDA (Deterministic PDA)
2. NPDA (Non-deterministic PDA)
A NPDA provides non-determinism to PDA.
In a DPDA there is only one move in every situation. Where as, in case of NPDA there
could be multiple moves under a situation.
DPDA is less powerful than NPDA. Every context free language can not be recognized
by a DPDA but it can be recognized by NPDA. The class of language a DPDA can accept lies
in between a regular language and CFL. A palindrome can be accepted by NPDA but it can not
be accepted by a DPDA.

Scanned by CamScanner
Theory of Comp, Sei, (MU-Sem. 5-Comp.) 6-26 Pushdown Automata (PDA)

M U - Dec. 2007
Solution :

An odd palindrome will be of the form :


R
1. waw
abj a baj, a b a a a b a aa a q
R R R
WWW WWW
i R
2. wbw
) a p ,aa! b
R h- R
W W W

If the length of w is n then a palindrome of odd length is :


First n characters are equal to the last n characters in reverse order with middle character
as ‘a’ or ‘b’.
Algorithm :

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"

The transition table for the PDA is given below,


8(q 0 , a, e) => {(q P e), (q0 , a))

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)

8(q P e, z 0 ) => {(q p E ) j [Accept through an empty stack]


Where, The set of states Q = {q 0 , q j

The set input alphabets = { a , b )


The set of stack symbols T = {a, b, zj

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 :

An even p a l i n d r o m e will b e of t h e form ww R

|a b a [ [aa |
R AR AR
w W W WWW

Centre Centre Centre

If the length of w is n then a palindrome of even length is :


First n characters are equal to the last n characters in the reverse order.
The character immediately before the middle position will be identical to the character
immediately after the middle position.
Algorithm :

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 >

(b) It belongs to first half.


- Push the current input
8(q 0 , a, £) => (q 0 , a)
8(q 0 , b, £) => (q 0 - b )

Scanned by CamScanner
Scanned by CamScanner
y Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-29 Pushdown Automata (PDA)

4. n is decided non-deterministically. Every character out of first n characters, whose


previous character is not same as itself should be pushed onto the stack.
- Push the current symbol using the transitions :
8(q 0 , a , l>) => (q0, ab)
8(q 0> b,a) => (q0,ba)
The transition table for the PDA is given below :
s
(q<r a > z o) => {(qo > az o)}
z0 ) {(q0, bzg)} . '
8(q 0 , a, a) => {(q0 , aa) (q p s)}
8(q 0, a, b) => {(q0 , ab)}
8(q 0 , b, a) => {(q0 , ba)}
8(q 0 , 8. b) — {(q0, bb), (q p £)}
8(q p a, a) => {(q p e)},
8(q p b,b) => {(q p e))
8(q p e, Zq) => {(q p e)} [Accept through an empty stack]
Where, the set of states Q = {90,9!) ‘
the set of input symbols E = (a, b}
the set of stack symbols T = {a, b, z0 ]
Starting state = q0
<
Initial stack symbol = z0 •
MU - Dec. 2012
Solution
A palindrome will be of the form :
1 wwR - even palindrome
2. waw
3, wbw R I - odd palindrome
If the length of w is n then a palindrome is :
First n characters are equal to the last* n characters in the reverse order with the middle
character as
[ For odd palindrome ]
(1) a
(2) b [ For odd palindrome]

(3) e [ For even palindrome]

Scanned by CamScanner
Theory of Comp, Sei, (MU-Sem. 5-Comp.) 6-30 Pushdown Automata (PDA)

The transition table for the PDA is given below :


8(q 0 , a, z0 ) => {(q t , z0 ), (q0< az0 ))
5(q 0, b , z 0 ) => {(q p z0),(q 0 , b z 0 )}
8(q 0, a, a) => {(q0 , aa) (q p a), (q p e)}
8(q 0 , a , b ) => { (q 0 , ab), (q p b) }
8(q 0, b, a) => {(q 0 , ba), (q p a)}
5(q 0 , b , b ) => {(q0 , b b ) , ( q p b),(q p E))
8(q t , a, a) => {(q p e)}
8(q 1 , b , b ) => {(q p e)}
8(q n e, z 0 ) = > {(q p e)} [Accept through an empty stack].

Details of important transitions : .

The transaction, 8(q 0 ,a,a) => { (q0 ,aa), (q a), (q 1 f e) }

Input
'a' is part
of w

Input 'a1 i s middle


point of odd palindrome

Input 'a' i s first


p
character of w
of even palindrome

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

Input 'a' Is Input 'a‘ Is


part of w middle point
, R
of waw

Scanned by CamScanner
'r Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-31 Pushdown Automata (PDA)

E x a m p l e 6.5.4 : Construct PDA for the language


L = { a b I i j or j * k)
Solution :
Algorithm :

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

1. Initial a’s will be pushed.


2. For every input symbol b, an ‘a’ should be erased from the stack.
3. If a’s and b’s are not equal i.e. either some a’s are left on the stack or some b’s are still
in the input then the string should be accepted.
In state q2

1. Initial a’s will be skipped.


2. For every input symbol b, a ‘b’ should be pushed.
3. For every input symbol c, a ‘ab’ should be erased from the stack.
4. If b’s and c’s are not equal i.e. either some b’s are left on the stack or some c’s are still
in the input then the string should be accepted.
Transitions for PDA :
8(q 0 , a, z 0 ) = {(q p aa), (q 2 , z 0 )}
8(q p a, a) = {(q P aa)}
8(q p b, a) = {(q3 , e)}
8(q 3 » 8. Zq) — { (q f , Zg)}
8 (q 3 , c, a) = ( (q f , £)}
8(q 3 , b, a) = {(q3 , £))
S(q 2 , a, z0 ) = {(q 2, e))

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)

S(q r , c , e ) = ((q f ,E)) ...


S(q f ,E,E) = ((q f ,E)}

Pushdown Automata and Context Free Language

The class of languages accepted by pushdown automata is exactly the class of


context-free languages. The following three classes of languages are same :
1. Context Free Language defined by CFG.
2. Languages accepted by PDA by final state.
3. Languages accepted by PDA by empty stack.

\ PDA by PDA by
empty:
\ stack

Fig. 6.6.1 : Equivalence of PDA and CFG


• It is possible to find a PDA for a CFG
• It is possible to find a CFG for a PDA.

6.6.1 Construction of PDA from CFG

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

Example 6.6.1 TTind a PDA for the given grammar


• s - > 0 S 1 I 00 I 11

Solution : The equivalent PDA, M is given by :


M = ({q), {0, 1 ), (0, 1, S}, 8, q, S, <j>), where 8 is given by :
8(q, E, S) = ((q, OSl), (q, 00), (q, 11)}
8(q, 0, 0) = ((q, £)}
8(q, 1, 1) = {(q, E)}

CamScanner
Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6J33 P hdown Automat PDA)

Example 6.6.2 : Convert the grammar


S->0S1 | A
A - > 1A0 I S I e
to PDA that accepts the same language by empty stack.
Solution :
Step 1 : for each variable A e V, include a transition 8(q, E, A) => { (q, a) I A -> a is a
production in G )
8 ( q , e , S ) =>((q,0Sl),(q,A)J
8(q, E, A) =>{(q, 1A0), (q, S), (q, E)}
Step 2 : For each terminal a e T, include a transition 8(q, a, a) => (q, E)
8(q, 0, 0) = ((q, e))
8(q, 1, 1) = {(q, £)}
Therefore, the PDA is given by :
' M = ({q}, {0, 1),{S, A, 0, 1 }, 8, q, S, <j>)
where 8 is :
8(q, 8, S) = {(q, 0S1), (q, A)}
8(q, e, A) = {(q, 1A0), (q, S), (q, e)}
? ;
8(q, 0, 0) = {(q, 8)}
8(q, 1, 1) = {(q, 8)}

Example 6.6.3 : Let G be the grammar given by


S - aABB I aAA, A -> aBB I a, B -> bBB I A.
Construct NPDA that accepts the language generated by this grammar.

Solution : The equivalent PDA, M is given by :


M = ({q},{a, b},{a, b, S, A, B},8, q, S, 0)
Where, 8 is given by :
8(q, E, S) => {(q, aABB), (q, aAA)}
8(q, e, A) => {(q, aBB), (q, a)} For each production in given grammar
8(q, 8, B) =>{(q, bBB), (q, A))
8(q, a, a) => (q> e)
8(q, b, b) => (q, 8) For each terminal in T

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)

Solution : The equivalent PDA, M is given by


M
= ((q}.{0, l ) , ( 0 , l , S , B ) , 8 , q , S,<|>),
where 8 is given by
8(q,e,S) = > ( ( q , OBB))
8(q,e,B) => {(q,OS),(q, l S ) , ( q , 0 ) ) For each production in the given grammar
8(q, 0, 0) => { (q, g) }
8(q, 1, 1 ) = > { ( q , e ) ) For each terminal
4
Acceptance of 01 0 by M :

8(q, 6, S) = (q, OBB)


8(q, 010000, S) -----------------------------------> (q> 010000, OBB)
8(q, 0, 0) = (q, e)
. -----------------------------------> (q, 10000, BB)

. 8(q, e, B) = (q, IS)


-------j—— > (q, 10000, 1SB)
8(q, 1, 1) = (q, e)
’ (q, 0000, SB)
8(q, E, S) = (q, OBB) ,
(q, 0000, 0BBB)

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)

'f 8(q, 0, 0) = (q, 6)


-----------------------------------> (q,O,B)
8(q, e, B) = (q> 0)
—■—————— > (q,0,0) ;

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)

Thus the string 0104 is accepted by M using an empty stack.


010 4 e L

E x a m p l e 6.6.5 : Design a PDA to recognize the language generated by the following


grammar :
S -> S + S I S * S I 4 I 2
Show the acceptance of the input string
2 + 2 * 4 by this PDA.
Solution : The equivalent PDA, M is given by :
M = ({q }, {+, ♦. 4, 2 ) , {+, *, 4, 2, S ) , 8, q, S, 0) '
Where 8 is given by :
8(q, E, S) => {(q, S + S), (q, S * S), (q, 4), (q, 2)} I For every production in G
8(q, +, +) = {(q. e)}
8(q, *, *) = {(q, e)}
8(q, 2, 2) = { (q, £)} for every terminal in T.
8(q, 4, 4) = {(q. e)}
Acceptance of 2 + 2 * 4 by this PDA :

8(q, e S) => (q, S + S)


8(q, 2 + 2 * 4 , S) > (q, 2 + 2 * 4, S + S)

8(q, e, S) => (q, 2)


> (q, 2 + 2 * 4, 2 + S)

8(q, 2, 2) => (q, e)


> (q. + 2 * 4, + S)

8(q, +, +) => (q, e)


> (q> 2 * 4, S)
8(q, e, S) => (q, S * S)
. ------------------------------------> (q, 2 * 4, S * S)
8(q, e, S) => (q, 2)
__________________> (q, 2 * 4, 2 * S)

8(q, 2, 2) => (q, e)


> (q, * 4, * S)

8(q, *, *) => (q, E)


------------------------------------> ( q . 4 , S )

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

Solution : The equivalent PDA, M is given by,


M = ({ q ) , {0, 1, a, b, *, + , (, )},(0, 1, a, b, *, +, (, ), I, E}, 8, q, E, *)
where, 8 is given by,
8 ( q , e , E ) = {(q, I), (q, E * E), (q, E + E), (q, (E))J
8(q, e, I) = {(q, a), (q, b), (q, lb), (q, la), (q, 10), (q, II)}
• 8(q, 0, 0) = {(q, e)}
8(q, 1, 1) = {(q.e)} ’
8(q, a, a) = {(q, e)J
8(q, b, b) = {(q, e))
8(q, +, +) = {(q, £)}
8(q, *, *) = ((q, e)}
6(q, (, ( ) = {(q.e)} -
8(q, ), )) = {(q.e)} y ■
Example 6.6.7 : Show that if L is generated by a CFG then there exists a PDA accepting L.
Solution :
Proof :
Let G = (V, T, P, S) be a Context Free Grammar and a PDA, M is constructed as given
below :
M = ({q}, T, V U T, 8, q, S, (|))
where 8 is defined by .
1. For each variable A e V, include a transition,
8(q, e, A) =>{(q, a) I A a is a production in G }

2. For each terminal a e T, include a transition


8(q,a, a) =>{(q, e)}

The transitions of M are designed to simulate a leftmost derivation of a string.


1 The transition of the form
8(q, e, A) =>(q, a)
is for expansion of the topmost variable of the stack.

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-37 Pushdown Automata (PDA)

2. The transition of the form


8(q, a, a) =>(q, e)
is for removing terminals from the stack so that a variable is exposed for further
expansion.
To prove that a PDA constructed using above rules is equivalent to G.
We can prove that a word w G L(M) if and only if w G L(G).
where, L(M) is the language of PDA
and L(G) is the language of the given CFG.
Let us take a word w G L (G). The word w can be derived using the leftmost derivation.
S => => a 2 => an=w
where, cq oq + j
is obtained by single application of leftmost derivation, using a production in grammar
G. We will show that for each otj there is a unique configuration of PDA. a n corresponds
to the configuration of PDA accepting w.
Let a, = Xj Pj
Where, x, G T* and Bj G (V u T)*
The string a, corresponds to a unique configuration of PDA given by a pair :
(Yi.Pi) .
The corresponding diagram is shown in Fig. Ex. 6.6.7.
Input

<Xj = Xj pj , State of
the PDA = q

Fig. Ex. 6.6.7 : Unique configuration of PDA for cq

• Xi portion of the input string w has already been scanned.


• The yj is the portion of the input string, yet to be scanned.
• The stack contains
The theorem can be proved by induction on i of oq.

Base case : i = 0 :

a 0 = S, the configuration of PDA is given by

Entl re Stack contains the


Input Initial symbol s.

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)

Set of productions for the equivalent CFG

1. Add tlie following productions for the start symbol S.


Z
S —> [q 0 Qi] for each q ( G Q, where z is the start symbol
2. For each transition of the form
8(qi, a, B) => (q jt C)
Where,
(a) q h qj G Q
(b) a belongs to (E u e)
(c) B and C belong to (T o £)
Then for each q G Q, we add the production :

[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

Example 6.6.8 : Convert PDA to CFG. PDA is given by


M = ({ p, q}»{0, 1}, {x, z), 8, q, z), transition function 8 is defined by :
8(q, 1 , z) =>{(q, xz))
8(q, 1 , x) =>{(q, xx)}
8(q, e, x) =>{(q, e)}
8(q, 0, x) =>{(p, x)}
8(p, 1 , x) =>{(p, e)}
8 (p, 0, z) =>{(q, z))
Solution :

Step 1 : Add productions for the start symbol.


Z
S-»[q q]
Z
S —> [q p] *
Step 2 : Add productions for 6(q, 1, z) => {(q, xz))
Z X Z
[q q] —> 1 [q q] [q q]

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)

Original name New name

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 Qo] ~ 1 [Qo Qo][Qo Qo]’ ;


x x x
[q 0 q 0] i [q 0 q J [qr q0]

[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 qcd —* 0 [q i x Qol ■■ 5’.

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

'' . r- * . 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.

S “> [Qo Qol


Zn
S->[q 0 ° qi ]
Step 2 : Add productions for (q 0, a, a) = { (q 0, aa) }

[Qo a Qol —> a ho a


qd tqo a qd
a a
[q 0 qd -> tq» a q d tqi a
qd

[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 £)}

[q« a q J - > b • ■ '


Step 4 : Add productions for 5(q p b , a ) = { (q i , e ) ) -

[qi a q i l - > b . '


Step 5 : Add productions for 8( q i , e, z0) —> { (q ,, e)}
„ Zn
£
________[qi qJ ________________ _ _ _ _ _ _ _ _ _ _ _________________
Example 6.6.11 : For the PDA
({ q 0 > q, ), { 0, 1 }, { 0, 1, Zq }, 8, q0 , Zo, 0) where 8 is
8(q 0 , e, Zo) = {(q, , e)}
8(q 0 , 0, Zo) = {(q 0 , Ozo )}
8(q 0 , 0, 0) = {(q0 , 00))
8(q0 . 1 , 0 ) = {(q0 . 10))
8(q 0 . 1 , 1 ) = {(q 0 . 11))
8(q 0 > 0, 1) = {(qi. e)}
8(qi, 0, 1) = {(q„ e))
8(q„ 0, 0) = {(q v e)}
8 ( q „ e , z0 ) = {(q 1 ,e))
Obtain CFG accepted by the above PDA and simplify the CFG and describe
the language it accepts.

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

[q<> ° 9ol - * 0 [qo 911[91 9ol


0 0
[qo qJ o [qo qo] [qo qJ

[qo Zo q i ] - * ° [ q o ° q i H q i °qij ____


4. 8(q 0 , 0, 0) = (q0 , 00)
[qo qol o [qo qo ] [qo qo]
0
[qo qo] [qo qJ [qi qo]
0
[q0 q J -> [qo qo] [qo q J
0
[qo qJ -> [qo qJ [qi qJ
5. 8(q 0 , 1,0) = (q0 , 10) fao Qol -» 1
[Qo qol [qo qol

[qo qol -> i [qo qJ [qi qol

[q0 0 qJ i tq0 1 qo l [q 0 ° q j

[q0 0 q j -> i lq0 1


qd [qi 0 qj
8(q 0 , 1, 1) = (q0. 1 D r 1 i
6. [q0 q0] —> i [qo 1 qol ho 1 qo]
r 1 i
[qo q0. -* i [qo qj [qi 1 qol
r 1 i
[q0 q>] —* i [qo 1 qol [qo 1 q J
[q0 1 qj 1 [qp 1 q j [q t 1
qj
7. 8(q 0 , 0, 1) = (qi. 6) [qo q j o
8. 5(q P o, 1) = (Q|. e) [qi q j —>o
e
9. 8(qj, 0, °) = > [Qi 0 q j -> o
8(qi, e. zo) = fa)’ £ )
z
r n
10. [Qi qd —» £ ____________ ____'

Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-45 Pushdown Automata (PDA)

Simplification of grammar :

We can rename the variables as given below :

[q 0 Z° q<J - A, [qo q J " B, [q, Z ° q 0] - C, [q, Z ° q,] - D

[q 0 Z° q 0 ] - E, [q 0 ° q,] - F, [q, ° q 0 ] - G, [q, ° q,] - H


1
■ [q 0 q o ] - 1 - [q» 1 q J - J . [qi 1
q o J - K > [qi 1
qil- L

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)

Removing non-generating symbols Terminals are generating

Set of productions after elimination of H -> 0, J -> 0, L 0

non-generating symbols {A, C, D, E, G, 1} is


given below :
S —> B

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 '

Language accepted by the PDA :


The language accepted by the PDA is given by :
L = {0n‘ l m 0 n + m I n, m > 1 } u e
n m n
Example 6.6.12 : Construct a PDA accepting {a b a I m, n > 1} by null store.
From the PDA construct the corresponding CFG.
Solution :
Algorithm :
1. Sequence of a’s should be pushed onto the stack in state q 0 .
8(q 0 ,a,z0 ) = (q0 ,az0 )
5(q 0 ,a,a) = (q0 ,aa)

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

Fig. Ex. 6.6.12 : Transition diagram


Transition table :

5(q O’a ’z o) = (q O’a ’z o)


3(q 0 ,a,a) = (q 0 ,aa)
8(q 0 ,b,a) = (q p a)
8(q p b,a) = (q p a)
8(q p a,a) = (q 2 , £)
8(q 2,a,a) = (q 2 , g)
8(q 2 , e, z 0 ) = (q 2 , e)
The PDA M = ((q 0 ,q p q 2 ),{a,b},{a,b,z0 },8,q 0 ,z0 ,<(>)
PDA to CFG :
Step 1 : Add productions for the start symbol

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

too 20 qj - > a [qo a qj fai 2

[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

[% Qj] a [qfl a qo] [q qj


[q o Z ° q 2l - > a [ q o
a
q| ] [q i Z ° q 2]

tqo l - a f q / q j [q 2 Z ° q 2]
Step 3 : Add productions for 8(q 0 ,a,a) = (q0 ,aa)

[ q o a qol a [ q o a qo] [q/qj

[qo a Qo] a [qo a qil [qi a q0] •

[q o a qol -> a [ q0 a q 2] [q 2 a q0]


lq o a qi] -* a [ q o a qo] [qo a q,]

[qo a q1] -> a [ q / q j [q/q,]

[qo a q j a [ q0 a q2] [q2 a q j

[q o a q2] “> a [ q o a qo] [qo a q 2]

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]

[qo a q 2] -* b[qi a q2l ■


Step 5 : Add productions for 6(q p b,a) = (q p a)

[qi a q o l -» b t q / q o ]

[qi a q j -> b t q q,]

[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

Step 7 : Add productions for S(q2>a,a) = (q2>e)

[q 2 a q 2J -* a

Scanned by CamScanner
V Theory of Comp. Scl. (MU-Som. 5-Comp.) 6-49 ____________ Pushdown Automata (PDA)

Step 8 : Add productions for S(q 2, e,z0 ) = (q 2,e)

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 ]

[qo Z q J —> a [q o a q o] [q 0 Z q d '

[qo Z q d —> a [q o a qi] [ q / q j

Scanned by CamScanner
CamScanner
___________ Pushdown Automata_(PDA

Step 3 : Add productions for 8(q 0 , c, z) = (q 0 , e)

[qo Qo) —> c


Step 4 : Add productions for 5(q 0 , a, a) = (q 0 , aa)

[qo a q 0 ] -» a [q0 a q 0 ] [q 0 a q 0 ]

[qo q 0 ] - -> a [qo a q,] [q, a q 0 ]

Cqo 3 Qi] —» a [q o a q o ] (qo a q 1]

. [q O a qj - > a [q o a q,] [ q / q , ] ' ' .


Step 5 : Add productions for 5(q 0 , a, b) = (q 0 , ab)

[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] > . •<;{.; ' !
- •<.

[Qo a Qi] “ * b [q 0 Qo] [Qo Qi] ' -■

[Qo 3 Qi] ~ b fao Qi]

Step 7 : Add productions for 5(q 0 , b, b) = (q 0> bb)


H b b
[Qo Qo] * b [Qo Qo] [Qo Qo]

[Qo b Qo] [Qo


ff \ nt ■• ' j
.. [q 0 Qi] tQo < Q°] '[Q° 9 i ]

[qok q j —> b [q° qj [Qi Q|] . ;

Step 8 : Add productions for 8(q 0 , c, a) = (q p a)

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

Step 9 : Add productions for 8(q0, c, b) = (q p b)

[qo q o ] - > c [ q i q o]

r b 1 cr
b ,
[q0 q J -* fai qJ

Step 10 : Add productions for 8(q p a, a) = (q p e)

a
fai QiJ “> a

Step 11 : Add productions for 8(q p b, b) = (q p e)

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] ’ - ; :

[q 0 Z q J — b[qo b q 0 ] lq 0 Z q,J '■ s- ’ L • \ 11

[q O Z qiJ b [ qo b q 1 ] [ q / q j j' " ' ’ ' -;

Example 6.6.14 : For the PDA


({q 0 . q j . (0. 1), { Zo, X}, 5, q 0 , z0 , (J) where 8 is
8(q 0 , 1 , Zg) = {(q 0 , xz0)}
8(q 0 , 1 , x) = {(q 0 , xx)}
8(q 0 , 0, x) = { ( q „ x)} .
< ,
8(q 0 , e, z0 ) = {(q 0 , e)}
5(q,, 1. x) = {(q 1 ( e)}
8(qp 0, Zo) = {(q0 , z0)}
Obtain CFG accepted by the above PDA and simplify the CFG and describe
the language it accepts.
Solution:
; §
‘/E
'■

s
s


*

1. Productions due to start symbol


S —> [qo ° qol
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

foo x q0] -> 1


foo x q J [qi x
qo]

foo x qj -» i foo x qol foo x q J

foo q J ~ i foo qJ foi qO


4. 8(q 0, 0, x) = (qj, x) foo x q«] -> ofoi x
qo]

— foo x q J -> ofoi x


qJ
8(q 0 , e, z0) = (q0, e) . .. zn
5. [qo q0] -> e

6. 8(qi, 1, x) = (q„ e) foi x q J —> 1

8(Qp 0, zo) = (Qi» Zo)


z z
7. foi °qo]->°foo °qo]
■ 7 7
foi ° q l ] - * O [ q 0 ° q i ]

We can rename the variables as given below :


Z
7 Zn Zn
[q 0 ° q 0 ] - A, foo “ q J - B . N i qol-C.lq, qj - D
[q0 x qo ] - E, [q0 X q J " F - foi X o] ~ G. foi X q j - H
With the above substitutions, the resulting set of productions can be written as :
S-> AIB

B -> 1EB I 1FD

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

------ A - > 1FC

•’ (i,O.1.AC,F,|{|

Fig. Ex. 6.6.14


Following symbols are non-generating
= (B, D, E, G)
Set of productions after elimination of non-generating symbols :
S-»A
A->1FC
C -» OA 10
F->1FHIOH
H—> 0
3. Unit production S -> A is removed, the resulting set of productions :
S -> 1FC

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 :

For simplicity, we will consider a normalized PDA with following properties:


1. There is a single final state q f
2. It empties the stack before accepting
3. Each transition either pushes a symbol onto the stack or performs a pop operation, but
not both.
A transition,
8(qj, x, b) => (q? c) [pop b and push c]
can be replaced by couple of transitions.
1. 8 (q,, x, b) => 3 (q ttmp , e) .'
2- S (q ttmp , e, e) 8 (qj, c) ■
A new state q Kmp has been introduced.
Similarly, a transition that neither pushes nor pops anything can be replaced with two
transitions : .
1. Transition pushing a dummy stack symbol
2. Transition popping the dummy stack symbol.
. Let us consider a normalized PDA,
N = (Q, S, T, 8, q 0 , z 0 , F), where F = { q f }
• A word w will be accepted by N if it starts in q0 and ends up in q f with an empty stack.
• The language L p> q , where p, q G Q, is defined as consisting of those strings that start at
state p with an empty stack and ends up in q with an empty stack.
Example 6.6.16 : Describe the language L(M) In English for push down automation.
M = (K, S, T, A, S , F)
where, k = {S, F}
F = {F}
S = {a, b)

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.

6.7 Deterministic Pushdown Automata (DPDA)


MU - Dec. 2009, May 2013, May 2011

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)

Example 6.7.1 : Design a DPDA for a binary number divisible by 3.


Solution :
Step 1 : Construction of DFA.
o u

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)

i.e. an accepting state in M becomes a non-acccpting state in M' and a non-accepting


state in M becomes an accepting state in M'.
Proof that M' accepts L' :

Case I : Let us take a string co G L.


On application of co, the machine M will reach a final state.
(q 0 , O), z0 ) 1- (p, -), where p e F.

Since die state of p £ Q-F, it will not be accepted by M'.


Case II : Let us take a string cog E* but co g L. i.e. co e L'.
On application of co, the machine M will reach a non-final state.
(q0 , w, z 0 ) l-£- (r, -), where r g F.
M
Since, the state r G Q-F, it will be accepted by M'.

Example 6.7.4 : Enlist the difference between PDM and FSM.


Solution :
Difference :

1. PDM is more powerful than FSM.


2. PDM has additional memory in the form of a stack. . .
3. PDM can handle CFL but FSM can not handle CFL.

6.8 Application of PDA


MU - D e c . 2012

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

6.9.1 Top-Down Parsing


A top down parser for a given grammar G tries to derive a string through a sequence of
derivations starting with the start symbol.
A top down parser, normally uses leftmost derivation to derive an input string.
• Recursive-descent parser is a general top down parser.
• A recursive descent parser may require backtracking and repeated scan of input string,
working of a recursive descent parser is being explained with the help of an example
Example : Consider the grammar
S —> aXb
X —> ab I b
and the input string abb;
The parse tree of abb can be constructed as given below :
Step 1 : We create a parse tree with single node S. S is the start symbol.
©
Step 2 : We use the first production S —> aXb to expand the node

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.

Fig. 6.9.1 Fig. 6.9.2

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)

• We can write a predictive parser (without backtracking) by modifying the grammar :


1. Left recursion should be eliminated.
2. Apply left factoring to the grammar.
We can easily write a program for a recursive descent parser.
• There must be a function corresponding to each variable.
• A global variable ‘position’ points to the current input character.
• If the current input character matches the terminal in the production then the next input
character is read by advancing the variable ‘position’ to the next location.
Example 6.9.1 : Write a program for recursive descent parser for the following grammar.
E = E + TIT
T=T*FIF
F = (E) I a I b
Solution :
Step 1 : Removing left recursion from the grammar we get :
E = TEj
Ej = + TEj I 8
T = FT t
Ti = * F T 1 l e
F = (E)lalb

Step 2 : The program will have a function for each variable.


Function corresponding to E = TEj
E()
(T();
E t ( );
:
}
Function corresponding to Ej = + T Ej I 8
Ej()
{ if (input [position] == ‘+’)
{ match( );
T();
EjO;

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<);
}
}
... ■■

Function corresponding to F = (E) I a I b


F()
{ ' . . ... . : . ' . • , . .

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)

printf(“\n enter a string


. gets(input);
E( );
if(error== 1 ] ! input [position]! = ‘\0’)
printf(“\n invalid string”);
else
printf(“\n valid string”);

6.9.2 Bottom-Up Parsing

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

• A stack is taken to hold grammar symbols.


• An array is taken to store input string to be parsed.
• Initially, the stack is empty.
a
At each step the parser can take one of the following two steps : ( _ |

1. Reduce : The process of reduction is shown in the Fig. 6.9.3 string (V u


; S ? ~xX:;

If there is a production X a and the string a is stack i


found at the top end of the stack then a should be Top

replaced by X. Fig. 6.9.3


2. If the reduction step cannot be carried out then the next input symbol is shifted on the
stack.
The parser repeats this cycle of shift-reduce until it has detected an error or until the
stack contains the start symbol and the input is empty.

Example 6.9.2 : Consider the grammar


E->E + TIT
T->T*FIF
F —> (E) I id
and the input string Id + id * id. Show the working of the 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

8. E+F * id Reduce by T —> F



9. ' E+T * id Shift

10. E + T* id Shift

n. . E + T * id ■— Reduce by F id

12. E + T*F ' — Reduce by T —> T * F

13. E +T — Reduce by

14. E — Accept

6.10 Multistack DPDA


MU - May 2017

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

Fig. 6.10,1 : A PDA with 2 stacks

A K-stack PDA is a deterministic PDA with K stacks.


It accepts an input like a normal PDA.

A *

Scanned by CamScanner
CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-63 Pushdown Automata (PDA)

• In each move, the multistack machine can :


1. Change to a new stack.
2. Replace the top symbol of each stack with a string of zero or more stack symbols.

6.11 Power of Various Machines


MU ■ Dec. 2012

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.

6.12 University Questions and Answers , . '

Dec. 2005

Q.1 Distinguish between NPDA and DPDA. (Section 6.5) (3 Marks)


Q.2 Design a PDA to accept (bdb)n Cn . (Example 6.4.5) (7 Marks)
Q.3 Design a suitable PDA for even palindrome. (Example 6.5.2) (10 Marks)
Q.4 Write short note on power of PDA. (Section 6.1) (5 Marks)

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

Q.7 Construct PDA accepting


L = { an b man I m, n £ 1} (Example 6.4.10) , (5 Marks)
‘ _ rf. ’
• • ' • ■■ '

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-64 Pushdown Automata (PDA)

■ .. ■.

_
May 2007
________' _______________ _

Q.8 Design a PDA to accept (ab) n (cd) n (Example 6.4.4) (5 Marks)

Q.9 Design a suitable PDA to accept an even palindrome over {a, b}


(Example 6.5.2) (10 Marks)

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

Q. 12 Design a PDA for accepting a language

L = { WcW R I W g {a, b}* } (Example 6.4.11) (10 Marks)

Dec. 2008

Q.13 Prove the variations and equivalence of the push down automata

(Section 6.4) (10 Marks)

Q.14 Convert the following expression grammar to PDA


I _> a I b I la I lb I ID 111
E I I E * E I E + E I (E) (Example 6.6.6) (10 Marks)

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)

Q. 16 Consider the PDA with the following moves :


8(q 0 , a, z0) = {(q0 , az0)}
8(q0 , a, a) = {(q0 , aa)}
6(q0, b, a) = { ( q1t e)}
5(q1, b , a) = {(q1t e)}
5(qi> z0 ) = {(q 1 , e)}
Obtain CFG equivalent to PDA. (Example 6.6.10) (10 Marks)

Dec. 2009

Q. 17 Write short note on DPDA. (Section 6.7) ( 5 Marks)

May 2010

Q. 18 Differentiate Deterministic Push Down Automata and Non-deterministic Push Down


f
Automata. (Section 6.5) < (5 Marks)

Q. 19 Design PDA for the following language


L (M) = { WcWR } W e { a, b }* where W R is reverse of W and C is constant.
(Example 6.4.11) • (10 Marks)

Dec. 2010

Q. 20 Construct a PDA accepting the following language L = {an b m a n I m,n > = 1)


(Example 6.4.10) ■ < (5 Marks)

May 2011

Q. 21 Design a PDA .for the language L = { WcW I W e {a, b}* }


(Example 6.4.1 1 ) (1 ° Marks)
q, 22 Design a PDA for the following grammar and test whether 01 0 4 is in the language
defined by that PDA.
S -» 0BB
B —» OS 11 S I 0 (Example 6.6.4) (1 0 Marks)

Pec- 2011 |

Q. 23 Construct PDA for the following language :


L = {0 m 1 n 0 m t n I m, n s 1} (Example 6.4.14) (8 Marks)

Q, 24 Differentiate between DPDA and NPDA. (Section 6.5) (4 Marks)

Q. 25 Define PDA and construct PDA for the grammar :


E -> E + E IE - E l (E) I id (Section 6.7 and Example 6.4.15) (10 Marks)

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)

Q. 27 Convert the grammar


S->0AA
. A -> OS U S I O
to a PDA that accepts the same language by empty stack.(Example 6.6.4) (10 Marks)

Dec. 2012

Q. 28 Explain PDA and power of PDM. (Sections 6.1 and 6.12) (10 Marks)

Q. 29 Construct a PDA accepting the language of Palindromes.


(Example 6.5.3) c (10 Marks)

Q. 30 Write application of PDA. (Section 6.8) (5 Marks)


■- -‘m 1 ■ fl " ’i ' ’i- ' :
May 2013
1 L V:; v ‘ ' ■: . ’ i .•

Q. 31 Explain DPDA vyith languages of them.


(Sections 6.5 and 6.7) (10 Marks)

Q. 32 Construct the PDA accepting following language


L = {a n bm a n 1m, n > = 1} (Example 6.4.10) ‘ ' (10 Marks)
.. ...... .
May 2014 •L ■ j

Q. 33 Design PDA that checks for well-formed parentheses. (Example 6.4.13) (10 Marks)

Q. 34 Design a PDA to accept language (a n ' 1


b 2n + 1 1n i l } (10 Marks)
Ans. : [Hint ; Refer as Example 6.4.8(A)]
• For every ‘a’ in the input, 2 b ’ s are pushed onto the stack.
• For the first *b’ in the input, 2 b’s are pushed onto the stack.
• For every ‘b’ in the input, 1 ‘ b’ is popped out from the stack. .
• Finally the stack should become empty. . .

Transitions :

6(q 0, a, Zq) = (9o> bb

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-67 Pushdown Automata (PDA)

8(q 0 , a, b)= (q0 , bbb)


5 (q 0, b, Zq) = (q t , bb z0 )
8(q 0 , b, b)= (qp bbb)
SCq b, b)= (q„ e )
8(q„ g.Zq) = (q 1( e )
[Accept using empty stack]
Q. 35 Design PDA to check even palindrome over £ = { 0 , 1 } (10 Marks)
Ans. : [Hint : Refer as Example 6.5.2(A)]
p
An even palindrome will be of the form ww

01 f [ 10 | , t0 1 0| }0 1 Op |Q0| [00 |
A R 4 R AR
ww wTw wTw
Centre Centre Centre

If the length of w is n then a palindrome of even length is :


First n characters are equal to the last n characters in the reverse order.
The character immediately before the middle position will be identical to the character
immediately after the middle position.
Algorithm :

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)

4 n is decided non-deterministically. Every character out of first n characters, whose


previous character is not same as itself should be pushed onto the stack.
- Push the current symbol using the transitions :
3(q 0 , 0, 1 ) => (q Qi o i )
8(q 0 , 1, 0) => (q 0 , 10)
The transition table for the PDA is given below :
z Oz
o) { (%’ o)}
lz
1* Zo) o))
5(q 0 , 0, 0)=> { (q Q, 00) (q p e) )
S(q 0 , 0, 1)=> {(q 0 , 01)} . ' . ' •' ■; ■
8(q 0 , l,0)=> {(q 0 , 10)}
5(q0 , 1, 1)=> {(q0 , 1 1), (q p e)J
5(q p 0, 0)=> {(q p e)} ' . ...
5(q p 1, 1)=> {(q P e)}
5(q p e, Zo) => { (q P £)} [Accept through an empty stack]
Where, the set of states Q = {q0 . <]i}
the set of input symbols Z = {0, 1 }
the set of stack symbols T = {0, 1, z 0 }
Starting state = q 0
Initial stack symbol = Zq

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

Q. 38 Desiqn DPDA to accept language L - (x e {a, b} N a (x) > N b (x)},


N (x) me ans number of a's are greater than number of b's in string x.
a b
(10 Marks)

Ans. • [Hint : Refer Example as Example 6.6.6(A)]


. .being
The PDA is . o aliened to accept the string using final state. The f stack is being used

Scanned by CamScanner
CamScanner
>r Theory of Comp. Sci. (MU-Sem. 5-Comp.) 6-69 Pushdown Automata (PDA)

Transitions

1. 8 (qo, a > Zq) — (q0 . a z<)) [Extra ‘a’ is pushed]


2. 8 (q 0 , b, Zq) = (q 0 , b Zq) [Extra ‘b’ is pushed]
3. 8 (q 0 , a, a) = (q 0> aa) [Excess a’s arc pushed]
4. 8 (q0 , a, b) = (q 0> e ) [Excess b’s decreased by 1]
5. 8 (q0 , b, b) = (q 0 , bb) . [Excess b’s are pushed]
6. 8 ( q 0 , b , a ) = (q 0 , g ) [Excess a’s decreased by 1]
7. • 8 (q 0 , G, a) = (q p e ) [Input ends with excess a’s on the stack]
The PDA is given by :
M
= ({qo-Qil ( a >b), {a,b, z0 },8, q 0 , z 0 , ( q j )

|j| May 2 0 1 6 I

Q. 4(b) Construct PDA accepting the language L = { a 2n b n I n > 0 }. (10 Marks)


Ans. : [Hint : Refer Example as Example 6.4.16]
Algorithm :
rprc " c .. ■. - ’ ‘
1. For every pair of leading a’ s, one X is inserted in the stack. .
2. X’s on the stack are matched with trailing b’s. r

The PDA is given by . j .


M —( {Qq, Qp q 2 » b} {X, Z o ), 8, q 0 , Z o, 0 ) - <
where the transition function 8 is . -
=
1* Qo» Zq) (q p Z 0 )
2. 8(q p a, Z o ) = (q 2 , X Z o ) '‘ ■
3. 8(q 2, a , X ) = (q p X)
4. 8(q p a , X ) = (q 2, XX) . • , • .
5. 8(q 2, b , X ) = (q 3 , e )
6. 8(q 3, b, X) = (q 3 , G)
7. 8(q 3 , G , Zq) — (q 3 , € )
Accept through empty stack.

Q. 39 Design PDA to check even palindrome over L = (0, 1} (Example 6.5.2) (10 Marks)

Dec. 2016 I

Q. 40 Give formal definition of a Push Down Automata. (Section 6.2) (5 Marks)

Scanned by CamScanner
CamScanner
$ Theory of Comp. Sei, (MU-Sem, 5-Comp.) 6-70

Q. 41 Design a PDA corresponding to the grammar : (10 Marks)


S -» aSA I e
A -»bB
B ->b .*

Ans. : [Hint : Please refer Example as Example 6.6.4(A)]


The equivalent PDA, M is given by :
M
= ( { q ) . { a , b ) , [ a , b, S, A, B ) , 8, q, S, <|>)
where 8 is given by :
8 (q, e , S) => { ( q , aSA), (q, 6 ) )
8(q, G , A ) = > { ( q , b B ) }
8 ( q , G , B) = {(q, b)}
8 (q, a, a) = {(q, e ) }
8 (q, b, b) = {(q, G ) }

Q. 42 Write short note on : Deterministic PDA and multistack PDA. -


(Sections 6.7 and 6.10) (5 Marks)

Q. 43 Design a PDA to accept language { a n b I n >= 1 } (10 Marks)

Ans.: [Hint: Refer Example as Example 6.4.17]


1. 8(q 0 , a, Zq) => (q„ aaZg)
2. Sfqp a, a) => (q„ aa)
3. 8(q I( b, a) => .(01, a)
4. 8(q2, b, a) => (qi, e )
5. 8(q2, e , Zo) => (02- £ )
Accept through empty stack.

Q. 44 Design a PDA that checks for well-formed parenthesis.


(Example 6.4.13) (10 Marks)

Q. 45 Write short note on Differentiation between DPDA and NPDA.


(Section 6.5) (5 Marks)

Chapter Ends. . . .
□ □□

Scanned by CamScanner
Turing Machine (TM)
Module 5

Introduction to Turing Machine


MU - Dec. 2010, Dec. 2012, May 2013

Turing machine is an example of computing machine. S o far we have discussed three


types of machines :

1. Finite state machine

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 :

FA < DPDA < NPDA < Post machine = Turing machine

Turing machine is capable of performing computations on inputs and producing a new


result.

fl Read /write head

■ ti , _ ‘ .

Fig. 7.1.1 : An example of a Turing Machine

An abstract model of a turing machine is shown in Fig. 7.1.1.

Input to a turing machine is provided through a long tape.

Turing machine is provided with a read / write head.

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.

• Blank squares hold a special character ‘ I f .

• The head is capable of performing three operations :

1. Reading a symbol being scanned.

2. Modifying a symbol being scanned.

3. Shifting either to previous square (L) or next square (R).

Example 7.1.1 : Design a turing machine to perform the following computation :

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.

(a) Input tape

(b) Output tape


Fig. Ex. 7.1.1 : Input, Output tapes

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 1 : Leftmost 1 of CDj is erased.

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

Fig. Ex. 7.1.1(d) : Transition diagram

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

Meaning of various transitions and their effect on Input tape :

B B 1 1 1 1 B B B 1 1 1 B B
1 1

1/B,R [Replacing First 1


% B B B 1 1 1 B B B 1 1 1 B B
by B and move
right.]
1/1X

BBB 1 1 1 BBB 1 1 1 B B [Replacing 1 with 1 has


no effect. This move will
skip l’s.]

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

1/iX B B B 1 1 1 1 B B 1 1 1 B B [skip 1’s


while moving
left]

[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

Fig. Ex. 7.1.1(e) : Transitions in detail for first cycle

The Formal Definition of Turing Machine


MU - Dec. 2008, May 2013

A Turing machine M is a 7-tuple given by :


M = (Q, Z, T, 8, q 0, B, F)
1
where. ''
Q is finite set of states
2. £ is finite set of input alphabet not containing B.
3. T is a finite set of tape symbols. Tape symbols include B.
4. q 0 G Q is the initial symbol.
5. B e T is a special symbol representing an empty cell.
F Q is the set of final states, final states are also known as halting states.
The transition function 5 is a function from
Q x T t o Q x T x (L,R,N)

Scanned by CamScanner
Theory of Comp. Scl. (MU-Som. 5-Comp.) 7-5 Turing Machine (TM)

A transition in turing machine is written as,


8(q 0 , a) = (q b b, R), which implies, when in state q 0 and scanning symbol a, the machine
will enter state q b it will rewrite a as b and move to the right cell.
A transition 8(q 0 , a) = (q b a, R), implies that the machine will enter state q b it will not
change the symbol being scanned and move to the right cell.
Movement of Read / Write head is given L, R or N
L —> Move to left cell
R —> Move to right cell
N —> Remain in the current cell (No movement)

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»

qj qi Qi <— Halting state


Fig. Ex. 7.2.1(a) : Transition table
The transition function can also be given as transition diagram as shown in
Fig. Ex. 7.2.1(b).

Scanned by CamScanner
7-6
Turing Machine (TM)

a/B,R
b/B,R
I B/BJxT

Fig. Ex. 7.2.1(b) : Transition diagram

A string a over Z is said to be accepted by a turing machine M = (QXr,8,q 0 ,B,F ) if


when the string a is placed on the tape and head is positioned on leftmost cell containing a
non-blank cell, machine is started in state q0, then after a finite number of moves the machine
is in a “Halt-state” e F.
" M
A string is rejected by a TM if the machine enters a state q £ F and scans a symbol x for
which 8(q,x) is not defined.

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

Each cycle will consist of following steps :


Step 1 : Leftmost a is changed to x.
Step 2 : Rightmost b is changed to y.
Step 3 : Head comes back to first a.
These cycles of computations are shown in Fig. Ex. 7.2.2(a).

oa V.| b | b | bTB] | B j: x | a La; b 1 s* Cycle

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

Fig. Ex. 7.2.2(a) s Cycles of computation for a’b 3

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-7 Turing Machine (TM)

The turing machine M is given by :


M = (Q, E, r, 8, q 0 , B , F) a/a,R
b/b,R
Where, a/x,R Ci B/B,L
Q = {qo-q1.q2.q3.q4) ------W 'y/y L W
E = {a, b )
vb/yJL
T = {a,b,B} y/y,N x/x3
b/bJL
q0 = Initial state
a/a,L
B = Blank symbol.
54
F = {q 4 } is a final state or ‘Halt-state’
Halt state
The transition function 8 is given in
Fig. Ex. 7.2.2(b). Fig. Ex. 7.2.2(b) : Transition diagram

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

Fig. Ex. 7.23

7.2.2 Instantaneous Descriptions for Turing Machines


Processing of a string by a turing machine can be shown using the instantaneous
description - An instantaneous description of a turing machine include :
1. The input string at any point of time
Position of head
3. State of the machine
The string a! a2 .... ai _ 1 .. n u gives the snapshot of the machine in which :

1. q is the state of the turing machine.


q a{
2. The head is scanning the symbol
Left sequence Right Sequence
The representation of machine with
Present Symbol being
head scanning the symbol a, and the machine state scanned
in state q is shown in Fig. 7.2.1.
Fig. 7.2.1 : Instantaneous description of TM

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

Fig. Ex. 7.2.4 : TM under consideration

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

|-xaabbbB |-xaabbbB |-xaabbyB


: t J.

|-xa abby B |-x aab by B|-xa abby B

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]

Example 7.2.5 : Let T be the Turing Machine defined by the 5-tuples,


(So , 0, 0, Sp L)
(S o, 1, 0, S o , L)
(S o , B, B, Halt, L)
(Sp 0, 1 , Sq, L) ,
( S p 1 , 1 , S 0 , R)
For each of the following initial tapes, determine the final tape when T halts,
assuming that T begins in initial position. ,
1)110B 2) 0011 B 3) 0101B

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]

Tape after 1 cycle

Tape after 2 nd cycle

Tape after 3 rd cycle

Tape after 4 cycle

Fig. Ex. 7.2.6 ; Cycles of computation

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-1 1 Turing Machine (TM)

Turing machine is shown in Fig. Ex. 7.2.6(a).

0/0J< 0/B,R B/0,L

B/B,L

1/B,R B/1JL

B/BX
v B/B,N

Fig. Ex. 7.2.6(a) : Transition diagram

0 ________1 B
(q o ,O,R) (q 0 ,l,R) (qi.B.L)

ji (q 2,B,R) (qj.B.R) (q 5 ,B,N) '

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

Meaning of various states :


Initial state q 0 is being used to skip the string of 0’s and l ’ s so that the head can be
positioned on last character. ;
On seeing a B (blank), the head takes a left turn and positions itself on the last character.
Machine enters state q P

A path through qj -> q 3 -> q 4 is for right shifting 1.

| l ) | 0 | I | 0 |<. | » | l |K|— 1>.|


s

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)

• A path through q1 —> q 2 > q4 js f or right shifting 0.

<14

Machine halts in state q5.

0 1 --- J
J ----L_

Example 7.2.7 : Design a TM to make a copy of a string over {0,1}

b| 1 | i | 0 | 0 | #

Input

' Output

. Fig. Ex. 7.2.7

Solution : « ’' . I r - . • t ..

• Two copies of the strings are separated by #.


• Machine is described in Fig. Ex. 7.2.7(a).

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

Fig. Ex. 7.2.7(a) : Transition diagram

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

03 (q 3 ,o,L) (q 3 ,l.L) (q 3 #,L) — (q 0 .°. R ) (qo-i.R)


Halting state v * q4 q4
- q4 q4 q4 q4 q4
Fig. Ex. 7.2.7(b) : Transition table
The turing machine M is given by :
M = (Q, S, r, 8, q 0 , B, F)
Where, Q = {qo.q1.q2.q3.q4)
S = {0,1)
T = {0, l , x , y , # , B )
q 0 = Initial state
B = Blank symbol
F = {q 4 }
Working of machine for an input 01 1 is shown in Fig. Ex. 7.2.7(c) :
B011#B|-Bxll#B|-Bxll#B|-Bxll#B|-Bxll#B

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

Example 7.2.8 : Design TM to reverse a string S = [a, b}


Solution :
The working of the TM is being explained with the help of the string aba.
Initial configuration of the tape

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's are erased

The transition diagram of the TM is given in Fig. Ex. 7.2.8.


x/x,R
a/a,R
b/b.R
a/a,R x/x,L
b/b,R
al*
B/a,L
B/B,L
a/a,L
b/b.L

x/B,R x/x,L
a/a.R
b/b,R
. z. \a/a,N x/x,R
b/b,N \

He

Fig. Ex. 7.2.8

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-15 Turing Machine (TM)

Simulation of machine for input abb is shown in Fig. Ex. 7.2.8(a).

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

|-BaxxbbB |-BaxxbbB |-BxxxbbB|-BxxxbbB


T , t T T
31 31 32 32

|-BxxxbbB |-BxxxbbB|-BxxxbbB (-BxxxbbaB


T T T
32 ' 32 32 ’ 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

|-BBxxbbaB |-BBBxbba 1-BBBBbbaB


t 'T ■■-T
35 . 3s Qs

l-BB BBbb aB
t
q6 - Halt
Fig. Ex. 7.2.8(a)

. 2009, 2008, . 2015


Solution :
Algorithm :
1. Locate first a or first b.
2. If it is ‘a’ then locate ‘b’ rewrite them as x.
3. If it is ‘b’ then locate ‘a’ rewrite them as x.
4. Repeat steps from 1 to 3 till every a or b is re-written as x.

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

Fig. Ex. 7.2.9(a) : State transition diagram

a b X B
—>
Qo (QiXR) (q 2 ,X,R) (q 0 ,X,R) (q 4 ,B,N)

qi (qi,a,R) (q 3 ,X,L) (qpX.R)

fe.X.L) (q 2 ,b,R) (q 2 ,X,R) —


q2
qa (q3>a,L) (q3.b,L) (q 3 ,X,L) (q 0 . B >R)
* q4 q4 q4 Halting state
q4 q4
Fig. Ex. 7.2.9(b) : Transition table
The taring machine M is given by :
M = (Q,S,r,5,q 0,B,F)

Where, Q — {Qo’ Qp 92» 03» q 4 )


£ = {a, b }
* T = {a, b , X, B }
q0 = Initial state
B = Blank symbol
=
F {q 4 }
Working of machine for an input abba is shown in Fig. Ex. 7.2.9(c) :
|-BabbaB|-BxbbaB|-BxxbaB|-BxxbaB

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

Fig. Ex. 7.2.9(c)

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)

The Turing machine M is given by :


M = (Q, s, r, 8, q 0 , B, F)
where, Q = {q0, q „ q 2, q3 )
S = {(,)) .
r = ((, ), x, b )
8 is given in Fig. Ex. 7.2.10(a) or Ex. 7.2.10(b)
q 0 = Initial state
B = Blank symbol
F = {q3 }, halting state
Making of the machine for input (()())() is given in Fig. Ex. 7.2.10(c) :

B(()())()B|-B(() ())()B|-B( ()())()


T t ?
qo Qj
|-B((x())()B|-B(xx())()B|-B( xx())()B
q
qo °
|-B(xx ()) ()B|-B (xx(x) ()B|-B( xxxx)()B
f • T ’ ' . T
qo qi ■ qo
|-B ( xx xx) () B|-B(xx xxx( ) B|-B (x xxxx( ) B
T ' . t / ■:
qo 1 P1
|— B ( x x x x x ( ) B | - B ( x x x x x ( ) B | - B ( x x x x x ( ) B

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

Example 7.2.11 : Design a TM that recognizes a string containing aba as a substring.


Solution : This problem can be solved by a DFA. A problem that can be solved by a DFA can
also be solved by a Turing machine.
• The DFA is shown in Fig. Ex. 7.2.1 1(a).
• The equivalent TM is shown in Fig. Ex. 7.2.11(b).
• The head scans the input from left to right without modifying it.

b
a a

Fig. Ex. 7.2.11(a) : DFA for substring aba

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)

5 is given in Fig. Ex. 7.2. 11(b)


q0 = Initial state
B = Blank symbol,
F = {q0 }, Halting state

Example 7.2.12 : Design a TM that replaces every occurrence of abb by baa.


Solution :
• The state q 2 implies that the preceding two characters are ab.
• In input b in state q 2, completes the sequence abb.
• abb is changed to baa by a transition through ,

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

->q 0 (qi,a,R)(qo.b -R ) (q7,B,N)


(q7,B,N)
qi ' (qi.a,R) (q2,b,R)
(q3.a,L) (q7,B,N)
<k (qi,a,R)
— (q4,a,L)
q3
— —•
q4 (qs .b.R)
— —"
q5 (qs>a.R)
— —
q6 (q6.a.R)
* q7 q7 q7 Halting state
q7
Fig. Ex. 7.2.12(b) : State transition table

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem, 5-Comp.) 7-21 Turing MachinejTM)

The Turing machine M is given by


M = (Q, S, T, 8, q 0 , B, F)
where, Q = {q 0, q b q 2, q 3 , q 4, q 5 , q 6 , q 7 }
S = {a, b)
T = (a, b, B)
■8 is given is Fig. Ex. 7.2. 12(a) or (b)
q 0 = initial state
B = blank symbol
F = (q 7 ), Halting state
Working of the machine for input bbabbaabbab is shown in Fig. Ex. 7.2.12(c) :

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|

Example 7.2.13 . Construct Turing Machine that recognizes the language :


L = {x e {0,1)* I x ends in 00 }
Solution . The given language L can have an equivalent DFA. Therefore, we will first design
a DFA and then convert this DFA into a TM.
Step 1 : DFA for the given language L.

1
Fig. Ex. 7.2.13(a) : DFA for L

Step 2 : TM from the DFA. .I / 1, nR


W 0/0, R o/o, R
-w— »—

1/1, R B/B, L

1/1, R
q3jj Halting state

Fig. Ex. 7.2.13(b) : TM from DFA

An input symbol B in either q0 or qj will imply rejection.


The turing machine M is given by :
M = (Q, S, T, 8, q0, B, F)
Where, Q = {cfo, Qn Qsl
S = {0,1}
T = {0,1, B)
8 = Transition function is given in step 2 <
q0 = initial state
B = Blank symbol for the tape
F = {q3 ), 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

Fig. Ex. 7.2.14

The tuning machine M is given by :


M = (Q, Z, T, 8, q0, B, F) y
Where, Q = {q0, q b cfc, q3, q4, qh,}
/ Z = {0,1}
T = {0,1, B}
f
f'r* ; : cs * • . -I •’

8 = Transition function is shown using the transition diagram


B = Blank symbol for the tape
F = {q , halting state
Working of the machine for input 0101101 is shown in Fig. Ex. 7.2.14(a) :
0 1 0 1 1 0 1 B |— 0 1 0 1 1 0 1 |— 0 1 O ' 1 1 O 1 B J - 0 1 0 1 1 0 1
:
t t t t
Qo % . Ai ( Aq

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 :

The solution can be given in two steps :


aa.
, Designing a TM from the DFA.
Step 1 • DFA for strings having aa in it.
a, b

a a

b
Fig. Ex. 7.2.15(a)

Step 2 : TM from the DFA -


b/b. R
a/a, R
po
Halting state '
b/b, R
Fig. Ex. 7.2.15(b)

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

Step 2 : Equivalent TM from the DFA.


x. a/a,R
, (jb/b.R
_
''S B/B,L
Halting
state

The Turing machine M = ({q 0. q i . q 2 ) . { a - b )> (“• b , B } , 8 , q 0 , B, {q2 })


J s
...... /> z z v a

Desi gn a TM which recognizes words


am
iriw.Twn.!,

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

Qi (qi,a,R) (q 2 ,y,R) — — (qi,y,R) — —

q2 — (q 2 ,b,R) (q 3 >z,R) — — (q 2 ,z R) —

93 (q3>a,L) (qAL) — (q 0 .x,R) (q3 >y>L) (q 3 ,z,L) —

q4 — .— — — (q 4 .y.R) (q 4 ,z,R) (q5.B,N)


* q 5 ..
q5 q5 qs q5 . qs qs q5
Hating -— — ... - - -. -
state . .. .•
Fig. Ex. 7.2.17(b) : Transition table

The Turing machine M is given by :


M = (Q, Z, r, 8, q 0 , B , F)
Where, Q = ( q 0 , q„ q 2 , q 3 , q 4 , q 5 )
Z = {a, b, c } ..
r = {a, b, c, X, y, z, B }
8 = The transition is given Fig. Ex. 7.2. 17(a, b)
q 0 = Initial state
B = Blank symbol
F = {q 5 }> Halting state
Algorithm :
For a string a n b nc n, the TM will need n cycles. In each cycle :
1. Leftmost a is written as x
2. Leftmost b is written as y
3. Leftmost c is written as z
At the end of n cycles, the tape should contain only x’s, y’s and z’s .

Scanned
Scanned by
by CamScanner
CamScanner
Theoryof-Comp Sej. (MU-Sem. 5-Cornp.) 7-26 Turing Machine (TM)

Working of the TM for input a 3 b3 c 3 is shown in Fig. Ex. 7.2.17(c) :

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)

Example 7.2.18 : Design a TM which recognizes palindromes over alphabet {a.b}

Solution :
A palindrome can have one of the following forms :
1. coco R
9
R
coaco
R
cobco

Where co is a string over { a,b ) with I co I > 0


Algorithm :

1. Algorithm requires n cycles, where I co I = n.


2. In each cycle, first character is matched with the last character and both are erased.
a/aj<
b/b>R

a/aJR.
b/bjt

a/BJv

B/B,N a/a>L
% b/bX

a/a.R for odd length


b/b,R palindrome
for even
length palindrome a/a>R
b/b,R

B/B,R

Fig. Ex. 7.2.18(a) : Transition diagram

If the leftmost character is ‘a’ the machine takes a path through


q0 ~ 93 9s ” > 9?» looking for last character as ‘a’.
If the leftmost character is ‘b‘, the machine takes a path through

The Turing machine M is given by :


M = (Q, S, r, 8, q 0 , B, F)

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

F = {q8 }, halting state


Working of TM for input abbabba is shown in Fig. Ex. 7.2.18(a) :

B ab b ab b aB |-B B b b ab b aB BB b b abb aB |-B b bab b aB

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

|-Bbb abbaB |-Bbb abbBB |-Bbb abb B |-B bb abb B


C
C5 07 07 7

|-BbbabbBI-B bbabb’B |-Bbb abbB |-BbbabbB


- - t - r- • - r ..... ;- T-
q7 ch \ q? ■? Ao
|-BBbabbBpBbabbB|-BbabbB|-BbabbB

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

' |-B a b B |-B abB |-B ab B|-B aBB |-BaB


q4 q4 -- Os 07 . 07
j-BaBl-BBBl-BBB

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

Fig. Ex. 7.2.19 : TM :

Example 7.2.20 : Design a TM which recognizes words of the form cococo over alphabet {a, b}.
Solution :

Step 1 : Three co’s of cdcdco can be separated as explained below :


1.1 Advance two places from the left. While advancing, each ‘a* is written as x and
‘b’ is written as y.

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)

Locate the last alphabet of cococo and change it to 1 if it is a, otherwise

1.3 Step number 1.1 over 1.2 is performed several times.


Situation of tape for co = abb is shown below.
Step 2 : Fist co of cococo is matched with last (0. While matching, the first co is erased and the
last co is converted to a string of {a,b }.
Situation of the tape is shown below :

Initial x

After 1st cycle y X y

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

B/B3 ' 1/13


x/x3
x/x»L y/y3
a/a,L X/B3 ZM2/23
Step 3: b/b,L y/y-L
y/L,L x/x»L
2/23
B/B,R b/23 1/13

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

7.3 Turing Machines as Computer of Functions


MU - M a y 2012

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

A function f, f : S* -> S* is said to be turing computable if there is a turing machine


M = (Q, S, r, 8, q 0 , B, F) such that „—
■ SB?*-*
Where cog S* and pe S* satisfying ; •
f(co) = p and q F

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

B [Represented by blank string ]


0
00
00000
0 .... n times . . . . 0, or 0 n

Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-33 Turing Machine (TM)

Example 7.3.1 : Design a TM to find 2’s complement of a binary machine.


S o l u t i o n : 2’s complement of a binary number can be found by not changing bits from right
end till the first ‘1’ and then complementing remaining bits. For example, the 2’s complement
of a binary number 0101 101000 is calculated as given below :

.1000
S 1010011000
z

Complement no change
every bit
Algorithm :

1. Locate the last bit (right most)


2. Move towards left till the first 1 .
3. Complement remaining bits, while moving towards left.
1/1,R
1/0 JL
0/0 JI 0/0 J.
B/BJL 4 1/1,L
--------►—

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)

F = {q3 }, Halting state


Working of TM for input 0101101000 is shown in Fig. Ex. 7.3.1(c) :

B01 01101000B|-B0101101000B|-B01 oil 01000B


Qo qo qo
|-B01011 01000B|-B0101101000B|-B0101101000B
T t t
qo qo %
| - B 0 1 0 1 1 0 1 0 0 0 B | - B 0 1 0 11 0 1 0 0 0 B H B 0 1 0 1 1 0 1 0 0 0 B

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?

Fig. Ex. 7.3.1(c)

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*

(a) Transition diagram (b) Transition table


Fig. Ex. 7.3.2

Scanned by CamScanner
V' Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-35 Turing Machine (TM)

The Turing machine M = ( ( q 0 , q l | , (0, I ) , (0, l , B } , 8 , q 0 , B , {q,})

The transition function 6 is given through transition diagram/tablc.

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 ,

Fig. Ex. 7.3.3(a) : Transition diagram

Scanned by CamScanner
CamScanner
Theory of Comp. Sci.(MU. Spm 5-Comp.) 7-36 Turing Machine (TM)

0 # B

-»q 0 (qi-B.R) (q 4 ,B,R)

qi (q b 0,R) (q 1(#,R) (q 2,B,L)



q2 (q 3 ,B,L) (q 5 0,N)
q
3 (q 3 ,0,L) (q 3 ,#,L) (q 0 ,B,R)
q
4 (q 4 ,B,R) _ (q 5 ,B,N)
*
q5 q? qs q5 <-- Halting state

Fig. Ex. 7.3.3(b) : Transition table

The Turing machine M is given by :


M = (Q, S, T, 8, q 0 , B, F)
where,
Q = {q<>, q i - q qa-
z = {0, 1 , # } J
r = (o,i, #,B) i' f
The transition function 8 is given in Fig. Ex. 7.3.3(a) and (b)
q 0 = initial state,
B = blank symbol < . . / r - .-
F = {CJ5}, Halting state >
The working of TM is being simulated for 5-3 is shown in Fig. Ex. 7.3.3(c) :
BOOOOO#OOOBFBBOOOO#OOOB|-BBOOOO#OOOB

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

Fig. Ex. 7.3.3(c) Contd...

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

|-B0 0#BB|-B0 0#B |-B00#B |-B00#B


% 93 93 93
|-B00#B|-BB0#BHBB0#B
T t t
qo qi qi
|-B0#B|-B0#B|-B00B
t t t
91 91 95
Fig. Ex. 7.3.3(c)

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

Fig. Ex. 7.3.4 .

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.

Example 7.3.5 : Design a TM that computes the function


g(x, y ) = x + y, i f . x S y

Simulate working for


(i) x = 2, y = 2 (ii) x = 0, y = 2.
Solution :
Working of the machine is being explained with the help of two examples :
y > x [y = x — 2]

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-39 Turing Machine (TM)

Example 1 (y=5,x = 2) Example 2 (y=2,x = 5)

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

• Initially, head is positioned on the rightmost bit of x.


In each cycle: .■ :
1. Nearest (from head) zero of x is written as #
2. Nearest zero of y is written a s # . .. f .. .
• If the head hits the B(blank) on the left side before B(blank) on the right side then #’s are
erased to perform y - x.
If the head hits the B(blank) on the right side before B(blank) on the left side then #’s are
re-written as 0’s to perform x + y. :

Scanned by CamScanner
CamScanner
jr Theory of Comp Sci. ( M U - S e m , 5-Comp.) 7-40 Turing Machine (TM)

The transition diagram of the TM is shown in Fig. Ex. 7.3.5.


#/#,R
0/#,R J 0/#,R

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)

Example 7.3.6 : Design a TM to compute multiplication of two unary numbers.


EDBE
MU - May 2007
Solution : Multiplication algorithm is being explained with the help of an example.
3 x 5 will require three cycles.

Initial BOOO # 00000 # B

5 Product will be
stored here

Cycle 1 BX00 # 00000 # OOOOOB 1x5 = 5

1x5 = 5

Cycle 2 BXXO # 0 0 0 0 0 # OOQOOOOOOB ] 2x5 = 10

Cycle 3 BXXX# 00000# 00000 00000 00000 B 3x5 = 15

3 x 5 = 15

To calculate 3 x 5 , three times, 5 zero’s are appended.


Unary representation of 3 is 000.
1
Unary representation of 5 is 00000. .
3, 5 and the result, are separated by #.
Inside each major cycles (three cycles for 3), there will be a number of minor cycles
(5 minor cycles for 5) to append 0’s one at a time.
#/#J<
0/0
o/x3
#/#>R 0/x,R

4/0
B/0,L
#/#,N
0/0, L
#/#X

Fig. Ex. 7.3.6 : Transition diagram for TM of example 7.3.6


Let us assume that the two numbers to be multiplied are Xj and x2 .
X! is represented by coa , where CDj is a string of 0’s,
x2 is represented by co2 , where co2 is a string of 0’s.
Xj * x 2 is represented by co3 , where co3 is a string 0’s.
# separates 0 and co2 , co2 and co3 .

Scanned by CamScanner
Theory of Comp. Sei, (MU-Sem. 5-Comp.) 7-42 Turing Machine (TM)

• In the TM shown in Fig. Ex. 7.3.6, there are two cycles.


• The cycle q 0 —> q , —> q 2 —> q 5 —> q 0 appends to2 to <o3 for every zero in (Oj, with the help
of cycle q 2 —> q 3 —> q 4 —> q 2
Working of TM for 2 X 2 is shown in Fig. Ex. 7.3.6(a) :

B00#00#B |-Bx0#00#B |-Bx0#00#B |-BxO#00#B

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

Fig. Ex. 7.3.6(a)

Scanned by CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-44 Turing Machine (TM)

Design TM for n2 where ‘n’ is integer and n £ 0.


E x a m p l e 7.3.7 :
2
Solution . Computation of n is same as multiplication of n with n. Computation of n is being
explained with the help of an example.
Computation of 3 2 :
3 is represented as 000.
B 0OQO
B QO# B# B
Initially the tape will contain | | >

we make a copy of the string | 000 # oooffi .

Now, the multiplication of 3 with 3 will require three cycles as shown in


Fig. Ex. 7.3.7(a).*
Initial B 000 # 000 B

Cycle 1 B x 00 # 0 0 0 # 000

Cycle 2 B xx 0 # 000 # 000000

Cycle 3 B xxx # 000 # 000000000


2 . • •'
i, ■ - • • ' j ' n j
Fig. Ex. 73.7(a)
• The transition diagram of the required TM is shown in Fig. Ex. 7.3.7 (b).

#/#,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 ;

Fig. Ex. 7.3.7(b)

Scanned by CamScanner
Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-45 Turing Machine (TM)

TM for multiplication is given in Example 7.3.6.


TM for copy operation is given in Example 7.2.7.

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

o/ox \ z/z,L B/B,N


0/0, L
#/#,L
0/0>R y/yX
z/y/,R
y/yj<

Fig. Ex. 73.8 : Transition diagrams

Division can be performed through repeated subtractions.


To perform x y, both x and y are represented in unary system, y is divisor and x is
dividend.
y is repeatedly subtracted from x, as long as x > y each time, y is subtracted from x,

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

Working of machine for 5 + 2 is shown in Fig. Ex. 7.3.8(b).


( >C
Qp- Q1- 2— 12 l6~> Cl7

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

Fig. Ex. 7.3.8(c) Contd...

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 .

|-BOx flyyzOO#OB|-BOx flyyzOOflOB -

% ; 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 ;

| - B 0 0 fly y z z 0 # OB |-B 0 0 fly y y z 0 # 0 B


t t
o«' q<
| - B 0 0 fly y y y 0 # 0 B | - B 0 Ofly y y y 0 # 0 B .
,T t
.04 04
l - B O O fly y y yO flO B | - B 0 Ofly y y y O f l O B

04 04

Fig. Ex. 7.3.8(c) Contd

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

t ' ' ' T J


qj qi ; • - .
| - B xO # y y y y 0 # 0 OB I- B xO# y y y y 0 # 0 0 B I- B x 0 # y y y yO # 0 OB

q2 . . ......... ... ... ... %


|— B x O # y y y y O # O O B | - B x O # y y y y z #00B
. ..

|-BxO#yyyyz #00B|-Bx0#yyyyz#00B |-BxO#yyyyz #00 B

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

I- B O x # y y y y z #OOB|-BOx flyyyyz #00B|-B0xyyyyz#00B

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

Fig. Ex. 7.3.8(c)

Example 7.3.9 : Design a TM to increment a binary number by 1 .


Solution : To increment a binary number by 1, following steps are required :
1. Trailing l’s should become 0.
2. First ‘O’ from right end should become 1 .
3. Remaining bits will remain as it is.
1/13
1/Q
0/0,R X

B/1JM

Fig. Ex. 7.3.9(a) : Transition diagram

0 i . B

(q o -O,R) (q 0 .l.R) (qi.B,L)

qi (q 2 .l.N) (qi,0,L) (q 2,l.N)


a
2 qi q2 . qz Halting state
Fig. Ex. 7.3.9(b) : Transition table

State q 0 moves the head to the first blank on the right side.
■»

A transition from q 0 to q p positions the head on the rightmost 0 or 1.

Scanned by CamScanner
Theoryof Comp. Sci. (MU-Sem. 5-Comp.) 7-50 Turing Machine (TM)

Trailing 1 s are written as 0’s in state q P


A transition from q, to q 2 changes the first ‘0’ or a blank from the right end towards left
to 1,
q 2 is halting state.
The working of TM being simulated for 010101 1 1, is shown in Fig. Ex. 7.3.9(c)

B 0 1 0 1 0 1 1 IB |-B 010 1011 IB |-B 010101 1 1 B


19“ t
T>
T
q0

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 )

2. cOj is appended to (O2 .

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

Fig. Ex. 7.3.10(a) : Transition diagram

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

The taring machine M is given by :


M = (Q, S, T, 8, q0 , B, F)
Where . Q = {q0 , q b 03} .5 ■; ;

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 y becomes 0 then x is GCD


Algorithm for finding GCD is given below in C-language :
while(x != 0 && y != 0)
{ if(x >= y)
X - x - y;
else
y = y-x;

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 :

Initially B 000000 0000 B

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

X/X,R y/BX X/0X


Y/YX
0/X,R V X/0X
>— ►
B/BX

0/Y,L

0/0 J,
/b/bx
a® x/xx
Y/YX

B/B,R

------KX
b/bx
XZB3
Y/0,R

The transition diagram of the TM is given in Fig. Ex. 7.3.1 1(a).


The TM can be defined as :
M (Q, S, r, 8, q 0 , B, F)
Where
Q {Qo» 04» 05» q 6 }
S {0}
{0,X, Y,B)
8 Transition function is given in Fig. Ex. 7.3.1 1(a)

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

Fig. Ex. 7.3.11(b) contd....

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

9i ..9i ' " 9i


|-BOxxxxxyyyyB|-BOxxxxxyyyyB|-BOxxxxxyyyyB
t t t
9i 9i . 9i

l-B 0 xxx xxy y y y B l-BO xxx xx y y y y B|-B 0 xxx xx y y y y B


T T ■ T
9i ■■ < 9i * 9j
1-BOxxxxxyyyBl-BOxxxxxyyBl-BOxxx xx y B

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

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

Fig. Ex. 7.3.11(b) contd....

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)

Solution : log2(n) or any numocr n lying oeiween z ana z is given by n.


n+
i.e. if 2” < n < 2 then log 2(n) = n
Let us consider the case of a number
n = 36
25 < 3 6 < 2 6
Therefore, log 2 (36) = 5 , ,
36 can be written as 100100.

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

qi (q],0,R) (qi,0,R) (q 2.B,L)


q* 2 £2 Qi <— Halting state
Fig. Ex. 7.3.12(b) : Transition table

Working of TM for (36) 10 is shown in Fig. Ex. 7.3.12(c) :


(36) 10 = (0100100) 2
B 0 1 0 0 1 00B |-B 1 0 0 1 0 0B I-B00 1 00B
T T ’ , t
q0 '• <io ■ q>
1-B00 100BI-B00100BI-B00000B
T T T.
qi qi qi
l-BOOOOOBl-BOOOOOBl-BOOOOOB
t T t
qi qi <b
Fig. Ex. 7.3.12(c)

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 nx1 n-1 nx(n-1)

2. Copy n - 2, i.e. 1 o#ooo#ooo#oo#oooooo#o


. n nx1 n-1 nx(n-1) n-2
Cycle 3 :

1. Multiplication 0#000#000#00#000000#0 #000000#

n 1xn n-1 nx(n-1) n-2 nx(n-1)(n-2)

#/#,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

Fig. Ex. 7.3.13(b)

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)

7.4 Extension of Turing Machine

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.

7.4.2 A Turing Machine with Multiple Heads


M U - Dec. 2007
A turing machine with single tape can have multiple heads. Let us consider a turing
machine with two heads Hj and H 2. Each head is capable of performing read/write /move
operation independently.
BabaabbaBBB

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

7.4.3 Multi-Tape Turing Machine


M U - Dec. 2007
Multi-Tape turing machine has multiple • tuples with each tape having its own
independent head. Let us consider the case of a two tape turing machine. It is shown in
Fig. 7.4.2.

Tape 2:

Fig. 7.4.2 : A two-tape turing machine

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


S(cji,ai»a2) = (Q2»( i»Mi)»(S2 ,M2 ))
Where,
q t is the current state,
q 2 is the next state,
a i is the symbol under the head on tape 1,

is the symbol under the head on tape 2,

Scanned by CamScanner
CamScanner
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 7-61 Turing Machine (TM)

Sj is the symbol written in the current cell on (ape I ,


S 2 is the symbol written in (he current cell on tape 2,
is the movement (L, R, N) of head on tape I ,
M 2 is the movement (L, R, N) of head on tape 2.

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

Fig. Ex. 7.4.1(a) : Initial configuration

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/x,R) (B/B.N) — (a/x,R) (B/B,N)


--- ---— W ------‘-----
(b/yj<) (B/B.N) (b/y,R)(B/B,N)

(a/B,L)(B/a,R)
--------. ----------
(b/B,L)(B/b,R)

Fig. Ex. 7.4.1(b)

Contents of tapes after step 1 are shown in Fig. Ex. 7.4.1(c)

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)

Step 2 . H 2 i s moved t o lei (most character on tape 2.

(c/c,N) (D/B,R)

(c/c,N) (n/a,L)
(c/c,N) (b/b,L)

<14

<15

Fig. Ex. 7.4.1(d) : Situation after step 2

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

Step 1 : String abb is copied to tape 2.


(a/o,R) (B/a,R)
(b/b,R) (B/b,R)

(B/B.N) (B/B,L)

Contents of tapes after stepl areJ shown in Fig. Ex. 7.4.2(b).


... .. .
Tape 1 B| a | b| b B B
!
»1
Bab b

H2

Fig. Ex. 7.4.2(b) : Contents of tapes after step 1

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)

Contents of tapes after step 2 are shown in Fig. Ex. 7.4.2(c)


1
B
n

a | b] b
i.-.

■■

•; r. ■

H 3
THz
' . •d . Fig. Ex. 7.4.2(c): Contents of tapes after step 2 .

Transition diagram of the turing machine is shown in Fig. Ex. 7.4.2(d)

(a/a,R) (B/aJ.) (B/a JI) (a/a J.)


(b/b,R)(B/b,R) (B/bJt)(b/b,L)

(B/BJJ)(B/B,L)
— — *---------------
(B/B JI) (B/B.N) NS
s
■, »4 *, *' ♦ ■ t jr * * 4\

Fig. Ex. 7.4.2(d) : Transition diagram for Example 7.4.2

CamScanner
Scanned by CamScanner
V Theory of Comp. Sci, (MU-Som. 5-Comp.) 7-64 Turing Machine (TM)

7.4.4 Non-determlnlstlc Turing Machine

Non-deterministic is a powerful feature. A non-deterministic TM machine might have


on certain combinations of state and symbol under the head, more than one possible choice ot
behaviour.
• Non-deterministic does not make a TM more powerful.
• For every non-deterministic TM, there is an equivalent deterministic TM.
• It is easy to design a non-deterministic TM for certain class of problems.
• A string is said to be accepted by a NDTM, if there is at least one sequence of moves
that takes the machine to final state.
• An example of non-deterministic move for a TM is shown in Fig. 7.4.3.
a/aj<

a/x>R

Fig. 7.4.3 : A sample move for NDTM

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

Fig. Ex. 7.4.3(c)

After every x, y e first co is matched, machine moves to state q 6 .


The state q 6 is to establish that every character from the second co of coco has been
matched.
q 7 is the final state.
The final TM is shown in Fig. Ex. 7.4.3(d).
x/x,R
y/y,R
z/zJR
a/x>R. x/x,L x/B,R
b/y,R y/yJL ------

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

7.5 Universal Turing Mach ine _________ ______ — ------


B M U - D e c , 2005, May 2007, Dec.
May 2009, May 2010, Dec. 2011, May.
A genei al -purpose computer can be programmed to solve different types of problems.
TM can also behave like a general-purpose computer. A general purpose computer solves
problem as given below :
1. A program is written in a high level language and its machine-code is obtained wi
help of a compiler.
2. Machine code is loaded in main memory.
3. Input to the program can also be loaded in memory.
4. Program stored in memory is executed line by line. Execution involves reading a line of
code pointed by IP (instruction pointer), decoding the code and executing it.
We can follow a similar approach for a TM. Such, a TM is known as Universal Turing
Machine. Universal Turing Machine (UTM) can solve all sorts of solvable problems.
• A Turing machine M is designed to solve a particular problem p, can be specified as .
1. The initial state q 0 of the TM M.
2. The transition function 8 of M can be specified as given :
If the current state of M is q, and the symbol under the head is a, then the machine
moves to state qj while changing Uj to a,. The move of tape head may be :
1. To-left, . ■
2. To-Right or
3. Neutral
Such a move of TM can be represented by tuple
{(qi j pmf) : q e Q; T ; mf e {To- left, To-Right, Neutral} }
• UTM should be able to simulate every turing machine. Simulation of a Turing will
involve :
1. Encoding behaviour of a particular TM as a program.
2. Execution of the above program by UTM.
• A move of the form can be represented as 10 l+ 1 10 1 itf +1
10 l 10 K,
Where K = 1, if move is to the left
K = 2, if move is to the right
K = 3, if move is ‘no-move*
State q 0 is represented by 0,
State qj is represented by 00,

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.

7.5.1 Church-Turing Hypothesis

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.

7.5.2 Power of Various Machines

M U - D e c . 2015,M a y 2016. D e c . 2016


A post machine can simulate functioning of a turing 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 nbnc n can be handled by a
post machine but it cannot be handled by a PDA.
An NPDA is more powerful than a DPDA. A string of the form O)mR can be handled by

Scanned by CamScanner
(MU-Senri. 5-Comp.) 7-68 Turing Machine (TM)

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.

1VH|Td 2oT2, D e c . 2 0 1 6
Solution :

1. Turing machine is more powerful than FA.


2. FA has no memory; Turing machine has additional memory in the form of a tape.
3. FA cannot modify its input. A TM can modify its own input.
4. FA cannot be used for arithmetic operations. A TM can perform arithmetic operations.
5. A language accepted by finite automata is regular language. It cannot handle context
free language, context sensitive or recursive languages. These languages can be handled
by a TM.

7.6 Linear Bounded Automata ________ ___________________________


-----------------------------? v ._. . • .1 • • ■■ •; . . . . . . .' nl. ~

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.

7.7 University Questions and Answers ______________________


. .

Q. 1 Write short note on : Universal TM (Section 7.5) (5 Marks)

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)

Q. 3 Design turing machine to recognize the language L = ( r 2 n 3 n I n S 1}


(Example 7.2.1 7) (Note : ‘0’ as ‘a’ , ‘1 ’ as ‘b’ and ‘3’ as ‘c’) (10 Marks)

Dec. 2006

Q. 4 Prove the variations and equivalence of Turing machine. (Section 7.4) (10 Marks)

Q. 5 Design TM to accept the language.


L = {x, s, {0, 1}* I x contains equal number of 0’s and 1’s}. Simulate the operation for
the string 110100. (Similar to Example 7.2.9) (Note : 0 as ‘a’ and 1 as ‘b’)(10 Marks)

May 2007

Q. 6 Design a TM to multiply 2 unary numbers. (Example 7.3.6) (12 Marks)

Q. 7 Write note on Universal TM (Section 7.5) (5 Marks)

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)

Q. 10 Write note on ‘Universal Turing Machine (Section 7.5) (5 Marks)

May 2008

Q. 11 Design a Turing Machine which recognizes the language L = {anb n c n I n > 1}.

(Example 7.2.17) (10 Marks)


Q. 12 Write note on ‘Universal turing machine’. (Section 7.5) (5 Marks)

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

Q. 14 What is Turing machine ? Explain different techniques for Turing machine


construction. (Sections 7.2 and 7.4) (10 Marks)

Design a Turing machine to compute nl. (Example 7.3.13) (10 Marks)


Q. 15
Q.16 (3 Marks)
Write detail note on : Universal TM (Section 7.5)

i May 2009?

Write a short note on Universal Turing Machine. (Section 7.5) (5 Marks)


Q. 1 7

Q. 1 8 Construct a Turing machines M to accept the language L = {coccd I we {0, 1} and


coR means reverse of w}. Trace the sequence of moves made corresponding to inpu
string 01 c10. (Similar to Example 7.2.18) (10 Marks)

Q. 1 9 Construct a turing machine M for performing proper subtraction m-n is defined to be m-


n for m > n and zero for m < n. The TM started with 0 10 its tape, halts with 0 on
its tape. Trace the sequence to moves for the input string 0000100.
(Similar to Example 7.3.3) . . Marks)

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)

Q. 22 ■ Write short note on Variants of TM (Section 7.4) (5 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. 28 Design a Turing machine to accept the language L = { an b n I n > = 1 }.


(Example 7.2.2) (10 Marks)

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

Q. 30 Design Turing machine for recognizing the following Language : (5 Marks)


n n n
L = {a b c I n i 1) (Example 7.2.17)

Q. 31 Write short note on : Universal turning machine (Section 7.5) (6 Marks)

May 2012

Q. 32 Design a Turing Machine for the following language.


"set of all the strings of balanced parentheses” (Example 7.2.10) (10 Marks)
Q. 33 Short note on Universal Turing Machine. (Section 7.5) (5 Marks)
Q. 34 Write short on application areas of Turing machine. (Section 7.3) (5 Marks)

Dec. 2012

Q. 35 What is TM ? Give the power of TM over FSM.


(Section 7.1, Example 7.5.1) (10 Marks)
Q. 36 Write short note on Universal TM (Section 7.5) (6 Marks)

Q. 37 Define TM and give its variants. (Sections 7.1 and 7.2)


J * ‘
(5 Marks)
Q. 38
(Example 7.2.2)
(10 Marks)

Q. 39 Design a TM that recognizes palindrome strings where X » {0, 1}

(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

(0,0, L) (0,0, L) (B,1,L) ,


(0,0, L) (B,B,L) (B,1,L)
(B,B,L) (0,0, L) (B.1.L)

(B,B,R) (B,B,I (B,B,R) (B,B,R) (B,1,R)

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

Ans. : [Hint : Refer Example as Example 7.2.17(A)]


a/a,R
b/b,R

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

.(53) Halt state


Fig. 1-Q. 42

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

The taring machine M is given by :


M = (Q, S, T, 8, q 0, B , F)

Where, Q = { q0 > Q4» Q5>


S - = {0, 1} .

r = {0,1, B}
5 = Transition function is shown using the transition diagram

B = Blank symbol for the tape


F = { q5 } , halting state

Q. 44 Construct TM to check wellformed ness of parenthesis. (Example 7.2.10) (10 Marks)

Q. 45 Write short note on : Variants of TM (Section 7.4) (5 Marks)

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 47 Write short note on "Universal Turing machine" (Section 7.5) .. .. (5 Marks)

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

Q, 49 Explain variations a n d equivalence of Turing machine. (Section 7.5.2) ( 1 0 Marks)

Q. 50 Design a Turing machine as an acceptor for the language


n m
{a b l n , m £ 0 and m£ n) • (10 Marks)

Ans. : [Hint : Refer Example as Example 7.2.22]

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,

‘ Q = {qo.q1.q2.Q3.q4) ' ’ ' ’

S = {a, b }

r = {a, b , X , Y, B )
, ;
q0 = initial state

B = Blank symbol'

F = { q4 } : ~

Q. 51 State and explain the power and limitations of a Turning machine.


(Section 7.5.2 and Example 7.5.1) ' (5 Marks)

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)

Q. 53 Design a Turning machine to accept the language 0 n 1n 2n .


(Similar to Example 7.2.17) (10 Marks)

May 2017 |

Q. 54 Design Turning machine to recognize wellformedness of parenthesis.


(10 Marks)
(Example 7.2.10)
(5 Marks)
Q. 55 Write short note on : Variants of turning Machine. (Section 7.4)
(5 Marks)
Q. 56 Write short note on : Church-Turing Thesis. (Section 7.5.1)

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

8.1 Recursive and Recursively Enumerable Language


M U - Dec. 2005, Dec. 2009, Dec. 2010, May 2014 Dec 2014,
188 May 2015, Dec. 2015, May 2016, Dec. 2016. Dec 2017

There is a difference between recursively enumerable (Turing Acceptable) and recursive


(Turing Decidable) language.
• Following statements are equivalent :
1. The language L is Turing acceptable.
2. The language L is recursively enumerable.
• Following statements are equivalent
1. The language L is Turing decidable.
2. The language L is recursive.
3. There is an algorithm for recognizing L.
• Every Turing decidable language is Turing acceptable.
• Every Turing acceptable language need not be Turing decidable.

8.1.1 Turing Acceptable Language

A language L Q X* is said to be a Turing Acceptable language if there is a Turing


machine M which halts on every co g L with an answer ‘YES*. However, if co g L, then M
may not halt.

Turing Decidable Language

A language L c X* is said to be turing being decidable if there is a turing machine M


which always halts on every co e X*. If co g L then M halts, with answer ‘YES*, and if co £ L
then M halts, with answer ‘NO*.

Scanned by CamScanner
CamScanner
Theory of Comp Sci. (MU-Sem. 5-Comp.) 8-2 Undecidability

A set of solutions for any problem defines a language.


A problem P is said to be decidable /solvable if the language L £ S* representing the
problem (set of solutions) is taring decidable.
If P is solvable / decidable then there is an algorithm for recognizing L, representing the
problem. It may be noted that an algorithm terminates on all inputs.
Following statements are equivalent :
1. The language L is Turing decidable.
2. The language L is recursive.
There is an algorithm for recognizing L.
Remarks

Every turing decidable language is turing acceptable.


2. Every turing acceptable language need not be turing decidable.
3. A language L C P many not be turing acceptable and hence not turing decidable. Thus
we cannot design a turing machine / algorithm which halts for every co G L.

Example 8.1.1 : Show that for two recursive languages L1 and L2 , each of the following is
recursive.
•1 ' 1
*~2

Solution :

Let the turing machine Mj decides L 3 and M2 decides L 2.


If a word co e L, then M ( returns “Y” else it returns “N”. Similarly, if a word co e L>
then M 2 returns “Y” else it returns N .
Let us construct a turing machine M3 as shown in Fig. Ex. 8.1.1(a).

coeL*

Fig. Ex. 8.1.1(a) : A turing machine for Lj u L 2

Output of machine M ( is written on the tape of M3.


Output of machine M2 is written on the tape of M3.
The machine M3 returns “Y” as output, if at least one of the outputs of M ( , or of M 2 is

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

Let the turing machine Mj decide Lj. • " ■ ■


Let us construct a turing machine M 5 as shown in Fig. Ex. 8.1.1(c).

coeS
Mi

• Output of machine M r is written on the tape of M 5 .


s
t

• The machine M, returns “ Y” as output, if the output of M, is “N”; otherwise, M s returns


“N”. ‘
It should be clear that M 5 decides l' , As L is turing decidable, after a finite time M t
will halt with answer “Y” or “N”. The machine M, is activated after Mj halts. The machine M 5
halts with answer “Y” if co £ Lj, else M 3 halts with answer “N”.

Scanned by CamScanner
CamScanner
Theory of Comp. S C j. (MU-Sem. 5-Comp.) 8-4 Undecidability

Example 8.1.2 : Show that the language L = {a n b nc n I n > 0} is taring decidable.


Solution :
In order to prove that L is turing decidable, we must construct TM accepting both L and
L . We can construct a 3-tape TM. The construction is given below.
Step 1 . b is copied to tape 2 and c n is copied to tape 3 using the following moves, b s and
c s are erased from the first tape. Initially the three heads H 2 and H 3 are
positioned on the leftmost symbol. **

8(q 0 (B,B,B)) = (q 1,(B,B,B),(R,R,R)) [ Skip the first blank ]


8(q 1 ,(a,B,B)) = (q 1 ,(a,B,B).(R,N,N)) [ skip a’s on tape 1 ]
8( qi ,(b,B,B)) = (q 2 ,(B,b,B),(R,R,N)) [ copy first b to tape 2 ]

8(q 2 ,(b,B,B)) = (q 2 ,(B,b,B),(R,R,N)) [ copy remaining b’s to tape 2 ]

8(q 2 ,(c,B,B)) = (q 3 ,(B,B,c),(R,N,R)) [ copy first c to tape 3 ]


. 8(q3,(c,B,B)) . = (q 3 ,(B,B,c),(R,N,R)) [ copy remaining c’s to tape 3 ]

8(q 3>(B,B,B)) = (q 4 >(B,B,B),(L,N,N))


Step 2 : H j is positioned on the rightmost a
8(q 4 (B,B,B)) = (q 4,(B,B,B),(L,N,N))
8(q 4 ,(a,B,B)) = (q 5 ,(a,B,B),(N,L,L))

H 2 is positioned on the rightmost b and H 3 is positioned on the rightmost c .

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

8.2 Enumerating a Language

A Turing enumerable language can be enumerated by some Turing machine. To


enumerate a language means to list the elements one at a time.
A Turing machine enumerating a language
• A Turing machine M can be made to list elements of a language L.
• A multi-tape Turing machine, with K-tapes can be considered for this purpose. Tape 1
can be reserved exclusively for output.
• Let M be a K-tape Turing machine with K > 1 and L c S*. M is made to operate in such
a fashion so that the following conditions are satisfied :
1. The tape head on first tape moves only in the forward direction replacing blank
symbols with valid strings to; G L.
2. For every tOjCL, there is some point in the operation of M when tape 1 has
following contents :
CDj # 0 ) 2 #....# (Dn # ( D #

Where, the strings . .con are distinct strings in L.


(a) If L is finite than nothing is printed after the # following the last word.
(b) If L is finite than machine can either halt or continue to loop forever after
the last word has appeared on the tape.
(c) If L is infinite, M continues to move forever.
The machine M cannot hope to finish its computation on each string co, before beginning
to work on the next. If co L then M may loop forever. The solution for this is given below :
Instead of completing the computation on each string as it is generated, M carries out the
following sequence of operations :
1. Wj+ 1 is computed from cd, where,cd, + i = • S for each alphabet in S.
2. If to. + 1 takes the machine M to an accepting state than (Di + 1 is written to tape 1.
3. Step 1 and step 2 are carried out infinitely if L is infinite.

8.2.1 Finite and Infinite Sets


Number of elements in a set is also known as its cardinality.
• If A G B, then the size of A is less than or equal to that of B.
• Two sets A and B are said to be equinumerous (having same number of elements) if
there is a bijection.
f : A —> B [bijection means one-to-one and onto]
• a finite set has finite number of elements.

Scanned by CamScanner
wSf
V Theory of Comp. Sci. (MU-Sem. 5-Comp.) 8-7 Undecidability

• A set is infinite if it is not finite.


• The set N of natural numbers is infinite.
• A set S is said to be countably infinite if there is a bijection from N to S.
f : N —> S is a bijection.
• To prove that a set S is countably infinite, we must show a bijection between some
countably infinite set and the set S.

Example 8.2.1 : Show that any subset of a countable set is countable.


Solution :
Let A be a given countably infinite set.
Let S be an infinite subset of the set A.
Obviously, there exists a bijection between the set N of natural numbers and the set A.
f : N —> A, f(n) = x for xe A is a bijective relation.
Elements of the set A can be arranged as :
f(l), f(2), f(3)....
Now, we can delete from the set A, those elements which are not present in S. The
remaining elements in A must be infinite. Let us denote these elements by :
f(ii),f(i2 )...... ;
Now, we can define a function
g : N —> S such that g(n) = f(in)
Thus, g is one-to-one and onto.
Hence, S is countable.

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

The sequence m p m2, m3 .... is a recursive enumeration of a subset of S without


repetition in order of magnitude. This subset is infinite and recursive.

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

It is given that the set T is countable.


T = { X1 , x 2 ..-..xra }
Since, the set S is uncountable, we can always find a word y such that
ye S , y e S-T and yg T
Hence, a contradiction.
Therefore, S-T is uncountable.

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

• Transitions of a Turing machine can be represented by a binary number. If (0, represent a


Turing machine then the set of all Turing machines.
T = {cob co2 , }
Obviously, there exists a bijection between the set N of natural numbers and the set T.
f : N —>T, f(n) = co for cog T is a bijective relation
A recursively enumerable language L can be accepted by some Turing machine. For
each L, let g(L) be such a Turing machine. Since every TM accepts only one language, the
relation g is one-to-one.
RE is subset of T.
Since, T is countable, RE is also countable.
Now, that we have shown the set of recursively enumerable language to be countable-
proving that there are uncountable many languages which are not recursively enumerable

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

cok will differ from con at the n lh place.


Therefore, the set S is uncountable.
Hence, proved by contradiction.

Scanned by CamScanner
V Theory of Comp. Sei, (MU-Sem, 5-Comp.) 8-10 Undecidability

— Compare Type 0, Type 1, Type 2 and Type 3 Grammars


HUSH
N..nw.,fLa„ s„aBt, Restriction on Accepting Machine
MP""*! ___ Productions • : . '■■■/■ ■-

0 Recursively <X| —> a 2 where Turing machine


enumerable <X|, a 2 g (VuT)*
1 Context sensitive GCj —> a 2 where Turing machine with bounded
a b a 2 e (Vuiy tape. Length of the tape in
and locj < loc2l finite
2 Context free X —> dj where X e V and PDA
a t e (VuT)*

3 Regular X -> aY 1 a 1 £ 1 Ya FA
where,
X,YeVandae T

A general hierarchy of various languages is given below :

Fig. 8.3.1 : Hierarchy of languages

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.

2. A class of problems is un-solvable.


How to prove that a given language is un-decidable ?
A language can be proved to be un-decidable through a method of reduction. We have
already seen that the halting problem is un-decidable.
• To show that a problem A is un-decidable, we must reduce another problem that is
known to be un-decidable to A.
• Having proved that the halting problem is un-decidable, we can use problem reduction to
show that other problems are un-decidable.
First of all, we will provide proof for the un-decidability of some standard problems.
Subsequently, these problems will be used to show that other problems are un-decidable. Some
standard un-decidable problems are :
1. Halting problem of a Turing machine.
2. Diagonalization language.
3. The post correspondence problem.
4. The universal language.

8.4.1 Halting Problem of a Turing Machine


M ec 006 ec 007 a 008, Dec. 2008, May 2011, Dec. 2011
Pec. 2015, Dec. 2016, May 2017
The halting problem of a Turing machine states :
Given a Turing machine M and an input co to the machine M, determine if the
machine M will eventually halt when it is given input co.
Halting problem of a Turing machine is unsolvable.
Proof :
Moves of a turing machine can be represented using a binary number. Thus, a Turing
machine can be represented using a string over S*(0,l). This concept has already been
explained in the chapter.
Insolvability of halting problem of a Turing machine can be proved through the method
of contradiction.

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.

-M-U u "T] — ►halt — ► Machine H 3 loops forever


l_» — ►loop — ► Machine H 3 halts

H 3 will do the opposite of the output of H 2 .


Step 4 : Let us give H3 itself as inputs to H 3 .

If H 3 halts on H 3 as input then H3 would loop (that is how we constructed it).


If H 3 loops forever on H 3 as input H 3 halts (that is how we constructed it).
In either case, the result is wiong.
Hence,
H 3 does not exist.
If H 3 does not exist than H2 does not exist.
If H 2 does not exist than Hj does not exist. ,

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

Thus, the PCP has solution.


We are accepting the un-decidability of post correspondence problem without proof.

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

Does PCP with following two list :


A = (10, 011, 101) and B = (101, 11, 011) have you!
answer. MU - May 2009 I

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.

MU - May 2006, Dec. 2007, Dec. 2008

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

Let us consider two sequences of strings over E.


A = {u h u2 , u3 . . . u m )
B = { v i , v 2, v 3 . . . v m )
Let us take a new set of symbols a h a 2 . . . a m such that
{a h a 2 . . . a m ) n E = 0.
Symbols a b a 2 . . . ani are being taken as index symbols. The index symbol a, represents a
choice of Uj from A and Vj from the list B.
A string of the form Uj Uj u k . . . a k aj a it Over alphabet E u { a b . . . a m } can be defined
using the set of productions :

GA = ( A - > U I A a ' l u 2 A a 2 l - l u mAam'.


[ u1 a 1 l u 2 a 2 l . . . l u m a m
Similarly a string of the form v, Vj v k . . . a j aj over alphabet Z u { a . . . a,,,} can be
defined using the set of productions :
Aa
GB = i ’ A a J . . . l v Aam
[ v 1 a 1 l v 2 a 2 l . . . l v m a mm
Finally, we can combine the languages and grammars of two lists to form a grammar
Gab • '
• A new start symbol S is added to G AB
• Two new productions are added to G AB
S -> A
S —> B
• All productions of G A and G B are taken.
Now, we will show that G AB is ambiguous if and only if an instance (A, B) of PCP has a
solution.
Assumption :

Suppose the sequence i „ i 2 , . . . , i m is a solution to this instance of PCP. Two derivations


for the above string in G AB is :
S => A => u A => Ujj u i2 A a h a12 => . . . =>
U U a
U »2 2 im
S => B => vj, B aj, => Vj, Vj2 B aj, aj2 => . . . =>
V
«1 V i2 • " V
ira 2 a
im

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

The decidability of L ) = L(r) will imply :


1. For every co, in L(r), (Dj is in L(Mj) is Turing decidable.

2. For every co, in L(r) , C0j is in L(Mj) is Turing decidable.

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.

8.4.4 Greibach Theorem


MU - May 2006, Dec. 2007, May 2011

The Theorem states that :

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

8.5 Partial Recursive Function

Turing machine is viewed as a mathematical model of partial recursive function.


Some basic concepts :
1. Total function :

A total function from x to y assigns a unique element of y to every element of x.


f,(x) = 2x is a total function

Scanned by CamScanner
CamScanner
Theory of Comp. Sci, (MU-Sem. 5-Comp.) 8-19 Undecidability

2. Partial function :

A partial function from x to y assigns at-most one element of y to every element of x.


f 2(x) = + y/x. is a partial function.
f 2(x) is not defined if x is a negative real number.
3. Function of K-variables :
A function of K-variables is represented as f(x b x2...x k).
f(x b x2) = x t + 2x2 is a function of two variables.

4. Primitive recursive 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.

Example 8.5.1 : Show that f(x, y) = x + y is primitive recursive.


Solution :
f(x, 0) = x + 0 = x = uj (x)
Only element x is selected from the tape.
f(x, y + 1) = f (x,y) + 1 = S (f (x,y))
= S [ u ’ ( x , y, f(x, y))]

/ Select the third element

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

Q. 1 Write short note on : Recursively Enumerable Languages. (Section 8.1) (5 Marks)

May 2006

Q.2 Prove that it is un-decidable whether a context free grammar is ambiguous.


(Example 8.4.4) (10 Marks)
Q. 3 Write short notes on post correspondence problem and Greibach Theorem.
(Sections 8.4.2 and 8.4.4) (8 Marks)

Dec.2006

Q.4 Write short notes on :


(a) Halting problem (Section 8.4.1)
(b) Post’s correspondence problem. (Section 8.4.2) (10 Marks)

May 2007

Q.5 Write note on : Post correspondence problem. (Section 8.4.2) (5 Marks)

•; • -:v' ..

Q.6 Prove that is un-decidable whether Context Free Grammar is ambiguous.


(Example 8.4.4) (10 Marks)

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. 10 Prove that it is un-decidable whether a Context Free Grammer is ambiguous.


(Example 8.4.4) (10 Marks)

Q. 11 Write detail notes on :


(a) Post correspondence problem (Section 8.4.2)
(b) Halting problem (Section 8.4.1) - (7 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.z13 . Write short note on :Recursive and recursively enumerated languages.


(Section 8.1) , ( r . (5 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

Q.16 Write short notes on :


(a) Halting problem (Section 8.4.1)
(b) Post Correspondence Problem (Section 8.4.2)
(c) Greibach Theorem. (Section 8.4.4) (15 Marks)

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

Q. 1 8 Short note on : Post Correspondence Problem. (Section 8.4.2) (5 Marks)

Dec. 2012

Q. 1 9 Write short note on : Rice Theorem (Section 8.4.3) (5 Marks)

MSZM13

Q. 20 Define the following terms : Undecidability (Section 8.4) (2 Marks)

Q. 21 Write short notes on : Rice’s theorem. (Section 8.4.3) (5 Marks)

Q. 22 Write short notes on :


(c) Rice’s Theorem (Section 8.4.3)
(d) Recursive and Recursively enumerable languages. (Section 8.1) (10 Marks)

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)

Q. 29 Explain post correspondence problem. (Section 8.4.2) (5 Marks)

Q. 30 Compare recursive and recursively enumerable languages. (Section 8.1) (5 Marks)

Q. 31 State and prove Rice's theorem. (Section 8.4.3) (1 0 Marks)

Dec 2016
-
Q. 32 Write detailed note on : Recursive and Recursively Enumerable Languages.
(Section 8.1) e (10 Marks)

Q. 33 Write detailed note on : Rice’s Theorem. (Section 8.4.3) (10 Marks)


Q. 34 Write detailed note on : Halting problem. (Section 8.4.1) (10 Marks)

Q. 35 Explain Rice’s Theorem. (Section 8.4.3) (5 Marks)


Q. 36 Write short note on : Halting Problem. (Section 8.4.1) . (5 Marks)

Q. 37 Explain Recursive and Recursively enumerable languages.


(Section 8.1) (5 Marks)
Q. 38 Write short note on Rices’ Theorem. (Section 8.4.3) (5 Marks)

Chapter Ends.,.
□□□

Scanned by CamScanner
Appendix A

Solved University Question Paper of Dec. 2018

Theory of Computer
Science

R.E. can be written from the transition graph. The


Dec. 2018
required R. E. = 1 (1 + 01)*

Q. 1(a) Explain Chomsky Hierarchy Q. 1(d) Explain Halting Problem.


(Section 8.4.1) (5 Marks)
(Section 1.3) (5 Marks)
Q. 2(a) Design a Finite State machine to determine whether
Q. 1(b) Differentiate between PDA and NPDA. (5 Marks)
- ternary number (base 3) is divisible 5.
Ans. : i
(Example 2.2.25) (10 Marks)
PDA NPOA
Q. 2(b) Give and explain formal definition of Pumping
1. Always a single move on Multiple moves are
Lemma for Regular Language and prove that
a new input. possible o n a new input.
following language is not regular.
2. Less powerful than More powerful than a L = {am b m “ 1 l m > 0} (10 Marks)
NPDA. PDA.
Ans. :
. 3. Algorithms related to Algorithms related to
PDA are simple. NPDA are complex. Stepl: Let us assume that the given language
n n 1
L(a b “ I n > 0} is regular and L is accepted by
4. Algorithms related to Algorithms related to
a FA with n states.
PDA do not require NPDA require
backtracking. backtracking. Step 2 : Let us choose a string

Q. 1(c) Define Regular Expression and give regular co = an b n-1

expression for : | co | = 2n ~ 1 £ n for n > 0


(i) Set of all strings over { 0 , 1 } that end with 1 has Let us write co as xyz, with
no substring 00 (Section 3.1) (5 Marks)
|y|>0
Ans. :
and | xy | £ n
The transition graph Is shown In Fig. 1-Q. 1(c) :
since, | xy | £ n, y must be of the form ar | r > 0.

since | xy | £ n, x must be of the form as.

Now, an b n ” 1
can be written as
• r
a a

Fig. 1-Q. 2(b)

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]

= -> ibtaeibta [using C - > b ]


1- S(qo> a, Zo) (9t> %o)
(iii) Parse tree as shown i n Fig. 1-Q. 3(b)
2. 6(q b a,Zo) = (qo>xZo) .....

3. 6(q 0, a , x ) = (q b x) ' '*'• ■ ;’>

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

For the string 'ibtaelbta' find the following :

(I) Leftmost derivation

(ii) Rightmost derivation

(iii) Parse tree

(Iv) Check if above grammar is ambiguous.

Fig. 2-Q. 3(b)

Scanned by CamScanner
Appendix

Q. 4(a) Construct TM to check wellfomiedness of Ans. :


(10 Marks)
R. E.toNFA

q. 4(b) Convert following CFG to CNF (10 Marks)


0,1
S -> ASA I Ab

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)

S -> AS I SA I ASA I aB I a NFA to DFA using direct method

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)

4. Finding an equivalent grammar in CNF. 2. Moore Machine


S -> AS I SA |AC2 | c t B l a [Replacing SA by C2] qvi
C2 -+ SA SI-
A -+ b I AS I SA I AC2 I CiB I a
B -> b

Ci —> a

Q. 5(a) Convert (0 + 1) (10)*(0 + 1) Into NFA with e-moves


and obtain DFA. (10 Marks)

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

Semester V - Computer Engineering


Computer Networks
J. S. Katre
Database Management System
Mahesh Mali

Microprocessor
Harish G. Narula
Theory of Computer Science
Dilip Kumar Sultania

Multimedia System (Dept. Elective I)


Tulsiram Sule, Poonam Kadam

Advance Operating System (Dept. Elective I)


Rajesh D. Kadu

coming soon

now with TechKnowled


easy-solutions Pu b I i c a t i o

Head Office
ISBN : 978-93-89. 9-95-3
lony, Aranyeshwar Corner,
91-20-24221234, 91-20-24225678

Student’s Agencies (I ) Pvt . Ltd


89389 H2 9 9 9
■T. : (022) 40496161, M 1 672 90777
vi _ S— _ _ _ _’ _____________ Price ? 4 5 5 /
rwwwwrwwi M046A

like us at:
o®oo©o
Tech knowledgepublications

Scanned by CamScanner

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy