P. 1
Introduction to the Design & Analysis of Algorithms 2ND Edition by Anany Levitin

Introduction to the Design & Analysis of Algorithms 2ND Edition by Anany Levitin

|Views: 24,269|Likes:
Published by audrey3rd

More info:

Published by: audrey3rd on Jul 04, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

06/24/2015

pdf

text

original

Using the formal definitions o£ the asymptotic notations, we can prove their
general properties (see Problem 7 in Exercises 2.2 for a few simple examples). The
following property, in particular, is useful in analyzing algorithms that comprise
two consecutively executed parts.

THEOREM If 1

1(n) E O(g

1(n)) and 1

2(n) E O(g2(n)), then

l1(n) + l2(n) E O(max{gt(n), g2(n))).

(The analogous assertions are true for the 0 and B notations as well.)

PROOF (As you will see, the proof extends to orders of growth the following
simple fact about four arbitrary real numbers al> bl> a2o and b2: if a1 :: b1 and

a

2:: b2, then a

1 + a2:: 2 max{b1, h2].) Since IJ(n) E O(gJ(n)), there exist some

positive constant c1 and some nonnegative integer n1 such that

11(n)c:c1g1(n) forallno:n1.

Similarly, since 12(n) E O(g2(n)),

12(n) C: c2g2(n) for alln 2: n2.

Let us denote c

3 = max{c1, c2) and consider n 2: max{n1, n2] so that we can use
both inequalities. Adding the two inequalities above yields the following:

i](n) + 12(11) :" CJ8J(n) + c2g2(n)

:" c3g1(lr) + c3g2(n) = c3[g1(n) + 82(n)]

C: c32 max{gJ(n), g2(n)).

Hence, 11(n) + 1

2(n) E O(max{g1(n), g2(n))), with the constants c and n

0 required

by the 0 definition being 2c3 = 2 max{c1, c2] and max{n1, n2], respectively.

II

So what does this property imply for an algorithm that comprises two consec-
utively executed parts? It implies that the algorithm's overall efficiency is deter-
mined by the part with a larger order of growth, i.e., its least efficient part:

t1(n) E O(g1(n)) }

12(n) E O(g

2(n))

IJ(n) + t2(n) E O(max{gJ(n), gz(n))).

For example, we can check whether an array has identical elements by means of

the following two-part algorithm: first, sort the array by applying some known
sorting algorithm; second, scan the sorted array to check its consecutive elements
for equality. If, for example, a sorting algorithm used in the frrst part makes no
more than ~ n ( n -1) comparisons (and hence is in O(n2

)) while the second part
makes no more than n-1 comparisons (and hence is in O(n)), the efficiency of
the entire algorithm will be in O(max{n2

, n)) = O(n2

).

I

l

I

I.

I

I

I

I

I

2.2 Asymptotic Notations and Basic Efficiency Classes

57

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->