Docsity

More on Asymptotic Notation - Lecture Notes | CSCE 310, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Mihaela Gheorghiu Spring 2002 CSCE-310 Data Struct. & Alg.
More on Asymptotic Notation
January 23, 2002
How to use asymptotic notation for algorithm analysis.
Asymptotic notation is used to determine rough estimates of relative running time of algo-
rithms. In class we saw an example of worst-case analysis of pseudocode that led to a big-O
estimate. A worst-case analysis of any algorithm will always yield such an estimate, because it
gives an upper bound on the running time

of the algorithm, that is

, and so

. In some cases, an exact analysis of the running time is possible, and then we get

, so

. Here is an example:
a <- 0 1 unit 1 time
for i <- 1 to n do 1 unit n times
for j <- 1 to i do 1 unit n(n+1)/2 times
a <- a+1 1 unit n(n+1)/2 times
where the times for the inner loop have been computed as follows: for each
from
to
, the loop
is executed
times, so the total number of times is
 "!#$%$&$'
 (
) *
+-,
./

0
(see Appendix A.1 in the book for this, and other summation formulas). Hence in this case

1



20
.3
"
45$
If we write

3
6
7
, then

, that is
89
3
6
7
. We actually
write

3
, as recommended by the following rule.
Be as simple and as precise as possible in analysis.
Although the definitions of asymptotic notation allow one to write, for example,
8:

!
3
.
, in practice we never write something like that; we simplify the function in between
the parentheses as much as possible (in terms of rate of growth), and write instead
;
3
.
Similarly, we don’t write, for example,
8<"=5> ?@
3
:!
, but
A"B>C
. Also, it is
wrong to write, for instance,
ED
(
*
+-,
; write instead

3
, after computing the sum.
Also, the analysis should be as precise as possible, in the sense that it should provide the closest
asymptotic bounds for functions; for example, while it is true that
!
"F
3
, we know that
!
is
actually constant time, rather than quadratic time, so
!
GF
or
!

is the right analysis.
In the spirit of the simplicity rule above, when we are to compare, for instance, two candidate
algorithms
H
and
I
having running times
JK8L
3
?
!
=
and
M18ONP>
.!
, rather
than writing
JKQRMS
, we write
JKQTB
3
, and
M1UT->V
, and then we
1
pf2

Partial preview of the text

Download More on Asymptotic Notation - Lecture Notes | CSCE 310 and more Study notes Algorithms and Programming in PDF only on Docsity!

Mihaela Gheorghiu Spring 2002 CSCE-310 Data Struct. & Alg.

More on Asymptotic Notation

January 23, 2002

How to use asymptotic notation for algorithm analysis.

Asymptotic notation is used to determine rough estimates of relative running time of algo-

rithms. In class we saw an example of worst-case analysis of pseudocode that led to a big-O

estimate. A worst-case analysis of any algorithm will always yield such an estimate, because it

gives an upper bound on the running time

of the algorithm, that is

, and so

. In some cases, an exact analysis of the running time is possible, and then we get

, so

. Here is an example:

a <- 0 1 unit 1 time

for i <- 1 to n do 1 unit n times

for j <- 1 to i do 1 unit n(n+1)/2 times

a <- a+1 1 unit n(n+1)/2 times

where the times for the inner loop have been computed as follows: for each  from  to

, the loop

is executed  times, so the total number of times is

(see Appendix A.1 in the book for this, and other summation formulas). Hence in this case

If we write

7 , then

, that is

. We actually

write

, as recommended by the following rule.

Be as simple and as precise as possible in analysis.

Although the definitions of asymptotic notation allow one to write, for example,

, in practice we never write something like that; we simplify the function in between

the parentheses as much as possible (in terms of rate of growth), and write instead

.

Similarly, we don’t write, for example,

, but

A"B>C

. Also, it is

wrong to write, for instance,

ED

; write instead

, after computing the sum.

Also, the analysis should be as precise as possible, in the sense that it should provide the closest

asymptotic bounds for functions; for example, while it is true that!

"F

, we know that! is

actually constant time, rather than quadratic time, so!

GF

or!

is the right analysis.

In the spirit of the simplicity rule above, when we are to compare, for instance, two candidate

algorithms H and I having running times

J

K8L 3?

and

M18ONP>

.! , rather

than writing

JK QRMS

, we write

JK QTB

, and

M1 UT->V

, and then we

1

Mihaela Gheorghiu Spring 2002 CSCE-310 Data Struct. & Alg.

conclude that H is better than I , using the fact that

(quadratic) is better than

(cubic) time,

since

GF> 

. A comparison of the most common functions is given next.

Common functions.

The following relations hold for some of the most common functions encountered in algorithm

analysis (where “ ” means “better than”):

 4   4F 4   ; 4F 3  .F

The same relations hold for

instead of

. Also, the following general rules apply:

  1. logarithmic is better than polynomial:

F      

, for any constants   where

 ;

  1. lower-degree polynomial is better than higher-degree polynomial:

F  F 

, for any

constants   with  ;

  1. polynomial is better than exponential:



; 4F

 (

, for any constants   , with  .

The same rules apply for

instead of

.

Other things to prove about asymptotic bounds.

  1. Prove that 

G  2

iff



. Proof: By definition,



iff there exist constants 



! such that 





, for all

. Dividing by , we get 





, for all

. But this is just

the definition of



, in which 

.

  1. Prove that!

>80GF 3 

.

Proof: By contradiction. Suppose!

G

, Then, by definition, we have constants $





 such that !



, for all

. Dividing by

we get !

 , for all

. This

means that



, for all

. But this is obviously not true for

&(')*

%-,

.

  1. Fill in the most appropriate symbol from *

  .,

at position. Justify your answer.

(a)

   3       

;

(b) / ( 54 (

.

Solution: (a) Using the property of logarithms

 7698 ;: <

, we get

   3

, so

GB  

.

(b) Using the fact that /1032=

6

, we get / ( 54 (

/ ( ?>

, so / ( @4 (

(since

"

for all

A"

).

Read carefully Chapter 3 of the book, with all the details pertaining to



, including

Section 3.2. Be prepared to use formulas from this section, and from Appendix A of the book

at any time.

2