Sie sind auf Seite 1von 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

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

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



















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.






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

















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.


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 e 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 e 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 xe A
= 0 if xe 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.
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 xe A.
iv) X = {x
i + 1
= x
i
+ 1, i = 0, 1, 2, 3 where x
0
= 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 c 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 c Y and Y c Z, then Xc 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
R Q Z N c c c .
ii) If A = {1, 3, 5}, B = {1, 3, 5, 7} then A is a proper subset of B.

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 e A or x e B} is denoted by
A B and we call it as the union of A and B.
iv) The set {x / x e A and x e 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/ xe X, or xe Y, but not both}. It is denoted as X A 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 e B / x e A} is denoted by B A (or
B \ A) and it is called as the complement A in B.

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 e A or x e B } = {1, 2, 3, 4, 5, a, b, c, d }.
ii) A B = {x / xe A and xe B}
= the set of all elements that are both in A and B
= | (the empty set).
iii) A C = {x / x e A and x e C} = {2}.
iv) A D = {x / x e A and x e D}= {3}
v) B C = {x/ xe B or xe 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 / xe A or xe |} = A.
ix) A | = {x / xe A and xe |] = |.
1.2.13 Definitions
i) If S and T are two sets, then the set {(s, t) / s e S and t e 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 e S and t e T}.
Note that if S and T are two sets, then S T and T S may not be
equal.
ii) If S
1
, S
2
, ..., S
n
are n sets, then the Cartesian product is defined as
S
1
S
2
S
n
= {(s
1
, s
2
, , s
n
) / s
i
e S
i
for 1 s i s n}.

Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 6
Here the elements of S
1
S
2
S
n
are called ordered n-tuples. For
any two n-tuples, we have (s
1
, s
2
, , s
n
)= (t
1
, t
2
, , t
n
) s
i
= t
i
1s i s n.
iii) Let A
i
be a collection of sets one for each element i e I, where I is
some set (I may be the set of all positive integers). We define

I i
i
A
e
= {a / a e A
i
for all i e I}, and

I i
i
A
e
= {a / a e A
i
for some i e I}.
A collection {A
i
}
ie I
of sets is said to be mutually disjoint if
A
i
A
j
= | for all i e I, j e 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 A
i
= {i, i+1, i+2, } for each i e N, the set of natural numbers.
Then it is easy to observe that

N i
i
A
e
= N and

I i
i
A
e
= |.
v) If B
i
= {2i, 2i +1} for all i e N, then {B
i
}
ieN
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
2
n
.
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 s m s n.
We can select m elements from the given set A in
n
C
m
ways. So A contains
n
C
m
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
=
n
C
0
+
n
C
1
+
n
C
2
+ +
n
C
n
= 2
n
.
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, t,
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 A
c
(with respect to the universal set of real numbers) in the
following cases
i) A
1
= (1, ) (-, -2)
ii) A
2
= (-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.
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 e S, there exists t e T with (s, t) e f;
ii) (s, u) e f and (s, t) e f t = u.
If (s, t) e 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 e 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(s
1
) = f(s
2
) s
1
= s
2
.
onto function (or surjective function): t e T there exists an element
s e 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 I
S
. 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.
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 = {x
1
, x
2
, , x
n
}. The characteristic
function of A is defined as a function from U to {0, 1} by the following:
f
A
(x
i
) =
i
i
1 if x A
0 if x A
e

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


then f
A
(1) = 0, f
A
(2) = 1, f
A
(3) = 1, f
A
(7) = 1. and f
A
(11) is undefined. It can be
verified that f
A
is everywhere defined and onto, but not one one.
1.3.7 Definition
A relation R between the sets A
1
, A
2
, , A
n
is a subset of A
1
A
2

A
n
. This relation R is called an n-ary relation.
(twoary 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) e R, (b, c) e R implies (a, c) e R;
- reflexive if (a, a) e R for all a e S;
- anti-symmetric if (a, b) e R and (b, a) e R a = b;
- symmetric if (a, b) e R implies (b, a) e R;
Self Assessment Questions
7. Determine whether or not each of the binary relations defined on the
given sets S
i
(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) S
1
= {1, 2, 3, 4}; R
1
= {(1, 1), (1, 2), (2, 1), (3, 4)}
Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 10
ii) S
2
= Z , (a, b) e R
2
if and only if ab > 0.
iii) S
3
= R R, R
3
= {((x, y), (u, v)) / x + y s 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 e 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 e 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 e (0, 1]}.
1.4.4 Example
For (x
1
,y
1
) and (x
2
,y
2
) in R
2
(Euclidean Plane), define (x
1
,y
1
) ~ (x
2
,y
2
) if and
only if x
1
2
+ y
1
2
= x
2
2
+ y
2
2
. Then ~ is an equivalence relation on R
2
.
1.4.5 Definition
Let n > 0 be a fixed integer. We define a relation namely Congruence
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 e 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 eZ 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] =
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 {S
i
/ | = S
i
_ S and i e I where I is
some index set} satisfying

I i
i
S
e
= S and S
i
S
j
= | if i = j.
1.4.9 Example
i) Write A = {1, 2, 3, 4, 5, a, b, c}, S
1
= {1, 2}, S
2
= {3}, S
3
= {4, 5, a} and
S
4
= {b, c}. Then S
1
, S
2
, S
3
, S
4
form a partition for A.
ii) Consider R, the set of all real numbers. The collection
{(a, b) / a, b e 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 e 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 e [a] [b]
x e [a] and x e [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 e [a] y ~ a.
Since a ~ b, we get y ~ b (by transitive property) b ~ y y e [b].
Hence [a] _ [b]. Similarly, we get that [b] _ [a]. Therefore [a] = [b].

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 e A}.
Since each [a] _ A, we have that

A a
] a [
e
_ A.
Now let x e A. It is clear that x e [x] _

A a
] a [
e
.
Therefore we have that
A =

A a
] a [
e
.
By the lemma 1.4.11, we know that either [a] = [b] or [a] [b] = | for any
a, b e A.
Hence the set of all equivalence classes forms a partition.
Observation: Let A be a set and { A
i
/ i e 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 e R \{0}, define x ~ y
y
x
e Q. Whether or not, ~ is an
equivalence relation.

10. Let A = {x e Z
+
/ 1 s x s 10}. State whether or not each of the following
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) = 4
n
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) = 4
n
.
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 s
0
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) = s
0

ii) f(n + 1) = F(f(n)) for all integers n e 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, ar
2
, . Then
G(0) = a, G(n +1) = r G(n).
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) F
0
= 1 = F
1

ii) F
n+1
= F
n
+ F
n-1
for n > 1.
Then
F
2
= F
1
+ F
0
= 2
F
3
= F
2
+ F
1
= 3
F
4
= F
3
+ F
2
= 5
..
Here, there are two initial conditions.
1.5.7 Example:
Define
f(x) =
when x is even
2
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.

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) s
0
= 1
ii) f(k) = 3k
2
+ 7, where f: N N
Then g(0) = s
0
. 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
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).

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.

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 = c
1

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


f(n) = 1.00 + (0.1)n = c
n
.
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) = n
2
. Then f(1) = 1, f(2) = 2
2
= 4,
and so we get a sequence 1, 4, 9, , n
2
, is a sequence on A. Let a
n

denote the n
th
term of this sequence. Then a
1
= 1, a
2
= 4, , and so on. We
denote this sequence by

=1
2
} {
n
n or simply by {n
2
}.
1.6.3 Definition
Let

=1 n n
} a { be a sequence. Consider the following terms of this sequence:
a
m
, a
m+1
, , a
n
.
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 a
m
, a
m+1
, , a
n
is written as

=
n
m i
i
a . 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
m j
j
a
or

=
n
m k
k
a .
Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 21
1.6.4 Example
i)

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

=
+
3
1 i
1 i
i
= + 2/3 + = 23/12.
1.6.5 Example
Consider the sums

=
+
3
1
) 1 (
i
i and

=
4
2 i
j

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

=
4
2 i
j = 2 + 3 + 4 = 9.
Therefore

=
+
3
1 i
) 1 i ( =

=
4
2 i
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
1 i
) 1 i ( and change the index variable to j = i + 1.

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
2 j
j .
1.6.8 Example
Consider the sum

=
+ +
1 n
0 i
2
) i 1 n ( , 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
n
2
+ 1 + i = n
2
+ j.
Hence, the new sum is

=
+
n
1 j
2
) j n ( .
1.6.9 Properties of Summation
Let

=1 n n
} a { and

=1 n n
} 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
m i
i
a +

=
n
m i
i
b = ) b a (
n
m i
i i
=
+ ,
(ii) c.

=
n
m i
i
a =

=
n
m i
i
ca .
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 s
1
and s
2
are two strings over a set A, then the concatenation of s
1
and s
2

is the string s
1
s
2
. That is, to obtain the concatenation of s
1
and s
2
we list the
elements of s
1
followed by the elements of s
2
.
1.6.11 Example
Suppose s
1
= abbabcdb and s
2
= caabcdbbd are two strings over the set
A = {a, b, c, d}. Then the concatenation of s
1
and s
2
is s
1
s
2

= abbabcdbcaabcdbbd.
It follows that if s
1
and s
2
are strings over a set A, then s
1
s
2
= s
1
+ s
2
.
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 s
1
= 1001010 and s
2
= 00111 then s
1
s
2
= 100101000111. Also s
1
=
7, s
2
= 5, and s
1
s
2
= 12 = s
1
+ s
2
.
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.
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 C
1
, k
1
with C < C
1
and k < k
1
also satisfies the definition, since
f (x) < C
1
g(x) whenever x > k
1
> k.
1.6.16 Example
Use O-notation to express 3x
3
+ 2x + 7 12 x
3
, for all real numbers
x > 1.
Solution: Take C = 12 and k = 1, the given statement translates to
2x
3
+ 2x + 7 is O(x
3
).
1.6.17 Note: Order of Polynomial functions
i) If 1 < x then x < x
2
and so x
2
< x
3
. Thus, if 1 < x, then 1 < x < x
2
< x
3
.
ii) For any rational numbers r and s, if x > 1 and r < s, then x
r
< x
s
.
Therefore x
s
is O(x
s
).
1.6.18 Example
Show that for any real number x > 1, 2x
4
+ 4x
3
+ 5 11x
4
.
Solution: Since x is a real number and x > 1, we have
x
3
< x
4
and 1 < x
4
. So
4x
3
< 4x
4
and 5 < 5x
4
.
Adding we obtain, 2x
4
+ 4x
3
+ 5 2x
4
+ 4x
4
+ 5x
4
= 11x
4
.
Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 25
1.6.19 Example
Use the definition of order to show that 5x
3
3x + 4 is O(x
3
).
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) = 5x
3
3x + 4 and g(x) = x
3
.
For all real numbers x > 1,
5x
3
3x + 4 5x
3
+ 3x + 4 (by the triangle inequality)
5x
3
+ 3x + 4
5x
3
+ 3x
3
+ 4x
3

12x
3
12 x
3
.
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, 5x
3
3x + 4 is O(x
3
).
1.6.20 Example
Show that 9x
2
is O(x
3
). Is it true that x
3
is O(9x
2
)?
Solution: We note that 9x
2
< x
3
is true whenever x > 9 (by dividing both
sides by x
2
). Hence, 9x
2
is O(x
3
), taking C = 1 and k = 9 in the definition of
big-oh notation.
To determine whether x
3
is O(9x
2
) or not , it is necessary to determine
whether there are constants C and k such that x
3
C (9x
2
) whenever x > k.
This is equivalent to the inequality x < 9C (we get this dividing both sides by x
2
).
No such x can exist since x can be marked arbitrarily large. Hence, x
3
is not
O(9x
2
).
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.
Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 26
1.6.21 Theorem
Let f(x) = a
n
x
n
+ a
n-1
x
n-1
+ + a
1
x + a
0
where a
0
, a
1
, , a
n-1
, a
n
are real
numbers then f(x) is O(x
n
).
Proof: Using the triangle inequality, if x > 1, we have that
f(x) = a
n
x
n
+ a
n-1
x
n-1
+ + a
1
x + a
0

a
n
x
n
+ a
n-1
x
n-1
+ + a
1


x + a
0

x
n
(

a
n
+ a
n-1
/ x + + a
1
/ x
n-1
+ a
0
/x
n
)
x
n
(

a
n
+ a
n-1
+ + a
1
+ a
0
) (since x > 1).
This shows that
f(x) C x
n
where
C =

a
n
+ a
n-1
+ + a
1
+ a
0
, whenever x > 1.
Hence, f(x) is O(x
n
).
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

times n
n n n

+ + + ... = n n = n
2
.
Therefore, 1 + 2 + 3 + + n is O(n
2
).
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.
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 = n
n
.
This inequality shows that n ! is O(n
n
), taking C = 1 and k = 1.
Taking logarithm both sides, we get
log n! log n
n
= 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 f
1
(x) is O(g
1
(x)) and f
2
(x) is O(g
2
(x)). Then (f
1
+ f
2
) (x) is
O(max (g
1
(x), g
2
(x))).
- Suppose that f
1
(x) and f
2
(x) are both O(g(x)). Then (f
1
+ f
2
)(x) is O(g(x)).
- Suppose that f
1
(x) is O(g
1
(x)) and f
2
(x) is O(g
2
(x)). Then (f
1
f
2
)(x) is
O(max (g
1
(x)g
2
(x)).
1.6.25 Example
Give a big O-estimate for f(n) = 3n log(n!) + (n
2
+ 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(n
2
log n).
Next the product (n
2
+ 3) log n will be estimated.
(n
2
+ 3) 2n
2
when n > 2, it follows that n
2
+ 3 is O(n
2
).
Therefore (n
2
+3)log n is O(n
2
log n). Using theorem, to combine the two
big O-estimates for the products shows that
f(n) = 3nlog (n!) + (n
2
+ 3)log n is O(n
2
log n).
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(x
2
+ 1) + 3x
2
.
Solution: A big-O estimate for (x + 1) is O(x).
When x > 1, x
2
+ 1 2x
2
. Therefore,
log (x
2
+ 1)
log(2x
2
)
= log 2 + log x
2
= log 2 + 2 log x
3 log x, if x > 2.
Therefore
log(x
2
+ 1) is O(log x).
By theorem, it follows that (x + 1)log(x
2
+ 1) is O(x logx).
Further, 3x
2
is O(x
2
), by theorem, f(x) is O(max (x logx, x
2
)).
Since x log x x
2
, for x > 1, it follows that f(x) is O(x
2
).
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 O 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 O(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).

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 u g(x) if f(x) is O(g(x)) and f(x) is
Og(x). When f(x) is u 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 u 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 u g(x).

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

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.



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, t,
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 , t,
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
1
A = [-2, 1]; (ii).
c
2
A = (- , 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) e R
1
.
Not symmetric as (3, 4) e R
1
but (4, 3) e R
1

Not anti symmetric as (1, 2), (2,1) e R
1
, but 1 = 2
Not transitive as (2, 1), (1, 2) e R
1
but (2, 2) e R
1
.

Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 31
ii) Reflexive: For any a e S
2
, a
2
> 0 and so (a, a) e R
2

Symmetric: If (a, b) e R
2
, then ab > 0, so ba > 0 and hence (b, a)e R
2
.
Not Anti symmetric: (5, 2) e R
2
, since 5(2) = 10 > 0 and similarly
(2, 5) e R
2
, but 5 = 2.
Not transitive: (5, 0) e R
2
, (0, -6) e R
2
but (5, -6) e R
2
, since
5(-6) />0.
iii) Reflexive:
Not symmetric: ((1, 2), (3, 4)) e R
3
, but ((3, 4), (1, 2)) e R, since
3 + 4 / s 1 + 2.
Not anti symmetric: ((1, 2), (0, 3)) e R
3
, ((0, 3), (1, 2)) e R
3
but
(1, 2) = (0, 3).
Transitive:
8. No: Since R is not reflexive as (2, 2) e R.
9. Yes
10. (i) No; (ii) No; (iii)Yes.
11. 2
n
.
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.
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) = x
2
+ 2x

