MC0082 - Theory of Computer science

235
MC 0082 Theory of Computer Science Contents Unit 1 Mathematical Fundamentals for Computer Science 1 Unit 2 Proof Techniques 33 Unit 3 Trees and Binary Trees 46 Unit 4 Grammar and Formal Languages 65 Unit 5 Deterministic Finite Automata (DFA) 85 Unit 6 Nondeterministic Finite Automata (NFA) 108 Unit 7 Regular Expressions and Regular Languages 126 Unit 8 Regular Languages Properties and Pumping Lemma 145 Unit 9 Context Free Grammars 160 Unit 10 Pushdown Automata (PDA) 176 Unit 11 Context Free Languages - Properties 199 Unit 12 Turing Machines Construction 212 Edition: Fall 2007 BKID B0970 10 th Dec. 2008

description

Ebook - SMU MCA Sem 5

Transcript of MC0082 - Theory of Computer science

Page 1: MC0082 - Theory of Computer science

MC 0082

Theory of Computer Science

Contents

Unit 1

Mathematical Fundamentals for Computer Science 1

Unit 2

Proof Techniques 33

Unit 3

Trees and Binary Trees 46

Unit 4

Grammar and Formal Languages 65

Unit 5

Deterministic Finite Automata (DFA) 85

Unit 6

Nondeterministic Finite Automata (NFA) 108

Unit 7

Regular Expressions and Regular Languages 126

Unit 8

Regular Languages – Properties and Pumping Lemma 145

Unit 9

Context Free Grammars 160

Unit 10

Pushdown Automata (PDA) 176

Unit 11

Context Free Languages - Properties 199

Unit 12

Turing Machines – Construction 212

Edition: Fall 2007

BKID – B0970 10th

Dec. 2008

Page 2: MC0082 - Theory of Computer science

Department: Information Technology

Program: MCA

Prof. V. B. Nanda Gopal

Director & Dean,

Directorate of Distance Education

Sikkim Manipal University of Health, Medical & Technological Sciences

Board of Studies

1. Name Dr. U. B. Pavanaja

Designation General Manager – Academics

Organisation / Institution Manipal Universal Learning Pvt. Ltd.

Location Bangalore

2. Name Prof. Bhushan Patwardhan

Designation Chief Academics

Organisation / Institution Manipal Education

Location Bangalore

3. Name Dr. Harishchandra Hebbar

Designation Director

Organisation / Institution Manipal Centre for Information Sciences

Location Manipal

4. Name Dr. N. V. Subba Reddy

Designation Head of Department, Computer Science and Engineering

Organisation / Institution Manipal Institute of Technology

Location Manipal

5. Name Dr. Ashok Hegde

Designation Vice President

Organisation / Institution MindTree Consulting Ltd.

Location Bangalore

6. Name Dr. Ramprasad Varadachar

Designation Director, Computer Studies

Organisation / Institution Dayanand Sagar College of Engineering

Location Bangalore

Page 3: MC0082 - Theory of Computer science

7. Name Nirmal Kumar Nigam

Designation Head of Program, Information Technology

Organisation / Institution Sikkim Manipal University

Location Manipal

8. Name Dr. A. Kumaran

Designation Research Manager, Multilingual Research

Organisation / Institution Microsoft Research Labs India

Location Bangalore

9. Name Ravindranath P. S.

Designation Director, Quality

Organisation / Institution Yahoo India

Location Bangalore

10. Name Dr. Ashok Kallarakkal

Designation VP

Organisation / Institution IBM India

Location Bangalore

11. Name H. Hiriyannaiah

Designation Group Manager

Organisation / Institution EDS Mphasis

Location Bangalore

Page 4: MC0082 - Theory of Computer science

Program (s) : MCA

Subject (s) : Theory of Computer Science

Subject Code (s) : MC 0082

Content Preparation Team

Content writing / compilation

1. Name Dr. Kuncham Syam Prasad

Designation Associate Professor

Organisation / Institution Department of Mathematics, MIT

Location Manipal

Content Editing

1. Name Mr. Deepak Shetty

Designation Assistant Professor (Mathematics)

Organisation / Institution SMU

Location Manipal

Edition: Fall 2007

This book is distance education module comprising written and collated learning material for our students.

All rights reserved. No part of this work may be reproduced in any form by any means without permission in writing from Sikkim Manipal University of Health, Medical and Technological Sciences, Gangtok, Sikkim.

Printed and Published on behalf of Sikkim Manipal University of Health, Medical and Technological Sciences, Gangtok, Sikkim by Mr. Rajkumar Mascreen, GM, Manipal Universal Learning Pvt. Ltd., Manipal – 576 104. Printed at Manipal Press Limited, Manipal.

Page 5: MC0082 - Theory of Computer science

Theory of Computer Science is a subject which has to be mastered by

every computer professional. It has its applications in all subjects related to

computer science.

Unit 1: In this unit, we study all the mathematical fundamentals required

for proper understanding the concepts in computer science.

Unit 2: This unit discusses the various methods of proofs with examples.

The techniques discussed here give us an idea to analyze and

solve the problems.

Unit 3: In this unit, we discuss the concept of graph theory, which is used

by many mathematicians and engineers. It has many applications

in all developing areas. It serves as a mathematical model for any

system involving a binary relation.

Unit 4: This unit deals with the concept of grammar and formal

languages. The theory of former languages involves the study of

language syntax.

Unit 5: This unit studies about finite Automata and its application to the

design of several common types of computer algorithms and

programs.

Unit 6: In this unit, we study about a point of time at which we cannot

determine exactly in which state the machine will be. This is called

as Nondeterministic Finite Automata.

Unit 7: In this unit, we learn about regular expressions along with finite

automata, which act as a device for computing regular

expressions.

SUBJECT INTRODUCTION

Page 6: MC0082 - Theory of Computer science

Unit 8: This unit deals with closure properties of regular languages. The

different closure properties are covered in detail in this unit with

properly illustrated examples.

Unit 9: In this unit, we study the context free grammar and develop

mathematical expressions. These are useful for generating

algorithms.

Unit 10: In this unit, we learn the basics of pushdown automata. It is

defined as finite state Automata which is equipped with memory

device for storing symbols.

Unit 11: In this unit, we learn the properties of context free languages.

Using Pumping Lemma we verify that certain languages are not

context free.

Unit 12: This unit deals with the construction of turing machines and the

language accepted by turing machine. It is a generalized machine

which can recognize all types of languages.

Page 7: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 1

Unit 1 Mathematical Fundamentals

for Computer Science

Structure

1.1 Introduction

Objectives

1.2 Sets and Properties

1.3 Functions and Relations

1.4 Equivalence Relations

1.5 Recursive Functions

1.6 Strings and Growth Functions

1.7 Summary

1.8 Terminal Questions

1.9 Answers

1.1 Introduction

The idea of set is fundamental in the study of mathematical structures. All

most all mathematical objects of can be defined in terms of sets and the

language of set theory is used in every mathematical subject. Also the

concept of relation and function arises when we consider a pair of objects

and compare each other. In this unit we study the different relations on a set

and the algebraic structures with single binary operation. In computer

programming, recursion plays an important role. It is an important facility in

many programming languages. We have discussed the concepts recursive

functions and growth functions which are useful in analysis of algorithms

and approximating the time complexity.

The content of this unit provides a mathematical tool for the later units.

Page 8: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 2

Objectives:

At the end of the unit you would be able to

understand the fundamental idea of certain mathematical concepts.

learn the various operations on sets.

understand relations and types of relations

know the recursive functions and growth functions.

1.2 Sets and Properties

1.2.1 Definition

A set is a collection of objects in which we can say whether a given object is

in the collection. Sets will be denoted by capital letters.

The fact that a is a member of a set A is denoted by a A and we call it as

„a belongs to A‟. The members of a set are called elements.

If x is not an element of A then we write x A.

1.2.2 Note

There are five ways used to describe a set:

i) Describe a set by describing the properties of the members of the set.

ii) Describe a set by listing its elements.

iii) Describe a set A by its characteristic function, defined as

A(x) = 1 if x A

= 0 if x A

for all x in U, where U is the universal set, some times called the

“universe of discourse ,” or just universe.

iv) Describe a set by recursive formula. This is to give one or more

elements of the set and a rule by which the rest of the elements of the

set may be generated.

v) Describe a set by an operation (say union, intersection, complement

etc) on some other sets.

puru
Highlight
puru
Highlight
Page 9: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 3

1.2.3 Example

Describe the set containing all the non-negative integers less than or equal

to 4.

Let X denote the set. Then X can be described in the following ways:

i) X = {x / x is a non-negative integer less than or equal to 4}

and N = {x / x is a natural number} = {1, 2, 3, 4, …} is an infinite set.

ii) X = {0, 1, 2, 3, 4}.

iii) A(x) = 1 for x = 0, 1, 2, 3, 4

= 0 if x A.

iv) X = {xi + 1 = xi + 1, i = 0, 1, 2, 3 where x0 = 0}.

1.2.4 Definition

Suppose A and B are two sets. Then we say that A is a subset of B (written

as A B) if every element of A is also an element of B. A set A is said to be

a proper subset of B if there exists an element of B which is not an element

of A. Then A is a proper subset of B if A B and A B.

1.2.5 Note

The containment of sets has the following properties: Let X, Y and Z be

sets.

i) X X.

ii) If X Y and Y Z, then X Z.

iii) If X Y and Y Z, then X Z.

1.2.6 Example

i) Let N, Z, Q, R denote the set of natural numbers; the set of integers;

the set of rational numbers; the set of real numbers respectively. Then

RQZN .

ii) If A = {1, 3, 5}, B = {1, 3, 5, 7} then A is a proper subset of B.

puru
Highlight
Page 10: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 4

1.2.7 Definition

Two sets A and B are said to be equal (denoted by A = B) if A is a subset of

B, and B is a subset of A.

Observation: To show two sets A and B are equal, we must show that each

element of A is also an element of B, and conversely.

1.2.8 Definitions

i) The set that contains no members is called the empty set and it is

denoted by .

ii) A set which contains a single element is called singleton set. X = {2} is

a singleton set.

iii) If A and B are two sets, then the set {x / x A or x B} is denoted by

A B and we call it as the union of A and B.

iv) The set {x / x A and x B} is denoted by A B and we call it as the

intersection of A and B.

1.2.9 Properties

S. No. Property Union Intersection

1 Idempotent A A = A A A = A

2 Commutative A B = B A A B = B A

3 Associative A(B C) = (A B) C A(BC) = (AB) C

1.2.10 Definition

Let X and Y be two sets. The symmetrical difference of X and Y is defined

as {x/ x X, or x Y, but not both}. It is denoted as X Y. It is also called

Boolean sum of two sets.

1.2.11 Definition

If A and B are two sets, then the set {x B / x A} is denoted by B – A (or

B \ A) and it is called as the complement A in B.

puru
Highlight
puru
Highlight
puru
Highlight
puru
Sticky Note
X delta Y = X union Y - X intersection Y
puru
Sticky Note
puru
Highlight
Page 11: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 5

1.2.12 Examples

Let A = {1, 2, 3, 4, 5}, B = {a, b, c, d}, C = {2, b, d}, D = {3, a, c} and E = {x /x

is an integer and 1 < x < 2}.

i) A B = {x / x A or x B } = {1, 2, 3, 4, 5, a, b, c, d }.

ii) A B = {x / x A and x B}

= the set of all elements that are both in A and B

= (the empty set).

iii) A C = {x / x A and x C} = {2}.

iv) A D = {x / x A and x D}= {3}

v) B C = {x/ x B or x C} = {2, a, b, c, d}.

Here we may note that, in rooster form, there is no necessity of writing

the same element two times. So we avoid writing the same element

second time in rooster form.

vi) B C = {a, b, c, d} {2, b, d}= {b, d}.

vii) E = {x/ x is an integer and 1 < x < 2} = . (Since there is no integer

strictly lying between 1 and 2)

viii) A = {x / x A or x } = A.

ix) A = {x / x A and x ] = .

1.2.13 Definitions

i) If S and T are two sets, then the set {(s, t) / s S and t T} is called

the Cartesian product of S and T (here (a, b) = (s, t) a = s and

b = t). The Cartesian product of S and T is denoted by S T. Thus

S T = {(s, t) / s S and t T}.

Note that if S and T are two sets, then S T and T S may not be

equal.

ii) If S1, S2, ..., Sn are n sets, then the Cartesian product is defined as

S1 S2 … Sn = {(s1, s2, …, sn) / si Si for 1 i n}.

puru
Highlight
Page 12: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 6

Here the elements of S1 S2 … Sn are called ordered n-tuples. For

any two n-tuples, we have (s1, s2, …, sn)= (t1, t2, …, tn) si = ti 1 i n.

iii) Let Ai be a collection of sets – one for each element i I, where I is

some set (I may be the set of all positive integers). We define

Ii

iA

= {a / a Ai for all i I}, and

Ii

iA

= {a / a Ai for some i I}.

A collection {Ai}i I of sets is said to be mutually disjoint if

Ai Aj = for all i I, j I such that i j.

1.2.14 Examples

i) If X = {a, b} and Y = {x, y}, then

X Y = {(a, x), (a, y), (b, x), (b, y)} and Y X = {(x, a) (x, b), (y, a),

(y, b)}.

Note that X Y Y X.

ii) If A = {a, b}, B = {2}, C = {x}, then A B C = {(a, 2, x), (b, 2, x))}.

iii) Let T = {a, b, c, d} and S = {1, 2, 4}. Then the (S T) (T S) is an

empty set.

iv) Write Ai = {i, i+1, i+2, …} for each i N, the set of natural numbers.

Then it is easy to observe that Ni

iA

= N and Ii

iA

= .

v) If Bi = {2i, 2i +1} for all i N, then {Bi}iN is a collection of mutually

disjoint sets.

1.2.15 Definition

Let A be a set. The set P(A) = the set of all subsets of A, is called the power

set of A.

Result: If the set A has n elements, then the number of elements in P(A) is

2n.

puru
Highlight
Page 13: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 7

Proof: Suppose A has n elements. Let m be an integer such that 0 m n.

We can select m elements from the given set A in nCm ways. So A contains

nCm distinct subsets containing m elements. Therefore,

the number of elements in P(A)

= number of subsets containing 0 number of elements

+ number of subsets containing only 1 element

+ … + number of subsets containing n elements

= nC0 + nC1 + nC2 + … + nCn = 2n.

Self Assessment Questions

1. Let A = {a, b} and B = {x, y, z}. Then find A B, B A.

2. Let S = {2, 5, 2 , 25, , 2

5} and T = {4, 25, 2 , 6,

2

3}

i) Find S T, S T and T (S T)

ii) Find Z S, Z S, Z T and Z T.

3. Find Ac (with respect to the universal set of real numbers) in the

following cases

i) A1 = (1, ) (-, -2)

ii) A2 = (-3, ) (-, 5).

4. Let x and y be real numbers with x < y. Find (x, y)c, [x, y)c, (x, )c, and

(-, y]c.

5. Let S = {x, y, z, {x, y}}. Find (i) S \ {x, y}; (ii) S \ ; (iii) ({x, y, z} {S})

\ S; and (iv) S \ {{x, y}}.

6. Find the power set of S = {(x, y), Z }.

1.3 Functions and Relations

Relation describes connections between different elements of the same set,

where as functions describe connections between two different sets.

Functions give a mathematical precise framework for the intuitive idea of

transformation.

Page 14: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 8

1.3.1 Definitions

Let S and T be sets. A function f from S to T is a subset f of S T such that

i) for s S, there exists t T with (s, t) f;

ii) (s, u) f and (s, t) f t = u.

If (s, t) f, then we write (s, f(s)) or f(s) = t.

Here t is called the image of s; and s is called the preimage of t.

The set S is called the domain of f and T is called the codomain.

The set {f(s) / s S } is a subset of T and it is called the image of S under f

(or image of f). We denote the fact „f is a function from S toT‟ by “f : S T”.

f: S T is said to be

1.3.2 One-one function (or injective function)

f(s1) = f(s2) s1 = s2.

onto function (or surjective function): t T there exists an element

s S such that f(s) = t.

1.3.3 Bijection

if it is both one-one and onto.

Let g: S T and f : T U. The composition of f and g is a function

fog: S U defined by (fog)(s) = f(g(s)) for all s in S.

1.3.4 Definitions

A function f : S T is said to have an inverse if there exists a function g

from T to S such that (gof)(s) = s for all s in S and (fog)(t) = t for all t

in T. We call the function „g‟ the inverse of f. A function f : S S is said

to be an identity function if f(s) = s for all s in S. The identity function on S is

denoted by either I or IS. Inverse of a function f, if it exists, is denoted by f -1.

Two functions f : A B and g : C D are said to be equal if A = C,

B = D and f(a) = g(a) for all elements a in A = C. If two functions f

and g are equal, then we write f = g. The identity function is one-one

and onto. A function g is inverse of f fog and gof are identity functions. A

function f has an inverse f is one-one and onto.

Page 15: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 9

1.3.5 Example

Let the functions f and g defined by f(x) = 2x and g(x) = x + 5 for all x in R

(the set of real numbers). Then (fog)(1) = f(g(1)) = f(1 + 5) = f(6) = 12 and

(gof)(1) = g(f(1)) = g(2) = 2 + 5 = 7. This shows that the two functions are

not equal at 1 and so fog gof.

1.3.6 Definition

Let A be a subset of the Universal set U = {x1, x2, …, xn}. The characteristic

function of A is defined as a function from U to {0, 1} by the following:

fA(xi) = i

i

1 if x A

0 if x A

. For example, if A = {2, 3, 7} and U = {1, 2, …, 10},

then fA(1) = 0, fA(2) = 1, fA(3) = 1, fA(7) = 1. and fA(11) is undefined. It can be

verified that fA is everywhere defined and onto, but not one one.

1.3.7 Definition

A relation R between the sets A1, A2, …, An is a subset of A1 A2 …

An. This relation R is called an n-ary relation.

(two–ary is called binary, three-ary is called ternary). In general, a relation

means binary relation on a set S (means a subset of S S). A relation R

on S is said to be

transitive if (a, b) R, (b, c) R implies (a, c) R;

reflexive if (a, a) R for all a S;

anti-symmetric if (a, b) R and (b, a) R a = b;

symmetric if (a, b) R implies (b, a) R;

Self Assessment Questions

7. Determine whether or not each of the binary relations defined on the

given sets Si (i = 1, 2, 3) is reflexive, symmetric, anti symmetric or

transitive. If a relation has a certain property, prove this is so;

otherwise, provide a counter example to show that it does not.

i) S1 = {1, 2, 3, 4}; R1 = {(1, 1), (1, 2), (2, 1), (3, 4)}

Page 16: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 10

ii) S2 = Z , (a, b) R2 if and only if ab 0.

iii) S3 = R R, R3 = {((x, y), (u, v)) / x + y u + v}

1.4 Equivalence Relations

1.4.1 Definition

A relation is said to be an equivalence relation if it is reflexive, symmetric

and transitive.

If (a, b) is an element of the equivalence relation, then we write a ~ b and

we say that a and b are equivalent.

Let R be an equivalence relation on S and a is an element of S. Then the

set [a] = {s S / s ~ a} is called the equivalence class of a (or equivalence

class containing a).

1.4.2 Example

If A ={a, b, c} and R = {(a, a), (b, b), (c, c), (a, b), (b, a)}, then R is an

equivalence relation on A and [a] = {a, b}, [b] = {a, b}, and [c] = {c}.

1.4.3 Example

i) Let S be the set of all integers. Define for any a, b S, a ~ b a - b

is an even number. Then relation ~ is an equivalence relation.

ii) Let R, the set of all real numbers, x ~ y x – y is an integer. Then ~

is an equivalence relation. The set of all equivalence classes are given

by

{[x] / x (0, 1]}.

1.4.4 Example

For (x1,y1) and (x2,y2) in R2 (Euclidean Plane), define (x1,y1) ~ (x2,y2) if and

only if x12 + y1

2 = x22 + y2

2. Then ~ is an equivalence relation on R2.

1.4.5 Definition

Let n > 0 be a fixed integer. We define a relation namely “Congruence

Page 17: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 11

modulo n” on Z, the set of integers as :

a b mod n n divides (a - b).

Some times we write a b as a b. and we read as “a b mod n” as „a is

congruent to b modulo n‟.

1.4.6 Problem

The relation “a b mod n” defined above is an equivalence relation on Z.

Solution: Let a Z.

Reflexive: Since n divides a - a = 0, we have a a mod n.

Symmetric: Let a b mod n.

n divides a - b

n divides - (a - b)

n divides b - a

b a mod n.

Transitivity: Let a, b, c Z such that a b mod n, b c mod n.

n divides a - b, and n divides b - c

n divides ( a - b ) + ( b - c)

n divides a - c

a c mod n.

Hence the relation is an equivalence relation.

1.4.7 Example

Suppose n = 5. Then

[0] = {x / x 0 mod 5} = {x / 5 divides x - 0 = x} = {…, -10, -5, 0, 5, 10, …}

[1] = {x / x 1 mod 5} = {x / 5 divides x - 1} = {…, -9, -4, 1, 6, …}

[2] = { x / x 2 mod 5} = {x / 5 divides x -2} = {…, -8, -3, 2, 7, 12, …}

[3] = {x / x 3 mod 5} = { x / 5 divides x -3} = {…, -7, -2, 3, 8, 13, …}

[4] = { x / x 4 mod 5} = { x / 5 divides x - 4} = {…, -6, -1, 4, 9, 14, …}

Also it is clear that [0] = [5] = [10] = …

Page 18: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 12

[1] = [6] = [11] = …

[2] = [7] = [12] = …

[3] = [8] = [13] = …

[4] = [9] = [14] = …

Therefore the set of equivalence classes is given by {[0], [1], [2], [3], [4]}.

1.4.8 Definition

A partition of a set S is a set of subsets {Si / Si S and i I where I is

some index set} satisfying Ii

iS

= S and Si Sj = if i j.

1.4.9 Example

i) Write A = {1, 2, 3, 4, 5, a, b, c}, S1 = {1, 2}, S2 = {3}, S3 = {4, 5, a} and

S4 = {b, c}. Then S1, S2, S3, S4 form a partition for A.

ii) Consider R, the set of all real numbers. The collection

{(a, b) / a, b Z and b = a + 1} of subsets of R, forms a partition for R.

1.4.10 Lemma

If R is an equivalence relation on a set S and a, b S, then either

[a] = [b] or [a] [b] = .

Proof: If [a] [b] = , then it is clear.

Now suppose the intersection is non-empty.

Let x [a] [b]

x [a] and x [b]

x ~ a and x ~ b

a ~ x and x ~ b (since ~ is symmetric)

a ~ b (since ~ is transitive).

Now we show that [a] = [b]. For this, let y [a] y ~ a.

Since a ~ b, we get y ~ b (by transitive property) b ~ y y [b].

Hence [a] [b]. Similarly, we get that [b] [a]. Therefore [a] = [b].

Page 19: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 13

From this lemma, we can conclude that any two equivalence classes are

either equal or disjoint.

1.4.11 Problem

Let A be a set and ~ an equivalence relation on A. Then the set of all

equivalence classes forms a partition for A.

Solution:

The collection of all equivalence classes is {[a] / a A}.

Since each [a] A, we have that Aa

]a[

A.

Now let x A. It is clear that x [x] Aa

]a[

.

Therefore we have that

A = Aa

]a[

.

By the lemma 1.4.11, we know that either [a] = [b] or [a] [b] = for any

a, b A.

Hence the set of all equivalence classes forms a partition.

Observation: Let A be a set and { Ai / i I} be a collection of nonempty

subsets of A, which forms a partition for A. Then there exists an equivalence

relation ~ on A such that the equivalence classes are nothing but the sets of

the partition.

Self Assessment Questions

8. Whether R = {(1, 1), (1, 2), (3, 2), (3, 3), (2, 3), (2, 1)} is an equivalence

relation on S = {1, 2, 3}?

9. For x, y R \{0}, define x ~ y y

x Q. Whether or not, „‟ is an

equivalence relation.

10. Let A = {x Z+ / 1 x 10}. State whether or not each of the following

Page 20: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 14

families of sets is a partition of A.

i) {{1, 3, 5}, {4, 7, 9}, {2, 6, 10}}; (ii) {{1, 3, 5, 7}, {2, 4, 6}, {3, 8, 9,

10}};

ii) {{1, 2, 3}, {5, 8, 9}, {4, 6, 7, 10}}.

11. If |S| = n, then how many relations are there from S to S.

12. Give an example of a relation that is both symmetric and anti

symmetric.

13. Let S = Z \ {0} and define „R‟ on S by (a, b) R (c, d) ad = bc. Is „R‟

an equivalence relation? If so find the equivalence class containing

(1, 2).

1.5 Recursive Functions

Recursion is the technique of defining a function, a set or an algorithm in

terms of itself. That is, the definition will be in terms of previous values.

1.5.1 Definition

A function f: N N, where N is the set of non-negative integers is defined

recursively if the value of f at 0 is given and for each positive integer n, the

value of f at n is defined in terms of the values of f at k, where 0 ≤ k < n.

Observation: f defined (above) may not be a function. Hence, when a

function is defined recursively it is necessary to verify that the function is

well defined.

1.5.2 Example

The sequence 1, 4, 16, 64, ... , can be defined explicitly by the formula

f(n) = 4n for all integers n 0.

The same function can also be defined recursively as follows:

f(0) = 1, f(n + 1) = 4f(n), for n > 0

To prove that the function is well defined we have to prove existence and

uniqueness of such function. In this case, existence is clear as f(n) = 4n.

Page 21: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 15

1.5.3 Theorem: (Recursion Theorem):

Let F is a given function from a set S into S. Let s0 be fixed element of S.

Then there exists a unique function f: N N where N is the set of non-

negative integers satisfying

i) f(0) = s0

ii) f(n + 1) = F(f(n)) for all integers n N.

(Here the condition (i) is called initial condition and (ii) is called the

recurrence relation).

1.5.4 Example

Define n! recursively and compute 5! recursively.

Solution: We have f: N N. Then

i) f(0) = 1

ii) f(n + 1) = (n + 1)f(n) for all n 0.

Clearly f(n) = n!.

Now we compute 5! recursively as follows:

5! = 5. 4!

= 5. 4. 3!

= 5. 4. 3. 2!

= 5. 4. 3. 2. 1!

= 5. 4. 3. 2. 1. 0!

= 5. 4. 3. 2. 1. 1

= 120.

1.5.5 Note

Any sequence in arithmetic progression or geometric progression can be

defined recursively. Consider the sequence a, a + d, a + 2d, …. Then

A(0) = a, A(n + 1) = A(n) + d.

Consider another sequence a, ar, ar2, … . Then

G(0) = a, G(n +1) = r G(n).

Page 22: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 16

1.5.6 Definition

The Fibonacci sequence can be defined recursively as

i) F0 = 1 = F1

ii) Fn+1 = Fn + Fn-1 for n > 1.

Then

F2 = F1 + F0 = 2

F3 = F2 + F1 = 3

F4 = F3 + F2 = 5

…..

Here, there are two initial conditions.

1.5.7 Example:

Define

f(x) =

when x is even2

1 when x is odd

2

x

x

Solution: Define f: N N such that f(0) = 0 and f(x + 1) = x – f(x).

Then f(6) = 5 – f(5) = 5 – [4 – f(4)]

= 5 – 4 + [3 – (3)]

= 5 – 4 + 3 – 2 + [1 – f(1)]

= 5 – 4 + 3 – 2 +1 – [0 – f(0)]

= 3.

and f(5) = 4 – f(4)

= 4 – [3 - f(3)]

= 4 – 3 + 2 - [1-f(1)]

= f – 3 + 2 -1 + [0 – f(0)]

= 2.

Page 23: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 17

1.5.8 Example

Using recursion theorem, verify that the object defined by the recursive

definition is a function. That is.

i) g(0) = 1

ii) g(n + 1) = 3[g(n)]2 + 7 for all n > 0

Solution: We obtain

i) s0 = 1

ii) f(k) = 3k2 + 7, where f: N N

Then g(0) = s0. And g(n +1) f(g(n)). Thus g is a well-defined function.

The following is an example of a recursive function that does not define a

function.

1.5.9 Example

Consider a recursion function g: Z+ (the set of positive integers) Z (the set

of integers), for all integers n 1.

Solution: Suppose g is a function. Then by definition of g,

g(1) = 1

g(2) = 1 + g(1) = 1 + 1 = 2.

g(3) = g(8) = 1 + g(4) = 1 + (1 + g(2)) = 1 + (1 + 2) = 4.

g(4) = 1 + g(2) = 1 + 2 = 3.

Now, g(5) = g(14) = 1 = g(7) = 1 + g(20)

= 1 + (1 + g(10))

= 1 + 1(1 + (1 + g(5)))

= 3 + g(5).

Subtracting g(5) we get 0 = 3. Therefore g is not well defined.

1.5.10 Definition

If m and n are two non-negative integers then the (greatest common divisor)

g.c.d. (m, n) is defined as the largest positive integer d such that d divides

Page 24: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 18

both m and n. Euclidean algorithm computes the greatest common divisor

(g.c.d.) of two non-negative integers.

We can find g.c.d. (m, n) recursively as follows:

g.c.d. (n, m) if n > m

. . . (m, n) = m if n = 0

g.c.d. (n, mod (m, n)) Otherwise

g c d

where mod (m, n) is the remainder obtained when m is divided by n.

Observations:

The first part interchanges the order of m and n if n > m.

Second part is the initial condition.

Third part is the recursive part mod (m, n) will become 0 in a finite

number of steps.

1.5.11 The recursive functions of more than one variable

i) Define f(x, y) = x + y recursively.

Here, we keep x fixed and use recursion on y. We define

i) f(x, 0) = x

ii) f(x, y + 1) = f(x, y) + 1.

Take x = 2, y = 3. Now f(2, 3) = f(2, 2) + 1

= f(2, 1) + 1 + 1

= f(2, 0) + 1 + 1 +1

= 2 + 1 + 1 +1

= 5.

ii) Define g(x, 0) = 0, g(x, y + 1) = g(x, y) + x. Take x = 3, y = 4. Then

g(3, 4) = g(3, 3) + 3

= g(3, 2) + 3 + 3

= g(3, 1) + 3 + 3 + 3

= g(3, 0) + 3 + 3 + 3 + 3 = 12 (since g(3, 0) = 0).

Page 25: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 19

Self Assessment Questions

14. The g.c.d. (20, 6) of _______

15. The g.c.d. (81, 36) of ________

16. The g.c.d. (22, 8) of _______

1.6 Strings and Growth Functions

Let us see the simple example, to understand this.

Bhanu is running a successful business and is planning a short vacation trip.

She is planning to take a cell phone with her so that in the case of an

emergency, the manager at work can reach her. To budget her calls, we

looks at various plans and chooses the pan that charges US $ 1.00 for the

connection charge and US $ 10 for each minute. For example, the charges

