Sie sind auf Seite 1von 227

Fundamentals of Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 1


Unit 1 Mathematical Preliminaries
for Computer Science
Structure
1.1 Introduction
Objectives
1.2 Sets and Cartesian product of sets
1.3 Functions and Relations
1.4 Equivalence Relations
1.5 Matrix Representations
1.6 Closure Relations
Self Assessment Questions
1.7 Summary
1.8 Terminal Questions
1.9 Answers

1.1 Introduction
The idea in set is fundamental in the study of mathematical structures.
Almost all mathematical objects can be defined in terms of sets and the
language of set theory is used in every mathematical topic. Also, the
concept of relation and function arises when we consider a pair of objects
and compare them. In this unit we study the different relations on a set and
the algebraic structures with single binary operation. The content of this unit
provides a mathematical tool for the later units.
Objectives:
At the end of the unit the student must be able to:
- Understand the fundamental idea of certain mathematical concepts.
- Learn the various operations on sets.
- Understand relations and types of relations
- Recognize the Matrix representations
- Know the closure relations and functions.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 2
1.2 Sets and Cartesian product of sets
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 are 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 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

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.
1.2.3 Example
Describe the set containing all the nonnegative integers less than or equal
to 4.
Let X denotes the set. Then X can be described in the following ways:
i) X = {x / x is a nonnegative integer less than or equal to 4}
and N = {x / x is a natural number} = {1, 2, 3, 4, } is an infinite set.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 3
ii) X = {0, 1, 2, 3, 4}.
iii)
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 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. That A is a proper subset of B if .
1.2.5 Note
The containment of sets has the following properties: Let X, Y and Z be
sets.

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

Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 4
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 is denoted
by and we call it the union of A and B.
iv) The set is denoted by and we call it the
intersection of A and B.
1.2.9 Properties
1.2.10 Definition
Let X and Y be two sets. The symmetrical difference of X and Y is defined
as It is denoted as . It is also called
Boolean sum of two sets.
1.2.11 Definition
If A and B are two sets, then the set is denoted by B A (or
B \ A) and it is called the complement A in B.





Fundamentals of 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}.

Here we may note that, in roster form, there is no necessity of writing
the same element two times. So we avoid writing the same element
second time in roster form.








Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 6
1.2.13 Definition
i) If S and T are two sets, then the set {(s, t / s S and t T} is called
the Cartesian product of S and T (here (a, b) = (s, t) a = s and
b = t). The Cartesian product of S and T is denoted by S T. Thus
S T ={(s, t) / s S and t T}.
Note that if S and T are two sets, then S T and T S may not be
equal.
ii) If 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
S
i
for 1 s i s n}.
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
,
1 s i s n.
iii) Let A
i
be a collection of sets one for each element i I, where I is
some set (I may be the set of all positive integers). We define

A collection {A
i
}
ie I
of sets is said to be mutually disjoint if A
i
A
j
= | for
all i I, j I such that i j.
1.2.14 Example
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 .
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 is an
empty set.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 7
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 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.
Observation: If the set A has n elements, then the number of elements in
P(A) is 2
n
.
Verification: 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
.

1.3 Functions and Relations
Relation describes connections between different elements of the same set,
whereas functions describe connections between two different sets.
Functions give a mathematical precise framework for the intuitive idea of
transformation.
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

Here t is called the image of s; and s is called the preimage of t.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 8
The set S is called the domain of f and T is called the codomain.
The set {f(s) / s S } is a subset of T and it is called the image of S under f
(or image of f). We denote the fact f is a function from S to T by f : S T.
f: S T is said to be
one-one function (or injective function):
onto function (or surjective function): there exists an element s e
S such that f(s) = t.
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.2 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 and 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.
1.3.3 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.4 Definition
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 9
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.5 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
i) transitive if (a, b) R, (b, c) R implies (a, c) R;
ii) reflexive if (a, a) R for all a S;
iii) anti-symmetric if (a, b) R and (b, a) R a = b;
iv) symmetric if (a, b) R implies (b, a) R;
1.3.6 Note
i) Let the number of elements of A and B be m and n respectively. Then
the number of elements of A B is mn.
ii) The number of elements of the power set of A B is 2
mn
. A B has
2
mn
distinct subsets.
iii) Every subset of A B is a relation from A to B. Therefore the number
of different relations A to B is 2
mn
.

