演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5...

27
演演演演演 演演演演演 (Algorithms) (Algorithms) 國國國國國國 國國國國國國 國國國國國 國國國國國國 國國國國國國 國國國國國 Course 5 演演演演演 Divide-and-Conquer
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    296
  • download

    20

Transcript of 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5...

Page 1: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

演算法課程 演算法課程 (Algorithms)(Algorithms)

國立聯合大學 資訊管理學系 陳士杰老師國立聯合大學 資訊管理學系 陳士杰老師

Course 5

切割與征服Divide-and-Conquer

Page 2: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

2國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

▓ Outlines

本章重點 Divide-and-Conquer 策略的描述 Binary Search Merge Sort The Divide-and-Conquer Approach Quick Sort Strassen's Matrix Multiplication Algorithm When Not to Use Divide-and-Conquer

Page 3: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

3國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

The divide-and-conquer approach is a top-downtop-down approach.

It dividesdivides a problem into two or more smaller problems. The smaller problems are usually instances of the original problem.

If solutions to the smaller problems can be obtained

readilyreadily, the solution to the original problem can be obtained by combining these solutionscombining these solutions.

If the smaller problems are still too largetoo large to be solved readily, they can be divided into still smaller problemsstill smaller problems.

This process of dividing the problems continues until

they are they are so small that a solution is readily so small that a solution is readily

obtainableobtainable.

▓ Divide-and-Conquer 策略的描述與技巧

Page 4: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

4國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

Def: 可將母問題切割成較小的問題 較小的問題 ( 切割 ) ,使用相同的解決程序相同的解決程序

加以處理 ( 征服 ) 。所有小問題的解可以成為母問題的最後解 ; 若有必要,則再將每個小問題的處理結果加以合併,就可以得到最後的答案。

由於使用相同的解決程序處理每個小問題,這一個程序就會被遞迴呼叫,因此一個遞迴演算法則通常以一個副程式的型式出現,內部包含一個解決程序與遞迴呼叫。

對於具有遞迴關係遞迴關係的問題,或是一些採用遞迴定義的資料結構,都適合採用 Divide-and-Conquer 演算法設計策略

最簡潔、易懂 效率差 (∵ 採用遞迴設計 )

Page 5: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

5國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

下列兩種情況是適合使用 Divide-and-Conquer 設計策略 ( 也是遞迴演算法的適用時機 ):

問題本身具有遞迴關係問題本身具有遞迴關係 母問題可被切割成較小的 “相同相同” 問題 如 : 階乘問題、費氏數問題、河內塔問題、快速排序問題、二

元搜尋問題…等 資料結構屬於遞迴定義資料結構屬於遞迴定義

大量的 Data Set ,在切割後仍為一組具 “相同性質相同性質” 的 Data Set

如 : 二元樹 (Binary Tree) 、鏈結串列 (Link List)… 等

Divide-and-Conquer 使用時機

Page 6: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

6國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

遞迴演算法則的設計1. 找出問題的終止條件終止條件 .2. 找出問題本身的遞迴關係 遞迴關係 (( 遞迴呼叫遞迴呼叫 )). 技巧 :

思考遞迴呼叫需要哪些參數 ? 遞迴呼叫的傳回值為何 ? 遞迴呼叫的終止條件為何 ? 終止傳回何值 ?

Procedure Recursion_subroutine(Parameter);{ if ( 終止條件終止條件 ) then Return(); else Recursion_subroutine(New_parameter)Recursion_subroutine(New_parameter) ;}

Page 7: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

7國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

▓ Binary Search ( 二分搜尋 ) 實施前提 :

檔案中記錄須事先由小到大由小到大排序過 須由 Random (Random ( 或或 Direct) accessDirect) access 之機制支援 (e.g., Array)

觀念 : 每次皆與 Search 範圍的中間記錄中間記錄進行比較 !!

while ( l u )

比較 (k, S[m]) case “=”: found, i = m, return i; case “<”: uu = m-1; case “>”: ll = m+1;recurn 0;

小 大

2

middleul

2

mul

l umiddleu lm m

S

// 找到了// 要找的資料在左半部// 要找的資料在右半部

Page 8: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

8國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

分析利用 Time function T(n) = T(n/2) + O(1) = T(n/2) + c = (T(n/4 + c)) + c = T(n/4) + 2c = (T(n/8) + c) + 2c = T(n/8) +3c = … = T(n/n) + log2nc

= T(1) + c log2n (T(1) = 1, c 為大於 0 的常數 )

= 1 + c log2n

T(n) = O(log2n)

Page 9: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

9國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

