
Understanding Asymptotic Behavior in Algorithms
Dive into the concept of asymptotic behavior in algorithms and data structures through examples and methods. Learn why it matters and how to analyze functions as input size approaches infinity, shedding light on the importance of asymptotic analysis for algorithm efficiency.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
Analysis of Algorithms: Methods and Examples CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1
Why Asymptotic Behavior Matters Asymptotic behavior: The behavior of a function as the input approaches infinity. h(N) c*f(N) N: Size of data g(N) f(N) Running Time for input of size N 2
Why Asymptotic Behavior Matters Which of these functions is smallest asymptotically? h(N) c*f(N) N: Size of data g(N) f(N) Running Time for input of size N 3
Why Asymptotic Behavior Matters Which of these functions is smallest asymptotically? g(N) seems to grow very slowly after a while. h(N) c*f(N) N: Size of data g(N) f(N) Running Time for input of size N 4
Why Asymptotic Behavior Matters Which of these functions is smallest asymptotically? However, the picture is not conclusive (need to see what happens for larger N). h(N) c*f(N) N: Size of data g(N) f(N) Running Time for input of size N 5
Why Asymptotic Behavior Matters Which of these functions is smallest asymptotically? Proving that ?(?) = ?(?(?)) would provide a conclusive answer. h(N) c*f(N) N: Size of data g(N) f(N) Running Time for input of size N 6
Using Limits ?(?) ?(?) is a constant, then g(N) = O(f(N)). if lim ? "Constant" includes zero, but does NOT include infinity. ?(?) ?(?)= then g(N) = O(f(N)). if lim ? ?(?) ?(?) is a constant, then g(N) = (f(N)). if lim ? Again, "constant" includes zero, but not infinity. ?(?) ?(?) is a non-zero constant, then g(N) = (f(N)). if lim ? In this definition, both zero and infinity are excluded. 7
Using Limits - Comments The previous formulas relating limits to big-Oh notation show once again that big-Oh notation ignores: constants behavior for small values of N. How do we see that? 8
Using Limits - Comments The previous formulas relating limits to big-Oh notation show once again that big-Oh notation ignores: constants behavior for small values of N. How do we see that? In the previous formulas, it is sufficient that the limit is equal to a constant. The value of the constant does not matter. In the previous formulas, only the limit at infinity matters. This means that we can ignore behavior up to any finite value, if we need to. 9
Using Limits: An Example Show that ?5+3?4+2?3+?2+?+12 = (???). 5?3+?+3 10
Using Limits: An Example Show that ?5+3?4+2?3+?2+?+12 = (?2). 5?3+?+3 Let g ? =?5+3?4+2?3+?2+?+12 5?3+?+3 Let ?(?) = ?2. ?5+ 3?4+ 2?3+ ?2+ ? + 12 5?3+ ? + 3 ?5+ 3?4+ 2?3+ ?2+ ? + 12 5?5+ ?3+ 3?2 ?(?) ?(?)= lim 1 ?2 lim ? ? 1 511 = lim ? =
Using Limits: An Example Show that ?5+3?4+2?3+?2+?+12 = (?2). 5?3+?+3 Let g ? =?5+3?4+2?3+?2+?+12 5?3+?+3 Let ?(?) = ?2. ?(?) ?(?)= 1 5 In the previous slide, we showed that lim ? Therefore, ?(?) = (?(?)). 12
Big-Oh Transitivity If ? ? = ? ? ? and ? ? = ?( ? ), then ? ? = ?( ? ). How can we prove that? 13
Big-Oh Transitivity If ? ? = ? ? ? and ? ? = ?( ? ), then ? ? = ?( ? ). How can we prove that? Using the definition of the big-Oh notation. g(N) < c0 f(N) for all N > N0. f(N) < c1 h(N) for all N > N1. Set: c2 = c0 * c1 N2 = max(N0, N1) Then, g(N) < c2 h(N) for all N > N2. 14
Big-Oh Hierarchy 1 = ?(log(?)) log(?) = ?(?) ? = ? ?2 If ? ? 0, then ?? = ?(??). Higher-order polynomials always get larger than lower- order polynomials, eventually. For any ?, if ? > 1, ?? = ? ??. Exponential functions always get larger than polynomial functions, eventually. You can use these facts in your assignments. You can apply transitivity to derive other facts, e.g., that log(?) = ?(?2). 15
Using Substitutions If lim ? (?) = , then: ? ? = ? ? ? ? (?) = ?(? (?) ). How do we use that? For example, prove that log ? = ?( ?). 16
Using Substitutions If lim ? (?) = , then: ? ? = ? ? ? ? (?) = ?(? (?) ). How do we use that? For example, prove that log Use h x = ? = ?( ?). ?. We get: log N = O N log ? = ? ? 17
Summations ? Summations are formulas of the sort: ?=0 ?(?) Computing the values of summations can be handy when trying to solve recurrences. Oftentimes, establishing upper bounds is sufficient, since we use big-Oh notation. ? If ? ? 0 , then: ?=0 ? ? ?=0 ? ? Sometimes, summing to infinity give a more simple formula. 18
Geometric Series A geometric series is a sequence Ckof numbers, such that Ck = D * Ck-1 , where D is a constant. How can we express C1 in terms of C0? C1 = D * C0 How can we express C2 in terms of C0? C2 = D * C1 = D2 * C0 How can we express Ck in terms of C0? Ck = Dk * C0 So, to define a geometric series, we just need two parameters: D and C0. 19
Summation of Geometric Series This is supposed to be a review of material you have seen in Math courses: Suppose that ? < ? < ?: ??=1 ??+1 ? Finite summations: ?=0 1 ? 1 Infinite summations: ?=0 ??= 1 ? 1 ? ?? ?=0 ??= Important to note: ?=0 Therefore, ?=0 1 ? ? ??= ? 1 . Why? 20
Summation of Geometric Series This is supposed to be a review of material you have seen in Math courses: Suppose that ? < ? < ?: ??=1 ??+1 ? Finite summations: ?=0 1 ? 1 Infinite summations: ?=0 ??= 1 ? 1 ? ?? ?=0 ??= Important to note: ?=0 Therefore, ?=0 Because 1 ? ? ??= ? 1 . Why? 1 1 ? is independent of n. 21
Summation of Geometric Series Suppose that ? > 1: The formula for finite summations is the same, and can be rewritten as: ??=??+1 1 ? 1 ? ?=0 This can be a handy formula in solving recurrences: For example: 1 + 5 + 52 + 53 + + 5?=5?+1 1 = ?(5?) 5 1 22
Harmonic Series 1 ? ? ??= ?=1 ln ? ?? ln ? + 1 The above formula shows that the harmonic series can be easily approximated by the natural logarithm. It follows that ??= ? log ? . Why? log2? log2?= ??= ? ln ? = ?(log ? ) 1 ln ? = log?? = log2? log2? = ?(log ? ) 23
Approximation by Integrals Suppose that f(x) is a monotonically increasing function: This means that ? ? ? ? ?(?). Then, we can approximate summation ?=? using integral ? ? ?(?) ?+1? ? ??. ?+1? ? ??. Why? Because ?(?) ? ?+1? ? ?? is the average value of ?(?) in the interval [?,? + 1]. For every ? in the interval [?,? + 1], ? ?.Since ?(?) is increasing, if ? ? then ? ? ?(?). Why? ? 24
Solving Recurrences: Example 1 Suppose that we have an algorithm that at each step: takes O(N2) time to go over N items. eliminates one item and then calls itself with the remaining data. How do we write this recurrence? 25
Solving Recurrences: Example 1 Suppose that we have an algorithm that at each step: takes O(N2) time to go over N items. eliminates one item and then calls itself with the remaining data. How do we write this recurrence? ?(?) = ?(? 1) + ?2 = ?(? 2) + (? 1)2 + ?2 = ?(? 3) + (? 2)2 + (? 1)2 + ?2 = 12 + 22 + + ?2 = ?=1 ?2. How do we approximate that? ? 26
Solving Recurrences: Example 1 ? We approximate ?=? ?? using an integral: Clearly, ?(?) = ?2 is a monotonically increasing function. ?+13 13 3 ?+1?2?? = ? So, ?=1 ?2 1 =?3+2?2+2?+1 1 = ?(?3) 3 27
Solving Recurrences: Example 2 Suppose that we have an algorithm that at each step: takes O(log(N)) time to go over N items. eliminates one item and then calls itself with the remaining data. How do we write this recurrence? 28
Solving Recurrences: Example 2 Suppose that we have an algorithm that at each step: takes O(log(N)) time to go over N items. eliminates one item and then calls itself with the remaining data. How do we write this recurrence? ?(?) = ?(? 1) + log(?) = ?(? 2) + log(? 1) + log(?) = ?(? 3) + log(? 2) + log(? 1) + log(?) = log(1) + log(2) + + log(?) ? ???(?). How do we compute that? = ?=1 29
Solving Recurrences: Example 2 ? We process ?=? log(?) + log(?) = log(??) ???(?) using the fact that: N k=1 = log(?!) log((? log(k) = log 1 + log 2 + + log N ?)?) = ? log(? ?) = ?log ? ?log ? = ?(?log ? ) 30
Solving Recurrences: Example 3 Suppose that we have an algorithm that at each step: takes O(1) time to go over N items. calls itself 3 times on data of size N-1. takes O(1) time to combine the results. How do we write this recurrence? 31
Solving Recurrences: Example 3 Suppose that we have an algorithm that at each step: takes O(1) time to go over N items. calls itself 3 times on data of size N-1. takes O(1) time to combine the results. How do we write this recurrence? ?(?) = 3?(? 1) + 1 = 32? ? 2 + 3 + 1 = 33? ? 3 + 32+ 3 + 1 = 3? 1? 1 + 3? 2+ 3? 3+ 3? 4+ + 1 Note: ?(1) is just a constant finite summation 32
Solving Recurrences: Example 3 Suppose that we have an algorithm that at each step: takes O(1) time to go over N items. calls itself 3 times on data of size N-1. takes O(1) time to combine the results. How do we write this recurrence? ?(?) = 3?(? 1) + 1 = 32? ? 2 + 3 + 1 = 33? ? 3 + 32+ 3 + 1 = 3? 1? 1 + 3? 2+ 3? 3+ 3? 4+ + 1 = O 3?+ O 3?= O(3?) 33
Solving Recurrences: Example 4 Suppose that we have an algorithm that at each step: calls itself N times on data of size N/2. takes O(1) time to combine the results. How do we write this recurrence? 34
Solving Recurrences: Example 4 Suppose that we have an algorithm that at each step: calls itself N times on data of size N/2. takes O(1) time to combine the results. How do we write this recurrence? Let n = log?. ?(2?) = 2??(2? 1) + 1 = 2? 2? 1? ? 2 + 2?+ 1 = 2? 2? 1 2? 2? ? 3 + 2? 2? 1+ 2?+ 1 ? 2? ? ? 3 + 1 + ? ? 2? = ?=? 2 ?=? 1 ?=? 35
Solving Recurrences: Example 4 Suppose that we have an algorithm that at each step: calls itself N times on data of size N/2. takes O(1) time to combine the results. How do we write this recurrence? Let n = log?. ?(2?) = 2??(2? 1) + 1 = 2? 2? 1? ? 2 + 2?+ 1 = 2? 2? 1 2? 2? ? 3 + 2? 2? 1+ 2?+ 1 ? 2? ? ? 4 + 1 + ? ? 2? = ?=? 3 ?=? 2 ?=? 36
Solving Recurrences: Example 4 Suppose that we have an algorithm that at each step: calls itself N times on data of size N/2. takes O(1) time to combine the results. How do we write this recurrence? Let n = log?. ?(2?) = 2??(2? 1) + 1 = 2? 2? 1? ? 2 + 2?+ 1 = 2? 2? 1 2? 2? ? 3 + 2? 2? 1+ 2?+ 1 ? 2? ? 1 + 1 + ? ? 2? = ?=2 ?=3 ?=? 37
Solving Recurrences: Example 4 ? 2? = 2?2? 12? 2 2221 ?=2 = 2(?+? 1+? 2+ +1) ?(?+1) 2 = 2 ?+1 2 2? = Substituting N for 2? log(?)+1 2 = ? log ? 2 = ?(? ) 38
Solving Recurrences: Example 4 ? 2?(which we have just computed). Let X = ?=2 ? ? 2?< ? +? 2+? 4+ ?=3 ?=? ? ? 2?< 2? (taking previous slide into account) ?=3 ?=? ? ? log ? 2 2?= ?(? ) ?=3 ?=? 39
Solving Recurrences: Example 4 Based on the previous two slides, we can conclude that the solution of: ?(?) = ??(?/2) + 1is that: log ? 2 ? ? = ?(? ) 40
Big-Oh Notation: Example Problem Is ? = ?(sin ? ?2)? Answer: 41
Big-Oh Notation: Example Problem Is ? = ?(sin ? ?2)? Answer: no! Why? sin(?) fluctuates forever between -1 and 1. As a result, sin ? ?2 fluctuates forever between negative and positive values. Therefore, for every possible ?0> 0 and ?0, we can always find an ? > ?0such that: ? > ?0sin(?)?2 42