1.4 Equivalence Relations
1.4.1 Definition
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 10
A relation is said to be an equivalence relation if it is reflexive, symmetric
and transitive.
If (a, b) is an element of the equivalence relation, then we write a ~ b and
we say that a and b are equivalent.
Let R be an equivalence relation on S and a is an element of S. Then the
set [a] = {s S / s ~ a} is called the equivalence class of a (or equivalence
class containing a).
1.4.2 Example
If A = {a, b, c} and R = {(a, a), (b, b), (c, c), (a, b), (b, a)}, then R is an
equivalence relation on A and [a] = {a, b}, [b] = {a, b}, and [c] = {c}.
1.4.3 Example
i) Let S be the set of all integers. Define for any a, b S, a ~ b a - b
is an even number. Then relation ~ is an equivalence relation.
ii) Let R, the set of all real numbers, x ~ y x y is an integer. Then ~
is an equivalence relation. The set of all equivalence classes is given
by
{[x] / x (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
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.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 11
1.4.6 Problem
The relation a b mod n defined above is an equivalence relation on Z.
Solution: Let a Z.
Reflexive: Since n divides a - a = 0, we have a a mod n.
Symmetric: Let a b mod n.
n divides a - b
n divides - (a - b)
n divides b - a
b a mod n.
Transitivity: Let a, b, c Z such that a b mod n, b c mod n.
n divides a - b, and n divides b - c
n divides ( a - b ) + ( b - c)
n divides a - c
a c mod n.
Hence the relation is an equivalence relation.
1.4.7 Example
Suppose n = 5. Then
[0] = {x / x 0 mod 5} = {x / 5 divides x 0 = x} = {, -10, -5, 0, 5, 10, }
[1] = {x / x 1 mod 5} = {x / 5 divides x - 1}= {, -9, -4, 1, 6, }
[2] = {x / x 2 mod 5} = {x / 5 divides x -2} = {, -8, -3, 2, 7, 12, }
[3] = {x / x 3 mod 5} = { x / 5 divides x -3} = {, -7, -2, 3, 8, 13, }
[4] = {x / x 4 mod 5} = { x / 5 divides x - 4} = {, -6, -1, 4, 9, 14, }
Also it is clear that [0] = [5] = [10] =
[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]}.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 12
1.4.8 Definition
A partition of a set S is a set of subsets {S
i
/ = S
i
S and i 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 Z
and b = a + 1} of subsets of R, forms a partition for R.
1.4.10 Lemma
If R is an equivalence relation on a set S and a, b S, then either [a] = [b]
or [a] [b] = .
Proof. If [a] [b] = , then it is clear.
Now suppose the intersection is non-empty.
Let x [a] [b]
x [a] and x [b]
x ~ a and x ~ b
a ~ x and x ~ b (since ~ is symmetric)
a ~ b (since ~ is transitive).
Now we show that [a] = [b]. For this, let y [a] y ~ a.
Since a ~ b, we get y ~ b (by transitive property) b ~ y y [b].
Hence [a] [b]. Similarly, we get that [b] [a]. Therefore [a] = [b].
From this lemma, we can conclude that any two equivalence classes are
either equal or disjoint.
1.4.11 Problem
Let A be a set and ~ an equivalence relation on A. Then the set of all
equivalence classes forms a partition for A.
Solution: The collection of all equivalence classes is {[a] / a A}.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 13

Observation: Let A be a set and {A
i
/ i I} be a collection of nonempty
subsets of A, which forms a partition for A. Then there exists an equivalence
relation ~ on A such that the equivalence classes are nothing but the sets of
the partition.

1.5 Matrix Representations
1.5.1 Definition
Let A = {a
1
, a
2
, , a
n
} and B = {b
1
, b
2
, , b
n
}.
If R is relation from A to B, then R can be represented by matrix M
R
=
(M
ij
)
mn
, defined
( )
i j
ij
m n
i j
1 if (a , a ) R
M
0 if (a , a ) R

=

e


Where M
ij
is the element in the i
th
row and j
th
column. M
R
can be first obtained
by first constituting a table, whose columns are preceded by a column
consisting of successive elements of A and where rows are headed by row
consisting of successive elements of B. If (a
i
, b
j
) R, then we enter 1 in the
i
th
row and j
th
column.
1.5.2 Example
Let A = {1, 2, 3} and = {(x, y) | x < y}. Write M
R
.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 14
Solution: R = {(1, 2), (1, 3), (2, 3)}. Since (1, 2) e R, we have m
12
= 1;
(1, 3)e R, we have m
12
= 1; also m
23
= 1.
Therefore
R
0 1 1
M 0 0 1
0 0 0
(
(
=
(
(


1.5.3 Example
Let A = {1, 2, 3, 4}. Define a R b a < b.
Then
R
0 1 1 1
0 0 1 1
M
0 0 0 1
0 0 0 0
(
(
(
=
(
(


1.5.4 Example
Write the relation for the relation matrix
R
1 0 0
M 0 0 1
1 1 0
(
(
=
(
(


Solution: Since M is a 3 3 matrix, take A = {a
1
, a
2
, a
3
} and B = {b
1
, b
2
, b
3
}
Then R = {(a
1
, b
1
), (a
2
, b
2
), (a
2
, b
3
), (a
3
, b
1
)}
1.5.5 Definition
A relation R is transitive if and only if M
R
= [m
ij
] has the property:
m
ij
= 1 and m
ik
= 1 m
ik
= 1
1.5.6 Example
Define a relation R represented by a matrix
(
(
(

=
1 1 0
1 1 0
0 0 1
M
R

Here, m
22
= 1, m
23
= 1 m
23
= 1
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 15
m
23
= 1, m
32
= 1 m
22
= 1
m
33
= 1, m
32
= 1 m
32
=1
Therefore the relation R is transitive.

1.6 Closure Relations
1.6.1 Definition
Let R be a relation on a set A. R may or may not have some property P,
such as reflexivity, symmetry or transitivity. If there is a relation S with
property P containing R such that S is a subset of every relation with P
containing R, then S is called the closure of R with respect to P.
1.6.2 Definition
Let R be a relation on a set S. The reflexive closure of R is the smallest
reflexive relation R
1
which contains R. It is also denoted by R
(r)
.
1.6.3 Note

1.6.4 Definition
The symmetric closure of R is the smallest symmetric relation containing R.
That is R R
1
is symmetric closure R, where R
1
is the inverse of the
relation R. It is denoted by R
(s)
.
1.6.5 Definition
Transitive closure of a relation R is the smallest transitive relation containing
R.
1.6.6 Example
Consider the set S = {1, 2, 3, 4}
i) The relation R = { (1,2), (2, 1), (1, 1), (2, 2)} is not reflexive, since
(3, ,3) e S.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 16
Consider = {(1, 1,), (2, 2), (3, 3), (4, 4)}. Now the reflexive closure
R
1
= = {(1, 2), (2, 1), (1, 1), (2, 2), (3, 3), (4, 4)}.
Observe that R
1
(the reflexive closure of R, sometimes we denote as
R
(r)
) is obtained by supplementing with exactly essential (no more, no
less) in order to get a reflexive relation containing R.
ii) Consider the relation K = { (1,2), (4, 3), (2, 2), (2, 1), (3, 1)}, which is
not symmetric on S. Now
K
1
= {(2, 1), (3, 4), (2, 2) (1, 2), (1, 3)}.
The symmetric closure K
(s)
of K is given by
K
(s)
= K K
1
= { (1, 2), (2, 1), (4, 3), (3, 4), (3, 1), (1, 3)}.
1.6.7 Note
Given a relation R on a set A. To make a relation R transitive, add all pairs
of R
2
, all pairs of R
3
, ,all pairs of R
m
( assume that | A| = m ), unless these
pairs are already in R.
Then the transitive closure of R, denoted by R

or R
(T)

R
(T)
= R R
2
R
m
.
1.6.8 Properties of Transitive closure
i) R
(T)
is transitive
ii) R R
(T)

iii) If S is any other transitive relation that contains R, then R
(T)
S.
1.6.9 Representation of Closure Relations
Let M be the relation matrix of the relation R. Then
i) the symmetric closure of R, denoted by M
S
, defined as M
S
= M v M'
where M' is the transpose of M.
ii) The reflexive closure of R, denoted by M
R
, defined as M
R
= M v I
n

where n is the cardinality of the set of for which the relation defined
and I
n
is the identity matrix of size n.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 17
iii) The transitive closure of R, denoted by M
T
or
R
M

defined as M
T
= M
v M
2
v M
3
v .v M
n
.
1.6.10 Example
Take A = {1, 2, 3} and R = {(1, 2), (2, 3), (3, 1)}. Find the reflexive,
symmetric and transitive closure of R, using composition of matrix relation
of R.
Solution: Let M be the matrix relation R then
(
(
(

=
0 0 1
1 0 0
0 1 0
M
i) The Reflexive closure of R, M
R
= M v I
3

(
(
(

=
(
(
(

v
(
(
(

=
1 0 1
1 1 0
0 1 1
1 0 0
0 1 0
0 0 1
0 0 1
1 0 0
0 1 0

One can write the reflexive closure R
(r)
, using the above matrix as
R
(r)
= {(1, 1), (1, 2), (2, 2), (2, 3), (3, 1), (3, 3)}.
ii) The symmetric closure of R, is M
S
= M v M' =
(
(
(

v
(
(
(

0 1 0
0 0 1
1 0 0
0 0 1
1 0 0
0 1 0
.
One can write the symmetric closure R
(S)
, using the above matrix as
R
(S)
= {(1, 2), (1, 3), (2,1), (2,3), (3,1), (3,2)}
iii) To find the transitive closure of R, we first find M
2
and M
3
(since the
cardinality of the set A = 3).

(
(
(

=
(
(
(

(
(
(

=
0 1 0
0 0 1
1 0 0
0 0 1
1 0 0
0 1 0
0 0 1
1 0 0
0 1 0
M
2
and
M
3
= M
2
. M =
(
(
(

=
(
(
(

(
(
(

1 0 0
0 1 0
0 0 1
0 0 1
1 0 0
0 1 0
0 1 0
0 0 1
1 0 0

Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 18
Therefore the transitive closure of R, M
T
= M v M
2
v M
3

=
(
(
(

=
(
(
(

v
(
(
(

v
(
(
(

1 1 1
1 1 1
1 1 1
1 0 0
0 1 0
0 0 1
0 1 0
0 0 1
1 0 0
0 0 1
1 0 0
0 1 0
.
One can write the transitive closure of R,
R
T
= { (1,10), (1,2), (1,3), (2,1), (2,1), (2,2), (2,3), (3,1), (3,2) (3,3)}.
1.6.11 Definition
Let R be a relation from A to B, S be a relation from B to C. Then the
relation SoR from A to C is defined by a (SoR) c a R b and b S c for
some b B for all a A and c C.
1.6.12 Example
Take A = {1, 2, 3, 4}
Define R = {(1, 1), (1, 2) (2, 3), (2, 4), (3, 4), (4,1), (4,2)} and
S = {(3, 1), (4, 4), (2, 3), (2, 4), (1, 1), (1, 4)}
Then, since (1,1) R, (1,1) S, we have (1,1) SoR.
Since (1,2) R, (2,3) S, we have (1, 3) SoR.
Since (2,3) R, (3,1) S, we have (2, 1) SoR.
Continuing this way we get
SoR = {(1, 1), (1, 4), (1, 3), (2, 1), (2, 4), (3, 4), (4, 1), (4, 4), (4, 3)}
Similarly,
RoR = {(1,1), (1,2), (1,3), (1,4), (2,4), (2,1), (2,2), (3,1), (3,2), (4,1), (4,2),
(4,3), (4,4)}
1.6.13 Note
a) If R
1
and R
2
are relations from A to B
,
R and R
4
are relations from B to C,
then

Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 19
b) If R is a relation from A to B, S is a relation form B to C, and T is a
relation from C to D, then (RoS)o T = Ro(SoT).
1.6.14 Problem
If R is a relation from A to B and S is a relation from B to C, then
(RoS)
1
= S
1
o R
1
Solution: Since R is a relation from A to B we have R
1
is a relation from B
to A. Similar way, S
1
is a relation from C to B.
Therefore S
1
oR
1
is a relation from C to B.

This is true for any x A and z C. Hence (RoS)
1
= S
1
o R
1
.
1.6.15 Problem
If R is a relation on a set A, then R is transitive .

Converse: Suppose R
2
R. Take (x, y) R, and (y, z) R.
Then (x, z) RoR = R
2
R. Thus R is transitive.



Self Assessment Questions
1. Let A = {a, b} and B = {x, y, z}. Then find .
2. Let S = {2, 5, 2 , 25, ,
2
5
} and T = {4, 25, 2 , 6,
2
3
}

Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 20

3. Find A
c
(with respect to the universal set of reals) in the following
cases


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 }.
7. If a set A has n elements, then the number of relations from A to A is
______
8. If A has m elements and B has n elements, then the number of
relations from A to B is ________
9. Let A = Z
+
, the set of positive integers. R be the relation defined by
aRb if and only if there exists a k e Z
+
so that a = b
k
. Then which of
the following belong to R?
(i) (4, 16), (ii) (1, 3), (iii) (4, 2), (iv) (2, 8), (v) (4, 4) (vi) (2, 16).
10. Given the list of relations among people. For each of the following
relations, state whether the relation is reflexive, symmetric,
antisymmetric, or transitive.
i) xRy stands for x is child of y
ii) xRy stands for x is a spouse of y
iii) xRy stands for x is a wife of y
iv) xRy stands for x is a superior of y
v) xRy stands for x and y have the same parents.
11. Give an example of a relation that is both symmetric and anti
symmetric.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 21
12. Which of the following relations defined in the set of real numbers are
equivalence relation ?
i) aRb if and only if a = b
ii) aRb if and only if a > b
13. Let Z be the set of integers. Define a relation R on Z as
R = {(x, y) x, y Z, (x-y) is a multiple of 3}.
Then
i) R is an ____________
ii) The Equivalence class containing 0 is _______
iii) The number of equivalence classes is _________
14. Let S be the set of non-zero integers and let R be the relation on A A
defined by (a, b) R (c, d) if and only if ad = bc.
Then
i) R is an _______
ii) The equivalence class containing (1, 2) is ________
15. Let A = {0, 1, 2, 3, 4}. The relation R = {(0, 0), (0, 4), (1, 1), (1, 3), (2,
2), (3, 1), (3, 3), (4, 0), (4, 4)} is an equivalence relation on A. Find the
distinct equivalence classes of R.
16. The set theoretic union of distinct equivalence classes of an
equivalence relation forms a __________ of the given set.
17. Determine whether or not each of the binary relations defined on the
given sets S
i
(i = 1, 2, 3) are 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)}
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}
18. Is R = {(1, 1), (1, 2), (3, 2), (3, 3), (2, 3), (2, 1)} an equivalence relation
on S = {1, 2, 3}?
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 22
19. For x, y e R \{0}, define x ~ y
y
x
e Q. Whether or not, ~ is an
equivalence relation.
20. Let A = {x Z
+
/ 1 s x s 10}. State whether or not each of the
following 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}};
iii) {1, 2, 3}, {5, 8, 9}, {4, 6, 7, 10}}.
21. If |S| = n, then how many relations are there from S to S.
22. Let R be the relation from the set A = {1, 3, 4} on itself. Define R by
R = {(1, 1), (1, 3), (3, 3), (4, 4)}.
23. The relations corresponding to the following relation matrices
(i)
(
(
(

0 1 1
0 1 0
0 0 1
, (ii)
(
(
(
(

0 1 0 1
1 1 0 0
0 1 1 0
1 0 0 1

on a set A = {1, 2, 3} are __________
24. Let A = {a, b, c, d} and R be a relation defined on A whose matrix
representation is M
R
=
(
(
(
(

1 0 0 0
0 1 1 0
0 1 1 0
0 0 0 1
. Verify whether or not the
relation on A is reflexive and symmetric.
25. Consider the relation R ={(0, 1), (1, 2), (2,3)}defined on A = {0, 1, 2, 3}.
Find the reflexive, symmetric, and transitive closure of R using
composition of relation.

1.7 Summary
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 23
In this unit we introduced the basic concept sets and the different properties
of sets. Some properties common to operations on sets and logical
statements were discussed. Cartesian product of sets was studied as
relations between two sets. We also discussed the equivalence relations
and closure relations with few illustrations. The reader can easily apply the
mathematical concepts introduced, in various situations.

1.8 Terminal Questions
1. If R is a relation on the set of integers Z defined by R = {(x, y) x, y Z,
(x-y) is divisible by 6}, then prove that R is an equivalence relation.
2. Represent each of the following relation on {1, 2, 3} with a matrix
(i) R = {(1, 1), (1, 2), (1, 3)}, S = {(1, 2), (2, 1), (2, 3), (3, 3)}, T = {(1, 3),
(3, 2), (3, 3)}.
3. Let A = {1, 2, 3, 4, 5, 6}. Define a relation R = {(x, y) x + y is a divisor
of 24}.
Find the relation matrix M of R. Compute M
2
and M and M
2
whether or not R
is transitive.
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 problems
illustrates that, in general, the sets A B and B A are different.
2. i) S T = { 2 , 25}, S T = {2, 5, 2 , 25, ,
2
5
, 4, 6,
2
3
},
T (S T) = {(4, 2 ), (4, 25), (25, 2 ), (25, 25), ( 2 , 2 ),
( 2 , 25), (6, 2 ), (6, 25), (
2
3
, 2 ), (
2
3
, 25)}.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 24
ii) Z S = { 2 , ,
2
5
, 0, 1, -1, 2, -2, . }, Z S = {2, 5, 25}
Z T = { 2 ,
2
3
, 0, 1, -1, 2, -2, .}, Z T = {4, 25, 6}.
3. i).
c
1
A = [-2, 1];

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

8. 2
m n

9. i) No, ii) No, iii) Yes, iv) No, v) Yes, vi) No.
10. i) anti symmetric, ii) symmetric, iii) anti symmetric, iv) anti symmetric
and transitive, v) reflexive, symmetric and transitive.
11. The identity relation is both symmetric and anti-symmetric. Also, define
the relation in which xRy is true if and only if x = y is both symmetric
and anti-symmetric. Take A = {a, b, c}, define R = {(a, a), (b, b)}.
12. i) Equivalence relation, ii) Not an equivalence relation..
13. i) An equivalence relation
ii) Equivalence class containing 0,
[0] = {0, 3, 6, 9, 12, }
iii) The set of equivalence classes are {[0], [1], [2]}.
14. i) Equivalence relation
ii) [(1, 2)] = {(1, 2), (-1, -2), (2, 4), (-2, -4), (3, 6), (-3, -6), }
15. [0] = [4] = {0, 4}, [1] = [3] ={1, 3}, [2] = {2}.
16. Partition.
17. i) Not reflexive as (2, 2) R
1
.
Not symmetric as (3, 4) R
1
but (4, 3) R
1

Not anti symmetric as (1, 2), (2,1) R
1
, but 1 2
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 25
Not transitive as (2, 1), (1, 2) e R
1
but (2, 2) e R
1
.
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 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:
18. No: Since R is not reflexive as (2, 2) e R.
19. Yes
20. (i). No; (ii). No; (iii).Yes.
21. 2
n
.
22. M
R
=
(
(
(

1 0 0
0 1 0
0 1 1

23. (i) {(1, 1), (2, 2), (3, 1), (3, 2)}
(ii) {(1, 1), (1, 4), (2, 2), (2, 3), (3, 3), (3, 4), (4, 1), (4, 3)}.
24. Reflexive and Symmetric.
25. (i) R
(r)
= {(0, 0), (1, 1), (2, 2), (3, 3), (0, 1), (1, 2), (2, 3)},
R
(s)
= {(0, 1), (1, 0), (1, 2), (2, 1), (2, 3), (3, 2)},
R
(T)
= {(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)}.
Fundamentals of Theory of Computer Science Unit 1
Sikkim Manipal University Page No.: 26






Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 26
Unit 2 Recursive Functions, Strings
and Growth Functions
Structure
2.1 Introduction
Objectives
2.2 Recursive Functions
2.3 Integer Functions
2.4 Sequences and Strings
2.5 Growth Functions
Self Assessment Questions
2.6 Summary
2.7 Terminal Questions
2.8 Answers

2.1 Introduction
Sometimes it is difficult to define an object explicitly. However, it may be
easy to define this object in terms of itself. This process is called recursion.
Recursion relates to several related concepts in computer science.
In computer programming, recursion plays an important role. It is an
important facility in many programming languages.
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.
Objectives:
At the end of the unit the student must be able to:
- Understand the concept of Recursive Functions
- Recognize sequences and Strings
- Know the idea of Growth Functions
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 27
2.2 Recursive Functions
2.2.1 Definition
A function , 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.
2.2.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
.
2.2.3 Theorem (Recursion Theorem)
Let F be a given function from a set S into S. Let s
0
be fixed element of S.
Then there exists a unique function 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 .
(Here the condition (i) is called initial condition and (ii) is called the
recurrence relation).
2.2.4 Example
Define n! recursively and compute 5! recursively.

Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 28
Solution: We have . 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.
2.2.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).
2.2.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.
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 29

2.2.7 Example
Define
f(x) =

odd is x when
2
1 x
even is x when
2
x
.
Solution: Define 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)]
= 4 3 + 2 1 + [0 f(0)]
= 2.
2.2.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
Then g(0) = s
0
. And g(n +1) f(g(n)). Thus g is a well-defined function.
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 30
The following is an example of a recursive function that does not define a
function.
2.2.9 Example
Consider a recursion function g: Z
+
(the set of positive integers) (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.
2.2.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
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:

=
>
=
otherwise )) n , m mod( , n ( . d . c . g
0 n if m
m n if ) m , n ( . d . c . g
) n , m ( . d . c . g
where mod (m, n) is the remainder obtained when m is divided by n.
Observations:
a) The first part interchanges the order of m and n if n > m.
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 31
b) Second part is the initial condition.
c) Third part is the recursive part mod (m, n) will become 0 in a finite
number of steps.
2.2.11 Example
Calculate the g.c.d. (20, 6).
Solution: 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.
2.2.12 Example
Calculate the g.c.d. (81, 36).
Solution: g.c.d. (81, 36) = g.c.d. (36, 9)
= g.c.d. (9, 0)
= 9.
2.2.13 Example
Calculate the g.c.d. (22, 8).
Solution: 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.
Note: The recursive definition can be extended to functions of more than
one variable.
Consider the following example.
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 32
2.2.14 Example
Define f(x, y) = x + y recursively.

Solution: 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.
2.2.15 Example
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).

2.3 Integer Functions
2.3.1 Definition
For any real number x, we define the floor of x as

2.3.2 Example
Take x = 2.52, then

Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 33
2.3.3 Definition

2.3.4 Example
Take x = 3.732, then

Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 34






















Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 35
2.3.5 Geometric Interpretation
Floor and Ceiling functions may be understood from their graphical
(or geometrical) representation. Consider the line f(x) = x, the diagonal on I, III
coordinates, take x=e=2.71828. we describe floor and ceiling of e as follows:


2.3.6 Properties
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 36
2.3.7 Example
The above rules can be illustrated, by taking x = 4.5.


2.3.8 Problem
Prove that for any integer n and real x.



Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 37
2.3.9 Remark
In general, taking out of a constant factor is not true.
2.3.10 Problem
Find a necessary and sufficient condition that when n is a
positive integer.

2.4 Sequences and Strings
Let us see the simple example, to understand this.
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 38
Komal 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, she
looks at various plans and chooses the plan 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 Komal 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.
Define a function (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
.


Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 39
2.4.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.
2.4.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 n
2
} n { or simply by {n
2
}.
2.4.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 the sum as

=
n
m j
j
a or

=
n
m k
k
a .
2.4.4 Example
(i)

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

=
+
3
1 i
1 i
i
= + 2/3 + = 23/12.

Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 40
2.4.5 Example
Consider the sums

=
+
3
1 i
) 1 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
2.4.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.
2.4.7 Example
Consider the sum

=
+
3
1 i
) 1 i ( and change the index variable to j = i + 1.
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 .
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 41
2.4.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 ( .
2.4.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 .
2.4.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
.
2.4.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
.
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 42
Example: Let A = {0, 1}.

2.5 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.
2.5.1 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)).
2.5.2 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.
2.5.3 Example
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 43
Use O-notation to express , 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
).
2.5.4 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
).
2.5.5 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
.

2.5.6 Example
Use the definition of order to show that x
2
+ 2x + 1 is O(x
2
).
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) = x
2
+ 2x

+ 1 and g(x) = x
2
.
2.5.7 Example
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 44
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.

2.5.8 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 by 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 the 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.
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 45
2.5.9 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
2.5.10 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
).
2.5.11 Note
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.
2.5.12 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.
Note that the function n! grows rapidly.
20 ! = 2, 432, 902, 008, 176, 640, 000.
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 46
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.
2.5.13 Theorem
Suppose that f
1
(x) is O(g
1
(x)) and f
2
(x) is O(g
2
(x)).

2.5.14 Theorem
Suppose that f
1
(x) and f
2
(x) are both O(g(x)). Then (f
1
+ f
2
)(x) is O(g(x)).
2.5.15 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)).
2.5.16 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
have log n! is O(n log n). Also 3n is O(n).
Using these two estimates, and the theorem, we have estimation that 3n
log (n!) is
O(n
2
log n).
Next the product (n
2
+ 3) log n will be estimated.

Therefore (n
2
+3)log n is O(n
2
log n). Using theorem, to combine the two big O-
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 47
estimates for the products shows that f(n) = 3nlog (n!) + (n
2
+ 3)log n is O(n
2
log n).
2.5.17 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
).
2.5.18 Big-Omega and Big-Theta Notations
If f(x) is O(g(x)), all that one can conclude is that except for constants and a
finite number of exceptions, f is bounded above by g, so g grows at least as
fast as f. For example, if f(x) = x and g(x) = 2x, then f(x) is O(g(x)), but g
grows considerable faster than f. The statement f(x) is g(x) says nothing
about lower bund 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.
2.5.19 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 , if there are positive
constants C and k such that whenever x > k which is
read as f(x) is big-Omega g(x).
Let f and g be functions from the set of integers or the set of real numbers to
the set of real numbers. We say that f(x) is g(x) if f(x) is O(g(x)) and f(x) is
. When f(x) is g(x), we say that f is big-Theta of g(x) and we say
f(x) is of order g(x).
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 48

Therefore, f(x) is O(g(x)), then f(x) is an upper bound for f(x) and whenever
f(x) is g(x), g(x) is a lower bound for f(x). The big-O notation compares the
rate of growth of functions rather than their values, so when f(x) is g(x),
f(x) and g(x) have the same rates of growth, but can be very different in their
values.
2.5.20 Example
Let f(x) = x and g(x) = 1, 000, 000x, then f(x) C g(x) for C = 1 and k = 1, so
that f(x) is O(g(x)). Also Cf(x) > g(x) for C = 1, 000, 000 and k = 1, so f(x) is
O(g(x)). Therefore f(x) is g(x).
Self Assessment Questions

2.6 Summary
In this unit we discussed the recursive functions which are useful to write
efficient algorithms. There are special integer functions defined with enough
illustrations. The concepts, sequences, strings and growth are introduced.
These are useful in approximating the time complexity and construction of
DFA, NDFA.

2.7 Terminal Questions
1. Explain the geometrical meaning of floor and ceiling functions.
2. Practice to find order of different algebraic polynomials.
Fundamentals of Theory of Computer Science Unit 2
Sikkim Manipal University Page No.: 49
3. Practice problems on Big-O, Big-u notations.
2.8 Answers
Self Assessment Questions
1. 2
2. 2
3. 0
4. 2
5. 1
6. The concatenation of 1110 and 0111 is: 11100111.
7. The concatenation of aaabbb and bbabab is: aaabbbbbabab.




Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 48
Unit 3 Methods of Proof
Structure
3.1 Introduction
Objectives
3.2 Proof Techniques
Self Assessment Questions
3.3 Summary
3.4 Terminal Questions
3.5 Answers

3.1 Introduction
In this unit we discuss various methods of proofs and few examples. The
techniques will give an idea to analyze and solve the problems.

Objectives:
At the end of the unit the student must be able to:
- Understand the proof techniques
- Apply in various problems
- Analyse various proof in automata

3.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.
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 49
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 which are not direct are called indirect. Two main
types of indirect proof, uses 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 from the inequalities: a c and b d
imply that
a + b c + d for all real numbers a, b, c, d.
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 Digitalization Principle
5. Proof by Contradiction
6. Exhaustive Proof and Proof by cases

Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 50
3.2.1 Proof by Induction
Let A be the set of all natural numbers such that

Then A = N.
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 for n + 1. By the induction principle, A is then equal to N, that is, P
holds for every natural number.
3.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
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 51
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=
(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.
3.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
= = .
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 52
Right side
( )( )
1
6
3 . 2 . 1
6
1 1 . 2 1 1 1
= =
+ +
= .
Hence the result is true for 1 n= .
(ii) Induction Hypothesis: Assume that the result to be true for m n=
Then
( ) ( )
6
1 m 2 1 m m
m ... 3 2 1
2 2 2 2
+ +
= + + + + .
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.
3.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:
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 53
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
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.
3.2.5 Example
(Refer unit 4 for definition of binary tree) A binary tree is a tree in which no
parent can have more than two children. 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
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 54
Therefore our claim is true for n + 1. Since n is
arbitrary, we can conclude that the statement is true for all n.
3.2.6 Example
(Refer unit 4 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.
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.
|E(T
1
)| = |V(T
1
)| - 1 and |E(T
2
)| = |V(T
2
)| - 1.

3.2.12 Problem
Prove by mathematical induction that
n
2 n > for all positive integer n.
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 55
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
m
2 m >
Now
m 1 m
2 2 . 2 2m
+
= > . We know that 2m m m m 1 = + > + for all
m N e .
m 1
Therefore 2 m 1
+
> + . Hence ( ) P m 1 + is true.
Therefore by induction ( ) P n is true for all n.
3.2.13 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.
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 = + +
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 56
Since each term on the R.H.S is divisible by 6 their sum is also divisible by 6.
Hence ( ) P m 1 + is divisible by 6. Therefore by induction ( ) P n is divisible by
6 for all n N e
3.2.7 The Pigeonhole Principle
If A and B are finite sets and , 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.
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.
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 57
3.2.8 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.
3.2.9 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:

The sequence of boxes along the diagonal is
x x x
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 58
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 1 n=
(ii) Assuming the theorem to be true for m n= , prove that it is also true for
1 m n + =
Note that here m is a particular value of n. From (i) the theorem is true for
1 n= and from (ii) it is true for 2 1 1 n = + = ; since it is true for 2 n= it follows from
(iii) that it is also true for 3 1 2 n = + = and so on. Hence theorem is true for
all positive integral values of n.
3.2.10 Proof by Contradiction
Proof by contradiction is sometimes a 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.
3.2.11 Example
Prove by contradiction, that is not a rational number.
Solution: A rational number is of the form p/q where , and p, q are not
having any common factors.
Assume that is a rational number. So it can be written as
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 59

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 is not a rational number.
3.2.12 Example
Give a proof by contradiction of if 3n + 2 is odd, then n is odd.
Solution: Let p: 3n+2 is odd
q: n is odd.
To construct a proof by contradiction, assume that both p and ~ q are true.
That is, assume that 3n + 2 is odd and that n is not odd.
Since n is not odd, it is even.
Now we can show that if n is even, then 3n + 2 is even.
(Verification: n is even n = 2k for some integer k. Substituting 2k for n,
we get 3n + 2 = 3(2k) + 2 = 6k + 2 = 2(3k + 1) 3n + 2 is even).
Now the statement 3n + 2 is even is ~ p. Now since p and ~ p are true, we
have a contradiction. This completes the proof by contradiction, proving that
if 3n + 2 is odd, then n is odd.
3.2.13 Exhaustive Proof and Proof by Cases
Some theorems can be proved by examining a relatively small number of
examples. Such proofs are called exhaustive proofs, since these proofs
proceed by exhausting all possibilities. An exhaustive proof is a special type
of proof by cases where each case involves checking a single example.
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 60
3.2.14 Example
Prove that (n + 1)
3
3
n
if n is a positive integer with n 4.
Solution: We use a proof by exhaustion. We only need verify the inequality
(n + 1)
3
3
n
when n = 1, 2, 3, 4.
For n = 1, we have (n + 1)
3
= 2
3
= 8 and 3
n
= 3
1
= 3;
for n = 2, we have (n + 1)
3
= 3
3
= 27 and 3
n
= 3
2
= 9;
for n = 3, we have (n + 1)
3
= 4
3
= 64 and 3
n
= 3
3
= 27; and
for n = 4, we have (n + 1)
3
= 5
3
= 125 and 3
n
= 3
4
= 81;
Therefore, (n + 1)
3
3
n
for all positive integers n 4.
3.2.15 Example
(Proof by Cases) Prove that if n is an integer, then .
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 is not a rational number (by the method of contradiction).
3. Prove that the product of two odd integers is an odd integer.

Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 61
3.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.
3.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 the same
color.
Induction Hypothesis: In any group of upto 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.
(Hint: The induction proof fails for n=2).

3.5 Answers
Self Assessment Questions
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 = .
(ii) Assume the result to be true for m n =
Fundamentals of Theory of Computer Science Unit 3
Sikkim Manipal University Page No.: 62
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.
2. Proof by Contradiction method .
3. Take two odd integers m and n. Then there exist two integers r and t so
that m = 2r + 1 and n = 2t + 1.



Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 63
Unit 4 Graphs and Binary Trees
Structure
4.1 Introduction
Objectives
4.2 Definitions and Examples
4.3 Isomorphism
4.4 Trees
4.5 Rooted Trees
Self Assessment Questions
4.6 Summary
4.7 Terminal Questions
4.8 Answers

4.1 Introduction
Graph theory was invented in 1736 with Eulers paper in which he solved
the Kongsberg Bridges problem. In 1847, Kirchhoff (1824 - 87) developed
the theory of trees to applications in electrical networks. 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. The
development of high-speed computers is also one of the reasons for the
recent growth of interest in Graph Theory.
Objectives:
At the end of the unit the student must be able to
- appreciate the relevance of Graph Theory in real life situation
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 64
- observe the difference between different concepts defined through the
examples presented
- understand some techniques used in proving simple theorems
- know the isomorphism between two graphs.
- Understand the trees and rooted trees.

4.2 Definitions and Examples
4.2.1 Definition
(i) A linear graph (or simply 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
.
(ii) An edge associated with a vertex pair {v
i
, v
i
} is called a loop (or)
selfloop.
(iii) If there are more than one edge associated with a given pair of
vertices, then these edges are called parallel edges (or) multiple
edges.
4.2.2 Example
Consider the graph given here.







e
6
e
7
e
1
e
2
e
4
v
3 v
4
v
5
e
5
v
2
v
1
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 65
This is a graph with five vertices and six edges. Here G = (V, E) where
V = {v
1
, v
2
, v
3
, v
4
, v
5
} and E = {e
1
, e
2
, 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
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.
4.2.3 Definition
A graph that has neither self-loops nor parallel edges is called a simple
graph. Graph containing either parallel edges or loops is also referred as
general graph. A graph G with a finite number of vertices and a finite
number of edges is called a finite graph. A graph G that is not a finite
graph is said to be an infinite graph.
Observation: The two graphs given below are one and the same.






4.2.4 Example
Consider the following three graphs







4
3 2
1
3
2
4
1
u
1
u
1
u
2
u
3
e
1
e
2
e
4
e
3
e
5
u
4
u
2
u
3
e
3
e
1
e
5
e
6
e
7
e
2
e
7
u
4
e
4
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 66






It can be observed that the number of vertices, and the number of edges
are finite. Hence these three graphs are finite graphs.
4.2.5 Example
Consider the two graphs given here. It can be understood that the number
of vertices of these two graphs is not finite. So we conclude that these two
figures represent infinite graphs.







4.2.6 Example
The diagrams of fig. (a) and (b) illustrate two non directed graphs. The
graph G, shown in Fig. (a) is not simple since there is a loop incident on
vertex c.
The graph G
1
shown in Fig. (b) is simple since there are no self loops and
parallel edges.
The graph G
11
in fig. (c) represents a multi-graph since there are three
edges between the vertices b and c.
u
1
e
4
e
6
e
6
e
5
e
2
e
3
u
2

u
3
u
5
e
1
u
4
Infinite
Graphs
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 67
V(G) = {a, b, c, d) and E(G) = {{a, b},{a, c},{b, c},{c, c},{a, d},{c, d}}.
Therefore G is of order 4 and size 6.
Similarly, the graph G
1
has order 4 and size 5, and the multi-graph G
11
has
order 4 and size 7.
Observation: The non-directed graphs may be viewed as symmetric
directed graphs, in which for every edge (u, v) between two vertices in
direction there is also an edge (v, u) between the same vertices in the other
direction.

4.2.7 Definition
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.
4.2.8 Example
Consider the graph given here. Here the edges e
2
, e
6
, e
7
are incident with
the vertex u
4
.






e
6
e
7
e
1
e
2
e
3
e
4
u
3 u
4
u
5
e
5
u
2
u
1
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 68
4.2.9 Definition
(i) Two non-parallel edges are said to be adjacent if they are incident on
a common vertex.
(ii) Two vertices are said to be adjacent if they are the end vertices of the
same edge.
4.2.10 Example
Consider the graph given in example 4.2.8. Here the vertices u
4
, u
5
are
adjacent. The vertices u
1
and u
4
are not adjacent. The edges e
2
and e
3
are
adjacent.
4.2.11 Definition
The number of edges incident on a vertex v is called the degree (or
valency) 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.
4.2.12 Example
Consider the graph given here.
Here d(u
1
) = 2; d(u
2
) = 1; d(u
3
) = 3; d(u
4
) = 2; d(u
5
) = 2; d(u
6
) = 2; d(u
7
) = 1;
d(u
8
) = 3; d(u
9
) = 2; d(u
10
) = 2.










u
10
u
1
u
2
u
3
u
4
u
5
u
6
u
7
u
8
u
9
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 69

4.2.13 Example
Consider the graph.







Here d(u
1
) = 2, d(u
3
) = d(u
4
) = 3; d(u
2
) = 3; d(u
5
) = 1
So, d(u
1
) + d(u
2
) + d(u
3
) + d(u
4
) + d(u
5
) = 2 + 3 + 3 + 3 + 1 = 12 = 2(6) =
2e, where e denotes the number of edges. Hence we can observe that
d(u
1
) + d(u
2
) + d(u
3
) + d(u
4
) + d(u
5
) = 2e
(that is, the sum of the degrees of all vertices is equal to twice the number of
edges).
4.2.14 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

eV x
) x ( d =

e } v , u { \ V x
) x ( d + d(u) + d(v) = 0 + 1 + 1 = 2 = 2 1
= 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.
u
1
e
5
e
4
u
3
e
6
u
5
u
2
e
2
u
4
e
7
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 70
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.
4.2.15 Theorem
The number of vertices of odd degrees is always even.
Proof: 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
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 71

=
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

isodd
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.
4.2.16 Example
Show that the number of people who dance (at a dance where the dancing
is done in couples) an odd number of times is even.
Solution: Suppose the people are vertices. If two people dance together,
then we can consider it as an edge. Then the number of times a person v
danced is o(v). By Theorem 9.5.9, the number of vertices of odd degree is
even. Therefore the number of people who dance odd number of times is
even.
4.2.17 Definition
A vertex having no incident edge is called an isolated vertex. In other
words, a vertex v is said to be an isolated vertex if the degree of v is equal
to zero.
4.2.18 Example
Consider the graph given here. The vertices v
4
and v
7
are isolated vertices.


Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 72







4.2.19 Definition
A vertex of degree one is called a pendent vertex or an end vertex. Two
adjacent edges are said to be in series if their common vertex is of degree
two.
4.2.20 Definition
In a non-directed graph G a sequence P of zero more edges of the form {v
0
,
v
1
}, {v
1
, v
2
},, {v
n-1
, v
n
}, (in this repetition of vertex is allowed) is called a
path from v
0
to v
n
. The vertex v
0
is called the initial vertex and v
n
is the
terminal vertex, and they both are called endpoints of path P.
We denote this path P as a v
0
v
n
path. If v
0
= v
n
then it is called a closed
path, and if v
0
= v
n
then it is called an open path.
4.2.21 Definition
A path P may have no edges at all, in which case, the length of P is zero, P
is called a trivial path, and V(P) = {v
0
}.
A path P is simple if all edges and vertices on the path are distinct except
possibly the endpoints.
Two paths in a graph are said to be edge-disjoint if they share no common
edges; they are vertex-disjoint if they share no common vertices.

v
6
v
2
v
7
v
1
v
5
v
3
v
4
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 73
4.2.22 Note
(i) An open simple path of length n has n + 1 distinct vertices and n distinct
edges, while a closed simple path of length n has n distinct vertices and n
distinct edges. The trivial path is taken to be a simple closed path of length
zero.
4.2.23 Definition
A path of length > 1 with no repeated edges and whose endpoints are equal
is called a circuit. A circuit may have repeated vertices other than the
endpoints; a cycle is a circuit with no other repeated vertices except its
endpoints.
Observation:
(i) A cycle is a simple circuit, and, in particular, a loop is a cycle of length
1.
(ii) In a graph, a cycle that is not a loop must have length at least 3, but
there may be cycles of length 2 in a multi-graph.
4.2.24 Example
Consider the graphs in example 4.2.6.
(i) The path {c, c} is a cycle of length 1; the sequence of edges {a, b}, {b,
c}, {c, a} and {a, d}, {d, c}, {c, a} form cycles of length 3.
(ii) The path {a, b}, {b, c}, {c, d}, {d, a} is a cycle of length 4.
(iii) The sequence {a, b}, {b, c}, {c, c}, {c, a} is a circuit of length 4; it is not
a cycle because the sequence of vertices a-b-c-c-a includes more than
one repeated vertex. Similarly the sequence of edges {a, b},{b, c},{c,
a},{a, d},{d, c},{c, a} forms a closed path of length 6, but this path is not
a circuit because the edge {c, a} is repeated twice.
Observation: A simple path is certainly a path and the converse statement
need not be true.

Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 74
4.3 Isomorphism
4.3.1 Definition
Two graphs G and G
1
are said to be isomorphic to each other if there is a
one-to-one correspondence between their vertices and a one-to-one
correspondence between their edges such that the incident relation ship
must be preserved.
That is, two graphs G = (V, E) & G
1
= (V
1
, E
1
) are said to be isomorphic if
there exist one- one and onto functions f : V V
1
and g :E E
1
such that
g(v
i
v
j
) = f(v
i
)f(v
j
) for any edge v
i
v
j
in G.
4.3.2 Example
(i) Consider the two graphs given in Figures A and B. Observe that these
are isomorphic. The correspondence between these two graphs is as
follows. f(a
i
) = v
i
for 1 s i s 5 and g(i) = e
i
for 1 s i s 6. Except the labeling of
their vertices and edges of the isomorphic graphs, they are same, perhaps
may be drawn differently.








Observations: If there is an isomorphism between two graphs G and H,
then G and H must have:
i) The same number of vertices,
ii) The same number of edges,
iii) An equal number of vertices of a given degree, and
iv) The incident relationship must be preserved.
a
5
a
4
a
3
a
2
a
1
6

5
3
2
4
1
Fig A
v
3
v
1
v
2
v
5
v
4
e
5

e
1
e
3
e
6
e
2
e
4
Fig B
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 75
4.3.3 Example
For each pairs of graphs shown below, either label the graphs so as to
exhibit an isomorphism or explain why the graphs are not isomorphic.
(i).



(ii).



(iii).




(iv).



Solution:
(i) The graphs are isomorphic, by the labeling shown below.

























v
1 v
2
v
3 v
4




v
2
v
1
v
4
v
3
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 76
(ii) The graphs are not isomorphic, since second graph has a vertex of
degree 1 and the first graph does not have.
(iii) The graphs are not isomorphic




(iv) the graphs are not isomorphic, since the two graphs differ by number
of edges.
4.3.4 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.
4.3.5 Example
The graphs H and K are subgraphs of graph G.









Observations: Here we can observe the following facts:
i) Every graph is a sub graph of itself;
ii) A subgraph of a subgraph of G is a subgraph of G;
iii) A single vertex in a graph G is a subgraph of G; and
iv) A single edge in G together with its end vertices is a subgroup of G.
1
2
3
6
i
b
d
j

Graph K
3

Graph H
4
1
b
d
c
a
e
2
g
4
1
2
3
6
a
5
b
c
d
e
f
h
j
i

Graph G
v
1
v
1 v
2
v
3 v
4





v
2

v
4
v
3
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 77
4.4 Trees
Trees are extensively used as models in areas like computer science,
chemistry, geology, electrical networks and botany etc. Trees are also
useful in design of wide range of algorithms.
The concept of a tree plays a vital role in the theory of graphs. First we
introduce the definition of tree, study some of its properties and its
applications. We shall also provide equivalent conditions for a tree.
4.4.1 Definition
A connected graph without circuits is called a tree.
4.4.2 Example
Trees with one, two, three and four vertices are given in the fig.



4.4.3 Example
Consider the two trees G
1
= (V, E
1
) and G
2
= (V, E
2
) where
V = {a, b, c, d, e, f, g, h, i, j}
E
1
= {{a, c}, {b, c}, {c, d}, {c, e}, {e, g}, {f, g}, {g, i}, {h, i}, {i, j}}
E
2
= {(c, a), (c, b), (c, d), (c, f), (f, e), (f, i), (g, d), (h, e), (j, g)}

Neither of these two trees is a directed tree.
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 78
If vertex c is designated as the root of each tree, vertex j is a level 4 in G
1

and at level 3 in G
2
.
4.4.4 Example
A directed tree T is shown in the following fig. Here T = (V, E) where V = {a,
b, c, d, e, f, g, h} and E = {(a, b), (a, c), (a, d), (b, e), (d, f), (e, g), (e, h)}.
The root of T is the vertex a and the vertices at level 2 are e and f.

4.4.5 Note
Directed trees are conventionally drawn with the root at the top and all
edges going from the top of the page toward the bottom so that the direction
of edges is sometimes not explicitly shown.
4.4.6 Note
i) Since a tree is a graph, we have that a tree contains at least one vertex.
ii) A tree without any edge is referred to as a null tree.
iii) Since we are considering only finite graphs, we have that the trees
considered are also finite.
iv) A tree is always a simple graph.
v) A vertex of degree of 1 is called a pendent vertex.
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 79
4.4.7 Note
Let G = (V, E) be a disconnected graph. We define a relation ~ on the set
of vertices as follows: v ~ u there is a walk from v to u.
Then this relation ~ is an equivalence relation.
Let {V
i
}
ieA
be the collection of all equivalence classes. Now V =

A e i
i
V .
Write E
i
= {e e E / an end point of e is in V
i
} for each i.
Then (V
i
, E
i
) is a connected subgraph of G for every ieA.
This connected subgraph (V
i
, E
i
) of G is called a connected component (or
component) of G for every i e A.
The collection {(V
i
, E
i
)}
ieA
of subgraphs of G is the collection of all
connected components of G.
Observations:
i) If G is a connected graph, then G is the only connected component
of G.
ii) A disconnected graph G consists of two or more connected components.
iii) Connected component of a graph G is a maximal connected sub graph
of G.
iv) A graph is connected if it has exactly one component.
v) Consider the graph given in Fig.



