Você está na página 1de 28

Big O: A Review

Pat Morin COMP2402/2002

Carleton University

Pat Morin COMP2402/2002

Big O: A Review

Big O: Denition

O (g (n)) = {f (n) : there exists positive constants c and n0 such that f (n) cg (n) for all n n0 }

Pat Morin COMP2402/2002

Big O: A Review

Big O: Denition

O (g (n)) = {f (n) : there exists positive constants c and n0 such that f (n) cg (n) for all n n0 } Notice: O (g (n)) is a set of functions

Pat Morin COMP2402/2002

Big O: A Review

Big O: Denition

O (g (n)) = {f (n) : there exists positive constants c and n0 such that f (n) cg (n) for all n n0 } Notice: O (g (n)) is a set of functions
When we say f (n) = O (g (n)) we really mean f (n) O (g (n))

Pat Morin COMP2402/2002

Big O: A Review

Big O: Denition

O (g (n)) = {f (n) : there exists positive constants c and n0 such that f (n) cg (n) for all n n0 } Notice: O (g (n)) is a set of functions
When we say f (n) = O (g (n)) we really mean f (n) O (g (n)) E.g., n2 + 42n + 7 = O (n2 ) means:
The function f (n) = n2 + 42n + 7 is in the set O (n2 )

Pat Morin COMP2402/2002

Big O: A Review

n2 + 42n + 7 = O (n2 )

n2 + 42n + 7 2n2 for all n 50


Pat Morin COMP2402/2002 Big O: A Review

Example

Prove n2 + 42n + 7 = O (n2 )

Pat Morin COMP2402/2002

Big O: A Review

Example

Prove n2 + 42n + 7 = O (n2 ) n2 + 42n + 7 n2 + 42n2 + 7n2 = 50n


2

for n 1

Pat Morin COMP2402/2002

Big O: A Review

Example

Prove n2 + 42n + 7 = O (n2 ) n2 + 42n + 7 n2 + 42n2 + 7n2 = 50n


2

for n 1

So, n2 + 42n + 7 50n2 for all n 1

Pat Morin COMP2402/2002

Big O: A Review

Example

Prove n2 + 42n + 7 = O (n2 ) n2 + 42n + 7 n2 + 42n2 + 7n2 = 50n


2

for n 1

So, n2 + 42n + 7 50n2 for all n 1 n2 + 42n2 + 7n2 = O (n2 ) [ c = 50, n0 = 1 ]

Pat Morin COMP2402/2002

Big O: A Review

Example

Prove 5n log2 n + 8n 200 = O (n log2 n)

Pat Morin COMP2402/2002

Big O: A Review

Example

Prove 5n log2 n + 8n 200 = O (n log2 n) 5n log2 n + 8n 200 5n log2 n + 8n 5n log2 n + 8n log2 n 13n log2 n

for n 2 (log2 n 1)

Pat Morin COMP2402/2002

Big O: A Review

Example

Prove 5n log2 n + 8n 200 = O (n log2 n) 5n log2 n + 8n 200 5n log2 n + 8n 5n log2 n + 8n log2 n 13n log2 n 5n log2 n + 8n 200 13n log2 n for all n 2

for n 2 (log2 n 1)

Pat Morin COMP2402/2002

Big O: A Review

Example

Prove 5n log2 n + 8n 200 = O (n log2 n) 5n log2 n + 8n 200 5n log2 n + 8n 5n log2 n + 8n log2 n 13n log2 n 5n log2 n + 8n 200 13n log2 n for all n 2 5n log2 n + 8n 200 = O (n log2 n) [ c = 13, n0 = 2 ]

for n 2 (log2 n 1)

Pat Morin COMP2402/2002

Big O: A Review

Some common relations


O (nc1 ) O (nc2 ) for any c1 < c2 For any constants a, b , c > 0, O (a) O (log n) O (nb ) O (c n )

Pat Morin COMP2402/2002

Big O: A Review

Some common relations


O (nc1 ) O (nc2 ) for any c1 < c2 For any constants a, b , c > 0, O (a) O (log n) O (nb ) O (c n ) These make things faster 2 log2 n + 2 = O (log n) n + 2 = O (n) 2n + 15n1/2 = O (n)

Pat Morin COMP2402/2002

Big O: A Review

Some common relations


