Growth of Functions - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Growth of Functions

Description:

Measuring Runtimes. Benchmarking - Code and run algorithm and measure running time ... Analysis determines the runtime of an algorithm based on the input size ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 30
Provided by: mahmood5
Category:

less

Transcript and Presenter's Notes

Title: Growth of Functions


1
Growth of Functions
  • Section 3.2

2
Algorithm
  • Any well-defined computational procedure that
    takes some value or set of values as input and
    produces some value or set of values as output.

3
Factors to be considered for choosing an algorithm
  • Simplicity - Easy to implement and obviously
    correct
  • Clarity - Easy to read and to maintain
  • Extensibility - Can easily be extended to new
    tasks
  • Reusability - Can be adapted to different tasks
  • Efficiency - Uses time and space well

4
Measuring Runtimes
  • Benchmarking - Code and run algorithm and measure
    running time
  • Requires implementation
  • Need to run a variety of typical inputs
  • Need to make sure that comparisons are done only
    across comparable platforms

5
Analysis
  • Analysis determines the runtime of an algorithm
    based on the input size
  • Gives you the relative rate of growth but not
    specific time/space of execution
  • Allows for comparison of algorithms without
    implementation
  • Not hardware specific
  • Describes the number of operations for a given
    input size, or the space required.

6
What is Input Size?
  • Depends on the problem being analyzed
  • The number of items in the input is often used
  • Number of values in a sorting algorithm
  • May use more than one parameter
  • Number of edges and vertices in a graph
  • Number of columns and rows in a matrix

7
Asymptotic notations
  • Asymptotic efficiency of algorithms
  • How does the running time of an algorithm
    increase as the input increases in size without
    bound?
  • Asymptotic notations
  • Define sets of functions that satisfy certain
    criteria and use these to characterize time and
    space complexity of algorithms

8
Big-O
  • Let f and g be functions from N or R to R. We say
    that f(x) is O(g(x)) if there are positive
    constants C and k such that
  • f(x) ? Cg(x)
  • whenever x gt k.
  • Read as f(x) is big-oh of g(x)

9
Big-O
C g(x)
f(x)
g(x)
x
k
10
Big-O
  • Big-O provides an upper bound on a function (to
    within a constant factor)
  • O(g(x)) is a set of functions
  • Commonly used notation
  • f(x) O(g(x))
  • Correct notation
  • f(x) ? O(g(x))
  • Meaningless statement
  • O(g(x)) f(x)

11
Example
  • Show that x22x1 is O(x2).

12
Example
  • Show that 2x33x21 is O(x3).

13
Example
  • Show that 7x2 is O(x3).

14
Properties of Big-O
  • If f(x) is O(g(x)) then g(x) grows at least as
    fast as f(x)
  • f(x) is O(g(x)) iff O(f(x)) ? O(g(x))
  • If f(x) is O(g(x)) and g(x) is O(f(x)) then
  • O(f(x))O(g(x))
  • If f(x) is O(g(x)) and h(x) is O(g(x)) then
  • (fh)(x) is O(g(x))

15
Properties of Big-O (Cont..)
  • If a is a scalar and f(x) is O(g(x)), then
  • af(x) is O(g(x))
  • If f(x) is O(g(x)) and g(x) is O(h(x)), then
  • f(x) is O(h(x))
  • f(x) O(g(x))
  • In practice, g(x) is chosen to be as small as
    possible

16
Common Complexity Functions
Complexity Term O(1) constant O(log
n) logarithmic O(n) linear O(n log
n) n log n O(nb) polynomial O(bn) b gt
1 exponential O(n!) factorial
17
Growth of Some Common Functions
18
Important Big-O Result
  • Let f(x)anxnan-1xn-1a1xa0, where a0, a1,
    ,an-1, an are real numbers. Then f(x) is O(xn).

19
Big-O Estimates
  • What is the big-O estimate for n!?

n! ? 1 2 3 n
? n! ? n n n n
? n! ? nn
n! is O(nn)
  • What is the big-O estimate for log(n!)?

n! ? nn
? log n! ? log nn
log n! is O(n log n)
? log n! ? n log n
20
Growth of Combinations of Functions
  • Addition of functions
  • If f1(x) is O(g1(x)) and f2(x) is
    O(g2(x)),
  • then (f1 f2)(x) is
    O(max(g1(x),g2(x))).
  • Example What is the complexity of the function
    n2 log(n!) ?

21
Growth of Combinations of Functions
  • Multiplication of functions
  • If f1(x) is O(g1(x)) and f2(x) is
    O(g2(x)),
  • then (f1f2)(x) is O(g1(x)g2(x)).
  • Example What is the complexity of the function
    3nlog(n!) ?

22
Big-Omega
  • f(x)O(g(x)) only provides an upper bound in
    terms of g(x) for f(x).
  • What do we do for a lower bound?
  • Big-Omega provides a lower bound for a function
    to within a constant factor.

23
Big-Omega
  • Let f and g be functions from N or R to R. We say
    that f(x) is ?(g(x)) if there are positive
    constants C and k such that
  • f(x) ? C g(x)
  • whenever x gt k.
  • Read as f(x) is big-Omega of g(x)

24
Big-Omega
f(x)
Cg(x)
g(x)
x
k
25
Example
  • Show that 8x35x27 is ?(x3).

26
Big-Theta
  • f(x)O(g(x)) only provides an upper bound in
    terms of g(x) for f(x).
  • f(x)?(g(x)) only provides a lower bound in terms
    of g(x) for f(x).
  • Big-Theta provides both an upper bound and a
    lower bound for a function in terms of a
    reference function g(x)

27
Big-Theta
  • Let f and g be functions from N or R to R. We say
    that f(x) is ?(g(x)) if f(x) is O(g(x)) and
    f(x) is ?(g(x)), i.e., there are positive
    constants C1, C2, and k such that
  • C1g(x) ? f(x) ? C2g(x)
  • whenever xgtk.
  • Read as f(x) is big-Theta of g(x)
  • f(x) is of order g(x)

28
Big-Theta
C2g(x)
f(x)
C1g(x)
x
k
29
Example
  • Show that 3x28xlogx is ?(x2).
Write a Comment
User Comments (0)
About PowerShow.com