growth of functions. asymptotic analysis for algorithms t(n) = the maximum number of steps taken by...
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)](https://reader036.vdocuments.mx/reader036/viewer/2022082818/56649ecf5503460f94bdcbf5/html5/thumbnails/1.jpg)
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)](https://reader036.vdocuments.mx/reader036/viewer/2022082818/56649ecf5503460f94bdcbf5/html5/thumbnails/2.jpg)
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)](https://reader036.vdocuments.mx/reader036/viewer/2022082818/56649ecf5503460f94bdcbf5/html5/thumbnails/3.jpg)
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)](https://reader036.vdocuments.mx/reader036/viewer/2022082818/56649ecf5503460f94bdcbf5/html5/thumbnails/4.jpg)
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)](https://reader036.vdocuments.mx/reader036/viewer/2022082818/56649ecf5503460f94bdcbf5/html5/thumbnails/5.jpg)
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)](https://reader036.vdocuments.mx/reader036/viewer/2022082818/56649ecf5503460f94bdcbf5/html5/thumbnails/6.jpg)
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)](https://reader036.vdocuments.mx/reader036/viewer/2022082818/56649ecf5503460f94bdcbf5/html5/thumbnails/7.jpg)
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))