Asymptotic Notations: DR Ashok Kumar Sahoo Mobile: 9810226795 E-Mail: Ashok - Sahoo@gehu - Ac.in
Asymptotic Notations: DR Ashok Kumar Sahoo Mobile: 9810226795 E-Mail: Ashok - Sahoo@gehu - Ac.in
– size of an array
– polynomial degree
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
... ...
arr[N-1] = 0; c1
----------------------------------- -----------------------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1 =
(c2 + c1) x N + c2
Another Example
• Algorithm 3 Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2
Insertion Sort C Function
void insertion_sort(int a[], int n)
{
int c, d, t;
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && a[d] < a[d-1]) {
t = a[d];
a[d]=a[d-1];
a[d-1] = t;
d--;
}
}}
Asymptotic Analysis
• To compare two algorithms with running
times f(n) and g(n), we need a rough
measure that characterizes how fast
each function grows.
• Hint: use rate of growth
• Compare functions in the limit, that is,
asymptotically!
(i.e., for large values of n)
Rate of Growth
• Consider the example of buying elephants and
goldfish:
Total Cost: cost_of_elephants + cost_of_goldfish
Cost cost_of_elephants (approximation)
• The low order terms in a function are relatively
insignificant for large n
n4 + 100n2 + 10n + 50 n4
• 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)
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)??
O-notation
For function g(n), we define
O(g(n)), big-O of n, as the set:
O(g(n)) = {f(n) :
positive constants c and n0,
such that n n0,
we have 0 f(n) cg(n) }
Algorithm 3 Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2 = O(N2)
Examples
1000n2+1000n = O(n2):
RR
O( f ) ( f )
•f
( f )
Exercises
• Express the function n3/1000 − 100n2 − 100n + 3
in terms of Θ-notation.
• Show that for any real constants a and b, where
b > 0, (n + a)b = Θ(nb)
• Show that the solution of T (n) = T (n/2) + 1 is
O(lg n).
• Solve the followings:
– T (n) = 4T (n/2) + n.
– T (n) = 4T (n/2) + n2.
– T (n) = 4T (n/2) + n3.