Ch4:Divide-and-Conquer
分治法是透過將原問題分解成幾個規模較小、形式相同或相似的子問題,遞迴地解決各子問題,最後再將這些子問題的解合併,得到原問題的解。
基本步驟
所有分治法皆可拆分為三個步驟:
- Divide(分割):把大規模問題分割成好幾個小問題
- Conquer(征服):對每個小問題遞迴求解
- Combine(合併):整合小問題的答案,形成原問題的答案
範例:矩陣乘法
矩陣乘法可以透過分治法來加快執行時間
- 問題:
- 先把 矩陣分割成 4 個 矩陣
- 我們必須求出這八個小乘法然後加起來,就可以得到 C 的答案
- Presudo code:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- 遞迴式:
遞迴式轉一般式的解法
替換法(Substitution Method):
- 先猜測遞迴式的解
- 利用 數學歸納法 或 直接替換來證明
遞迴樹法(Recursion-Tree Methods):
- 將遞迴關係化成一顆樹,每一層代表一次分割子問題
- 步驟:
- 範例:
- 先畫出第一層:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- 然後畫出第二層:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- 第三層:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- 依此類推,然後每一層加起來找規律:
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
主定理(Master Theorem)
- 當遞迴式為 時:
- Case 1:當
- Case 2:當
- Case 3:當