CmSc 250 Intro to Algorithms


The Big-Oh and the other notations in Algorithm Analysis

Learning Goals
Exam-like problems


  1. Introduction
  2. The complexity of an algorithm is measured by the operations needed to solve the corresponding problem.
    We are concerned with estimating the complexity of algorithms, where the number of operations
    depends on the size of the input. Examples of such algorithms are:

      • reading a file: the number of read operations depends on the number of records in the file
      • finding a name in a list of names:
        the number of operations depend on the number of the names in the list
      • finding the greatest element in an array of elements:
        the number of operations depends on the length of the array.

    If N is the number of the elements to be processed by an algorithm (N is the size of the input)
    then the number of operations can be represented as a function of N: f(N)
    (sometimes we use small n)

    We can compare the complexity of two algorithms by comparing the corresponding functions.
    Moreover, we are interested what happens with the functions for large N,
    i.e. we are interested in the asymptotic growth of these functions.

    Classifying functions by their asymptotic growth

    Each particular growing function has its own speed of growing.
    Ssome functions grow faster, others grow slower.

    The question is: ? Is there a way to compare the rate of growth of two functions?

    The rate of growth of a function is called asymptotic growth.
    We can compare functions by studying their asymptotic growth.

  3. Background theory
  4. Given a function f(n), all other functions fall into three classes:

    1. growing with the same rate as f(n)
    2. growing faster than f(n)
    3. growing slower than f(n)

    • CASE 1: f(n) and g(n) have the same rate of growing, if
    • lim(f(n)/g(n)) = c, 0 < c < ∞, n

      Notation: f(n) = Θ(g(n)) , pronounced "theta"

       

    • CASE 2: f(n) grows slower than g(n) (i.e g(n) grows faster than f(n)) if
    • lim(f(n)/g(n)) = 0, n → ∞

      Notation: f(n) = o(g(n)), pronounced "little oh"

       

    • CASE 3: f(n) grows faster than g(n) ( i.e. g(n) grows slower than f(n)) if
    • lim(f(n)/g(n)) = , n → ∞

      Notation: f(n) = ω (g(n)), pronounced "little omega"

  5. Discussion
    1. Case 2 and Case 3.
    2. Obviously, case 2 is the reverse of case 3:

      if

      g(n) = o( f(n) ),
      then
      f(n) = ω( g(n) )

      Examples :

      Compare n and n2

      lim( n/n2 ) = 0, n → , hence n = o(n2),

      lim( n2/n ) = , n → , hence n2 = ω(n)

      Compare n and log n

      lim( (log n) / n ) = 0, n → , hence log n = o(n),

      lim( n / log n ) = , n → , hence n = ω (log n),

    3. Case 1 in details
    4. f(n) and g(n) have the same rate of growth if

      lim( f(n)/g(n) ) = c, 0 < c < , n

      Let Θ( f(n) ) be the set of all functions, that grow with the rate of f(n).
      If g(n) has the same rate of growth, then g(n) є Θ( f(n) ).

      Consider the properties of the relation: "having the same rate of growth":

      1. The relation is symmetric:
        If g(n) grows with the same rate as f(n), then f(n) grows with the same rate as g(n),
        hence if f(n) = Θ(g(n)) then g(n) = Θ(f(n))

      2. The relation is transitive:
        Obviously, if g(n) = Θ(f(n)) and f(n) = Θ(h(n)) then g(n) = Θ(h(n))
      3. The relation is reflexive:
        g(n) = Θ(g(n))

      Hence this is a relation of equivalence and it gives a partition over the set of all functions - classes of equivalence.
      Thus functions in one and the same class are equivalent with respect to their growth.

    The important result is:

    Two algorithms have same complexity,
    if the functions representing the number of operations have same rate of growth,
    i.e. if they belong to one and the same class of equivalence.
    Among all functions in a given class of equivalence we choose the simplest one
    to represent the complexity for that class.

    Note: In Theory of Algorithms we use '=' instead of 'є' : g(n) = Θ(f(n))

    Here are some examples:

    1. Compare n and (n+1)/2
    2. lim( n/((n+1)/2)) = 2, same rate of growth.

      (n+1)/2 = Θ(n) - rate of growth of a linear function

    3. Compare n2 and n2 + 6n

    4. lim( n2 / (n2 + 6n) ) = 1, same rate of growth.

      n2 + 6n = Θ(n2) - rate of growth of a quadratic function

    5. Compare log n and log n2
    6. lim( log n / log n2 ) = lim( log n / 2log n) = 1/2, same rate of growth.

      log n2 = Θ(log n) - rate of growth of a logarithmic function

    Other examples:

      1. All cubic functions, such as n3, 5n3+ 4n, 105n3+ 4n2 + 6n, have same rate of growth: Θ(n3)
      2. All quadratic functions, such as n2, 5n2+ 4n + 6, n2 + 5, have same rate of growth: Θ(n2)
      3. All logarithmic functions, such as log n , log n2 , log (n + n3), have same rate of growth: Θ(log n)

    Compare n2 and n2 + log n2

    They have same rate of growth: Θ(n2). Here is how we show this:

    lim(n2 /(n2+ log n2)) = lim(n2 /(n2+ 2log n)) = lim(2n /(2n+ (2loge)/n)) =

    lim(n /((n2+ loge)/n))= lim(n2 /(n2+ loge))= lim(2n /(2n))= lim(2/2) = 1

    (L'Hopital's rule has been used to find the limit. (log n)' = (log e)/n )

    Thus, for any two functions f(n) and g(n) one of the following is true:

    a. same rate of growth: g(n) = Θ(f(n))

    b. different rate of growth:

    either g(n) = o(f(n)), g(n) grows slower than f(n), and hence f(n) = ω(g(n))

    or g(n) = ω(f(n)), g(n) grows faster than f(n), and hence f(n) = o(g(n))

  6. The Big-Oh notation
  7. The Big-Oh notation is used to simplify our reasoning about growth rates.

    f(n) = O( g(n) ) if f(n) grows with the same rate or slower than g(n).

    i.e. if f(n) = Θ(g(n)) or f(n) = o(g(n)), then we write f(n) = O(g(n))

    Thus n+5 = Θ(n) = O(n) = O(n2) = O(n3) = O(n5)

    While all the equalities are technically correct, we would like to have the closest estimation:

    n+5 = Θ(n)

    However, the general practice is to use the Big-Oh notation and to write:

    n+5 = O(n)

  8. The Big-Omega notation
  9. Big-Omega notation is the inverse of the Big-Oh:

    If g(n) = O(f(n)), then f(n) = Ω (g(n))

    Here we say that f(n) grows faster or with the same rate as g(n), and write

    f(n) = Ω(g(n))

    In the analysis of algorithms we shall use mainly the Big-Oh estimate.

  10. Big-Oh rules
  11. Let T(N) (and also T1(N), T2(N)) denote the run time of an algorithm for input of size N

    Rule 1:

    1. If T1(N) = O( f(N) ) and T2(N) = O( g(N) ) then

      T1(N) + T2(N) = max( O( f(N) ), O( g(N) ))
      where max( O( f(N) ), O( g(N) )) is the faster growing function.

    2. If T1(N) = O( f(N) ) and T2(N) = O( g(N) ) then

      T1(N) * T2(N) = O( f(N) * g(N) )

    Rule 2: If T(N) is a polynomial of degree k, then T(N) = Θ(Nk)

    Rule 3: logkN = O(N) for any constant k.

    Examples:

    x2 = O(x2), actually x2 = Θ (x2)

    2x2 = O(2x2) = O(x2), we do not consider coefficients

    x2 + x = O(x2), we disregard any lower-order term

    xlog(x) = O(xlog(x))

    Typical growth rates:

    C

    constant, we write O(1)

    logN

    logarithmic

    log2N

    log-squared

    N

    linear

    NlogN

     

    N2

    quadratic

    N3

    cubic

    2N

    exponential

    N!

    factorial


The following figure illustrates the notations above:


Summary

For a given function f(n) we have;

Θ(f(n))

the set of all function with same rate of growth as f(n)

o(f(n))

is the set of all functions that grow much slower than f(n)

ω(f(n))

the set of all functions that grow much faster than f(n)

O(f(n))

Θ(f(n)) È o(f(n)) same or slower

Ω(f(n))

Θ(f(n)) È ω(f(n)) same or faster


Learning goals

  1. To be able to compare the rate of growth of polynomial, logarithmic and exponential functions
  2. To know the meaning of the five notations: Theta, Big-Oh, little oh, Big Omega, little omega.

Exam-like questions and problems with solutions

  1. Compare NlogN and N2 : NlogN = O(N2) or N2 = O(NlogN) ?
  2. Is it true that NlogN = o(N2) ?
  3. Compare NlogN and N: NlogN = O(N) or N = O(NlogN) ?
  4. Is it true that NlogN = o(N) ?
  5. N2 + NlogN = O(….. )
  6. N + NlogN = O(….. )
  7. N2 + logN = O(….. )
  8. Find a function f(N) such that N2 = o(f(N))
  9. Find a function g(N) such that N2 = ω(g(N))
  10. Arrange the following functions in order of increasing rate of growth.
    Identify any functions with the same rate of growth.
  11. n2, 2n, nlogn, n!, n6 + n2, n4, n6 - n3, 4n, n, nn, 22n.


Back to Contents page
Created by Lydia Sinapova