for a one minute call are US $ 1.10, the charges for a two minute call are US

$ 1.20 and so on. So Bhanu makes the following table for the first ten

minutes of telephone charges.

Min 1 2 3 4 5 6 7 8 9 10

Charges 1.10 1.20 1.30 1.40 1.50 1.60 1.70 1.80 1.90 2.00

From the table we see that for a 30 min call, the charges are 1.00 + 30(0.10)

= 4.00.

In general, for an n-minute call, the charges are

1.00 + n(0.10) = 1.00 + 0.10 n.

Let us list the telephone charges as follows.

1.10, 1.20, 1.30, …, (1.00 + 0.1 n), …

This is an ordered list of real numbers in which the first element is 1.10, the

second element is 1.20, and so on. Such an ordered list of elements is

called a sequence. If the sequence stops after n elements for some positive

integer n, then it is called finite otherwise it is called infinite sequence.

Page 26: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 20

Define a function f: N (natural numbers) R (real numbers) as f(n) = 1.00 +

(0.1)n.

Then f(1) = 1.00 + (0.1)1 = 1.10 = c1

f(2) = 1.00 + (0.1)2 = 1.20 = c2

… … …

f(n) = 1.00 + (0.1)n = cn.

1.6.1 Definition

An infinite sequence or a sequence, on a non empty set X is a function from

the set of positive integers N to X. A sequence whose terms are integers is

called an integer sequence.

1.6.2 Example

Let f: N Z be a function defined by f(n) = n2. Then f(1) = 1, f(2) = 22 = 4,

and so we get a sequence 1, 4, 9, …, n2, …is a sequence on A. Let an

denote the nth term of this sequence. Then a1 = 1, a2 = 4, …, and so on. We

denote this sequence by

1

2}{ nn or simply by {n2}.

1.6.3 Definition

Let 1nn }a{ be a sequence. Consider the following terms of this sequence:

am, am+1, …, an.

Some of the common things we do with these terms are adding them and

multiplying them. Let us first consider the addition.

The sum of the terms am, am+1, …, an is written as

n

mi

ia . There is nothing about the choice of the variable i. We could choose

j or k as the index of the summation and write as the sum as

n

mj

ja

or

n

mk

ka .

Page 27: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 21

1.6.4 Example

i)

4

1i

i = 1 + 2 + 3 + 4 = 10.

ii)

3

1i1i

i = ½ + 2/3 + ¾ = 23/12.

1.6.5 Example

Consider the sums

3

1

)1(i

i and

4

2i

j

3

1i

)1i( = (1+1) + (2 + 1) + (3 +1) = 9.

4

2i

j = 2 + 3 + 4 = 9.

Therefore

3

1i

)1i( =

4

2i

j

1.6.6 Note

To change the index variable in a sum we do the following.

1. Calculate the lower limit of the new index variable.

2. Calculate the upper limit of the new index variable.

3. Find the general term of the summation in terms of the new index

variable.

1.6.7 Example

Consider the sum

3

1i

)1i( and change the index variable to j = i + 1.

Page 28: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 22

Solution: Step (i): Lower limit for j: The lower limit for i is 1, so the lower

limit for j is j = i + 1 = 1 + 1 = 2.

Step (ii): Upper limit for j: The upper limit for i is 3, so the upper limit for j is j

= i + 1 = 3 + 1 = 4.

Step (iii): The general term is i + 1 = j.

Hence, the equivalent sum is

4

2j

j .

1.6.8 Example

Consider the sum

1n

0i

2)i1n( , change the index variable to j = i + 1.

Solution:

Step (i): Lower limit for j = i + 1 = 0 + 1 = 1.

Step (ii): Upper limit for j = i + 1 = n – 1 + 1 = n.

Step (iii): The general term for the new summation is given by

n2 + 1 + i = n2 + j.

Hence, the new sum is

n

1j

2)jn( .

1.6.9 Properties of Summation

Let 1nn }a{ and

1nn }b{ be sequences of real numbers and let c be a real

number. Suppose m and n are integers such that 1 ≤ m ≤ n. Then

(i)

n

mi

ia +

n

mi

ib = )ba(

n

mi

ii

,

(ii) c.

n

mi

ia =

n

mi

ica .

Page 29: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 23

1.6.10 Definition

Let A be a nonempty finite set. A string or word, over A is a finite sequence

of elements from A. The set A is called an alphabet.

A string with no element in it is called the empty string or empty word.

If s1 and s2 are two strings over a set A, then the concatenation of s1 and s2

is the string s1s2. That is, to obtain the concatenation of s1 and s2 we list the

elements of s1 followed by the elements of s2.

1.6.11 Example

Suppose s1 = abbabcdb and s2 = caabcdbbd are two strings over the set

A = {a, b, c, d}. Then the concatenation of s1 and s2 is s1s2

= abbabcdbcaabcdbbd.

It follows that if s1 and s2 are strings over a set A, then s1s2 = s1 + s2 .

1.6.12 Example

Let A = {0, 1}.

i) Let s = 01101010. Then s is a string over A and s = 8.

ii) 00, 01, 10, 11 are only strings of length 2 over A.

iii) If s1 = 1001010 and s2 = 00111 then s1s2 = 100101000111. Also s1 =

7, s2 = 5, and s1s2 = 12 = s1 + s2.

1.6.13 Growth Functions

The growth of a function is often described using a special notation,

O-notation (read as “big-oh notation”). It provides a special way to compare

relative sizes of functions that is very useful in the analysis of computer

algorithms. It often happens that the time or memory space requirements

for the algorithms available to do a certain job differ from each other on such

a grand scale that differences of just a constant factor are completely

overshadowed. The O-notation makes use of approximations that highlight

these large-scale differences while ignoring differences of a constant factor

and differences that only occur for small sets of input data.

Page 30: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 24

1.6.14 Definition

Let f and g be functions from the set of integers or the set of real numbers to

the set of real numbers. Then f of order g written as f(x) is O(g(x)), if there

are constants C and k such that f(x) ≤ C g(x) whenever x > k (this is read

as „f(x) is big-oh of g(x)‟).

1.6.15 Remark

To show f(x) is O(g(x)), we need only find one pair of constants C and k

such that f(x) < C(g(x)) if x > k. However, a pair C, k that satisfies the

definition is not unique. Moreover, if one such pair exists, there are infinitely

many such pairs. A simple way to see this is to note that if C, k is one such

pair, any pair C1, k1 with C < C1 and k < k1 also satisfies the definition, since

f (x) < C1g(x) whenever x > k1 > k.

1.6.16 Example

Use O-notation to express 3x3 + 2x + 7 ≤ 12 x3, for all real numbers

x > 1.

Solution: Take C = 12 and k = 1, the given statement translates to

2x3 + 2x + 7 is O(x3).

1.6.17 Note: Order of Polynomial functions

i) If 1 < x then x < x2 and so x2 < x3. Thus, if 1 < x, then 1 < x < x2 < x3.

ii) For any rational numbers r and s, if x > 1 and r < s, then xr < xs.

Therefore xs is O(xs).

1.6.18 Example

Show that for any real number x > 1, 2x4 + 4x3 + 5 ≤ 11x4.

Solution: Since x is a real number and x > 1, we have

x3 < x4 and 1 < x4. So

4x3 < 4x4 and 5 < 5x4.

Adding we obtain, 2x4 + 4x3 + 5 ≤ 2x4 + 4x4 + 5x4 = 11x4.

Page 31: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 25

1.6.19 Example

Use the definition of order to show that 5x3 – 3x + 4 is O(x3).

Solution

The functions f and g referred to in the definition of O-notation are defined

as follows.

For all real numbers x, f(x) = 5x3 – 3x + 4 and g(x) = x3.

For all real numbers x > 1,

5x3 – 3x + 4 ≤ 5x3 + 3x + 4 (by the triangle inequality)

≤ 5x3 + 3x + 4

≤ 5x3 + 3x3 + 4x3

≤ 12x3 ≤ 12 x3.

Therefore, f(x) ≤ 12 g(x) for all x > 1

Or f(x) ≤ C g(x) for all x > k where C = 12 and k = 1.

Hence, 5x3 – 3x + 4 is O(x3).

1.6.20 Example

Show that 9x2 is O(x3). Is it true that x3 is O(9x2)?

Solution: We note that 9x2 < x3 is true whenever x > 9 (by dividing both

sides by x2). Hence, 9x2 is O(x3), taking C = 1 and k = 9 in the definition of

big-oh notation.

To determine whether x3 is O(9x2) or not , it is necessary to determine

whether there are constants C and k such that x3 ≤ C (9x2) whenever x > k.

This is equivalent to the inequality x < 9C (we get this dividing both sides by x2).

No such x can exist since x can be marked arbitrarily large. Hence, x3 is not

O(9x2).

Now we generalize above example, to show that any polynomial function is

big –oh of the power function of its highest order term or of any larger power

function.

Page 32: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 26

1.6.21 Theorem

Let f(x) = anxn + an-1x

n-1 + … + a1x + a0 where a0, a1, …, an-1, an are real

numbers then f(x) is O(xn).

Proof: Using the triangle inequality, if x > 1, we have that

f(x) = anxn + an-1x

n-1 + … + a1x + a0

≤ an xn + an-1 x

n-1 + … + a1 x + a0

≤ xn ( an + an-1 / x + … + a1 / x

n-1 + a0 /x

n)

≤ xn ( an + an-1 + … + a1 + a0) (since x > 1).

This shows that

f(x) ≤ C xn where

C = an + an-1 + … + a1 + a0, whenever x > 1.

Hence, f(x) is O(xn).

1.6.22 Example

Use big-oh notation to estimate the sum of the first n positive integers?

Solution: Each positive integer n is greater than every positive integer that

precedes it. Therefore, for each positive integer n,

1 + 2 + 3 + … + n ≤ timesn

nnn

... = n n = n2.

Therefore, 1 + 2 + 3 + … + n is O(n2).

Observation: “big O-estimates will be developed for the factorial function

and its logarithm. These estimates will be important in the analysis of the

number of steps used in sorting procedures.

1.6.23 Example

Give big-O estimates for the factorial function and the logarithm of the

factorial function, where the factorial function f(n) = n = 1 2 3 … n

where n is a positive integer, and 0 = 1.

For example, 1 = 1, 2 = 2, 3 = 6, 4 = 24.

Page 33: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 27

Note that the function n grows rapidly.

20 = 2, 432, 902, 008, 176, 640, 000.

Solution: A big-O estimate for n can be obtained by noting that each term

in the product does not exceed n. Hence,

n = 1 2 … n ≤ n n … n = nn.

This inequality shows that n is O(nn), taking C = 1 and k = 1.

Taking logarithm both sides, we get

log n ≤ log nn = n log n.

This shows that log n is O(n log n), again taking C = 1 and k =1.

1.6.24 Theorem

Suppose that f1(x) is O(g1(x)) and f2(x) is O(g2(x)). Then (f1 + f2) (x) is

O(max (g1(x), g2(x))).

Suppose that f1(x) and f2(x) are both O(g(x)). Then (f1 + f2)(x) is O(g(x)).

Suppose that f1(x) is O(g1(x)) and f2(x) is O(g2(x)). Then (f1f2)(x) is

O(max (g1(x)g2(x)).

1.6.25 Example

Give a big O-estimate for f(n) = 3n log(n!) + (n2 + 3) log n, where n is a

positive integer.

Solution: First we estimate the product 3n log (n). From the example, we

log n is O(n log n). Also 3n is O(n).

Using these two estimates, and the theorem, we can estimate that 3n log (n) is

O(n2 log n).

Next the product (n2 + 3) log n will be estimated.

(n2 + 3) ≤ 2n2 when n > 2, it follows that n2 + 3 is O(n2).

Therefore (n2+3)log n is O(n2 log n). Using theorem, to combine the two

big O-estimates for the products shows that

f(n) = 3nlog (n) + (n2 + 3)log n is O(n2 log n).

Page 34: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 28

1.6.26 Example

Give a big – O estimate for f(x) = (x + 1)log(x2 + 1) + 3x2.

Solution: A big-O estimate for (x + 1) is O(x).

When x > 1, x2 + 1 ≤ 2x2. Therefore,

log (x2 + 1)

≤ log(2x2)

= log 2 + log x2

= log 2 + 2 log x

≤ 3 log x, if x > 2.

Therefore

log(x2 + 1) is O(log x).

By theorem, it follows that (x + 1)log(x2 + 1) is O(x logx).

Further, 3x2 is O(x2), by theorem, f(x) is O(max (x logx, x2 )).

Since x log x ≤ x2, for x > 1, it follows that f(x) is O(x2).

1.6.27 Big-Omega and Big-Theta Notations

If f(x) is O(g(x)), all that one can conclude that except for constants and a

finite number of exceptions, f is bounded above by g, so g grows at least as

fast as f. For example, if f(x) = x and g(x) = 2x, then f(x) is O(g(x)), but g

grows considerable faster than f. The statement f(x) is g(x) says nothing

about lower bound for f. For this, we use big-Omega notation. When we

want to give both upper and a lower bound on the size of a function f(x)

relative to a reference function g(x), we use big-Theta notation.

1.6.28 Definition

Let f and g be functions from the set of integers or the set of real numbers to

the set of real numbers. We say that f(x) is (g(x)), if there are positive

constants C and k such that f(x) C g(x) whenever x > k which is read

as “f(x) is big-Omega g(x)”.

Page 35: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 29

Let f and g be functions from the set of integers or the set of real numbers to

the set of real numbers. We say that f(x) is g(x) if f(x) is O(g(x)) and f(x) is

g(x). When f(x) is g(x), we say that “f is big-Theta of g(x)” and we say

f(x) is of order g(x).

Therefore, f(x) is O(g(x)), then f(x) is an upper bound for f(x) and whenever

f(x) is g(x), g(x) is a lower bound for f(x). The big-O notation compares the

rate of growth of functions rather than their values, so when f(x) is g(x), f(x)

and g(x) have the same rates of growth, but can be very different in their

values.

1.6.29 Example

Let f(x) = x and g(x) = 1, 000, 000x, then f(x) ≤ C g(x) for C = 1 and k = 1, so

that f(x) is O(g(x)). Also Cf(x) g(x) for C = 1, 000, 000 and k = 1, so f(x) is

O(g(x)). Therefore f(x) is g(x).

Self Assessment Questions

17. Use the definition of order to show that x2 + 2x + 1 is O(x2).

1.7 Summary

In this unit we introduced the basic concept sets and the different properties

of sets. Some properties common to operations on sets, cartesian product

of sets were studied as relations between two sets. We also discussed the

equivalence relations with a few illustrations. The reader can easily apply

the mathematical concepts introduced, in various situations. In the last

section we studied the strings and growth functions which are very useful in

time complexity of algorithms.

Page 36: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 30

1.8 Terminal Questions

1. Explain Equivalence relation and give examples.

2. Explain growth functions and give examples.

3. Define recursive function and give examples.

4. Explain Big Omega and Big theta functions.

1.9 Answers

Self Assessment Questions

1. A B = {(a, x), (a, y), (a, z), (b, x), (b, y), (b, z)}, and

B A = {(x, a), (x, b), (y, a), (y, b), (z, a), (z, b)}. This problem illustrates

that, in general, the sets A B and B A are different.

2. i) S T = { 2 , 25}, S T = {2, 5, 2 , 25, , 2

5, 4, 6,

2

3},

T (S T) = {(4, 2 ), (4, 25), (25, 2 ), (25, 25), ( 2 , 2 ), ( 2 ,

25), (6, 2 ), (6, 25), (2

3, 2 ), (

2

3, 25)}.

ii) Z S = { 2 , , 2

5, 0, 1, -1, 2, -2, …. }, Z S = {2, 5, 25}

Z T = { 2 , 2

3, 0, 1, -1, 2, -2, ….}, Z T = {4, 25, 6}.

3. i) c

1A = [-2, 1]; (ii). c

2A = (- , 3] [5, ).

4. (x, y)c = (-, x] [y, ); [x, y)c = (-, a) [y, ); (x, )c = (-, x]; and (-,

y]c = (y, )

5. (i) {z, {x, y}}, (ii). S (iii). {x, y, z}, and (iv).{x, y, z}.

6. P (S) = the power set of S = {, S, {(a, b)}, {c}}.

7. i) Not reflexive as (2, 2) R1.

Not symmetric as (3, 4) R1 but (4, 3) R1

Not anti symmetric as (1, 2), (2,1) R1, but 1 2

Not transitive as (2, 1), (1, 2) R1 but (2, 2) R1.

Page 37: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 31

ii) Reflexive: For any a S2, a2 0 and so (a, a) R2

Symmetric: If (a, b) R2, then ab 0, so ba 0 and hence (b, a) R2.

Not Anti symmetric: (5, 2) R2, since 5(2) = 10 0 and similarly

(2, 5) R2, but 5 2.

Not transitive: (5, 0) R2, (0, -6) R2 but (5, -6) R2, since

5(-6) /0.

iii) Reflexive:

Not symmetric: ((1, 2), (3, 4)) R3, but ((3, 4), (1, 2)) R, since

3 + 4 / 1 + 2.

Not anti symmetric: ((1, 2), (0, 3)) R3, ((0, 3), (1, 2)) R3 but

(1, 2) (0, 3).

Transitive:

8. No: Since R is not reflexive as (2, 2) R.

9. Yes

10. (i) No; (ii) No; (iii)Yes.

11. 2n.

12. The relation „R‟ defined by xRy x = y. That is., the „identity relation‟,

is both symmetric as well as anti symmetric.

13. Yes. [(1, 2)] = {(1, 2), (-1, -2), (2, 4), (-2, -4), …}.

14. g.c.d. (20, 6) = g.c.d. (6, mod (20, 6)) (since 20 = 6• 3 + 2)

= g.c.d. (6, 2)

= (2, mod (6, 2))

= g.c.d. (2, 0)

= 2.

15. g.c.d. (81, 36) = g.c.d. (36, 9)

= g.c.d. (9, 0)

= 9.

Page 38: MC0082 - Theory of Computer science

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 32

16. g.c.d. (22, 8) = g.c.d. (8, mod (22, 8))

= g.c.d. (8, 6)

= g.c.d. (6, mod (8, 6))

= g.c.d. (6, 2)

= g.c.d. (2, 0)

= 2.

17 The functions f and g referred to in the definition of O-notation are

defined as follows. For all real numbers x, f(x) = x2 + 2x + 1 and

g(x) = x2.

For all real numbers x > 1, x2 + 2x + 1 = x2 + 2x + 1

≤ x2 + 2x2 + x2

≤ 4x2

≤ 4 x2.

Therefore,

f(x) ≤ 4 g(x) for all x > 1.

Or f(x) ≤ C g(x) for all x > k where C = 4 and k = 1.

Hence, x2 + 2x + 1 is O(x2).

Page 39: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 33

Unit 2 Proof Techniques

Structure

2.1 Introduction

Objectives

2.2 Proof Techniques

2.3 Summary

2.4 Terminal Questions

2.5 Answers

2.1 Introduction

In this unit would discuss various methods of proofs and a few examples.

The techniques will give an idea to analyze and solve the problems. We are

introduced to certain fundamental mathematical concepts. The idea of

graphs and trees is being discussed in this unit.

Objectives:

At the end of the unit you would be able to

understand the fundamental idea of certain mathematical concepts.

learn the various operations on sets.

learn Graphs and Trees.

know the methods of proof.

2.2 Proof Techniques

A significant requirement for reading this subject is the ability to follow

proofs. In mathematical arguments, we employ the accepted rules of

deductive reasoning, and many proofs are simply a sequence of such steps.

Direct Proof: Consider a set of hypothesis H1, H2, …, Hn from which we

want to infer a conclusion C.

Consider the example: Prove that if x and y are rational numbers then

x + y is rational.

Page 40: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 34

Solution: Since x and y are rational numbers, we can find integers p, q, m,

n such that x = p/q and y = m/n. Then x + y = p/q + m/n = (pn + mq)/qn.

Since pn + mq and qn are both integers, we conclude that x + y is a rational

number.

Indirect Proof: Proofs that are not direct are called indirect. Two main

types of indirect proof uses both the negation and conclusion, so they are

often suitable when that negation is easy to state. The first type of proof is

contra-positive proof.

Consider the example: Prove that if m + n 73, then m 37 or n 37, m

and n being positive integers.

Solution: We prove this by taking contra-positive: not “m 37 or n 37”

implies not “m + n 73”. By De morgan law, the negation of “m 37 or n

37” is “not m 37 and n 37”. That is,

“m ≤ 36 and n ≤ 36” so that the contrapositive proposition is if m ≤ 36 and

n ≤ 36 then m + n ≤ 72. This follows that from the inequalities: a ≤ c and

b ≤ d imply that a + b ≤ c + d for all real numbers a, b, c, d.

A few special proof techniques are used so frequently that it is appropriate

to review them briefly.

1. Proof by induction

2. Proof by contradiction

3. The pigeonhole principle, and

4. The Diagonalization Principle

5. Proof by Contradiction

2.2.1 Proof by Induction

Let A be the set of all natural numbers such that

i) 0 A, and

ii) for each natural number n, if {0, 1,... , n} A, then n + 1 A.

Then A = N.

Page 41: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 35

In other words: The principle of mathematical induction states that any set

of natural numbers containing zero, and with the property that it contains n +

1 whenever it contains all the numbers up to and including n, must in fact be

the set of all natural numbers.

In practice, induction is used to prove assertions of the following form:

“For all natural numbers n, property P is true.”

The above principle is applied to the set A = {n: P is true of n} in the

following way.

1. In the basis step we show that 0 A, that is, that P is true of 0.

2. The induction hypothesis is the assumption that for some fixed but

arbitrary n 0, P holds for each natural number 0,1,... , n.

3. In the induction step we show, using the induction hypothesis, that P is

true of n + 1. By the induction principle, A is then equal to N, that is, P

holds for every natural number.

2.2.2 Example

Prove by mathematical induction that the sum of the first n natural numbers

is

2

1nn

Solution:

That is to prove that 1 + 2 + 3 + …. + n

2

1nn

i) Base Step: Let n = 0. Then the sum on the left is zero, since there is

nothing to add. The expression on the right is also zero.

For 1n , left side = 1, right side

12

111

. Hence the result is

true for 1n

Page 42: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 36

ii) Induction Hypothesis: Assume that the result to be true for m ≤ n

and n 0. Then 1 + 2 + 3 + … + 2

1mmm

iii) Induction Step: We now show that the above result is true for

1mn . Adding the th1m term viz., 1m to both sides we

obtain.

1 + 2 + 3 + ... + 1m2

1mm1mm

2

2m1m1

2

m1m

2

11m1m ,

which is the same as the given result for 1mn

Hence by mathematical induction, the result is true for all positive

integral values of n.

2.2.3 Example

Prove by mathematical induction that

6

1n21nnn....321 2222

Solution:

i) Base Step: Let n = 0. Then the sum on the left is zero, since there is

nothing to add. The expression on the right is also zero.

If 1n , left side 112 .

Right side

16

3.2.1

6

11.2111

.

Hence the result is true for n = 1.

ii) Induction Hypothesis: Assume that the result to be true for n = m

Then

6

1m21mmm...321

2222 .

Page 43: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 37

Adding the th1m term i.e. 21m to both sides of the above

equation, we get,

222221m

6

1m21mm1mm...21

1m61m2m

6

1m

6m7m2

6

1m 2

6

3m22m1m

6

11m211m1m

Therefore, the result is true for 1mn . Hence by mathematical

induction, the given result is true for all positive integers n.

2.2.4 Example

For any finite set A, the cardinality of the power set of A is 2 raised to a

power equal to the cardinality of A.

Solution:

i) Basis Step: Let A be a set of cardinality n = 0. Then A = , and 2A =

20 = 1; on the other hand, 2A = {}, and 2A = {} = 1.

ii) Induction Hypothesis: Let n > 0, and suppose that 2A = 2A

provided that A≤ n.

iii) Induction Step: Let A be such that A = n+ 1. Since n > 0, A

contains at least one element a. Let B = A -{a}; then B = n. By the

induction hypothesis, 2B = 2B = 2n.

Now the power set of A can be divided into two parts, those sets containing

the element a and those sets not containing a. The latter part is just 2B, and

the former part is obtained by introducing a into each member of 2B. Thus

Page 44: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 38

2A =2B {C {a}: C 2B}.

This division in fact partitions 2A into two disjoint equinumerous parts, so the

cardinality of the whole is twice 2B , which, by the induction hypothesis, is

2 2n = 2 n+1. This completes the proof.

2.2.5 Example

(Refer the unit 3 for definition of binary tree) A binary tree is a tree in which

no parent can have more than two children.

Example: Prove that a binary tree of height n has at most 2n leaves.

Solution: If we denote the maximum number of leaves of a binary tree of

height n by l(n), then we want to show that l(n) ≤ 2n.

Basic Step: Clearly l(0) = 1 = 20 since a tree of height 0 can have no nodes

other than the root, that is , it has at most one leaf.

Inductive Hypothesis: l(i) ≤ 2i for i = 0, 1, …, n.

Inductive step: To get a binary tree of height n +1 from one of height n, we

can create, at most, two leaves in place of each previous one. Therefore,

l(n + 1) = 2l(n).

Now, using the inductive assumption, we get

l(n + 1) ≤ 2 2n = 2n+1.

Therefore, our claim is true for n + 1. Since n is arbitrary, we can conclude

that the statement is true for all n.

2.2.6 Example

(Refer unit 3 for the definition of tree) A tree G with „n‟ vertices has (n - 1)

edges.

Proof: We prove this theorem by induction on the number vertices n.

Basic step: If n = 1, then G contains only one vertex and no edge. So the

number of edges in G is n -1 = 1 - 1 = 0.

Page 45: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 39

Induction hypothesis: The statement is true for all trees with less than „n‟

vertices.

Induction step: Now let us consider a tree with „n‟ vertices. Let „ek‟ be any

edge in T whose end vertices are vi and vj. Since T is a tree, by Theorem

6.5, there is no other path between vi and vj. So by removing ek from T, we

get a disconnected graph. Furthermore, T- ek consists of exactly two

components (say T1 and T2). Since T is a tree, there were no circuits in T

and so there were no circuits in T1 and T2. Therefore T1 and T2 are also trees.

It is clear that |V(T1)| + |V(T2)| = |V(T)| where V(T) denotes the set of vertices

in T.

Also |V(T1)| and |V(T2)| are less than n.

Therefore by the induction hypothesis, we have

|E(T1)| = |V(T1)| - 1 and |E(T2)| = |V(T2)| - 1.

Now |E(T)| - 1 = |E(T1)| + |E(T2)| = |V(T1)| - 1 + |V(T2)| - 1

|E(T)| = |V(T1)| + |V(T2)| - 1 = |V(T)| - 1 = n-1.

2.2.7 Problem

Prove by mathematical induction that n2 n for all positive integer n.

Solution: Let P(n) be the given proposition. Now P(1) implies 2 > 1 which is

true. Hence P(1) is true

Induction hypothesis: Let us assume that P m is true. That is 2m > m

Now 2m+1 = 2. 2m > 2m.. We know that 2m m m m 1 for all m N .

Therefore 2m+1 > m+1. Hence P (m+1) is true.

Therefore by induction P n is true for all n.

2.2.8 Example

Show by induction that n (n+1) (2n+1) is divisible by 6.

Solution: Let P (n) = n (n+1) (2n+1)

Now P (1) = 1. (1+1) (2+1) = 6 this is divisible by 6.

Page 46: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 40

Assume that P (m) is divisible by 6.

That is, m(m+1) (2m+1) is divisible by 6.

Therefore, m (m+1) (2m+1) = 6k for some integer k.

Now

P(m+1) = (m+1) [(m+1) + 1] [2 (m+1)+1]

m 1 m 2 2m 3

m 1 m 2 2m 1 2

m 1 m 2 2m 1 2 m 1 m 2

m m 1 2m 1 2 m 1 2m 1 2 m 1 m 2

6k 2 m 1 3m 3 by induction hypothesis

2

6k 6 m 1

Since each term on the R.H.S is divisible by 6 their sum is also divisible by 6.

Hence P(m+1) is divisible by 6. Therefore, by induction P (n) is divisible by

6 for all n N

2.2.9 The Pigeonhole Principle: If A and B are finite sets and A > B,

then there is no one-to-one function from A to B.

(In other words, if we attempt to pair off the elements of A (the “pigeons”)

with elements of B (the “pigeonholes”), sooner or later we will have to put

more than one pigeon in a pigeonhole).

Proof: Basis Step: Suppose B = 0, that is, B = . Then there is no

function f: A B and so no one to one function.

Induction Hypothesis: Suppose that f is not one-to-one, provided that

f: A B, A> B, and B ≤ n, where n 0.

Page 47: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 41

Induction Step: Suppose that f: A B and A> B = n + 1. Choose some

a A (since A > B = n + 1 1, A is nonempty, and therefore such a

choice is possible). If there is another element a a1 A, such that f(a) =

f(a1), then obviously f is not a one-to-one function, and we are done.

So, suppose that a is the only element mapped by f to f(a).

Consider then the sets A – {a}, B – {f(a)}.

The function g: A-{a} B-{f(a)} that agrees with f on all elements of A-{a}.

Now the induction hypothesis applies, because B-{f(a)} has n elements, and

A -{a} = A -1 > B -1 = B-{f(a)}.

Therefore, there are two distinct elements of A-{a} that are mapped by g

(and therefore by f) to the same element of B-{b}. Hence f is not one-to-one.

2.2.10 The Diagonalization Principle: Let R be a binary relation on a set

A, and let D, the diagonal set for R, be {a aA and (a, a) R}. For each

a A, let Ra = {b: b A and (a, b) R}. Then D is distinct from each Ra.

If A is a finite set, then R can be pictured as a square array; the rows and

columns are labeled with the elements of A and there is a cross in the box

with row labeled a and column labeled b, just in case (a, b) B. The

diagonal set D corresponds to the complement of the sequence of boxes

along the main diagonal, boxes with crosses being replaced by boxes

without crosses, and vice versa. The sets Ra correspond to the rows of the

array. The diagonalization principle can then be rephrased: the complement

of the diagonal is different from each row.

2.2.11 Example: Let us consider the relation R = {(a, b), (a, d), (b, b), (b, c),

(c, c), (d, b), (d, c), (d, e), (d, f), (e, e), (e, f), (f, a), (f, c), (f, d), (f, e)}; notice

that Ra = {b, d}, Rb = {b, c}, Rc = {c}, Rd = {b, c, e, f}, Re = {a, e}, and

Rf = {c, d, e}. All in all, R may be pictured like this:

Page 48: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 42

The sequence of boxes along the diagonal is

x x x

Its complement is

x x x

which corresponds to the diagonal set D = {a, d, f}. Indeed, D is different

from each row of the array; for D, because of the way it is constructed,

differs from the first row in the first position, from the second row in the

second position, and so on.

Mathematical induction is the process of proving a general theorem or