+ 1 and
g(x) = x
2
.
For all real numbers x > 1, x
2
+ 2x + 1 = x
2
+ 2x + 1
x
2
+ 2x
2
+ x
2

4x
2

4 x
2
.
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, x
2
+ 2x + 1 is O(x
2
).

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 H
1
, H
2
, , H
n
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.
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 e A, and
ii) for each natural number n, if {0, 1,... , n} _ A, then n + 1 e A.
Then A = N.
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 e 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
1 n n +

Solution:
That is to prove that 1 + 2 + 3 + . + = n
( )
2
1 n n +

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 1 = n , left side = 1, right side
( )
1
2
1 1 1
=
+
= . Hence the result is
true for 1 n =


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
1 m m
m
+
=
iii) Induction Step: We now show that the above result is true for
1 m n + = . Adding the ( )
th
1 m + term viz., 1 m + to both sides we
obtain.
1 + 2 + 3 + ... + ( )
( )
( ) 1 m
2
1 m m
1 m m + +
+
= + +
( )
( ) ( )
2
2 m 1 m
1
2
m
1 m
+ +
=
(

+ + =

( ) ( )
2
1 1 m 1 m + + +
= ,
which is the same as the given result for 1 m n + =
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
1 n 2 1 n n
n .... 3 2 1
2 2 2 2
+ +
= + + + +
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 1 = n , left side 1 1
2
= = .
Right side
( )( )
1
6
3 . 2 . 1
6
1 1 . 2 1 1 1
= =
+ +
= .
Hence the result is true for n = 1.
ii) Induction Hypothesis: Assume that the result to be true for n = m
Then
( ) ( )
6
1 m 2 1 m m
m ... 3 2 1
2 2 2 2
+ +
= + + + + .
Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 37
Adding the ( )
th
1 m + term i.e. ( )
2
1 m + to both sides of the above
equation, we get,
( )
( ) ( )
( )
2 2 2 2 2
1 m
6
1 m 2 1 m m
1 m m ... 2 1 + +
+ +
= + + + + +

( )
( ) ( ) { } 1 m 6 1 m 2 m
6
1 m
+ + +
+
=

( )
( ) 6 m 7 m 2
6
1 m
2
+ +
+
=

( )( ) ( )
6
3 m 2 2 m 1 m + + +
=

( ) ( ) ( ) ( )
6
1 1 m 2 1 1 m 1 m + + + + +
=
Therefore, the result is true for 1 m n + = . 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 2
A
=
2
0
= 1; on the other hand, 2
A
= {|}, and 2
A
= {|} = 1.
ii) Induction Hypothesis: Let n > 0, and suppose that 2
A
= 2
A

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, 2
B
= 2
B
= 2
n
.
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 2
B
, and
the former part is obtained by introducing a into each member of 2
B
. Thus
Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 38
2
A
=2
B
{C {a}: C e 2
B
}.
This division in fact partitions 2
A
into two disjoint equinumerous parts, so the
cardinality of the whole is twice 2
B
, which, by the induction hypothesis, is
2 2
n
= 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 2
n
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) 2
n
.
Basic Step: Clearly l(0) = 1 = 2
0
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) 2
i
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 2
n
= 2
n+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.
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 e
k
be any
edge in T whose end vertices are v
i
and v
j
. Since T is a tree, by Theorem
6.5, there is no other path between v
i
and v
j.
So by removing

e
k
from T, we
get a disconnected graph. Furthermore, T- e
k
consists of exactly two
components (say T
1
and T
2
). Since T is a tree, there were no circuits in T
and so there were no circuits in T
1
and T
2.
Therefore T
1
and T
2
are also trees.
It is clear that |V(T
1
)| + |V(T
2
)| = |V(T)| where V(T) denotes the set of vertices
in T.

Also |V(T
1
)| and |V(T
2
)| are less than n.
Therefore by the induction hypothesis, we have
|E(T
1
)| = |V(T
1
)| - 1 and |E(T
2
)| = |V(T
2
)| - 1.
Now |E(T)| - 1 = |E(T
1
)| + |E(T
2
)| = |V(T
1
)| - 1 + |V(T
2
)| - 1
|E(T)| = |V(T
1
)| + |V(T
2
)| - 1 = |V(T)| - 1 = n-1.
2.2.7 Problem
Prove by mathematical induction that
n
2 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 2
m
> m
Now 2
m+1
= 2. 2
m
> 2m.. We know that 2m m m m 1 = + > + for all m N e .
Therefore 2
m+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.
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 e 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.
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 e A (since A > B = n + 1 > 1, A is nonempty, and therefore such a
choice is possible). If there is another element a = a
1
e A, such that f(a) =
f(a
1
), 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 aeA and (a, a) e R}. For each
a e A, let R
a
= {b: b e A and (a, b) e R}. Then D is distinct from each R
a
.
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) e 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 R
a
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 R
a
= {b, d}, R
b
= {b, c}, R
c
= {c}, R
d
= {b, c, e, f}, R
e
= {a, e}, and
R
f
= {c, d, e}. All in all, R may be pictured like this:

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.
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.
p
2
= 2q
2
.
p
2
is even p is even.
If p is even, then it can be written as p = 2k. Therefore, 4k
2
= 2q
2
. 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
1 n n
n ... 3 2 1
2 2
3 3 3 3
+
= + + + + .
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.




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 1 = n , left side 1 1
3
= =
right side
( )
1
4
4 . 1
4
1 1 1
2 2
= =
+
=
Hence it is true for 1 n =

Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 45
ii) Assume the result to be true for m n =
Then
( )
4
1 m m
m ... 3 2 1
2 2
3 3 3 3
+
= + + + + (induction
hypothesis)
Adding the ( )
th
1 m + term viz., ( )
3
1 m + to both sides,
( )
( )
( )
3
2 2
3 3 3 3
1 m
4
1 m m
1 m m ... 2 1 + +
+
= + + + + +

( )
( ) 4 m 4 m
4
1 m
2
2
+ +
+
=

( ) ( )
4
2 m 1 m
2 2
+ +
=

( ) ( )
4
1 1 m 1 m
2 2
+ + +
=
Therefore the result is true for 1 m n + = . 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.




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
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 = {v
1
, v
2
, } called vertices and another set, E = {e
1
, e
2
, } of
elements called edges such that each edge e
k
is identified with an
unordered pair {v
i
, v
j
} of vertices.
- The vertices v
i
, v
j
associated with edge e
k
are called the end vertices of
e
k
. An edge associated with a vertex pair {v
i
, v
i
} 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 = {v
1
, v
2
, v
3
, v
4
, v
5
} and E = {e
1
, e
2
, e
3
, e
4
, e
5
, e
6
, e
7
}.
The identification of edges with the unordered pairs of vertices is given by
e
1
{v
2
, v
2
}, e
2
{v
2
, v
4
}, e
3
{v
1
, v
2
},
e
4
{v
1
, v
3
}, e
5
{v
1
, v
3
}, e
6
{v
3
, v
4
}.
Here e
1
is a loop and e
4
, e
5
are parallel edges.
3.2.3 Definition
A graph that does not have self-loop nor even parallel edges is called a
v
1
e
1
e
6
e
7
e
2
e
3
e
4
v
3 v
4
v
5
e
5
v
2
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,

eV
i
v
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
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 49

eV x
) x ( d =
=

e } v , u { \ V x
) 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 e {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

eV
i
v
i
*
) v ( d = 2(k - 1).
Now
2(k - 1) =

eV
i
v
i
*
) v ( d =

e } v , u {
i
v
i
*
) v ( d + d
*
(u) + d
*
(v)
=

e } v , u {
i
v
i
) v ( d + (d(u) - 1) + (d(v) - 1)
=

e } v , u {
i
v
i
) v ( d + d(u) + d(v) - 2 =

eV
i
v
i
*
) v ( d - 2
2(k - 1) + 2 =

eV
i
v
i
*
) v ( d 2k =

eV
i
v
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.
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, v
i
,
1s i s n) of a graph G is twice the number of edges (e) of G. So we have

=
n
1 i
i
) v ( d = 2e --------- (i)
If we consider the vertices of odd degree and even degree separately, then

=
n
1 i
i
) v ( d =

iseven
j
v
j
) v ( d +

isodd
k
v
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,

odd is
k
v
k
) v ( d --------- (iii)
is an even number.
In (iii), each d(v
k
) 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.



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
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.
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 v
1
is of degree 2, and all other vertices are of degree either 1 or 3).











v
1
v
3
v
2
v
4
v
6
v
8
v
5
v
11
v
7
v10
v
1
3
v
9
v
1
2
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 54
ii) Since the vertex of degree 2 (that is, v
1
) is distinct from all other
vertices, this vertex v
1
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
1 n +
.
Property (iii): Number of vertices of degree 3 is = n - p - 1 = n - (
2
1 n +
) - 1
=
2
3 n
.
3.4.5 Example
In the above graph, we have that
N = 13, p =
2
1 n +
=
2
1 13+
=
2
14
= 7.
Therefore, number of vertices of degree 3 is
2
3 n
=
2
3 13
= 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
1 n
= (p -1) where
p = the number of pendent vertices.
ii) In the above binary tree, the internal vertices are v
1
, v
3
, v
4
, v
5
, v
6
, v
9
.
These are 6 (= 7 - 1 = p - 1) in number.


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
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 v
1
av
2
bv
3
cv
3
d v
4
e v
2
f v
5

is a walk.










c
v
4
v
3
v
5
v
2
v
1
g
b
d
e
h
f
a
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 v
1
av
2
bv
3
cv
1
is a closed walk and the walk
v
1
av
2
bv
3
dv
4
ev
2
fv
5
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 v
1
av
2
bv
3
dv
4
is a path, and v
1
av
2
bv
3
cv
3
dv
4
ev
2
f v
5
is a
walk but not a path (because this walk contains repeated vertex v
2
).
i) Consider the graph given above, the path v
1
av
2
bv
3
dv
4


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 v
1
av
2
bv
3
dv
4
(refer the graph given). With
respect to this path d(v
1
) = 1, d(v
2
) = 2, d(v
3
) = 2, d(v
4
) = 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.



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) v
1
v
5
, v
5
v
3
, v
3
v
4
, v
4
v
5
, v
5
v
2
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.














v
4
v
3
v
2
v
1
v
5
v
6

v
3

v
1

v
4

v
2

v
5

v
7

b
2

b
3

b
1

b
6

b
4

c
5

c
6

c
8

c
7

c
1

b
5

c
2

c
3

c
4

Fig- A Graph-G
v
7

v
1

v
2

v
3

v
4

v
5

v
6


Fig B
Graph-T
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(e).
- Let T be any spanning tree of a connected graph G. Then the weight of
T, that is, e(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.

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 e
1
of G such that e(e
1
) is as small as possible and e
1
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 e
1
, e
2
, , e
i
have been chosen, then chose e
i+1
from E \ {e
1
, e
2
,
, e
i
} in such way that graph with {e
1
, e
2
, , e
i+1
} is acyclic and e(e
i+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 its simple path/closed path.

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 v
1
, v
2
, ) and a set E (the elements
of E are normally denoted by e
1
, e
2
, .) and a mapping that maps every
element of E onto an ordered pair (v
i
, v
j
) 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 E and v
i
, v
j
e V such that
(e) = (v
i
, v
j
), then we write e =
j
v
i
v . In this case, we say that e is
an edge between v
i
and v
j
. (We also say that e is an edge from v
i
to v
j
).
(We also say that e originates at v
i
and terminates at v
j
). (An edge from v
i

to v
j
is denoted by a line segment with an arrow directed from v
i
to v
j
).
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.







e
9
v
2
e
10
e
5
e
7
v
1
e
8
e
6
v
4
v
3
v
5
e
1
e
4
e
3 e
2
Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 62
Here v
5
is the initial vertex and v
4
is the terminal vertex for the edge e
7
. The
edge e
5
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.
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
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
+
(v
1
) = 3, d
+
(v
2
) = 1, d
+
(v
3
) = 1, d
+
(v
4
) = 1, d
+
(v
5
) = 4. d
-
(v
1
) = 1,
d
-
(v
2
) = 2, d
-
(v
3
) = 4, d
-
(v
4
) = 3, d
-
(v
5
) = 0.
ii) d(v
1
) = 4 = 3 + 1 = d
+
(v
1
) + d
-
(v
1
) ; d(v
2
) = 3 = 1 + 2 = d
+
(v
2
) + d
-
(v
2
),
and so on.

level 5
level 0
level 1
level 2
level 4
level 3
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
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.
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 u denote these production rules. Now if you consider that your
vocabulary is restricted to Sandy, Jack, Sam, ran, jumped, ate,
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 e 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 e S
*

