unit 1- final

32
Theory of Computer Science Unit 1 Sikkim Manipal University Page No.: 1 Unit 1 Mathematical Fundamentals for Computer Science Structure 1.1 Introduction Objectives 1.2 Sets and Properties 1.3 Functions and Relations 1.4 Equivalence Relations 1.5 Recursive Functions 1.6 Strings and Growth Functions 1.7 Summary 1.8 Terminal Questions 1.9 Answers 1.1 Introduction The idea of set is fundamental in the study of mathematical structures. All most all mathematical objects of can be defined in terms of sets and the language of set theory is used in every mathematical subject. Also the concept of relation and function arises when we consider a pair of objects and compare each other. In this unit we study the different relations on a set and the algebraic structures with single binary operation. In computer programming, recursion plays an important role. It is an important facility in many programming languages. We have discussed the concepts recursive functions and growth functions which are useful in analysis of algorithms and approximating the time complexity. The content of this unit provides a mathematical tool for the later units.

Upload: yogesh795

Post on 13-Apr-2015

40 views

Category:

Documents


0 download

DESCRIPTION

MCA 5TH SEM BOOK

TRANSCRIPT

Page 1: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 1

Unit 1 Mathematical Fundamentals

for Computer Science

Structure

1.1 Introduction

Objectives

1.2 Sets and Properties

1.3 Functions and Relations

1.4 Equivalence Relations

1.5 Recursive Functions

1.6 Strings and Growth Functions

1.7 Summary

1.8 Terminal Questions

1.9 Answers

1.1 Introduction

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

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

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

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

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

and the algebraic structures with single binary operation. In computer

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

many programming languages. We have discussed the concepts recursive

functions and growth functions which are useful in analysis of algorithms

and approximating the time complexity.

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

Page 2: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 2

Objectives:

At the end of the unit you would be able to

understand the fundamental idea of certain mathematical concepts.

learn the various operations on sets.

understand relations and types of relations

know the recursive functions and growth functions.

1.2 Sets and Properties

1.2.1 Definition

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

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

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

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

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

1.2.2 Note

There are five ways used to describe a set:

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

ii) Describe a set by listing its elements.

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

A(x) = 1 if x A

= 0 if x A

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

“universe of discourse ,” or just universe.

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

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

set may be generated.

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

etc) on some other sets.

Page 3: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 3

1.2.3 Example

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

to 4.

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

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

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

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

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

= 0 if x A.

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

1.2.4 Definition

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

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

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

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

1.2.5 Note

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

sets.

i) X X.

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

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

1.2.6 Example

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

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

RQZN .

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

Page 4: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 4

1.2.7 Definition

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

B, and B is a subset of A.

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

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

1.2.8 Definitions

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

denoted by .

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

a singleton set.

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

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

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

intersection of A and B.

1.2.9 Properties

S. No. Property Union Intersection

1 Idempotent A A = A A A = A

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

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

1.2.10 Definition

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

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

Boolean sum of two sets.

1.2.11 Definition

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

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

Page 5: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 5

1.2.12 Examples

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

is an integer and 1 < x < 2}.

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

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

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

= (the empty set).

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

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

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

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

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

second time in rooster form.

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

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

strictly lying between 1 and 2)

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

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

1.2.13 Definitions

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

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

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

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

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

equal.

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

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

Page 6: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 6

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

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

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

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

Ii

iA

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

Ii

iA

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

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

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

1.2.14 Examples

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

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

(y, b)}.

Note that X Y Y X.

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

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

empty set.

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

Then it is easy to observe that Ni

iA

= N and Ii

iA

= .

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

disjoint sets.

1.2.15 Definition

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

set of A.

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

2n.

Page 7: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 7

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

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

nCm distinct subsets containing m elements. Therefore,

the number of elements in P(A)

= number of subsets containing 0 number of elements

+ number of subsets containing only 1 element

+ … + number of subsets containing n elements

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

Self Assessment Questions

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

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

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

2

3}

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

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

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

following cases

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

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

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

(-, y]c.

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

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

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

1.3 Functions and Relations

Relation describes connections between different elements of the same set,

where as functions describe connections between two different sets.

Functions give a mathematical precise framework for the intuitive idea of

transformation.

Page 8: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 8

1.3.1 Definitions

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

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

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

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

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

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

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

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

f: S T is said to be

1.3.2 One-one function (or injective function)

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

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

s S such that f(s) = t.

1.3.3 Bijection

if it is both one-one and onto.

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

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

1.3.4 Definitions

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

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

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

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

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

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

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

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

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

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

Page 9: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 9

1.3.5 Example

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

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

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

not equal at 1 and so fog gof.

1.3.6 Definition

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

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

fA(xi) = i

i

1 if x A

0 if x A

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

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

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

1.3.7 Definition

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

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

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

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

on S is said to be

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

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

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

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

Self Assessment Questions

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

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

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

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

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

