Chapter 1 Chang ChiChung 2011 09 22 n
- Slides: 40
Chapter 1 Chang Chi-Chung 2011. 09. 22
演算法的表示方法 n Natural language l Chinese、English n Graphic representation l Flow Chart n Pseudo language
演算法分析方法 n 實驗分析 n 漸進分析 n 攤銷分析 Time Complexity Space Complexity
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 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 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 of the array A sum 0; for i 0 to n-1 do sum + A[i]; return sum;
Asymptotic Notation(O, , )
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 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 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 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. (函數自己呼叫自己) 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 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) 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. 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 1 50 2 51 3
- Joe chang
- Dr tc chang
- Jichuan chang
- Darrick chang icfo
- Kai wei chang
- Peter chang jewelry
- Fay chang google
- Khởi nghĩa chàng lía nổ ra ở đâu
- Bor-yuh evan chang
- Ching chang walla walla bing bang song
- Chang pao chinese clothing
- Lpity
- 25 m/s
- Hee chang trading co. ltd
- Winnie chang fiu
- Zeinab chahine
- Chang wah
- Chang pui chung memorial school
- Thêu giáp tỉa
- Conclusion machine learning
- Angel x chang
- Conrad chang
- Rong chang dictation
- Paoti chang
- Chia hui chang
- Chẳng may em đánh vỡ một lọ hoa đẹp
- Chúa chăn nuôi tôi, tôi chẳng thiếu thốn chi
- Post classical vietnam
- Chang'an
- Johan chang
- Chẳng nhìn thấy ve đâu
- Chang horing
- Language change theorists
- Chia hui chang
- Jichuan chang
- Chemistry by raymond chang 10th edition
- Chang cgi
- Remco chang
- 1939
- Vodstvo azie
- Adxl 50