formula involving the positive integer n from particular cases.

A proof by mathematical induction consists of the following two steps.

i) Show by actual substitution that the theorem is true for n = 1

ii) Assuming the theorem to be true for n = m, prove that it is also true for

n = m + 1.

Note that here m is a particular value of n. From (i), the theorem is true for n = 1

and from (ii), it is true for n=1+1=2 ; since it is true for n=2 it follows from

(iii) that it is also true for n = 2+1 = 3 and so on. Hence, theorem is true for

all positive integral values of n.

Page 49: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 43

2.2.12 Proof by Contradiction

Proof by contradiction is sometimes very useful technique to prove that

some statements are true. In this technique, let us assume that property P is

not true. Using logical reasoning, we have to get a conclusion that

contradicts the given conditions.

2.2.13 Example

Prove by contradiction, that 2 is not a rational number.

Solution: A rational number is of the form p/q where q 0, and p, q are not

having any common factors.

Assume that 2 is a rational number. So it can be written as

2 = p/q.

p2 = 2q2.

p2 is even p is even.

If p is even, then it can be written as p = 2k. Therefore, 4k2 = 2q2. Therefore,

q is even.

This is a contradiction to our assumption that p and q have no common

factors. Therefore, 2 is not a rational number.

Self Assessment Questions

1. Prove by mathematical induction that

4

1nnn...321

223333 .

2. Prove that 8 is not a rational number (by the method of contradiction).

3. Prove that the product of two odd integers is an odd integer.

Page 50: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 44

2.3 Summary

We introduced a variety of different methods of proof and illustrated how

each method is used. This unit is useful for several other important proof

methods, where we consider different cases separately and proof where we

prove the existence of objects with desired properties.

2.4 Terminal Questions

1. What is wrong with the following purported proof that all horses are the

same color?

The proof is by induction on the number of horses.

Basic step: There is only one horse. Then clearly all horses have same

color.

Induction Hypothesis: In any group of up to n horses, all horses have

the same color.

Induction Step: Consider a group of n+1 horses. Discard one horse; by

induction hypothesis, all the remaining horses have the same color.

Now put that horse back and discard another; again all the remaining

horses have the same color. So all the horses have the same color as

the ones that were not discarded either time and so they all have the

same color.

2.5 Answers

1. i) For 1n , left side 113

right side

14

4.1

4

11122

Hence it is true for 1n

Page 51: MC0082 - Theory of Computer science

Theory of Computer Science Unit 2

Sikkim Manipal University Page No.: 45

ii) Assume the result to be true for mn

Then

4

1mmm...321

223333 (induction

hypothesis)

Adding the th1m term viz., 31m to both sides,

322

33331m

4

1mm1mm...21

4m4m

4

1m 22

4

2m1m22

4

11m1m22

Therefore the result is true for 1mn . Hence by mathematical

induction the given result is established for all positive integers.

3. Hint: Take two odd integers m and n. Then there exist two integers r

and t so that m = 2r + 1 and n = 2t + 1.

Page 52: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 46

Unit 3 Trees and Binary Trees

Structure

3.1 Introduction

Objectives

3.2 Graphs

3.3 Trees

3.4 Binary Trees

3.5 Walks and Paths

3.6 Directed Graphs

3.7 Summary

3.8 Terminal Questions

3.9 Answers

3.1 Introduction

The last three decades have witnessed more interest in Graph Theory,

particularly among applied mathematicians and engineers. Graph Theory

has a surprising number of applications in many developing areas. The

Graph Theory is also intimately related to many branches of mathematics

including Group Theory, Matrix Theory, Automata and Combinatorics.

Graph Theory serves as a mathematical model for any system involving a

binary relation.

Objectives:

At the end of the unit you would be able to

learn the concept, graph and illustration.

know the Trees and Binary Trees.

know the weighed graphs and applications.

find the spanning tree of weighted graphs

define a directed graph

Page 53: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 47

3.2 Graphs

3.2.1 Definitions

A graph G = (V, E) consists of a nonempty set of objects,

V = {v1, v2, …} called vertices and another set, E = {e1, e2, …} of

elements called edges such that each edge „ek‟ is identified with an

unordered pair {vi, vj} of vertices.

The vertices vi, vj associated with edge ek are called the end vertices of

ek. An edge associated with a vertex pair {vi, vi} is called a loop (or) self-

loop.

If there is more than one edge associated with a given pair of vertices,

then these edges are called parallel edges (or) multiple edges.

3.2.2 Example

Consider the graph given here.

This is a graph with five vertices and seven edges. Here G = (V, E) where

V = {v1, v2, v3, v4, v5} and E = {e1, e2, e3, e4, e5, e6, e7}.

The identification of edges with the unordered pairs of vertices is given by

e1 {v2, v2}, e2 {v2, v4}, e3 {v1, v2},

e4 {v1, v3}, e5 {v1, v3}, e6 {v3, v4}.

Here „e1‟ is a loop and e4, e5 are parallel edges.

3.2.3 Definition

A graph that does not have self-loop nor even parallel edges is called a

v1 e1

e6

e7

e2

e3

e4

v3 v4

v5 e5

v2

Page 54: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 48

simple graph. Graph containing either parallel edges or loops is referred as

general graph.

3.2.4 Example

It can be observed that the two graphs given in Figures A and B are one and

the same.

3.2.5 Definition

i) If a vertex v is an end vertex of some edge e, then v and e are said to

be incident with (or on, or to) each other.

ii) Two non-parallel edges are said to be adjacent if they are incident on a

common vertex. Two vertices are said to be adjacent if they are the

end vertices of the same edge.

iii) The number of edges incident on a vertex v is called the degree of v.

The degree of a vertex v is denoted by d(v). It is to be noted that a self-

loop contributes two to the degree of the vertex.

3.2.6 Theorem

The sum of the degrees of the vertices of a graph G is twice the number of

edges. That is, Viv

i )v(d = 2e. (Here e is the number of edges).

Proof: (The proof is by induction on the number of edges „e‟).

Case-(i): Suppose e = 1. Suppose f is the edge in G with f = uv.

Then d(v) = 1, d(u) = 1. Therefore,

4

3

2

1

Fig-A

2 4

3

1

Fig-B

Page 55: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 49

Vx

)x(d =

= }v,u{\Vx

)x(d + d(u) + d(v)

= 0 + 1 + 1 = 2 = 2

= 2 (number of edges).

Hence the given statement is true for n = 1.

Now we can assume that the result is true for e = k - 1.

Take a graph G with k edges.

Now consider an edge „f‟ in G whose end points are u and v.

Remove f from G.

Then we get a new graph G* = G - {f}.

Suppose d*(v) denotes the degree of vertices v in G*.

Now for any x {u, v}, we have d(x) = d*(x), and

d*(v) = d(v) - 1, d*(u) = d(u) - 1.

Now G* has k - 1 edges. So by induction hypothesis

Viv

i*

)v(d = 2(k - 1).

Now

2(k - 1) = Viv

i*

)v(d = }v,u{iv

i*

)v(d + d*(u) + d*(v)

= }v,u{iv

i )v(d + (d(u) - 1) + (d(v) - 1)

= }v,u{iv

i )v(d + d(u) + d(v) - 2 = Viv

i*

)v(d - 2

2(k - 1) + 2 = Viv

i*

)v(d 2k = Viv

i )v(d .

Hence, by induction we get that “the sum of the degrees of the vertices of

the graph G is twice the numbers of edges”.

Page 56: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 50

3.2.7 Problem

The number of vertices of odd degrees is always even.

Solution: We know that the sum of degrees of all the „n‟ vertices (say, vi,

1 i n) of a graph G is twice the number of edges (e) of G. So we have

n

1i

i )v(d = 2e --------- (i)

If we consider the vertices of odd degree and even degree separately, then

n

1i

i )v(d = isevenjv

j )v(d + isoddkv

k )v(d -------- (ii)

Since the L.H.S of (ii) is even (from (i)) and the first expression on the RHS

side is even, we have that the second expression on RHS is always even.

Therefore,

oddiskv

k )v(d --------- (iii)

is an even number.

In (iii), each d(vk) is odd. The number of terms in the sum must be even to

make the sum an even number. Hence the number of vertices of odd

degree is even.

3.2.8 Definition

A graph H is said to be a sub-graph of a graph G if all the vertices and all

the edges of H are in G, and each edge of H has the same end vertices in

H as in G.

3.2.9 Example

The graphs H and K are subgraphs of graph G.

Page 57: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 51

1

6

f

Self Assessment Questions

1. Every graph is a ______of itself;

2. A subgraph of a subgraph of G is a _______of G;

3. A single vertex in a graph G is a _____of G

4. A single edge in G together with its end vertices is a ____of G.

3.3 Trees

The concept of a „tree‟ plays a vital role in the theory of graphs. First, we

introduce the concept of „tree‟, study its properties and some of its

applications. Later, we introduce the concept of „spanning tree‟, and study

the relationships among circuits and trees.

3.3.1 Definition

A connected graph without circuits is called a tree.

3.3.2 Example

Trees with one, two, three and four vertices are given.

1

2

3

6

i b

d j

Graph K

3

Graph H

4

1

b

d

c

a

e

2 g

4

2

3

a

5

b c

d

e

h

j

i

Graph G

Page 58: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 52

3.3.3 Note

Since a tree is a graph, we have that a tree contains at least one vertex.

A tree without any edge is referred to as a null tree.

Since we are considering only finite graphs, we have that the trees

considered are also finite.

A tree is always a simple graph.

3.3.4 Definition

A connected graph is said to be minimally connected if the removal of any

one edge from the graph provides a disconnected graph.

Observation:

A graph G is a tree it is minimally connected.

If a graph G contains n vertices, n -1 edges and no circuits, then G is a

connected graph.

If G is a tree (with two or more vertices), then there exists at least two

pendant vertices.

3.3.5 Theorem

For a given graph G, with n vertices the following conditions are equivalent:

i) G is connected and is circuitless;

ii) G is connected and has n –1 edges;

iii) G is circuitless and has n –1 edges;

iv) There is exactly one path between every pair of vertices in G;

v) G is a tree.

3.4 Binary Trees

3.4.1 Definition

A tree in which one vertex (called the root) is distinguished from all the

other vertices, is called a rooted tree. In a rooted tree, the root is generally

marked in a small triangle.

Page 59: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 53

3.4.2 Example

Distinct rooted trees with four vertices were given in Fig.

Generally, the term „tree‟ means trees without any root. However, they are

sometimes called free trees or non-rooted trees. A verity of rooted trees

(called the Binary rooted trees) is of particular interest (since they are

extensively used in the computer search methods, binary identification

problems, and variable length binary codes).

3.4.3 Definition

A tree in which there is exactly one vertex of degree 2, and all other

remaining vertices are of degree one or three, is called a binary tree.

i) Clearly, the following graph represents a binary tree (because the only

vertex „v1‟ is of degree 2, and all other vertices are of degree either 1 or 3).

v1

v3

v2

v

4

v6

v8

v5

v11

v7

v10

v1

3

v9

v1

2

Page 60: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 54

ii) Since the vertex of degree 2 (that is, v1) is distinct from all other

vertices, this vertex v1 is the root.

iii) In a binary tree, the vertex with degree 2 serves as a root. So every

binary tree is a rooted tree.

3.4.4 Properties of Binary trees

Property (i): The number of vertices n, in a binary tree is always odd.

Property (ii): The number of pendent vertices is 2

1n .

Property (iii): Number of vertices of degree 3 is = n - p - 1 = n - (2

1n ) - 1

= 2

3n .

3.4.5 Example

In the above graph, we have that

N = 13, p = 2

1n =

2

113 =

2

14= 7.

Therefore, number of vertices of degree 3 is 2

3n =

2

313 = 5.

3.4.6 Definition

A non-pendent vertex in a tree is called an internal vertex.

Note:

i) The number of internal vertices in a Binary tree is 2

1n = (p -1) where

p = the number of pendent vertices.

ii) In the above binary tree, the internal vertices are v1, v3, v4, v5, v6, v9.

These are 6 (= 7 - 1 = p - 1) in number.

Page 61: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 55

3.4.7 Definition

Let v be a vertex in a binary tree. Then v is said to be at level i if v is at a

distance of i from the root.

3.4.8 Example

i) A 13-vertex, 4-level binary tree was given in Fig.

Here the number of vertices at levels 0, 1, 2, 3, 4 is 1, 2, 2, 4 and 4

respectively.

level 1

level 4

level 0

level 2

level 3

Page 62: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 56

Self Assessment Question

5. Draw the 11-vertex binary trees and also find its path length.

3.5 Walks and Paths

3.5.1 Definition

A finite alternating sequence of vertices and edges (no repetition of edge

allowed) beginning and ending with vertices such that each edge is

incident with the vertices preceding and following it, is called a walk.

Vertices with which a walk begins and ends are called the terminal

vertices of the given walk.

The remaining vertices in the walk are called intermediate vertices of the

walk.

A walk is said to be a closed walk if the terminal points are same.

3.5.2 Example

i) From the above definition, it is clear that no edge appears more than

once in a walk.

ii) Consider the graph given. We can observe that “v1av2bv3cv3d v4e v2f v5”

is a walk.

c

v4

v3

v5

v2

v1

g

b

d e

h

f

a

Page 63: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 57

The set of vertices and edges constituting a given walk in a graph G forms a

subgraph of G. A walk which is not closed, is called an open walk. [In other

words, a walk is said to be an open walk if the terminal points are different].

In the above example, the walk “v1av2bv3cv1” is a closed walk and the walk

“v1av2bv3dv4ev2fv5” is an open walk.

3.5.3 Definition

An open walk, in which no vertex appears more than once, is called a path

or simple path.

(In other words, an open walk is said to be a path if it does not intersects

itself. The number of edges in a path is called the length of the path).

3.5.4 Example

We can observe that “v1av2bv3dv4” is a path, and “v1av2bv3cv3dv4ev2f v5” is a

walk but not a path (because this walk contains repeated vertex v2).

i) Consider the graph given above, the path “v1av2bv3dv4” is of length 3.

ii) An edge that is not a self-loop is a path of length one.

iii) A self-loop is a walk but not a path.

iv) Consider a path as a subgraph. With respect to the subgraph, we have

that terminal vertices of the path are of degree one and the rest of the

vertices (these are called intermediate vertices) are of degree two. For

example, consider the path v1av2bv3dv4 (refer the graph given). With

respect to this path d(v1) = 1, d(v2) = 2, d(v3) = 2, d(v4) = 1.

3.5.5 Note

Consider the graph given below Observe the following.

i) If there is no repetition of vertices in a walk, then the walk is a non-

intersecting walk.

Page 64: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 58

ii) Observe that a walk is intersecting there lies repetition of vertices.

iii) v1v5, v5v3, v3v4, v4v5, v5v2 is a walk.

3.5.6 Definition

A collection of trees is called a forest.

A tree T is said to be a spanning tree of a connected graph G if T is a

subgraph of G and T contains all the vertices of G.

3.5.7 Example

Consider the graph G given in Fig A. Graph T (given in Fig B) is a spanning

tree of G.

v4

v3 v2

v1

v5

v6

v3

v1

v4 v2

v5 v7

b

2

b3

b1

b6

b4

c5

c6

c8

c7

c1

b5

c2

c3

c

4

Fig- A Graph-G

v7

v1

v2

v3

v4

v5

v6

Fig B Graph-T

Page 65: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 59

3.5.8 Note

i) Since spanning trees are the largest (with the maximum number of

edges) trees among all trees in G, we have that a spanning tree is also

called a maximal tree subgraph or maximal tree of G.

ii) Spanning is defined only for a connected graph. (since a tree is always

connected). However, each component of a disconnected graph, does

have a spanning tree. Thus a disconnected graph with k components

contains a spanning forest consisting of k spanning trees.

3.5.9 Theorem

Every connected graph has at least one spanning tree.

Proof: Let G be a connected graph. If G has no circuit, then G is a spanning

tree. If G has a circuit, then delete an edge from this circuit. If till leaves the

graph connected. If there are more circuits, repeat the operation till an edge

from the last circuit is deleted, leaving the graph connected, circuitless, and

contains all the vertices of G. Thus the subgraph obtained is a spanning tree

of G. Hence every connected graph has at least one spanning tree.

3.5.10 Definitions

A graph G is said to be a weighted graph if all the edges e of G were

assigned by a corresponding real number (e).

Let T be any spanning tree of a connected graph G. Then the weight of

T, that is, (T) is defined as the sum of weights of all branches in T.

A spanning tree with the smallest weight in a weighted graph is called a

minimal spanning tree.

3.5.11 Note

i) Different spanning trees of G may have different weights. Among all the

spanning trees of G, one with the smallest weight is of practical

significance.

Page 66: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 60

ii) Let G be a graph on n vertices in which every edge has a unit weight .

Then all the spanning trees have the same weight of (n -1) units.

3.5.12 Kruskal algorithm for finding shortest spanning tree

Step-(i): List all the edges of G in order of non-decreasing weight. Now we

select an edge e1 of G such that (e1) is as small as possible and „e1‟ is not

a loop.

Step-(ii): Select next smallest edge from the set of all remaining edges of

G such that the selected edges do not form a circuit with the edges that

have already been chosen.

Step-(iii): We continue this process of taking smallest edges among those

not already chosen, provided no circuit is formed with those, that have been

chosen already.

[If edges e1, e2, …, ei have been chosen, then chose ei+1 from E \ {e1, e2,

…, ei} in such way that graph with {e1, e2, …, ei+1} is acyclic and (ei+1) is

as small as possible].

Step-(iv): If a graph G has „n‟ vertices, then we will stop this process after

choosing n - 1 edges. These edges form a subgraph T, which is not cyclic.

(Thus T is a shortest spanning tree of G).

Self Assessment Questions

7. Consider the following graph: Write the paths, lengths of path, cycles,

and also specify whether or not it‟s simple path/closed path.

Page 67: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 61

3.6 Directed Graphs

3.6.1 Definition

A directed graph (or) a digraph G consists of a non-empty set V (the

elements of V are normally denoted by v1, v2, …) and a set E (the elements

of E are normally denoted by e1, e2, ….) and a mapping that maps every

element of E onto an ordered pair (vi, vj) of elements from V.

The elements of V are called as vertices or nodes or points. The elements

of E are called as edges or arcs or lines. If e E and vi, vj V such that

(e) = (vi, vj), then we write e = jviv . In this case, we say that e is

an edge between vi and vj. (We also say that e is an edge from vi to vj).

(We also say that e originates at vi and terminates at vj). (An edge from vi

to vj is denoted by a line segment with an arrow directed from vi to vj).

3.6.2 Note

i) A directed graph is also refereed to as an oriented graph.

ii) Let G be a directed graph and e = vu .

3.6.3 Example

The graph given in figure is a digraph with 5 vertices and ten edges.

e9

v2

e10

e5

e7

v1

e8

e6

v4

v3

v5

e1 e4

e3 e2

Page 68: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 62

Here v5 is the initial vertex and v4 is the terminal vertex for the edge e7. The

edge e5 is a self-loop.

3.6.4 Definition

The number of edges incident out of a vertex v is called the out degree

of v. The out degree of a vertex v is denoted by d+(v).

The number of edges incident into v is called the in-degree of v. The in-

degree of a vertex v is denoted by d-(v).

Observation: The degree of v is equal to the sum of in-degree and out

degree of v, for any vertex v in a graph.

(In symbols, we can write as d(v) = d+(v) + d-(v) for all vertices v).

Self Assessment Questions

7. Consider the graph given in Example: 3.6.3. Write the indegrees and

outdegrees of the graph.

3.7 Summary

This Unit is meant for beginning your process of learning Graph Theory. It

started with the definition of Graph and illustrated the concepts like

incidence, degree, isolated vertex, pendent vertex and null graph. We also

discussed the isomorphism between graphs and subgraphs of a given graph

with appropriate illustrations. We dealt with a special type of graphs called

trees and studied some properties. We considered the binary search tree

and their properties. We presented some illustrations on binary search tree,

complete binary search trees and directed graphs.

3.8 Terminal Questions

1. Define the terms: Graph, finite graph, infinite graph, incidence, degree,

isolated vertex, pendent vertex, null graph.

2. Show that the sum of the degrees of the vertices of a finite graph G is

twice the number of edges.

Page 69: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 63

3. Show that the number of vertices of odd degree is always even.

4. Show that an infinite graph with finite number of edges must have an

infinite number of isolated vertices.

5. Show that the maximum degree of any vertex in a simple graph is (n - 1).

6. Draw all regular binary trees

a) with exactly 7 vertices

b) with exactly 9 vertices

7. Draw all distinct binary tree with (i) 3 vertices, (ii) 4 vertices.

8. Draw binary search trees for the following lists.

i) 18, 44, 2, 5, 73, 45, 14, 6, 8, 10, 20, 11

ii) 2, 1, 5, 6, 8, 9, 7, 3, 4.

7. Define a binary tree, complete binary tree and give examples of each.

3.9 Answers

Self Assessment Questions

1. Every graph is a sub graph of itself;

2. A subgraph of a subgraph of G is a subgraph of G;

3. A single vertex in a graph G is a subgraph of G; and

4. A single edge in G together with its end vertices is a subgraph of G.

5. (i)

level -1

level - 0

level -2

level -3

Page 70: MC0082 - Theory of Computer science

Theory of Computer Science Unit 3

Sikkim Manipal University Page No.: 64

The path length: 2 + 2 + 3 + 3 + 3 + 3 = 16.

(ii)

The path length: 1 + 2 + 3 + 4 + 5 + 5 = 20.

6.

Path Length Simple

(yes/no)

Closed (yes/no)

Circuit (yes/no)

Cycle (yes/no)

a-d-c-e-g-j-d-a 7 no yes no no

b-c-e-f-g-j-f-b 7 no yes yes no

a-b-a 2 No Yes No No

a-d-c-b-a 4 Yes Yes Yes Yes

i-i 1 Yes Yes Yes Yes

a 0 Yes Yes No No

e-f-g-j-f-b 5 No No No No

d-b-c-d 3 yes yes yes Yes

7.

i) Here d+(v1) = 3, d+(v2) = 1, d+(v3) = 1, d+(v4) = 1, d+(v5) = 4. d - (v1) = 1,

d - (v2) = 2, d - (v3) = 4, d -(v4) = 3, d - (v5) = 0.

ii) d(v1) = 4 = 3 + 1 = d+(v1) + d-(v1) ; d(v2) = 3 = 1 + 2 = d+(v2) + d-(v2),

and so on.

level 5

level 0

level 1

level 2

level 4

level 3

Page 71: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 65

Unit 4 Grammar and Formal Languages

Structure

4.1 Introduction

Objectives

4.2 Grammars and Languages

4.3 Types of Grammar

4.4 Summary

4.5 Terminal Questions

4.6 Answers

4.1 Introduction

The basic machine instructions of a digital computer are very primitive

compared with the complex operations that must be performed in various

disciplines such as engineering, science, management and mathematics.

Even though a complex procedure can be programmed in machine

language, it is desirable to use a high level language that contains

instructions similar to those required in a particular application. The

specification of a programming language involves the set of symbols and set

of correct programs.

Objectives:

At the end of the unit you would be able to

learn to construct the language using grammar.

construct the grammar, for a given language.

4.2 Grammars and Languages

A language L can be considered as a subset of the free monoid on an

alphabet. It is a set of strings or sentences over some finite alphabet. Finite

Page 72: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 66

languages can be specified by exhaustively enumerating all their sentences.

Any device which specifies a language should be finite. A simple method of

specification which satisfies this requirement uses a generative device is

referred as grammar. Precisely, a grammar consists of a finite set of rules or

productions which specify the syntax of the language.

The theory of formal languages exclusively involves the study of the

language syntax and this theory incepts from the works of well-known

linguist Noam Chomsky.

The goal of Chomsky was to define the syntax of natural languages using

simple and clear mathematical rules in order to precisely characterize the

structure of language. The primary idea behind this concept was to define a

formal grammar for describing the natural languages like English so that

language translation using a computer would become easy. Chomsky

developed the mathematical model of grammar in 1956. However, rather

than becoming useful for natural languages, this model turned out to be

suitable for the grammar of computer languages.

In order to simplify the concepts of grammar in computer languages, you

need to have some basic idea of two types of sentences used in English

and how these sentences are constructed. The first type of sentence

contains only noun and verb such as „Jack sang‟, while the other type

contains noun, verb and adverb such as „Sandy ran swiftly‟. The sentence

„Sandy ran swiftly‟ has the words „Sandy‟, „ran‟, „swiftly‟ in the order of noun,

verb and adverb. You can replace „Sandy‟ any other noun such as „Tom‟

and „Jim‟, „ran‟ by any verb in the past tense „jumped‟ and „drank‟ and

„swiftly‟ by any adverb like „quickly‟ and „fast‟. These replacements can give

other grammatically correct sentences like „Sandy ran swiftly‟. So, the

structure of this type of sentence can be defined <noun> <verb> <adverb>.

Here, for <noun>, you substitute any name „Sandy‟, „Jack‟, „Hana‟, etc.

Page 73: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 67

Similarly, <verb> can be substituted by „ate‟, „ran‟ „drank‟, etc., while the

<adverb> can be substituted by „slowly‟, „„quickly‟, etc. In the same way, you

can define the structure of the first t sentence <„Jack sang‟> by <noun> <verb>.

It is seen that the sequence <noun> <verb> <adverb> does not exactly

represent a sentence; rather, it provides description of a particular type of

sentence. Replacing <noun>, <verb> and <adverb> with appropriate words,

you can produce grammatically correct sentences. The terms <noun>,

<verb> and <adverb>, are known as variables and the suitable words that

can sentences are known as terminals. This signifies that a sentence is

nothing but a string of terminals. Now, if the variable S denotes a sentence

then you can form the rules below to generate two different types of

sentences:

S <noun> <verb> <adverb>

S <noun> <verb>

<noun> sandy

<noun> Jack

<noun> Sam

<verb> ran

<verb> jumped

<verb> ate

<adverb> swiftly

<adverb> quickly

In the above representation, each of the arrows specifies a rule, which

depicts that the work on the left side of the arrow can be replaced by the

word on the right side of the arrow. These rules may be called production

rules and let denote these production rules. Now if you consider that your

vocabulary is restricted to „Sandy‟, „Jack‟, „Sam‟, „ran‟, „jumped‟, „ate‟,

Page 74: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 68

„swiftly‟ and „quickly‟ to form sentences of the form <noun> <verb>

<adverb>, then you can describe the grammar as 4-tuple.

Before presenting the formal definition of grammar, we review some

preliminary notations and definitions.

4.2.1 Definition

Let S denote a nonempty set of symbols, called an alphabet. We assume

that S to be finite. The elements of the set are called letters. A word or a

string on the set S is a finite sequence of the elements.

4.2.2 Example

Take S = {a, b}. Then x = abab, y = aaab, z = aaabb are strings on S.

4.2.3 Definition

The length of the string is the number of symbols in the string.

In the above example, length of x is 4, length of z is 5.

(denote respectively are | x | = 4, | z | = 5)

Note that a string is called empty if | | = 0. In this unit, we denote the

empty string by “ ”.

4.2.4 Properties of strings

Let S be the set of symbols, and S* denote the set of all strings (including

empty string).

i) Concatenation on S* associative

For any x, y, z S*, x (y z) = (x y) z.

ii) Identity: The empty string is an identity element for the operation.

That is, x = x = x for all x S*

iii) Cancellation: For any x, y, z S*,

x y = x z implies y = z (left cancellation)

y x = y w implies x = w (right cancellation)

iv) For x, y s*, | xy | = | x | + | y |

Page 75: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 69

4.2.5 Notation

VT = Finite non empty set of symbols (alphabet), called terminal

symbols.

(The strings of terminal symbols denoted by lower case

letters x, y , z, …)

VN = Set of non – terminal symbols, which are used to define the

syntax (or structure) of the language (A, B, C, …, X, Y, Z, …)

VN VT = Consisting of non terminal and terminal symbols, called

vocabulary of the language. (Strings of symbols over the

vocabulary are given by , , , … ).

,N TV V empty set (assumption).

If A 1, A 2, …, A n are n A-productions, then they

can be written as A 12 … n.

4.2.6 Definition

A grammar (phrase structure) is defined by a 4 – tuple G = (VN, VT, S, )

where S is a distinguished element of VN (called the starting symbol), is a

finite subset of the relation from

* * *

.T N N T N T NV V V V V to V V

In general, an element (, ) is written as (called a production rule or

a rewriting rule).

4.2.7 Example

Let the symbols

L : letter

D : digit

I : identifier

Write the grammar G = , , ,N TV V S

Where VN = {I, L, D}

Page 76: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 70

VT = {a, b, c, … , x, y, z}

S = I

= {I L, I IL, I ID, L a, L b, …, L z,

D 0, D 1, …, D 9}

4.2.8 Definition

Let G = , , ,N TV V S be a grammar. For

, (VN VT) * – {empty string}, is said to be a direct derivative of ,

(denoted as ) if there are strings 1 and 2 (including possibly empty

strings) such that 2121 ,and and is a production of

G. If , then we say that directly produces (or directly reduces to

).

4.2.9 Example

Consider the example 4.2.7, the direct derivatives are as follows:

Rule used 1 2

I L I L

Ib Lb I L b

Lb Ab L a b

LD L1 D 1 L

LD aD L a D

4.2.10 Definition

Let G = (VN, VT, S, ) be a grammar. The string produces (or is the

direct derivative of ), written as ,

if there are strings 0, 1,…, n,

(n > 0) such that n1n2110 ..., (The relation

is

the transitive closure of ).

Page 77: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 71

If n = 0, then the reflective transitive closure of as

*

.or

4.2.11 Definition

A sentential form is any derivative of the unique non terminal symbol S.

The language L generated by a grammar G is the set of all sentential forms

whose symbols are terminal.

That is, *

*

TL G S and V

This means that, the language is a subset of all terminal strings over VT.

4.2.12 Example

Let G = , , , , , , , , ,E T F a E

Where consists of productions

E E + T

E T

T T * F

T F

F (E)

F a

where the variables E (expression), T (term), and F (factor) used in

conjunction with arithmetic expressions.

We wish to derive the expression a * a + a as follows: Starting with the

symbol E.

E E + T

T + T

T * F + T

F * F + T

Page 78: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 72

a * F + T

a * a + T

a * a + F

a * a + a

4.2.13 Problem

Generate the language L (G) = 1n n na b c n by the following grammar.

, , , , , , ,G S B C a b c S

where consists of productions,

S asBC

S aBC

CB BC

aB ab

bB bb

bC bc

cC cc

Solution:

We generate the language for n = 2. That is, we derive the string a2b2c2.

S aSBC

aaBCBC

aaBBCC

aabBCC

aabbCC

aabbcC

aabbcc

4.2.14 Example

Consider the grammar , , , , ,G S C a b S

Page 79: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 73

where is the set of productions : S aCa

C aCa

