0% found this document useful (0 votes)
47 views14 pages

Lect02 102 2019

f(n) and g(n) are both O(log n) as their growth rates are the same up to a constant factor.

Uploaded by

Mukesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views14 pages

Lect02 102 2019

f(n) and g(n) are both O(log n) as their growth rates are the same up to a constant factor.

Uploaded by

Mukesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

INDIAN INSTITUTE OF TECHNOLOGY ROORKEE

Data Structures
CSN- 102

Dr. R. Balasubramanian
Professor
Department of Computer Science and Engineering
Indian Institute of Technology Roorkee
Roorkee 247 667
[email protected]
https://sites.google.com/site/balaiitr/
Five Important Guidelines for finding Time
Complexity in a code
1. Loops
2. Nested Loops
3. Consecutive Statements
4. if –else statements
5. Logarithmic statements

2
Asymptotic Notations

• O, , , o, 
• Defined for functions over the natural numbers.
– Ex: f(n) = (n2).
– Describes how f(n) grows in comparison to n2.
• Define a set of functions; in practice used to
compare two function sizes.
• The notations describe different rate-of-growth
relations between the defining function and the
defined set of functions.

3
Asymptotic Notation

• Big Oh notation (with a capital letter O, not a zero), also


called Landau's symbol, is a symbolism used in
Computational Complexity Theory, Computer Science, and
Mathematics to describe the asymptotic behavior of
functions. Basically, it tells you how fast a function grows or
declines.

• Landau's symbol comes from the name of the German


number theoretician Edmund Landau who invented the
notation. The letter O is used because the rate of growth of a
function is also called its order.

4
Big-Oh Notation (Formal Definition)
• Given functions f(n) and g(n), we say that f(n) is O(g(n)) if
there are positive constants
c and n0 such that
f(n)  cg(n) for n  n0
• Example: 2n  10 is O(n)
2n  10  cn
(c  2) n  10
n  10(c  2)
Pick c 3 and n0 10

5
Big-Oh Example

• Example: the function n2 is not O(n)


n2  cn
nc
The above inequality cannot be satisfied since c must be
a constant
n2 is O(n2).

6
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

7
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

8
2 2
• Prove that f ( n )  5n  2 n  1 is O (n )

9
• Write an efficient program to find whether given number is
prime or not.

10
AKS algorithm

• Agrawal, Kayal and Saxena from IIT Kanpur come up with


an algorithm for a prime number program
O (log k n)

– 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

11
Programming Contest Sites

• https://icpc.baylor.edu/regionals/finder/world-finals-2019 (ACM
ICPC)
– Team “Hold right there Sparky” of IITR is in the World finals 2019
– Team “Triangulation” of IITR was in the World finals 2018
• http://www.codechef.com (Online Contest)
• http://www.topcoder.com
• http://www.spoj.com
• http://www.interviewstreet.com

12
Find the output of the following code

int n=32;
steps=0;
for (int i=1; i<=n;i*=2)
steps++;
cout<<steps;

• Example of O(log n) algorithm.

13
• 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)
xm xk
m  n and k n
xm xk
m k
 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)

You might also like