Docsity

Understanding Asymptotic Notations & Time Complexity: Big O, Worst, Best, Average Cases, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Understanding Asymptotic Notations & Time Complexity: Big O, Worst, Best, Average Cases and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Time complexity

Asymptotic Notations

Worst/ best/average cases

  • (^) Worst case is the longest running time for any input of size n
  • (^) O-notation represents upper bound I.e. an upper bound for worst case.
  • (^) Best case is the input data set that results in best possible performance.You cannot do better. This is lower bound.
  • (^) Average case is the average performance
  • (^) A non negative function T(n) is said to be O(f(n)) provided there exists a constant c>0 and an integer n 0 > 0 such that
  • (^) T(n)c. f(n) for all integer n > n 0
  • (^) Consider 1/3 n^2 – 5n
  • (^) The dominating term is n^2
  • (^) Therefore it should be of O(n^2 )
  • (^) Given a positive constant c , a positive integer n 0 to be found such that
  • (^) 1/3 n^2 - 5 nc n^2
  • (^) Suppose T(n) = 1/3 n^3 + ½ n^2 + 1/6 n
  • (^) So T(n) (1/3 n^3 ) which can be proved as follows
  • (^) 1/3 n^3 + ½ n^2 + 1/6 nc 1/3 n^3
  • (^) 1/3 + 1/(2n) + 1/(6n^2 )c/
  • (^) Inequality is valid for case c3 and integers n1
  • (^) O( g ) - the set of functions that grow no faster than g.
  • (^) g(n) describes the worst case behaviour of an algorithm that is O( g )
  • (^) O( g ) - the set of functions that grow no faster than g.
  • (^) g(n) describes the worst case behaviour of an algorithm that is O( g )
  • (^) Two additional notations

 ( g )

  • the set of functions, f, such that f(n) > c g(n) for some constant, c , and n > N g is a lower bound for f

Properties of the O notation

  • (^) Constant factors may be ignored  (^) k > 0 kf  is O( f)

Properties of the O notation

  • (^) Constant factors may be ignored  (^) k > 0 kf  is O( f)
  • (^) Higher powers grow faster
    • (^) n r^  is O( n s ) if 0  r  s

Properties of the O notation

  • (^) Constant factors may be ignored  k > 0 kf  is O( f)
  • (^) Higher powers grow faster
    • (^) n r^  is O( n s ) if 0  r  s  (^) Fastest growing term dominates a sum
    • (^) If f is O(g) , then f + g is O(g) eg an^4 + bn^3 is O(n^4 )  (^) Polynomial’s growth rate is determined by leading term
    • (^) If f is a polynomial of degree d , then f is O(nd)

Properties of the O notation

  • (^) f is O(g) is transitive
    • (^) If f is O(g) and g is O(h) then f is O(h)

Properties of the O notation

  • (^) f is O(g) is transitive
    • (^) If f is O(g) and g is O(h) then f is O(h)
  • (^) Product of upper bounds is upper bound for the product - (^) If f is O(g) and h is O(r)  then fh is O(gr)
  • (^) Exponential functions grow faster than powers
    • (^) n k^  is O( b n^ )  b > 1 and k  0 eg n^20 is O( 1.05 n)

Properties of the O notation

  • (^) f is O(g) is transitive
    • (^) If f is O(g) and g is O(h) then f is O(h)
  • (^) Product of upper bounds is upper bound for the product
    • (^) If f is O(g) and h is O(r)  then fh is O(gr)
  • (^) Exponential functions grow faster than powers
    • (^) n k^  is O( b n^ )  b > 1 and k  0 eg n^20 is O( 1.05 n)
  • (^) Logarithms grow more slowly than powers
    • (^) log bn  is O( n k )  b > 1 and k  0 eg log 2 n is O( n 0.5 )

Properties of the O notation

  • (^) All logarithms grow at the same rate
    • (^) log bn^ is^ O( log dn)^ ^ b, d > 1

Properties of the O notation

  • (^) All logarithms grow at the same rate
    • (^) log bn^ is^ O( log dn)^ ^ b, d > 1
  • (^) Sum of first n rth^ powers grows as the (r+ 1 )th^ power

 kr^  is  ( nr +1^ )

eg  i = is ( n^2 )

k = n k = n n(n+ 1 ) 2