Presentation is loading. Please wait.

Presentation is loading. Please wait.

Asymptotic Notations Algorithms Lecture 9.

Similar presentations


Presentation on theme: "Asymptotic Notations Algorithms Lecture 9."— Presentation transcript:

1 Asymptotic Notations Algorithms Lecture 9

2 Algorithms Lecture 9 Dr. Arwa Zabian
Growth of functions The order of growth of the running time of an algorithm, gives a simple characterization of the algorithm’s efficiency and also allow us to compare the relative performance of alternative algorithms . Once the input size n becomes large enough , Merge sort ( in the worst case ) with its  ( n log n ) out performs Insertion sort in the worst case whose running time is  (n2 ). We are studying the asymptotic efficiency of algorithms. That is , we are concerned with how the running time of an algorithm increases with the size of the input in the limit, as the size of the input increased without bound. So, we give a bound to the running time when the input increased without bound. Algorithms Lecture 9 Dr. Arwa Zabian

3 Algorithms Lecture 9 Dr. Arwa Zabian
Asymptotic Notations The notation we use to describe the asymptotic running time of an algorithm are defined in terms of functions. Whose domain are the set of natural numbers N={0,1,2,3…}. So, using the asymptotic notation we describe the running time T(n) of an algorithm for an integer input size Algorithms Lecture 9 Dr. Arwa Zabian

4 Algorithms Lecture 9 Dr. Arwa Zabian
 Notation Definition : for a given function g(n) , we denote  (g(n)) the set of functions (g(n)) = { f(n): there exists a positive constant c1 , c2 and n0 , such that : 0  c1 g(n)  f(n)  c2 g(n) for all n  n0 } That means we must choose a value n0 from which the definition of  is hold If the definition of  is hold for n n0 .  means give upper bound and lower bound. That means give an equal bound. Algorithms Lecture 9 Dr. Arwa Zabian

5 Algorithms Lecture 9 Dr. Arwa Zabian
 Notation In other words , for all n n0, the function f(n) is equal to g(n) to within a constant factor. We say that g(n) is an asymptotically tight bound for f(n) The definition of  (g(n)), requires that every number of  (g(n)) be asymptotically non negative. That is, that f(n) be non negative whenever n is sufficient large. Consequently, the function g(n) it self must be asymptotic non negative, or else the set  (g(n)) is empty Algorithms Lecture 9 Dr. Arwa Zabian

6 Algorithms Lecture 9 Dr. Arwa Zabian
Exercise c1  ½ - 3/n  c2 For 7 > n 1 G(n) always negative so, we choose n0 = 1/14 and c1 = 1/14 , c2 =1/2 That means for all n0  7 we can find two constant that makes the inequality correct. That means g(n)=  (n2 ) Use the formal description to show that g(n) = ½ n2 - 3n =  (n2 ) solution : To do so, we must determine positive constant c1 , c2 and n0 such that: c1 n2  ½ n2 – 3n  c2 n2 for all n  n0 dividing by n2 yields: Algorithms Lecture 9 Dr. Arwa Zabian

7 Algorithms Lecture 9 Dr. Arwa Zabian
Exercise Using the formal definition proof that 6n3 ≠  (n2 ) We assume that 6n3 = (n2 ) So given the definition for all n  n0 C1 n2  6n3 c2 n2 c1 6 n  c2 n  c2 /6 Which cannot possibly hold for arbitrary large n , since c2 is constant Algorithms Lecture 9 Dr. Arwa Zabian

8 Algorithms Lecture 9 Dr. Arwa Zabian
O -notation O notation gives an asymptotic upper bound Definition : for a given function g(n) , we denote O (g(n)) the set of functions O(g(n)) = { f(n): there exists a positive constant c, and n0 , such that : 0  f(n)  c g(n) for all n  n0 } For all value n to the right of n0 , the value of the function f(n) is on or below g(n). O notation gives an upper bound on a function Using O-notation , we can often describe the running time of an algorithm merely by inspection the algorithm’s over all structure. Algorithms Lecture 9 Dr. Arwa Zabian

9 Algorithms Lecture 9 Dr. Arwa Zabian
Exercise Insertion sort The algorithm is composed on two loops . The inner loop while is executed one time its cost O(1) that consists on assignment to the key and comparisons. The indices i, j are executed n times So immediately we say that the running time is O(n2) in the worst case. for j to length[A] do key A[j] insert A[j] into the sorted sequence A[1….j-1] i j-1 While i> 0 and A[i] < key do A[i+1] A[i] i i-1 A[i+1] key Algorithms Lecture 9 Dr. Arwa Zabian

10 Algorithms Lecture 9 Dr. Arwa Zabian
O -notation When we said the running time is O (n2) in the worst case means : the algorithm cannot run worst than n2 This value is for every input however,  (n2) in the worst case does not imply a bound on the running time for every input ( but specific input ) That means O is more general than  So, we are interested in studying the behavior of the algorithm when the input is increased. Algorithms Lecture 9 Dr. Arwa Zabian

11 Algorithms Lecture 9 Dr. Arwa Zabian
 notation  notation provides an asymptotic lower bound Definition : for a given function g(n) , we denote  (g(n)) the set of functions (g(n)) = { f(n): there exists a positive constant c, and n0 , such that : 0  c g(n)  f(n) for all n  n0 } For all value n to the right of n0 , the value of the function f(n) is on or above g(n). Algorithms Lecture 9 Dr. Arwa Zabian

12 Algorithms Lecture 9 Dr. Arwa Zabian
Theorem For any two functions f(n) and g(n), f(n) =  (g(n)) if and only if : f(n) = O(g(n)) and f(n) =  (g(n)) That means has the same asymptotic bound if and only if is upper and lower bounded by g Algorithms Lecture 9 Dr. Arwa Zabian


Download ppt "Asymptotic Notations Algorithms Lecture 9."

Similar presentations


Ads by Google