The steps of Binary Search can be summarized as follows. If x equals the middle item, quit. Otherwise:

DivideDivide the array into two subarrays about half as large. If x is smaller thansmaller than the middle item, choose the left subarrayleft subarray. If x is lala

rger thanrger than the middle item, choose the right subarrayright subarray. ConquerConquer (solve) the subarray by determining whether x is in that

subarray. Unless the subarray is sufficiently small, use recursionrecursion to do this.

ObtainObtain the solution to the array from the solution to the subarray. Binary Search is the simplest kind of divide-and-conquer

algorithm because the instance is broken down into only one smaller instance, so there is no combinationno combination of outputs.

Page 10: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

10國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

觀念 : 將兩個已排序過的記錄合併,而得到另一個排序好的記

錄。可分為兩種類型 :

Recursive ( 遞迴 ) Iterative ( 迴圈 , 非遞迴 )

▓ Merge Sort ( 合併排序 )

Page 11: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

11國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

Recursive Merge Sort ( 遞迴合併排序 )將資料量 n 切成 n/2 與 n/2 兩半部兩半部,再各自M

erge Sort ,最後合併兩半部之排序結果即成。

切割資料量 n 的公式為 :

[ ]: Run, 已排序好已排序好的檔案記錄Run 的長度 : Run 中記錄個數記錄個數

2high)(low

Page 12: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

12國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

Stack

第一層切割所有資訊依序輸入

第二層切割所有資訊依序輸入

第三層切割所有資訊依序輸入

第四層切割所有資訊依序輸入

Page 13: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

13國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

Time-Complexity

Avg. / Worst / Best Case: O(n log n)O(n log n)以 Recursive Merge Sort角度 :[ 說明 ]:

時間函數 : T(n) = T(n/2) + T(n/2) + cn時間複雜度求法 :

遞迴樹遞迴樹 步驟 :

將原本問題照遞迴定義展開 計算每一層的 Cost 加總每一層的 Cost即為所求

數學解法數學解法

最後合併左右兩半部合併左右兩半部所花時間 ∵ 左、右半部排好之後,各只剩一個 Run ,且兩半部各有兩半部各有n/2n/2 的資料量的資料量,其最後一次合併時的比較次數 “最多最多”為 nn/2 + n/2 -1/2 + n/2 -1 次,即約 n-1 次 (slide 72)

時間的表示可為 ccn n 次次(∵ 為線性時間線性時間 ))

左半部遞迴 右半部遞迴

Page 14: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

14國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

Mergesort involves the following steps : DivideDivide the array into two subarrays each with n/2 items. ConquerConquer (solve) each subarray by sorting it. Unless the array i

s sufficiently small, use recursion to do this. CombineCombine the solutions to the subarrays by merging them into

a single sorted array.

Page 15: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

15國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

The divide-and-conquer design strategy involves the following steps:

DivideDivide a problem into one or more smaller problems.

ConquerConquer (solve) each of the smaller problem. Unless a smaller problem is sufficiently small, use

recursion to do this.

If necessaryIf necessary, combinecombine the solutions to the smaller problems to obtain the solution to the original problem.

Necessary: Merge sort

Unnecessary: Binary search

▓ The Divide-and-Conquer Approach

Page 16: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

16國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

▓ Quick Sort ( 快速排序 ) Avg. case 下,排序最快的 algo. Def:

將大且複雜的問題切成許多獨立的小問題切成許多獨立的小問題,再加以解決各小問題後,即可求出問題的 Solution 。

此即 “ Divide-and-ConquerDivide-and-Conquer” ( 切割並征服 ) 的解題策略。 觀念 :

將第一筆記錄視為 Pivot Key (樞紐鍵 (P.K.) ,或稱 Control Key) ,在 Pass 1 (第一回合 ) 後,可將 P.K.置於 “最正確” 的位置上。

Ex:

(經過 Pass 1)

把 P.K.擺在正確的位置 為切割切割的概念 (∴可使用遞迴遞迴 )

P.K.

P.K.

Ri Rj , Ri.key P.K. 且 Rj.key P.K.

Page 17: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

17國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

多顆 CPU 時的運算過程 :

Page 18: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

18國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

Time-Complexity

Best Case: O(n log n)O(n log n) P.K. 之最正確位置恰好將資料量恰好將資料量均分成二等份均分成二等份

以 Multiprocessor來看, 2 個 CPU 的工作量相等,工作可同時做完,沒有誰等誰的問題

[ 說明 ]:時間函數 : T(n) = cn + T(n/2) + T(n/2)時間複雜度求法 :