This graph is a disconnected graph with two components.
4.4.8 Problem
T is a tree there is one and only one path between every pair of vertices.
Solution:
Part 1: Suppose T is a tree. Then T is a connected graph and contains no
circuits.
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 80
Since T is connected, there exists at least one path between every pair of
vertices in T.
Suppose that between two vertices a and b of T, there are two distinct
paths.
Now, the union of these two paths will contain a circuit in T, a contradiction
(since T contains no circuits).
This shows that there exists one and only one path between a given pair of
vertices in T.
Part 2: Let G be a graph.
Assume that there is one and only one path between every pair of vertices
in G.
This shows that G is connected.
If possible suppose that G contains a circuit.
Then there is at least one pair of vertices a, b such that there are two
distinct paths between a and b. But this is a contradiction to our assumption.
So G contains no circuits. Thus G is a tree.
4.4.9 Problem
A tree G with n vertices has (n-1) edges.
(Refer unit 3 for the solution)
Problem: If T is a tree (with two or more vertices), then there exists at least
two pendant (a vertex of degree 1) vertices.
Solution: Let n = the number of vertices in G. Then G has n-1 edges. Now
1
deg( ) 2 2( 1) (2 2)
n
i
i
v E n n
=
= = =

.
Now if there is only one vertex, say v
1
of degree 1, then
deg(v
i
) > 2 for i = 2, 3, , n and
1 2
deg( ) 1 deg( ) 1 2 2 2 1.
n n
i i
i i
v v n n
= =
= + > + =


Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 81
But 2n-2 > 2n-1 or -2 > -1, a contradiction.
Therefore there are at least two vertices of degree 1.
Note: If two nonadjacent vertices of a tree T are connected by adding an
edge, then the resulting graph will contain a cycle.
4.4.11 Problem
Any connected graph with n vertices and n 1 edges is a tree.
Solution: Let G be a connected graph with n vertices and n 1 edges.
It is enough to show that G contains no circuits.
If possible suppose that G contains a circuit.
Let e be an edge in that circuit.
Since e in a circuit, we have that G e is still connected.
Now G - e is connected with n vertices, and so it should contain at least n
1 edges, a contradiction (to the fact that G - e contain only (n-2) edges).
So G contains no circuits. Therefore G is a tree.
4.4.12 Problem
If a graph G contains n vertices, n -1 edges and no circuits, then G is a
connected graph.
Proof: Let G be a graph with n vertices, n 1 edges and contains no circuits.
In a contrary way, suppose that G is disconnected.
G consists of two or more circuitless components (say, g
1
, g
2
, , g
k
).
Now k > 2. Select a vertex v
i
in g
i
, for 1 s i s k.
Add new edges e
1
, e
2
, , e
k-1
where e
i
=
1 i i
v v
+
to get a new graph G
*
.
It is clear that G
*
contains no circuits and connected, and so G
*
is a tree.
Now G
*
contains n vertices and (n - 1) + (k - 1) = (n + k - 2) > n edges, a
contradiction (since a tree contains (n - 1) edges).
This shows that G is connected.
This completes the proof.
We summarize the above problems as a following theorem.
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 82
4.4.13 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.

