Вы находитесь на странице: 1из 20

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. 1
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?


 Fastcoding
 No bugs

 Reusability

 Algorithm
2
Dept. of Computer Science
& Information Engineering

Text Book
 Textbook: Introduction to Algorithms
(Second Edition).
 Written by Cormen, Leiserson, Rivest, and Stein.
 Published by MIT Press and McGraw-Hill.

3
Dept. of Computer Science
& Information Engineering

Another Excellent Reference Book


 Introduction to Algorithms: A Creative
Approach.
 Written by Udi Manber.
 Published by Addison-Wesley.

4
Dept. of Computer Science
& Information Engineering

Grading Policy
 Midterm (Close Book) - 40%
 Final (Close Book) - 40%
 Homework - 20%
 Quiz - extra 10%

5
Dept. of Computer Science
& Information Engineering

Teaching Information
 Instructor
 Yao-Ting Huang: ythuang@cs.ccu.edu.tw
 Office: 511

 Office hours: 11AM~12AM, Thursday.

 Teaching Assistants
 吳顯如: zuzu7472@gmail.com
 廖基復: fjliao@gmail.com

 Lab: 405

 TA hours: 10AM~11AM, Friday.

6
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.

7
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


8
solution (you are not able to pass).
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.
9
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.

10
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
11
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]

12
Dept. of Computer Science
& Information Engineering

Algorithm 1: Recursion
 Formal definition: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
0 if n = 0

F (n) = 1 if n = 1
 F (n − 1) + F (n − 2) if n > 1

int fib (int n){


if(n == 0 || n == 1)
return 1;
else
return ( fib (n-1) + fib (n-2) );
}
13
Dept. of Computer Science
& Information Engineering 0 if n = 0

F (n) = 1 if n = 1
Algorithm 2: Loop  F (n − 1) + F (n − 2) if n > 1

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;
} result
tmp1 tmp2
}
0, 1, 1, 2, 3, 5, 8, 13, 21, …14
Dept. of Computer Science
& Information Engineering

Which One is Better?


int fib (int n){
if(n == 0 || n == 1)
return 1;
Algorithm 1 else
return ( fib (n-1) + fib (n-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++){
Algorithm 2 result = tmp1 + tmp2;
tmp1 = tmp2;
tmp2 = result;
}
return result;
}
} 15
Dept. of Computer Science
& Information Engineering

Analysis of Algorithm 1
int fib (int n){
if(n == 0 || n == 1)
return 1;
Algorithm 1 else
return ( fib (n-1) + fib (n-2) );
}

fib(100) 1 = 20
fib(99 fib(98) 2 = 21
)
fib(98 fib(97 fib(97 fib(96 4 = 22
) ) ) )
100 fib(97 fib(96 … fib(96 fib(95 … 8 = 23
) ) ) )

fib(1 fib(0
) ) Exponential to n 16
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;
3*(n-1)=3n-3
tmp2 = result;
}
return result; Linear to n
}
}

result
tmp1 tmp2

0, 1, 1, 2, 3, 5, 8, 13, 21, …17


Dept. of Computer Science
& Information Engineering

Which One is Better?


 Algorithm 2 runs faster in average and worst
cases.
 Ifthe 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).


Algorithm 1

Time Algorithm 2

18
n
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

Time Algorithm 2

n
19
Dept. of Computer Science
& Information Engineering

Concluding Remarks
 Ritchard Karp at NewYork Times (2006)
 “Algorithms are good at describing dynamic
processes, while scientific formulas or equations are
more suited to static phenomena.”
 “Algorithms are small but beautiful.”

20

Вам также может понравиться