C b

Generate the language: 1n na ba n .

Solution:

Derivation for n = 2. i.e., the string a2ba2

S aCa

aaCaa

aabaa

4.2.15 Problem

Generate the language L (G) = , 1n ma ba n m by the grammar.

, , , , , , ,G S A B C a b S

where is the set of productions.

, , , ,S aS S aB B bC C aC C a

Solution:

Take n = 2, m = 3. We generate the string a2ba3

S aS

aaB

aabC

aabaC

aabaaC

aabaaa

4.2.16 Problem

If G = ({S}, {0, 1}, {S 0S1, S }, S), then find L(G), the language

generated by G.

Page 80: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 74

Solution: Since S is a production, S . This implies that L(G).

Now, for all n 1, we can write the following:

S 0S1 00S11 … 0nS1n 0n1n.

Therefore, 0n1n L(G).

In the above derivation, at every step, S 0S1 is applied, except in the last

step where S is applied.

Therefore, {0n1n n 0} L(G).

Now suppose w L(G). So we should start the derivation of w with S.

If we are applying S first, then we will get w = .

Otherwise, the first production that we need to apply is S 0S1.

However, at any stage we can apply S to obtain the terminating string.

Therefore, w can be derived in the following form.

S 0nS1n 0n1n, for some n 1, That is L(G) {0n1n n 0}.

Hence L(G) = {0n1n n 0}.

4.2.17 Problem

Suppose G = ({S, A, B}, {0, 1}, , S) where consists of productions: S

0AB0, A 10AB1, B A01, 0A 100 and 1B1 0101. Show that w =

100110100011010 is in L(G).

Solution: To prove that the given w L(G), we need to start with an S-

production and subsequently apply the suitable productions in order to

derive w. The following sequences show the derivation of w.

S 0AB0

100B0

100A010

10010AB1010

1001100B1010

1001100A011010

Page 81: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 75

100110100011010 = w L(G).

In this sequence, the strings that can be replaced are underlined.

4.2.18 Problem

Suppose G = ({S, A, B}, {a, b}, , S), where consists of the following

productions.

S abAB, A aBb, B abA, bA bab, aB aaa.

Show that w = abaaababaaab L(G).

Solution:

Here, we can follow the proof by starting with an S-production and

subsequently applying the suitable productions in order, we can derive w.

The following sequences show the derivation of w:

S abAB

abaBbB

abaaabB

abaaababA

abaaababaBb

abaaababaaab. Hence w L(G).

Self Assessment Questions

1. Suppose G = ({S, A, B}, {a, b}, , S) where consists of the following

productions: S abAB, A aBb, B abA, bab, aB aaa.

Then verify whether or not w = abaaababaaab L(G).

2. Consider the string x = well, find all prefixes and suffixes of x. Also

find all subwords of x.

3. Let x = 0100, y = 11. Find xy and yx.

4. Given the strings u = a2bab2 and v = bab2, find the strings uv, vu, v2,

u. Also find their lengths.

Page 82: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 76

5. Let A = {ab, bc, ca}. Find whether the following strings

(i) abc, (ii) ababab, (iii) abba, (iv) bcabbab

belong to A*.

4.3 Types of Grammar

Every language is specified by a particular grammar. The classification of

languages is based on the classification of the grammar used to specify

them. Grammar is classified accordingly to the types of productions.

4.3.1 Definition

i) A grammar in which there are no restrictions on its productions is called

type – 0 grammar or unrestricted grammar 0L T .

ii) A grammar that contains only productions of the form where

is called type – 1 grammar or context sensitive grammar.

The language generated by this grammar is called context sensitive

language 1L T .

iii) A grammar that contains only productions of the form where

and NV is called type – 2 grammar or context free

grammar. The language generated by this grammar is called context

free language 2L T .

iv) A grammar that contains only productions of the form , where

, NV and has the form a, B or a, where a VT, B VN is

called type -3 grammar or regular grammar. The language generated

by this grammar is called a regular language 3L T .

Page 83: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 77

v) A grammar G(VN, VT, S, ) is called monotonic when every

production in is of the form having ≤ or S . In the

second situation, S does not appear on the right hand side of any of the

production of G.

In other words, in any production, the left hand string is always a

single non – terminal and right hand string is either a terminal or a terminal

followed by a non – terminal.

4.3.2 Theorem

If G be type 0 grammar, then we can find an equivalent grammar G1 where

each production is either of the form or A a. Here, and are the

strings variables, A is a variable and a is a terminal.

Proof: To construct G1, consider a production in G with or having

the same terminals. Let in both and , a new variable Ca replaces each of

the terminals to produce ‟s and ‟s.

Now, for every , where and have same terminals, we can get a

corresponding with productions of the form Ca a for each terminal

that appears on or . Therefore, the new productions obtained from the

above constriction are the new productions for G1. Also, the variables of G

along with the new variables of the form Ca are the variables of G1.

Similarly, the terminals and the start symbol of G1 are also same as those of

G. Hence, G1 satisfies the required conditions for a grammar and it is

equivalent to G. Therefore L(G) = L(G1).

4.3.3 Note

i) The above theorem also holds for grammar of type 1, 2 and 3.

ii) (ii) 3 2 1 0L T L T L T L T .

4.3.4 Theorem

Every monotonic grammar G is equivalent to type 1 grammar.

Page 84: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 78

4.3.5 Problem

Construct a grammar for the language.

L = {aaaa, aabb, bbaa, bbbb}

Solution:

Since L has a finite number of strings, we can list all strings in the language.

Let VT = {a, b} be the set of terminals.

VN = {S}, non terminal (starting symbol)

Productions: S aaaa

S aabb

S bbaa

S bbbb

We simplify the productions as follows.

Let VN = {S, A}

: , , .S AA A aa A bb

Therefore, the Grammar , , , , ,T NG V a b V S A S

4.3.6 Problem

Construct a grammar for the language.

*, , ' 3L x x a b the number of a s in x is a multiple of

Solution:

Let T = {a, b} and N = {S, A, B},

S is a starting symbol.

The set of productions:

S bS

S b

S aA

A bA

A aB

Page 85: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 79

B bB

B aS

B a

For instance,

bbababbab can be generated as follows.

S bS bbS bbaA bbabA bbabaB bbababbB bbababbaS

bbababbab

Therefore the grammar , , , , , ,T NG V a b V S A B S

4.3.7 Problem

Find the highest type number that can be applied to the following

productions:

1. S A0, A 1 2 B0, B 012.

2. S ASB b, A bA c

3. S bS bc.

Solution:

1. Here, S A0, A B0 and B 012 are of type 2, while A 1 and

A 2 are type 3. Therefore, the highest type number is 2.

2. Here, S ASB is of type 2, while S b, A bA and A c are type 3.

Therefore, the highest type number is 2.

3. Here, S bS is of type 3, while S ab is of type 2. Therefore, the

highest type number is 2.

Notation: Let L0, Lcs, Lcf and Lr are the family of type 0, context sensitive,

context free and regular languages respectively.

4.3.8 Operations on Languages

In formal languages, there are certain common operations. These

operations include standard set operations such as intersection, union and

complementation operations. These are other operations such as string

Page 86: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 80

operations that are applied element-wise on the languages. For example, if

we consider two languages L1 and L2 over some common alphabet, then we

can define the following operations.

Concatenation: It combines the two languages to produce the

concatenated language denoted by L1L2. Here, L1L2 consists of all the

strings of type xy, where x is a string in L1 and y is a string in L2.

Intersection: It produces the language L1 L2 which consists of all the

strings that are contained in both the languages L1 and L2.

Union: It produces the languages L1 L2 which consists of all the strings

that are contained in either of the languages L1 and L2.

Complement: It produces the language L1 from the language L1. Here,

L1 is known as the complement of the language L1 with respect to an

alphabet, where L1 consists of all the strings over the alphabets that are

not in the language L1.

These operations are generally used in determining the closure properties of

the classes of languages. A class of languages is called closed under some

operation, when applying the operation to the class of languages always

produces a language in the same class.

4.3.9 Theorem

The languages L0, Lcs, Lcf and Lr are closed under the operations

concatenation and union.

4.3.10 Problem

Construct a grammar for the language.

, 1,i jL a b i j i j

Solution:

We decompose 1 2L L L where

Page 87: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 81

1 2

i j i jL a b i j and L a b i j

Grammar for L1: Set of production for L1

A aA

A aB

B aBb

B ab,

where VT = {a, b} , VN = {A, B}

A is a starting symbol.

Grammar for L2:

VT = {a, b}, VN = {C, D}, C is starting symbol.

Productions: C Cb

C Db

D aDb

D ab

Now by adding the two sets of productions, S A, S C with S as the

starting symbol, we get the grammar

, , , , , , ,T NG V a b V S A B C S

where : S A, S C, A aA, A aB, B aBb, B ab, C Cb,

C Bb.

4.3.11 Problem: Obtain a grammar to generate the language

L = {0i1j i j, i 0 and j 0}.

Solution: It is clear from the statement that it a string has n number of 0‟s

as the prefix, this prefixed string should not be followed by n number of 1‟s,

that is, we should not have equal number of 0‟s and 1‟s. At the same time,

0‟s should precede 1‟s. The grammar for this can be written as:

G = (VN, VT, , S) where

Page 88: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 82

VN = {S, A, B, C}

VT = {0, 1}

Productions

: S 0S1 (generates 0i1j recursively)

S A (to generate more 0‟s than 1‟s)

S B (to generate more 1‟s than 0‟s)

A 0A 0 (at least one 0 is generated)

B 1B 1 (at least one 1 is generated); and

S is the starting symbol.

4.3.12 Problem

Obtain a grammar to generate the language

L = {x / x mod 3 = 0} on the set VT = {a}.

Solution: The language accepted by the grammar can also be written as

L = {, aaa, aaaaaa, aaaaaaaaa, …}.

It is clear from this definition that any string generated should have the

length multiple of 3 which can be easily done by the production rule:

S aaaS .

Therefore, the final grammar is

G = (VT, VN, , S), where

VT = {S}

VT = {a}, and the set of productions

: S aaaS ,

S is the starting symbol.

Self assessment Questions

6. Find the language for the grammar.

0,1 , , ,T NG V V S S

where the set of productions

: 11 , 0.S S S

Page 89: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 83

7. Find the language L (G), generated by the grammar.

, , , , , ,T NG V x y z V S A S

where : , , , .S xS S yA A yA A Z

8. Find the language L (G), generated by the grammar

, , , ,T NG V a b V S S where

: , , .S aaS S a S b

4.4 Summary

In this unit, we studied the formal languages and develop mathematical

expressions, phrase structure grammar, a simple device for the construction

of useful formal languages. Some types of grammar depending on their

productions were discussed. These are useful for generating algorithms.

4.5 Terminal Questions

1. Construct the grammar which generates the following language and also

specify their types.

i) 1 , 3n mL a b n m

(Hint: (i) , , , , ,T NG V a b V S B S

Where is : , , , .S aS S bbB B bB B b

It is a regular language).

ii) 1n nL a ba n

(Hint: , , , ,T NG V a b V S S

Where is: , .S aSa S b

It is a context – free – language).

Page 90: MC0082 - Theory of Computer science

Theory of Computer Science Unit 4

Sikkim Manipal University Page No.: 84

iii) 1 , 1n mL a ba n m

(Hint: , , , , , ,T NG V a b V S A S where

: , , , , , .S aAb S bAa A bAa A aAb A ab A ba

It is a context – free – language).

4.6 Answers

Self Assessment Questions

1. Yes, w L(G).

2. , w, we, wel, well;

, l, ll, ell, well;

, w, e, l, we, el, ll, wel, ell, well.

3. xy = 010011, yx = 110100.

4. uv = a2bab4ab2, uv = 11

vu = bab2a2bab3, vu = 11;

v2 = bab3ab2, v2 = 8;

u = a2bab2, u = 6

5. No, Yes, No, No.

6. ( ) 0, 110 ,11110, 1111110,L G

7. ( ) 0, 1n mL G x y z n m

8. 2 1 2( ) 0 0n nL G a n a b n

Page 91: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 85

Unit 5 Deterministic Finite Automata (DFA)

Structure

5.1 Introduction

Objectives

5.2 Basic Terms

5.3 Deterministic Finite Automata (DFA)

5.4 Transition System (Transition graph):

5.5 Language accepted by a DFA

5.6 Summary

5.7 Terminal Questions

5.8 Answers

5.1 Introduction

A study of finite automaton is their applicability to the design of several

common types of computer algorithms and programs. For example, the

lexical analysis phase of a compiler (in which program units such as „begin‟

and „+‟ are identified) is often based on the simulation of a finite automaton.

Also, the problem of finding an occurrence of a string within another, for

example, whether any of the strings air, water, earth, and fire occur in the

text of Elements of the Theory of Computation, can also be solved efficiently

by methods originating from the theory of finite automata.

Page 92: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 86

Let us now describe the operation of a finite automaton in more detail.

Strings are fed into the device by means of an input tape, which is divided

into squares, with one symbol inscribed in each tape square (see figure).

The main part of the machine itself is a “black box” with innards that can be,

at any specified moment, in one of a finite number of distinct internal states.

This black box - called the finite control - can sense what symbol is written

at any position on the input tape by means of a movable reading head.

Initially, the reading head is placed at the leftmost square of the tape and

the finite control is set in a designated initial state.

At regular intervals the automaton reads one symbol from the input tape and

then enters a new state that depends only on the current state and the

symbol just read. This is why we shall call this device a deterministic finite

automaton. After reading an input symbol, the reading head moves one

square to the right on the input tape so that on the next move it will read the

symbol in the next tape square. This process is repeated again and again; a

symbol is read, the reading head moves to the right, and the state of the

finite control changes. Eventually, the reading head reaches the end of the

input string. The automaton then indicates its approval or disapproval of

what it has read by the state it is in at the end: if it winds up in one of a set of

final states the input string is considered to be accepted. The language

accepted by the machine is the set of strings it accepts.

Objectives:

At the end of the unit you would be able to

understand the idea of DFA.

draw transition diagram.

find the language accepted by a DFA.

apply the techniques to various finite automata problems.

know applications of DFA.

Page 93: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 87

5.2 Basic Terms

Input: The various inputs i1, i2, …, ip applied at the input side of the model

are the elements of an input set, , also called the input alphabet.

Output: The various outputs o1, o2, …, oq generated at the output side of

the model are the elements of an output set O, also called the output

alphabet.

States: The entire automata system, at any given instant of time, is in any

one of the states q1, q2, …, qn. (These are labeled with circles)

State relation: State relation helps determine the next state that the

automaton system is going to attain. State relation takes into consideration

the present input and the present state of the system in determining its next

state.

Output relation: It helps to determine the next output of the automaton

system. The output relation may take into consideration only the current

input or both the current input and the current state for determining the next

output.

5.2.1 Definition

An automata system in which the output depends only on the present input

is called a Moore machine. Alternatively, an automata system in which the

output depends both on the present input and the present state is called

Mealy machine.

We will learn more about the Moore machine and the Mealy machine (vice

versa) in the next unit.

Self Assessment Questions

1. The symbol used for _______

2. The symbol O used for ________

3. In an automaton system, the states are represented by _________

Page 94: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 88

4. State relation helps to determine _________

5. An automata system in which the output depends only on the present

input is called a ________

An automaton system in which the output depends both on the present

input and the present state is called ________

5.3 Deterministic Finite Automata (DFA)

The first type of automata, we study in detail the finite accepters that are

deterministic in their operation. We start with a precise formal definition of

deterministic accepters.

5.3.1 Definition

A DFA is 5-tuple or quintuple M = (Q, , , q0, F) where

Q is non-empty, finite set of states.

is non-empty, finite set of input alphabet.

is transition function, which is a mapping from Q to Q. For this

transition function the parameters to be passed are state and input symbol.

Based on the current state and input symbol, the machine may enter into

another state.

q0 Q is the start state.

F Q is set of accepting or final states.

5.3.2 Note

For each input symbol a, from a given state there is exactly one transition

(there can be no transitions from a state also) and we are sure (or can

determine) to which state the machine enters. So, the machine is called

Deterministic machine. Since it has finite number of states the machine is

called Deterministic finite machine (automaton).

5.3.3 Illustration: Let us take the pictorial representation of DFA shown in

figure and understand the various components of DFA.

Page 95: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 89

It is clear from this diagram that, the DFA is represented using circles,

arrows and arcs labeled with some digits, concentric circles etc. The circles

are nothing but the states of DFA. In the DFA shown in figure, there are

three states viz., q0, q1 and q2. An arrow enters into state q0 and is not

originating from any state and so it is quite different from other states and is

called the start state or initial state. The state q2 has two concentric circles

and also a special state called the final state or accepting state. In this DFA,

there is, only one final state. Based on the language accepted by DFA, there

can be more than one final state also.

The states other than start state and final states are called intermediate

states. Always the machine initially will be in the start state. It is clear from

the figure that, the machine in state q0, after accepting the symbol 0, stays in

state q0 and after accepting the symbol 1, the machine enters into state q1.

Whenever the machine enters from one state to another state, we say that

there is a transition from one state to another state. Here we can say that

there is a transition from state q0 to q1 on input symbol 1.

In state q1, on input symbol 0, the machine will stay in q1 and on symbol 1,

there is a transition to state q2. In state q2, on input symbol 0 or 1, the

machine stays in state q2 only. This DFA has three states q0, q1 and q2 and

can be represented as

Q = {q0, q1, q2}, the possible input symbols set = {0, 1}, which is set of

input symbols (alphabet) for the machine.

q0 q1 q2

Start state

Final state

0 0

0, 1

1 1

Page 96: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 90

There will be a transition from one state to another based on the input

alphabet. If there is a transition from vi, to vj on an input symbol a, it can be

represented as (vi, a) = vj.

The transitions from each state of the machine shown in figure based on the

input alphabet {0, 1} are shown in table.

State input Output Transition Representation

q0 0 q0 (q0, 0) = q0

q0 1 q1 (q0, 1) = q1

q1 0 q1 (q1, 0) = q1

q1 1 q2 (q1, 1) = q2

q2 0 q2 (q2, 0) = q2

q2 1 q2 (q2, 1) = q2

Self Assessment Questions

6. Consider the example 5.3.3.

Write the states, input alphabet, final states, starting state.

5.4 Transition System (Transition graph)

A finite directed labeled graph in which each node or vertex of the graph

represents a state and the directed edges from one node to another

represent transition of a state. All the edges of the transition graph are

labeled as input/output. For example, an edge labeled 1/0 specifies that for

a certain initial state if the input is 1, then the output is 0.

Consider the following diagram:

In the transition graph as shown in the figure,

The initial state, q0, of the system is represented by a circle with an

arrow pointing towards it.

The final state, q1, is represented by two concentric circles.

The directed edges from the initial state to the final state are labeled as

input/output.

Page 97: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 91

5.4.1 Example

The graph represents the DFA,

M = (Q = {q0, q1, q2}, = {0, 1}, , q0 = initial state, F = {q1}), where is given

by

(q0, 0) = q0,

(q0, 1) = q1,

(q1, 0) = q0,

(q1, 1) = q2,

(q0, 0) = q2,

(q2, 1) = q1.

Representation of DFA using Transition table:

In this method, the DFA is represented in the tabular form. This table is

called transitional table. There is one row for each state, and one column

for each input. Since, in the transition diagram shown in the fig., there are

three states, there are three rows for each state. The input symbols are

only 0 and 1 so, there are two columns for the input symbols. The

transitional table for the diagram is given below.

States 0 1

q0

q1

q0 q1

q1 q2

q2 q1 q2

Page 98: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 92

Self Assessment Questions

7. Construct the state table for the following DFA.

5.5 Language accepted by a DFA

Consider the transition diagram or DFA shown in figure. The start state is q0

and the final state is q2. To start with the machine will be in start state q0.

Verification of acceptance of the string 1011:

Let us assume that the string 1011 is the input. On first input symbol 1, the

machine enters into state q1. In state q1, on input symbol 0, the machine

stays in state q only. In state q1, on input symbol 1, the machine enters into

state q2. In state q2, on the input symbol 1, the machine stays in state q2.

Now we encounter end of the input and note that we are in the accepting

state q2. The moves made by the DFA for the string 1011. Therefore, after

scanning the input string 1011, the machine finally stays in state q2.

Verification of non-acceptance of the string 1011:

Take the string 0100: The moves made by the machine for the string 0100

are clear from the following figure. Note that after scanning the string 0100

q0 q1 q2

Start state

Final state

0 0 0, 1

1 1

q0 q1 q2

Start state

Final state

0 0

0, 1

1 1

Page 99: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 93

the machine stays in state q1 which is not a final state. Therefore, the string

0100 is rejected by the machine.

5.5.1 Definition

Let M = (Q, , , q0, F) be a DFA where

Q is non-empty, finite set of states.

is a non-empty, finite set of input alphabet.

is a transition function, which is a mapping from Q to Q. For this

transition function the parameters to be passed are state and input symbol.

Based on the current state and input symbol, the machine may enter into

another state.

q0 Q is the start (or initial) state.

F Q is set of accepting or final states.

The string (also called language) w accepted by a DFA can be defined as

follows.

L(M) = {w w * and

(q0, w) F}.

Non-acceptance means that after the string is processed, the DFA will not

be in the final state and so the string is rejected. The non-acceptance of the