4.5 Rooted Trees
We study binary trees and applications of binary trees. Since the natural
way their vertices correspond to an initial segment of the positive integers,
complete binary trees can be represented very efficiently on computers.
They are applied in a number of excellent algorithms, including Heap Sort,
priority queue implementation, and algorithms for the efficient ordering of
data in hash tables. We also considered Height balanced trees which are
important in computer science.
4.5.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.
4.5.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
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 83
extensively used in the computer search methods, binary identification
problems, and variable length binary codes).
4.5.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).











(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.
4.5.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
.
v
3
v
2
v
4
v
6
v
8
v
5
v
1
1
v
7
v
1
v
1
0
v
13
v
9
v
12
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 84
4.5.5 Definition
A non-pendent vertex in a tree is called an internal vertex.
4.5.6 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 binary tree 4.5.3, 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.
4.5.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.
4.5.8 Definition
The sum of path lengths from the root to all pendent vertices is called the
path length (or) external path length of a tree.
4.5.9 Example
(i) A 13-vertex, 4-level binary tree was given in Fig.












level 1
level 4
level 0
level 2
level 3
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 85
Here the number of vertices at levels 0, 1, 2, 3, 4 is 1, 2, 2, 4 and 4
respectively.
4.5.10 Definition
A binary tree is a directed tree T = (V, E), together with an edge-labeling f: E
{0, 1} such that every vertex has at most one edge incident from it
labeled with 0 and at most one edge incident from it labeled with 1.
Each edge (u, v) labeled with 0 is called a left edge; in this case u is called
the parent of v and v is called the left child of u.
Each edge (u, v) labeled with 1 is called a right edge; in this case u is also
called the parent of u, but v is called the right child of u. The subtrees of
which the left and right children of a vertex u are the roots are called the left
and right subtrees of u, respectively. We represent a binary tree by a triple
(V, E, f).
Observation:
(i) Every vertex in a binary tree has a unique parent, a unique left child,
and a unique right child, if it has any at all. That each vertex has a
unique parent (if any) follows from the definition of tree, where it is
required that there be a unique path from the root to each vertex.
(ii) Each vertex has a unique left child and a unique right child (if any)
follows from the labeling of the edges of the tree with 0s and ls. (At
most one edge from the parent can have a 0 label and at most one
edge can have a 1.)
(iii) Every vertex other than the root has a parent. Since every vertex u in a
tree must have a path to it from the root and the last vertex before v on
such a path must be the parent of v.
(iv) See the following figure for an illustration.


Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 86

4.5.11 Example
Details such as edge labels and the direction of edges are usually
represented only implicitly in drawings of binary trees. The convention is that
for each vertex v the root of us left subtree lies below v and to its left on the
page, whereas the root of us right subtree lies below v and to its right on the
page. Figure 5-46 shows an example of a binary tree drawn with and
without edge labels and directed edges.
There are a few special kinds of binary trees that are important in computer
applications; one of these is the complete binary tree.


4.5.12 Definition
A complete binary tree is a binary tree for which the level-order indices of
the vertices form a complete interval 1, , n of the integers. That is, if such
a tree has n vertices there is a vertex in the tree with index i for every i from
1 to n.



Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 87
4.5.13 Example
Consider the two binary trees shown in the figure. Only the first one is
complete. In particular, the second tree has ten vertices but has no vertex
with index 10.


4.5.14 Lemma
In a complete binary tree with n vertices the indices of the vertices in the k
th
level comprise the complete interval 2
k
through 2
k+1
- 1,or from 2
k
through n
if n is less than 2
k+1
-1.
Proof: The proof is by induction on k.
For k = 0 and n = 0 the lemma holds vacuously.
For k = 0 and n > 0 there is exactly one vertex with index 2
0
= 2
0+1
- 1 = 1,
and that is the root, which is also the only vertex at level 0.
Induction Hypothesis: For larger values of k, we assume the lemma holds
for k-1,
If n < 2
k
, the lemma holds vacuously. Otherwise, we invoke the definition of
level.
The vertices in level k are exactly those at distance k from the root.
The vertices in level k - 1 are exactly those at distance k-1 from the root.
It follows that the vertices at level k are precisely the children of the vertices
at level k -1, which the inductive hypothesis asserts are those with indices
2
k-1
through 2
k
-1. By the definition of level-order index, the children have
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 88
indices in the range 2
k
through 2
k+1
-1. This complete interval, or the initial
segment of it up through n, must be in T, by the definition of complete binary
tree.
Self Assessment Questions
1. Write the degrees of all vertices in the graph given in example 4.2.8.
2. Can a simple graph exist with 15 vertices each of degree five.
3. How many vertices does a regular graph of degree 4 with 10 edges
have ?
4. Consider the following graph: Write the paths, lengths of path, cycles,
and also specify whether or not simple path/closed path.

5. Verify whether the following pairs of graphs are isomorphic. If not,
explain the relation..

(i)



(ii).

























Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 89
(iii).




(iv).




(v).




6. Which of the following graphs are trees?




7. Draw all trees with five vertices.
8. Consider the following graph.

The number of components in this graph is _____
9. How many non-isomorphic trees have 6 vertices ?
























G
1
G
2
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 90
10. In the graph 4.5.3, the number of vertices of degree 3 is _______
11. Draw the 11-vertex binary trees, also find its path lengh.

4.6 Summary
This Unit is meant for beginning your process of learning Graph Theory. It
started with the definition of Graph and moved on to illustrate the concepts
of finite and infinite graph, 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 graph called trees and studied some properties. We
considered the binary search tree and their properties. We presented some
illustrations on binary search tree and complete binary search trees.

4.7 Terminal Questions
1. Define the terms: Graph, finite graph, infinite graph, incidence, degree,
isolated vertex, pendent vertex, null graph
2. Explain the Koingsberg Bridges problem.
3. Explain the Seating Arrangement Problem.
4. Show that the sum of the degrees of the vertices of a finite graph G is
twice the number of edges.
5. Show that the number of vertices of odd degree is always even.
6. Show that an infinite graph with finite number of edges must have an
infinite number of isolated vertices.
7. Show that the maximum degree of any vertex in a simple graph is (n -
1).
8. Show that the maximum number of edges in a simple graph with n
vertices is
2
) 1 n ( n
.

Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 91
9. Define an isomorphism between two graphs and give an example of it.
1. Draw all regular binary trees
(a) with exactly 7 vertices
(b) with exactly 9 vertices
2. Draw all distinct binary tree with (i) 3 vertices, (ii) 4 vertices.
3. 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.
4. Define a binary tree, complete binary tree and give examples of
each.

4.8 Answers
Self Assessment Questions
1. Here d(u
1
) = 3; d(u
2
) = 4; d(u
3
) = 3; d(u
4
) = 3; and d(u
5
) = 1
Now

=
5
1 i
i
u = 3 + 4 + 3 + 3 + 1 = 14. E = 7.
So

=
5
1 i
i
) u ( d = 2 E
Therefore the sum of degrees of all the vertices of a graph G is twice
the number of edges in G
2. No, since the sum of the degrees of the vertices cannot be odd.
3. Let G be a regular graph of degree 4 with 10 edges and let 'n' be the
number of vertices in G. Then

eV u
) u ( d = 2 10 = 20.
n 4 = 20. n = 5.



Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 92
4.
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

5. (i) Isomorphic.
(ii) Isomorphic
(iii). Not isomorphic, since they do not have the same number of
vertices.
(iv). Not isomorphic, since the first graph has a vertex of degree 2
but second does not..
(v). Not isomorphic, since the first graph has two vertices of degree
2 and the second has one vertex of degree 1.
6. G
1
is a tree, since it is a connected graph without circuits. G
2
is not a
tree (since it is not connected).

7.





G
1
G
2
G
3
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 93
First draw five vertices. Then connect them, so that no cycles are
created. In this process, we must be careful not to repeat trees since
two trees which appear different may just be drawn differently. Here
there are three trees with five vertices as shown above.
8. Three components
9. Six non-isomorphic trees.
10. 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.
11. (i)














level -1
level - 0
level -2
level -3
Fundamentals of Theory of Computer Science Unit 4
Sikkim Manipal University Page No.: 94
The path length: 2 + 2 + 3 + 3 + 3 + 3 = 16.
(ii)










The path length: 1 + 2 + 3 + 4 + 5 + 5 = 20.

level 5
level 0
level 1
level 2
level 4
level 3
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 95
Unit 5 Formal Languages and Grammars
Structure
5.1 Introduction
Objectives
5.2 Grammars and Languages
5.3 Classification of Grammars
Self Assessment Questions
5.4 Summary
5.5 Terminal Questions
5.6 Answers

5.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 the student must be able to:
- Learn to construct the language using grammar.
- Construct the grammar, for a given language.

5.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
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 96
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 using 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 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 with any other noun such as Tom and
Jim, ran by any verb in the past tense like 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. Similarly,
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 97
<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
represents 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 form 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:

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 denotes these production rules. Now if you consider that
your vocabulary is restricted to Sandy, Jack, Sam, ran, jumped, ate,
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 98
swiftly and quickly to form sentences of the form <noun> <verb>
<adverb>, then you can describe the grammar as 4-tulple.
Before presenting the formal definition of grammar, we review some
preliminary notations and definitions.
5.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.
5.2.2 Example
Take S = {a, b}. Then x = abab, y = aaab, z = aaabb are strings on S.
5.2.3 Definition
The length of the string is the number of symbols in the string.

5.2.4 Properties of strings
Let S be the set of symbols, and S* denote the set of all strings (including
empty string).

Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 99
5.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).