O (nc1 ) O (nc2 ) for any c1 < c2 For any constants a, b , c > 0, O (a) O (log n) O (nb ) O (c n ) These make things faster 2 log2 n + 2 = O (log n) n + 2 = O (n) 2n + 15n1/2 = O (n) We can multiply these to learn about other functions, O (an) = O (n) O (n log n) O (n1+b ) O (nc n )

Pat Morin COMP2402/2002

Big O: A Review

Some common relations


O (nc1 ) O (nc2 ) for any c1 < c2 For any constants a, b , c > 0, O (a) O (log n) O (nb ) O (c n ) These make things faster 2 log2 n + 2 = O (log n) n + 2 = O (n) 2n + 15n1/2 = O (n) We can multiply these to learn about other functions, O (an) = O (n) O (n log n) O (n1+b ) O (nc n ) Examples: O (n1.5 ) O (n1.5 log n)
Pat Morin COMP2402/2002 Big O: A Review

An indulgence

In this course, we have seen expressions like O (n i )


Two argument function g (n, i ) = n i For the purposes of this course, we will take O (g (n, i )) to be O (g (n, i )) = {f (n, i ) : there exists positive constants c and n0 such that f (n, i ) cg (n, i ) for all n n0 and all valid arguments i }

For example (Lists) valid values of i are {0, . . . , n 1} or (sometimes) {0, . . . , n}

Pat Morin COMP2402/2002

Big O: A Review

Why Use big-O Notation?


Consider the following (simple) code:
for ( int i = 0; i < n ; i ++) { a[i] = i; }

Pat Morin COMP2402/2002

Big O: A Review

Why Use big-O Notation?


Consider the following (simple) code:
for ( int i = 0; i < n ; i ++) { a[i] = i; }

The running time is

Pat Morin COMP2402/2002

Big O: A Review

Why Use big-O Notation?


Consider the following (simple) code:
for ( int i = 0; i < n ; i ++) { a[i] = i; }

The running time is


1 assignment (int i = 0)

Pat Morin COMP2402/2002

Big O: A Review

Why Use big-O Notation?


Consider the following (simple) code:
for ( int i = 0; i < n ; i ++) { a[i] = i; }

The running time is


1 assignment (int i = 0) n+1 comparisons (i < n)

Pat Morin COMP2402/2002

Big O: A Review

Why Use big-O Notation?


Consider the following (simple) code:
for ( int i = 0; i < n ; i ++) { a[i] = i; }

The running time is


1 assignment (int i = 0) n+1 comparisons (i < n) n increments (i++)

Pat Morin COMP2402/2002

Big O: A Review

Why Use big-O Notation?


Consider the following (simple) code:
for ( int i = 0; i < n ; i ++) { a[i] = i; }

The running time is


1 assignment (int i = 0) n+1 comparisons (i < n) n increments (i++) n array oset calculations (a[i])

Pat Morin COMP2402/2002

Big O: A Review

Why Use big-O Notation?


Consider the following (simple) code:
for ( int i = 0; i < n ; i ++) { a[i] = i; }

The running time is


1 assignment (int i = 0) n+1 comparisons (i < n) n increments (i++) n array oset calculations (a[i]) n indirect assignments (a[i] = i)

Pat Morin COMP2402/2002

Big O: A Review

Why Use big-O Notation?


Consider the following (simple) code:
for ( int i = 0; i < n ; i ++) { a[i] = i; }

The running time is


1 assignment (int i = 0) n+1 comparisons (i < n) n increments (i++) n array oset calculations (a[i]) n indirect assignments (a[i] = i) = a + b (n + 1) + cn + dn + en, where a, b , c , d , and e are constants that depend on the machine running the code

Pat Morin COMP2402/2002

Big O: A Review

Why Use big-O Notation?


Consider the following (simple) code:
for ( int i = 0; i < n ; i ++) { a[i] = i; }

The running time is


1 assignment (int i = 0) n+1 comparisons (i < n) n increments (i++) n array oset calculations (a[i]) n indirect assignments (a[i] = i) = a + b (n + 1) + cn + dn + en, where a, b , c , d , and e are constants that depend on the machine running the code

Easier just to say O (n) (constant-time) operations

Pat Morin COMP2402/2002

Big O: A Review

Você também pode gostar