The DivideandConquer Strategy 1 The DivideandConquer Strategy n

  • Slides: 71
Download presentation
The Divide-and-Conquer Strategy (個各擊破)

The Divide-and-Conquer Strategy (個各擊破)

演算法方式總覽 1. The Divide-and-Conquer Strategy (個各擊 破) n binary Searching、Quick Sort…. 2. The Greedy

演算法方式總覽 1. The Divide-and-Conquer Strategy (個各擊 破) n binary Searching、Quick Sort…. 2. The Greedy Method(貪婪演算法) n Prim MST、Kruskal MST、Djikstra's algorithm 3. Dynamic Programming(動態演算法) n 二項是係數、矩陣連乘、最佳二元搜尋樹… 4. Trace Back(回溯) n 圖形著色、漢米爾迴路問題…. 5. Tree Searching Strategy(樹的追蹤)

階乘 求n!,一般可能用的規則: n! = n * (n-1)!; 利用迴圈: ans = 1; for (i =

階乘 求n!,一般可能用的規則: n! = n * (n-1)!; 利用迴圈: ans = 1; for (i = 2; i <= n; i++) ans = ans * i; 利用遞迴: int fact(int n) { int x, y; if (n == 0) // boundary condition return(1); x = n-1; y = fact(x); return(n*y); } /* end fact */

遞迴呼叫流程圖 int fact(int n) { int x, y; if (n == 0) // boundary

遞迴呼叫流程圖 int fact(int n) { int x, y; if (n == 0) // boundary condition return(1); x = n-1; y = fact(x); return(n*y); } /* end fact */ n=4 fact(4) n=3 … y=fact(3 ) Fact(4)=24 fact(3) n=2 … Fact(3)=6 y=fact(2 ) fact(2) n=1 … Fact(2)=2 y=fact(1) n=0 … … Fact(1)=1 y=fact(0 ) fact(0) Fact(0)=1 Return(1)

遞迴呼叫流程圖 n 練習:fact(5)的呼叫過程,遞迴呼叫流程圖為 何。 n=5 int fact(int n) { int x, y; if (n

遞迴呼叫流程圖 n 練習:fact(5)的呼叫過程,遞迴呼叫流程圖為 何。 n=5 int fact(int n) { int x, y; if (n == 0) // boundary condition return(1); x = n-1; y = fact(x); return(n*y); } /* end fact */

遞迴推疊圖 (The recursive stack) 呼叫過程: f 4 f 3 f 2 f 1 f

遞迴推疊圖 (The recursive stack) 呼叫過程: f 4 f 3 f 2 f 1 f 0 10

 (i)printf(“%d”, fact(3)) n x y The stack at various times during execution. (An

(i)printf(“%d”, fact(3)) n x y The stack at various times during execution. (An asterisk indicates an uninitialized value. ) 11

費氏數列 n 費氏數列 (Fibonacci number): f(0) = 0; * f(1) = 1; f(2) =

費氏數列 n 費氏數列 (Fibonacci number): f(0) = 0; * f(1) = 1; f(2) = f(1) + f(0); f(3) = f(2) + f(1); : f(n) = f(n-1) + f(n-2); n 利用遞迴範例: int fib(int n) { int x, y; if (n <= 1) return(n); x = fib(n-1); y = fib(n-2); return(x+y); } /* end fib */

遞迴呼叫流程圖 fib(4) … x=fib(3) n=3 fib(3)=2 … x=fib(2) n=1 fact(2) fib(3) n=2 … fib(1)=1

遞迴呼叫流程圖 fib(4) … x=fib(3) n=3 fib(3)=2 … x=fib(2) n=1 fact(2) fib(3) n=2 … fib(1)=1 n=0 fib(2)=1 n=1 fact(2) n=2 y=fib(2) … fib(0) Return(0) fib(1)=1 n=1 fib(1)=1 n=0 y=fact(0) fib(4)=3 fib(0)=0 Return(1) fib(1) Return(1) x=fib(1) fib(2)=1 Return(1) x=fib(1) y=fact(0) y=fib(1) fib(0)=0 fib(0) Return(0) n=4 int fib(int n) { int x, y; if (n <= 1) return(n); x = fib(n-1); y = fib(n-2); return(x+y); } /* end fib */

遞迴推疊圖 (The recursive stack) 呼叫過程: f 4 f 3 f 2 f 1 15

遞迴推疊圖 (The recursive stack) 呼叫過程: f 4 f 3 f 2 f 1 15 f 2 f 1 f 0

遞迴推疊圖 f 5 呼叫過程: f 4 f 3 f 2 f 1 17 f

遞迴推疊圖 f 5 呼叫過程: f 4 f 3 f 2 f 1 17 f 2 f 1 f 0 f 1 f 2 f 0 f 1 f 0

A general divide-and-conquer algorithm Step 1: If the problem size is small, solve this

A general divide-and-conquer algorithm Step 1: If the problem size is small, solve this problem directly; otherwise, split the original problem into 2 sub-problems with equal sizes. Step 2: Recursively solve these 2 sub-problems by applying this algorithm. Step 3: Merge the solutions of the 2 subproblems into a solution of the original problem.

29

29

35

35

37

37

 40

40

樞紐(pivot)

樞紐(pivot)

練習 1. finding the maximum of a set S of n numbers 2. 給下列陣列S,請用各個擊破演算法設計找

練習 1. finding the maximum of a set S of n numbers 2. 給下列陣列S,請用各個擊破演算法設計找 出最大的數 S 29, 14, 15, 1, 6, 10, 32, 12

Time complexity n Time complexity: n Calculation of T(n): Assume n = 2 k,

Time complexity n Time complexity: n Calculation of T(n): Assume n = 2 k, T(n) = 2 T(n/2)+1 = 2(2 T(n/4)+1)+1 = 4 T(n/4)+2+1 : =2 k-1 T(2)+2 k-2+…+4+2+1 =2 k-1 = n-1

Strassen矩陣相乘設計 58

Strassen矩陣相乘設計 58

Example

Example

Example

Example

Strassen 演算法

Strassen 演算法

68

68