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 is solved in time recursively
Let be a positive function satisfying the following recurrence relation
符合上面的格式,就可以套用公式
where and are constants.
- Case 1: If for some constant , then .
- Case 2: If , then .
- Case 3: If
- for some constant , and
- for some constant and all sufficiently large , then .
Recursion-Tree for Master Theorem
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 的次方數就是展開的層數,和b的次方的成長速度是一樣的,所以如果可以得到b的次方數的話就可以知道a的次方數
可以看到最後 時代表 n 就等於他的分母
例如:最後一層是 好了那 a 的次方就可以這樣表示
只是我們不知道會展開幾層所以只能用 n 代替,所以這裡才會寫成
Recursion-Tree for Master Theorem(2)
回到之前的計算
為什麼要換成 n 為底呢?因為 n 是我們的 problem size 用 n 為底的話比較好判斷,也就會跟上面三個 case 長得差不多了
複習一下數學(2)
-
是怎麼變成 的呢?
- 需要的知識是對數律 → 真數 n 次方 ,對數 n 倍
- 還有
知道這些之後就可以開始推導
再用對數律真數 n 次方 ,對數 n 倍
-
又是怎麼變成 的呢?
- 這裡老師就有解釋了使用換底公式
知道換底公式之後就可以再從上面的結果繼續推導
Three Case
-
- , the number of subproblems
- , the factor by which the subproblem size decreases
- work to divide/combine subproblems
-
Compare with
- Case 1: grows polynomially slower then
- Case 2: and grow at similar rate
- Case 3: grows polynomially faster then
Case 1: Total dominated by leaves
ex.
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 →
💡 grows polynomially slower then
- Case 1: If for some constant , then .
Case 2: Total cost evenly distributed among levels
ex.
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 →
帶入公式
💡 這裡需要知道的知識是 是多少,這代表的意義是 b 的幾次方是 1,我們知道 0 次方會是 1
💡 and grow at similar rates
Case 3: Total cost dominated by root cost
ex.
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 →
💡 grows polynomially faster then
- Case 3: If
- for some constant , and
- for some constant and all sufficiently large , then .
Example
-
Case 1: If , then
Observe that
-
Case 2: If , then
Observe that
-
Case 3: If , then
Floors and Ceilings
- Master theorem can be extended to recurrences with floors and ceilings
- The proof is in the Ch. 4.6
Theorem 1
試著用 Master method 套用到之前的 Merge Sort
→
Theorem 2
試著用 Master method 套用到之前的 Bitonic Champion Problem
Theorem 3
試著用 Master method 套用到之前的改進的 Bitonic Champion Problem
Andy結論
需要注意的是, 不論大 O 記法還是大 Θ 記法還是大 Ω 記法, 默認討論的都是最壞情況, 並不是像很多高票答主說的那樣, 大 Ω 記法就是討論最好情況了. 它們三個描述的都是同一個估計值 (比如某算法的最壞時間複雜度), 只是 O 描述這個估計值的上限, Ω 描述這個估計值的下限, 而 Θ 描述的是這個估計值的上限和下限相同 (即很確定了, 就在這一階了).