string w by a DFA can be defined in notation as:

)M(L = {w w * and

(q0, w) F}, where

: Q * Q, is an extended

transition function. The second argument of

is a string, rather than a

single symbol, This value gives the state of after reading that string.

(For example, in the above figure, (q0, 1) = q1 and (q1, 1) = q2. So,

(q0, 11) = q2).

Page 100: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 94

5.5.2 Properties:

1. (q, ) =

(q, ) = q

2. (q, wa) = (

(q, w), a)

3. (q, aw) =

((q, a), w), where q Q, a , w *.

5.5.3 Example

For the DFA shown in fig. given below, what is

(q0, 101)?

Solution:

Property 1: This means that when the current state of the machine is q and

when there is no input ( means no input), the machine will not move to any

new state, instead, it stays in the same state q.

Property 2: Consider

(q0, 101) = (

(q0, 10), 1), here w = 10 and a = 1.

Now

(q0, 10) = (

(q0, ), 1)

= (q0, 1)

= q1.

Therefore,

(q0, 101) = (

(q0, 10), 1) = (q1, 1) = q2.

5.5.4 Example

Obtain a DFA to accept strings of a‟s and b‟s starting with the string ab.

q0 q1 q2

Start state

Final state

0 0

0, 1

1 1

Page 101: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 95

Solution: It is clear that the string should start with ab and so, the minimum

string that can be accepted by the machine is ab.

To accept the string ab, we need three states and the machine can be

written as

where q2 is the final or accepting state. In state q0, if the input symbol is b,

the machine should reject b (note that the string should start with a). So, in

state q0, on input b, we enter into the rejecting state q3. The machine for

this can be of the form

The machine will be in state q1, if the first input symbol is a. If this a is

followed by another a, the string aa should be rejected by the machine. So,

in state q1, if the input symbol is a, we reject it and enter into q3 which is the

rejecting state. The machine for this can be of the form –

q0 q1 q2

Start state

Final state

a b

q3

b

q0 q1 q2

Start state

Final state

a b

Page 102: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 96

Whenever the string is not starting with ab, the machine will be in state q3

which is the rejecting state. So, in state q3, if the input string consists of a‟s

and b‟s of any length, the entire string can be rejected and can stay in state

q3 only.

The resulting machine can be of the form –

The machine will be in state q2, if the input string starts with ab. After the

string ab, the string containing any combination of a‟s and b‟s, can be

accepted and so remain in state q2 only. The complete machine to accept

the strings of a‟s and b‟s starting with the string ab is shown in figure above.

The state q3 is called trap state or rejecting state.

q0 q1 q2

Start state

Final state

a b

q3

b a

q0 q1 q2

Start state

Final state

a b

q3

b

a

a, b

Page 103: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 97

In the set notation, the language accepted by DFA can be represented as

L = {ab(a + b)n n 0}

Or

L = {ab(a + b)*}

Therefore, the DFA which accepts strings of a‟s and b‟s starting with the

string ab is given by M = (Q, , , q0, F), where

Q = {q0, q1, q2, q3}, = {a, b}, q0: initial state, F = {q2}, and the transition

function is defined as

States a b

q0

q1

q3

q1 q3

q3 q2

q2 q2

q3 q3

To accept the string abab: The string is accepted by the machine.

(q0, abab) = (

(q0, aba), b)

q0 q1 q2

Start state

Final state

a b

q3

b

a

a, b

a, b

q2

Page 104: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 98

= ( (

(q0, ab), a), b)

= ( ( ( (q0, a), b), a), b)

= ( ( (q1, b), a), b)

= ( (q2, a), b)

= (q2, b)

= q2 F.

To reject the string aabb:

(q0, aabb) = (

(q0, aab), b)

= ( (

(q0, aa), b), b)

= ( ( ( (q0, a), a, b), b)

= ( ( (q1, a), b), b)

= ( (q3, b), b)

= (q3, b)

= q3 F.

Therefore, the string aabb is not accepted by the machine.

5.5.5 Note

Sometimes we ignore the extended notion

and we use only (assuming

that the reader is well-known with it).

5.5.6 Example

Consider a finite Automata that will accept the set of natural numbers which

are divisible by 3,

F,,q,,QM 0 , where = {0, 1, 2, … , 9}, Q = {q0, q1, q2}

F = {q0}, q0 is the starting state.

IIxQ: defined by

Page 105: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 99

8,5,2c,7,4,1b,9,6,3,0a .

Consider the string 142.

2,14,q142,q 00

= 2,4,1,q0

2,4,q1

2,q2

Fq1

Therefore, the string 142 is not accepted by M.

Consider the string 150.

Now 0,5,1,q150,q 00

0,5,q1

0,q0

Fq0

Therefore, 150, is accepted by M.

5.5.7 Example

Obtain a DFA to accept even number of a‟s, and odd number of a‟s.

Solution: Observe the following transition diagrams.

Consider the string aa: (q0, aa) = ((q0, a), a) = (q1, a) = q0, which is a

final state (acceptable state).

a b c

q0 q0 q1 q2

q1 q1 q2 q0

q2 q2 q0 q1

Page 106: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 100

Consider the string aaa: (q0, aaa) = ( (q0, aa), a) = ( ( (q0, a), a),

a) = ( (q1, a), a) = (q0, a) = q1, which is a acceptable state.

5.5.8 Problem

Obtain DFA to accept strings of a‟s and b‟s having exactly one a, at least

one a, not more than three a‟s.

Solution:

To accept exactly one a:

To accept exactly one a, we need two states q0 and q1 and make q1 as the

final state. The machine to accept one a is shown below.

In q0, on input symbol b, remains q0 only so that any number of b‟s can end

with one a. The machine for this can be of the form –

q1

q0

a

q0 q1

a

q1

q0

a

a

a

Page 107: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 101

In q1, on input symbol b, remains q1 and machine can take the form

But, in state q1, if the input symbol is a, the string has to be rejected as the

machine can have any number of b‟s but exactly one a. So, the string has

to be rejected and we enter into a trap state q2. Once the machine enters

into trap state, there is now way to come out of the state and the string is

rejected by the machine. The complete machine is shown in the figure.

In the set notation, the language accepted by DFA can be represented as

L = {bmabn m, n 0}.

The machine M = (Q, , , q0, F), where

Q = {q0, q1, q2}, = {a, b}, q0: initial state, F = {q1}, and the transition function

is defined as –

States a b

q0

q2

q1 q0

q2 q1

q2 q2

q0 q1

a

b b

q2

a, b

q0 q1

a

b b

q0 q1

a

b

q1

Page 108: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 102

The machine to accept at least one a: The minimum string that can be

accepted by the machine is a. For this, we need two states q0 and q1 where

q1 is the final state. The machine for this is shown below.

In state q0, if the input symbol is b, remains in q0. Once the final state q1is

reached, whether the input symbol is a or b, the entire string has to be

accepted. The machine to accept at least one a is shown in fig.

In set notation, the language accepted DFA can be represented as

L = {bma(a + b)n m, n 0}.

The machine M = (Q, , , q0, F), where

Q = {q0, q1}, = {a, b}, q0: initial state, F = {q1}, and the transition function

is defined as –

States a b

q0

q1 q0

q1 q1

The machine to accept not more than three a’s: The machine should

accept not more than three a‟s means,

q0 q1

a

b a, b

q0 q1

a

q1

Page 109: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 103

It can accept zero a‟s

It can accept one a

It can accept two a‟s

It can accept 3 a‟s

But, it can not accept more than three a‟s.

In this machine, maximum of three a‟s can be accepted (that is, the machine

can accept zero a‟s, one a, two a‟s). So, we need maximum four states q0,

q1, q2 and q3, where all these states are final states and q0 is the start state.

In state q3, if the input symbol is a, the string has to be rejected and we

enter into a trap state q4. Once this trap state is reached, whether the input

symbol is a or b, the entire string has to be rejected and remain in state q4.

Now, the machine can take the form as shown below.

In state q0, q1, q2 and q3, if the input symbol is b, stay in their respective

states and the final transition diagram is shown below.

q1

a a

a q0

q2 q3 q4

a a

a, b

q1

a a

a

q0

q2 q3

Page 110: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 104

In set notation, the language accepted DFA can be represented as

L = {biabjabkabl i, j, k, l 0}

The DFA is M = (Q, , , q0, F) where Q = {q0, q1, q2, q3, q4}, = {a, b}, q0 is

the start state, F = {q0, q1, q2, q3}, and is the transition function.

States a b

q4

q1 q0

q2 q1

q3 q2

q4 q3

q4 q4

5.5.9 Problem

Obtain a DFA to accept the language L = {w / w mod 5 0} on = {a, b}.

Solution: The number of symbols in a string consisting of a‟s and b‟s should

not have multiples of 5. The machine to accept the corresponding language

is shown in below.

q1

a a

a

q0 q2

q3 q4

a

a

a, b

b b b b

q0

q1

q2

q3

Page 111: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 105

Self Assessment Questions

8. Draw a DFA to accept strings of a‟s and b‟s with even number of a‟s and

even number of b‟s. Also find the language accepted by DFA.

5.6 Summary

The concept of finite automata is used in wide applications. Large natural

vocabularies can be described using finite automata which includes the

applications such as spelling checkers and advisers, multi-language

dictionaries, to indent and documents, in calculators to evaluate complex

expressions based on the priority of an operator etc. In this unit, we give a

comprehensive idea about the DFA and a graphical representation of DFA.

Further we discussed the language accepted by DFA with certain examples.

5.7 Terminal Questions

1. What is DFA ? Explain with example.

2. When we say that a language is accepted by the machine? Illustrate

with example.

3. Obtain a DFA to accept strings of 0‟s and 1‟s starting with at least two

0‟s and ending with at least two 1‟s. Also find the language accepted by

this.

q1

a

a

a

q0 q2 q3

a, b

Page 112: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 106

(Hint:

The language accepted by DFA can be represented as

L = {w w 00(0+1)*11}

4. Obtain a DFA to accept strings of a‟s and b‟s with at most two

consecutive b‟s.

Page 113: MC0082 - Theory of Computer science

Theory of Computer Science Unit 5

Sikkim Manipal University Page No.: 107

5.8 Answers

Self Assessment Questions

1. Input alphabet.

2. Output alphabet.

3. These are labeled with circles.

4. The next state that the automaton system is going to attain.

5. Moore machine; Mealy machine.

6. States: {q0, q1, q2}, Input alphabet: {0, 1}, final state: {q2}, starting state {q0}.

7.

States 0 1

q0

q1

q0 q1

q1 q2

q2 q2

8. The language accepted by DFA is

L = {w w (a + b)* and total number of strings in both a and b are

even}.

q2

Page 114: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 108

Unit 6 Nondeterministic Finite Automata (NFA)

Structure

6.1 Introduction

Objectives

6.2 Nondeterministic Finite Automata

6.3 Language accepted by a NFA

6.4 Construction from NFA to DFA

6.5 Moore and Mealy Machines

6.6 Summary

6.7 Terminal Questions

6.8 Answers

6.1 Introduction

Consider the transition table as shown below. To start with, the machine will

be in q0. If the first input symbol is a, the machine can enter into either state

q1 or q2 (since there are two transitions on input symbol a from state q0).

In state q0, if the input symbol is b, the machine enters into state q2.

Similarly, from state q1, there are multiple transitions on an input symbol a to

the states q1 and q2. That is, the machine can either enter into state q2 or

state q1. At this point of time, we can not determine exactly in which state

the machine will be. So, this is called Nondeterministic Finite Automata

(NFA).

The transitions from each state of the machine shown below based on the

input alphabet {a, b}.

Current state input Next state Representation

q0 a q1, q2 (q0, a) = {q1, q2}

q0 b q2 (q0, b) = q2

q1 a q1, q2 (q1, a) = {q1, q2}

q1 b q0, q1 (q1, b) = {q0, q1}

q2 a q2 (q2, a) = q2

q2 b q1 (q2, b) = q1

Page 115: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 109

Let Q is the set of states, then we define 2Q to represents the set of subsets

of Q.

Objectives:

At the end of the unit you would be able to

understand the idea of NFA.

draw transition diagram for NFA.

find the language accepted by NFA.

know applications of NFA.

learn Moore and Mealy Machines

6.2 Nondeterministic Finite Automata

There is another type of finite state automata in which there may be several

possible next states for each of input value and state. Such machines are

called nondeterministic.

6.2.1 Definition

A NFA is 5-tuple or quintuple M = (Q, , , q0, F) where,

Q is non-empty, finite set of states.

is non-empty, finite set of input alphabet.

is transition function, which is a mapping from Q to set of subsets of Q

(that is, 2Q). This function accepts two arguments as the input with first

argument being q Q and the symbol argument as the symbol a and

returns a set of states which is reachable from q on input a. This function

shows change of state from one state to a set of states based on the input

symbol.

q0 Q is the start state.

F Q is set of accepting or final states.

Page 116: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 110

6.2.2 Note: Suppose (p, ) = A where q Q, A 2Q and a . Here

there will be a transition from state q to the set of states A on an input

symbol a. The transition function can be extended to

, whenever string

operations are involved. The transition from state q to the set of states A,

on the input string w can be written as

(q, w) =A.

6.2.3 Properties:

1. (q, ) =

(q, ) = q

2. (q, wa) = (

(q, w), a) = Aj

3. (q, aw) =

((q, a), w) = Aq, where q Q, a , w * and Aj and Aq

are the set of states which are reachable from q.

6.3 Language Accepted by NFA

A string is a sequence of symbols obtained from . The set of all strings

recognized by and automata is called language. The language L accepted

by an NFA M = (Q, , , q0, F) is defined as follows.

6.3.1 Definition

Let M = (Q, , , q0, F) be an NFA where Qs is the set of finite states, is

set of input alphabet (from which a string can be formed), is transition from

Q to 2Q, q0 is the start state and F is the final state. The string

(also called language) w accepted by an NFA can be defined in formal

notation as:

L (M) = {w w * and

(q0, w) = P with at least on component of P in F}.

Here, P is set of states.

Page 117: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 111

(In other words, the language consists of all strings w for which there is a

walk labeled w from the initial vertex of the transition graph to some final

vertex).

6.3.2 Note (Need for Non-deterministic finite automata)

Digital computers are deterministic machine. Given the input, the state of

the machine is predictable. Sometimes, constructing deterministic machine

is difficult compared to non-deterministic machine. In such cases, there is a

need to construct a machine very easily which can be achieved by

constructing an NFA. After constructing an NFA, DFA can be easily

constructed. This is an efficient mechanism to describe some complicated

languages concisely. So, practically non-deterministic machines will not

exist. But, one can construct an NFA easily and later that can be converted

into DFA.

6.3.3 Example

Obtain an NFA to accept the following language L = {w w ababn or aban,

where n 0}.

The machine to accept ababn where n 0 is shown below.

The machine to accept aban where n 0 is shown below.

Since both the machines accept a as the first input symbol, the states q1 and

q5 can be merged into a single state and the machine to accept either ababn

or aban where n 0 is shown below.

6.4 Conversion from NFA to DFA

Let MN = (QN, N, N, q0, FN) be an NFA and accepts the language L(MN).

There should be an equivalent DFA, MD = (QD, D, D, q0, FD) such that

L(MD) = L(MN). The procedure to convert an NFA to its equivalent DFA is

shown below.

Page 118: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 112

Step 1: The start state of MN is the start state of MD. So, add q0 (which is

the start state of MN) to Q and find the transitions from this state. The way

to obtain different transitions is shown in the next step.

Step 2: For each state {qi, qj, …, qk} QD, the transitions for each input

symbol in can be obtained as shown below.

1. D({qi, qj, …, qk}, a) = N(qi, a) N(qj, a) … N(qk, a) = {ql, qm, …,

qn}(say)

2. Add the state {ql, qm, …, qn} to Q, if it is not already in QD.

3. Add the transition from {qi, qj, …, qk} to {ql, qm, …, qn} on the input

symbol a if and only if the state {ql, qm, …, qn} is not added to QD in the

previous step.

Step 3: The state {qa, qb, …, qc} Q is the final state, if at least one of the

states in qa, qb, …, qc FN (that is, at least one component in {qa, qb, …, qc}

should be the final state of NFA.

6.4.1 Example

Convert the following NFA to its equivalent DFA.

Solution:

Step 1: q0 is the start DFA, so QD = {{q0}} _______________(i).

Step 2: Fine the new states from each state in QD and obtain the

corresponding transitions.

Consider the state {q0}:

When a = 0, D ({q0}, 0) = N ({q0}, 0) = {q0, q1}

When a = 1, D ({q0}, 1) = N ({q0}, 1) = {q1}.

Since the states obtained above are not in QD, add these two states to QD

so that

QD = {{q0, q1}, {q0, q1}, {q1}} ____________(ii).

Page 119: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 113

The corresponding transition on a = 0 and a = 1 are shown below.

Sta

tes

0 1

{q0} {q0, q1} {q1}

{q0, q1}

{q1}

Consider the state {q0, q1}:

When a = 0, D ({q0, q1}, 0) = N ({q0, q1}, 0)

= N (q0, 0) N (q1, 0)

= {q0, q1} {q2}

= {q0, q1, q2}.

When a = 1, D ({q0, q1}, 1) = N ({q0, q1}, 1)

= N (q0, 1) N (q1, 1)

= {q1} {q2}

= {q1, q2}.

Since the states obtained above are not defined in QD (refer (ii)), add these

two states to QD so that

QD = {{q0, q1}, {q0, q1}, {q1}, {q0, q1, q2}, {q1, q2}} ____________(iii).

Sta

tes

0 1

{q0} {q0, q1} {q1}

{q0, q1} {q0, q1, q2} {q1, q2}

{q1}

{q0, q1, q2}

{q1, q2}

Consider the {q0}:

When a = 0, D ({q1}, 0) = N ({q1}, 0) = {q2}

When a = 1, D ({q1}, 1) = N ({q1}, 1) = {q2}.

Page 120: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 114

Since the states obtained above are same and {q2} is not in QD, add the

state {q2} so that

QD = {{q0, q1}, {q0, q1}, {q1}, {q0, q1, q2}, {q1, q2}, {q2}} _______(iii).

Sta

tes

0 1

{q0} {q0, q1} {q1}

{q0, q1} {q0, q1, q2} {q1, q2}

{q1} {q2} {q2}

{q0, q1, q2}

{q1, q2}

{q2}

Consider the state {q0, q1, q2}:

When a = 0, D ({q0, q1, q2}, 0) = N ({q0, q1, q2}, 0)

= N (q0, 0) N (q1, 0) N (q2, 0)

= {q0, q1} {q2}

= {q0, q1, q2}.

When a = 1, D ({q0, q1, q2}, 1) = N ({q0, q1, q2}, 1)

= N (q0, 1) N (q1, 1) N (q2, 1)

= {q0, q1} {q2} {q2}

= {q1, q2}.

Since the states obtained above are not new states (are already in QD), do

not add these two states to QD. But, the transitions on a = 0 and a = 1

should be added to the transitional table.

Sta

tes

0 1

{q0} {q0, q1} {q1}

{q0, q1} {q0, q1, q2} {q1, q2}

{q1} {q2} {q2}

{q0, q1, q2} {q0, q1, q2} {q1, q2}

{q1, q2}

{q2}

Page 121: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 115

Consider the state {q1, q2}:

When a = 0, D ({q1, q2}, 0) = N ({q1, q2}, 0)

= N (q0, 0) N (q2, 0)

= {q2}

= {q2}.

When a = 1, D ({q1, q2}, 1) = N ({q1, q2}, 1)

= N (q1, 1) N (q2, 1)

= {q2} {q2}

= {q2}.

Since the states obtained above are not new states (are already in QD), do

not add these two states to QD. But, the transitions on a =a0 and a = 1

should be added to the transitional table as shown.

Sta

tes

0 1

{q0} {q0, q1} {q1}

{q0, q1} {q0, q1, q2} {q1, q2}

{q1} {q2} {q2}

{q0, q1, q2} {q0, q1, q2} {q1, q2}

{q1, q2} {q2} {q2}

{q2}

Consider the state {q2}:

When a = 0, D ({q2}, 0) = N ({q2}, 0) =

When a = 1, D ({q2}, 1) = N ({q2}, 1) = {q2}.

Since the states obtained above are not new states (already in QD), do not

add these two states to QD. But, the transition on a = 0 and a = 1 should be

added to the transitional table. The final transitional table and the final DFA

are shown below.

Page 122: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 116

Sta

tes

0 1

{q0} {q0, q1} {q1}

{q0, q1} {q0, q1, q2} {q1, q2}

{q1} {q2} {q2}

{q0, q1, q2} {q0, q1, q2} {q1, q2}

{q1, q2} {q2} {q2}

{q2} {q2}

6.4.2 Example

Construct a DFA corresponding to the NFA as shown below.

States 0 1

q0

q1

q0 q1

q1 q0

q0, q2

Solution: The DFA corresponding to the given NFA is

Sta

tes

0 1

{q0} {q2} {q0, q1}

{q2} {q0, q1} --

{q0, q1} {q1, q2} {q0, q1}

{q1, q2} {q0, q1} {q0}

Self Assessment Questions

1. Obtain NFA to recognize the strings abc, abd and aacd assuming = {a,

b, c, d}.

6.5 Moore and Mealy Machines

The automata systems we have discussed so far are limited to binary

output. That is, the systems can either accept or reject a string. In those

q2

Page 123: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 117

systems, this acceptability is decided based on the reachability of the initial

state to the final state. This property of the system produces restrictions in

choosing outputs from some other alphabet, then output. You can remove

this constraint using both the Moore and Mealy machine, which help in

generating an output from a certain output alphabet.

Let us denote the output function with the symbol . Thus, when the output

of an automata system is dependent only on the present state, then,

Output = (q(t)), where q(t) is the present state.

The above automata system is called a Moore machine.

Alternatively, when the output of the automata system is dependent on both

the present input and the present state, then,

Output = (q(t), x(t)), where q(t) is the present state and x(t) is the present

input.

The above automata system is called a Mealy machine.

Since the output of a Mealy machine is dependent on both the input and the

present state, no output is generated when the input is a null string. This

implies that when the input is , the output is also . However, in case of the

Moore machine, there is some output of the Moore machine only dependent

on the present state and not on the present input. Hence, when the input to

a Moore machine is , the output is (q0).

6.5.1 Definition

A Moore machine can be with a 6-tuple (Q, , , , , q0) where:

Q is non-empty, finite set of states.

is non-empty, finite set of input alphabet.

is the output alphabet

is transition function, which is a mapping from Q into Q.

Page 124: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 118

is the output function mapping Q into

q0 Q is the start state.

Moore machine

Present State Next state Output

Input a = 0 Input a = 1

q0 q2 q0 1

q1 q0 q1 0

q2 q1 q2 1

From the state table of Moore machine as shown in the table, it is clear that

the output is common for both the inputs, which means the output is only

dependent on the present state and not on the present input.

6.5.2 Definition

A Moore machine can be with a 6-tuple (Q, , , , , q0) where:

Q is non-empty, finite set of states.

is non-empty, finite set of input alphabets.

is the output alphabet

is transition function, which is a mapping from Q into Q.

is the output function mapping E Q into

q0 Q is the start state.

Present State Input a = 0 Input a = 1

State Output State Output

q0 q2 1 q1 1

q1 q0 0 q2 1

q2 q1 0 q0 0

From the state table of Mealy machine as shown in the table, it is clear that

the output is dependent on both the present state and present input in a

Mealy machine.

Page 125: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 119

6.5.3 Conversion of Moore Machine into Mealy machine

The procedure of converting a Moore machine into a Mealy Machine is very

simple. From the given Moore machine state table, you need to transform each

column of inputs into the pairs of the next state and the output. The output for a

particular state in a pair can be determined by observing the outputs in first

table (Moore machine). If, after converting table to this format, it is observed

that for any two of the present state the other values in the row are identical,

then we can delete one of the states. Let us now consider a few examples to

convert a given Moore machine into a Mealy machine.

6.5.4 Example

Convert the Moore machine M1 whose state table is given in table into and

equivalent mealy machine.

Moore machine

Present State Next state Output

Input a = 0 Input a = 1

q0 q1 q2 1

q1 q3 q2 0

q2 q2 q1 1

q3 q0 q3 1

Solution: Let us first transform each column of inputs into the pairs of the

next state and the output as shown in the following table.

Present State Input a = 0 Input a = 1

State Output State Output

q0 q1 0 q2 1

q1 q3 1 q2 1

q2 q2 1 q1 0

q3 q0 1 q3 1

Since there are no two states in the above state table which have identical

row elements, the state table as shown represents the equivalent Mealy

machine for the Moore machine depicted above.

Page 126: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 120

6.5.5 Example

Convert the Moore machine whose state table is given into an equivalent

Mealy machine.

Moore machine

Present State Next state Output

Input a = 0 Input a = 1

q0 q0 q1 1

q1 q0 q2 1

q2 q0 q2 0

Solution: Let us first transform each column of inputs into the pairs of the

next state and the output as shown in the following table.

Present State

Next State

Input a = 0 Input a = 1

State Output State Output

q0 q0 1 q1 1

q1 q0 1 q2 0

q2 q0 1 q2 0

From the state table shown above, we observe that for the states q1 and q2

the rows are identical; hence, we can delete one of these states to generate

the equivalent Mealy machine as shown below.

Present State

Next State

Input a = 0 Input a = 1

State Output State Output

q0 q0 1 q1 1

q1 q0 1 q1 0

In this table, we replaced the occurrence of q2 in the remaining rows with q1.

Page 127: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 121

6.5.6 Conversion of Mealy machine into Moore Machine

Consider the following steps

Step 1: For a state qi determine the number of different outputs that are

available in state table of the Mealy machine.

Step 2: If the outputs corresponding to state qi in the next state columns are

same, then retain state qi as it is. Else, break qi into different states with the

number of new states being equal to the number of different outputs of qi.

Step 3: Rearrange the states and outputs in the format of a Moore machine.

The common output of the new state table can be determined by examining

the outputs under the next state columns of the original Mealy machine.

Step 4: If the output in the constructed state table corresponding to the

initial state is 1, then this specifies the acceptance of the null string by

Mealy machine. Hence, to make both the Mealy and Moore machines

equivalent, we either need to ignore the output corresponding to the null

string or we need to insert a new initial state at the beginning whose output

is 0; the other row elements in this case would remain the same.

Consider the following example, to convert a given mealy machine into a

Moore machine.

6.5.7 Example

Consider the Mealy machine:

Present State

Next State

Input a = 0 Input a = 1

State Output State Output

q1 q1 1 q2 0

q2 q4 1 q4 1

q3 q2 1 q3 1

q4 q3 0 q1 1

Page 128: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 122

After assessing the state table given above, we observe that the outputs for

the states q1 and q4 are same while the outputs for the states q2 and q3,

have two different values. Hence, after splitting states q2 and q3, we obtain

the state table shown below.

Present State

Next State

Input a = 0 Input a = 1

State Output State Output

q1 q1 1 q20 0

q20 q4 1 q4 1

q21 q4 1 q4 1

q30 q21 1 q31 1

q31 q21 1 q31 1

q4 q30 0 q1 1

Now, rearranging the state table as shown in table, into the Moore machine

format, we obtain the state table as shown below.

Moore machine

Present State Next state

Output Input a = 0 Input a = 1

q1 q1 q20 1

q20 q4 q4 0

q21 q4 q4 1

q30 q21 q31 0

q31 q21 q31 1

q4 q30 q1 1

Now, from the state table as shown above, we observe that the output

corresponding to the initial state q1 is 1. Hence, to ensure that the Mealy

and Moore machines are equivalent, we need to insert a row at the

beginning of the state table with present initial state q0.

Page 129: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 123

Moore machine

Present State Next state

Output Input a = 0 Input a = 1

q0 q1 q20 1

q1 q1 q20 1

q20 q4 q4 0

q21 q4 q4 1

q30 q21 q31 0

q31 q21 q31 1

q4 q30 q1 1

Self Assessment Questions

2. What do you understand by the term NFA?

3. What is a Moore machine?

4. What is a Mealy machine?

6.6 Summary

An automata is a system that performs some specific tasks without the

direct intervention of a human being. In this unit, we learnt the concept of

Non-deterministic finite automata. Construction of equivalent DFA from a

given NFA discussed. We also discussed a method to convert a Moore

Machine into a mealy machine (vice-versa). Some illustrations are

presented on these aspects.

6.7 Terminal Questions

1. Draw the transition diagram of the nondeterministic finite-state automata

whose next state function is given in the table.

States 0 1

q0 {q0, q1} {q2}

q1 (empty) {q1}

q2 {q1, q2}

Page 130: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 124

Hint:

2. Explain with the help of an example, the process of construction an

equivalent DFA from a given NFA.

6.8 Answers

Self Assessment Questions

1. The machine to accept the string abc is shown below.

The machine to accept the string abd is shown below.

The machine to accept the string aacd

q0 q2

Start state

0

1 0

0 1

q1

1

Page 131: MC0082 - Theory of Computer science

Theory of Computer Science Unit 6

Sikkim Manipal University Page No.: 125

2. An NFA is similar to a DFA with the only difference being in the definition

of . In the case of DFA, the output of transition function is a single

state q Q. However, in case of NFA, the output of transition function

is a set of states, which are an element of Q.

3. When the output of an automata system is dependent only on the

present state, then it is called a Moore machine.

4. When the output of an automata system is dependent both on the

present state and the present input, then it is called a Mealy machine.

Page 132: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 126

Unit 7 Regular Expressions and

Regular Languages

Structure

7.1 Introduction

Objectives

7.2 Regular expressions

7.3 Regular Expressions accepted by the Language

7.4 Finite Automata from Regular Grammar

7.5 Regular Grammar from Finite Automata

7.6 Summary

7.7 Terminal Questions

7.8 Answers

7.1 Introduction

In this unit, you will learn about regular expressions along with finite

automata, which act as a device for computing regular expressions. A

regular expression is a set of strings of symbols that can be generated by a

regular grammar using certain operations such as union, intersection and

concatenation. A regular expression also follows different identities that are

based on common mathematical operations such as addition and

multiplication. These identities help simplify the regular expression. A

regular expression can be accepted both by deterministic as well as non-

deterministic automata.

Objectives:

At the end of the unit you would be able to

explain the concept of regular expressions

understand the regular expression accepted by the language.

Convert finite automata from regular grammar.

Convert regular grammar from finite automata.

Page 133: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 127

7.2 Regular Expressions

In computing, regular expressions are used to represent a set of strings,

include symbols that are arranged using certain syntax rules. We can de

regular is expression R1 using terminal symbols such as and that are

elements of . Some of the algebraic operations defined with regular

expression are:

1. Union: The union of two regular expressions is also a regular

expression. For example, if R1 and R2 are the two regular expressions,

then the union R1 + R2 is also a regular expression.

2. Concatenation: The concatenation of two regular expressions is a

regular expression. For example, if R1 and R2 are the two regular

expressions, then the concatenation R1R2 is also a regular expression.

3. Iteration: The iteration of a regular expression is also a regular

expression. For example, if R1 is a regular expression, then the iteration

1R is also a regular expression.

4. Order of evolution: The order of evolution of a regular expression is a

regular expression. For example, if R1 is a regular expression, then

order of evolution (R1) is also a regular expression.

7.2.1 Definition

A regular expression is recursively defined as follows.

1. is a regular expression denoting an empty language.

2. is a regular expression indicates the language containing an empty

string.

3. a is a regular expression which indicates the language containing only

{a}

4. If R is a regular expression denoting the language LR and S is a regular

expression denoting the language LS, then

a. R+S is a regular expression corresponding to the language LR LS.

Page 134: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 128

b. RS is a regular expression corresponding to the language LR.LS.

c. R* is a regular expression corresponding to the language LR.

5. The expressions obtained by applying any of the rules from 1 to 4 are

regular expressions.

Note: If parentheses are not present in the regular expressions, then

precedence of the operands is as follows: iteration, concatenation and

union. First you need to perform the iteration operation, then the

concatenation operation and finally the union operation.

Note: Any set, which is represented by using a regular expression, is known

as regular set. If the regular expression is R, then the regular set of R is

L(R).

7.2.2 Example

Let x, y , where,

x represents the set {x}

x + y represents the set {x, y}

xy represents the set {xy}

x* represents the set {, x, xx, xxx, …}

(x + y)* represents the set{x + y}*

7.3 Regular Expressions accepted by the Language

7.3.1 Example

We give list of some examples of regular expressions and the language

corresponding to these regular expressions is given here.

Page 135: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 129

Regular Expressions

Meaning

(a+b)* Set of strings of a‟s and b‟s of any length including the NULL string.

(a+b)*abb Set of strings of a‟s and b‟s ending with the string abb

ab(a+b)* Set of strings of a‟s and b‟s starting with the string ab.

(a+b)*aa(a+b)* Set of strings of a‟s and b‟s having a sub string aa.

a*b*c*

Set of strings consisting of any number of a‟s(may be empty string also) followed by any number of b‟s(may include empty string) followed by any number of c‟s(may include empty string).

abc

Set of strings consisting of at least one „a‟ followed by string consisting of at least one „b‟ followed by string consisting of at least one „c‟.

aa*bb*cc*

Set of strings consisting of at least one „a‟ followed by string consisting of at least one „b‟ followed by string consisting of at least one „c‟.

(a+b)* (a + bb) Set of strings of a‟s and b‟s ending with either a or bb

(aa)* (bb)* b

Set of strings consisting of even number of a‟s followed by odd number of b‟s.

(0 + 1)*000 Set of strings of 0‟s and 1‟s ending with three consecutive zeros (or ending with 000).

(11)* Set of strings consisting of even number of 1‟s

(01)* + 1 The language consists of a string 1 or strings of (01)‟s that repeat zero or more times.

7.3.2 Example

Obtain a regular expression to accept a language consisting of strings of a‟s

and alternate a‟s and b‟s.

Solution: The alternate a‟s and b‟s can be obtained by “concatenating the

string ab zero or more times” which can be represented by the regular

expression

(ab)*

and adding an optional b to the front and adding an optional a at the end as

shown below:

( + b) (ab)* ( + a).

Page 136: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 130

Thus, the complete expression is given by

( + b) (ab)* ( + a)

7.3.3 Note

The expression can also be obtained as shown below:

The a‟s and b‟s can be generated using one of the following ways:

i. (ab)*

ii. b(ab)*

iii. (ba)*

iv. a(ba)*

Therefore, the expression to generate alternate a‟s and b‟s can be obtained

by taking the union of regular expressions as shown below:

(ab)* + b(ab)* + (ba)* + a(ba)*

7.3.4 Example

Obtain a regular expression to accept a language consisting of strings of 0‟s

and 1‟s with at most one pair of consecutive 0‟s.

Solution: It is clear from the statement that the string consisting of at most

one pair of consecutive 0‟s may

begin with combination of any number of 1‟s and 01‟s represented by

(1 + 01)*

end with any number of 1‟s represented by 1*.

Therefore, the complete regular expression which consists of strings 0‟s and

1‟s with at most one pair of consecutive 0‟s is given by

(1 + 01)*00 1*.

7.3.5 Example

Obtain a regular expression to accept a language containing at least one a

and at least one b, where = {a, b, c}.

Page 137: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 131

Solution: Strings of a‟s, b‟s and c‟s can be generated using the regular

expression

(a + b + c)*.

But this string should have at least one „a‟ and at least one „b‟. There are

two cases to be considered:

First „a‟ preceding „b‟ which can be represented using

c*a(a + c)*b

First „b‟ preceding „a‟ which can be represented using

c*b(b + c)*a

The regular expression (a + b + c)* can be preceded by one of the regular

expressions considered in the two cases just discussed.

Therefore, the final regular expression is

c*a(a + c)*b(a + b + c)* +c*b(b + c)*a(a + b + c)*

This expression can also be written as shown below:

[c*a(a+c)*b + c*b(b4c)*a] (a+b+c)*

7.3.6 Example

Obtain a regular expression to accept a language consisting of strings of a‟s

and b‟s of even length.

Solution: String of a‟s and b‟s of even length can be obtained by the

combination of the strings aa, ab, ba and bb.

The language may even consist of an empty string denoted by .

Therefore, the regular expression can be of the form

(aa + ab + ba + bb)*

The * closure includes the empty string.

The language corresponding to the regular expression is denoted by

L(R)={(aa + ab + ba + bb)n n 0}.

Page 138: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 132

7.3.7 Example

Obtain a regular expression to accept a language consisting of strings of a‟s

and b‟s of odd length.

Solution: String of a‟s and b‟s of odd length can be obtained by the

combination of the strings aa, ab, ba and bb followed by either a or b.

Therefore, the regular expression can be of the form

(aa + ab + ba + bb)* (a + b)

String of a‟s and b‟s of odd length can also be obtained by the combination

of the strings aa, ab, ba and bb preceded by either a or b.

Therefore, the regular expression can also be represented as

(a + b) (aa + ab + ba + bb)*.

Observation: Even though these two expressions seem to be different, the

language corresponding to those two expressions is the same.

7.3.8 Example

Obtain a regular expression such that L(R) = {w w {0, 1}* with at least

three consecutive 0‟s.

Solution: A string consisting of 0‟s and l‟s can be represented by the

regular expression

(0 + 1)*

This arbitrary string can precede three consecutive zeros and can follow

three consecutive zeros.

Therefore, the regular expression can be written as

(0 +1)* 000(0+1)*.

The language corresponding to the regular expression can be written as

L(R) = { (0 + 1)m000(0+1)n m 0 and n 0}.

Page 139: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 133

Self Assessment Questions

1. The regular expression (11)* stands for _______

2. The regular expression (01)* + 1 stands for _____

3. The regular expression (0 + 10)*1* stands for ______

7.4 Finite Automata from Regular Grammar

7.4.1 Definition:

A grammar G = (VN, VT, S, ) is said to be regular grammar the

grammar is right regular or left regular.

A grammar G is said to be right regular if all the productions are of the form

A wB and / or A w, where A, B VT and w VT*.

A grammar G is said to be left regular if all the productions are of the form

A Bw and / or A w, where A, B VT and w VT*.

7.4.2 Example

(i) The grammar with the set of productions

S aaB bbA

A aA b

B bB a

is a right linear grammar.

(ii) The grammar with the set of productions

S Baa Abb

A Aa b

B Bb a

is a left linear grammar.

7.4.3 Definition

A grammar which has most non terminal on the right side of any production

without restriction on the position of this non terminal (observe that: non

terminal can be leftmost or rightmost) is called linear grammar.

Page 140: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 134

7.4.4 Theorem

Let G = (VN, VT, S, ) be a right linear grammar. Then there exists a

language L(G) which is accepted by a finite automata, that is, the language

generated f7 from the regular grammar is a regular language.

Proof: Let V = {q0, q1, …} be the variables and S = q0 be the start state.

Let the productions in the grammar be

q0 x1q1

q1 x2q2

q2 x3q3

… … …

qn xn+1

Assume that the language L(G) generated from these productions is w.

Corresponding to each production in the grammar we can have a equivalent

transitions in the FA to accept the string w.

After accepting the string wm the FA will be in the final state.

The procedure to obtain FA from these productions is given below.

Step 1: The start symbol q0 in the grammar is the start state of FA.

Step 2: For each production of the form qi wqj, the corresponding

transition defined will be of the form

*(qi, w) = qj.

Step 3: For each production of the form qi w, the corresponding transition

defined will be of the form

*(qi, w) = qf, where qf is the final state.

Since the string w L(G) is also accepted by FA, by applying the transitions

obtained in step 1 through 3, the language is regular.

Page 141: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 135

7.4.5 Problem

Construct a DFA and the transition diagram, to accept the language

generated by the following grammar.

S 01A

A 10B

B 0A 11

Solution: Observe that each production of the form

A wB

the corresponding transition will be (A, w) = B

Also, for each production of the form A w, we can introduce the transition

(A, w) = qf, where qf is the final state.

The transitions obtained from grammar G is shown in the table.

Productions Transitions

S 01A

A 10B

B 0A

B 11

(S, 01) = A

(A, 10) = B

(B, 0) = A

(B, 11) = qf

The transition diagram is shown below.

Page 142: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 136

The DFA is

M = (Q, , , q0, F), where

Q = {S, A, B, qf, q1, q2, q3},

= {0, 1}, q0 = S (start state), F = {qf}, is shown in the table. Here, the

additional vertices (states) introduced are q1, q2, q3.

7.4.6 Problem:

Construct DFA and the corresponding transition diagram to accept the

language generated by the following grammar.

S aA

A aA bB

B bB

Solution: Observe that each production of the form

A wB,

the corresponding transition will be

(A, w) = B

Also, for each production of the form

A w,

we can introduce the transition

(A, w) = qf, where qf is the final state.

The transitions obtained from grammar G are shown in the table.

Productions Transitions

S aA

S

A aA

A bB

A

B bB

B

(S, a) = A

S is the final state

(A, a) = A

(A, B) = B

A is the final state

(B, b) = B

B is the final state

Observe that for each production of the form A , make A as the final

state.

Page 143: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 137

The transition diagram corresponding to this is shown below.

The DFA is

M = (Q, , , q0, F), where

Q = {S, A, B},

= {a, b}, q0 = S (start state), F = {S, A, B}, and is shown in the table.

7.5 Regular Grammar from Finite Automata

7.5.1 Theorem

Let M = (Q, , , q0, F) be a finite automata. If L is the regular language

accepted by FA, then there exists a right linear grammar G = (VN, VT, S, )

so that L = L(G).

Proof: Let M = (Q, , , q0, F), where Q = {q0, q1, …, qn}, = {a1, a2, …, am}.

A regular grammar G = (VN, VT, S, ) can be constructed, where

VN = {q0, q1, …, qn}, VT = , S = q0.

The set of productions can be obtained as shown below.

Step 1: For each transition of the form (qi, a) = qj, the corresponding

production is

qi aqj

Step 2: If q F, the final state in FA, then introduce the production q .

Since these productions are obtained from the transitions defined for FA, the

language accepted by FA is also accepted by the grammar.

Page 144: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 138

7.5.2 Example:

Obtain a regular grammar from the following DFA given by the transition

diagram.

Solution: For each transition of the form (A, a) = B, introduce the

production A aB. If q F (the final state), introduce the production A .

The productions obtained from the transitions defined for DFA are shown

below.

Transitions Productions

(S, a) = A

(S, b) = C

(A, a) = C

(A, b) = B

(B, a) = B

(B, b) = B

(C, a) = C

(C, b) = C

S aA

S bC

A aC

A bB

B aB

B bB

C aC

C bC

From the diagram, it is clear that the state B is a final state.

Therefore, we introduce the production B .

Page 145: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 139

The grammar G corresponding to the productions obtained is shown below.

G = (VN, VT, S, ), where

VN = {S, A, B, C}

VT = {a, b}

The set of productions: S aA bC

A aC bB

B aB bB

C aC bC

S is the starting symbol.

7.5.3 Example

Construct a regular grammar for the following DFA given by the transition

diagram.

Solution: For each transition of the form (A, a) = B, introduce the

production A aB.

If q F (the final state), introduce the production A . The productions

obtained from the transitions defined for DFA are shown below.

Transitions Productions

(S, a) = A

(S, b) = S

(A, a) = A

(A, b) = B

(B, a) = A

(B, b) = C

(C, a) = C

(C, b) = C

S aA

S bS

A aA

A bB

B aA

B bB

C aC

C bC

Page 146: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 140

Since the set of final states: {S, A, B}, we introduce the productions S ,

A , and B .

Therefore, the grammar G is:

G = (VN, VT, S, ) where

VN = {S, A, B, C}

VT = {a, b}

The set of productions: S aA bS

A aA bB

B aA bC

C aC bC

S is the starting symbol.

Observation: The finite automata in this problem accepts strings of a‟s and

b‟s except those containing the substring abb. Therefore, from the grammar

G we can obtain a regular language which consist of strings of a‟s and b‟s

without the substring abb.

7.5.4 Example

Obtain a right linear grammar for the regular expression ((aab)* ab)*, given

by the transition diagram.

Page 147: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 141

The right linear grammar is given by

G = (VN, VT, S, ) where

VN = {S, A, B}

VT = {a, b}

The set of productions: S abA aabB

A abA aabB

B aabB abA

7.5.5 Note

The left linear grammar can be obtained from FA as shown below:

Step 1: Obtain the reverse of given DFA

Step 2: Obtain the right linear grammar from the reversed DFA

Step 3: Obtain the left linear grammar from right linear grammar.

7.5.6 Example

Obtain a left linear grammar for the DFA shown below.

Step 1: Reverse the DFA. That is, A as the final state and C as the start

state and reverse the direction of the arrow. The reversed DFA is shown

below.

Page 148: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 142

Step 2: obtain the right linear grammar for the above DFA. The

corresponding productions are shown below.

C 1C 1B

B 0B 0A 0C

A 1A

Step 3: Reverse the productions of right linear grammar to get left linear

grammar.

If A abcd B is the production in right linear grammar, after reversing the

production will be of the form

A Bdcba.

The conversion of right linear grammar to the left linear grammar is shown

below.

Right linear grammar Left linear grammar

C 1C 1B

B 0B 0A 0C

A 1A

C C1 B1

B B0 A0 C0

A A1

Therefore, the final left linear grammar is

G = (VN, VT, S, ), where

VN = {C, A, B}

VT = {0, 1}

The set of productions: C C1 B1

B B0 A0 C0

A A1

C is the start symbol.

Page 149: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 143

Now we show that the string 10101 is accepted by DFA.

C B1 (by applying C B1)

A01 (by applying B A0)

A101 (by applying A A1)

A0101 (by applying A A0)

A10101 (by applying A A1)

10101 (by applying A )

Hence, the left linear grammar obtained is equivalent to the give FA.

Self Assessment Questions

1. What is a regular expression?

2. List the operations under which a regular set is enclosed.

3. Obtain a left linear grammar for the regular expression ((aab)* ab)*.

7.6 Summary

In this unit, we have learnt about the regular expressions along with their

simplification by implementing various operations. A special type of

grammar called regular grammar was considered. Different forms of regular

expressions and the regular expressions accepted by the language are

given. We provided method of obtaining a regular grammar from the finite

and automata (and vice versa). Sufficient number of examples are given.

Page 150: MC0082 - Theory of Computer science

Theory of Computer Science Unit 7

Sikkim Manipal University Page No.: 144

7.7 Terminal Questions

1. Obtain a right linear grammar for the language L = {anbm n 2, m 3}.

2. Obtain the left linear grammar for the right linear grammar shown below.

S abA, A baB, B aA bb.

7.8 Answers

1. Set of strings consisting of even number of 1‟s.

2. The language consists a string of 1 or strings of (01)‟s that repeat zero

once or more times.

3. Stings of 0‟s and 1‟s ending with any number of 1‟s (possible none).

4. Regular expressions are used to represent the set of strings which

include symbols arranged using certain syntax rules.

5. A regular set is closed under: union, concatenation, closure, transpose,

intersection, and complementation.

6. G = (VN, VT, S, ) where VN = {A, B, S}, VT = {a, b}

The set of productions: A Aab Bab Sab

B Baab Aaab Saab

S .

Page 151: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 145

Unit 8 Regular Languages-Properties

and Pumping Lemma

Structure

8.1 Introduction

Objectives

8.2 Closure Properties of Regular Sets

8.3 Pumping Lemma

8.4 Applications of Pumping Lemma

8.5 Summary

8.6 Terminal Questions

8.7 Answers

8.1 Introduction

This unit deals with closure properties of regular languages. The different

closure properties covered in this unit are union, concatenation, closure,

intersection, complementation etc. We also cover the Pumping lemma and

some of its applications to check whether the given language is regular or

not.

Objectives:

At the end of the unit you would be able to

explain the concept of regular languages

describe the closure properties of regular sets

apply the Pumping lemma to prove that certain languages are not regular

8.2 Closure Properties of Regular Sets

When two real numbers are multiplied, the resultant product also becomes a

real number. Thus, we can say that the real numbers are closed under the

operation of multiplication. A regular set is closed under the following

Page 152: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 146

operations such as union, concatenation, closure, transpose, intersection

and complementation.

8.2.1 Theorem

If L1 and L2 are regular, then L1 L2, L1.L2 and L1* also denote the regular

languages and we say that the regular languages are closed under union,

concatenation, star-closure.

Proof: Let L1 and L2, are regular languages corresponding to the regular

expressions R1 and R2.

By definition, R1 + R2, R1.R2 and R1* are regular expressions and so L1 L2,

L1.L2, L1* denote the regular languages and so regular languages are closed

under union, concatenation and star- closure.

8.2.2 Theorem

If L1 and L2 are regular, then the regular language is closed under

complementation.

Proof: Let M = (Q, , , q0, F) be a DFA which accepts the language L1.

Now, let us define the machine M1 = (Q, , , q0, Q-F).

(Observe that there is no difference between M and M1 except the final

states).

The non-final states of M are the final states of M1 and final states of M are

the non- final states of M1. So, the language which is rejected by M is

accepted by M1.

Also, a language accepted by a DFA is regular. So, the language accepted

by M1 is regular. So, a regular language is closed under complementation.

8.2.3 Theorem

If L1 and L2 are regular, then the regular language is closed under

intersection.

Page 153: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 147

Proof: Let us consider M1 = (Q1, , 1, q1, F1) which accepts L1 and

M2 = (Q2, , 2, q2, F2)

which accepts L2.

It is clear from these two machines that the alphabet of both machines are

same.

Assume both the machines are DFAs.

To accept the language L1 L2, let us construct the machine M that

simulates both M1 and M2 where the states of the machine M are the pairs

(p, q), where p Q1 and q Q2.

The transition for the machine M from the state (p, q) on input symbol a

is the

((p, a), (q, a)).

That is, if, 1(p, a) = r and 2(q, a) = s, then the machine moves from the

state (p, q) to the state (r, s) on input symbol a.

In this manner, the machine M can simulate the effect of M1 and M2.

Now, the machine M = (Q, , , q, F) recognizes L1 L2 where

Q = Q1 Q2

q = (q1, q2), where q1 and q2 are the start states of machine M1 and M2

respectively.

F = {(p, q) p F1 and q F2}

: Q Q, defined by

((p, q), a) = (1(p, a), 2(q, a))

It is clear from that

((p, q), w) = (

1 (p, w),

2 (q, a))

and the string w is accepted only if

Page 154: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 148

((q1, q2), w) F,

which implies that

(

1 (q1, w),

2 (q2, w)) F

1 (q1, w) F1 and

2 (q2, w)) F2.

w L1 L2.

Therefore, the regular language is closed under intersection.

8.2.4 Theorem

If L1 and L2 are regular, then the regular language is closed under difference.

Proof is straightforward as above.

8.2.5 Definition

Let and are set of alphabets. The function f: is called

homomorphism (that is, substitution where a single letter is replaced by a

string) if w = a1a2…an then f(w) = f(a1)f(a2)…f(an).

If L is made of alphabets from, then f(L) = {f(w) w L} is called

homomorphic image.

8.2.6 Example

Let = {0, 1}, = {0, 1, 2} and f(0) = 01, f(1) =112. Find f(010). Write the

homomorphic image of L = {00, 010}.

Solution: By definition we have

f(w) = f(a1)f(a2)…f(an).

So, f(010) = f(0) f(1)f(0) = 0111201.

L (00, 010) = L( f(00), f(010)) = L( f(0)f(0), f(0)f(1)f(0)) = L(0101, 0111201).

Therefore,

f(010) = 0111201

L(00, 010) = L(0101, 0111201).

Page 155: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 149

8.2.7 Example

If = {0, 1}, = {1, 2, 3}, f(0) = 3122, f(1) = 132, then find (0 +1 )*(00)*.

Solution: By definition we have

f(w) = f(a1)f(a2)…f(an).

So, (0+1)*(00)* = (f(0) + f(1))* (f(0)f(0))* = (3122 + 132)* (31223122)*

8.2.8 Theorem

If L is regular and f is homomorphism, then homomorphic image f(L) is

regular.

Proof: Let R be the regular expression and L(R) be the corresponding

regular language. We can easily find f(R) by substituting f(a) for each a .

By definition of regular expression, f(R) is a regular expression and so f(L) is

regular language. So, the regular language is closed under homomorphism.

Remark:

Any finite language can be expressed using regular expression and for any

language which can be represented using regular expression, we can have

a DFA. Even some of the infinite languages of also can be represented

using Regular expressions and for these languages also we can construct

DFA and so are regular.

But, some of the infinite languages are not regular. Though the regular

languages are important, there are non-regular languages which are very

interesting and important.

Some of the non-regular languages are:

1. {w {0, 1} w contains an equal number of 0’s and 1’s}

2. {0n1n {0, 1}* n 0}

3. {ap {a}* p 2 is a prime number}

4. Check for the matching parentheses (not possible using DFA)

Page 156: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 150

5. Count number of a’s and then the number of b’s (not possible using

DFA) and so it can not be used as a counter.

Self Assessment Questions

1. The homomorphic image of a regular language is _______

Answer: Regular.

2. Intersection of two regular languages is ______

Answer: regular

3. Difference of two regular languages is _____

4. Ans: Regular.

8.3 Pumping Lemma

It is clear from these examples that it is not possible to obtain corresponding

DFA for these and so they are not regular. So, we are facing problem

whenever we come across non regular languages. The easiest way to prove

that a language is regular is to construct a DFA. Then, what is the easiest

way to prove that a language is not regular?

In this section, we discuss the way to prove that certain languages are

infinite using Pumping Lemma.

The principle used in Pumping Lemma is similar to the Pigeonhole principle.

Pigeonhole principle (Refer unit 2): The pigeonhole principle is based on

a simple observation. Suppose there are n objects and m boxes where

number of objects n, are greater than number of boxes m. In this case, if all

n objects are placed into m boxes, then at least one box will have more than

one object.

The pumping lemma is based on this principle.

The pumping lemma is stated as follows:

Page 157: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 151

8.3.1 Lemma

Let M = (Q,, , q0, F) be an FA and has n number of states. Let L be the

language accepted by M and assume the language is regular. Let x L and

x n, that is, length of the string x is greater than the number of states of

FA. If the string x can be decomposed into combinations of strings

x = uvw

such that uv ≤ n, v 1, then uvi w L for i = 0, 1, 2, ….

Proof: Let M = (Q, , , q0, F) be an FA and Let x = a1a2a3 … am is the input

which is accepted by the machine. Assume the machine has n states and

assume that m n.

Observe that, if exactly one symbol is accepted by an FA, then there are two

distinct states in it. Similarly, to accept exactly two symbols, FA should have

three distinct states. In general, to accept a string x where x = n, then the

FA should have n + 1 states.

Let those states be q0, q1, …, qm.

Since we have m input symbols, naturally we should have m + 1 states in

the sequence q0, q1, …, qm, where q0 will be the start state and qm will be the

final state as shown below:

It is clear from the figure that (qi-1, ai) = qi for each 1 ≤ i ≤ m. In this chain of

states from q0 to qm, suppose a state q appears more than once, say q = qi

and q = qj, where i < j.

In this case, the chain should have a loop and we can split into three

groups:

1. The first group is the string prefix from a1a2a3 … ai

Page 158: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 152

2. The second group is the ioop string from ai+1ai+2 … aj-1aj

3. The third group is the string suffix from aj+1aj+2 … am, shown below.

Note that the machine cannot remember the previous state or it does not

know how it has reached the current state.

So, whenever we say that the machine has reached the state q, the

machine does not know whether the state is reached after the end of the

prefix or after it has processed the loop. Since the machine cannot

distinguish these states, after the prefix (u), the machine may be in a loop

for zero or more time (accepting the string v zero or more times) and then

accept the suffix string (w). In general, if the string x is split into sub strings

uvw, then for all i 0,

uvi w L.

If F = {qm}, a1a2a3 … am L(M), then

a1a2a3 … ai-1ai ai+1ai+2 … aj-2aj-1aj aj+1aj+2 … am L(M).

This can be expressed using the transition as shown below.

(q0, a1a2a3 … ai-1ai ai+1ai+2 … aj-2aj-1aj aj+1aj+2 … am)

= ( (q0, a1a2a3 … ai-1ai ), ai+1ai+2 … aj-2aj-1aj aj+1aj+2 … am)

= (q, aj+1aj+2 … am)

= (qk, ak+1ak+2 … am)

= qm.

Page 159: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 153

Also, after the string

a1a2a3 … ai-1ai

the machine will be in state qi. Since qi and qj are same, we can input the

string

ai+1ai+2 … aj-2aj-1aj

any number of times and the machine will stay in qj only. Finally, if the input

string is

aj+1aj+2 … am

the machine enters into final state qm.

Self Assessment Questions

4. State advantages of pumping lemma.

8.4 Applications of Pumping Lemma

All languages are not-regular. A non-regular language is that language for

which a FA cannot be constructed. So, to prove that certain languages are

not regular, we use pumping lemma. The typical applications of Pumping

Lemma are:

1. To prove that certain languages are not regular. The pumping lemma

cannot he used to prove that a given Language is regular.

2. To check whether the language is infinite. If there is a string x such that

x the number of states accepted by DFA M, then L(M) is infinite.

Otherwise, L(M) is finite. So, using Pumping lemma we can check

whether the language is finite or infinite.

8.4.1 Steps to prove that certain language is not regular:

Step 1: 1. Assume that the language L is regular and the number of states

in FA be n.

Step 2: Select the string say x and break it into substrings u, v and w such

that x = uvw with the constraints x n, uv ≤ n and v 1.

Page 160: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 154

Step 3: Find any i such that uviw L. According to pumping lemma, uviw

L. So, the result is contradiction to the assumption that the language is

regular.

Conclusion: The given language L is not regular.

8.4.2 Example

Show that L = {an bn n 0} is not regular.

Solution:

Step 1: Let L is regular and n be the number of states in FA. Consider the

string x = anbn.

Step 2: Note that x = 2n and is greater than n. So, we can split x into uvw

such that uv ≤ n and v 1 as shown below.

x =

n

vu

aaaaaaa

n

w

bbbbbbb

where u = n – 1 and v = 1 so that uv = u + v = n -1 + 1 = n and

w = n.

According to Pumping lemma, uviw L for i = 0, 1, 2, ….

Step 3: If i = 0, that is, v does not appear and so the number of a’s will be

less than the number of b’s and so the string x does not contain some

number of a’s followed by same number of b’s (equal to that of a’s).

Similarly, if i = 2, 3, ..., then number of a’s will be more than the number of

b’s and so number of a’s followed by equal number of b’s does not exist.

But, according to pumping lemma, n number of a’s should be followed by n

number of b’s which is a contradiction to the assumption that the language

is regular.

So, the language L = {an bn n 0} is not regular.

Page 161: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 155

8.4.3 Example

Show that L = {aibj i > j}is not regular.

Solution:

Step 1: Let L is regular and n be the number of states in FA.

Consider the string x = an+1 bn.

Step2: Note that x = 2n+1 n. So, we can split x into uvw such that uv ≤

n and v 1 as shown below.

x = an+1bn = u

ja v

ka w

nab

where u = j and v = k 1 and so that uv = u + v = j + k ≤ n.

Step 3: According to pumping lemma, uviw L for i = 0

That is, niki

ab)a(a L for i = 0.

Now, if we choose i = 0, number of a’s in string u will not be more than the

number of b’s in w which is contradiction to the assumption that number of

a’s are more than the number of b’s.

So, the language L = {aibj i > j} is not regular.

8.4.4 Example

Show that L = {anblcn+l n, l 0}is not regular.

Solution:

Step 1: Let L is regular and n be the number of states in FA.

Step2: Since L is regular, it is closed under homomorphism, so, we can take

f(a) = a, f(b) = a and f(c) = c.

Now, the language L is reduced to

L = {analcn+l n + l 0}

which can be written as

L = {an+1cn+l n + l 0}

Page 162: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 156

We know that the above language is not regular (proved above), which is a

contradiction to the assumption that the language is regular. So, the given

language is not regular.

8.4.5 Example

Show that L = {0n n is prime} is not regular.

Solution: The language generated from this can take the following form

L = {00, 000, 00000, …}

Step 1: Let L is regular and n be the number of states in FA. Let us choose

the value of x which depends on n.

Let x = 0n L where n is prime.

Step2: x = n and so, we can split x into uvw such that uv ≤ n and v 1

as shown below.

x = 0n = u

j0 v

k0 w

kjn 0

where u = j and v = k 1 and so that uv = u + v = j + k ≤ n.

Step 3: According to pumping lemma, uviw L for i = 0, 1, 2, ...

That is, u

j0 v

k0 w

kjn 0 L.

This means that j + ki + n – j – k = n + k(i -1) is prime for all i 0.

Now, if we choose i = n + 1, then

n + k(i-1) = n + kn = n(k + 1)

is also a prime for each k 1,

which is a contradiction (because if k = 1, it will not be a prime) to the

assumption that the language is regular.

Therefore, the language L = {0n n is prime} is not regular.

Page 163: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 157

8.4.6 Example

Show that L = {ww w {a, b}* } is not regular.

Solution:

Step 1: Let L is regular and n be the number of states in FA. Let us choose

the value of x which depends on n. Let x = anban b L.

Step 2: Note that x = 2n + 2 n and so, we can split x into uvw such that

uv ≤ n and v 1, as shown below.

x = anban b = u

ja v

ka w

nbba

where u = j and v = k 1 and so that uv = u + v = j + k ≤ n.

Step 3: According to pumping lemma, uviw L for i = 0, 1, 2, ...

That is, bba)a(anikj

L for i = 0, 1, 2, ...

Now, if we choose i = 0, number of a’s on the left of first b will be less than

the number of a’s after the first b which is contradiction to the assumption

that they are not equal. Therefore, the language L = {ww w {a, b}*} is not

regular.

8.4.7 Example

Show that L = {an n = k2 for k 0} is not regular.

Equivalently: The above language can also be defined as: L = {an is a

perfect square}

Solution:

Step1: Let L is regular and ii be the number of states in FA.

Let us choose the value of x which depends on n. Let x = am L (where m =

n2)

Step 2: Note that x n and so, we can split x into uvw such that uv ≤ n

and v 1 as shown below.

Page 164: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 158

x = am = u

ja v

ka w

kjma ,

where u = j and v = k 1 and so that uv = u + v = j + k ≤ n.

Step 3: According to pumping lemma, uviw L for i = 0, 1, 2, ...

That is,

kjmkijaaa

L for i = 0, 1, 2, …

Now, if we choose i = 2, we have

uv2w L.

That is,

kjmi2jaaa

L for i = 0, 1, 2, …

Therefore, am+k L. Since k 1, we have,

am+k = m + k = n2 + k (since m = n2).

Note that

n2 < n2 + k < n2 + 1 (when k = 1) < (n2 + 2n + 1) = (n + 1)2.

This means that n2 < (n + 1)2,

Since n2 + k lies between n2 and (n + 1)2, it is not a perfect square which is a

contradiction to the assumption that it should be a perfect square.

Therefore the language

L = {an n = k2 for k 0}

is not regular.

Self Assessment Questions

5. Verify whether the language L = {ww w {a, b}*} regular.

8.5 Summary

In this unit we discussed certain properties (called closed properties) of

regular languages. We gave the definition of homomorphism and obtained

that the regular languages are closed under homomorphic images. Further,

Page 165: MC0082 - Theory of Computer science

Theory of Computer Science Unit 8

Sikkim Manipal University Page No.: 159

we discussed the way to prove certain languages are infinite using Pumping

Lemma. Some applications of Pumping lemma are discussed.

8.6 Terminal Questions

1. Show that L = {w number of a’s in w is less than the number of b’s in

w} is not regular.

(Hint: Use Pumping Lemma).

2. Prove that the regular languages are closed under intersection and

difference.

3. State and prove Pumping Lemma.

8.7 Answers

Self Assessment Questions

1. Regular.

2. Regular

3. Regular.

4. We can easily prove that certain languages are non-regular.

It is possible to check whether a language accepted by FA is finite or

infinite.

5. By Pumping Lemma L is not regular.

Page 166: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 160

Unit 9 Context Free Grammars

Structure

9.1 Introduction

Objectives

9.2 CFG for various types of CFL

9.3 Derivations

9.4 Ambiguous Grammar

9.5 Summary

9.6 Terminal Questions

9.7 Answers

9.1 Introduction

In the regular grammar there is restriction on the number of non-terminals

on the left hand side as well as on the right hand side. On the left hand side

of the production, there will be only one symbol and that symbol must be a

non-terminal (variable). But, the right hand side of the production may

contain zero or more terminals. If a non-terminal (variable) is present, that

non- terminal should be the left most symbols or the right most symbols.

But, in a context free grammar there is only one restriction. The symbol on

the left hand side of the production should be a single non-terminal but,

there is no restriction on the right hand side of the production. Any number

of non-terminals and terminals may be present (including the symbol ).

The context free grammar is defined as follows.

(Refer unit 4, definition of type 2 grammar and a few simple illustrations).

Objectives:

At the end of the unit you would be able to

know the context free grammars.

construct the grammar corresponding the language.

discuss the ambiguous grammars.

draw the derivations trees.

Page 167: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 161

9.2 Context -free Grammars

9.2.1 Definition

A grammar G is a quadruple G = (VN, VT, , S), where

VN is set of variables or non-terminals

VT is set of terminals symbols

is set of productions

S is the start symbol,

is said to be type 2 grammar or context free grammar (CFG) if all the

productions are of the form A , where

(VN VT)* and A VN. The symbol (indicating NULL string) can

appear on the right hand side of any production).

The language generated from this grammar is called type-2 language or

context free language (CFL).

Observations:

1. There is only one symbol A on the left hand side of the production and

that symbol must be a non-terminal.

2. (VN VT)* implies that right hand side of the string may contain any

number of terminals and non-terminals including (NULL string).

3. Every regular grammar is a CFG and hence a regular language is also

context free language but the reverse is not true always.

4. Notation: nx(w) = number of x’s in the string w.

9.2.3 Example

Let G = (VN, VT, , S) be a CFG, where

VN = {S}

VT = {a, b}

: S aSa bSb

S: Starting symbol.

Find the language generated by this grammar.

Page 168: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 162

Solution: The null string can be obtained by applying the production

S so that S .

S aSa (applying S aSa)

abSba (applying S bSb)

abbSbba (applying S bSb)

abbbSbbba (applying S bSb)

abbbbbba (applying S ).

Therefore, by applying the productions

S aSa and S bSb, and any number of times and in any order, finally

applying the production S , we get a string w followed by reverse of it

(say wR).

Hence, the language generated by this grammar is

L = {wwR w {a + b}*}.

Since this language is generated from the context free grammar, it is a

context free language.

9.2 4 Example

Show that the language L = {ambn m n} is context free.

Solution: It is clear from the given language that a number of a’s are

followed by n number of b’s and number of a’s and b’s are not equal.

As a first attempt, we can have the production

S aSb

using which n number of a’s are followed by one S followed by n number of

b’s are generated.

Now, if we replace the non-terminal S by the production

S

we get n number of a’s followed by n number of b’s.

Page 169: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 163

But, we should see that number of a’s and b’s are different. So, we should

be in a position to generate either one or more extra a’s or one or more

extra b’s. Hence, instead of the production

S

we can have productions

S A B

From the non-terminal A, we can generate one or more a’s and from non-

terminal B, we can generate one or more b’s as shown below:

A aA a

B bB b.

So, the context free grammar G = (VN, VT, , S), where

VN = {S, A, B}, VT = {a, b},

: S aSb A B

A aA a

B bB b

S: starting symbol,

which generates the language L = {ambn m n}. Since a CFG exists for the

language, the language is context free.

9.2.5 Example

Obtain a Context free grammar on {a, b} to generate a language

L = {an wwRbn w *, n 1}.

Solution: Here, the string wwR must be enclosed between an and bn where

n 1. The final grammar is

VN = (S}

VT = {a, b}

: S aSb aAb

A aAa bAb , and

S is the start symbol.

Page 170: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 164

We will verify, by taking n = 2, so that we generate a string aabaabbb (here

w = ba, and that wR = ab). Consider the following sequence of productions

to get the string aabaabbb.

S aSb (applying S aSb)

aaAbb (applying S aAb)

aabAbbb (applying A bAb)

aabaAabbb (applying A aAa)

aabaabbb (applying A )

aabaabbb. (since is an empty string)

9.2.6 Problem

Obtain the context free grammar for the regular expression

(011 + 1)*(01)*.

Solution: The expression (011 + 1)*(01)* is of the form A*B* where A = 001

or 1 and B = 01. The regular expression A*B* means that any number of A’s

(possibly none) are followed by any number of B’s (possibly none). Any

number of A’s (that is, 011’s or 1’s) can be generated using the productions

A 011A 1A

Any number of B’s (that is, 01’s) can be generated using the productions

B 01B

Now, the language generated from the regular expression (011 + 1)*(01)*

can be obtained by concatenating A and B using the production

S AB

Therefore, the final grammar G = (VN, VT, , S), where

VN: {S, A, B}

VT: {0, 1}

: S AB,

A 011A 1A

B 01B

S: start symbol.

Page 171: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 165

9.2.7 Backus – Naur Form (BNF)

This is an alternative method of displaying productions of context – free –

grammar.

In a context – free – grammar, left hand side of all productions is single non

– terminal symbols.

i) Every non terminal symbol is enclosed in angle brackets < >.

ii) The terminal symbols are written without any special making.

iii) The symbol : : = is used instead of and should be read as

