Comparing growth rates: O, Omega and Theta
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Big O is usually met as a rough label. Here it gets an exact meaning. Count the steps a short Python function takes for lists of 1 to 6 numbers, stack them into towers, and test the towers against rings at c·g(n): a ceiling for Big O, a floor for Big Omega, and both for Big Theta. The Software lesson "How fast is fast? Big O" introduces counting steps, and the Maths lessons "Logarithms and exponents" and "Recurrences: describing a value by smaller cases" cover the functions you will compare later in this course.
Count steps, not seconds
To compare algorithms we count steps, not seconds. This Python function adds up a list. Rule: each simple line costs one step every time it runs; the for line only drives the loop, so we skip it. n is how many numbers the list holds.
cost model: one step per simple line, each time it runs 1 def sum_count(nums): (not counted) 2 s = 0 1 step 3 c = 0 1 step 4 for x in nums: (not counted) 5 s += x 1 step 6 c += 1 1 step 7 return s, c 1 step example: sum_count([4, 1, 5]) returns (10, 3) n = len(nums), the input size
اقرأ الدرس خطوة بخطوة
Count steps, not seconds
To compare algorithms we count steps, not seconds. This Python function adds up a list. Rule: each simple line costs one step every time it runs; the for line only drives the loop, so we skip it. n is how many numbers the list holds.
cost model: one step per simple line, each time it runs 1 def sum_count(nums): (not counted) 2 s = 0 1 step 3 c = 0 1 step 4 for x in nums: (not counted) 5 s += x 1 step 6 c += 1 1 step 7 return s, c 1 step example: sum_count([4, 1, 5]) returns (10, 3) n = len(nums), the input sizeRun it with n = 3
Three numbers in the list. s = 0 and c = 0 cost 2 steps. The loop body runs 3 times at 2 steps each: 6 more. return adds 1. Total: 9 steps, one block each on the tower for n = 3.
n = 3 lines 2, 3 2 steps lines 5, 6 × 3 6 steps line 7 1 step total 9 steps order: 2 3 5 6 5 6 5 6 7A tower for every n
Do the same for n = 1 to 6. Each extra number adds one more loop pass, so 2 more blocks: 5, 7, 9, 11, 13, 15. The count is a function of n: T(n) = 2n + 3.
T(n) = 2 + 2n + 1 = 2n + 3 n T(n) 1 5 2 7 3 9 4 11 5 13 6 15 the count grows by 2 for each extra itemA ceiling: try 2n
Big O asks for a ceiling: is T(n) below c times a simple function g(n), here g(n) = n? Put a ring at height 2·n on each tower. Every tower pokes 3 blocks above its ring: c = 2 is too small.
ceiling c·g(n) with g(n) = n, c = 2 n T(n) 2nunder? 1 5 2 no 2 7 4 no 3 9 6 no 4 11 8 no 5 13 10 no 6 15 12 no 2n + 3 > 2n for every n: this c never worksBig O: a ceiling at 3n
Lift the rings to 3·n. Towers 1 and 2 still poke out, but from n = 3 on every tower fits under. That is the rule: T(n) ≤ c·g(n) for all n ≥ n₀. With c = 3 and n₀ = 3, T(n) = O(n).
ceiling c·g(n) with g(n) = n, c = 3 n T(n) 3nunder? 1 5 3 no 2 7 6 no 3 9 9 yes 4 11 12 yes 5 13 15 yes 6 15 18 yes 2n + 3 ≤ 3n ⇔ 3 ≤ n, so n₀ = 3 T(n) = O(n)Big Omega: a floor
Big Omega flips the test: a floor. Rings at 2·n sit below the top of every tower, for every n ≥ 1, since 2n + 3 is always more than 2n. So T(n) = Ω(n): it grows at least as fast as n.
floor c·g(n) with g(n) = n, c = 2 n T(n) 2nabove? 1 5 2 yes 2 7 4 yes 3 9 6 yes 4 11 8 yes 5 13 10 yes 6 15 12 yes 2n + 3 ≥ 2n for all n ≥ 1, so n₀ = 1 T(n) = Ω(n)Big Theta: squeezed between
Both at once: from n = 3 on, each tower sits between its floor at 2n and its ceiling at 3n. A count caught between two multiples of g(n) is Θ(g(n)). So T(n) = Θ(n): it grows like n, up to a constant.
2n ≤ T(n) ≤ 3n for all n ≥ 3 n 2n T(n) 3n 1 2 5 3 2 4 7 6 3 6 9 9 4 8 11 12 5 10 13 15 6 12 15 18 O(n) and Ω(n) together: T(n) = Θ(n)n² is not a floor
Now try g(n) = n². Even rings at ½·n² overtake the towers at n = 6 (18 > 15), and any c·n² does sooner or later. So T(n) is O(n²), a loose ceiling, but not Ω(n²) or Θ(n²).
floor c·g(n) with g(n) = n², c = ½ n T(n) ½n²above? 1 5 0.5 yes 2 7 2 yes 3 9 4.5 yes 4 11 8 yes 5 13 12.5 yes 6 15 18 no n² grows faster than 2n + 3: no floor c·n² holds for all large n T(n) = O(n²) too (2n + 3 ≤ n² for n ≥ 3), but not Θ(n²)
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01With the lesson's rule (one step per simple line each time it runs), how many steps does sum_count take on a list of 10 numbers?
T(n) = 2n + 3: two steps before the loop, two per loop pass, one for return. With n = 10 that is 2 + 20 + 1 = 23.
02What does T(n) = O(g(n)) mean?
Big O is a ceiling: from some n₀ on, T(n) never rises above c times g(n). Small inputs below n₀ do not count.
03Why does the ceiling 2n fail for T(n) = 2n + 3, while 3n works?
2n + 3 > 2n for every n, so c = 2 never works. 2n + 3 ≤ 3n rearranges to 3 ≤ n, so c = 3 with n₀ = 3 proves T(n) = O(n). Both 2n and 3n grow like n; only the constant changed.
04Which statement about T(n) = 2n + 3 is false?
Any c·n² eventually passes 2n + 3, even with a tiny c (½n² passes it at n = 6), so no floor c·n² holds for all large n. O(n²) is true but loose: 2n + 3 ≤ n² for n ≥ 3.
05If you also count the for line (it is checked n + 1 times), T(n) becomes 3n + 4. What happens to the Θ bound?
3n ≤ 3n + 4 ≤ 4n for n ≥ 4, so 3n + 4 is still squeezed between two multiples of n. A different counting rule changes the constants, not the growth rate.
المسرد
- Input size (n)
- How big the input is. For a function on a list, n is the number of items.
- Cost model
- The rule for what counts as one step. Here: each simple line, each time it runs.
- T(n)
- The number of steps an algorithm takes on an input of size n, written as a function of n.
- Big O, O(g(n))
- An upper bound. T(n) = O(g(n)) if some constant c and n₀ give T(n) ≤ c·g(n) for every n ≥ n₀.
- Big Omega, Ω(g(n))
- A lower bound. T(n) = Ω(g(n)) if some constant c and n₀ give T(n) ≥ c·g(n) for every n ≥ n₀.
- Big Theta, Θ(g(n))
- A tight bound: T(n) is both O(g(n)) and Ω(g(n)), so it grows like g(n) up to a constant factor.
- n₀
- The input size from which a bound must hold. Smaller inputs are ignored.
- Growth rate
- How fast a function rises as n gets large, ignoring constant factors and smaller terms. Also called the order of growth.
الملاحظات والمصادر
- The cost model counts each simple line once per run and skips the def and for lines. Counting the for line too (it is checked n + 1 times) gives 3n + 4 steps. The constants change; the growth rate does not: it is still Θ(n).
- The definitions follow MIT 6.006 and CLRS chapter 3: f(n) = O(g(n)) if some positive c and n₀ give f(n) ≤ c·g(n) for every n ≥ n₀; Ω flips the inequality; Θ means both. The figure checks n = 1 to 6, and the algebra (2n + 3 ≤ 3n exactly when n ≥ 3) covers every larger n.
- O, Ω and Θ describe how a function grows. They are not the same as worst case and best case: each of those cases has its own step count, and you can give O, Ω and Θ bounds for either one.
- Writing T(n) = O(n) is a convention. O(n) is really a set of functions and T(n) is one member of it, so some books write T(n) ∈ O(n).
- The code is real Python 3: sum_count([4, 1, 5]) returns (10, 3). Real running time also depends on the machine and the interpreter, which is exactly why we count steps instead.
- MIT 6.006 Introduction to Algorithms (Spring 2020), Recitation 1: Asymptotic Notation
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 1: Introduction
- Sedgewick and Wayne, Algorithms 4th ed., §1.4 Analysis of Algorithms
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 3
- Python Language Reference: The for statement
- Python Language Reference: Augmented assignment statements
اختبار يتاح بعد الخطوة الأخيرة.