Lecture 15 Complexity of Functions - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Lecture 15 Complexity of Functions

Description:

Previous example show that functions grow at different rates ... This allows us to compare algorithms for a specific task. For example: bubble sort (n2) ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 20
Provided by: Bill1173
Category:

less

Transcript and Presenter's Notes

Title: Lecture 15 Complexity of Functions


1
Lecture 15Complexity of Functions
  • CSCI 1900 Mathematics for Computer Science
  • Spring 2012
  • Bill Pine

2
Lecture Introduction
  • Reading
  • Kolman Sections 5.2, 5.3
  • Examine some functions occurring frequently in
    Computer Science
  • Characterize the efficiency of an algorithm
  • Big-Oh
  • Big-Theta

3
Functions in CS
  • mod n function
  • fn( m ) m mod n
  • Example
  • f3( 7 ) 1 f3( 8 ) 2 f3( 111 ) 0
  • Factorial function
  • f ( n ) n! n! n (n-1)!
  • f ( 0 ) 1 f ( 1 ) 1 f ( 2 ) 2 f ( 3 ) 6f
    ( 10 )gt 106 f ( 20 )gt1018 f (70) gt 10100

4
Functions in CS (cont)
  • Floor function
  • f ( x ) largest integer x
  • f (2.1) 2
  • f (2.9) 2
  • f (2.999999) 2
  • f (3) 3
  • f (-2.3) -3 Nota Bene -2 gt -2.3

5
Functions in CS (cont)
  • Ceiling function
  • f( x ) smallest integer x
  • f( 2.000001 ) 3
  • f( 2.9 ) 3
  • f( 2.999999 ) 3
  • f( 3 ) 3
  • f( 3.000001 ) 4
  • f( -2.3 ) -2 Nota Bene -2 gt -2.3

6
Functions in CS (cont)
  • Exponential
  • f ( x )2x
  • f ( 1 )2
  • f ( 2 )4
  • f ( 2.2 )4.4957
  • f ( 0 )1
  • f ( -1 )1/2
  • f ( -2 )1/4

7
Functions in CS (cont)
  • Logarithm
  • fn( x )logn(x) the power to which n must
    be raised to yield x
  • f2( 4 ) 2
  • f2( 8 ) 3
  • f2( 2 ) 1
  • f2( 1 ) 0
  • f2( 1/2 ) -1
  • f2( 1/16 ) -4
  • f2( 0 ) undefined

8
Growth of Functions
  • Previous example show that functions grow at
    different rates
  • Specifically, let f (x) x, g(x) 2x
  • f (1) 1, g(1) 2
  • f (10) 10, but g (10) 1024
  • Polynomial functions
  • n n2 n2.2 n3

9
Growth of Functions (cont)
  • All log functions grow at the same rate,
    regardless of the base
  • log2 ( n ) grows at the same rate as log100 ( n )
  • We usually write log2 as lg
  • Recall logn( k ) logm( k ) / logm( n )
  • Powers of the log function
  • (log n ) (log n)2 (log n)304 n

10
Growth of Functions (cont)
  • log compared to polynomials
  • lg n n n lg( n ) n ( lg( n ) )2 n
    (lg( n )1670 n2
  • Polynomials compare to exponentials
  • n2 n3 n2876 2n
  • Exponentials compared to factorial
  • 2n n!

11
Growth of Functions (cont)
  • In general, the classes of functions, in order of
    increasing running time are
  • constant log( n ) n n log( n )
    n2 n3 2n n!

12
Big-Oh Notation
  • f ? O( g ) if there exist two constants, c and n0
    such that f (n) ? c g(n) for all n ? n0
  • This means f grows no faster than g does
  • What is in O(n3)?
  • Anything that grows no faster than n3
  • f ( n ) 10n3
  • f ( n ) n3 100n2 n
  • f ( n ) n2
  • f ( n ) n lg n
  • f ( n ) lg n
  • f ( n ) 2

13
Big-Oh Example
  • Show that n3 100n2 20 ? O( n3 )
  • Need to find a c and n0 such that c n3 gt n3
    100n2 n for all n gt n0
  • Two important consequences of f ? O( g )
  • g serves as an upper bound on f
  • We ignore the behavior for small values of n

14
Big-Theta Notation
  • f ? ? ( g ), if f and g have same order
  • f ? O( g ) and g ? O( f )
  • What is in ?( n3 )
  • f ( n ) n3 100n2 n is in ?( n3 )
  • f ( n ) 2 is not in ?( n3 )
  • f ( n ) n2 is not in ?( n3 )
  • f ( n ) lg n is not in ?( n3 )
  • f ( n ) n lg n is not in ?( n3 )

15
Running Time
  • The ?-class of a function that describes the
    number of steps performed by an algorithm is
    called the running time of the algorithm
  • This allows us to compare algorithms for a
    specific task
  • For example
  • bubble sort ? ?( n2 )
  • quicksort ? ?( n lg n)
  • What is the fastest running-time for a
    comparison-based sorting algorithm?

16
Determining Running Time
  • To compare two algorithms running times
  • Select an operation that is central to the
    algorithm
  • For searching, we might choose comparison
  • For sorting, we might choose comparison or
    perhaps data saving
  • Examine the algorithm to determine how the count
    of the key operation depends upon input size
  • To evaluate a single algorithm
  • Count the total number of operations
  • Examine the algorithm to determine how the count
    of the key operation depends upon input size

17
Running Time Example
  • Consider the following function
  • function meanOf( n )
  • sum ? 0
  • for i 1 thru n
  • sum ? sum i
  • mean sum / n
  • return mean

18
Running Time Example (cont)
  • What is the input size?
  • Identify and count the characterizing operation
  • Number of pre-loop operations
  • Number of operations in the loop
  • Number of post-loop operations
  • What is the running time?
  • Is there a more efficient way to determine meanOf
    ?

19
Key Concepts Summary
  • Examined some functions useful in Computer
    Science
  • Characterize the efficiency of an algorithm
  • Big-Oh
  • Big-Theta
  • Reading for next time
  • Kolman Sections 7.1, 7.2
Write a Comment
User Comments (0)
About PowerShow.com