Page 10: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 10

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

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

1.4 Equivalence Relations

1.4.1 Definition

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

and transitive.

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

we say that a and b are equivalent.

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

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

class containing a).

1.4.2 Example

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

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

1.4.3 Example

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

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

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

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

by

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

1.4.4 Example

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

only if x12 + y1

2 = x22 + y2

2. Then ~ is an equivalence relation on R2.

1.4.5 Definition

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

Page 11: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 11

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

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

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

congruent to b modulo n‟.

1.4.6 Problem

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

Solution: Let a Z.

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

Symmetric: Let a b mod n.

n divides a - b

n divides - (a - b)

n divides b - a

b a mod n.

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

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

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

n divides a - c

a c mod n.

Hence the relation is an equivalence relation.

1.4.7 Example

Suppose n = 5. Then

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

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

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

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

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

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

Page 12: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 12

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

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

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

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

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

1.4.8 Definition

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

some index set} satisfying Ii

iS

= S and Si Sj = if i j.

1.4.9 Example

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

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

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

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

1.4.10 Lemma

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

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

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

Now suppose the intersection is non-empty.

Let x [a] [b]

x [a] and x [b]

x ~ a and x ~ b

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

a ~ b (since ~ is transitive).

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

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

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

Page 13: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 13

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

either equal or disjoint.

1.4.11 Problem

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

equivalence classes forms a partition for A.

Solution:

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

Since each [a] A, we have that Aa

]a[

A.

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

]a[

.

Therefore we have that

A = Aa

]a[

.

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

a, b A.

Hence the set of all equivalence classes forms a partition.

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

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

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

the partition.

Self Assessment Questions

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

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

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

x Q. Whether or not, „‟ is an

equivalence relation.

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

Page 14: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 14

families of sets is a partition of A.

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

10}};

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

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

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

symmetric.

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

an equivalence relation? If so find the equivalence class containing

(1, 2).

1.5 Recursive Functions

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

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

1.5.1 Definition

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

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

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

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

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

well defined.

1.5.2 Example

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

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

The same function can also be defined recursively as follows:

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

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

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

Page 15: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 15

1.5.3 Theorem: (Recursion Theorem):

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

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

negative integers satisfying

i) f(0) = s0

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

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

recurrence relation).

1.5.4 Example

Define n! recursively and compute 5! recursively.

Solution: We have f: N N. Then

i) f(0) = 1

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

Clearly f(n) = n!.

Now we compute 5! recursively as follows:

5! = 5. 4!

= 5. 4. 3!

= 5. 4. 3. 2!

= 5. 4. 3. 2. 1!

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

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

= 120.

1.5.5 Note

Any sequence in arithmetic progression or geometric progression can be

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

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

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

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

Page 16: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 16

1.5.6 Definition

The Fibonacci sequence can be defined recursively as

i) F0 = 1 = F1

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

Then

F2 = F1 + F0 = 2

F3 = F2 + F1 = 3

F4 = F3 + F2 = 5

…..

Here, there are two initial conditions.

1.5.7 Example:

Define

f(x) =

when x is even2

1 when x is odd

2

x

x

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

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

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

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

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

= 3.

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

= 4 – [3 - f(3)]

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

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

= 2.

Page 17: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 17

1.5.8 Example

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

definition is a function. That is.

i) g(0) = 1

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

Solution: We obtain

i) s0 = 1

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

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

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

function.

1.5.9 Example

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

of integers), for all integers n 1.

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

g(1) = 1

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

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

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

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

= 1 + (1 + g(10))

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

= 3 + g(5).

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

1.5.10 Definition

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

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

Page 18: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 18

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

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

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

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

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

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

g c d

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

Observations:

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

Second part is the initial condition.

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

number of steps.

1.5.11 The recursive functions of more than one variable

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

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

i) f(x, 0) = x

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

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

= f(2, 1) + 1 + 1

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

= 2 + 1 + 1 +1

= 5.

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

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

= g(3, 2) + 3 + 3

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

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

Page 19: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 19

Self Assessment Questions

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

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

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

1.6 Strings and Growth Functions

Let us see the simple example, to understand this.

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

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

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

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

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

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

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

minutes of telephone charges.

Min 1 2 3 4 5 6 7 8 9 10

Charges 1.10 1.20 1.30 1.40 1.50 1.60 1.70 1.80 1.90 2.00

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

= 4.00.

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

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

Let us list the telephone charges as follows.

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

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

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

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

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

Page 20: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 20

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

(0.1)n.

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

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

… … …

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

1.6.1 Definition

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

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

called an integer sequence.

1.6.2 Example

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

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

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

denote this sequence by

1

2}{ nn or simply by {n2}.

1.6.3 Definition

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

am, am+1, …, an.

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

multiplying them. Let us first consider the addition.

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

n

mi

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

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

n

mj

ja

or

n

mk

ka .

Page 21: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 21

1.6.4 Example

i)

