You are on page 1of 24

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
balarfcs@iitr.ac.in
https://sites.google.com/site/balaiitr/
 -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).

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

3
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

4
5
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."

6
-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).

7
-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.

8
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)

9
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)??

10
Examples

3n2 + 17

• (1), (n), (n2)  lower bounds

• O(n2), O(n3), ...  upper bounds

• (n2)  exact bound

11
Relations Between O, 

12
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?
13
o-notation

• f(n)=3n+2 is o(n2)

• f(n)=17n3 + n2 logn is o(n4)

14
 -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.

15
 -notation
• f(n)=3n+2 is  (1)
2
• f(n)=17n3 + n2 is  (n )

16
Comparison of Functions

fg  ab

f (n) = O(g(n))  a  b
f (n) = (g(n))  a  b
f (n) = (g(n))  a = b
f (n) = o(g(n))  a < b
f (n) =  (g(n))  a > b

17
Limits

• lim [f(n) / g(n)] = 0  f(n)  (g(n))


n

• lim [f(n) / g(n)] <   f(n)  (g(n))


n

• 0 < lim [f(n) / g(n)] <   f(n)  (g(n))


n

• 0 < lim [f(n) / g(n)]  f(n) (g(n))


n

• lim [f(n) / g(n)] =   f(n)  (g(n))


n

• lim [f(n) / g(n)] undefined can’t say


n

18
All Asymptotic Notations

T (n)  n 3  3n 2  4n  2

T ( n )  16 n  log n

19
Time Complexity

• We analyze time complexity for


a) A very large input size
b) Worst case scenario
• Rules
a) Drop lower order terms
b) Drop Constant multipliers

20
Time Complexity Calculation in a fragment
of a code
int a; //Example 1
a=5;
a++;
for (int i=0; i<n; i++)
{Simple statements;} 2
for (int j=0; j<n;j++) O(n )
{
for (int k=0; k<n;k++)
{Simple statements;}
}

21
int main() //Example 2
{const int n=100;
int arr[n];
for (int i=0; i<n; i++)
for (int j=0; j<i; j++)
{
some statements;
}
return 0;
}
2
O(n )
22
//Example 3

sum=0;
for(i=1; i<=n; i++)
for(j=1; j<=n;j*=2)
sum+=1;

O(n log n)

23
sum=0;
for(i=1; i<=n; i++)
for(j=1; j<=n;j*=2)
sum+=1;
• Suppose the above algorithm takes 10 seconds to complete
for an input size of 512. Approximately, how large a problem
(algorithm) can be solved in 2 hours, 50 minutes and 40
seconds?

24

You might also like