遞迴樹遞迴樹 步驟 :

將原本問題照遞迴定義展開 計算每一層的 Cost 加總每一層的 Cost即為所求

數學解法數學解法

變數 i 與 j 最多花 n 個執行時間找記錄 ( 即 : 決定 P.K. 最正確位置所花時間 )

左半部 右半部

Page 19: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

19國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

Worst Case: O(nO(n22)) 當輸入資料是由大到小由大到小或或由小到大由小到大排好排好時 (( 切割毫無用處切割毫無用處 ))

[ 說明 ]:

P.K.

P.K.

輸入資料輸入資料 :: 小 大

輸入資料輸入資料 :: 大 小

P.K.[0[0 筆筆 ]] [ n-1[ n-1 筆 筆

]]

P.K.[0[0 筆筆 ]][ n-1[ n-1 筆 筆

]]

Pass 1Pass 1

Pass 1Pass 1

Page 20: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

20國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

Average Case: O(n log n)O(n log n)[ 說明 ]:

, T(0) = 0cns)]T(n[T(s)n

1T(n)

n

1s

P.K.

S筆 (n-S)筆

Page 21: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

21國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

▓ Strassen's Matrix Multiplication Algorithm 矩陣乘法問題 (Matrix Multiplication Problem):

給定兩個方陣 A, B ,其 Size均為 nn ,其中 n=2n=2kk 。如果 n不是2 的冪次方,則可以增加額外的列與行增加額外的列與行,但是補上的元素都是零零 :

若矩陣是扁的,則可以在該矩陣下方補上數列列的 0,使之成為方陣 若矩陣是窄的,則可以在該矩陣右方補上數行行的 0,使之成為方陣

欲求 C = A B ,傳統的矩陣乘法 :

c11 = a11 b11+a12 b21

c12 = a11 b12+a12 b22

c21 = a21 b11+a22 b21

c22 = a21 b12+a22 b22

Page 22: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

22國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

該演算法的時間複雜度為 O(nO(n33)) ,乘法運算比加法運算要來得多。 前例的乘法有 8個,加法有 4 個。

然而,就系統執行的角度來說,乘法運算的複雜度遠超過加法運算,因此該演算法在實際執行的速度會更慢。

Page 23: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

23國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

將矩陣乘法問題放大來看 :

C11 = A11 B11 + AA1212 BB2121

C12 = AA1111 BB1212 + AA1212 BB2222

C21 = AA2121 BB1111 + AA2222 BB2121

C22 = AA2121 BB1212 + AA2222 BB2222

遞迴方程式為 : T(n) = 8T(n/2) +cn2

由支配理論可以得知該遞迴方程式最後可以得到 (n(n33))

Cij, Aij, Bij 皆為子矩陣子矩陣,即可用遞迴切割遞迴切割的方式來將此矩陣切割成數個小矩陣。

Page 24: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

24國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

In 1969, Strassen published an algorithm whose time complexity is better than cubic in terms of both multiplications and additions/subtractions.

Strassen's method requires 7 multiplications an7 multiplications and 18 additions/subtractid 18 additions/subtractionsons, whereas the straightforward method requires 8 multiplications an8 multiplications and 4 additions/ subtractid 4 additions/ subtractionsons .

Page 25: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

25國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

遞迴方程式為 : T(n) = 7T(n/2) +cn2

由支配理論可以得知該遞迴方程式最後可以得到 (nlg7)

Page 26: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

26國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

If possible, we should avoid divide-and-conquer in the following two cases:

1) An problem of size n is divided into two or more problems each almost of size almost of size nn.

2) An problem of size n is divided into almost almost nn probl problemsems of size size n/cn/c, where c is a constant.

The first partitioning leads to an exponential-exponential-time algorithmtime algorithm, where the second leads to a nnΘ(lg Θ(lg nn)) algorithm.

▓ When Not to Use Divide-and-Conquer

Page 27: 演算法課程 (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 5 切割與征服 Divide-and-Conquer.

27國立聯合大學 資訊管理學系 演算法課程 ( 陳士杰 )

Sometimes, on the other hand, a problem requires exponentialityexponentiality, and in such a case there is no reason to avoid the simple divide-and-conquer solution.

河內塔問題每呼叫一次就需搬動圓盤一次,當圓盤的個數 n 是 64 時,總共需要搬動圓盤 264-1次,因此演算法的複雜度等級 (order) 是 O(2n)

即 : 河內塔問題的圓盤搬動次序是與 n 成指數關係指數關係

但是經過証明上述河內塔問題的演算法,已經是給定該問題的限制下最佳的演算法則了。