iii) Cancellation: For any x, y, z e S
*
,
x y = x z implies y = z (left cancellation)
y x = y w implies x = w (right cancellation)
iv) For x, y e s
*
, | xy | = | x | + | y |
Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 69
4.2.5 Notation
V
T
= Finite non empty set of symbols (alphabet), called terminal
symbols.
(The strings of terminal symbols denoted by lower case
letters x, y , z, )
V
N
= Set of non terminal symbols, which are used to define the
syntax (or structure) of the language (A, B, C, , X, Y, Z, )
V
N
V
T
= Consisting of non terminal and terminal symbols, called
vocabulary of the language. (Strings of symbols over the
vocabulary are given by , | , T , ).
,
N T
V V | = empty set (assumption).
If A o
1
, A o
2,
, A o
n
are n A-productions, then they
can be written as A o
1
o
2
o
n
.
4.2.6 Definition
A grammar (phrase structure) is defined by a 4 tuple G = (V
N
, V
T
, S, | )
where S is a distinguished element of V
N
(called the starting symbol), | is a
finite subset of the relation from
( ) ( ) ( )
* * *
.
T N N T N T N
V V V V V to V V
In general, an element (o, |) is written as o | (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 T
V V S u
Where V
N
= {I, L, D}
Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 70
V
T
= {a, b, c, , x, y, z}
S = I
u = {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 T
V V S u be a grammar. For
o, e (V
N
V
T
)
*
{empty string}, o is said to be a direct derivative of ,
(denoted as o ) if there are strings |
1
and |
2
(including possibly empty
strings) such that
2 1 2 1
, and | | | o | o | = = and o | is a production of
G. If , o then we say that directly produces o (or o directly reduces to
).
4.2.9 Example
Consider the example 4.2.7, the direct derivatives are as follows:

o Rule used |
1
|
2

I L I L A A
Ib Lb I L A b
Lb Ab L a A b
LD L1 D 1 L A
LD aD L a A D

4.2.10 Definition
Let G = (V
N
, V
T
, S, | ) be a grammar. The string produces o (or o is the
direct derivative of ), written as , o
+
if there are strings |
0
, |
1
,, |
n
,
(n > 0) such that o | | | | | | = =
n 1 n 2 1 1 0
... , (The relation
+
is
the transitive closure of ).
Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 71
If n = 0, then the reflective transitive closure of as
*
. or o o o
+
=
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,
( )
*
*
T
L G S and V o o o

= e
`
)

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 + - u
Where u 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
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) =
{ }
1
n n n
a b c n > by the following grammar.
{ } { } ( )
, , , , , , , G S B C a b c S = u
where u 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 a
2
b
2
c
2
.
S aSBC
aaBCBC
aaBBCC
aabBCC
aabbCC
aabbcC
aabbcc
4.2.14 Example
Consider the grammar
{ } { } ( )
, , , , , G S C a b S = u
Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 73
where u is the set of productions : S aCa
C aCa
C b
Generate the language:
{ }
1
n n
a ba n > .
Solution:
Derivation for n = 2. i.e., the string a
2
ba
2

S aCa
aaCaa
aabaa
4.2.15 Problem
Generate the language L (G) =
{ }
, 1
n m
a ba n m> by the grammar.
{ } { } ( )
, , , , , , , G S A B C a b S = u
where u 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 a
2
ba
3

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.
Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 74
Solution: Since S . is a production, S .. This implies that . e L(G).
Now, for all n > 1, we can write the following:
S 0S1 00S11 0
n
S1
n
0
n
1
n
.
Therefore, 0
n
1
n

e L(G).
In the above derivation, at every step, S 0S1 is applied, except in the last
step where S . is applied.
Therefore, {0
n
1
n
n > 0} _ L(G).
Now suppose w e 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 0
n
S1
n
0
n
1
n
, for some n > 1, That is L(G) _ {0
n
1
n
n > 0}.
Hence L(G) = {0
n
1
n
n > 0}.
4.2.17 Problem
Suppose G = ({S, A, B}, {0, 1}, u, S) where u 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 e 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
Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 75
100110100011010 = w e L(G).
In this sequence, the strings that can be replaced are underlined.
4.2.18 Problem
Suppose G = ({S, A, B}, {a, b}, u, S), where u consists of the following
productions.
S abAB, A aBb, B abA, bA bab, aB aaa.
Show that w = abaaababaaab e 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 e L(G).
Self Assessment Questions
1. Suppose G = ({S, A, B}, {a, b}, u, S) where u consists of the following
productions: S abAB, A aBb, B abA, e bab, aB aaa.
Then verify whether or not w = abaaababaaab e 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 = a
2
bab
2
and v = bab
2
, find the strings uv, vu, v
2
,
u. Also find their lengths.

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
( ) ( )
0
L T .
ii) A grammar that contains only productions of the form | o where
| s o is called type 1 grammar or context sensitive grammar.
The language generated by this grammar is called context sensitive
language
( ) ( )
1
L T .
iii) A grammar that contains only productions of the form | o where
| s o and
N
V e o is called type 2 grammar or context free
grammar. The language generated by this grammar is called context
free language
( ) ( )
2
L T .
iv) A grammar that contains only productions of the form | o , where
| o s ,
N
V e o and | has the form a, B or a, where a eV
T
, B e V
N
is
called type -3 grammar or regular grammar. The language generated
by this grammar is called a regular language
( ) ( )
3
L T .
Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 77
v) A grammar G(V
N
, V
T
, S, u ) is called monotonic when every
production in u is of the form o | having o | 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 G
1
where
each production is either of the form o | or A a. Here, o and | are the
strings variables, A is a variable and a is a terminal.
Proof: To construct G
1
, consider a production o | in G with o or | having
the same terminals. Let in both o and |, a new variable C
a
replaces each of
the terminals to produce os and |s.
Now, for every o |, where o and | have same terminals, we can get a
corresponding o | with productions of the form C
a
a for each terminal
that appears on o or |. Therefore, the new productions obtained from the
above constriction are the new productions for G
1
. Also, the variables of G
along with the new variables of the form C
a
are the variables of G
1
.
Similarly, the terminals and the start symbol of G
1
are also same as those of
G. Hence, G
1
satisfies the required conditions for a grammar and it is
equivalent to G. Therefore L(G) = L(G
1
).
4.3.3 Note
i) The above theorem also holds for grammar of type 1, 2 and 3.
ii) (ii) ( ) ( ) ( ) ( )
3 2 1 0
L T L T L T L T _ _ _ .
4.3.4 Theorem
Every monotonic grammar G is equivalent to type 1 grammar.
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 V
T
= {a, b} be the set of terminals.
V
N
= {S}, non terminal (starting symbol)
Productions: S aaaa
S aabb
S bbaa
S bbbb
We simplify the productions as follows.
Let V
N
= {S, A}
: , , . S AA A aa A bb u
Therefore, the Grammar
{ } { } ( )
, , , , ,
T N
G V a b V S A S = = = u
4.3.6 Problem
Construct a grammar for the language.
{ }
{ }
*
, , ' 3 L x x a b the number of a s inx is a multiple of = e
Solution:
Let T = {a, b} and N = {S, A, B},
S is a starting symbol.
The set of productions: u
S bS
S b
S aA
A bA
A aB
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 N
G V a b V S A B S = = = u
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 L
0
, L
cs
, L
cf
and L
r
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
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 L
1
and L
2
over some common alphabet, then we
can define the following operations.
Concatenation: It combines the two languages to produce the
concatenated language denoted by L
1
L
2
. Here, L
1
L
2
consists of all the
strings of type xy, where x is a string in L
1
and y is a string in L
2
.
Intersection: It produces the language L
1
L
2
which consists of all the
strings that are contained in both the languages L
1
and L
2
.
Union: It produces the languages L
1
L
2
which consists of all the strings
that are contained in either of the languages L
1
and L
2
.
Complement: It produces the language L
1
from the language L
1
. Here,
L
1
is known as the complement of the language L
1
with respect to an
alphabet, where L
1
consists of all the strings over the alphabets that are
not in the language L
1
.
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 L
0
, L
cs
, L
cf
and L
r
are closed under the operations
concatenation and union.
4.3.10 Problem
Construct a grammar for the language.
{ }
, 1,
i j
L a b i j i j = > =
Solution:
We decompose
1 2
L L L = where
Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 81
{ } { }
1 2
i j i j
L a b i j and L a b i j = > = <
Grammar for L
1
: Set of production for L
1

A aA
A aB
B aBb
B ab,
where V
T
= {a, b} , V
N
= {A, B}
A is a starting symbol.
Grammar for L
2
:
V
T
= {a, b}, V
N
= {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 N
G V a b V S A B C S = = = u
where u: 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 = {0
i
1
j
i = j, i > 0 and j > 0}.
Solution: It is clear from the statement that it a string has n number of 0s
as the prefix, this prefixed string should not be followed by n number of 1s,
that is, we should not have equal number of 0s and 1s. At the same time,
0s should precede 1s. The grammar for this can be written as:
G = (V
N
, V
T
, u, S) where
Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 82
V
N
= {S, A, B, C}
V
T
= {0, 1}
Productions
u: S 0S1 (generates 0
i
1
j
recursively)
S A (to generate more 0s than 1s)
S B (to generate more 1s than 0s)
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 V
T
= {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 = (V
T
, V
N
, u, S), where
V
T
= {S}
V
T
= {a}, and the set of productions
u: S aaaS .,
S is the starting symbol.
Self assessment Questions
6. Find the language for the grammar.

{ } { } ( )
0,1 , , ,
T N
G V V S S = = = u
where the set of productions
: 11 , 0. S S S u

Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 83
7. Find the language L (G), generated by the grammar.

{ } { } ( )
, , , , , ,
T N
G V x y z V S A S = = = u
where : , , , . S xS S yA A yA A Z u
8. Find the language L (G), generated by the grammar

{ } { } ( )
, , , ,
T N
G V a b V S S = = = u where
: , , . S aaS S a S b u

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 , 3
n m
L a b n m = > >
(Hint: (i)
{ } { } ( )
, , , , ,
T N
G V a b V S B S = = = u
Where uis : , , , . S aS S bbB B bB B b
It is a regular language).
ii)
{ }
1
n n
L a ba n = >
(Hint:
{ } { } ( )
, , , ,
T N
G V a b V S S = = = u
Where uis: , . S aSa S b
It is a context free language).

Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 84
iii)
{ }
1 , 1
n m
L a ba n m = > >
(Hint:
{ } { } ( )
, , , , , ,
T N
G V a b V S A S = = = u where
: , , , , , . S aAb S bAa A bAa A aAb A ab A ba u
It is a context free language).

4.6 Answers
Self Assessment Questions
1. Yes, w e 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 = a
2
bab
4
ab
2
, uv = 11
vu = bab
2
a
2
bab
3
,

vu = 11;
v
2
= bab
3
ab
2
, v
2
= 8;
u = a
2
bab
2
, u = 6
5. No, Yes, No, No.
6. { } ( ) 0, 110 ,11110, 1111110, L G =
7.
{ }
( ) 0, 1
n m
L G x y z n m = > >
8.
{ } { }
2 1 2
( ) 0 0
n n
L G a n a b n
+
= > >


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.

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.

Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 87
5.2 Basic Terms
Input: The various inputs i
1
, i
2
, , i
p
applied at the input side of the model
are the elements of an input set, E, also called the input alphabet.
Output: The various outputs o
1
, o
2
, , o
q
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 q
1
, q
2
, , q
n
. (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 E used for _______
2. The symbol O used for ________
3. In an automaton system, the states are represented by _________
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, E, o, q
0
, F) where
Q is non-empty, finite set of states.
E is non-empty, finite set of input alphabet.
o is transition function, which is a mapping from Q E 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.
q
0
e 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.
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., q
0
, q
1
and q
2
. An arrow enters into state q
0
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 q
2
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 q
0
, after accepting the symbol 0, stays in
state q
0
and after accepting the symbol 1, the machine enters into state q
1
.
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 q
0
to q
1
on input symbol 1.
In state q
1
, on input symbol 0, the machine will stay in q
1
and on symbol 1,
there is a transition to state q
2
. In state q
2
, on input symbol 0 or 1, the
machine stays in state q
2
only. This DFA has three states q
0
, q
1
and q
2
and
can be represented as
Q = {q
0
, q
1
, q
2
}, the possible input symbols set E = {0, 1}, which is set of
input symbols (alphabet) for the machine.
q
0
q
1
q
2
Start
state

Final
state

0 0
0, 1
1 1
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 v
i
, to v
j
on an input symbol a, it can be
represented as o(v
i
, a) = v
j
.
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
q
0
0 q
0 o(q
0
, 0) = q
0
q
0
1 q
1 o(q
0
, 1) = q
1
q
1
0 q
1 o(q
1
, 0) = q
1
q
1
1 q
2 o(q
1
, 1) = q
2
q
2
0 q
2 o(q
2
, 0) = q
2
q
2
1 q
2 o(q
2
, 1) = q
2

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, q
0
, of the system is represented by a circle with an
arrow pointing towards it.
- The final state, q
1
, is represented by two concentric circles.
- The directed edges from the initial state to the final state are labeled as
input/output.
Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 91
5.4.1 Example
The graph represents the DFA,

M = (Q = {q
0
, q
1,
q
2
}, E = {0, 1}, o, q
0
= initial state, F = {q
1
}), where o is given
by
o(q
0
, 0) = q
0
,
o(q
0
, 1) = q
1
,
o(q
1
, 0) = q
0,
o(q
1
, 1) = q
2
,
o(q
0
, 0) = q
2
,
o(q
2
, 1) = q
1.
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.
E
States 0 1
q
0
q
1


q
0
q
1
q
1
q
2
q
2
q
1

q
2
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 q
0

and the final state is q
2
. To start with the machine will be in start state q
0
.

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 q
1
. In state q
1
, on input symbol 0, the machine
stays in state q only. In state q
1
, on input symbol 1, the machine enters into
state q
2
. In state q
2
, on the input symbol 1, the machine stays in state q
2
.
Now we encounter end of the input and note that we are in the accepting
state q
2
. The moves made by the DFA for the string 1011. Therefore, after
scanning the input string 1011, the machine finally stays in state q
2
.
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
q
0
q
1 q
2
Start
state

Final
state

0
0 0, 1
1 1
q
0
q
1
q
2
Start
state

Final
state

0
0
0, 1
1 1
Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 93
the machine stays in state q
1
which is not a final state. Therefore, the string
0100 is rejected by the machine.
5.5.1 Definition
Let M = (Q, E, o, q
0
, F) be a DFA where
Q is non-empty, finite set of states.
E is a non-empty, finite set of input alphabet.
o is a transition function, which is a mapping from Q E 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.
q
0
e 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 e E
*
and
.
o (q
0
, w) e 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 e E
*
and
.
o (q
0
, w) e F}, where
.
o : Q E
*
Q, is an extended
transition function. The second argument of
.
o is a string, rather than a
single symbol, This value gives the state of after reading that string.
(For example, in the above figure, o(q
0
, 1) = q
1
and o (q
1
, 1) = q
2
. So,
.
o (q
0
, 11) = q
2
).


Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 94
5.5.2 Properties:
1. o(q, .) =
.
o (q, .) = q
2. o(q, wa) = o(
.
o (q, w), a)
3. o(q, aw) =
.
o (o(q, a), w), where q e Q, a e E, w e E
*
.
5.5.3 Example
For the DFA shown in fig. given below, what is
.
o (q
0
, 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
.
o (q
0
, 101) = o (
.
o (q
0
, 10), 1), here w = 10 and a = 1.
Now
.
o (q
0
, 10) = o (
.
o (q
0
, .), 1)
= o(q
0
, 1)
= q
1
.
Therefore,
.
o (q
0
, 101) = o (
.
o (q
0
, 10), 1) = o (q
1
, 1) = q
2
.
5.5.4 Example
Obtain a DFA to accept strings of as and bs starting with the string ab.
q
0
q
1
q
2
Start
state

Final
state

0
0
0, 1
1 1
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 q
2
is the final or accepting state. In state q
0
, if the input symbol is b,
the machine should reject b (note that the string should start with a). So, in
state q
0
, on input b, we enter into the rejecting state q
3
. The machine for
this can be of the form

The machine will be in state q
1
, 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 q
1
, if the input symbol is a, we reject it and enter into q
3
which is the
rejecting state. The machine for this can be of the form
q
0
q
1 q
2
Start
state

Final
state



a
b
q
3
b
q
0
q
1 q
2
Start
state

Final
state




a b
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 q
3

which is the rejecting state. So, in state q
3
, if the input string consists of as
and bs of any length, the entire string can be rejected and can stay in state
q
3
only.
The resulting machine can be of the form


The machine will be in state q
2
, if the input string starts with ab. After the
string ab, the string containing any combination of as and bs, can be
accepted and so remain in state q
2
only. The complete machine to accept
the strings of as and bs starting with the string ab is shown in figure above.
The state q
3
is called trap state or rejecting state.
q
0
q
1 q
2
Start
state

Final
state




a b
q
3
b
a
q
0
q
1 q
2
Start
state

Final
state


a b
q
3
b
a
a, b
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 as and bs starting with the
string ab is given by M = (Q, E, o, q
0
, F), where
Q = {q
0
, q
1
, q
2
, q
3
}, E = {a, b}, q
0
: initial state, F = {q
2
}, and the transition
function o is defined as
o E
States a b
q
0
q
1

q
3

q
1
q
3
q
3
q
2
q
2
q
2
q
3
q
3

To accept the string abab: The string is accepted by the machine.
.
o (q
0
, abab) = o (
.
o (q
0
, aba), b)
q
0
q
1 q
2
Start
state

Final
state



a b
q
3
b
a
a, b
a, b
q
2
Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 98
= o (o (
.
o (q
0
, ab), a), b)
= o (o (o (o (q
0
, a), b), a), b)
= o (o (o (q
1,
b), a), b)
= o (o (q
2
, a), b)
= o (q
2
, b)
= q
2
e F.
To reject the string aabb:
.
o (q
0
, aabb) = o (
.
o (q
0
, aab), b)
= o (o (
.
o (q
0
, aa), b), b)
= o (o (o (o (q
0
, a), a, b), b)
= o (o (o (q
1,
a), b), b)
= o (o (q
3
, b), b)
= o (q
3
, b)
= q
3
e F.
Therefore, the string aabb is not accepted by the machine.
5.5.5 Note
Sometimes we ignore the extended notion
.
o and we use only o (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 , , Q M
0
o E = , where E = {0, 1, 2, , 9}, Q = {q
0
, q
1
, q
2
}
F = {q
0
}, q
0
is the starting state.
I I x Q : o defined by

Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 99






{ } { } { } 8 , 5 , 2 c , 7 , 4 , 1 b , 9 , 6 , 3 , 0 a e e e .
Consider the string 142.
( ) ( ) ( ) 2 , 14 , q 142 , q
0 0
o o = o
= ( ) ( ) ( ) 2 , 4 , 1 , q
0
o o o
( ) ( ) 2 , 4 , q
1
o o =
( ) 2 , q
2
o =
F q
1
e =
Therefore, the string 142 is not accepted by M.
Consider the string 150.
Now ( ) ( ) ( ) ( ) 0 , 5 , 1 , q 150 , q
0 0
o o o = o
( ) ( ) 0 , 5 , q
1
o o =
( ) 0 , q
0
o =
F q
0
e =
Therefore, 150, is accepted by M.
5.5.7 Example
Obtain a DFA to accept even number of as, and odd number of as.
Solution: Observe the following transition diagrams.
Consider the string aa: o (q
0
, aa) = o ((q
0
, a), a) = o (q
1
, a) = q
0
, which is a
final state (acceptable state).
o a b c
q
0
q
0
q
1
q
2

q
1
q
1
q
2
q
0

q
2
q
2
q
0
q
1

Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 100


Consider the string aaa: o (q
0
, aaa) = o (o (q
0
, aa), a) = o (o (o (q
0
, a), a),
a) = o (o (q
1
, a), a) = o (q
0
, a) = q
1
, which is a acceptable state.


5.5.8 Problem
Obtain DFA to accept strings of as and bs having exactly one a, at least
one a, not more than three as.
Solution:
To accept exactly one a:
To accept exactly one a, we need two states q
0
and q
1
and make q
1
as the
final state. The machine to accept one a is shown below.




In q
0
, on input symbol b, remains q
0
only so that any number of bs can end
with one a. The machine for this can be of the form
q
1


q
0
a
q
0
q
1



a
q
1


q
0
a
a
a
Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 101



In q
1
, on input symbol b, remains q
1
and machine can take the form

But, in state q
1
, if the input symbol is a, the string has to be rejected as the
machine can have any number of bs but exactly one a. So, the string has
to be rejected and we enter into a trap state q
2
. 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 = {b
m
ab
n
m, n > 0}.
The machine M = (Q, E, o, q
0
, F), where
Q = {q
0
, q
1
, q
2
}, E = {a, b}, q
0
: initial state, F = {q
1
}, and the transition function
o is defined as
o E
States a b
q
0

q
2
q
1
q
0
q
2
q
1
q
2
q
2

q
0
q
1



a
b
b
q
2
a, b
q
0
q
1



a
b
b
q
0
q
1



a
b
q
1
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 q
0
and q
1
where
q
1
is the final state. The machine for this is shown below.

In state q
0
, if the input symbol is b, remains in q
0
. Once the final state q
1
is
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 = {b
m
a(a + b)
n
m, n > 0}.
The machine M = (Q, E, o, q
0
, F), where
Q = {q
0
, q
1
}, E = {a, b}, q
0
: initial state, F = {q
1
}, and the transition function o
is defined as
o E
States a b
q
0

q
1
q
0
q
1
q
1
The machine to accept not more than three as: The machine should
accept not more than three as means,
q
0
q
1



a
b
a, b
q
0
q
1



a
q
1
Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 103
It can accept zero as
It can accept one a
It can accept two as
It can accept 3 as
But, it can not accept more than three as.
In this machine, maximum of three as can be accepted (that is, the machine
can accept zero as, one a, two as). So, we need maximum four states q
0
,
q
1
, q
2
and q
3
, where all these states are final states and q
0
is the start state.

In state q
3
, if the input symbol is a, the string has to be rejected and we
enter into a trap state q
4
. 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 q
4
.
Now, the machine can take the form as shown below.

In state q
0
, q
1
, q
2
and q
3
, if the input symbol is b, stay in their respective
states and the final transition diagram is shown below.
q
1

a
a
a
q
0
q
2
q
3
q
4
a a
a, b
q
1


a
a
a
q
0
q
2 q
3
Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 104
In set notation, the language accepted DFA can be represented as
L = {b
i
ab
j
ab
k
ab
l
i, j, k, l > 0}
The DFA is M = (Q, E, o, q
0
, F) where Q = {q
0
, q
1
, q
2
, q
3
, q
4
}, E = {a, b}, q
0
is
the start state, F = {q
0
, q
1
, q
2
, q
3
}, and o is the transition function.
o E
States a b










q
4
q
1
q
0

q
2
q
1

q
3
q
2

q
4
q
3

q
4
q
4


5.5.9 Problem
Obtain a DFA to accept the language L = {w / w mod 5 = 0} on E = {a, b}.
Solution: The number of symbols in a string consisting of as and bs should
not have multiples of 5. The machine to accept the corresponding language
is shown in below.
q
1


a a
a
q
0
q
2
q
3
q
4
a
a
a, b
b b
b
b
q
0

q
1

q
2

q
3

Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 105

Self Assessment Questions
8. Draw a DFA to accept strings of as and bs with even number of as and
even number of bs. 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 0s and 1s starting with at least two
0s and ending with at least two 1s. Also find the language accepted by
this.



q
1

a
a
a
q
0
q
2
q
3
a, b
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 e 00(0+1)
*
11}
4. Obtain a DFA to accept strings of as and bs with at most two
consecutive bs.





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: {q
0
, q
1
, q
2
}, Input alphabet: {0, 1}, final state: {q
2
}, starting state {q
0
}.
7.
E
States 0 1
q
0
q
1


q
0
q
1
q
1
q
2
q
2
q
2


8. The language accepted by DFA is
L = {w w e (a + b)
*
and total number of strings in both a and b are
even}.

q
2
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 q
0
. If the first input symbol is a, the machine can enter into either state
q
1
or q
2
(since there are two transitions on input symbol a from state q
0
).
In state q
0
, if the input symbol is b, the machine enters into state q
2
.
Similarly, from state q
1
, there are multiple transitions on an input symbol a to
the states q
1
and q
2
. That is, the machine can either enter into state q
2
or
state q
1
. 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
q
0
a q
1
, q
2
o (q
0
, a) = {q
1
, q
2
}
q
0
b q
2
o (q
0
, b) = q
2

q
1
a q
1
, q
2
o (q
1
, a) = {q
1
, q
2
}
q
1
b q
0
, q
1
o (q
1
, b) = {q
0
, q
1
}
q
2
a q
2
o (q
2
, a) = q
2

q
2
b q
1
o (q
2
, b) = q
1

Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 109
Let Q is the set of states, then we define 2
Q
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, E, o, q
0
, F) where,
Q is non-empty, finite set of states.
E is non-empty, finite set of input alphabet.
o is transition function, which is a mapping from Q E to set of subsets of Q
(that is, 2
Q
). This function accepts two arguments as the input with first
argument being q e Q and the symbol argument as the symbol a e E 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.
q
0
e Q is the start state.
F _ Q is set of accepting or final states.


Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 110
6.2.2 Note: Suppose o (p, .) = A where q e Q, A e 2
Q
and a e E . Here
there will be a transition from state q to the set of states A on an input
symbol a. The transition function o can be extended to
.
o
, 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
.
o (q, w) =A.
6.2.3 Properties:
1. o(q, .) =
.
o (q, .) = q
2. o(q, wa) = o(
.
o (q, w), a) = A
j

3. o(q, aw) =
.
o (o(q, a), w) = A
q
, where q e Q, a e E, w e E
*
and A
j
and A
q

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 E. The set of all strings
recognized by and automata is called language. The language L accepted
by an NFA M = (Q, E, o, q
0
, F) is defined as follows.
6.3.1 Definition
Let M = (Q, E, o, q
0
, F) be an NFA where Qs is the set of finite states, E is
set of input alphabet (from which a string can be formed), o is transition from
Q E to 2
Q
, q
0
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 e E
*
and
.
o (q
0
, w) = P with at least on component of P in F}.
Here, P is set of states.
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 e abab
n
or aba
n
,
where n > 0}.
The machine to accept abab
n
where n > 0 is shown below.
The machine to accept aba
n
where n > 0 is shown below.
Since both the machines accept a as the first input symbol, the states q
1
and
q
5
can be merged into a single state and the machine to accept either abab
n

or aba
n
where n > 0 is shown below.

6.4 Conversion from NFA to DFA
Let M
N
= (Q
N
, E
N
, o
N
, q
0
, F
N
) be an NFA and accepts the language L(M
N
).
There should be an equivalent DFA, M
D
= (Q
D
, E
D
, o
D
, q
0
, F
D
) such that
L(M
D
) = L(M
N
). The procedure to convert an NFA to its equivalent DFA is
shown below.
Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 112
Step 1: The start state of M
N
is the start state of M
D
. So, add q
0
(which is
the start state of M
N
) 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 {q
i
, q
j
, , q
k
} e Q
D
, the transitions for each input
symbol in E can be obtained as shown below.
1. o
D
({q
i
, q
j
, , q
k
}, a) = o
N
(q
i
, a) o
N
(q
j
, a) o
N
(q
k
, a) = {q
l
, q
m
, ,
q
n
}(say)
2. Add the state {q
l
, q
m
, , q
n
} to Q, if it is not already in Q
D
.
3. Add the transition from {q
i
, q
j
, , q
k
} to {q
l
, q
m
, , q
n
} on the input
symbol a if and only if the state {q
l
, q
m
, , q
n
} is not added to Q
D
in the
previous step.
Step 3: The state {q
a
, q
b
, , q
c
} e Q

is the final state, if at least one of the


states in q
a
, q
b
, , q
c
e F
N
(that is, at least one component in {q
a
, q
b
, , q
c
}
should be the final state of NFA.
6.4.1 Example
Convert the following NFA to its equivalent DFA.
Solution:
Step 1: q
0
is the start DFA, so Q
D
= {{q
0
}} _______________(i).
Step 2: Fine the new states from each state in Q
D
and obtain the
corresponding transitions.
Consider the state {q
0
}:
When a = 0, o
D
({q
0
}, 0) = o
N
({q
0
}, 0) = {q
0
, q
1
}
When a = 1, o
D
({q
0
}, 1) = o
N
({q
0
}, 1) = {q
1
}.
Since the states obtained above are not in Q
D
, add these two states to Q
D

so that
Q
D
= {{q
0
, q
1
}, {q
0
, q
1
}, {q
1
}} ____________(ii).

Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 113
The corresponding transition on a = 0 and a = 1 are shown below.

E
S
t
a
t
e
s

o 0 1
{q
0
} {q
0
, q
1
} {q
1
}
{q
0
, q
1
}
{q
1
}

Consider the state {q
0
, q
1
}:
When a = 0, o
D
({q
0
, q
1
}, 0) = o
N
({q
0
, q
1
}, 0)
= o
N
(q
0
, 0) o
N
(q
1
, 0)
= {q
0
, q
1
} {q
2
}
= {q
0
, q
1
, q
2
}.
When a = 1, o
D
({q
0
, q
1
}, 1) = o
N
({q
0
, q
1
}, 1)
= o
N
(q
0
, 1) o
N
(q
1
, 1)
= {q
1
} {q
2
}
= {q
1
, q
2
}.
Since the states obtained above are not defined in Q
D
(refer (ii)), add these
two states to Q
D
so that
Q
D
= {{q
0
, q
1
}, {q
0
, q
1
}, {q
1
}, {q
0
, q
1
, q
2
}, {q
1
, q
2
}} ____________(iii).
E
S
t
a
t
e
s

o 0 1
{q
0
} {q
0
, q
1
} {q
1
}
{q
0
, q
1
} {q
0
, q
1
, q
2
} {q
1
, q
2
}
{q
1
}
{q
0
, q
1
, q
2
}
{q
1
, q
2
}

Consider the {q
0
}:
When a = 0, o
D
({q
1
}, 0) = o
N
({q
1
}, 0) = {q
2
}
When a = 1, o
D
({q
1
}, 1) = o
N
({q
1
}, 1) = {q
2
}.
Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 114
Since the states obtained above are same and {q
2
} is not in Q
D
, add the
state {q
2
} so that
Q
D
= {{q
0
, q
1
}, {q
0
, q
1
}, {q
1
}, {q
0
, q
1
, q
2
}, {q
1
, q
2
}, {q
2
}} _______(iii).
E
S
t
a
t
e
s

o 0 1
{q
0
} {q
0
, q
1
} {q
1
}
{q
0
, q
1
} {q
0
, q
1
, q
2
} {q
1
, q
2
}
{q
1
} {q
2
} {q
2
}
{q
0
, q
1
, q
2
}
{q
1
, q
2
}
{q
2
}
Consider the state {q
0
, q
1
, q
2
}:
When a = 0, o
D
({q
0
, q
1
, q
2
}, 0) = o
N
({q
0
, q
1
, q
2
}, 0)
= o
N
(q
0
, 0) o
N
(q
1
, 0) o
N
(q
2
, 0)
= {q
0
, q
1
} {q
2
} |
= {q
0
, q
1
, q
2
}.
When a = 1, o
D
({q
0
, q
1
, q
2
}, 1) = o
N
({q
0
, q
1
, q
2
}, 1)
= o
N
(q
0
, 1) o
N
(q
1
, 1) o
N
(q
2
, 1)
= {q
0
, q
1
} {q
2
} {q
2
}
= {q
1
, q
2
}.
Since the states obtained above are not new states (are already in Q
D
), do
not add these two states to Q
D
. But, the transitions on a = 0 and a = 1
should be added to the transitional table.
E
S
t
a
t
e
s

o 0 1
{q
0
} {q
0
, q
1
} {q
1
}
{q
0
, q
1
} {q
0
, q
1
, q
2
} {q
1
, q
2
}
{q
1
} {q
2
} {q
2
}
{q
0
, q
1
, q
2
} {q
0
, q
1
, q
2
} {q
1
, q
2
}
{q
1
, q
2
}
{q
2
}
Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 115
Consider the state {q
1
, q
2
}:
When a = 0, o
D
({q
1
, q
2
}, 0) = o
N
({q
1
, q
2
}, 0)
= o
N
(q
0
, 0) o
N
(q
2
, 0)
= {q
2
} |
= {q
2
}.
When a = 1, o
D
({q
1
, q
2
}, 1) = o
N
({q
1
, q
2
}, 1)
= o
N
(q
1
, 1) o
N
(q
2
, 1)
= {q
2
} {q
2
}
= {q
2
}.
Since the states obtained above are not new states (are already in Q
D
), do
not add these two states to Q
D
. But, the transitions on a =a
0
and a = 1
should be added to the transitional table as shown.
E
S
t
a
t
e
s

o 0 1
{q
0
} {q
0
, q
1
} {q
1
}
{q
0
, q
1
} {q
0
, q
1
, q
2
} {q
1
, q
2
}
{q
1
} {q
2
} {q
2
}
{q
0
, q
1
, q
2
} {q
0
, q
1
, q
2
} {q
1
, q
2
}
{q
1
, q
2
} {q
2
} {q
2
}
{q
2
}

Consider the state {q
2
}:
When a = 0, o
D
({q
2
}, 0) = o
N
({q
2
}, 0) = |
When a = 1, o
D
({q
2
}, 1) = o
N
({q
2
}, 1) = {q
2
}.
Since the states obtained above are not new states (already in Q
D
), do not
add these two states to Q
D
. 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.


Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 116
E
S
t
a
t
e
s

o 0 1
{q
0
} {q
0
, q
1
} {q
1
}
{q
0
, q
1
} {q
0
, q
1
, q
2
} {q
1
, q
2
}
{q
1
} {q
2
} {q
2
}
{q
0
, q
1
, q
2
} {q
0
, q
1
, q
2
} {q
1
, q
2
}
{q
1
, q
2
} {q
2
} {q
2
}
{q
2
} | {q
2
}
6.4.2 Example
Construct a DFA corresponding to the NFA as shown below.
States 0 1
q
0
q
1


q
0
q
1
q
1
q
0
q
0
,

q
2

Solution: The DFA corresponding to the given NFA is
E
S
t
a
t
e
s

o 0 1
{q
0
} {q
2
} {q
0,
q
1
}
{q
2
} {q
0
, q
1
} --
{q
0
, q
1
} {q
1
, q
2
} {q
0
, q
1
}
{q
1
, q
2
} {q
0
, q
1
} {q
0
}

Self Assessment Questions
1. Obtain NFA to recognize the strings abc, abd and aacd assuming E = {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
q
2
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 (q
0
).

6.5.1 Definition
A Moore machine can be with a 6-tuple (Q, E, A, o, , q
0
) where:
Q is non-empty, finite set of states.
E is non-empty, finite set of input alphabet.
A is the output alphabet
o is transition function, which is a mapping from E Q into Q.
Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 118
is the output function mapping Q into A
q
0
e Q is the start state.
Moore machine
Present State Next state Output
Input a = 0 Input a = 1
q
0
q
2
q
0
1
q
1
q
0
q
1
0
q
2
q
1
q
2
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, E, A, o, , q
0
) where:
Q is non-empty, finite set of states.
E is non-empty, finite set of input alphabets.
A is the output alphabet
o is transition function, which is a mapping from E Q into Q.
is the output function mapping E Q into A
q
0
e Q is the start state.
Present State
Input a = 0 Input a = 1
State Output State Output
q
0
q
2
1 q
1
1
q
1
q
0
0 q
2
1
q
2
q
1
0 q
0
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.

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 M
1
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
q
0
q
1
q
2
1
q
1
q
3
q
2
0
q
2
q
2
q
1
1
q
3
q
0
q
3
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
q
0
q
1
0 q
2
1
q
1
q
3
1 q
2
1
q
2
q
2
1 q
1
0
q
3
q
0 1
q
3
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.
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
q
0
q
0
q
1
1
q
1
q
0
q
2
1
q
2
q
0
q
2
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
q
0
q
0
1 q
1
1
q
1
q
0
1 q
2
0
q
2
q
0
1 q
2
0
From the state table shown above, we observe that for the states q
1
and q
2

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
q
0
q
0
1 q
1
1
q
1
q
0
1 q
1
0
In this table, we replaced the occurrence of q
2
in the remaining rows with q
1
.


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 q
i
determine the number of different outputs that are
available in u state table of the Mealy machine.
Step 2: If the outputs corresponding to state q
i
in the next state columns are
same, then retain state q
i
as it is. Else, break q
i
into different states with the
number of new states being equal to the number of different outputs of q
i
.
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
q
1
q
1
1 q
2
0
q
2
q
4
1 q
4
1
q
3
q
2
1 q
3
1
q
4
q
3 0
q
1
1


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 q
1
and q
4
are same while the outputs for the states q
2
and q
3
,
have two different values. Hence, after splitting states q
2
and q
3
, we obtain
the state table shown below.
Present State
Next State
Input a = 0 Input a = 1
State Output State Output
q
1
q
1
1 q
20
0
q
20
q
4
1 q
4
1
q
21
q
4
1 q
4
1
q
30
q
21
1 q
31
1

q
31
q
21
1 q
31
1

q
4
q
30
0 q
1
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
q
1
q
1
q
20
1
q
20
q
4
q
4
0
q
21
q
4
q
4
1
q
30
q
21
q
31
0
q
31
q
21
q
31
1
q
4
q
30
q
1
1

Now, from the state table as shown above, we observe that the output
corresponding to the initial state q
1
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 q
0
.


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
q
0
q
1
q
20
1
q
1
q
1
q
20
1
q
20
q
4
q
4
0
q
21
q
4
q
4
1
q
30
q
21
q
31
0
q
31
q
21
q
31
1
q
4
q
30
q
1
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
q
0
{q
0
, q
1
} {q
2
}
q
1
| (empty) {q
1
}
q
2
{q
1
, q
2
} |
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
q
0
q
2
Start
state


0
1 0
0 1
q
1
1
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 o. In the case of DFA, the output of o transition function is a single
state q e Q. However, in case of NFA, the output of o 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.



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.

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 R
1
using terminal symbols such as . and | that are
elements of E. 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 R
1
and R
2
are the two regular expressions,
then the union R
1
+ R
2
is also a regular expression.
2. Concatenation: The concatenation of two regular expressions is a
regular expression. For example, if R
1
and R
2
are the two regular
expressions, then the concatenation R
1
R
2
is also a regular expression.
3. Iteration: The iteration of a regular expression is also a regular
expression. For example, if R
1
is a regular expression, then the iteration
-
1
R is also a regular expression.
4. Order of evolution: The order of evolution of a regular expression is a
regular expression. For example, if R
1
is a regular expression, then
order of evolution (R
1
) 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 L
R
and S is a regular
expression denoting the language L
S
, then
a. R+S is a regular expression corresponding to the language L
R
L
S
.
Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 128
b. RS is a regular expression corresponding to the language L
R
.L
S
.
c. R* is a regular expression corresponding to the language L
R
.
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 e E, 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.




Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 129
Regular
Expressions
Meaning
(a+b)* Set of strings of as and bs of any length including the NULL
string.
(a+b)*abb Set of strings of as and bs ending with the string abb
ab(a+b)* Set of strings of as and bs starting with the string ab.
(a+b)*aa(a+b)* Set of strings of as and bs having a sub string aa.
a*b*c*

Set of strings consisting of any number of as(may be empty
string also) followed by any number of bs(may include empty
string) followed by any number of cs(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 as and bs ending with either a or bb
(aa)* (bb)
*
b

Set of strings consisting of even number of as followed by odd
number of bs.
(0 + 1)*000 Set of strings of 0s and 1s ending with three consecutive
zeros (or ending with 000).
(11)* Set of strings consisting of even number of 1s
(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 as
and alternate as and bs.
Solution: The alternate as and bs 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).
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 as and bs 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 as and bs 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 0s
and 1s with at most one pair of consecutive 0s.
Solution: It is clear from the statement that the string consisting of at most
one pair of consecutive 0s may
- begin with combination of any number of 1s and 01s represented by
(1 + 01)
*

- end with any number of 1s represented by 1
*
.
Therefore, the complete regular expression which consists of strings 0s and
1s with at most one pair of consecutive 0s 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 E = {a, b, c}.
Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 131
Solution: Strings of as, bs and cs 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 as
and bs of even length.
Solution: String of as and bs 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}.
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 as
and bs of odd length.
Solution: String of as and bs 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 as and bs 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 e {0, 1}* with at least
three consecutive 0s.
Solution: A string consisting of 0s and ls 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)
m
000(0+1)
n
m > 0 and n > 0}.
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 = (V
N
, V
T
, S, u) 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 e V
T
and w e V
T
*
.
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 e V
T
and w e V
T
*
.
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.
Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 134
7.4.4 Theorem
Let G = (V
N
, V
T
, S, u) 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 = {q
0
, q
1
, } be the variables and S = q
0
be the start state.
Let the productions in the grammar be
q
0
x
1
q
1

q
1
x
2
q
2

q
2
x
3
q
3


q
n
x
n+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 q
0
in the grammar is the start state of FA.
Step 2: For each production of the form q
i
wq
j
, the corresponding
transition defined will be of the form
o*(q
i
, w) = q
j
.
Step 3: For each production of the form q
i
w, the corresponding transition
defined will be of the form
o*(q
i
, w) = q
f
, where q
f
is the final state.
Since the string w e L(G) is also accepted by FA, by applying the transitions
obtained in step 1 through 3, the language is regular.


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 o(A, w) = B
Also, for each production of the form A w, we can introduce the transition
o(A, w) = q
f
, where q
f
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
o (S, 01) = A
o (A, 10) = B
o (B, 0) = A
o (B, 11) = q
f

The transition diagram is shown below.

Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 136
The DFA is
M = (Q, E, o, q
0
, F), where
Q = {S, A, B, q
f
, q
1
, q
2
, q
3
},
E = {0, 1}, q
0
= S (start state), F = {q
f
}, o is shown in the table. Here, the
additional vertices (states) introduced are q
1
, q
2
, q
3
.
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
o(A, w) = B
Also, for each production of the form
A w,
we can introduce the transition
o(A, w) = q
f
, where q
f
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 .
o (S, a) = A
S is the final state
o (A, a) = A
o (A, B) = B
A is the final state
o (B, b) = B
B is the final state

Observe that for each production of the form A ., make A as the final
state.
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, E, o, q
0
, F), where
Q = {S, A, B},
E = {a, b}, q
0
= S (start state), F = {S, A, B}, and o is shown in the table.

7.5 Regular Grammar from Finite Automata
7.5.1 Theorem
Let M = (Q, E, o, q
0
, F) be a finite automata. If L is the regular language
accepted by FA, then there exists a right linear grammar G = (V
N
, V
T
, S, u)
so that L = L(G).
Proof: Let M = (Q, E, o, q
0
, F), where Q = {q
0
, q
1
, , q
n
}, E = {a
1
, a
2
, , a
m
}.
A regular grammar G = (V
N
, V
T
, S, u) can be constructed, where
V
N
= {q
0
, q
1
, , q
n
}, V
T
= E, S = q
0
.
The set of productions u can be obtained as shown below.
Step 1: For each transition of the form o (q
i
, a) = q
j,
the corresponding
production is
q
i
aq
j

Step 2: If q e 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.
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 o (A, a) = B, introduce the
production A aB. If q e F (the final state), introduce the production A ..
The productions obtained from the transitions defined for DFA are shown
below.
Transitions Productions
o (S, a) = A
o (S, b) = C
o (A, a) = C
o (A, b) = B
o (B, a) = B
o (B, b) = B
o (C, a) = C
o (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 ..
Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 139
The grammar G corresponding to the productions obtained is shown below.
G = (V
N
, V
T
, S, u), where
V
N
= {S, A, B, C}
V
T
= {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 o (A, a) = B, introduce the
production A aB.
If q e F (the final state), introduce the production A .. The productions
obtained from the transitions defined for DFA are shown below.
Transitions Productions
o (S, a) = A
o (S, b) = S
o (A, a) = A
o (A, b) = B
o (B, a) = A
o (B, b) = C
o (C, a) = C
o (C, b) = C
S aA
S bS
A aA
A bB
B aA
B bB
C aC
C bC
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 = (V
N
, V
T
, S, u) where
V
N
= {S, A, B, C}
V
T
= {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 as and
bs except those containing the substring abb. Therefore, from the grammar
G we can obtain a regular language which consist of strings of as and bs
without the substring abb.
7.5.4 Example
Obtain a right linear grammar for the regular expression ((aab)
*
ab)
*
, given
by the transition diagram.

Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 141
The right linear grammar is given by
G = (V
N
, V
T
, S, u) where
V
N
= {S, A, B}
V
T
= {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.

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 = (V
N
, V
T
, S, u), where
V
N
= {C, A, B}
V
T
= {0, 1}
The set of productions: C C1 B1
B B0 A0 C0
A A1 .
C is the start symbol.


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.


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 = {a
n
b
m
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 1s.
2. The language consists a string of 1 or strings of (01)s that repeat zero
once or more times.
3. Stings of 0s and 1s ending with any number of 1s (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 = (V
N
, V
T
, S, u) where V
N
= {A, B, S}, V
T
= {a, b}
The set of productions: A Aab Bab Sab .
B Baab Aaab Saab
S ..




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
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 L
1
and L
2
are regular, then L
1
L
2
, L
1
.L
2
and L
1
*
also denote the regular
languages and we say that the regular languages are closed under union,
concatenation, star-closure.
Proof: Let L
1
and L
2
, are regular languages corresponding to the regular
expressions R
1
and R
2
.
By definition, R
1
+ R
2
, R
1
.R
2
and R
1
*
are regular expressions and so L
1
L
2
,
L
1
.L
2
, L
1
*
denote the regular languages and so regular languages are closed
under union, concatenation and star- closure.
8.2.2 Theorem
If L
1
and L
2
are regular, then the regular language is closed under
complementation.
Proof: Let M = (Q, E, o, q
0
, F) be a DFA which accepts the language L
1
.
Now, let us define the machine M
1
= (Q, E, o, q
0
, Q-F).
(Observe that there is no difference between M and M
1
except the final
states).
The non-final states of M are the final states of M
1
and final states of M are
the non- final states of M
1
. So, the language which is rejected by M is
accepted by M
1
.
Also, a language accepted by a DFA is regular. So, the language accepted
by M
1
is regular. So, a regular language is closed under complementation.
8.2.3 Theorem
If L
1
and L
2
are regular, then the regular language is closed under
intersection.
Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 147
Proof: Let us consider M
1
= (Q
1
, E, o
1
, q
1
, F
1
) which accepts L
1
and
M
2
= (Q
2
, E, o
2
, q
2
, F
2
)
which accepts L
2
.
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 L
1
L
2
, let us construct the machine M that
simulates both M
1
and M
2
where the states of the machine M are the pairs
(p, q), where p e Q
1
and q e Q
2
.
The transition for the machine M from the state (p, q) on input symbol a e E
is the
(o(p, a), o(q, a)).
That is, if, o
1
(p, a) = r and o
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 M
1
and M
2
.
Now, the machine M = (Q, E, o, q, F) recognizes L
1
L
2
where
Q = Q
1
Q
2
q = (q
1
, q
2
), where q
1
and q2 are the start states of machine M
1
and M
2

respectively.
F = {(p, q) p e F
1
and q e F
2
}
o: Q E Q, defined by
o((p, q), a) = (o
1
(p, a), o
2
(q, a))
It is clear from o that
.
o ((p, q), w) = (
.
o
1
(p, w),
.
o
2
(q, a))
and the string w is accepted only if
Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 148
.
o ((q
1
, q
2
), w) e F,
which implies that
(
.
o
1
(q
1
, w),
.
o
2
(q
2
, w)) e F

.
o
1
(q
1
, w) e F
1
and
.
o
2
(q
2
, w)) e F
2
.
w e L
1
L
2
.
Therefore, the regular language is closed under intersection.
8.2.4 Theorem
If L
1
and L
2
are regular, then the regular language is closed under difference.
Proof is straightforward as above.
8.2.5 Definition
Let E and I are set of alphabets. The function f: E I is called
homomorphism (that is, substitution where a single letter is replaced by a
string) if w = a
1
a
2
a
n
then f(w) = f(a
1
)f(a
2
)f(a
n
).
If L is made of alphabets fromE, then f(L) = {f(w) w e L} is called
homomorphic image.
8.2.6 Example
Let E = {0, 1}, E = {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(a
1
)f(a
2
)f(a
n
).
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).
Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 149
8.2.7 Example
If E = {0, 1}, I = {1, 2, 3}, f(0) = 3122, f(1) = 132, then find (0 +1 )*(00)*.
Solution: By definition we have
f(w) = f(a
1
)f(a
2
)f(a
n
).
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 e E.
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 e {0, 1} w contains an equal number of 0s and 1s}
2. {0
n
1
n
e {0, 1}
*
n > 0}
3. {a
p
e {a}
*
p > 2 is a prime number}
4. Check for the matching parentheses (not possible using DFA)
Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 150
5. Count number of as and then the number of bs (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:

Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 151
8.3.1 Lemma
Let M = (Q,E, o, q
0
, 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 e 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 uv
i
w e L for i = 0, 1, 2, .
Proof: Let M = (Q, E, o, q
0
, F) be an FA and Let x = a
1
a
2
a
3
a
m
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 q
0
, q
1
, , q
m
.
Since we have m input symbols, naturally we should have m + 1 states in
the sequence q
0
, q
1
, , q
m
, where q
0
will be the start state and q
m
will be the
final state as shown below:

It is clear from the figure that o(q
i-1
, a
i
) = q
i
for each 1 i m. In this chain of
states from q
0
to q
m
, suppose a state q appears more than once, say q = q
i

and q = q
j
, 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 a
1
a
2
a
3
a
i

Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 152
2. The second group is the ioop string from a
i+1
a
i+2
a
j-1
a
j
3. The third group is the string suffix from a
j+1
a
j+2
a
m
, 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,
uv
i
w e L.
If F = {q
m
}, a
1
a
2
a
3
a
m
e L(M), then
a
1
a
2
a
3
a
i-1
a
i
a
i+1
a
i+2
a
j-2
a
j-1
a
j
a
j+1
a
j+2
a
m
e L(M).
This can be expressed using the transition as shown below.
o (q
0
, a
1
a
2
a
3
a
i-1
a
i
a
i+1
a
i+2
a
j-2
a
j-1
a
j
a
j+1
a
j+2
a
m
)
= o (o (q
0,
a
1
a
2
a
3
a
i-1
a
i
), a
i+1
a
i+2
a
j-2
a
j-1
a
j
a
j+1
a
j+2
a
m
)
= o (q, a
j+1
a
j+2
a
m
)
= o (q
k
, a
k+1
a
k+2
a
m
)
= q
m
.
Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 153
Also, after the string
a
1
a
2
a
3
a
i-1
a
i

the machine will be in state q
i
. Since q
i
and q
j
are same, we can input the
string
a
i+1
a
i+2
a
j-2
a
j-1
a
j

any number of times and the machine will stay in q
j
only. Finally, if the input
string is
a
j+1
a
j+2
a
m

the machine enters into final state q
m
.
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.
Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 154
Step 3: Find any i such that uv
i
w e L. According to pumping lemma, uv
i
w e
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 = {a
n
b
n
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 = a
n
b
n
.
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
v
u
a aaaaaa


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, uv
i
w e L for i = 0, 1, 2, .
Step 3: If i = 0, that is, v does not appear and so the number of as will be
less than the number of bs and so the string x does not contain some
number of as followed by same number of bs (equal to that of as).
Similarly, if i = 2, 3, ..., then number of as will be more than the number of
bs and so number of as followed by equal number of bs does not exist.
But, according to pumping lemma, n number of as should be followed by n
number of bs which is a contradiction to the assumption that the language
is regular.
So, the language L = {a
n
b
n
n > 0} is not regular.
Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 155
8.4.3 Example
Show that L = {a
i
b
j
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 = a
n+1
b
n
.
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 = a
n+1
b
n
=

u
j
a

v
k
a

w
n
ab
where u = j and v = k > 1 and so that uv = u + v = j + k n.
Step 3: According to pumping lemma, uv
i
w e L for i = 0
That is,
n i k i
ab ) a ( a e L for i = 0.
Now, if we choose i = 0, number of as in string u will not be more than the
number of bs in w which is contradiction to the assumption that number of
as are more than the number of bs.
So, the language L = {a
i
b
j
i > j} is not regular.
8.4.4 Example
Show that L = {a
n
b
l
c
n+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 = {a
n
a
l
c
n+l
n + l > 0}
which can be written as
L = {a
n+1
c
n+l
n + l > 0}
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 = {0
n
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 = 0
n
e 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 = 0
n
=

u
j
0

v
k
0

w
k j n
0
where u = j and v = k > 1 and so that uv = u + v = j + k n.
Step 3: According to pumping lemma, uv
i
w e L for i = 0, 1, 2, ...
That is,

u
j
0

v
k
0

w
k j n
0 e 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 = {0
n
n is prime} is not regular.

Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 157
8.4.6 Example
Show that L = {ww w e {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 = a
n
ba
n
b e 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 = a
n
ba
n
b =

u
j
a

v
k
a

w
n
b ba
where u = j and v = k > 1 and so that uv = u + v = j + k n.
Step 3: According to pumping lemma, uv
i
w e L for i = 0, 1, 2, ...
That is, b ba ) a ( a
n i k j
e L for i = 0, 1, 2, ...
Now, if we choose i = 0, number of as on the left of first b will be less than
the number of as after the first b which is contradiction to the assumption
that they are not equal. Therefore, the language L = {ww w e {a, b}*} is not
regular.
8.4.7 Example
Show that L = {a
n
n = k
2
for k > 0} is not regular.
Equivalently: The above language can also be defined as: L = {a
n
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 = a
m
e L (where m =
n
2
)
Step 2: Note that x > n and so, we can split x into uvw such that uv n
and v > 1 as shown below.
Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 158
x = a
m
=

u
j
a

v
k
a

w
k j m
a

,
where u = j and v = k > 1 and so that uv = u + v = j + k n.
Step 3: According to pumping lemma, uv
i
w e L for i = 0, 1, 2, ...
That is,
k j m ki j
a a a

e L for i = 0, 1, 2,
Now, if we choose i = 2, we have
uv
2
w e L.
That is,
k j m i 2 j
a a a

e L for i = 0, 1, 2,
Therefore, a
m+k
e L. Since k > 1, we have,
a
m+k
= m + k = n
2
+ k (since m = n
2
).
Note that
n
2
< n
2
+ k < n
2
+ 1 (when k = 1) < (n
2
+ 2n + 1) = (n + 1)
2
.
This means that n
2
< (n + 1)
2
,
Since n
2
+ k lies between n
2
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 = {a
n
n = k
2
for k > 0}
is not regular.

Self Assessment Questions
5. Verify whether the language L = {ww w e {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,
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 as in w is less than the number of bs 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.



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.
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 = (V
N
, V
T
, u, S), where
V
N
is set of variables or non-terminals
V
T
is set of terminals symbols
u 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 o, where
o e (V
N
V
T
)* and A e V
N
. 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. o e (V
N
V
T
)* 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: n
x
(w) = number of xs in the string w.
9.2.3 Example
Let G = (V
N
, V
T
, u, S) be a CFG, where
V
N
= {S}
V
T
= {a, b}
u: S aSa bSb .
S: Starting symbol.
Find the language generated by this grammar.
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 w
R
).
Hence, the language generated by this grammar is
L = {ww
R
w e {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 = {a
m
b
n
m = n} is context free.
Solution: It is clear from the given language that a number of as are
followed by n number of bs and number of as and bs are not equal.
As a first attempt, we can have the production
S aSb
using which n number of as are followed by one S followed by n number of
bs are generated.
Now, if we replace the non-terminal S by the production
S .
we get n number of as followed by n number of bs.
Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 163
But, we should see that number of as and bs are different. So, we should
be in a position to generate either one or more extra as or one or more
extra bs. Hence, instead of the production
S .
we can have productions
S A B
From the non-terminal A, we can generate one or more as and from non-
terminal B, we can generate one or more bs as shown below:
A aA a
B bB b.
So, the context free grammar G = (V
N
, V
T
, u, S), where
V
N
= {S, A, B}, V
T
= {a, b},
u: S aSb A B
A aA a
B bB b
S: starting symbol,
which generates the language L = {a
m
b
n
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 = {a
n
ww
R
b
n
w e E
*
, n > 1}.
Solution: Here, the string ww
R
must be enclosed between a
n
and b
n
where
n > 1. The final grammar is
V
N
= (S}
V
T
= {a, b}
u: S aSb aAb
A aAa bAb ., and
S is the start symbol.
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 w
R
= 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)
aaba.abbb (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 As
(possibly none) are followed by any number of Bs (possibly none). Any
number of As (that is, 011s or 1s) can be generated using the productions
A 011A 1A .
Any number of Bs (that is, 01s) 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 = (V
N
, V
T
, u, S), where
V
N
: {S, A, B}
V
T
: {0, 1}
u: S AB,
A 011A 1A .
B 01B .
S: start symbol.
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 > : : = < B
1
> , < A > : : = < B
2
> , , < A > = < B
n
>
In BNF may be combined as :
< A > : : = <B
1
>|<B
2
>||B
n
>
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 T
G V V S = u where
V
N
= {Decimal number, Decimal fraction, Unsigned integer, Digit}.
V
T
= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}


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 = u where
: , , , . S aB A aB B bA B b u
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 N
G V V S S = = = u ,
where the set of productions
: 11 , 0. S S S u

Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 167
2. Find the language L (G), generated by the grammar.

{ } { } ( )
, , , , , ,
T N
G V x y z V S A S = = = u ,
where : , , , . S xS S yA A yA A Z u
3. Find the language L (G), generated by the grammar

{ } { } ( )
, , , ,
T N
G V a b V S S = = = u , where
: , , . S aaS S a S b u
4. Suppose G = ({S, A, B}, {a, b}, u, S), where u consists of the following
productions: S abAB, A aBb, B abA, ba bab, aB aaa. Then
show that w = abaaababaaab e L(G).
5. The CFG to generate a language consisting of equal number of as and
bs is ________
6. Obtain a CFG to generate unequal number of as and bs.

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 N
G V a b V S S = = = u ,
Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 168
where : , . S aSb S ab u
The language to this grammar is
{ }
1
n n
a 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 N
G V a b V S A B S = = = u ,
Where the productions uare:
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
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
Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 170
9.4 Ambiguous Grammar
Let G = (V
N
, V
T
, u, S) be a context free grammar. A grammar G is
ambiguous if and only if there exists at least one string w e V
T
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
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 = + u with productions : , . S S S S a u +
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
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.

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)
{ }
1
n n
L a ba n = >
(Hint:
{ } { } ( )
, , , ,
T N
G V a b V S S = = = u , where uis:
, . S aSa S b
It is a context free language).
ii)
{ }
1 , 1
n m
L a ba n m = > >
(Hint:
{ } { } ( )
, , , , , ,
T N
G V a b V S A S = = = u where
: , , , , , . S aAb S bAa A bAa A aAb A ab A ba u
It is a context free language).
2. Give the BNF notations for the productions of the grammar.

( ) { } { }
( )
, , , , , , , , ,
T N
G V a V S A B S = = + = u where
: , , , S a A A a B B a B B a u + + +
(Hint: :: S a A < > = +< >
:: A a B < > = +< >
:: | B a B a < > = +< > ).

Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 174
3. Draw the derivation tree for the sentence x
2
y
2
z 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, 1
n m
L G x y z n m = > >
3.
{ } { }
2 1 2
( ) 0 0
n n
L G a n a b n
+
= > >
5. G = (V
N
, V
T
, S, u) where
V
N
= {S, A, B}
V
T
= {a, b}
The set of productions: S aB bA
A aS bAA a
B bS aBB b
S is the starting symbol.
6. G = (V
N
, V
T
, S, u) where
V
N
= {S, A, B}
V
T
= {a, b}
S
S
A
Z
x
x
y
y
S
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.



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, E, I, o, q
0
, Z
0
, F), where
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 177
Q: non-empty, finite set of states.
E: non-empty, finite set of input alphabet.
I: set of stack alphabet
o: transition function, which is a mapping from
Q (E .) I finite subset of Q I.
q
0
e Q: is the start state.
Z
0
e I 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.

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 a
0
b
0
w
1
a
0
b
1
. Here, b
0
initial
state of the machine and b
1
refers to the new state obtained while
reading the first symbol w
1
of the input string. Initially the pushdown
store is empty and represented by a
0
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 a
i
b
i
w
j
b
i
b
j
. Here, b
i
refers to
current state, b
j
refers to the new state of PDA and w
j
is the input
symbol. During transition b
i
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 a
i
b
i
a
j
b
j
.
Here, a
i
is the symbol at the top of the stack with the current state b
i
,
whereas a
j
refers to the new symbol at the top of the stack when the PDA
moves to its new state b
j
.
10.2.4 Example
What does each of the following transition represent?
i) o(p, a, Z) = (q, aZ)
ii) o(p, a, Z) = (q, .)
iii) o(p, a, Z) = (q, r)
iv) o(p, ., Z) = (q, r)
v) o(p, ., .) = (q, Z)
vi) o(p, ., Z) = (q, .)
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 179
Solution:
o(p, a, Z) = (q, aZ), means that the PDA in current state p after scanning the
input symbol a e E and if Z e I is on top of the stack, then the PDA enters
into new state q pushing a e E on to the stack.
The transition o(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 o(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 o(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 o(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 o(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, E, I, o, q
0
, Z
0
, F) be a PDA. The move of machine M
o(p, a, Z) = (q, o), where p, q e Q, a e E, Z e I and o e I
*

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 o.
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
o(p, a, Z) = (q, o)
then, there will be an arc from state p to state q and the arc is labeled
with
a, Z/o
indicating a as the current symbol, Z as the symbol on top of the stack
and o 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) o(p, a, Z) = (q, aZ)
ii) o(p, a, Z) = (q, .)
iii) o(p, a, Z) = (q, r)
iv) o(p, ., Z) = (q, r)
v) o(p, ., .) = (q, Z)
vi) o(p, ., Z) = (q, .)

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
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 182
v)

vi)

10.3.2 Definition
Let M = (Q,E, I, o, q
0
, Z
0
, F) be a PDA. An ID (instaneous description) is
defined as a triple
(q, w, o),
where q is the current state, w is the string to be processed and o is the
current content of stack. The leftmost symbol in the string o is on top of the
stack and rightmost symbol in o is at the bottom of the stack.
Observation: Let the current configuration of PDA be
(q, aw, Zo)
It means that
q: the current state
aw: the string to be processed
Zo: 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 o(q, a, Z) = (p, |) then the new configuration
obtained will be
p

q

(., Z)/r
p

q

(., .)/r
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 183
(p, w, |o)
The move from the current configuration to next configuration is
given by
(q, aw, Zo) (p, w, |o)
This can be read as
the configuration (q, aw, Zo) derives (p, w, |o) 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, Zo)
*
(p, w, |o)
means that the current configuration of the PDA will be (q, aw, Zo) and after
applying zero or more number of transitions, the PDA enters into new
configuration (p, w, |o).
The instantaneous description (q, aw, Zo)
+
(p, w, |o) indicates that the
configuration (p, w, |o) is obtained from the configuration (q, aw, Zo) 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:

Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 184
10.4.1 Definition
i) Let M = (Q, E, I, o, q
0
, Z
0
, F) be a PDA. The language L(M) accepted
by a final state is defined as
L(M)= {w (q
0
, w, Z
0
) * (p, ., o)}
for some o e I
*
, p e F and w e E
*
.
(Alternatively: The PDA, currently in state q
0
, 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 (q
0
, w, Z
0
) * (p, ., .)}
for w e E
*
, q
0
, p e Q.
10.4.2 Example
Obtain a PDA to accept the language L = {wcw
R
w e (a + b)*} where w
R
is
reverse of w by a final state.
Solution: If w = abb, then the reverse of w denoted by w
R
= bba.
The language L will be wcw
R
, that is, abbcbba, which is a string that is
palindrome.
Therefore, we have to construct a PDA which accepts a palindrome
consisting of as and bs 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
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 q
0
be the initial state and Z
0
be the initial symbol on the stack. In state q
0

and when top of the stack is Z
0
, whether the input symbol is a or b, push it
on to the stack and remain in q
0
. The transitions defined for this can be of
the form
o(q
0
, a, Z
0
) = (q
0
, aZ
0
)
o(q
0
, b, Z
0
) = (q
0
, bZ
0
)
Once the first scanned input symbol is pushed on to the stack, the stack
may contain either a or b.
Now, in state q
0
, 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
o(q
0
, a, a) = (q
0
, aa)
o(q
0
, b, a) = (q
0
, ba)
o(q
0
, a, b) = (q
0
, ab)
o(q
0
, b, b) = (q
0
, 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 q
0
, the first symbol itself can be c. In
this case, w is null string and Z
0
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 q
1
, and do not alter the contents of the stack. The
transitions defined for this can be of the form
o(q
0
, c, Z
0
) = (q
1
, Z
0
)
o(q
0
, c, a) = (q
1
, a)
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 186
o(q
0
, c, b) = (q
1
, 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 q
1
and delete that symbol
from the stack and repeat the process. The transitions defined for this can
be of the form
o(q
1
, a, a) = (q
1
, .)
o(q
1
, b, b) = (q
1
, .)
Step 4: In state q
1
, 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 Z
0
.
Now, change the state to q
2
and do not alter the contents of the stack. The
transition for this can be of the form
o(q
1
, ., Z
0
) = (q
2
, Z
0
)
Therefore, the PDA M to accept the language
L = {wcw
R
w e {a, b}
*
}
is given by
M = (Q,E , I, o, q
0
, Z
0
, F)
where
Q = {q
0
, q
1
, q
2
}, q
0
is the start state
E = {a, b, c}
I = {a, b, Z
0
}, Z
0
is the initial number of the stack, F = {q
2
} is the final state.
The transitions o are given by
o(q
0
, a, Z
0
) = (q
0
, aZ
0
)
o(q
0
, b, Z
0
) = (q
0
, bZ
0
)
o(q
0
, a, a) = (q
0
, aa)
o(q
0
, b, a) = (q
0
, ba)
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 187
o(q
0
, a, b) = (q
0
, ab)
o(q
0
, b, b) = (q
0
, bb)
o(q
0
, c, Z
0
) = (q
1
, Z
0
)
o(q
0
, c, a) = (q
1
, a)
o(q
0
, c, b) = (q
1
, b)
o(q
1
, a, a) = (q
1
, .)
o(q
1
, b, b) = (q
1
, .)
o(q
1
, ., Z
0
) = (q
2
, Z
0
)
The transition diagram is

q
0
q
1
q
2

a, Z
0
/aZ
0
b, Z
0
/bZ
0
a, a /aa

b, a/ba

a, b/ab
b, b/bb



c,
Z
0
/aZ
0
c, a / a

c, b / b



a, a / .

b, b / .




., Z
0
/ Z
0






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:
(q
0
, aabcbaa, Z
0
) (q
0
, abcbaa, aZ
0
)
(q
0
, bcbaa, aaZ
0
)
(q
0
, cbaa, baaZ
0
)
(q
1
, baa, baaZ
0
)
(q
1
, aa, aaZ
0
)
(q
1
, a, aZ
0
)
(q
2
, ., Z
0
)
Since q
0
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:
(q
0
, aabcbab, Z
0
) (q
0
, abcbab, aZ
0
)
(q
0
, bcbab, aaZ
0
)
(q
0
, cbab, baaZ
0
)
(q
0
, bab, baaZ
0
)
(q
1
, ab, aaZ
0
)
(q
1
, b, aZ
0
)
Since the transition o(q
1
, 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 = {a
n
b
n
n > 1} by a final state.
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 189
Solution:
General Method: Since n number of as should be followed by n number of
bs, let us push all the symbols on to the stack as long as the scanned input
symbol is a. Once we encounter bs, 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 as followed by n
number of bs.
Step 1: Let q
0
be the start and Z
0
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 q
0
.
The transitions defined for this can be of the form
o(q
0
, a, Z
0
) = (q
0
, aZ
0
)
o(q
0
, a, a) = (q
0
, aa)
Step 2: In state q
0
, if the next input symbol to be scanned is b and if the top
of the stack is a, change the state to q
1
and delete one b from the stack.
The transition for this can be of the form
o(q
0
, b, a) = (q
1
, .)
Step 3: Once the machine is in state q
1
, the rest of the symbols to be
scanned will be only bs 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 q
1
and delete the
corresponding a from the stack. The transitions defined for this can be of
the form
o(q
1
, b, a) = (q
1
, .)
Step 4: In state q
1
, if the next input symbol to be scanned is . and if the top
of the stack is Z
0
, change the state to q
2
which is an accepting state. The
transition defined for this can be of the form
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 190
o(q
1
, ., Z
0
) = (q
2
, Z
0
)
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
(q
0
, aaabbb, Z
0
) (q
0
, aabbb, aZ
0
)
(q
0
, abbb, aaZ
0
)
(q
0
, bbb, aaaZ
0
)
(q
1
, bb, aaZ
0
)
(q
1
, ., Z
0
)
(q
2
, ., Z
0
)
q
0
q
1
q
2

a,
Z
0
/aZ
0
a, a /aa


b, a / .







b, a / .




., Z
0
/ Z
0






Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 191
Since q
2
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: (q
0
, aabbb, Z
0
) (q
0
, abbb, aZ
0
)
(q
0
, bbb, aaZ
0
)
(q
0
, bb, aZ
0
)
(q
1
, b, Z
0
)
Since the transition o(q
1
, b, Z
0
) is not defined, the string
aabbb
is rejected by the PDA.
10.4.4 Problem
Obtain a PDA to accept the language L = {a
n
b
2n
n > 1} (that is, the machine
should accept n number of as followed by 2n number of bs).
Solution:
Step 1: Let q
0
be the start state and Z
0
be the initial symbol on the stack.
For each scanned input symbol a, push two as on the stack. The
transitions defined for this can be of the form
o(q
0
, a, Z
0
) = (q
0
, aaZ
0
)
o(q
0
, a, a) = (q
0
, aaa).
Step 2: In state q
0
, if the next input symbol to be scanned is b and if the top
of the stack is a, change the state to q
1
and delete one b from the stack.
The transition for this can be of the form
o(q
0
, b, a) = (q
1
, .).
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 192
Step 3: Once the machine is in state q
1
, the rest of the symbols to be
scanned will be only bs 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 q
1
and delete the corresponding a from
the stack. The transitions defined for this can of the form
o(q
1
, b, a) = (q
1
, .).
Step 4: In state q
1
, if the next input symbol to be scanned is . and if the top
of the stack is Z
0
, (it means that for each b in the input there exists
corresponding a on the stack) change the state to q
2
which is an accepting
state. The transition defined for this can be of the form
o(q
1
, ., Z
0
) = (q
2
, .).
Therefore, the PDA to accept the given language is
M = (Q,E , I, o, q
0
, Z
0
, F)
where
Q = {q
0
, q
1
, q
2
}, q
0
is the start state
E = {a, b}
I = {a, Z
0
}, Z
0
is the initial number of the stack, F = {q
2
}is the final state.
The transitions o are given by
o(q
0
, a, Z
0
) = (q
0
, aaZ
0
)
o(q
0
, a, a) = (q
0
, aaa).
o(q
0
, b, a) = (q
1
, .).
o(q
1
, b, a) = (q
1
, .).
o(q
1
, ., Z
0
) = (q
2
, .).
To accept the string: The sequence of moves made by the PDA for the
string aabbbb is shown below
Initial ID: (q
0
, aabbbb, Z
0
) (q
0
, abbbb, aaZ
0
)
(q
0
, bbbb, aaaaZ
0
)
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 193
(q
0
, bbb, aaaZ
0
)
(q
1
, bb, aaZ
0
)
(q
1
, b, aZ
0
)
(q
1
, ., Z
0
)
(q
2
, ., Z
0
)
Since q
2
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: (q
0
, aabbb, Z
0
) (q
0
, abbb, aaZ
0
)
(q
0
, bbb, aaaaZ
0
)
(q
0
, bb, aaaZ
0
)
(q
0
, b, aaZ
0
)
(q
0
, ., aZ
0
)
Since the transition o(q
0
, ., 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 e (a + b)
*
and the number of as in string w should be equal to
the number of bs in w}.

10.5 Deterministic and Non-deterministic PDA
10.5.1 Definition
Let M = (Q,E , I, o, q
0
, Z
0
, F) be a PDA. The PDA is deterministic if
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 194
i) o(q, a, Z) has only one element.
ii) If o(q, ., Z) is not empty, then o(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) = {wcw
R
w e (a + b)
*
}
Solution: The transitions defined for this machine are obtained in Example
10.4.2. For completeness we have reproduced here.
o(q
0
, a, Z
0
) = (q
0
, aZ
0
)
o(q
0
, b, Z
0
) = (q
0
, bZ
0
)
o(q
0
, a, a) = (q
0
, aa)
o(q
0
, b, a) = (q
0
, ba)
o(q
0
, a, b) = (q
0
, ab)
o(q
0
, b, b) = (q
0
, bb)
o(q
0
, c, Z
0
) = (q
1
, Z
0
)
o(q
0
, c, a) = (q
1
, a)
o(q
0
, c, b) = (q
1
, b)
o(q
1
, a, a) = (q
1
, .)
o(q
1
, b, b) = (q
1
, .)
o(q
1
, ., Z
0
) = (q
2
, Z
0
)
The PDA should satisfy the two conditions shown in the definition to be
deterministic.
1. o(q, a, Z) has only one element: Observe that in the transitions, for each
q e Q, a e E and Z e I, there is only one component defined and the
first condition is satisfied.

Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 195
2. The second condition states that if o(q, ., Z) is not empty, then o(q, a, Z)
should be empty. If there is an .-transition (that is, o (q
1
, ., Z
0
)), then
there should not be any transition from the state q
1
when top of the stack
is Z
0
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 = {a
n
b
2n
n > 1} deterministic ?
Solution: The transitions (refer example: 10.4.4) defined for this machine are
o(q
0
, a, Z
0
) = (q
0
, aaZ
0
)
o(q
0
, a, a) = (q
0
, aaa).
o(q
0
, b, a) = (q
1
, .).
o(q
1
, b, a) = (q
1
, .).
o(q
1
, ., Z
0
) = (q
2
, .).
The first condition to be deterministic is o(q, a, Z) should have only one
component. In this case, for each q e Q, a e E and Z e I, there exists only
one definition. Therefore, the first condition is satisfied.
To satisfy the second condition, consider the transition
o(q
1
, ., Z
0
) = (q
2
, .)
Since the transition is defined, the transition o(q
1
, ., Z
0
) where a e E 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 e (a, b)* and w contains more as than bs} 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?
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 = {ww
R
w e (a + b)*} is deterministic?

10.8 Answers
Self Assessment Questions
2.

q
0
q
2

a, Z
0
/aZ
0
b, Z
0
/bZ
0
a, a / aa

b, b / bb

a, b/ .

b, a / .


., Z
0
/ Z
0




Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 197
3. The transitions are
o(q
0
, a, Z
0
) = (q
0
, aZ
0
)
o(q
0
, b, Z
0
) = (q
0
, bZ
0
)
o(q
0
, a, a) = (q
0
, aa)
o(q
0
, b, b) = (q
0
, bb)
o(q
0
, a, b) = (q
0
, .)
o(q
0
, b, a) = (q
0
, .)
o(q
0
, ., a) = (q
1
, a).
The first condition to be deterministic is that o (q, a, Z) should have only
one component. In this case, for each q e Q, a e E and Z e I, there
exists only one component. Therefore the first condition is satisfied.
To satisfy the second condition, consider the transition
o (q
0
, ., a) = (q
1
, a)
Since this transition is defined, the transition o(q
0
, f, a) where f e E
should not be defined. But, there are two transitions
o(q
0
, a, a) = (q
0
, aa)
o(q
0
, b, a) = (q
0
, .)
defined from q
0
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.
Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 198
7. An instantaneous description is given as (q, a, Z) where q e Q, a e E
and Z e I. Since a PDA uses stack memory, its working can be
described as the change in instantaneous description.


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.

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 e 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 uv
i
w
i
y e 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 e L is context free. Otherwise, the
string z e L is not context free.
Proof of Pumping Lemma:
By Pumping Lemma, it is assumed that string z e L is finite and is context
free language. We know that z is string of terminal which is derived by
applying series of productions.

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 e L implies that after applying some / all productions some
number of times, we get finally string of terminals and the derivation stops.
Let z e 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
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
Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 203
A
-
vAx
and
A
-
w
are also possible. It follows that,
uv
i
wx
i
y e L
Hence the proof.

11.3 Applications of Pumping Lemma for CFLs
The Pumping Lemma for CFLs 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 uv
i
wx
i
y e L. According to Pumping Lemma, uv
i
wx
i

y e 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 = {a
n
b
n
c
n
n > 0} is not context free.
Solution:
Step 1: Let L is context free and is infinite. Let z = a
n
b
n
c
n
e L.
Step 2: Note that z > n and so we can split z into uvwxy such that
vwx n and vx > 1
Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 204
and so by Pumping Lemma, uv
i
wx
i
y e L for i = 0, 1, 2...
Case 1: The string vwx is within a
n
.
Let v = a
j
, x = a
k
where vx = j + k > 1 and vwx n which can be shown
as




y
n
n n
uvwx
c cc b bb a aa ... ... ...
Now, by Pumping Lemma for i = 2, uv
2
wx
2
y e L and the language generated
is




y
n
n
k j n
wx uv
c cc b bb a aa ... ... ...
2 2
+ +

Note that uv
2
wx
2
y = a
n+j+k
b
n
c
n
e L (since the string should have some
number of as followed by equal number of bs and cs) when j + k > 1, a
contradiction.
Case 2: The string vwx is a
n
b
n
.
Let v = a
j
, x = b
k
where vx = j + k > 1 and vwx n which can be shown
as

By Pumping Lemma, uv
2
wx
2
y e L for i = 2 and the language generated is


Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 205
Note that uv
2
wx
2
y = a
n+j
b
n+k
c
n
e L when j + k > 1, contradiction.
Case 3: The string vwx is within c
n
.
Let v = c
j
, x = c
k
where vx = j + k > 1 and vwx n which can be shown
as

By Pumping Lemma, uv
2
wx
2
y e L for i = 2 and the language generated is

Note that uv
2
wx
2
y = a
n
bc
n+j+k
e L when j + k > 1, contradiction.
But according to Pumping lemma, n number of as should be followed by n
number of bs which in turn should be followed by n number of cs. In all the
three cases we get contradiction to the assumption that the language is
context free.
Therefore, the language L = {a
n
b
n
c
n
n > 0} is not context free.
11.3.2 Example
Show that L = {a
n!
n > 0} is not context free.
Solution:
Step 1: Assume that L is context free and is infinite.
Let z = a
n!
e L where a
n!
> n.
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 = a
n!

into u, v, w, x and y such that
vwx n and vx > 1
so that uv
i
wx
i
y e L for i = 0, 1, 2,
The splitting of the string z is shown below.

Let v = a
j
, x = a
k
such that vx = j + k > 1 and vwx n.
The string uv
2
wx
2
y can be generated and shown below.
Now uv
2
wx
2
y = a
n! + 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
uv
2
wx
2
y = a
n! + j +k
e L,
which is a contradiction.
Therefore, the language L = {a
n!
n > 0} is not context free.
Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 207
Self Assessment Questions
1. Prove that the language L = {a
2
b
2
c
2
} is not context free.
2. Prove that the language L = {a
3!
} is not context free.
3. Prove that the language L = {aaabbbaaabbb} is not context free.

11.4 Closed Properties
11.4.1 Theorem
If L
1
and L
2
are context free languages, then L
1
L
2
is also a context free
language.
Proof:
Let L
1
and L
2
are two CFLs generated by the CFGs
G
1
= (V
N1
, V
T1
, u
1
, S
1
)
and
G
2
= (V
N2
. V
T2
, u
2
, S
2
)
respectively and assume that V
N1
and V
N2
are disjoint.
Consider the language L
3
generated by the grammar
G
3
= (V
N1
V
N2
S
3
, V
T1
V
T2
, u
3
, S
3
)
where
S
3
is the start symbol for the grammar G
3
and S
3
e (V
N1
V
N2
)
u
3
= u
1
u
2
{S
3
>S
1
S
2
}
It is clear from this that the grammar G
3
is context free and the language
generated by this grammar is context free. It is easy to prove that
L
3
= L
1
L
2

If we assume w e L
1
, then the possible derivation from S
3
is
S
3
S
1

-
W
On similar lines, if we assume w e L
2
, then the possible derivation from S
3
is
Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 208
S
3
S
2

-
w
So, if w e L
3
, one of the derivations
S
3
S
1

or
S
3
S
2

is possible.
In the first case, all the variables in V
1
and all the terminals in T
1
may be
used to get the derivation
S
1

-
W
which uses only the productions in u
1
. Similarly all the variables in V
N2
and
all the terminals in V
T2
may be used to get the derivation
S
2

-
w
which uses only the productions in u
2
and it follows that
L
3
= L
1
L
2
.
Therefore, context free languages are closed under union.
11.4.2 Theorem
If L
1
and L
2
are context free languages, then L
1
L
2
is also a context free
language.
Proof:
Let L
1
and L
2
are two CFLs generated by the CFGs
G
1
= (V
N1
, V
T1
, u
1
, S
1
)
and
G
2
= (V
N2
. V
T2
, u
2
, S
2
)
respectively and assume that V
1
and V
2
are disjoint.
Consider the language L
3
generated by the grammar
G
3
= (V
N1
V
N2
S
3
, V
T1
V
T2
, u
3
, S
3
)
Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 209
where
S
3
is the start symbol for the grammar G
3
and S
3
e (V
N1
V
N2
)
u
3
= u
1
u
2
{S
3
>S
1
S
2
}
It is clear from this that the grammar G
3
is context free and the language
generated by this grammar is context free. It is easy to prove that
L
3
= L
1
L
2
.
11.4.3 Theorem
Context free languages are closed under stat-closure.
Proof is similar to the above.
11.4.4 Theorem
If L
1
and L
2
are context free languages, then it is not always true that L
1

L
2
is also a context free language.
Proof:
Let us consider the following example:
Take L
1
= {a
n
b
n
c
m
n > 0, m > 0} and L
2
= {a
n
b
m
c
m
n > 0, m > 0}.
Then the two languages are context free. The corresponding context free
grammar are
S S
1
S
2,
S
1
aS
1
b ., S
2
cS
2
. and
S aS S
1
, S
1
bS
1
c


.
Now their intersection L
1
L
2
= {a
n
b
n
c
n
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).
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.
L
1
and L
2
are context free languages

1
L and
2
L are also context free (by assumption)

1
L
2
L is context free (since context free languages are closed under
unions)
2
1
L L is context free (by assumption)
Now
2
1
L L = L
1
L
2
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 = {a
n
b
n
c
n
n > 0} is not context free.
Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 211
4. Show that L = {a
p
b
q
p = q
2
} 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

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
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.
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, E, I, o, q
0
, , F), where
Q is a set that is used to contain different states of machine.
E is set of input alphabets.
I is a set of tape symbols.
represents a of blank character that belongs to I, and E _ I - .
o is the transition function defined as o: Q I Q I {L, R}
q
0
is the initial state of the Turing machine.
F is the set of final states or accepting states such that F _ Q.
In general, o is a partial function on Q I; its interpretation gives the
principle by which a Turing machine operates. The arguments of o 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 o is not defined.
12.2.2 Example
The following figure shows the situation before and after the move caused
by the transition
o(q
0
, a) = (q
1
, d, R)

Theory of Computer Science Unit 12
Sikkim Manipal University Page No.: 215
12.2.3 Example
Consider the Turing machine defined by
Q = {q
0
, q
1
}, E = {a, b}, I = {a, b, }, F = {q
1
}, and
o(q
0
, a) = (q
0
, b, R)
o(q
0
, b) = (q
0
, b, R)
o(q
0
, ) = (q
0
, , L)
If this Turing machine is started in state q
0
with the symbol a under the read
write head, the applicable transition rule is o(q
0
, a) = (q
0
, 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 q
0
. Any subsequent a will also be
replaced with b but bs will not be modified. When the machine encounters
the first blank, it will move one cell left, and then halts in final state q
1
.
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 o 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.
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 oq|, where q is the current state, o| is the string
made from tape symbols denoted by I. The read write head points to the
first character of the substring |. The initial ID is denoted by qo|, where q is
the start state and the read write head points to the first symbol of o from
left. The final ID is denoted by o | q where q e 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.

a
1
a
2
a
3
a
4
q
2
a
5
a
6
a
7
a
8
..

Read-Write Head





Control
Unit
Theory of Computer Science Unit 12
Sikkim Manipal University Page No.: 217
In this machine, each a
i
e I. In this snapshot, the symbol a
5
is under read-
write head which is the next symbol to be scanned and the symbol towards
left of a
5
(that is, q
2
is the current state). So, in this case an ID is denoted by

| o
8 7 6 5
q
2 4 3 2 1
a a a a q a a a a
where the substring

o
4 3 2 1
a a a a
is left of q
2
; and the substring

|
8 7 6 5
a a a a
is right of q
2
. The string q
2
is the current state of the machine.
The symbol a
5
is the next state symbol to be scanned. Assume that the
current ID of the Turing machine is
... a a a a q a a a a
8 7 6 5 2 4 3 2 1

as shown in the above diagram.
Suppose, there is a transition
o (q
2
, a
5
) = (q
3
, b
1
, R).
It means that if the machine is in state q
2
and the next symbol to be scanned
is a
5
, then the machine enters into state q
3
replacing the symbol a
5
by b
1

and R indicates that the read-write head is moved one symbol towards right.
The configuration obtained is
... a a a q b a a a a
8 7 6 3 1 4 3 2 1

This can be represented by a move as shown below.
... a a a a q a a a a
8 7 6 5 2 4 3 2 1
... a a a q b a a a a
8 7 6 3 1 4 3 2 1

Similarly, if the current ID of the Turing machine is
... a a a a q a a a a
8 7 6 5 2 4 3 2 1

Theory of Computer Science Unit 12
Sikkim Manipal University Page No.: 218
and the transition function is
o (q
2
, a
5
) = (q
3
, c
1
, L),
means that if the machine is in state q
2
and the next symbol to be scanned
is a
5
, then the machine enters into state q
1
replacing the symbol a
5
by c
1
and
L indicates that the read-write head is moved one symbol towards left. The
new configuration obtained is
... a a a c a q a a a
8 7 6 1 4 1 3 2 1

This can be represented by a move as shown below:
... a a a a q a a a a
8 7 6 5 2 4 3 2 1
... a a a c a q a a a
8 7 6 1 4 1 3 2 1

This configuration indicates that the new state is q
1
, the next input symbol to
be scanned is a
4
. 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, E, I, o, q
0
, , F) be a TM. Let the ID of M be
n 1 k k 1 k 3 2 1
a ... a qa a ... a a a
+
,
where each a
j
e I for 1 j n, q e Q is the current state and a
k
as the next
symbol to be scanned. If there is a transition
o(q, a
k
) = (p, b, R)
then the move of machine M will be
n k k k
a a qa a a a a ... ...
1 1 3 2 1 +

n k k k
a ba pa a a a a ... ...
1 1 2 3 2 1 +

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, E, I, o, q
0
, , F) be a TM. The language L(M) accepted by M is
defined as
L(M) = {w q
0
w
*
o
1
po
2
, where w e E
*
, p e F and o
1
, o
2
e I
*
}
where q
0
w is the initial ID and o
1
po
2
is the final ID. The set of all those
words w e E
*
which causes M to move from start state q
0
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 q
0
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 q
1
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 1s.
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 = {0
n
1
n
2
n
n > 1}
Solution: It is given that the language should consists of n number of 0s
followed by n number of 1s which in turn should be followed n number of
2s.
Let us consider the string 000011112222 and we shall design the Turing
machine.
To design the Turing Machine, consider the situation where first 0s are
replaced by Xs, first 1s are replaced by Ys and first 2s are replaced by Zs
as shown below.



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 q
0
, if the next scanned symbol is 0, replace it by X, change the state
to q
1
and move the pointer towards right and the situation shown in (ii) is
obtained. The transition for this can be of the form
o(q
0
, 0) = (q
1
, X, R)
In state q
1
, 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 q
1
only. The transitions for this can be of the form
o(q
1
, 0) = (q
1
, 0, R)
o(q
1
, Y) = (q
1
, Y, R)
The configuration shown in (iii) is obtained. In state q
1
, on encountering 1
change the state q
2
, replace 1 by Y and move the pointer towards right. The
transition for this can be of the form
o(q
1
, 1) = (q
2
, Y, R)
and the configuration shown in (i) is obtained.





XX00YY11ZZ22 XXX0YY11ZZ22 XXX0YY11ZZ22


q
0
q
1
q
1

(i) (ii) (iii)




q
0
Theory of Computer Science Unit 12
Sikkim Manipal University Page No.: 222






In state q
2
, 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 q
2
only and the configuration shown in (v) is obtained. The
transitions of the form
o(q
2
, 1) = (q
2
, 1, R)
o(q
2
, Z) = (q
2
, Z, R)
In state q
2
, on encountering 2, change the state to q
3
, replace 2 by Z and
move the pointer towards left. The transition for this can be of the form
o(q
2
, 2) = (q
3
, Z, L)
and the configuration shown in (vi) is obtained. Once the TM is in state q
3
, 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
Zs, 1s, Ys, 0s 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 q
3
only.
The transitions for this can be of the form
o(q
3
, Z) = (q
3
, Z, L)
o(q
3
, 1) = (q
3
, 1, L)
o(q
3
, Y) = (q
3
, Y, L)
o(q
3
, 0) = (q
3
, 0, L)
XXX0YYY1ZZ22 XXX0YYY1ZZ22 XXX0YYY1ZZZ2


q
2
q
2
q
3

(iv) (v) (vi)




q
0
Theory of Computer Science Unit 12
Sikkim Manipal University Page No.: 223
Only on encountering X, replace X by X, change the state to q
0
and move
the pointer towards right to get leftmost 0. The transition for this can be of
the form
o(q
3
, X) = (q
0
, X, R).
Therefore, we get




In state q
0
, if the input symbol is Y, it means that there are no 0s. If there
are no 0s we should see that there are no 1s also. For this to happen,
change the state to q
4
, replace Y by Y and move the pointer towards right.
The transition for this can be of the form
o(q
0
, Y) = (q
4
, Y, R).
In state q
4
, search for only Ys, replace Y by Y, remain in state q
4
only and
move the pointer towards right. The transition for this can be of the form
o(q
4
, Y) = (q
4
, Y, R).
In state q
4
, if we encounter Z, it means that there are no 1s and so we
should see that there are no 2s and only Zs should be present. So, on
scanning the first Z, change the state to q
5
, replace Z by Z and move the
pointer towards right. The transition for this can be of the form
o(q
4
, Z) = (q
5
, Z, R).
But, in state q
5
only Zs should be there and no more 2s. So, as long as the
scanned symbol Z remains in state q
5
, replace Z by Z and move the pointer
towards right. But, once blank symbol is encountered, change the state to
q
6
, 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
o(q
5
, Z) = (q
5
, Z, R).
XXXXYYYYZZZZ

q
0





q
0
Theory of Computer Science Unit 12
Sikkim Manipal University Page No.: 224
o(q
5
, ) = (q
6
, , R).
Here, q
6
is the final state.
Therefore, the TM to recognize the language L = {0
n
1
n
2
n
n > 1} is given
by
Q = {q
0
, q
1
, q
2
, q
3
, q
4
, q
5
, q
6
}
E = {0, 1, 2}
I = {0, 1, 2, X, Y, Z, }
q
0
is the start state
is the blank character
F = {q
6
} is the final character
o is shown in the following transition table.
States
E
0 1 2 Z Y X

q
0
q
1,
X, R

q
4,
Y, R
q
1
q
1,
0, R q
2,
Y, R q
1,
Y, R
q
2
q
2,
1, R q
3,
Z, L q
2,
Z, R
q
3
q
3,
0, L q
3,
1, L q
3,
Z, L q
3,
Y, L q
0,
X, R
q
4
q
5,
Z, R q
4,
Y, R
q
5
q
5,
Z, R
(q
0,
, R)
q
6









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 0s
and 1s ending with 011.
Solution: The DFA which accepts the language consisting of 0s and 1s
ending with the string 001 is shown below.



Theory of Computer Science Unit 12
Sikkim Manipal University Page No.: 226
The transition table for the DFA is shown below.
o 0 1
q
0
q
1
q
0
q
1
q
1
q
2
q
2
q
1
q
3
q
3
(final state)
q
1
q
0

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 0s and 1s ending with
a substring 001 as shown below.

States
0 1

q
0
q
1,
0, R

q
0,
1, R
q
1
q
1,
0, R q
2,
1, R
q
2
q
1,
0, R q
3,
1, R
q
3
q
1,
0, L q
0,
1, R q
4,
, R
q
4
- - -
Therefore, the TM to recognize the given language is given by
Q = {q
0
, q
1
, q
2
, q
3
}
E = {0, 1}
I = {0, 1, }, o is defined above.
q
0
is the start state
is the blank character
F = {q
4
} is the final character
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 = {0
n
1
n
n > 1}
2. Obtain a Turing machine to accept the language
L = {w w is even and E = {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 = {0
n
1
n
n > 1}.
6. Obtain a TM to accept the language L = {0
n
1
n
2
n
n > 1}.

12.7 Answers
Self Assessment Questions
1. Hint: It is given that the language accepted by TM should have n
number of 0s followed by n number of 1s. 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 1s.
Theory of Computer Science Unit 12
Sikkim Manipal University Page No.: 228
Let q
0
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 q
1
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 1s.
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





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, 2
nd
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, 5
th
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, 2
nd
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).

___________________________

Das könnte Ihnen auch gefallen