1 / 20

Lecture 2

Lecture 2. Asymptotic Notation. Q , O , W Defined for functions over the natural numbers. Ex: f ( n ) = Q ( n 2 ). Describes how f ( n ) grows in comparison to n 2 . Define a set of functions; in practice used to compare two function sizes.

ivan
Download Presentation

Lecture 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 2

  2. Asymptotic Notation • Q, O, W • Defined for functions over the natural numbers. • Ex:f(n) = Q(n2). • Describes how f(n) grows in comparison to n2. • Define a set of functions; in practice used to compare two function sizes. • The notations describe different rate-of-growth relations between the defining function and the defined set of functions.

  3. -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). g(n) is an asymptotically tight bound for f(n).

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

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

  6. Proving Big-O Complexity To prove that f(n) is O(g(n)) we find any pair of values n0 and c that satisfy: f(n) ≤ c * g(n) for  n n0 Note: The pair (n0, c) is not unique. If such a pair exists then there is an infinite number of such pairs. Example: Prove that f(n) = 3n2 + 5 is O(n2) We try to find some values of n and c by solving the following inequality: 3n2 + 5  cn2 OR3 + 5/n2 c (By putting different values for n, we get corresponding values for c)

  7. Proving Big-O Complexity Example: Prove that f(n) = 3n2 + 4n log n + 10 is O(n2) by finding appropriate values for c and n0 We try to find some values of n and c by solving the following inequality 3n2 + 4n log n + 10  cn2 OR 3 + 4 log n / n+ 10/n2 c ( We used Log of base 2, but another base can be used as well)

  8. Algorithm Analysis • Construct: • Sequence • Selection • Iterations • Recursion

  9. Algorithm Analysis • Sequence Statements: Just add the running time of the statements • If-Then-Else: if (condition) S1 else S2 Running time of the test plus the larger of the running times of S1 and S2. • Iteration is at most the running time of the statements inside the loop, (including tests) times the number of iterations. • Nested Loops: Analyze these inside out. The total Running time of a statement inside a group of nested loops is the running time of the statement multiplied by the product of the size of all the loops. • Function Calls: Analyzing from inside to out. If there are function calls, these must be analyzed first.

  10. Algorithm Analysis • What are the constructs / Keywords. • Time for each construct • Total Time • Total time as a function of input size

  11. Algorithm Analysis • How much time each construct / keyword of a pseudo code takes to execute. Assume it takes ti (the ith construct) • Sum / Add up the execution time of all the constructs / keywords. if there are m constructs then

  12. Algorithm Analysis • That will be the execution time for a given input size (say n) • Running time as the function of the input size T(n)

  13. x = logba is the exponent for a = bx. Natural log: ln a = logea Binary log: lg a = log2a lg2a = (lg a)2 lglg a =lg(lg a) Logarithms

  14. Review on Summations • Constant Series: For integers a and b, a  b, • Linear Series (Arithmetic Series): For n 0, • Quadratic Series: For n 0,

  15. Review on Summations • Cubic Series: For n 0, • Geometric Series: For real x 1, For |x| < 1,

  16. How to determine complexity of code structures • Sometimes if-else statements must carefully be checked: O(if-else) = O(Condition)+ Max[O(if), O(else)] int[] integers = new int[10]; ........ if(hasPrimes(integers) == true) integers[0] = 20; else integers[0] = -20; public boolean hasPrimes(int[] arr) { for(int i = 0; i < arr.length; i++) .......... .......... } // End of hasPrimes() O(1) O(1) O(n) O(if-else) = O(Condition) = O(n)

  17. How to determine complexity of code structures while (n > 0) { if (n % 2 = = 0) { System.out.println(n); n = n / 2; } else{ System.out.println(n); System.out.println(n); n = n – 1; } } • Note: Sometimes a loop may cause the if-else rule not to be applicable. Consider the following loop: The else-branch has more basic operations; therefore one may conclude that the loop is O(n). However the if-branch dominates. For example if n is 60, then the sequence of n is: 60, 30, 15, 14, 7, 6, 3, 2, 1, and 0. Hence the loop is logarithmic and its complexity is O(log n)

  18. Analysis Example • Find the value of the largest element in a list of n numbers. MaxElement(A[0..n-1) maxVal = A[0]; for(I = 1; I < n; I++) if(A[I] > maxVal) maxVal = A[I]; return maxVal

  19. Analysis Example • Check whether all the elements in a given array are distinct. UniqueElements(A[0..n-1]) for(I = 0; i<n-1; i++) for(j = i+1; j<n; j++) if(A[i] = A[j]) return false return true

  20. Problem to Solve? • There are four people who want to cross a bridge; they all begin on the same side. You have 17 minutes to get them all across to the other side. • It is night, and they have one flashlight. A maximum of two people can cross the bridge at one time. Any party that crosses, either one or two people, must have the flashlight with them. The flashlight must be walked back and forth; it cannot be thrown • Person 1 takes 1 minute to cross the bridge, person 2 takes 2 minutes, person 3 takes 5 minutes, and person 4 takes 10 minutes. • A pair must walk together at the rate of the slower person’s pace. • For example, if person 1 and person 4 walk across first, 10 minutes have elapsed when they get to the other side of the bridge. • If person 4 returns the flashlight, a total of 20 minutes have passed and you have failed the mission.

More Related