Try   HackMD
tags: ADA 3.3 Master Method

ADA 3.3: Master Method 大師套公式法

Textbook Chapter 4.5 - The master method for solving recurrences

Master Theorem

The proof is in Ch. 4.6 課本 p.118

divide a problem of size n into a subproblems, each of size

nb is solved in time
T(nb)
recursively

Let

T(n) be a positive function satisfying the following recurrence relation

T(n)={O(1)if n1aT(nb)+f(n)if n>1,

符合上面的格式,就可以套用公式

where

a1 and
b>1
are constants.

  • Case 1: If
    f(n)=O(nlogbaϵ)
    for some constant
    ϵ>0
    , then
    T(n)=Θ(nlogba)
    .
  • Case 2: If
    f(n)=Θ(nlogba)
    , then
    T(n)=Θ(nlogbalogn)
    .
  • Case 3: If
    • f(n)=Ω(nlogba+ϵ)
      for some constant
      ϵ>0
      , and
    • af(nb)cf(n)
      for some constant
      c<1
      and all sufficiently large
      n
      , then
      T(n)=Θ(f(n))
      .

💡 Compare

f(n) with
nlogba

Recursion-Tree for Master Theorem

T(n)=aT(nb)+f(n)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

複習一下數學

對數:

log223=3,logbbn=n

a 的次方數就是展開的層數,和b的次方的成長速度是一樣的,所以如果可以得到b的次方數的話就可以知道a的次方數

可以看到最後

T(1) 時代表 n 就等於他的分母

例如:最後一層是

b3 好了那 a 的次方就可以這樣表示
alogbb3=a3

只是我們不知道會展開幾層所以只能用 n 代替,所以這裡才會寫成

alogbnT(1)

Recursion-Tree for Master Theorem(2)

回到之前的計算

T(n)=f(n)+af(nb)+a2f(nb2)+a3f(nb3)+...+alogbnT(1)

alogbn=nlognalogbn=nlogba

alogbnT(1)=nlogbaT(1)

為什麼要換成 n 為底呢?因為 n 是我們的 problem size 用 n 為底的話比較好判斷,也就會跟上面三個 case 長得差不多了

複習一下數學(2)

  1. alogbn 是怎麼變成
    nlognalogbn
    的呢?

    1. 需要的知識是對數律
      loga(bn)=n(logab)
      → 真數 n 次方 ,對數 n 倍
    2. 還有
      3log34=4,nlogna=a

    知道這些之後就可以開始推導

    alogbn=nlognalogbn 再用對數律真數 n 次方 ,對數 n 倍

    =nlognalogbn

  2. alogbn 又是怎麼變成
    nlogba
    的呢?

    1. 這裡老師就有解釋了使用換底公式
      logab=logcblogca

    知道換底公式之後就可以再從上面的結果繼續推導

    nlognalogbn=nlogbalogbnlogbn

    =nlogba

Three Case

  • T(n)=aT(nb)+f(n)

    • a1
      , the number of subproblems
    • b>1
      , the factor by which the subproblem size decreases
    • f(n)=
      work to divide/combine subproblems

    T(n)=f(n)+af(nb)+a2f(nb2)+a3f(nb3)+...+alogbnT(1)

  • Compare

    f(n) with
    nlogba

    1. Case 1:
      f(n)
      grows polynomially slower then
      nlogba
    2. Case 2:
      f(n)
      and
      nlogba
      grow at similar rate
    3. Case 3:
      f(n)
      grows polynomially faster then
      nlogba

Case 1: Total dominated by leaves

ex.

T(n)=9T(n3)+n,T(1)=1

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

💡

f(n) grows polynomially slower then
nlogba

T(n)=(1+93+(93)2+...+(93)log3n)n

💡 等比級數公式

a1=a,an=arn1,Sn=a(1rn)1r

=(93)1+log3n131n

=3n29log3n3log3n12n

=3n2nlog39n12n

💡 這裡的

9log3n 是如何變成
nlog39
請參考上面的 複習一下數學(2)

=Θ(nlog39)=Θ(n2)

  • Case 1: If
    f(n)=O(nlogbaϵ)
    for some constant
    ϵ>0
    , then
    T(n)=Θ(nlogba)
    .

Case 2: Total cost evenly distributed among levels

ex.

T(n)=T(2n3)+1,T(1)=1

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

a=1,b=32帶入公式
nlogba

💡 這裡需要知道的知識是

logb1 是多少,這代表的意義是 b 的幾次方是 1,我們知道 0 次方會是 1

nlog321=n0

💡

f(n) and
nlogba
grow at similar rates

T(n)=T(2n3)+1,T(1)=1

T(n)=1+1+1+...+1

=log32n+1

=Θ(logn)

  • Case 2: If
    f(n)=Θ(nlogba)
    , then
    T(n)=Θ(nlogbalogn)

Case 3: Total cost dominated by root cost

ex.

T(n)=3T(n4)+n5,T(1)=1

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

💡

f(n) grows polynomially faster then
nlogba

T(n)=3T(n4)+n5,T(1)=1

T(n)=(1+345+(345)2+...+(345)log4n)n5

T(n)>n5

💡 帶入等比級數相加公式

Sn=a(1rn)1r

T(n)11345n5

💡 其中的

(345)log4n應該是太小被省略了

T(n)=Θ(n5)

  • Case 3: If
    • f(n)=Ω(nlogba+ϵ)
      for some constant
      ϵ>0
      , and
    • af(nb)cf(n)
      for some constant
      c<1
      and all sufficiently large
      n
      , then
      T(n)=Θ(f(n))
      .

Example

💡 compare

f(n) with
nlogba

  • Case 1: If

    T(n)=9T(n/3)+n, then
    T(n)=Θ(n2)

    Observe that

    n=O(n2)=O(nlog39)

  • Case 2: If

    T(n)=T(2n/3)+1, then
    T(n)=Θ(logn)

    Observe that

    1=Θ(n0)=Θ(nlog3/21)

  • Case 3: If

    T(n)=3T(n/4)+n5, then
    T(n)=Θ(n5)

    • n5=Ω(nlog43+ϵ)
      with
      ϵ=0.00001
    • 3(n4)5cn5
      with
      c=0.99999

Floors and Ceilings

  • Master theorem can be extended to recurrences with floors and ceilings
  • The proof is in the Ch. 4.6

T(n)=aT(nb)+f(n)

T(n)=aT(nb)+f(n)

Theorem 1

試著用 Master method 套用到之前的 Merge Sort

T(n)={O(1)if n=12T(n/2)+O(n)if n2

T(n)=O(nlogn)

  • Case 2

f(n)=Θ(n)=Θ(n1)=Θ(nlog22)=Θ(nlogba)

T(n)=Θ(f(n)logn)=O(nlogn)

Theorem 2

試著用 Master method 套用到之前的 Bitonic Champion Problem

T(n)={O(1)if n=12T(n/2)+O(1)if n2T(n)=O(n)

  • Case 1

f(n)=O(1)=O(n)=O(nlog22)=O(nlogba)

T(n)=Θ(nlog22)=Θ(n)

Theorem 3

試著用 Master method 套用到之前的改進的 Bitonic Champion Problem

T(n){O(1)if n=1T(n/2)+O(1)if n2T(n)=O(logn)

f(n)=Θ(1)=Θ(n0)=Θ(nlog21)=Θ(nlogba)

T(n)=Θ(f(n)logn)=O(logn)


Andy結論

需要注意的是, 不論大 O 記法還是大 Θ 記法還是大 Ω 記法, 默認討論的都是最壞情況, 並不是像很多高票答主說的那樣, 大 Ω 記法就是討論最好情況了. 它們三個描述的都是同一個估計值 (比如某算法的最壞時間複雜度), 只是 O 描述這個估計值的上限, Ω 描述這個估計值的下限, 而 Θ 描述的是這個估計值的上限和下限相同 (即很確定了, 就在這一階了).