Professional Documents
Culture Documents
Data Structures
CSN- 102
Dr. R. Balasubramanian
Professor
Department of Computer Science and Engineering
Indian Institute of Technology Roorkee
Roorkee 247 667
balarfcs@iitr.ac.in
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
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
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
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
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;
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)