Presentation is loading. Please wait.

Presentation is loading. Please wait.

Growth of Functions. Asymptotic Analysis for Algorithms T(n) = the maximum number of steps taken by an algorithm for any input of size n (worst-case runtime)

Similar presentations


Presentation on theme: "Growth of Functions. Asymptotic Analysis for Algorithms T(n) = the maximum number of steps taken by an algorithm for any input of size n (worst-case runtime)"— Presentation transcript:

1 Growth of Functions

2 Asymptotic Analysis for Algorithms T(n) = the maximum number of steps taken by an algorithm for any input of size n (worst-case runtime) Always talking about worst-case runtime – Will become more important later We are not concerned with how good things can be Don’t want to be surprised by a “bad” case after implementation – Especially if “bad” cases are not uncommon

3 Asymptotic Analysis for Algorithms T(n) = the maximum number of steps taken by an algorithm for any input of size n (worst-case runtime) If T(n) n 0 then T(n) is in O(u(n)) – Need to show that the number of steps taken is less than u(n) for all inputs of size n>n 0

4 Asymptotic Analysis for Algorithms T(n) = the maximum number of steps taken by an algorithm for any input of size n (worst-case runtime) If T(n)>L(n) for all n>n 0 then T(n) is in Ω(L(n)) – Need to show that there exists at least one input that takes more than L(n) steps for all n>n 0

5 Input Size Asymptotic runtime is always measured with respect to the input size Input size represented by n – Later: n = Number of elements for the data structure Runtime is a function of n

6 Common Runtimes Constant: O(1) – No dependence on n – As n grows, the runtime is constant Logarithmic: O(log(n)) – Very efficient Linear: O(n) O(n*log(n)) Squared: O(n 2 ) Cubic: O(n 3 ) – Starting to be impractical except for small n Exponential: O(2 n ) – Impractical

7 Example How to find an element in a sorted array? – Linear search Check each element 1-by-1 and compare If the element is found, return true Linear runtime: O(n) – Binary search Recursively compare with center element If center element is smaller, check the larger half If center element is larger, check the smaller half Eliminate half the search space each comparison Logarithmic runtime: O(log(n))


Download ppt "Growth of Functions. Asymptotic Analysis for Algorithms T(n) = the maximum number of steps taken by an algorithm for any input of size n (worst-case runtime)"

Similar presentations


Ads by Google