Chapter 1 Chang ChiChung 2011 09 22 n

  • Slides: 40
Download presentation
Chapter 1 Chang Chi-Chung 2011. 09. 22

Chapter 1 Chang Chi-Chung 2011. 09. 22

演算法的表示方法 n Natural language l Chinese、English n Graphic representation l Flow Chart n Pseudo

演算法的表示方法 n Natural language l Chinese、English n Graphic representation l Flow Chart n Pseudo language

演算法分析方法 n 實驗分析 n 漸進分析 n 攤銷分析 Time Complexity Space Complexity

演算法分析方法 n 實驗分析 n 漸進分析 n 攤銷分析 Time Complexity Space Complexity

Example: 虛擬碼 Algorithm array. Max(A, n) Input: An array A storing n>=1 integers Output:

Example: 虛擬碼 Algorithm array. Max(A, n) Input: An array A storing n>=1 integers Output: The maximun element in A current. Max A[0] for i 1 to n - 1 do if current. Max < A[i] then current. Max A[i] return current. Max

演算法的計數分析(1) Algorithm array. Max(A, n) Input: An array A storing n>=1 integers Output: The

演算法的計數分析(1) Algorithm array. Max(A, n) Input: An array A storing n>=1 integers Output: The maximun element in A 2 current. Max A[0] 1+n for i 1 to n - 1 do 2 if current. Max < A[i] then 2 current. Max A[i] 2 return current. Max 1 5 n 至 n-1 7 n-2

演算法的計數分析(2) Algorithm recursive. Max(A, n) Input: An array A storing n>=1 integers Output: The

演算法的計數分析(2) Algorithm recursive. Max(A, n) Input: An array A storing n>=1 integers Output: The maximun element in A if n = 1 then return A[0] return max { recursive. Max(A, n-1), A[n-1]} T(n) = 3 if n=1 T(n-1) + 7 other

An Exercise Algorithm Sum(A, n) Input: An array A storing n>=1 integers Output: Sum

An Exercise Algorithm Sum(A, n) Input: An array A storing n>=1 integers Output: Sum of the array A sum 0; for i 0 to n-1 do sum + A[i]; return sum;

Asymptotic Notation(O, , )

Asymptotic Notation(O, , )

Exercises n 證明 2 n 3 + 4 n 2 log n 是 O(n

Exercises n 證明 2 n 3 + 4 n 2 log n 是 O(n 3) n 證明 3 log n + log log n 是 (log n)

Order the Time Complexity O(1) < O(logn) < O(nlogn) < O(n 2) < O(n

Order the Time Complexity O(1) < O(logn) < O(nlogn) < O(n 2) < O(n 3) < O(2 n) < O(n!) 1 2 4 8 16 32 0 1 2 3 4 5 0 2 8 24 64 160 1 4 16 64 256 1024 1 8 64 512 4096 32, 768 2 4 16 256 65, 536 4, 294, 967, 296 1 2 24 40, 320 20, 922, 789, 888, 000 …….

Example prime ( int n ) { int k; if (n <= 1) return

Example prime ( int n ) { int k; if (n <= 1) return false; k = 2; while ( k < n ) { if ( n % k == 0 ) return false; k++; } return true; } O(1) O(n) O(1) O(n)

範例:計算向量內積 需要 O(n) 時間。 int inner_product (int u[], int v[], int n) { int

範例:計算向量內積 需要 O(n) 時間。 int inner_product (int u[], int v[], int n) { int i, sum = 0; for (i = 0; i < n; i++) sum += u[i]*v[i]; return sum; } n

Recursive Algorithm n Direct Recursive l The functions call themselves before they is done.

Recursive Algorithm n Direct Recursive l The functions call themselves before they is done. (函數自己呼叫自己) n Indirect Recursive l The functions call other functions that again invoke the calling function. l Example: function A function B function A

Recursive Programming Style Function recursive() { if (終止條件判斷式) return; else (遞迴呼叫方式); } //成立,終止遞迴

Recursive Programming Style Function recursive() { if (終止條件判斷式) return; else (遞迴呼叫方式); } //成立,終止遞迴

Recursive Example – 求 n! n iterative version n recursive version int factor(int n)

Recursive Example – 求 n! n iterative version n recursive version int factor(int n) { int result=1; for (int i=n; i>=1; i--) { result *= i; } return result; } int factor(int n) { if (n==1) return 1; else return n*factor(n-1); } O(n)

Performance Analysis n recursive version 1 int factor(int n) 2 { 3 if (n==1)

Performance Analysis n recursive version 1 int factor(int n) 2 { 3 if (n==1) 4 return 1; 5 else 6 return n*factor(n-1); 7 } n 每次遞迴需 2 steps l line 3 與 line 6 n T(1) =2 (line 3 與 line 4) n Recursive Formula T(n) = T(n-1) + 2 = T(n-2) + 4 =. . . = T(1) + 2*(n-1) = 2 n n O(n)

Recursive Example: HANOI Towers //n is amount of disks, source, dest, temp are towers.

Recursive Example: HANOI Towers //n is amount of disks, source, dest, temp are towers. void tower(int n, char source, char dest, char temp) { static int step; if (n==1) { step++; cout << "Step " << step << ": desk " << n << " from " << source << " move to " << dest << endl; } else { tower(n-1, source, temp, dest); step++; cout << "Step " << step << ": desk " << n << " from " << source << " move to " << dest << endl; tower(n-1, temp, dest, source); } }

以下是一個輸出入的實例: Sample Input Sample Output AAABBC 122333 67 1 66 2 65 3 49

以下是一個輸出入的實例: Sample Input Sample Output AAABBC 122333 67 1 66 2 65 3 49 1 50 2 51 3