PE 459 LECTURE 2- natural gas basic concepts and properties
CS-102 DS-class03 Class DS Lectures .pdf
1. o-notation
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?
o(g(n)) = {f(n): c > 0, n0 > 0 such that
n n0, we have 0 f(n) < cg(n)}.
For a given function g(n), the set little-o:
2. • f(n)=3n+2 is o(n2)
• f(n)=17n3 + n2 logn is o(n4)
o-notation
4. (g(n)) = {f(n): c > 0, n0 > 0 such that
n n0, we have 0 cg(n) < f(n)}.
-notation
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.
For a given function g(n), the set little-omega:
6. 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
9. 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
10. 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;}
for (int j=0; j<n;j++)
{
for (int k=0; k<n;k++)
{Simple statements;}
}
)
( 2
n
O
11. 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
n
O
13. 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 256.
How large a problem (algorithm) can be
solved in 40 minutes?
14. const int n=100; //Example 4
int main()
{
for (int i=0;i<n;i++)
f();
return 0;
}
void f()
{ int a[n];
for (j=0;j<n;j++)
{
some statements;
}
}
)
( 2
n
O
15. int a; //Example 5
a=5;
a++;
int flag;
cin>>flag;
if (flag==0)
for (int i=0; i<n; i++)
{Simple statements;}
else{
for (int j=0; j<n;j++)
{
for (int k=0; k<n;k++)
{Simple statements;}
}
}
Time Complexity-Best, Average and
Worst Case
)
( 2
n
O
17. int n=64;
steps=0;
for (int i=1; i<=n;i*=2)
steps++;
cout<<steps;
• Assume that the above algorithm takes 20
seconds for an input size of 64 to complete in
the current configuration of any
system/workstation. How long does it take to
solve a problem of size 4096?
18. Classification
• O(k) or O(1) -----> Utopian (desirable or more
than excellent)
• O(log n) -----> Excellent
• O( ) -----> Very Very good
• O(n) -----> Very good
• O(n2) -----> Not So good
• O(n3) -----> Pretty Bad
• O(dn) -----> Disaster where d>1
n