“is defined as”.

iv) All the productions with the same non terminal left hand side are

combined into one statement with all the right hand sides listed on the

right of : : = , separated by vertical bars.

For instance, the production A B is written as

< A > : : = < B >

production of the form

< A > : : = < B1 > , < A > : : = < B2 > , …, < A > = < Bn >

In BNF may be combined as :

< A > : : = <B1>|<B2>|…|Bn>

For example, productions A Aa, A a and A AB can be combined in

BNF as

::A A a a A B

9.2.8 Example

Consider the grammar which generates the decimal numbers

, , ,N TG V V S where

VN = {Decimal number, Decimal fraction, Unsigned integer, Digit}.

VT = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

Page 172: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 166

The productions are given in BNF notations as follows :

1. <decimal numbers > : : = < unsigned integer > | < decimal fraction > |

< unsigned integer> < decimal fraction>

2. < decimal fraction > : : = < unsigned integer >

3. < unsigned integer > : : = <digits > | <digit > <unsigned integer >

4. < digit > : : = 0 |1 | 2 | 3 | 4 | 5 | 6 | 7 |8 | 9.

Now let us see the derivative of -321 in BNF.

< integer > < signed integer >

- < unsigned integer >

- < digit > < unsigned integer >

- < digit > < digit > < unsigned integer >

- <digit > < digit > < digit >

- 3 < digit > < digit >

- 3 2 < digit >

- 3 2 1

9.2.9 Example

Consider the grammar

, , , , , ,G S A B a b S where

: , , , .S aB A aB B bA B b

These productions can be written in BNI notation as:

::

::

:: |

S a B

A a b

B b A b

Self Assessment Questions

1. Find the language for the grammar.

0,1 , , ,T NG V V S S ,

where the set of productions

: 11 , 0.S S S

Page 173: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 167

2. Find the language L (G), generated by the grammar.

, , , , , ,T NG V x y z V S A S ,