5.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).
5.2.7 Example
Let the symbols
L : letter
D : digit
I : identifier
Write the grammar G = ( ) , , ,
N T
V V S u
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 100
Where V
N
= {I, L, D}
V
T
= {a, b, c, , x, y, z}
S = I

5.2.8 Definition

5.2.9 Example
Consider the above example, the direct derivatives are as follows:

5.2.10 Definition





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

5.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.
5.2.12 Example
Let G =
{ } ( ) { } ( )
, , , , , , , , , E T F a E + - u

Where the variables E (expression), T (term), and F (factor) used in
conjunction with arithmetic expressions.
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 102
We wish to derive the expression a * a + a as follows: Starting with the
symbol E.

5.2.13 Problem
Generate the language L (G) =
{ }
1
n n n
a b c n > by the following grammar.

Solution:






Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 103
We generate the language for n = 2. That is, we derive the string a
2
b
2
c
2
.

5.2.14 Example
Consider the grammar
{ } { } ( )
, , , , , G S C a b S = u

Solution:
Derivation for n = 2. i.e., the string a
2
ba
2


5.2.15 Problem

Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 104
Solution:
Take n = 2, m = 3. We generate the string a
2
ba
3


5.2.16 Problem
5.2.17 Problem
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 105
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.

In this sequence, the strings that can be replaced are underlined.
5.2.18 Problem

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:




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

