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/
-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).
2
2 2
• Prove that f ( n ) 5n 2 n 1 is (n )
3
Example
c * log n n , n 16
4
5
Omega
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).
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…
8
Example
• 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
• Is 3n3 (n4) ??
• How about 22n (2n)??
10
Examples
3n2 + 17
11
Relations Between O,
12
o-notation
• f(n)=3n+2 is o(n2)
14
-notation
15
-notation
• f(n)=3n+2 is (1)
2
• f(n)=17n3 + n2 is (n )
16
Comparison of Functions
fg ab
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
18
All Asymptotic Notations
T (n) n 3 3n 2 4n 2
T ( n ) 16 n log n
19
Time Complexity
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