where : , , , .S xS S yA A yA A Z

3. Find the language L (G), generated by the grammar

, , , ,T NG V a b V S S , where

: , , .S aaS S a S b

4. Suppose G = ({S, A, B}, {a, b}, , S), where consists of the following

productions: S abAB, A aBb, B abA, ba bab, aB aaa. Then

show that w = abaaababaaab L(G).

5. The CFG to generate a language consisting of equal number of a’s and

b’s is ________

6. Obtain a CFG to generate unequal number of a’s and b’s.

9.3 Derivation Trees

A derivation tree is an ordered tree in which each vertex is labeled with the

left sides of a production and in which the children of a vertex represent its

corresponding right sides. At the root of the tree, we put the non terminal

symbol with which we begin the derivation. Interior vertex of the tree

corresponds to a non terminal symbol that arises in the derivation. The

leaves of the tree represent the terminal symbols that arise.

9.3.1 Definition

In the derivation process, if a left most (respectively, right most) variable is

replaced at every step, then the derivation is said to be leftmost derivation

(respectively, right most derivation).

9.3.2 Example

Consider the grammar

, , { }, ,T NG V a b V S S ,

Page 174: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 168

where : , .S aSb S ab

The language to this grammar is 1n na b n

The sentence aaabbb is derived as follows

S aSb

aaSbb

aaabbb

The derivation tree is:

9.3.3 Problem

Consider the grammar

{ , } { , , }, ,T NG V a b V S A B S ,

Where the productions are:

S AB, S bA, A a, A aS, A bAA, B b, B bS, B aBB.

Construct the derivation trees for the strings.

i) abababba

ii) aababb

S

a

a

a

b

b

b

S

S

Page 175: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 169

Solution:

Derivation tree: for (i)

Derivation tree for (ii):

S AB a (aBB) aabaBB aababb

Self Assessment Questions:

7. Obtain a leftmost derivation for the string aaabbabbba using the

following grammar.

S aB bA, A aS bAA a, B bS aBB b.

S

A B

a

b S

A

a

B

b

S

A

a

B

b

S

b

a

A

S

A B

B

B

B

B

a

b

a

b b

a

Page 176: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 170

9.4 Ambiguous Grammar

Let G = (VN, VT, , S) be a context free grammar. A grammar G is

ambiguous if and only if there exists at least one string w VT for which two

or more different derivation trees exist by applying either the left most

derivation or right most derivation. Note that after applying leftmost

derivation or right most derivation even though the derivations look different

and if the structure of derivation trees obtained is same, we can not

conclude that the grammar is ambiguous. It is not the multiplicity of the

derivations that cause ambiguity. But, it is the existence of two or more

derivation trees for the same string w derived from the root labeled S.

Note:

1. Obtain a leftmost derivation and get a string w. Obtain the right most

derivation and get a string w. For both the derivations construct the

derivation trees. If there are two different derivation trees, then the

grammar is ambiguous.

2. Obtain the string w by applying leftmost derivation twice and construct

the derivation tree. If the two derivation trees are different, the grammar

is ambiguous.

3. Obtain the string w by applying rightmost derivation twice and construct

the derivation tree. If the two derivation trees are different, the grammar

is ambiguous.

4. Apply the leftmost derivation and get string. Apply the leftmost

derivation again and get a different string. The derivation trees obtained

will naturally be different and do not come to the conclusion that the

grammar is ambiguous.

9.4.1 Example

Verify whether or not the following grammar is ambiguous.

S aS A

Page 177: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 171

A aA a

Solution: Consider the two leftmost derivations for the string aaaa.

S aS

aaS

aaaS

aaaA

aaaa.

Other way: S A

aA

aaA

aaaA

aaaa.

Since there are two leftmost derivations for the same sentence aaaa, the

given grammar is ambiguous.

9.4.2 Example

Consider the grammar

G = , , , ,G S a S with productions : , .S S S S a

The sentence a + a + a can be derived in two different ways. The derivation

trees are distinct.

S S

a a a a

S S

S

S S

S S

+

a

+

+

+ a

a

Page 178: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 172

9.4.3 Example

Obtain the string aaabbabbba by applying left most derivation and the

derivation tree for the grammar shown below.

S aB bA, A aS bAA a, B bS aBB b

Solution: Apply the set of productions

S aB (applying S aB)

aaBB (applying B aBB)

aaaBBB (applying B aBB)

aaabSBB (applying B bS)

aaabbABB (applying S bA)

aaabbaBB (applying A a)

aaabbabB (applying B b)

aaabbabbS (applying B bS)

aaabbabbbA (applying S bA)

aaabbabbba (applying A a)

Observation: In this example, we have used the productions (different from

the application of production in the SAQ).

The derivation tree is given below.

Page 179: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 173

9.5 Summary

In this unit, we study the formal languages and develop mathematical

expressions, phrase structure grammar, a simple device for the construction

of useful formal languages. We also examined several useful methods like

BNF, derivation trees, to represent productions and grammars. Some types

of grammars depending on their productions were discussed. These are

useful for generating algorithms.

9.6 Terminal Questions

1. Construct the grammar which generates the following language.

i) 1n nL a ba n

(Hint: , , , ,T NG V a b V S S , where is:

, .S aSa S b

It is a context – free – language).

ii) 1 , 1n mL a ba n m

(Hint: , , , , , ,T NG V a b V S A S where

: , , , , , .S aAb S bAa A bAa A aAb A ab A ba

It is a context – free – language).

2. Give the BNF notations for the productions of the grammar.

, , , , , , , , ,T NG V a V S A B S where

: , , ,S a A A a B B a B B a

(Hint: ::S a A

::A a B

:: |B a B a ).

Page 180: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 174

3. Draw the derivation tree for the sentence x2y2z for the grammar given in

problem 2.

Ans:

5. Explain the leftmost and rightmost derivation trees.

9.7 Answers

1. ( ) 0, 110 ,11110, 1111110,L G

2. ( ) 0, 1n mL G x y z n m

3. 2 1 2( ) 0 0n nL G a n a b n

5. G = (VN, VT, S, ) where

VN = {S, A, B}

VT = {a, b}

The set of productions: S aB bA

A aS bAA a

B bS aBB b

S is the starting symbol.

6. G = (VN, VT, S, ) where

VN = {S, A, B}

VT = {a, b}

S

S

A

Z

x

x

y

y

S

Page 181: MC0082 - Theory of Computer science

Theory of Computer Science Unit 9

Sikkim Manipal University Page No.: 175

The set of productions: S A B

A a aA bAA AbA AAb

B bbBaBB BaB BBa

S is the starting symbol.

7. S aB (applying S aB)

aaBB (applying B aBB)

aaaBBB (applying B aBB)

aaabBB (applying B b)

aaabbB (applying B b)

aaabbaBB (applying B aBB)

aaabbabB (applying B b)

aaabbabbS (applying B bS)

aaabbabbbA (applying S bA)

aaabbabbba (applying A a).

The derivation tree is given below.

Page 182: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 176

Unit 10 Pushdown Automata (PDA)

Structure

10.1 Introduction

Objectives

10.2 Definition of PDA and Moves

10.3 Graphical Representation

10.4 Construction of Pushdown Automata

10.5 Deterministic and Non-deterministic PDA

10.6 Summary

10.7 Terminal Questions

10.8 Answers

10.1 Introduction

Since the DFA or NDFA can neither count nor store the input for future

reference, we are forced to have a new machine called Pushdown Automata

(PDA). In this unit, we provide the PDA and moves described in it. A

graphical representation is also given. Certain constructions of PDA are

discussed.

Objectives:

At the end of the unit you would be able to

know the new machine Pushdown Automata.

draw the transition diagram / graphical representation.

construct the PDA.

know the language accepted by PDA.

10.2 Definition of PDA and Moves

10.2.1 Definition

A Pushdown automata (PDA) is a 7-tuple M = (Q, , , , q0, Z0, F), where

Page 183: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 177

Q: non-empty, finite set of states.

: non-empty, finite set of input alphabet.

: set of stack alphabet

: transition function, which is a mapping from

Q ( ) finite subset of Q .

q0 Q: is the start state.

Z0 is the initial symbol on the stack.

F Q: is set of accepting or final states.

10.2.2 Note

The transitions performed by the PDA depends on

i) The current state

ii) The next input symbol

iii) The symbol on top of the stack.

10.2.3 Note

The action performed by the machine consists of

i) Changing the states from one state to another.

ii) Replacing the symbol on the stack.

Page 184: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 178

Initial transition: The initial transition refers to the first move performed

when the machine shifts from the initial state to a new state. The

machine reads the first symbol from the input string. Since it is the first

symbol, the content of the pushdown stack memory is not changed. This

initial transition can also be written as a0 b0 w1 a0 b1. Here, b0 initial

state of the machine and b1 refers to the new state obtained while

reading the first symbol w1 of the input string. Initially the pushdown

store is empty and represented by a0 that appears at the top of the

stack.

Push transition: The push transition, also called read transition, refers

to the move in which the PDA shifts from the current state to a new state

and pushes the label of its old state into the stack while reading input

symbol. It can be represented as ai bi wj bi bj. Here, bi refers to

current state, bj refers to the new state of PDA and wj is the input

symbol. During transition bi is pushed at the top of the pushdown store.

Pop transition: The pop transition, also known as lambda transition,

refers to the transition process in which the PDA moves from the current

state to a new state without reading any input symbol. It can be

represented as ai bi aj bj.

Here, ai is the symbol at the top of the stack with the current state bi,

whereas aj refers to the new symbol at the top of the stack when the PDA

moves to its new state bj.

10.2.4 Example

What does each of the following transition represent?

i) (p, a, Z) = (q, aZ)

ii) (p, a, Z) = (q, )

iii) (p, a, Z) = (q, r)

iv) (p, , Z) = (q, r)

v) (p, , ) = (q, Z)

vi) (p, , Z) = (q, )

Page 185: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 179

Solution:

(p, a, Z) = (q, aZ), means that the PDA in current state p after scanning the

input symbol a and if Z is on top of the stack, then the PDA enters

into new state q pushing a on to the stack.

The transition (p, a, Z) = (q, ), means that in state p, on scanning the input

symbol a, and when top of this stack is Z, the machine enters into state q

and the topmost symbol Z is deleted from the stack.

The transition (p, a, Z) = (q, r) means that in state q, on scanning the input

symbol a and when top of the stack is Z, the machine enters into state q and

topmost symbol Z on the stack is replaced by r.

The transition (p, , Z) = (q, r) means that in state q, on scanning the empty

string and when top of the stack is Z, the machine enters into q and

topmost symbol Z on the stack is replaced by r.

The transition (p, , ) = (q, Z) means that in state p, on scanning the

empty sting and when top of the stack empty, the machine enters into q,

pushing the symbol Z on the stack.

The transition (p, , Z) = (q, ) means that in state p, on scanning the

empty string when the top of the stack is Z, the machine enters into q and

topmost symbol Z on the stack is deleted.

10.2.5 Definition

Let M = (Q, , , , q0, Z0, F) be a PDA. The move of machine M

(p, a, Z) = (q, ), where p, q Q, a , Z and *

imply that, when the PDA is currently in state p, if the scanned input symbol

is a and the top of the stack is Z, then the PDA enters into new state q and

pushes Z on the top of the stack. The symbol on top of the stack and the

recently pushed symbol is denoted by .

Page 186: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 180

Self Assessment Questions

1. Explain Initial transition, Push transition, Pop transition.

10.3 Graphical Representation

The PDA can be represented using graphical diagram. Follow the following

points.

The states of the PDA correspond to the nodes in a graph and are

represented using circles.

The start state of the PDA is denoted by an arrow labeled start.

The nodes of the graph represented by two concentric circles are the

final states of the PDA.

If there is a transition of the form

(p, a, Z) = (q, )

then, there will be an arc from state p to state q and the arc is labeled

with

a, Z/

indicating a as the current symbol, Z as the symbol on top of the stack

and representing the top of the stack along with the recently pushed

symbol.

10.3.1 Example

For each of the transitions obtain the corresponding transition diagrams

i) (p, a, Z) = (q, aZ)

ii) (p, a, Z) = (q, )

iii) (p, a, Z) = (q, r)

iv) (p, , Z) = (q, r)

v) (p, , ) = (q, Z)

vi) (p, , Z) = (q, )

Page 187: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 181

Solution: For the explanation about the transition, refer the example 10.2.4.

We will give just the graphical representations of each of above.

i)

ii)

iii)

iv)

p q

(a, Z)/r

p q

(, Z)/r

p q

(a, Z)/

p q

(a, Z)/aZ

Page 188: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 182

v)

vi)

10.3.2 Definition

Let M = (Q,, I‟, , q0, Z0, F) be a PDA. An ID (instaneous description) is

defined as a triple

(q, w, ),

where q is the current state, w is the string to be processed and is the

current content of stack. The leftmost symbol in the string is on top of the

stack and rightmost symbol in is at the bottom of the stack.

Observation: Let the current configuration of PDA be

(q, aw, Z)

It means that

q: the current state

aw: the string to be processed

Z: the current contents of the stack with Z as the topmost symbol on the

stack.

10.3.3 Moves

1. If the transition defined is (q, a, Z) = (p, ) then the new configuration

obtained will be

p q

(, Z)/r

p q

(, )/r

Page 189: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 183

(p, w, )

The move from the current configuration to next configuration is

given by

(q, aw, Z) ├ (p, w, )

This can be read as

“the configuration (q, aw, Z) derives (p, w, ) in one move”.

2. If an arbitrary number of moves are used to move from one configuration

to another configuration then the moves are denoted by the symbol├ *,

├ +.

For example,

(q, aw, Z) ├ * (p, w, )

means that the current configuration of the PDA will be (q, aw, Z) and after

applying zero or more number of transitions, the PDA enters into new

configuration (p, w, ).

The instantaneous description (q, aw, Z) ├ + (p, w, ) indicates that the

configuration (p, w, ) is obtained from the configuration (q, aw, Z) by

applying one or more transitions.

10.4 Construction of Pushdown Automata

There are two cases wherein a string w is accepted by a PDA.

Get the final state from the start state.

Get an empty stack from the start state

In the first case, we say that the language is accepted by a final state and in

the second case we say that the language is accepted by an empty stack or

null stack. The formal definitions to accept the string by a final state and by

an empty stack are defined as follows:

Page 190: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 184

10.4.1 Definition

i) Let M = (Q, , , , q0, Z0, F) be a PDA. The language L(M) accepted

by a final state is defined as

L(M)= {w (q0, w, Z0) ├ * (p, , )}

for some *, p F and w *.

(Alternatively: The PDA, currently in state q0, after scanning the string

w enters into a final state p. Once the machine is in state p, the input

symbol should be and the contents of the stack are irrelevant. Any

thing can be there on the stack. The only point to remember here is

that when all the symbols in string w have been read and when the

machine is in the final state, the final contents of the stack are

irrelevant.

ii) A language N(M) accepted by an empty stack (Null stack) as

N(M)= {w (q0, w, Z0) ├ * (p, , )}

for w *, q0, p Q.

10.4.2 Example

Obtain a PDA to accept the language L = {wcwR w (a + b)*} where wR is

reverse of w by a final state.

Solution: If w = abb, then the reverse of w denoted by wR = bba.

The language L will be wcwR, that is, abbcbba, which is a string that is

palindrome.

Therefore, we have to construct a PDA which accepts a palindrome

consisting of a‟s and b‟s with the symbol c in the middle.

General Procedure: (To check for the palindrome)

Let us push all scanned symbols onto the stack till we encounter the letter c.

Once we pass the middle string, if the string is a palindrome, for each

scanned input symbol, there should be a corresponding symbol (same as

Page 191: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 185

input symbol) on the stack. Finally, if there is no input and the stack is

empty, we say that the given string is a palindrome.

Step1: Input symbols can be a or b.

Let q0 be the initial state and Z0 be the initial symbol on the stack. In state q0

and when top of the stack is Z0, whether the input symbol is a or b, push it

on to the stack and remain in q0. The transitions defined for this can be of

the form

(q0, a, Z0) = (q0, aZ0)

(q0, b, Z0) = (q0, bZ0)

Once the first scanned input symbol is pushed on to the stack, the stack

may contain either a or b.

Now, in state q0, the input symbol can be either a or b. Note that irrespective

of what is the input or what is there on the stack, we have to keep pushing

all the symbols on to the stack, till we encounter c. So, the transitions

defined for this can be of the form

(q0, a, a) = (q0, aa)

(q0, b, a) = (q0, ba)

(q0, a, b) = (q0, ab)

(q0, b, b) = (q0, bb)

Step 2: Now, if the next input symbol is c, the top of the stack may be a or

b. Another possibility is that, in state q0, the first symbol itself can be c. In

this case, w is null string and Z0 will be on the stack. In all these cases, the

input symbol is c i.e., the symbol which is present in the middle of the string.

So, change the state to q1, and do not alter the contents of the stack. The

transitions defined for this can be of the form

(q0, c, Z0) = (q1, Z0)

(q0, c, a) = (q1, a)

Page 192: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 186

(q0, c, b) = (q1, b)

We have passed the middle string.

Step 3: Input symbols can be a or b.

To be a palindrome, for each input symbol there should be a corresponding

symbol (same as input symbol) on the stack. So, whenever the input symbol

is same as symbol on the stack, remain in state q1 and delete that symbol

from the stack and repeat the process. The transitions defined for this can

be of the form

(q1, a, a) = (q1, )

(q1, b, b) = (q1, )

Step 4: In state q1, if the string is a palindrome, there is no input symbol to

be scanned and the stack should be empty i.e., the stack should contain Z0.

Now, change the state to q2 and do not alter the contents of the stack. The

transition for this can be of the form

(q1, , Z0) = (q2, Z0)

Therefore, the PDA M to accept the language

L = {wcwR w {a, b}*}

is given by

M = (Q, , I‟, , q0, Z0, F)

where

Q = {q0, q1, q2}, q0 is the start state

= {a, b, c}

= {a, b, Z0}, Z0 is the initial number of the stack, F = {q2} is the final state.

The transitions are given by

(q0, a, Z0) = (q0, aZ0)

(q0, b, Z0) = (q0, bZ0)

(q0, a, a) = (q0, aa)

(q0, b, a) = (q0, ba)

Page 193: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 187

(q0, a, b) = (q0, ab)

(q0, b, b) = (q0, bb)

(q0, c, Z0) = (q1, Z0)

(q0, c, a) = (q1, a)

(q0, c, b) = (q1, b)

(q1, a, a) = (q1, )

(q1, b, b) = (q1, )

(q1, , Z0) = (q2, Z0)

The transition diagram is

q0 q1

q2

a, Z0/aZ0

b, Z0/bZ0

a, a /aa

b, a/ba

a, b/ab b, b/bb

c, Z0/aZ0

c, a / a

c, b / b

a, a / b, b /

, Z0 / Z0

Page 194: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 188

To verify for accepting of the string: The sequence of moves made by

the PDA for the string aabcbaa.

Initial ID:

(q0, aabcbaa, Z0) ├ (q0, abcbaa, aZ0)

├ (q0, bcbaa, aaZ0)

├ (q0, cbaa, baaZ0)

├ (q1, baa, baaZ0)

├ (q1, aa, aaZ0)

├ (q1, a, aZ0)

├ (q2, , Z0)

Since q0 is the final state and input string is in the final configuration, the

string

aabcbaa

is accepted by the PDA.

To reject the string aabcbab:

(q0, aabcbab, Z0) ├ (q0, abcbab, aZ0)

├ (q0, bcbab, aaZ0)

├ (q0, cbab, baaZ0)

├ (q0, bab, baaZ0)

├ (q1, ab, aaZ0)

├ (q1, b, aZ0)

Since the transition (q1, b, a) is not defined, the string

aabcbab

is not a palindrome and the machine halts and the string is rejected by the

PDA.

10.4.3 Example

Obtain a PDA to accept the language L = {anbn n 1} by a final state.

Page 195: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 189

Solution:

General Method: Since n number of a‟s should be followed by n number of

b‟s, let us push all the symbols on to the stack as long as the scanned input

symbol is „a‟. Once we encounter b‟s, we should see that for each „b‟ in the

input, there should be corresponding „a‟ on the stack. When the input

pointer reaches the end of the string, the stack should be empty. If stack is

empty, it indicates that the string scanned has n number of a‟s followed by n

number of b‟s.

Step 1: Let q0 be the start and Z0 be the initial symbol the stack. As long as

the next input symbol to the scanned is „a‟, irrespective of what is there on

the stack, keep pushing all the symbols on to the stack and remain in q0.

The transitions defined for this can be of the form

(q0, a, Z0) = (q0, aZ0)

(q0, a, a) = (q0, aa)

Step 2: In state q0, if the next input symbol to be scanned is „b‟ and if the top

of the stack is „a‟, change the state to q1 and delete one „b‟ from the stack.

The transition for this can be of the form

(q0, b, a) = (q1, )

Step 3: Once the machine is in state q1, the rest of the symbols to be

scanned will be only b‟s and for each „b‟ there should be corresponding

symbol „a‟ on the stack. Therefore, as the scanned input symbol is b and if

there is a matching „a‟ on the stack, remain in q1 and delete the

corresponding „a‟ from the stack. The transitions defined for this can be of

the form

(q1, b, a) = (q1, )

Step 4: In state q1, if the next input symbol to be scanned is and if the top

of the stack is Z0, change the state to q2 which is an accepting state. The

transition defined for this can be of the form

Page 196: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 190

(q1, , Z0) = (q2, Z0)

Therefore, the PDA to accept the given language with the transition diagram

is

To accept the string :

The sequence of moves made by the PDA for the string aaabbb is shown

below

(q0, aaabbb, Z0) ├ (q0, aabbb, aZ0)

├ (q0, abbb, aaZ0)

├ (q0, bbb, aaaZ0)

├ (q1, bb, aaZ0)

├ (q1, , Z0)

├ (q2, , Z0)

q0 q1

q2

a, Z0/aZ0

a, a /aa

b, a /

b, a /

, Z0 / Z0

Page 197: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 191

Since q2 is the final state and input sting is in the final configuration, the

string

aaabbb

is accepted by the PDA.

To verify for rejecting the string:

The sequence of moves made by the PDA for the string aabbb is shown

below

Initial ID: (q0, aabbb, Z0) ├ (q0, abbb, aZ0)

├ (q0, bbb, aaZ0)

├ (q0, bb, aZ0)

├ (q1, b, Z0)

Since the transition (q1, b, Z0) is not defined, the string

aabbb

is rejected by the PDA.

10.4.4 Problem

Obtain a PDA to accept the language L = {anb2n n 1} (that is, the machine

should accept n number of a‟s followed by 2n number of b‟s).

Solution:

Step 1: Let q0 be the start state and Z0 be the initial symbol on the stack.

For each scanned input symbol a, push two a‟s on the stack. The

transitions defined for this can be of the form

(q0, a, Z0) = (q0, aaZ0)

(q0, a, a) = (q0, aaa).

Step 2: In state q0, if the next input symbol to be scanned is b and if the top

of the stack is a, change the state to q1 and delete one b from the stack.

The transition for this can be of the form

(q0, b, a) = (q1, ).

Page 198: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 192

Step 3: Once the machine is in state q1, the rest of the symbols to be

scanned will be only b‟s and for each b there should be corresponding

symbol a on the stack. So, as the scanned input symbol is b and if there is

a matching a on the stack, remain in q1 and delete the corresponding a from

the stack. The transitions defined for this can of the form

(q1, b, a) = (q1, ).

Step 4: In state q1, if the next input symbol to be scanned is and if the top

of the stack is Z0, (it means that for each b in the input there exists

corresponding a on the stack) change the state to q2 which is an accepting

state. The transition defined for this can be of the form

(q1, , Z0) = (q2, ).

Therefore, the PDA to accept the given language is

M = (Q, , I‟, , q0, Z0, F)

where

Q = {q0, q1, q2}, q0 is the start state

= {a, b}

= {a, Z0}, Z0 is the initial number of the stack, F = {q2}is the final state.

The transitions are given by

(q0, a, Z0) = (q0, aaZ0)

(q0, a, a) = (q0, aaa).

(q0, b, a) = (q1, ).

(q1, b, a) = (q1, ).

(q1, , Z0) = (q2, ).

To accept the string: The sequence of moves made by the PDA for the

string aabbbb is shown below

Initial ID: (q0, aabbbb, Z0) ├ (q0, abbbb, aaZ0)

├ (q0, bbbb, aaaaZ0)

Page 199: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 193

├ (q0, bbb, aaaZ0)

├ (q1, bb, aaZ0)

├ (q1, b, aZ0)

├ (q1, , Z0)

├ (q2, , Z0)

Since q2 is the final state and input string is in the final configuration, the

string

aabbbb

is accepted by the PDA.

To reject the string: The sequence of moves made by the PDA for the

string aabbb is shown below

Initial ID: (q0, aabbb, Z0) ├ (q0, abbb, aaZ0)

├ (q0, bbb, aaaaZ0)

├ (q0, bb, aaaZ0)

├ (q0, b, aaZ0)

├ (q0, , aZ0)

Since the transition (q0, , a) is not defined, the string

aabbb

is rejected by the PDA.

Self Assessment Question

2. Find the transition diagram of a PDA to accept the language

L = {w w (a + b)* and the number of a‟s in string w should be equal to

the number of b‟s in w}.

10.5 Deterministic and Non-deterministic PDA

10.5.1 Definition

Let M = (Q, , I‟, , q0, Z0, F) be a PDA. The PDA is deterministic if

Page 200: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 194

i) (q, a, Z) has only one element.

ii) If (q, , Z) is not empty, then (q, a, Z) should be empty.

Both the conditions should be satisfied for the PDA to be deterministic. If

one of the conditions fails, the PDA is non-deterministic.

10.5.2 Example

Is the Pushdown automata to accept the language

L(M) = {wcwR w (a + b)*}

Solution: The transitions defined for this machine are obtained in Example

10.4.2. For completeness we have reproduced here.

(q0, a, Z0) = (q0, aZ0)

(q0, b, Z0) = (q0, bZ0)

(q0, a, a) = (q0, aa)

(q0, b, a) = (q0, ba)

(q0, a, b) = (q0, ab)

(q0, b, b) = (q0, bb)

(q0, c, Z0) = (q1, Z0)

(q0, c, a) = (q1, a)

(q0, c, b) = (q1, b)

(q1, a, a) = (q1, )

(q1, b, b) = (q1, )

(q1, , Z0) = (q2, Z0)

The PDA should satisfy the two conditions shown in the definition to be

deterministic.

1. (q, a, Z) has only one element: Observe that in the transitions, for each

q Q, a and Z , there is only one component defined and the

first condition is satisfied.

Page 201: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 195

2. The second condition states that if (q, , Z) is not empty, then (q, a, Z)

should be empty. If there is an -transition (that is, (q1, , Z0)), then

there should not be any transition from the state q1 when top of the stack

is Z0 which is true.

Since, the PDA satisfies both the conditions, the PDA is deterministic.

10.5.3 Example

Is the PDA to accept the language L = {anb2n n 1} deterministic ?

Solution: The transitions (refer example: 10.4.4) defined for this machine are

(q0, a, Z0) = (q0, aaZ0)

(q0, a, a) = (q0, aaa).

(q0, b, a) = (q1, ).

(q1, b, a) = (q1, ).

(q1, , Z0) = (q2, ).

The first condition to be deterministic is (q, a, Z) should have only one

component. In this case, for each q Q, a and Z , there exists only

one definition. Therefore, the first condition is satisfied.

To satisfy the second condition, consider the transition

(q1, , Z0) = (q2, )

Since the transition is defined, the transition (q1, , Z0) where a should

not be defined which is true.

Since both the conditions are satisfied, the given PDA is deterministic.

Self Assessment Questions

3. Verify whether the given PDA

L = {w w (a, b)* and w contains more a’s than b’s} is non-deterministic.

4. What is an auxiliary memory used in PDA?

5. What are the main components of a PDA?

6. What is the initial transition?

7. What is the basic concept of instantaneous description?

Page 202: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 196

10.6 Summary

In this unit, we have learnt the basic concepts of a PDA. It is defined as a

finite state automata that is equipped with an auxiliary memory device for

storing an arbitrary number of symbols. The various transitions that take

place in the working of a PDA have also been explained in this unit.

10.7 Terminal Questions

1. How does a PDA differ from a finite automata?

2. What is the formal definition of PDA?

3. Describe the acceptance of an input string by a PDA.

4. Describe the conceptual model of a PDA.

5. Describe the construction of a PDA from the given CFL.

6. Is the PDA to accept the language L = {wwR w (a + b)*} is deterministic?

10.8 Answers

Self Assessment Questions

2.

q0 q2

a, Z0/aZ0

b, Z0/bZ0

a, a / aa

b, b / bb

a, b/ b, a /

, Z0 / Z0

Page 203: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 197

3. The transitions are

(q0, a, Z0) = (q0, aZ0)

(q0, b, Z0) = (q0, bZ0)

(q0, a, a) = (q0, aa)

(q0, b, b) = (q0, bb)

(q0, a, b) = (q0, )

(q0, b, a) = (q0, )

(q0, , a) = (q1, a).

The first condition to be deterministic is that (q, a, Z) should have only

one component. In this case, for each q Q, a and Z , there

exists only one component. Therefore the first condition is satisfied.

To satisfy the second condition, consider the transition

(q0, , a) = (q1, a)

Since this transition is defined, the transition (q0, f, a) where f

should not be defined. But, there are two transitions

(q0, a, a) = (q0, aa)

(q0, b, a) = (q0, )

defined from q0 when top of the stack is a.

Since the second condition is not satisfied, the given PDA is non-

deterministic.

4. The auxiliary memory implemented in the form of a stack helps the

machine to remember a number of states required for all the

occurrences of the given input symbol.

5. The main components of a PDA are input tape, control unit and

pushdown stack memory.

6. The initial transition refers to the first move performed when the machine

shifts from the initial state to a new state.

Page 204: MC0082 - Theory of Computer science

Theory of Computer Science Unit 10

Sikkim Manipal University Page No.: 198

7. An instantaneous description is given as (q, a, Z) where q Q, a

and Z . Since a PDA uses stack memory, its working can be

described as the change in instantaneous description.

Page 205: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 199

Unit 11 Context Free Languages - Properties

Structure

11.1 Introduction

Objectives

11.2 Pumping Lemma

11.3 Applications of Pumping Lemma for CFL

11.4 Closed Properties of CFL

11.5 Summary

11.6 Terminal Questions

11.7 Answers

11.1 Introduction

It is very important for us to learn the properties of context free languages.

The different closure properties covered in this unit are union,

concatenation, star-closure, intersection, complementation etc. This unit

also covers pumping lemma, which is a very useful concept in determining

whether the given language is context free or not.

A non terminal symbol A can be recursive if and only if it can generate a

string containing itself.

In the production,

S

uAy

the non terminal A is recursive.

Objectives:

At the end of the unit you would be able to