5.3 Classification of Grammars
Every language is specified by a particular grammar. The classification of
languages is based on the classification of the grammar used to specify
them. Grammars are classified accordingly to the types of productions.
5.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
| s o ,
N
V e o and | has the form a, B or a, where a eV
T
, B e V
N
is
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 107
called type -3 grammar or regular grammar. The language generated
by this grammar is called a regular language
( ) ( )
3
L T .
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.
5.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
replace 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
).
5.3.3 Note
i) The above theorem also holds for grammars of type 1, 2 and 3.
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 108
ii) ( ) ( ) ( ) ( )
3 2 1 0
L T L T L T L T _ _ _ .
5.3.4 Theorem
Every monotonic grammar G is equivalent to type 1 grammar.
5.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)

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
5.3.6 Problem
Construct a grammar for the language.
{ }
{ }
*
, , ' 3 L x x a b the number of a s in x is a multiple of = e





Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 109
Solution:

Therefore the grammar
{ } { } ( )
, , , , , ,
T N
G V a b V S A B S = = = u
5.3.7 Problem
Find the highest type number that can be applied to the following productions:
.


Solution:
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 110
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.
5.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. There are other operations such as string
operations that are applied element-wise on the languages. For example, if
we consider two languages L
1
and L
2
over some common alphabets, 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
.
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 111
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. Refer the units 10, 11 for detailed
proofs of closed operations.
5.3.9 Theorem
The languages L
0
, L
cs
, L
cf
and L
r
are closed under the operations
concatenation and union.
5.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
{ } { }
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


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.




Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 112
5.3.11 Problem
Obtain a grammar to generate the language

Solution: It is clear from the statement that if 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
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.
5.3.12 Problem
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 113
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
1. Suppose G = ({S, A, B}, {a, b}, u, S) where uconsists 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.
5. Let A = {ab, bc, ca}. Find whether the following strings
i) abc, ii) ababab, iii) abba, iv) bcabbab belong to A
*
.
6. Find the language for the grammar.
{ } { } ( )
0,1 , , ,
T N
G V V S S = = = u
where the set of productions
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 114
: 11 , 0. S S S u
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

5.4 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. Some types of grammars depending on their
productions, were discussed. These are useful for generating algorithms.

5.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)
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).
Fundamentals of Theory of Computer Science Unit 5
Sikkim Manipal University Page No.: 115
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).
5.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
+
= > >


Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 115
Unit 6 Deterministic Finite Automata
Structure
6.1 Introduction
Objectives
6.2 Basic Terms
6.3 Deterministic Finite Automaton (DFA)
6.4 Transition System (Transition graph)
6.5 Language accepted by a DFA
Self Assessment Questions.
6.6 Summary
6.7 Terminal Questions
6.8 Answers

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

Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 116
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 continuously; 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 the student must 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.

Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 117
6.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 automaton 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.
6.2.1 Definition
An automaton system in which the output depends only on the present input
is called a Moore machine. Alternatively, an automaton system in which the
output depends both on the present input and the present state is called
Mealy machine.

6.3 Deterministic Finite Automaton (DFA)
6.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 alphabets.
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 118
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.
6.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).
6.3.3 Illustration
Let us take the pictorial representation of DFA shown in figure and
understand the various components of DFA.

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 is 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
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 119
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 (alphabets) for the machine.
There will be a transition from one state to another based on the input
alphabets. 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 alphabets {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

6.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.
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 120
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.
6.4.1 Example
The graph represents the DFA,

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
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 121
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.

6.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
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 122
the machine stays in state q
1
which is not a final state. Therefore the string
0100 is rejected by the machine.
6.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 alphabets.
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, and its value gives the state the automaton will be in 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
).
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 123
6.5.2 Properties

6.5.3 Example

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.

6.5.4 Example
Obtain a DFA to accept strings of as and bs starting with the string ab.
Solution: It is clear that the string should start with ab and so, the minimum
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 124
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



Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 125

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.
The state q
3
is called trap state or rejecting state.
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 126

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

To accept the string abab: The string is accepted by the machine.


Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 127

To reject the string aabb:

Therefore the string aabb is not accepted by the machine.
6.5.5 Note
Sometimes we ignore the extended notion
.
o and we use only o (assuming
that the reader is well acquainted with it).
6.5.6 Example
Consider a finite automation that will accept the set of natural numbers
which are divisible by 3,


Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 128

6.5.7 Example
Obtain a DFA to accept even number of as, and odd number of as.
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 129
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).

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 an acceptable state.

6.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, remain q
0
only so that any number of bs can end
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 130
with one a. The machine for this can be of the form


In q
1
, on input symbol b, remains q
1
then 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 no 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



Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 131

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




Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 132

The machine to accept not more than three as: The machine should accept
not more than three as means,
It can accept zero as
It can accept one a
It can accept two as
It can accept 3 as
But, it cannot 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.
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 133

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.

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.
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 134

6.5.9 Problem

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


Self Assessment Questions
1. The symbol E is used for _______
2. The symbol O is used for ________
3. In an automaton system, the states are represented by _________
4. State relation helps to determine _________
5. An automaton system in which the output depends only on the present
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 135
input is called a ________
An automaton system in which the output depends both on the present
input and the present state is called ________
6. Consider the example 6.3.3.
Write the states, input alphabet, final states, starting state.
7. Construct the state table for the following DFA.

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.
9. Consider the example 6.5.8,
(i) The set of states ___________
(ii) The set of final states _________
(iii) Starting state ____________
(iv) S(q
2
, b) = _________, S(q
0
, b) = _______

6.6 Summary
The concept of finite automata is used in wide applications. Large natural
vocabularies can be described using finite automaton 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 gave a
comprehensive idea about the DFA and a graphical representation of DFA.
Further we discuss the language accepted by DFA with certain examples.

6.7 Terminal Questions
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 136
1. What is DFA ? Explain with example.
2. When do 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.
(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.

6.8 Answers
Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 137
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.

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

Fundamentals of Theory of Computer Science Unit 6
Sikkim Manipal University Page No.: 138

9. (i) Set of states {q
0
, q
1
, q
2
, q
3
, q
4
}
(ii) Set of final states {q
0
,q
1
, q
2
, q
3
}
(iii) Starting state {q
0
}
(iv) S(q
2
, b) = q
2
, S(q
0
, b) = q
0



Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 138
Unit 7 Non Deterministic Finite Automata
Structure
7.1 Introduction
Objectives
7.2 Non-Deterministic Finite Automata
7.3 Language accepted by a NDFA
7.4 Conversion from NDFA to DFA
7.5 Moore and Mealy Machines
Self Assessment Questions
7.6 Summary
7.7 Terminal Questions
7.8 Answers

7.1 Introduction
Consider the transition diagram shown in the figure. 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 cannot determine exactly in which state the machine will be.
So, this is called Non-Deterministic Finite Automaton (NDFA).
Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 139

The transitions from each state of the machine shown below based on the
input alphabets {a, b}.

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 the student must be able to:
- Understand the idea of NDFA.
- Draw transition diagram for NDFA.
- Find the language accepted by a NDFA.
- Know applications of NDFA.
- Learn Moore and Mealy Machines
7.2 Non-Deterministic Finite Automata
Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 140
There is another type of finite state automaton in which there may be
several possible next states for each of input value and state. Such
machines are called nondeterministic.
7.2.1 Definition
A NDFA 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 alphabets.
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 are 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.
7.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.
7.2.3 Properties



Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 141

7.3 Language Accepted by NDFA
A string is a sequence of symbols obtained from E. The set of all strings
recognized by automaton is called language. The language L accepted by
an NFA M = (Q, E, o, q
0
, F) is defined as follows.
7.3.1 Definition
Let M = (Q, E, o, q
0
, F) be an NDFA where Qs is the set of finite states, E is
set of input alphabets (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 states. The string
(also called language) w accepted by an NDFA 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.
(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).
7.3.2 Note (Need for Non-deterministic finite automaton)
Digital computers are deterministic machines. 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 NDFA. After constructing an NDFA, DFA can be easily
Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 142
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 NDFA easily and later that can be
converted into DFA.
7.3.3 Example
Obtain an NDFA 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.

7.4 Conversion from NDFA to DFA
Let M
N
= (Q
N
, E
N
, o
N
, q
0
, F
N
) be an NDFA and accept 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 NDFA to its equivalent DFA is
shown below.
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
.
Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 143
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


state 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 NDFA.
7.4.1 Example
Convert the following NDFA to its equivalent DFA.
Solution:
Step 1: q
0
is the start DFA, so Q
D
= {{q
0
}} _______________(i).
Step 2: Find 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).
The corresponding transition on a = 0 and a = 1 are shown below.

Consider the state {q
0
, q
1
}:



Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 144

Since the states obtained above are the 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).