4

1i

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

ii)

3

1i1i

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

1.6.5 Example

Consider the sums

3

1

)1(i

i and

4

2i

j

3

1i

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

4

2i

j = 2 + 3 + 4 = 9.

Therefore

3

1i

)1i( =

4

2i

j

1.6.6 Note

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

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

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

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

variable.

1.6.7 Example

Consider the sum

3

1i

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

Page 22: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 22

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

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

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

= i + 1 = 3 + 1 = 4.

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

Hence, the equivalent sum is

4

2j

j .

1.6.8 Example

Consider the sum

1n

0i

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

Solution:

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

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

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

n2 + 1 + i = n2 + j.

Hence, the new sum is

n

1j

2)jn( .

1.6.9 Properties of Summation

Let 1nn }a{ and

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

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

(i)

n

mi

ia +

n

mi

ib = )ba(

n

mi

ii

,

(ii) c.

n

mi

ia =

n

mi

ica .

Page 23: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 23

1.6.10 Definition

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

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

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

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

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

elements of s1 followed by the elements of s2.

1.6.11 Example

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

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

= abbabcdbcaabcdbbd.

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

1.6.12 Example

Let A = {0, 1}.

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

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

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

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

1.6.13 Growth Functions

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

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

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

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

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

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

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

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

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

Page 24: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 24

1.6.14 Definition

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

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

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

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

1.6.15 Remark

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

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

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

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

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

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

1.6.16 Example

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

x > 1.

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

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

1.6.17 Note: Order of Polynomial functions

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

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

Therefore xs is O(xs).

1.6.18 Example

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

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

x3 < x4 and 1 < x4. So

4x3 < 4x4 and 5 < 5x4.

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

Page 25: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 25

1.6.19 Example

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

Solution

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

as follows.

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

For all real numbers x > 1,

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

≤ 5x3 + 3x + 4

≤ 5x3 + 3x3 + 4x3

≤ 12x3 ≤ 12 x3.

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

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

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

1.6.20 Example

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

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

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

big-oh notation.

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

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

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

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

O(9x2).

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

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

function.

Page 26: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 26

1.6.21 Theorem

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

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

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

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

f(x) = anxn + an-1x

n-1 + … + a1x + a0

≤ an xn + an-1 x

n-1 + … + a1 x + a0

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

n-1 + a0 /x

n)

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

This shows that

f(x) ≤ C xn where

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

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

1.6.22 Example

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

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

precedes it. Therefore, for each positive integer n,

1 + 2 + 3 + … + n ≤ timesn

nnn

... = n n = n2.

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

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

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

number of steps used in sorting procedures.

1.6.23 Example

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

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

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

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

Page 27: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 27

Note that the function n grows rapidly.

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

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

in the product does not exceed n. Hence,

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

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

Taking logarithm both sides, we get

log n ≤ log nn = n log n.

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

1.6.24 Theorem

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

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

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

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

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

1.6.25 Example

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

positive integer.

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

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

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

O(n2 log n).

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

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

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

big O-estimates for the products shows that

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

Page 28: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 28

1.6.26 Example

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

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

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

log (x2 + 1)

≤ log(2x2)

= log 2 + log x2

= log 2 + 2 log x

≤ 3 log x, if x > 2.

Therefore

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

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

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

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

1.6.27 Big-Omega and Big-Theta Notations

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

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

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

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

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

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

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

1.6.28 Definition

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

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

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

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

Page 29: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 29

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

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

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

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

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

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

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

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

values.

1.6.29 Example

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

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

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

Self Assessment Questions

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

1.7 Summary

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

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

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

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

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

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

time complexity of algorithms.

Page 30: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 30

1.8 Terminal Questions

1. Explain Equivalence relation and give examples.

2. Explain growth functions and give examples.

3. Define recursive function and give examples.

4. Explain Big Omega and Big theta functions.

1.9 Answers

Self Assessment Questions

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

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

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

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

5, 4, 6,

2

3},

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

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

3, 2 ), (

2

3, 25)}.

ii) Z S = { 2 , , 2

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

Z T = { 2 , 2

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

3. i) c

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

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

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

y]c = (y, )

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

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

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

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

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

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

Page 31: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 31

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

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

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

(2, 5) R2, but 5 2.

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

5(-6) /0.

iii) Reflexive:

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

3 + 4 / 1 + 2.

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

(1, 2) (0, 3).

Transitive:

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

9. Yes

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

11. 2n.

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

is both symmetric as well as anti symmetric.

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

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

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

= (2, mod (6, 2))

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

= 2.

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

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

= 9.

Page 32: Unit 1- Final

Theory of Computer Science Unit 1

Sikkim Manipal University Page No.: 32

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

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

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

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

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

= 2.

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

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

g(x) = x2.

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

≤ x2 + 2x2 + x2

≤ 4x2

≤ 4 x2.

Therefore,

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

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

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