1. introduction to algorithms
TRANSCRIPT
1
Dept. of Computer Science & Information Engineering
Introduction to Algorithms
Instructor: Yao-Ting Huang
Bioinformatics Laboratory,Department of Computer Science & Information Engineering,
National Chung Cheng University.
2
Dept. of Computer Science & Information Engineering
The Course
Introduction to the design and analysis of algorithms. This is not a programming course. This course tells you what is a “good” program.
How do you define a good programmer? Fast coding No bugs Reusability Algorithm
3
Dept. of Computer Science & Information Engineering
Text Book Textbook: Introduction to Algorithms (Secon
d Edition). Written by Cormen, Leiserson, Rivest, and Stein. Published by MIT Press and McGraw-Hill.
4
Dept. of Computer Science & Information Engineering
Another Excellent Reference Book Introduction to Algorithms: A Creative Approac
h. Written by Udi Manber. Published by Addison-Wesley.
5
Dept. of Computer Science & Information Engineering
Grading Policy
Midterm (Close Book) - 40% Final (Close Book) - 40% Homework - 20% Quiz - extra 10%
6
Dept. of Computer Science & Information Engineering
Teaching Information Instructor
Yao-Ting Huang: [email protected] Office: 511 Office hours: 11AM~12AM, Thursday.
Teaching Assistants 吳顯如: [email protected]廖基復: [email protected] Lab: 405 TA hours: 10AM~11AM, Friday.
7
Dept. of Computer Science & Information Engineering
What is Algorithm?
An algorithm is a sequence of computational steps that transform input into the output. In terms of software, an algorithm is considered to be
“good” if the CPU time and memory usage are minimized.
8
Dept. of Computer Science & Information Engineering
Algorithms for Passing This Course
高分專業式演算法用功上課、認真預習與複習、用心準備考試。 You maximize your own profession ability.
低空飛過式演算法花足量時間寫作業準備考試 You minimize your efforts for passing this course.
明年再來式演算法不上課、不做作業,考試靠眼力。 This is not an algorithm that outputs a feasible solutio
n (you are not able to pass).
9
Dept. of Computer Science & Information Engineering
Why Do You Need to Learn Algorithms? The CPU is fast and the memory is cheap.
But CPU is not infinite fast and the size of memory is still bounded.
In reality, many important applications require efficient programs in terms of time and space. e.g., Search engine. e.g., The human genome is a long string of
3,253,037,807 length. Many hard problems lack of efficient algorithms.
10
Dept. of Computer Science & Information Engineering
Why Do You Need to Learn Algorithms? How do you fairly compare the performance
of two programs? It is not so fair to only compare the CPU time. We will teach you the conventional way (Big O) to
measure the performance of a program. Make you a better programmer.
Analyze your algorithm before writing your code.
11
Dept. of Computer Science & Information Engineering
Syllabus Growth of Functions Sorting Algorithms Graph Algorithms Dynamic Programming Greedy Algorithms Advanced Data Structures Selected Topics NP-completeness
12
Dept. of Computer Science & Information Engineering
First Try
Fibonacci numbers F(n), for n = 0, 1, 2, …, are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, … Sunflowers have spirals in pair of adjacent Fibonacci numbers
for two directions and (e.g., 34 and 55) [Wikipedia]
13
Dept. of Computer Science & Information Engineering
Algorithm 1: Recursion
1 if)2()1(
1 if1
0 if0
)(
nnFnF
n
n
nF
int fib (int n){if(n == 0 || n == 1)
return 1;else
return ( fib (n-1) + fib (n-2) );}
Formal definition: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
14
Dept. of Computer Science & Information Engineering
Algorithm 2: Loopint fib (int n){
if (n == 0 || n == 1){return 1;
}else{int tmp1 = 0, tmp2 = 1, result;for (int i = 2; i <= n; i++){
result = tmp1 + tmp2;tmp1 = tmp2;tmp2 = result;
}return result;
}}
0, 1, 1, 2, 3, 5, 8, 13, 21, …
tmp1 tmp2result
1 if)2()1(
1 if1
0 if0
)(
nnFnF
n
n
nF
15
Dept. of Computer Science & Information Engineering
Which One is Better?
int fib (int n){if (n == 0 || n == 1){
return 1;}else{
int tmp1 = 0, tmp2 = 1, result;for (int i=2; i<=n; i++){
result = tmp1 + tmp2;tmp1 = tmp2;tmp2 = result;
}return result;
}}
int fib (int n){if(n == 0 || n == 1)
return 1;else
return ( fib (n-1) + fib (n-2) );}
Algorithm 2
Algorithm 1
16
Dept. of Computer Science & Information Engineering
Analysis of Algorithm 1
int fib (int n){if(n == 0 || n == 1)
return 1;else
return ( fib (n-1) + fib (n-2) );}
Algorithm 1
fib(100)
fib(99)
fib(98)
fib(98)
fib(97)
fib(97)
fib(96)
fib(97)
fib(96)
fib(96)
fib(95)
… …
…
fib(1)
fib(0)
1 = 20
100
Exponential to n
2 = 21
4 = 22
8 = 23
17
Dept. of Computer Science & Information Engineering
Analysis of Algorithm 2
int fib (int n){if (n == 0 || n == 1){
return 1;}else{
int tmp1 = 0, tmp2 = 1, result;for (int i=2; i<=n; i++){
result = tmp1 + tmp2;tmp1 = tmp2;tmp2 = result;
}return result;
}}
3*(n-1)=3n-3
Linear to n
0, 1, 1, 2, 3, 5, 8, 13, 21, …
tmp1 tmp2result
18
Dept. of Computer Science & Information Engineering
Algorithm 2 runs faster in average and worst cases. If the Fibonacci number is quite small, Algorithm 1
can be taken (more intuitively). Algorithm 2 is more suitable for obtaining arbitrary
Fibonacci number (better time complexity).
Which One is Better?
Algorithm 1
Algorithm 2
n
Time
19
Dept. of Computer Science & Information Engineering
Which One is Better?
We are more interested in how an algorithm behaves as the problem size goes large. All algorithms behave similar under a small problem
size.
Algorithm 1
Algorithm 2
n
Time
20
Dept. of Computer Science & Information Engineering
Concluding Remarks
Ritchard Karp at NewYork Times (2006) “Algorithms are good at describing dynamic processe
s, while scientific formulas or equations are more suited to static phenomena.”
“Algorithms are small but beautiful.”