Consider the {q
0
}:

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


Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 145


Consider the state {q
0
, 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.

Consider the state {q
1
, q
2
}:
Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 146

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 = a0 and a = 1
should be added to the transitional table as shown.

Consider the state {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.

Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 147


7.4.2 Example:
Construct a DFA corresponding to the NDFA as shown below.

Solution: The DFA corresponding to the given NDFA is

7.5 Moore and Mealy Machines
The automaton systems we have discussed so far are limited to binary
output. That is, the systems can either accept or do not accept a string. In
those systems, this acceptability is decided based on the reachability from
the initial state to the final state. This property of the system produces
restrictions in choosing outputs from some other alphabet, then output. You
Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 148
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 automation system is dependent only on the present state, then,
Output = (q(t)), where q(t) is the present state.
The above automaton system is called a Moore machine.
Alternatively, when the output of the automaton 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 automaton 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 which is 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
).
7.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 alphabets.
A is the output alphabet
o is transition function, which is a mapping from E Q into Q.
is the output function mapping Q into A
q
0
e Q is the start state.

Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 149

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

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.
7.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
Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 150
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.
7.5.4 Example:
Convert the Moore machine M
1
whose state table is given in table into and
equivalent mealy machine.

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.
7.5.5 Example:
Convert the Moore machine whose state table is given into an equivalent
Mealy machine.
Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 151

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.

From the state table shown above, we observe that for the states 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.

In this table, we replaced the occurrence of q
2
in the remaining rows with q
1
.
7.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.
Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 152
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.
7.5.7 Example:
Consider the Mealy machine:

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.

Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 153

Now, rearranging the state table as shown in table, into the Moore machine
format, we obtain the state table as shown below.

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
.


Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 154

Self Assessment Questions
1. Obtain NDFA to recognize the strings abc, abd and aacd assuming
E = {a, b, c, d}.

7.6 Summary
In this unit, we learnt the concept of Non-deterministic finite automata.
Construction of equivalent DFA from a given NDFA was discussed. We also
discussed a method to convert a Moore Machine into a mealy machine
(vice-versa). Some illustrations are presented on these aspects.
7.7 Terminal Questions

1. Draw the transition diagram of the nondeterministic finite-state
automaton whose next state function is given in the table.

Hint:
Fundamentals of Theory of Computer Science Unit 7
Sikkim Manipal University Page No.: 155

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

Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 155
Unit 8 Further Problems on DFA and NDFA
Structure
8.1 Introduction
Objectives
8.2 Problems on DFA
8.3 Problems on NDFA
8.4 Difference between DFA and NDFA
Self Assessment Questions
8.5 Summary
8.6 Terminal Questions
8.7 Answers

8.1 Introduction
In this unit we study the different problems on DFA and NDFA. We have
given corresponding transition diagrams to understand the concepts easily.
Objectives:
At the end of the unit the student must be able to:
- Understand more precisely the idea of DFA.
- Draw transition diagrams.
- Obtain the DFA to accept a given string.
- Apply the techniques to various DFA and NDFA.
- Know applications of DFA and NDFA.

8.2 Problems on DFA
8.2.1 Problem
A finite state automaton is defined by a transition diagram shown below.

Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 156

Find
(i) Its states
(ii) Its input symbols
(iii) Its initial state
(iv) Its accepting states
(v) o (q
1
, 1), o (q
0
, 0)
(vi) write the next state table.
Solution:
(i) The states are Q = {q
0
, q
1
, q
2
}, these are labeled with circles.
(ii) The input symbols of Q are 0 and 1, these are the labels of the arrows.
(iii) The initial state of Q is q
0
, since the unlabeled arrows points to q
0
.
(iv) The final or accepting state is q
2
, since this state is marked by double
circle.
(v) Since there is arrow from q
1
to q
2
labeled 1, we have o (q
1
, 1) = q
2
, and
similarly, o (q
0
, 0) = q
1
.
(vi) The next state table is


Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 157
8.2.2 Problem:
Consider the finite state automaton defined by the following next table.

Find
(i) the states,
(ii) the input symbols,
(iii) the initial state,
(iv) the accepting states
(v) o (q
0
, c) = ____
(vi) the transition diagram.
Solution:
i) States of M = {q
0
, q
1
, q
2
, q
3
}
ii) Set of input symbols I = {a, b, c}
iii) Initial state : q
0

iv) ( )
2 0
q c , q = o
v) State diagram as follows.





Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 158

8.2.3 Example
Consider the finite state automaton given by the transition diagram

(i) To what state does the automaton go when the symbols of the following
strings are input to it in sequence starting from the initial state ?
(ii) 00, 0010, 10101, 010100
(iii) Which of the strings in part (i) are accepted by an automaton?
(iv) What is the language accepted by an automaton?

Solution:
Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 159
(i) The respective states are q
2
, q
1
, q
0
, q
2
.
(ii) Since q
2
is an accepting state, the string 00 and 010100 are sent to an
accepting state and hence they are accepted by an automaton.
(iii) We note that if x is any string that ends in 00, then x is accepted by an
automaton. For if the length of x > 2, then the first n-2 symbols of x
have been input, the DFA is in one of the three states q
0
, q
1
and q
2
.
From any of these three states, the input of 00 in sequence sends DFA
first q
1
and then the accepting state q
2
. Hence, any strings of 0s and
1s that ends in 00 is accepted by DFA.
8.2.4 Problem:
Draw the state diagram for the finite automation ( ) F , , q , , Q M
0
o E = where E
= {a, b}, { }
2 1 0
q , q , q Q= , F = {q
0
, q
1
}, Q x Q : E o defined by

Verify whether or not the strings aaab and bbaa are acceptable to M.
Solution:
i) Consider the string: aaab
Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 160

Therefore the string aaab is not accepted by M.
ii) Consider the string: bbaa

Therefore bbaa is not accepted by M.









iii) State diagram:
Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 161

Example: Construct deterministic finite state automata that recognize each
of this language.
a) the set of bit strings that begin with two 0s.
b) the set of bit strings that contain two consecutive 0s.
c) the set of bit strings that do not contain two consecutive 0s.
d) the set of bit strings that end with two 0s.
Solution: (a) our aim is to construct a deterministic finite state automaton
that recognizes the set of bit strings with two 0s. Besides that start state s
0
,
we include a nonfinal state s
1
; we move to s
1
from s
0
if the first bit is a 0.
Next, we add a final state s
2
, which we move to from s
1
if the second bit is a
0. When we have reached s
1
we know that the first two input bits are both
0s, so we stay in the state s
2
(no matter what the succeeding bits, if any
are). We move to a nonfinal state s
3
from s
0
if the first bit is a 1 and from s
1

if the second bit is a 1. We can easily verify that the finite state automaton
recognizes the set of bit strings that begin with two 0s.
Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 162


To construct a deterministic finite-state automaton that recognizes that
contain two consecutive 0s. Besides the start state s
0
, we include a nonfinal
state s
1
, which tells us that the last input bit seen is a 0, but either the bit
before it was a 1, or this bit was the initial bit of the string. We include a final
state s
2
that we move to from s
1
when the next input bit after a 0 is also a 0.
If a 1 follows a 0 in the string (before we encounter two consecutive 0s), we
return to s
0
and begin looking for consecutive 0s all over again.

In this, our goal is to construct a deterministic finite-state automaton that
recognizes the set of bit strings that end with two 0s. Besides the start state
s
0
, we include a nonfinal state s
1
, which we move to the first bit is 0. We
include a final state s
2
, which we move to from s
1
if the next input bit after a
0 is also 0. If an input of 0 follows a previous 0, we stay in state s
2
because
the last two input bits are still 0s. Once we are in state s
2,
an input bit of 1
sends us back to s
0
and we begin looking for consecutive 0s all over again.


Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 163

8.3 Problems on NDFA:
8.3.1 Example: For NDFA whose transition diagram is shown below

Find
(i) initial state
(ii) set of states
(iii) input set
(iv) state table defining the next state function.
Solution:
8.3.2 Problem:
Construct a DFA which accepts strings of 0s and 1s where the value of
each string is represented as a binary number. Only the strings
representing zero modulo five should be accepted.
For instance, 0000, 0101, 1010, 1111 etc should be accepted.
Solution: Each string represents a modulo 5, the modulo 5 integer may
represent either 0, 1, 2, 3 or 4.
For each integer we shall represent states S, A, B, C and D respectively.
First we list states corresponding to binary numbers below.
Decimal Binary States
Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 164
0 0000 S
1 0001 A
2 0010 B
3 0011 C
4 0100 D
5 0101 S
6 0110 A
7 0111 B
8 1000 C
9 1001 D
10 1010 S
11 1011 A
12 1100 B
13 1101 C
14 1110 D
15 1111 S
The transition diagram corresponding to this shown below.

8.3.3 Problem
Construct a DFA which accepts the set of all strings beginning with a 1 that
when interpreted as a binary integer, is a multiple of 5. For example, 101,
1010, 1111 etc are multiples of 5.
(Observe that the string 0101 is not beginning with 1 and it should not be
accepted).
Solution:
Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 165

8.3.4 Problem
Obtain deterministic finite automata to accept the language
L = {w(ab + ba) w e {a, b}
*
}
Solution: The set of states: {q
0
, q
1
, q
2
, q
3
, q
4
};
The set input symbols: {a, b}
The set of final states: {q
2
, q
4
}
Starting state: {q
0
}.



8.4 Difference between DFA and NDFA
Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 166


Self Assessment Questions
Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 167
1. Find the transition diagram for DFA to accept the language
L = {wbab w e {a, b}
*
}
2. Find the transition diagram for DFA to accept the language only odd
number of as and any number of bs.

8.5 Summary
This unit deals with some problems on DFA and NDFA, we also study the
difference between DFA and NDFA here. The different state diagrams are
illustrated in the form of pictures. We also come to know how to accept a
given string and get the DFA. The different applications of DFA and NDFA
are illustrated here

8.6 Terminal Questions
1. Obtain deterministic finite automata to accept the language
L = {w(ab+ba) | w e {a, b}* }
2. Construct a deterministic finite state automata that recognizes each of
this language.
a) the set of bit strings that begin with two 0s .
b) the set of bit strings that contain two consecutive 0s.
c) the set of bit strings that do not contain two consecutive 0s .
d) the set of bit strings that end with two 0s

8.7 Answers
Self Assessment Questions
1. The transition diagram to accept the given language is
Fundamentals of Theory of Computer Science Unit 8
Sikkim Manipal University Page No.: 168

Here the set of states: {q
0
, q
1
, q
2
, q
3
};
The set input symbols: {a, b}
The set of final states: {q
3
}
Starting state :{q
0
}.
2. The transition diagram to accept the given language is

