Sunteți pe pagina 1din 17

DESIGN AND ANALYSIS OF ALGORITHMS

PREPARED BY: Kanika

University Institute of Engineering (UIE)


Design and Analysis of Algorithms

(CST-302)

Lecture 2
Asymptotic notations

University Institute of Engineering (UIE)


OUTCOMES

Designing of algorithms.
Denotation of their time complexities
Analysis on the basis of graph structure

University Institute of Engineering (UIE)


ASYMPTOTIC NOTATIONS

Types

Big-O Notation (Worst Case)


Theta Notation (Average Case)
Omega Notation(Best Case)

University Institute of Engineering (UIE)


COMPUTING TIMES

O(1): Constant O(2n): Exponential


O(n): Linear O(lg n): Logarithmic
O(n2): Quadratic
O(n3): Cubic

University Institute of Engineering (UIE)


TIME COMPLEXITY

s/e freq (s/e*freq)


1. Algorithm Sum(a,n) 0 - -
2. { 0 - -
3. s:=0; 1 1 1
4. for i:= 1 to n do 1 n+1 n+1
5. s:= s+a[i]; 1 n n
6. Return s;
1 1 1
7. }
0 - -
Total = (2n +3)
= O(n)

University Institute of Engineering (UIE)


-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)).
Ill accept either

f(n) and g(n) are nonnegative, for large n.

University Institute of Engineering (UIE)


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)

University Institute of Engineering (UIE)


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

University Institute of Engineering (UIE)


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) }
Intuitively: Set of all functions whose rate of
growth is the same as or lower than that of
g(n).

g(n) is an asymptotic upper bound for f(n).


f(n) = (g(n)) f(n) = O(g(n)).
(g(n)) O(g(n)).

University Institute of Engineering (UIE)


Examples
O(g(n)) = {f(n) : positive constants c and n0, such
that n n0, we have 0 f(n) cg(n) }

Any linear function an + b is in O(n2). How?


Show that 3n3=O(n4) for appropriate c and n0.

University Institute of Engineering (UIE)


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


f(n) = (g(n)) f(n) = (g(n)).
(g(n)) (g(n)).

University Institute of Engineering (UIE)


(g(n)) = {f(n) : positiveExample
constants c and n0, such that
n n0, we have 0 cg(n) f(n)}
n = (lg n). Choose c and n0.

University Institute of Engineering (UIE)


University Institute of Engineering (UIE)
Importance of complexity analysis

The algorithm or the programming language?

I dont care about


C++ is faster the programming
than Java language

Note: An O(nlogn) algorithm in Java


will probably be faster than a O(n2)
algorithm in C++

University Institute of Engineering (UIE)


FAQ
How to compute differences between various asymptotic
notations?
Computing the importance of such notations.

University Institute of Engineering (UIE)


References
1. Introduction to Algorithms by Coreman,
Leiserson,Rivest, Stein.
2. Fundamentals of Algorithms by Ellis Horwitz,
Sartaj Sahni, Sanguthevar Rajasekaran
3. Different websites providing study material.

University Institute of Engineering (UIE)

S-ar putea să vă placă și