INDIAN INSTITUTE OF TECHNOLOGY ROORKEE
Data Structures CSN- 102 Dr. R. Balasubramanian Associate Professor Department of Computer Science and Engineering Indian Institute of Technology Roorkee Roorkee 247 667
[email protected] https://sites.google.com/site/balaiitr/
More Big-Oh Examples 7n-2 7n-2 is O(n) need c > 0 and n0 1 such that 7n-2 c•n for n n0 this is true for c = 7 and n0 = 1
3n3 + 20n2 + 5
3n3 + 20n2 + 5 is O(n3) need c > 0 and n0 1 such that 3n3 + 20n2 + 5 c•n3 for n n0 this is true for c = 4 and n0 = 21
3 log n + 5 3 log n + 5 is O(log n) need c > 0 and n0 1 such that 3 log n + 5 c•log n for n n0 this is true for c = 8 and n0 = 2 2
Big-Oh and Growth Rate • The big-Oh notation gives an upper bound on the growth rate of a function • The statement “f(n) is O(g(n))” means that the growth rate of f(n) is no more than the growth rate of g(n) • Useful to find the worst case of an algorithm
3
• Prove that
2
f ( n ) 5n 2 n 1
is
2
O (n )
4
• Write an efficient program to find whether given number is prime or not.
5
AKS algorithm • Agrawal, Kayal and Saxena from IIT Kanpur have come up with O(log k n) algorithm for a prime number program – PRIMES is in P, Annals of Mathematics, 160(2): 781-793, 2004 • Infosys Mathematics Prize, 2008. • Fulkerson Prize for the paper “PRIMES is in P”, 2006. • Gödel Prize for the paper “PRIMES is in P", 2006. • Several awards and prizes
6
Programming Contest Sites • • • • •
http://icpc.baylor.edu(ACM ICPC) http://www.codechef.com (Online Contest) http://www.topcoder.com http://www.spoj.com http://www.interviewstreet.com
7
Find the output of the following code
int n=32; steps=0; for (int i=1; i<=n;i*=2) steps++; cout<
8
• A log10 (n) , B log 2 (n) both are of O(log n), base doesn’t matter, • Suppose xm log m n f (n) and xk log k n g (n)
m
xm
m
xm
n and
k
k
xk
n
xk
x m x k log m k f ( n ) g ( n ) log m k cg ( n ) where c log m k f ( n ) cg ( n ) f ( n ) and g ( n ) are of O( log n)
-notation For function g(n), we define (g(n)), big-Omega of n, as the set: (g(n)) = {f(n) : positive constants c and n0, such that n n0, we have 0 cg(n) f(n)} Intuitively: Set of all functions whose rate of growth is the same as or higher than that of g(n). g(n) is an asymptotic lower bound for f(n). 10
• Prove that
2
f ( n ) 5n 2 n 1
is
2
(n )
11
Example (g(n)) = {f(n) : positive constants c and n0, such that n n0, we have 0 cg(n) f(n)} • n = (log n). Choose c and n0. for c=1 and n0 =16,
c * log n n
, n 16
12
13
Omega • Omega gives us a LOWER BOUND on a function. Big-Oh says, "Your algorithm is at least this good." Omega says, "Your algorithm is at least this bad."
14
-notation For function g(n), we define (g(n)), big-Theta of n, as the set: (g(n)) = {f(n) : positive constants c1, c2, and n0, such that n n0, we have 0 c1g(n) f(n) c2g(n)
} Intuitively: Set of all functions that have the same rate of growth as g(n).
g(n) is an asymptotically tight bound for f(n). 15
-notation For function g(n), we define (g(n)), big-Theta of n, as the set: (g(n)) = {f(n) : positive constants c1, c2, and n0, such that n n0, we have 0 c1g(n) f(n) c2g(n)
} Technically, f(n) (g(n)). Older usage, f(n) = (g(n)). I’ll accept either…
f(n) and g(n) are nonnegative, for large n. 16
Example (g(n)) = {f(n) : positive constants c1, c2, and n0, such that n n0, 0 c1g(n) f(n) c2g(n)} • 10n2 - 3n = (n2) • What constants for n0, c1, and c2 will work? • Make c1 a little smaller than the leading coefficient, and c2 a little bigger. • To compare orders of growth, look at the leading term. • Exercise: Prove that n2/2-3n= (n2)
17
Example (g(n)) = {f(n) : positive constants c1, c2, and n0, such that n n0, 0 c1g(n) f(n) c2g(n)}
• Is 3n3 (n4) ?? • How about 22n (2n)??
18
Examples 3n2 + 17 • (1), (n), (n2) lower bounds • O(n2), O(n3), ... upper bounds • (n2) exact bound
19
Relations Between O,
20
o-notation For a given function g(n), the set little-o: o(g(n)) = {f(n): c > 0, n0 > 0 such that n n0, we have 0 f(n) < cg(n)}. f(n) becomes insignificant relative to g(n) as n approaches infinity:
lim [f(n) / g(n)] = 0 n
g(n) is an upper bound for f(n) that is not asymptotically tight. Observe the difference in this definition from previous ones. Why? 21
o-notation •
f(n)=3n+2 is o(n2)
•
f(n)=17n3 + n2 logn
is o(n4)
22
-notation For a given function g(n), the set little-omega:
(g(n)) = {f(n): c > 0, n0 > 0 such that
n n0, we have 0 cg(n) < f(n)}.
f(n) becomes arbitrarily large relative to g(n) as n approaches infinity:
lim [f(n) / g(n)] = .
n
g(n) is a lower bound for f(n) that is not asymptotically tight. 23
-notation •
f(n)=3n+2 is
•
f(n)=17n3 +
n2
(1) is
2
(n )
24
Comparison of Functions fg ab f (n) = O(g(n)) f (n) = (g(n)) f (n) = (g(n)) f (n) = o(g(n)) f (n) = (g(n))
a a a a a
= < >
b b b b b
25