know the significance of Pumping Lemma.

understand the context free languages.

verify that certain languages are not context free.

learn the closed properties of context free languages.

Page 206: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 200

11.2 Pumping Lemma (for Context Free Languages)

Statement: Let L be the context free language and is infinite. Let z be

sufficiently long string and z L so that z n where n is some positive

integer. If the string z can be decomposed into combinations of strings

z = uvwxy

such that vwx ≤ n, vx 1, then uvi wiy L for i = 0, 1, 2, ….

Observations:

n is the length of the longest string that can be generated by the parse

tree where the same non terminal never occurs twice on the same path

through the tree.

The string z is sufficiently long so that it can be decomposed into various

sub strings u, v, w, x and y in that sequence.

The two sub strings v and x are present somewhere in z.

The sub string u appears before v, the sub string w is in between v and x

and the sub string y appears after x.

The string w in between v and x cannot be too long since vwx ≤ n for

some positive integer n.

Both the sub strings v and x cannot be empty since vx 1. (One of

them can be empty).

If all the points mentioned (first five points) are satisfied and if we

duplicate sub string v and x same number of times, the resulting string

will definitely be in L and the string z L is context free. Otherwise, the

string z L is not context free.

Proof of Pumping Lemma:

By Pumping Lemma, it is assumed that string z L is finite and is context

free language. We know that z is string of terminal which is derived by

applying series of productions.

Page 207: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 201

Case 1: To generate a sufficiently long string z (it is assumed that the string

is infinite), one or more variables (non-terminals) must be recursive. Let us

assume that the language is finite, the grammar has a finite number of

variables (assume that all are ‘useful variables) and each has finite length.

The only way to derive sufficiently long string using such productions is that

the grammar should have one or more recursive variables. Assume that no

variable is recursive.

Since no non-terminal (variable) is recursive, each variable must be defined

only ‘in terms of terminal(s) and / or other variables’. Since those variables

are also non-recursive, they have to be defined in terms of terminals and

other variables and so on. If we keep applying the productions, there are no

variables at all in the final derivation and finally we get string of terminals

and the generated string is finite.

From this we conclude that there is a limit on the length of the string that is

generated from the start symbol S. This contradicts our assumption that the

language is finite.

Therefore, the assumption that one or more variables are non recursive is

incorrect.

This means that one or more variables are recursive and hence the proof.

Proof of Case 2:

The string z L implies that after applying some / all productions some

number of times, we get finally string of terminals and the derivation stops.

Let z L is sufficiently long string and so the derivation must have involved

recursive use of some non-terminal A and the derivations must have the

form

S

uAy

Page 208: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 202

Note that any derivation should start from the start symbol S. Since A is

used recursively, the derivation can take the following form:

S

uAy

uvAxy

and the final derivation should be of the form

S

uAy

uvAxy

uvwxy = z

It implies that the following derivations

A

vAx

and

A

w

are also possible. From this, we can easily conclude that the derivation

A

vwx

must also be possible.

Next, we have to prove that the longest string vwx is generated without

recursion since it is assumed that vwx ≤ n. This can be easily proved since

CFG that generates CFL does not contain -productions or unit productions.

It shows that every derivation step either increases the length of the

sentential form (using recursive variable) or introduces a terminal. The

derivation

A

vAx

used earlier clearly shows that

vx l.

Observe from the derivation

S

uAy

uvAxy

that uvAxy occurs in the derivation, and

Page 209: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 203

A

vAx

and

A

w

are also possible. It follows that,

uvi wxi y L

Hence the proof.

11.3 Applications of Pumping Lemma for CFL’s

The Pumping Lemma for CFL’s is used to prove that certain languages are

not context free .

In this section, we show that certain languages are not context free using

Pumping Lemma.

General Procedure:

1. Assume that the language L is infinite and it is context free.

2. Select the string say z and break it into sub strings u, v, w, x and y such

that z = uvwxy where vwx ≤ n and vx 1.

3. Find any i such that uvi wxi y L. According to Pumping Lemma, uvi wxi

y L

So, the result is a contradiction to the assumption that the language is

context free. Therefore, the given L is not context free.

11.3.1 Example

Show that L = {anbncn n 0} is not context free.

Solution:

Step 1: Let L is context free and is infinite. Let z = anbncn L.

Step 2: Note that z > n and so we can split z into uvwxy such that

vwx ≤ n and vx 1

Page 210: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 204

and so by Pumping Lemma, uvi wxi y L for i = 0, 1, 2...

Case 1: The string vwx is within an.

Let v = aj, x = ak where vx = j + k 1 and vwx ≤ n which can be shown

as

y

nnn

uvwx

cccbbbaaa .........

Now, by Pumping Lemma for i = 2, uv2wx2y L and the language generated

is

y

nnkjn

wxuv

cccbbbaaa .........22

Note that uv2wx2y = an+j+kbncn L (since the string should have some

number of a’s followed by equal number of b’s and c’s) when j + k 1, a

contradiction.

Case 2: The string vwx is anbn.

Let v = aj, x = bk where vx = j + k 1 and vwx ≤ n which can be shown

as

By Pumping Lemma, uv2wx2y L for i = 2 and the language generated is

Page 211: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 205

Note that uv2wx2y = an+jbn+kcn L when j + k 1, contradiction.

Case 3: The string vwx is within cn.

Let v = cj, x = ck where vx = j + k 1 and vwx ≤ n which can be shown

as

By Pumping Lemma, uv2wx2y L for i = 2 and the language generated is

Note that uv2wx2y = anbcn+j+k L when j + k 1, contradiction.

But according to Pumping lemma, n number of a’s should be followed by n

number of b’s which in turn should be followed by n number of c’s. In all the

three cases we get contradiction to the assumption that the language is

context free.

Therefore, the language L = {anbncn n 0} is not context free.

11.3.2 Example

Show that L = {an! n 0} is not context free.

Solution:

Step 1: Assume that L is context free and is infinite.

Let z = an! L where an! > n.

Page 212: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 206

Step 2: since z > n, according to Pumping Lemma we can split z = an!

into u, v, w, x and y such that

vwx ≤ n and vx 1

so that uviwxiy L for i = 0, 1, 2, …

The splitting of the string z is shown below.

Let v = aj, x = ak such that vx = j + k 1 and vwx ≤ n.

The string uv2wx2y can be generated and shown below.

Now uv2wx2y = an! + j +k whenever j + k 1.

When n = 2, n! + j + k = n! + j + k ≤ n! + n

< n! + n!n

= n!(n + 1)

= (n + 1)!

Also

n! < n! + j + k < (n+1)!.

Since n! + j + k lies between n! and (n + 1)!, the string generated

uv2wx2y = an! + j +k L,

which is a contradiction.

Therefore, the language L = {an! n 0} is not context free.

Page 213: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 207

Self Assessment Questions

1. Prove that the language L = {a2b2c2} is not context free.

2. Prove that the language L = {a3!} is not context free.

3. Prove that the language L = {aaabbbaaabbb} is not context free.

11.4 Closed Properties

11.4.1 Theorem

If L1 and L2 are context free languages, then L1 L2 is also a context free

language.

Proof:

Let L1 and L2 are two CFL’s generated by the CFGs

G1 = (VN1, VT1, 1, S1)

and

G2 = (VN2. VT2, 2, S2)

respectively and assume that VN1 and VN2 are disjoint.

Consider the language L3 generated by the grammar

G3 = (VN1 VN2 S3, VT1 VT2, 3, S3)

where

S3 is the start symbol for the grammar G3 and S3 (VN1 VN2)

3 = 1 2 {S3—>S1 S2}

It is clear from this that the grammar G3 is context free and the language

generated by this grammar is context free. It is easy to prove that

L3 = L1 L2

If we assume w L1, then the possible derivation from S3 is

S3 S1

W

On similar lines, if we assume w L2, then the possible derivation from S3 is

Page 214: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 208

S3 S2

w

So, if w L3, one of the derivations

S3 S1

or

S3 S2

is possible.

In the first case, all the variables in V1 and all the terminals in T1 may be

used to get the derivation

S1

W

which uses only the productions in 1. Similarly all the variables in VN2 and

all the terminals in VT2 may be used to get the derivation

S2

w

which uses only the productions in 2 and it follows that

L3 = L1 L2.

Therefore, context free languages are closed under union.

11.4.2 Theorem

If L1 and L2 are context free languages, then L1 L2 is also a context free

language.

Proof:

Let L1 and L2 are two CFL’s generated by the CFGs

G1 = (VN1, VT1, 1, S1)

and

G2 = (VN2. VT2, 2, S2)

respectively and assume that V1 and V2 are disjoint.

Consider the language L3 generated by the grammar

G3 = (VN1 VN2 S3, VT1 VT2, 3, S3)

Page 215: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 209

where

S3 is the start symbol for the grammar G3 and S3 (VN1 VN2)

3 = 1 2 {S3—>S1S2}

It is clear from this that the grammar G3 is context free and the language

generated by this grammar is context free. It is easy to prove that

L3 = L1L2.

11.4.3 Theorem

Context free languages are closed under stat-closure.

Proof is similar to the above.

11.4.4 Theorem

If L1 and L2 are context free languages, then it is not always true that L1

L2 is also a context free language.

Proof:

Let us consider the following example:

Take L1 = {anbncm n 0, m 0} and L2 = {anbmcm n 0, m 0}.

Then the two languages are context free. The corresponding context free

grammar are

S S1S2, S1 aS1b , S2 cS2 and

S aS S1, S1 bS1c

Now their intersection L1 L2 = {anbncn n 0} is not context free (using

Pumping Lemma) (refer the previous problems).

Therefore the family of context free languages is not closed under

intersection.

11.4.5 Theorem

The context free languages are not closed under complementation.

(In other words, if L is context free language, then it is not true that

complement of L is context language).

Page 216: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 210

Proof:

In a contrary way, suppose that context free languages are closed under

complementation.

L1 and L2 are context free languages

1L and 2L are also context free (by assumption)

1L 2L is context free (since context free languages are closed under

unions)

21 LL is context free (by assumption)

Now 21 LL = L1 L2 is context free (by de Morgan law),

a contradiction. (by theorem 11.4.4).

Self Assessment Questions

4. Prove that context free languages are closed under star-closure.

11.5 Summary

As we have discussed in previous units that some of the non regular

languages can be represented using context free grammars from which we

can obtain the context free languages. In this unit, we learnt the properties

of context free languages. Using Pumping Lemma for context free

languages, we have verified that certain languages are not context free.

Further, we have proved that context, free languages are closed under

union, concatenation, and star closure. A few illustrations are given.

11.6 Terminal Questions

1. State and prove Pumping Lemma for Context free languages?

2. State the applications of Pumping Lemma.

3. Apply Pumping Lemma, show that L = {anbncn n 0} is not context free.

Page 217: MC0082 - Theory of Computer science

Theory of Computer Science Unit 11

Sikkim Manipal University Page No.: 211

4. Show that L = {apbq p = q2} is not context free.

5. Prove that context free languages are closed under union,

concatenation and star-closure.

6. Prove that context free languages are not closed under intersection.

11.7 Answers

Self Assessment Questions

1. Apply Pumping Lemma, take n = 2 in the example: 11.3.1

2. Apply Pumping Lemma, take n = 3 in the example: 11.3.2

3. Similar to 11.3.1

Page 218: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 212

Unit 12 Turing Machines- Construction

Structure

12.1 Introduction

Objectives

12.2 Turing Machine Model

12.3 Language Accepted by a Turing Machine

12.4 Design or Construction of a Turing Machine

12.5 Summary

12.6 Terminal Questions

12.7 Answers

12.1 Introduction

The concept of Turing machine was first explained by Alan Turing during

l936. The Turing machine is considered as the primitive and the theoretical

concept computers. The Turing machine is used to formally represent the

concept computations of mathematical functions.

The Church-Turing theory helps in explaining the importance of the Turing

machine. According to this theory, the Turing machine can carry out any

computation that can also be carried out by a computer and human beings.

Therefore, this theory considers the Turing machine as an abstract model of

digital computer.

The different application areas in which the Turing machine plays an

important part are as follows:

The Turing machine can be used in the automaton theory.

It can be used for calculating different types of functions such as simple

functions and recursive functions.

It can also be used in the field of artificial intelligence

Page 219: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 213

It helps in analyzing the different types of problems terms of their space

and time complexity.

Objectives:

At the end of the unit you would be able to

explain the concept of Turing machine and the model followed by it

describe the different methods used in Turing machine representation

illustrate the basic guidelines for designing a Turing machine.

explain the different tools for constructing a Turing machine.

describe the different types of Turing machines.

12.2 Turing Machine Model

The Turing machine is a state machine that consists of a finite number of

states. Usually, it is said to be in a particular state at any given time and this

state can be initial, final or halt. The Turing machine model basically

consists of one dimensional tape that is divided into a number of cells. This

tape, which is of an indefinite length, is used to accumulate the data inside

it. The data stored in the cells of the tape can be read and written by

Read/Write (R/W) head whose one end is connected with the tape and the

other end is connected with the finite control. The finite control in the Turing

machine is just like a buffer that keeps track of the next instruction to be

executed. Therefore, the structure of the Turing machine is somewhat

similar to the modern computer in which the memory acts as the tape and

the processor as the R/W head.

In figure (given in 12.2.6), the tape is divided into a number of cells in which

data is stored. However, each cell of the tape can contain only one symbol

that is either 0 or 1. The R/W head, connected with the tape is used to

examine the value stored in the particular cell. However, at a given time, the

R/W head can examine only one cell moving to the left and right of the tape.

Page 220: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 214

The current state of the Turing machine and the symbols stored in the cell

help in determining the functions performed by the machine.

Turing machine cannot be defined in terms of tape and R/W head. It can

only be explained in terms of some mathematical notations.

The Turing machine is actually 7-tuple, M = (Q, , , , q0,□, F), where

Q is a set that is used to contain different states of machine.

is set of input alphabets.

is a set of tape symbols.

□ represents a of blank character that belongs to , and - □.

is the transition function defined as : Q Q {L, R}

q0 is the initial state of the Turing machine.

F is the set of final states or accepting states such that F Q.

In general, is a partial function on Q ; its interpretation gives the

principle by which a Turing machine operates. The arguments of are the

current state of the control unit and the current tape symbol being read. The

result is a new state of the control unit, a new tape symbol, which replaces

the old one, and a move symbol, L or R. The move symbol indicates

whether the read-write head moves left or right one cell after the new

symbol has been written on the page.

12.2.1 Definition

A Turing Machine is said to halt whenever it reaches a configuration for

which is not defined.

12.2.2 Example

The following figure shows the situation before and after the move caused

by the transition

(q0, a) = (q1, d, R)

Page 221: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 215

12.2.3 Example

Consider the Turing machine defined by

Q = {q0, q1}, = {a, b}, = {a, b, □}, F = {q1}, and

(q0, a) = (q0, b, R)

(q0, b) = (q0, b, R)

(q0, □) = (q0, □, L)

If this Turing machine is started in state q0 with the symbol „a‟ under the read

write head, the applicable transition rule is (q0, a) = (q0, b, R). Therefore,

the read write head will replace the „a’ with „a b’, then move right on the

tape. The machine will remain in state q0. Any subsequent „a‟ will also be

replaced with „b but „b‟s will not be modified. When the machine encounters

the first blank, it will move one cell left, and then halts in final state q1.

Since there can be several variations of TM, the TM that we discuss, is

called Standard Turing Machine.

1. The Turing machine has a tape that is unbound in both directions,

allowing any number of left and right moves.

2. The Turing machine is deterministic in the sense that defines at most

one move for each configuration.

3. There is no special input file. We assume that at the initial time the tape

has some specified content. Some of this may be considered input.

Similarly, there is no special output device. Whenever the machine

halts, some or all of the contents of the tape may be viewed as output.

12.2.4 Representation of Turing Machine

The concept of a Turing machine is considered as an abstract concept and

so, you can represent the machine only on the basis of the activities it

carries out. There are generally three ways on the basis of which Turing

machines can be represented.

Page 222: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 216

These ways are as follows:

Instantaneous description

State transition table

State transition diagram

12.2.5 Instantaneous Description

Instantaneous Description refers to the operating state of the Turing

machine. The Turing machine can be represented by taking a snapshot of

the machine when it is performing a task. This type of representation of the

Turing machine includes the current state of the machine, the cell of the

tape pointed by the R/W head and the symbols stored in that cell.

12.2.6 Definition

An ID of TM is a string in q, where q is the current state, is the string

made from tape symbols denoted by . The read write head points to the

first character of the substring . The initial ID is denoted by q, where q is

the start state and the read write head points to the first symbol of from

left. The final ID is denoted by q □ where q A is the final state and

the read write head points to the blank character denoted by □.

For example, consider a snapshot of a Turing machine shown in Figure

below.

a1 a2 a3 a4 q2 a5 a6 a7 a8 …..

Read-Write Head

Control Unit

Page 223: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 217

In this machine, each ai . In this snapshot, the symbol a5 is under read-

write head which is the next symbol to be scanned and the symbol towards

left of a5 (that is, q2 is the current state). So, in this case an ID is denoted by

8765

q

24321 aaaaqaaaa

where the substring

4321 aaaa

is left of q2; and the substring

8765 aaaa

is right of q2. The string q2 is the current state of the machine.

The symbol a5 is the next state symbol to be scanned. Assume that the

current ID of the Turing machine is

...aaaaqaaaa 876524321

as shown in the above diagram.

Suppose, there is a transition

(q2, a5) = (q3, b1, R).

It means that if the machine is in state q2 and the next symbol to be scanned

is a5, then the machine enters into state q3 replacing the symbol a5 by b1

and R indicates that the read-write head is moved one symbol towards right.

The configuration obtained is

...aaaqbaaaa 876314321

This can be represented by a move as shown below.

...aaaaqaaaa 876524321 ├ ...aaaqbaaaa 876314321

Similarly, if the current ID of the Turing machine is

...aaaaqaaaa 876524321

Page 224: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 218

and the transition function is

(q2, a5) = (q3, c1, L),

means that if the machine is in state q2 and the next symbol to be scanned

is a5, then the machine enters into state q1 replacing the symbol a5 by c1 and

L indicates that the read-write head is moved one symbol towards left. The

new configuration obtained is

...aaacaqaaa 876141321

This can be represented by a move as shown below:

...aaaaqaaaa 876524321 ├ ...aaacaqaaa 876141321

This configuration indicates that the new state is q1, the next input symbol to

be scanned is a4. In general, the actions performed by TM depends on

Current state

The whole string to be scanned

The current position of the read-write head

The action performed by the machine consists of

Changing the states from one to another

Replacing the symbol pointed to by the read-write head

Movement of the read-write head towards left or right

12.2.7 Definition

Let M = (Q, , , , q0, □, F) be a TM. Let the ID of M be

n1kk1k321 a...aqaa...aaa ,

where each aj for 1 ≤ j ≤ n, q Q is the current state and ak as the next

symbol to be scanned. If there is a transition

(q, ak) = (p, b, R)

then the move of machine M will be

nkkk aaqaaaaa ...... 11321 ├ nkkk abapaaaaa ...... 112321

Page 225: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 219

12.2.8 Note

The Turing Machine can do one of the following:

Halt and accept by entering into final state.

Halt and reject. This is possible if the transition is not defined.

TM will never halt and enter into an infinite loop.

12.3 Language accepted by TM

12.3.1 Definition

Let M = (Q, , , , q0, □, F) be a TM. The language L(M) accepted by M is

defined as

L(M) = {w q0w ├ * 1p2, where w *, p F and 1, 2 *}

where q0w is the initial ID and 1p2 is the final ID. The set of all those

words w * which causes M to move from start state q0 to the final state p.

12.3.2 Definition

A language L is recursively enumerable, if it is accepted by a TM. That is,

given a string w which is input to TM, the machine halts and outputs YES

which belongs to the language. If w does not belong to the language L, the

TM halts and outputs NO.

12.4 Design of Turing Machine

Let q0 be the start state and let the read-write head point to the first symbol

of the string to be scanned.

12.4.1 Procedure

Step 1: Replace the left most 0 by X and change the state q1 and then

move the read-write head towards right. This is because, after a zero is

replaced, we have to replace the corresponding 1 so that number of zeros

matches with number of 1‟s.

Page 226: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 220

Step 2: Search for the leftmost 1 and replace it by the symbol Y and move

towards left (so as to obtain the leftmost 0 again).

Step 3: Steps 1 and 2 can be repeated.

12.4.2 Transition Diagram for Turing Machine (TM)

The Turing machine can be represented using transition diagram. The

transition diagram consists of nodes corresponding to the states of Turing

Machine. An edge from state q to state q will have a label of the form (X/Y,

D) where X and Y are tape symbols and D is the direction either L (left) or R

(right), to represent the movement of the head right or left. Here, X is

scanned symbol and Y is the symbol written on the tape. The start state of

the Turing machine is indicated by an arrow entering the state with label

“Start”. The final states are represented by two concentric circles.

12.4.3 Example

Obtain a Turing machine to accept the language

L = {0n1n2n n 1}

Solution: It is given that the language should consists of n number of 0‟s

followed by n number of 1‟s which in turn should be followed n number of

2‟s.

Let us consider the string 000011112222 and we shall design the Turing

machine.

To design the Turing Machine, consider the situation where first 0‟s are

replaced by X‟s, first 1‟s are replaced by Y‟s and first 2‟s are replaced by Z‟s

as shown below.

Page 227: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 221

Now, with (i) as the current configuration, let us design the Turing Machine.

In state q0, if the next scanned symbol is 0, replace it by X, change the state

to q1 and move the pointer towards right and the situation shown in (ii) is

obtained. The transition for this can be of the form –

(q0, 0) = (q1, X, R)

In state q1, we have to search for the leftmost 1. It is clear from (ii), that,

when we are searching for the symbol 1, we may encounter the symbols 0

or Y. So, replace 0 by 0, Y by Y and move the pointer towards right and

remain in state q1 only. The transitions for this can be of the form

(q1, 0) = (q1, 0, R)

(q1, Y) = (q1, Y, R)

The configuration shown in (iii) is obtained. In state q1, on encountering 1

change the state q2, replace 1 by Y and move the pointer towards right. The

transition for this can be of the form

(q1, 1) = (q2, Y, R)

and the configuration shown in (i) is obtained.

XX00YY11ZZ22 XXX0YY11ZZ22 XXX0YY11ZZ22

q0 q1 q1

(i) (ii) (iii)

q0

Page 228: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 222

In state q2, we have to search for the leftmost 2. It is clear from (iv) that,

when we are searching for the symbol 2, we may encounter the symbols 1

or Z. So, replace 1 by 1 and Z by Z and move the pointer towards right and

remain in state q2 only and the configuration shown in (v) is obtained. The

transitions of the form

(q2, 1) = (q2, 1, R)

(q2, Z) = (q2, Z, R)

In state q2, on encountering 2, change the state to q3, replace 2 by Z and

move the pointer towards left. The transition for this can be of the form

(q2, 2) = (q3, Z, L)

and the configuration shown in (vi) is obtained. Once the TM is in state q3, it

means that first 0 is replaced by X, first 1 is replaced by Y and first 2 is

replaced by Z. At this point, we have to search for the rightmost X to get

leftmost 0. During this process, it is clear from (vi) that the symbols such as

Z‟s, 1‟s, Y‟s, 0‟s and X are scanned respectively one after the other.

So, replace Z by Z, 1 by 1, Y by Y, 0 by 0, move the pointer towards left and

stay in state q3 only.

The transitions for this can be of the form

(q3, Z) = (q3, Z, L)

(q3, 1) = (q3, 1, L)

(q3, Y) = (q3, Y, L)

(q3, 0) = (q3, 0, L)

XXX0YYY1ZZ22 XXX0YYY1ZZ22 XXX0YYY1ZZZ2

q2 q2 q3

(iv) (v) (vi)

q0

Page 229: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 223

Only on encountering X, replace X by X, change the state to q0 and move

the pointer towards right to get leftmost 0. The transition for this can be of

the form

(q3, X) = (q0, X, R).

Therefore, we get

In state q0, if the input symbol is Y, it means that there are no 0‟s. If there

are no 0‟s we should see that there are no 1‟s also. For this to happen,

change the state to q4, replace Y by Y and move the pointer towards right.

The transition for this can be of the form

(q0, Y) = (q4, Y, R).

In state q4, search for only Y‟s, replace Y by Y, remain in state q4 only and

move the pointer towards right. The transition for this can be of the form

(q4, Y) = (q4, Y, R).

In state q4, if we encounter Z, it means that there are no 1‟s and so we

should see that there are no 2‟s and only Z‟s should be present. So, on

scanning the first Z, change the state to q5, replace Z by Z and move the

pointer towards right. The transition for this can be of the form

(q4, Z) = (q5, Z, R).

But, in state q5 only Z’s should be there and no more 2’s. So, as long as the

scanned symbol Z remains in state q5, replace Z by Z and move the pointer

towards right. But, once blank symbol □ is encountered, change the state to

q6, replace □ by □ and move the pointer towards right and say that the input

string is accepted by the machine. The transitions for this can be of the form

(q5, Z) = (q5, Z, R).

XXXXYYYYZZZZ

q0

q0

Page 230: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 224

(q5, □) = (q6, □, R).

Here, q6 is the final state.

Therefore, the TM to recognize the language L = {0n 1n 2n n 1} is given

by

Q = {q0, q1, q2, q3, q4, q5, q6}

= {0, 1, 2}

= {0, 1, 2, X, Y, Z, □}

q0 is the start state

□ is the blank character

F = {q6} is the final character

is shown in the following transition table.

States

0 1 2 Z Y X □

q0 q1, X, R q4, Y, R

q1 q1, 0, R q2, Y, R q1, Y, R

q2 q2, 1, R q3, Z, L q2, Z, R

q3 q3, 0, L q3, 1, L q3, Z, L q3, Y, L q0, X, R

q4 q5, Z, R q4, Y, R

q5 q5, Z, R (q0, □, R)

q6

Page 231: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 225

The transition diagram is:

12.4.4 Example

Obtain a Turing machine to accept the language containing strings of 0‟s

and 1‟s ending with 011.

Solution: The DFA which accepts the language consisting of 0‟s and 1‟s

ending with the string 001 is shown below.

Page 232: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 226

The transition table for the DFA is shown below.

0 1

q0 q1 q0

q1 q1 q2

q2 q1 q3

q3

(final state)

q1 q0

For each scanned input symbol (either 0 or 1), in whichever state the DFA

was in, TM also enters into the same states on same input symbols,

replacing 0 by 0 and 1 by 1 and the read-write head moves towards right.

Therefore, the transition table for DFA and TM remains same (the format

may be different. It is evident in both the transition tables). So the transition

table for TM recognizes the language consisting of 0‟s and 1‟s ending with

a substring 001 as shown below.

States 0 1 □

q0 q1, 0, R q0, 1, R

q1 q1, 0, R q2, 1, R

q2 q1, 0, R q3, 1, R

q3 q1, 0, L q0, 1, R q4, □, R

q4 - - -

Therefore, the TM to recognize the given language is given by

Q = {q0, q1, q2, q3}

= {0, 1}

= {0, 1, □}, is defined above.

q0 is the start state

□ is the blank character

F = {q4} is the final character

Page 233: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 227

Self Assessment Questions

1. Obtain a Turing machine to accept the language

L = {0n1n n 1}

2. Obtain a Turing machine to accept the language

L = {w w is even and = {a, b}}

12.5 Summary

In this unit, the construction of Turing machine and the language accepted

by a Turing machine were discussed. Turing machine is a generalized

machine which can recognize all types of languages. These are very useful

and much more powerful than a PDA. Instead of using stack as in PDA, the

TM uses the tape to store the symbols.

12.6 Terminal Questions

1. Explain a Turing machine model

2. Define a Turing machine

3. Define move of TM.

4. What is the language accepted by TM?

5. Obtain a TM to accept the language L = {0n1n n 1}.

6. Obtain a TM to accept the language L = {0n1n2n n 1}.

12.7 Answers

Self Assessment Questions

1. Hint: It is given that the language accepted by TM should have n

number of 0‟s followed by n number of 1‟s. For this let us take an

example of the string w = 00001111. The string w should be accepted as

it is four zeroes followed by four 1‟s.

Page 234: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 228

Let q0 be the start state and let the read write head point to the first

symbol of the string to be scanned. The general procedure to design

TM for this case is shown below.

Step 1: Replace the left most 0 by X and change the state q1 and then

move the read write head towards right. This is because, after a zero is

replaced, we have to replace the corresponding 1 so that the number of

zeros matches with number of 1‟s.

Step 2: Search for the leftmost 1 and replace it by the symbol Y and

move towards left (so as to obtain the leftmost 0 again). Steps 1 and 2

can be repeated.

2. The DFA to accept the language consisting of even number of characters

is shown below.

The transition diagram of Turing machine is given –

Page 235: MC0082 - Theory of Computer science

Theory of Computer Science Unit 12

Sikkim Manipal University Page No.: 229

References

1. Hopcroft, J.E. and J.D. Ulman: “Formal Language and Their relations to

Automata”, Addison-Wesley Pub. Company, Inc. Reading 1969.

2. Nelson Raymond J: “Introduction to Automata”, John Wiley & Sons, Inc New

York, 1968.

3. Z. Kohavi, Switching and Finite Automata Theory, 2nd

Edn, McGraw Hill New

York, 1978.

4. T. A. Sudkamp, Languages and Machines: An Introduction to the Theory of

Computer Science, Addison Wesley, Reading, MA, 1996.

5. J.A. Bonday and U.S.R. Murthy: Graph Theory with Applications, Elsevier

Science, New York, 1979.

6. Barker, S. F., The elements of Logic, 5th Ed., Mc Graw – Hill, New York, 1989.

7. Bernard Kolman, R.C. Busby, Sharon Ross, “Discrete Mathematical

Structures” PHI, 1999.

8. Graham R. L., Knuth, D. E., and Patashnik O., Concrete Mathematics, A

Foundation for Computer Science, 2nd

Ed., Addison Wesley, 1994.

9. Harary F. "Graph Theory”, Narosa Publishing House, 1995.

10. Liu.CL, Elements of Discrete Mathematics, Mc. Graw Hill.

11. Narsing Deo “Graph Theory with Applications to Engineering and Computer

Sience”, Prentice – Hall of India Pvt. Ltd., 1997.

12. Richard Johnsonbaugh, Discrete Mathematics” Pearson Education Asia, 2001

13. Rosen K. H., Hand Book of Discrete and Combinatorial Mathematics, CRC

Press, 1999.

14. Trembly J. P., and Manohar R. “Discrete Mathematical Structures with

Applications to Computer Science”, Mc-Graw Hill, 1975.

15. A. Salomma: “Computations and Automata”, in Encyclopedia of Mathematics

and Its Applications. Cambridge University Press (1985).

16. Peter Linz: An introduction to Formal languages and Automata, Narosa Publ.

company (2004).

___________________________