growth of functions. asymptotic analysis for algorithms t(n) = the maximum number of steps taken by...

7
Growth of Functions

Upload: gillian-peters

Post on 01-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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)

Growth of Functions

Page 2: 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)

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

Page 3: 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)

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)<u(n) for all n>n0 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>n0

Page 4: 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)

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>n0 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>n0

Page 5: 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)

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

Page 6: 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)

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(n2)• Cubic: O(n3)

– Starting to be impractical except for small n• Exponential: O(2n)

– Impractical

Page 7: 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)

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