ENع
→O, Ω and Θ
الدرس 01 · Measuring cost

Comparing growth rates: O, Omega and Theta

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة8 خطوات5 أسئلةBeginner
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 1 · تفاعلي
01 / 08

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
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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
  2. Run 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 7
  3. A 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 item
  4. A 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 works
  5. Big 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)
  6. 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)
  7. 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)
  8. 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²)

اختبار يتاح بعد الخطوة الأخيرة.