Here the set of states: {q
0
, q
1
};
The set input symbols: {a, b}; The set of final states: {q
1
}; Starting state
:{q
0
}.

Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 169
Unit 9 Regular Expressions and
Regular Languages
Structure
9.1 Introduction
Objectives
9.2 Regular expressions
9.3 Regular Expressions accepted by the Language
9.4 Finite Automaton from Regular Grammar
9.5 Regular Grammar from Finite Automata
Self Assessment Questions
9.6 Summary
9.7 Terminal Questions
9.8 Answers

9.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 is
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:
After going through this unit, you will 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.
Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 170
9.2 Regular Expressions
In computing, regular expressions are used to represent a set of strings and
include symbols that are arranged using certain syntax rules. We can de
regular 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.
9.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 which 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
Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 171
a. R+S is a regular expression corresponding to the language
L
R
L
S
.
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).
9.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}
*

9.3 Regular Expressions accepted by the Language
9.3.1 Example:
Some examples of regular expressions and the language corresponding to
these regular expressions are given here.


Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 172
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 string 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 string 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.
9.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).
Thus, the complete expression is given by
(. + b) (ab)* (. + a)

Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 173
9.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)
*

9.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
o begin with combination of any number of 1s and 01s represented by (1
+ 01)
*

o 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
*
.
9.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}.
Solution: Strings of as, bs and cs can be generated using the regular
expression
(a + b + c)*.
Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 174
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)*
9.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}.


Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 175
9.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 these two expressions is same.
9.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}.

Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 176
9.4 Finite Automaton from Regular Grammar
9.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
*
.
9.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.
9.4.3 Definition
A grammar which has at most one 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.
9.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 from the regular grammar is a regular language.
Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 177
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 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.





Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 178
9.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.

The transition diagram is shown below.

The DFA is
M = (Q, E, o, q
0
, F) where
Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 179
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
.
9.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 is shown in the table.

Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 180
Observe that for each production of the form , make A as the final
state.
The transition diagram corresponding to this is shown below.


9.5 Regular Grammar from Finite Automata
9.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.
Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 181
9.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 is shown
below.

From the diagram, it is clear that the state B is a final state.
Therefore we introduce the production .
The grammar G corresponding to the productions obtained is shown below.


Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 182

9.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 is shown below.

Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 183
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}

Observation: The finite automaton 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.
9.5.4 Example
Obtain a right linear grammar for the regular expression ((aab)
*
ab)
*
, given
by the transition diagram.

The right linear grammar is given by
G = (V
N
, V
T
, S, u) where
V
N
= {S, A, B}
V
T
= {a, b}

Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 184

9.5.5 Note
The left linear grammar can be obtained from FA as follows.
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.
9.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.

Step 2: obtain the right linear grammar for the above DFA. The
corresponding productions are shown below.

Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 185

Step 3: Reverse the productions of right linear grammar to get left linear
grammar.
If A abcdB 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.

Therefore the final left linear grammar is
G = (V
N
, V
T
, S, u) where
V
N
= {C, A, B}
V
T
= {0, 1}

Now we show that the string 10101 is accepted by DFA.



Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 186

Hence the left linear grammar obtained is equivalent to the given FA.

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 ______
4. Obtain a left linear grammar for the regular expression ((aab)
*
ab)
*
.


9.6 Summary
In this unit special type of grammar called regular grammars were
considered. Different forms of regular expressions and the regular
expressions accepted by the language are given. We provided a method of
Fundamentals of Theory of Computer Science Unit 9
Sikkim Manipal University Page No.: 187
obtaining a regular grammar from the finite and automaton (and vice versa).
Sufficient number of examples were given.

9.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.
9.8 Answers
Self Assessment Questions
1. Set of strings consisting of even number of 1s.
2. The language consists of a string 1 or strings of (01)s that repeat zero
or more times.
3. Stings of 0s and 1s ending with any number of 1s (possible none).
4. G = (V
N
, V
T
, S, u) where V
N
= {A, B, S}, V
T
= {a, b}



Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 188
Unit 10 Properties of Regular Languages
and Pumping Lemma
Structure
10.1 Introduction
Objectives
10.2 Closure Properties of Regular Sets
10.3 Pumping Lemma
10.4 Applications of Pumping Lemma:
Self Assessment Questions
10.5 Summary
10.6 Terminal Questions
10.7 Answers

10.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 the student must be able to
- Apply the closure property on regular sets
- Understand the applications of Pumping Lemma

10.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
Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 189
operations such as union, concatenation, closure, transpose, intersection
and complementation.
10.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, start-closure.
Proof: Let L
1
and L
2
be 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.
10.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.
10.2.3 Theorem
If L
1
and L
2
are regular, then the regular language is closed under
intersection.
Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 190
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 alphabets 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 q
2
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
.
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.
Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 191
10.2.4 Theorem
If L
1
and L
2
are regular, then the regular language is closed under
difference.
Proof is straightforward as above.
10.2.5 Definition
Let E and I be the 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.
10.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)
10.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)*


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

10.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 face problems whenever
we come across non regular languages. The easiest way to prove that a
Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 193
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 3): 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:
10.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.
Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 194
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

2. The second group is the loop 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.
Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 195
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
.
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
.



Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 196
10.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 be 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.
10.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.
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 a contradiction to the assumption that the language is
regular.
Conclusion: The given language L is not regular.
10.4.2 Example
Show that L = {a
n
b
n
n > 0} is not regular.
Solution:
Step 1: Let L be 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.
Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 197
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 same
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.
10.4.3 Example
Show that L = {a
i
b
j
i > j}is not regular.
Solution:
Step 1: Let L be 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.
Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 198
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.
10.4.4 Example
Show that L = {a
n
b
l
c
n+l
n, l > 0}is not regular.
Solution:
Step 1: Let L be 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}
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.
10.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 be 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.
Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 199
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.
10.4.8 Example
Show that L = {ww w e {a, b}* } is not regular.
Solution
Step 1: Let L be 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.
Step2: 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.
Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 200
10.4.9 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.
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 j
a a a
2
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.
Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 201
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 _____
Ans: Regular.
4. State advantages of pumping lemma.
5. Verify whether the language L = {ww w e {a, b}
*
} is regular.

10.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
we discussed the way to prove that certain languages are infinite using
Pumping Lemma. Some applications of Pumping lemma were discussed.

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



Fundamentals of Theory of Computer Science Unit 10
Sikkim Manipal University Page No.: 202
10.7 Answers
Self Assessment Question
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.
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 203
Unit 11 Context Free Grammars
Structure
11.1 Introduction
Objectives
11.2 CFG for various types of CFL
11.3 Derivations
11.4 Ambiguous Grammar
Self Assessment Questions
11.5 Summary
11.6 Terminal Questions
11.7 Answers

11.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 symbol or the right most symbol. 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 few simple illustrations).


Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 204
Objectives:
At the end of the unit the student must be able to
- Understand the concept of Context free grammars
- Know the ideas in derivation trees
- Test the ambiguity in context free grammar

11.2 Context -free Grammars
11.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 terminal 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.
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 205
11.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.
Solution: The null string . can be obtained by applying the production

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.
11.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 if number of as are
followed by n number of bs and number of as and bs are not equal.
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 206
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.
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:

So, the context free grammar G = (V
N
, V
T
, u, S) where
V
N
= {S, A, B}, V
T
= {a, 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.
11.2.5 Example
Obtain a Context free grammar on {a, b} to generate a language
.
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 207
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.
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.

11.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 .
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 208
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.
11.2.7 Backus Naur Form (BNF)
This is an alternative method of displaying productions of context free
grammars.
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 it may be combined as :
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 209
< 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 < > =< > < >< >

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

11.2.9 Example
Consider the grammar
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 210
{ } { } ( )
, , , , , , 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
< > = < >
< > = < >
< > = < >


11.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 represents 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.
11.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).
11.3.2 Example
Consider the grammar
{ } ( )
, , { }, ,
T N
G V a b V S S = = = u

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
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 211

The derivation tree is:


11.3.3 Problem
Consider the grammar
( ) { , } { , , }, ,
T N
G V a b V S A B S = = = u

Where the productions
u
are:

Construct the derivation trees for the strings.
i) abababba
ii) aababb






Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 212
Solution:
Derivation tree: for (i)

Derivation tree for (ii):




Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 213








11.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 left most
derivation or right most derivation even though the derivations look different
and if the structure of derivation trees obtained is same, we cannot 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 left most 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 left most derivation twice and construct
the derivation tree. If the two derivation trees are different, the grammar
is ambiguous.
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 214
3. Obtain the string w by applying right most derivation twice and construct
the derivation tree. If the two derivation trees are different, the grammar
is ambiguous.
4. Apply the left most derivation and get string. Apply the left most
derivation again and get a different string. The derivation trees obtained
will naturally be different and don not come to the conclusion that the
grammar is ambiguous.


11.4.1 Example
Verify whether or not the following grammar is ambiguous.

Solution: Consider the two leftmost derivations for the string aaaa.

Since there are two left most derivations for the same sentence aaaa, the
given grammar is ambiguous.
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 215
11.4.2 Example
Consider a grammar
G= ) S, }, {a, ({S}, G + = With productions a. S S, S S : +
The sentence a + a + a can be derived in two different ways. The derivation
trees are distinct.

11.4.3 Example
Obtain the string aaabbabbba by applying left most derivation, the derivation
tree for the grammar is shown below.








Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 216
Solution: Apply the set of productions

Observation: In this example, we have used the productions (different from
the application of production in the SAQ).
The derivation tree is given below.

Self Assessment Questions
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 217
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

2. Find the language L (G), generated by the grammar.

{ } { } ( )
, , , , , ,
T N
G V x y z V S A S = = = u

where Z. A yA, A yA, S S, x S :
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: . 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.
7. Obtain a left most derivation for the string aaabbabbba using the
following grammar.
.

11.5 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 is also studied. 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.
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 218

11.6 Terminal Questions
1. Construct the grammar which generates the following language.
i) 1) /n
n
b
n
{a L > =
(Hint: ) S, {S}, VN b}, {a, (VT G = = = , where is: b S aSa, S
It is a context free language).
ii) 1} m 1, /n ba {a L
m n
> > =
(Hint: S, A}, {S, V b}, {a, (V G
N T
= = =
where ba A ab, A aAb, A bAa, A bAa, S aAb, S : It
is a context free language).
2. Give the BNF notations for the productions of the grammar.
G = (V
1
= {a, +, (,)}, V
n
= {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 < > = +< >
).
3. Draw the derivation tree for the sentence x
2
y
2
z for the grammar given in
problem 2.









Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 219
Ans:

4. Explain the left most and right most derivation trees.

11.7 Answers
Self Assessment Questions
1. L(G)={0, 110, 11110, 1111110, }
2. } 1 m , 0 n / Z Y X { ) G ( L
m n
> > =
3. } 0 n / b a { } 0 n / a { ) G ( L
n 2 1 n 2
> > =
+

4. 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}
The set of productions: S A B
A a aA bAA AbA AAb
B bbBaBB BaB BBa
S is the starting symbol.
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 220
7.

The derivation tree is given below.

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. J.A. Bonday and U.S.R. Murthy: Graph Theory with Applications,
Elsevier Science, New York, 1979.
Fundamentals of Theory of Computer Science Unit 11
Sikkim Manipal University Page No.: 221
4. Bernard Kolman, R.C. Busby, Sharon Ross, Discrete Mathematical
Structures PHI, 1999.
5. Graham R. L., Knuth, D. E., and Patashnik O., Concrete Mathematics,
A Foundation for Computer Science, 2
nd
Ed., Addison Wesley, 1994.
6. Harary F. "Graph Theory, Narosa Publishing House, 1995.
7. Liu.CL, Elements of Discrete Mathematics, Mc. Graw Hill.
8. Narsing Deo Graph Theory with Applications to Engineering and
Computer Sience, Prentice Hall of India Pvt. Ltd., 1997.
9. Richard Johnsonbaugh, Discrete Mathematics Pearson Education
Asia, 2001
10. Rosen K. H., Hand Book of Discrete and Combinatorial Mathematics,
CRC Press, 1999.
11. Trembly, J.P., and Manohar, R. Discrete Mathematical Structures with
Applications to Computer Science, Mc-Graw Hill, 1975.
12. A. Salomma: Computations and Automata, in Encyclopedia of
Mathematics and Its Applications. Cambridge University Press (1985).
13. Peter Linz: An introduction to Formal languages and Automata,
Narosa Publ. company (2004).


_______________________

Das könnte Ihnen auch gefallen