ENع
→Master theorem
الدرس 04 · Measuring cost

The master theorem

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

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

Recursive algorithms that split their input into equal pieces have step counts like T(n) = 2T(n/2) + n. Draw the recursion tree of a short Python function for n = 8, add up each level, and watch which levels hold the work. The master theorem turns that picture into a rule with three cases: the leaves win, it is a tie, or the root wins. The Software lessons "How fast is fast? Big O" and "Recursion" and the Maths lessons "Logarithms and exponents" and "Recurrences: describing a value by smaller cases" cover counting steps, recursive calls, log₂ n and unrolling a recurrence.

ستتعلّم

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

شكل 4 · تفاعلي
01 / 10

A function that splits in two

This Python function counts the steps of a divide-and-conquer algorithm. A call on size 1 costs 1 step. A bigger call makes two calls on halves, then does n steps of its own. So T(n) = 2T(n/2) + n, with T(1) = 1.

the recurrence, read off the code:
  n == 1      return 1         T(1) = 1
  two calls   cost(n // 2) × 2  2·T(n/2)
  own work    + n               n

T(n) = 2T(n/2) + n
like merge sort: two halves, then a merge of n items
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A function that splits in two

    This Python function counts the steps of a divide-and-conquer algorithm. A call on size 1 costs 1 step. A bigger call makes two calls on halves, then does n steps of its own. So T(n) = 2T(n/2) + n, with T(1) = 1.

    the recurrence, read off the code:
      n == 1      return 1         T(1) = 1
      two calls   cost(n // 2) × 2  2·T(n/2)
      own work    + n               n
    
    T(n) = 2T(n/2) + n
    like merge sort: two halves, then a merge of n items
  2. The root: cost(8)

    Call cost(8). Leave its two calls aside for now: on its own, the call does 8 steps, the + n. Draw it as a block 8 units wide. It is the root of a recursion tree, and level 0 costs 8 steps.

    cost(8)
      own work: + n = 8 steps
      still to come: cost(4), cost(4)
    
    level 0: 1 call × 8 = 8
    block width = steps the call does itself
  3. Level 1: two halves

    The root’s two calls are cost(4) and cost(4). Each does 4 steps of its own, so each child block is half as wide. Level 1 has 2 calls: 2 × 4 = 8 steps, the same as level 0.

    cost(8) → cost(4), cost(4)
    
      level 0: 1 call  × 8    = 8
      level 1: 2 calls × 4    = 8
    
    twice the calls, half the work each
  4. Level 2: four quarters

    Each cost(4) calls cost(2) twice. Level 2 has 4 calls doing 2 steps each: 4 × 2 = 8 again. Going down a level, the number of calls doubles while each call’s own work halves.

    each cost(4) → cost(2), cost(2)
    
      level 0: 1 call  × 8    = 8
      level 1: 2 calls × 4    = 8
      level 2: 4 calls × 2    = 8
  5. Level 3: the leaves

    Each cost(2) calls cost(1) twice. Now n == 1, so these calls stop and return 1. They are the leaves: 8 × 1 = 8 steps. Halving 8 reaches 1 after 3 splits, because log₂ 8 = 3.

    each cost(2) → cost(1), cost(1)
    cost(1) hits n == 1: returns 1, no more calls
    
      level 0: 1 call  × 8    = 8
      level 1: 2 calls × 4    = 8
      level 2: 4 calls × 2    = 8
      level 3: 8 calls × 1    = 8
    
    8 → 4 → 2 → 1: 3 halvings = log₂ 8
  6. Add up the levels

    Add the levels: 4 levels of 8 steps, so T(8) = 32, which is what cost(8) returns. For any power of 2 there are log₂ n + 1 levels of n steps: T(n) = n log₂ n + n = Θ(n log n).

    T(8) = 8 + 8 + 8 + 8 = 32
    cost(8) returns 32
    
    in general (n a power of 2):
      log₂ n + 1 levels, n steps each
      T(n) = n log₂ n + n
      n = 1024: 10·1024 + 1024 = 11264
    T(n) = Θ(n log n)
  7. One shape, three numbers

    Many recursive algorithms share one shape: T(n) = a·T(n/b) + f(n). A call makes a calls on pieces of size n/b, plus f(n) steps of its own. Our function has a = 2, b = 2 and f(n) = n.

    T(n) = a·T(n/b) + f(n)
      a     calls each call makes      2
      b     how much the size shrinks  2
      f(n)  the call’s own steps       n
    
    level i: a^i calls, each f(n / b^i)
  8. Master theorem: a tie

    The master theorem compares f(n) with the number of leaves, n^(log_b a). Here that is n¹: 8 leaves, and f(n) = n grows just as fast. Every level costs the same, so T(n) = Θ(n log n).

    leaves = a^(log_b n) = n^(log_b a)
      2^3 = 8 = n¹
    f(n) = n¹: same exponent → balanced
    
      level 0: 1 call  × 8    = 8
      level 1: 2 calls × 4    = 8
      level 2: 4 calls × 2    = 8
      level 3: 8 calls × 1    = 8
      total = 32
    T(n) = Θ(n^(log_b a) · log n) = Θ(n log n)
  9. Four calls: the leaves win

    Make four calls instead: a = 4. Now there are 4³ = 64 leaves, that is n², which grows faster than f(n) = n. Level costs double: 8, 16, 32, 64. The leaves win: T(n) = Θ(n²).

    T(n) = 4T(n/2) + n
    leaves = 4^3 = 64 = n^(log₂ 4) = n²
    f(n) = n¹, exponent 1 < 2 → leaves dominate
    
      level 0: 1 call  × 8    = 8
      level 1: 4 calls × 4    = 16
      level 2: 16 calls × 2   = 32
      level 3: 64 calls × 1   = 64
      total = 120
    T(n) = Θ(n^(log_b a)) = Θ(n²)
  10. Bigger own work: the root wins

    Back to two calls, but now f(n) = n². Level costs halve: 64, 32, 16, 8, so the root’s own work wins: T(n) = Θ(n²). This case also needs a·f(n/b) ≤ c·f(n) for some c < 1; here c = ½.

    T(n) = 2T(n/2) + n²
    leaves = 2^3 = 8 = n¹
    f(n) = n², exponent 2 > 1 → root dominates
    check: 2·(n/2)² = ½·n², so c = ½ < 1
    
      level 0: 1 call  × 64   = 64
      level 1: 2 calls × 16   = 32
      level 2: 4 calls × 4    = 16
      level 3: 8 calls × 1    = 8
      total = 120
    T(n) = Θ(f(n)) = Θ(n²)

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