1 / 28

D&C, DP

D&C, DP. Divide and Conquer Dynamic Programming. Recursive Method…. Big Problem!!!. Big Problem!!!. Sub-Problem. Sub-Problem. Sub-Problem. Three major steps. Big Problem!!!. Solved!!!. Conquer. Divide. Merge. Sub-Problem. Solved. Sub-Problem. Solved. Sub-Problem. Solved.

larue
Télécharger la présentation

D&C, DP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. D&C, DP Divide and Conquer Dynamic Programming

  2. Recursive Method… Big Problem!!! Big Problem!!! Sub-Problem Sub-Problem Sub-Problem

  3. Three major steps Big Problem!!! Solved!!! Conquer Divide Merge Sub-Problem Solved Sub-Problem Solved Sub-Problem Solved Solved Solved Solved

  4. Stairs Climbing 每次可踏上一階或踏上兩階 眼前有四階樓梯, 每次可踏上一階或踏上兩階, 那麼爬完四階共有幾種踏法? = = + + = + f(3) 3 = f(2) 2 f(1) f(4) = f(3) f(2) + 1 + 5 3 2 f(4) = f(3) + f(2)

  5. Tiling Problem with L-shaped Tile

  6. Fast Exponentiation 7^13=96889010407 7^13=?

  7. 7^13=7^7 * 7^6 • 7^7= 7^4 * 7^3 • 7^6=7^3 * 7^3 • 7^4= 7^2 * 7^2 7^3=7^2 * 7^1 • 7^2= 7^1 * 7^1 • 7^1= 7

  8. Exception(Prune and search) Kth smallest(related to Quick sort) Binary Search

  9. Dynamic Programming(動態規劃) Programming  最佳化(Optimization) DP  運用”動態”的方式求最佳解 Dynamic 動態

  10. DP可視為 D&C 的延伸 f(4) f(2) f(3) f(0) f(1) f(1) f(2) f(0) f(1) Overlapping

  11. 讀取、計算、儲存 f(4) f(2) f(3) f(0) f(1) f(1) f(2) f(3) = f(2) + f(1) f(4) = f(3) + f(2) f(0) f(1) f(2) = f(0) + f(1)

  12. 讀取、計算、儲存 f(4) f(2) f(3) f(0) f(1) f(1) f(2) f(3) = 3 f(4) = 5 f(1) = 1 f(0) f(1) f(2) = 2 f(0) = 1

  13. DP的時間、空間複雜度 時間複雜度:O(1) 時間複雜度:O(n) f(n) = f(n-1) + f(n-2) 時間複雜度:O(s) 時間複雜度:O(n*s*1)

  14. DP的時間、空間複雜度 空間複雜度:O(n) 空間複雜度:以子問題的出現期間決定。 空間複雜度:O(1)

  15. Note on DP 2.決定問題的計算順序。 3.確認初始值、問題的計算範圍。 1.發現相似的求解方式,紀錄重複的子問題資訊 。

  16. Practice in Dp 2.Bottom-up, Iterative way. 1.Top-down, Recursive way.

  17. Stairs Climbing(初步思考) f(n) = { 1 , if n = 1 { 2 , if n = 2 { f(n-1) + f(n-2) , if n >= 3

  18. Stairs Climbing(美觀) f(n) = { 0 , if n < 0 [Exterior] { 1 , if n = 0 [Initial] { 1 , if n = 1 [Initial] { f(n-1) + f(n-2) , if n >= 2 and n <= 5 [Compute] { 0 , if n > 5 [Exterior]

  19. Stairs Climbing(不好的示範) int f(int n) { if (n == 0 || n == 1) return1; else return f(n-1) + f(n-2); }

  20. Stairs Climbing(Two ways) f(4) f(2) f(3) f(0) f(1) f(1) f(2) f(0) f(1) 2. Bottom-up, Iterative. 1.Top-down, Recursive.

  21. Stairs Climbing(Top-down) inttable[5];// 表格,儲存全部問題的答案。 boolsolve[5];// 記錄問題是否已計算 int f(int n) { if (n == 0 || n == 1) return1; if (solve[n]) return table[n];     table[n] = f(n-1) + f(n-2); // 將答案存入表格 solve[n] = true;            // 紀錄已計算 return table[n]; }

  22. Stairs Climbing(Top-down) f(n) = f(n-1) + f(n-2) 1.好處是不必斤斤計較計算順序。 2.只計算必要的問題,而不必計算整個陣列。

  23. Stairs Climbing(Top-down) f(4) f(2) f(3) f(n) = f(n-1) + f(n-2) f(0) f(1) f(1) f(2) f(0) f(1)  1.壞處是不斷呼叫函式,執行效率較差。 2.無法自由地控制計算順序,無法妥善運用記憶體。

  24. Stairs Climbing(Bottom-up) int table[5]; voiddp() { // initial for (inti=0; i<=4;i++)         table[i] = 0; table[0] = 1;     table[1] = 1; // compute for (inti=2; i<=4;i++)         table[i] = table[i-1] + table[i-2]; } Int table[5]; table[0] = 1; table[ 1] = 1;

  25. Stairs Climbing(Bottom-up) for (int i=0; i<=5; i++) table[i] = 0; table[2] = ? table[3] = ? for (int i=2; i<=5; i++)         table[i] = table[i-1] + table[i-2]; table[4] = ? 訂定一個計算順序,然後由最小的問題開始計算。 其特色是程式碼通常只有幾個迴圈。 這個方式的好處與壞處恰與前一個方式互補。

  26. The Summary of D&C, DP 2.可考慮使用表格紀錄出現頻繁的子問題數值。 3.To iterate is man, to recurse is divine. +智慧 靈感 = 經驗 1.找出結構相似之處(recursion),設計一套公式(recurrence)。

  27. UVA DP 623 900 11069 10446 495 D&C 11038 620 10230 920 374

More Related