Download Asymptotic Notation: Understanding Function Growth and more Study notes Algorithms and Programming in PDF only on Docsity! CS231 Algorithms Handout #5 Prof. Lyn Turbak January 31, 2001 Wellesley College ASYMPTOTICS AND FUNCTIONS Note: This handout summarizes highlights of CLR Chapter 2. See the book for more details. --------------------------------------------------------------------------------------------------------------------- Motivation Fine-grained bean counting exposes too much detail for comparing functions. Want a course-grained way to compare functions that ignores constant factors and focuses on their relative growth in the limit as input sizes get large. For example, consider: n = 1 n = 1,000 n = 1,000,000 p(n) = 100n + 1000 q(n) = 3n2 + 2n + 1 r(n) = 0.1n2 Sketch the above functions on the same set of axes: 2 --------------------------------------------------------------------------------------------------------------------- How Do Your Functions Grow? Asymptotic notation is a way of characterizing functions that facilitates comparing their growth in the limit of large inputs. Here is an informal summary of the notation: Notation Pronunciation Loosely f ∈ ω(g) f is way bigger than g f > g f ∈ Ω(g) f is at least as big as g f ≥ g f ∈ Θ(g) f is about the same as g f = g f ∈ O(g) f is at most as big as g f ≤ g f ∈ o(g) f is way smaller than g f < g Notes: • Each of ω(g), Ω(g), Θ(g), O(g), o(g) denotes a set of functions. Thus, ω(g) is the set of all functions way bigger than g, Ω(g) is the set of all functions at least as big as g, etc. • The notation f = ω(g) is really shorthand for f ∈ ω(g). • The phrases “is at least O(...)” and “is at most Ω(...)” are non-sensical. “Is at least” should be written Ω, and “is at most” should be written O. Intuitively, what are the relationships between p, q, and r? --------------------------------------------------------------------------------------------------------------------- 5 --------------------------------------------------------------------------------------------------------------------- Formalizing O, Ω, and Θ O(g) = {f | there exist positive constants c, n0 such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 .} Think of this as a game. Suppose you claim that f ∈ O(g). Then you select c and n0 , but I try to find a particular n that defeats your claim. Ω(g) = {f | there exist positive constants c, n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 .} Θ(g) = {f | there exist positive constants c1 , c2 , n0 such that 0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0 .} 6 --------------------------------------------------------------------------------------------------------------------- Examples Show p∈ O(q) (1) use c = 1, n0= 1000 (2) use c = 1000, n0= 1 Can we show q_ O(p) ? Show r_ O(q) (use c = 1 n 0 = 1) Show q∈ O(r) (use c = 40; what must n0 be?) If limn→∞ (f(n)/g(n)) = k > 0, then f ∈ Θ (g). This is an easy way to show that two functions are related by Θ. E.g., use it to show that q ∈ Θ (r) and r ∈ Θ (q). The converse of the above limit trick is not true. That is, although the limit trick works most of the time to show that two functions are related by Θ, there are some cases where it doesn't work. E.g., f(n) = 2 + sin(n) and g(n) = 2. 7 --------------------------------------------------------------------------------------------------------------------- Does anything Fall Between the Cracks? The diagram on p. 3 implies that there are functions that are O(g) that are neither o(g) nor Θ (g), and there are functions that are Ω(g) that are neither ω(g) nor Θ (g). Here's a concrete example: f(n) = 1/n g(n) = n h(n) = nsin(n) Show that h ∈ 0 (g) , but h ∉ o (g) and h ∉ Θ (g). (Similarly, h ∈ Ω (f), but h ∉ ω (g) and h ∉ Θ (g)). --------------------------------------------------------------------------------------------------------------------- Incomparable Functions Not every two functions are comparable. Is k(n) = sqrt(n) related to h(n) above?