Sunteți pe pagina 1din 14

Estimating Running Time

for Input of Size n


If run time then doubling input
grows like: size does following For example:
to run time:
n

doubles

1 hr. 2 hrs.

n2

multiplies by 4

1 hr. 4 hrs.

n3

multiplies by 8

1 hr. 8 hrs.

log2 n

increase by
small constant

1 hr.
1 hr. +

2n

squares it

26 212

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

Lectures 1,2 - Carla Savage


CSC 505, NCSU

Time Complexity/Running Time


(assuming 106 operations per second)
Problem size
Time complexity
of algorithm
32 log n

103

106

109

1012

.0001 sec. .0002 sec. .0003 sec. .0004 sec.

16n

.016 sec.

16 sec.

4.4 hrs.

185 days

8n log n

.024 sec.

48 sec

20 hrs

3 yrs.

4n2

4 sec.

46 days

1,268 cent.

2n3

33 min.

634 cent.

2n

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

Lectures 1,2 - Carla Savage


CSC 505, NCSU

Growth Classes
of functions (asymptotically nonnegative)
defined on positive integers
Informally:
O(g(n)): fcns. which grow no faster than g(n)

(g(n)): fcns. which grow no slower than g(n)

(g(n)): functions which grow at the same


rate as g(n)

o(g(n)): functions which grow slower than g(n)

(g(n)): functions which grow faster than g(n)

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

Lectures 1,2 - Carla Savage


CSC 505, NCSU

Big-O
f (n) O(g(n)) iff there exist c, n0 > 0
such that
0 f (n) cg(n)
for all n n0.
Big-Omega
f (n) (g(n)) iff there exist c, n0 > 0
such that
f (n) cg(n) 0
for all n n0.
Big-Theta
(g(n)) = O(g(n)) (g(n))

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

Lectures 1,2 - Carla Savage


CSC 505, NCSU

(1) n2 + 2n + lg n O(n2)
Proof.

(2) 2n3 3n2 + 2n (n3)


Proof.

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

Lectures 1,2 - Carla Savage


CSC 505, NCSU

(3) Show n3 10n2 6 O(n2).

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

Lectures 1,2 - Carla Savage


CSC 505, NCSU

(4) Show ln n (lg n)


ln n = loge n
lg n = log2 n
loga n = logb n / logb a

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

Lectures 1,2 - Carla Savage


CSC 505, NCSU

Little-o
f (n) o(g(n)) iff for every c > 0 there is
an n0 > 0 such that
0 f (n) < cg(n)
for all n n0.
Little-omega
f (n) (g(n)) iff g(n) o(f (n)).

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

Lectures 1,2 - Carla Savage


CSC 505, NCSU

(5) Show 50n2 + 7 o(n3)


Proof.

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

Lectures 1,2 - Carla Savage


CSC 505, NCSU

Note:
If :

lim

f (n)

n g(n)

=0

then: f (n) o(g(n)).

If :

lim

f (n)

n g(n)

then: f (n) (g(n)).

If :

f (n)

lim

n g(n)

=d>0

then: f (n) (g(n)).

WARNING: Converses not necessarily true since


limits may not exist.

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

10

Lectures 1,2 - Carla Savage


CSC 505, NCSU

(6) Show 2n o(3n)

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

11

Lectures 1,2 - Carla Savage


CSC 505, NCSU

(7) 10n5 n4 + 7 (6n5)


(but 10n5 n4 + 7 6 o(6n5)
and 10n5 n4 + 7 6 (6n5).)
Proof.

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

12

Lectures 1,2 - Carla Savage


CSC 505, NCSU

LHopitals Rule
If s and t are continuous and differentiable functions on the positive real numbers and if

lim s(n) = and

lim t(n) =

or if
lim s(n) = 0 and

then
lim

s(n)

n t(n)

lim t(n) = 0

= lim

s0(n)

n t0(n)

provided that limit exists.

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

13

Lectures 1,2 - Carla Savage


CSC 505, NCSU

(8) ln n o(n1/t) for any t > 0.

(9) en (nt) for any t 1.

Introduction to Algorithms, Second Ed.


by Cormen, Leiserson, Rivest, and Stein

14

Lectures 1,2 - Carla Savage


CSC 505, NCSU

S-ar putea să vă placă și