Algorithm Design

Download Report

Transcript Algorithm Design

Nattee Niparnan
Dept. of Computer Engineering,
Chulalongkorn University
The Course
 Midterm 40%
 Final 40%
 Something else 20%
 Quiz 10%
 Lab 10%
Yes, we have labs
 This year, we introduce lab
 You will be required to participate in “online” activities
 Writing code (in C language)
 Teacher and TA will help you
Topics
 Analysis
 Asymptotic notation
 Big O analysis
 NP-Complete
 Design
 Divide and Conquer
 Dynamic Programming
 Graph Algorithm
 Greedy Algorithm
 Search
There will be labs
for each of these
topics
Books : Required
 Algorithms, S. Dasgupta, C. Papadimitriou and U.
Vazirani, McGraw-Hill, 2008
Books : Supplemental
 การวิเคราะห์และออกแบบอัลกอริทมึ , สมชาย ประสิทธิจู์ ตระกูล, NECTEC,
2544
 Data Structure & Algorithm Analysis, Mark Allen
Weiss.
 Introduction to Algorithms 2nd edition, T. Cormen, C.
Leiserson, R. Rivest, C. Stein, MIT Press & McGrawHill, 2001.
 Introduction to Algorithms: A Creative Approach, Udi
Manber.
Web
 Webboard
 http://www.cp.eng.chula.ac.th/webboard/viewforum.ph
p?f=18
 Lab
 http://www.nattee.net/teaching/
 You can also find my slides there
Chapter 0
What is an algorithm?
 A precise instruction based on elementary operation
 Which takes something as an input and process it to
some output
 Usually to solve some
What is Problem?
 A task with precise description of admissible input
 and the “property” of the desired output
 E.g., GCD
 Given two positive integers (input)
 Determine GCD of the given integers


(express the property of the desired output)
GCD is well defined
Problem Instance
 Determining GCD is a problem
 How many actual problems?



GCD of 1,2 ?
GCD of 234,42?
???
 Problem instance
 A problem with a specific input
 E.g., find a GCD of 42 and 14
Purpose of this class
 “how to design an algorithm for given problems”
 Analysis
 Synthesis  emphasized….
 Correctness
 For any instances, it must produce appropriate output
 Efficient!!
Calculating Fibonacci Sequence
 Fibonacci sequence
 1,1,2,3,5,7,8,13,21,34,55,89,144,233,377,610,987,1597,2584
0
: n  0;


Fn  
1
: n  1;
 F  F : n  1;
n2
 n 1
The Problem
 Input:
 a positive number N
 Output:
 Fn (the nth Fibonacci Number)
 Example instances
 Ex. 1:
N = 10
 Ex. 2:
N = 15
 Ex. 3:
N=0
N = -4 is not an
instances of
this problem!!!
Approach 1
 Array based
1
 DP
1
2
(linear)
3
5
8
13 21 34 55
Approach 2
F(9)
F(8)
 Recursive
F(7)
F(7)
F(6)
F(6)
F(5)
…
…
…
…
(exponential)
Approach 3
 F1   0 1 F0 
   
 
 F2   1 1 F1 
n 1
 Fn 1   0 1  Fn  2 

  

 
 Fn   1 1  Fn 1 
 Divide and Conquer (logarithmic)
Approach 3
 Find exponential

n / 2  n / 2  ; n is even

x
x
n
x    n / 2  n / 2 

x
x ; n is odd 
x
 Divide and Conquer (logarithmic)
Approach 4
Golden
Ratio
n
1 5    2 

 
 2   1  5 

Fn  
5
 Closed form solution (constant)
n
Conclusion
 Difference Design  Difference Performance
 This class emphasizes on designing “efficient
algorithm”
Algorithm Again
 It is the essence of the computation
Side Note on Algorithm
 Named after a Persian
mathematician
“Muhammad ibn Musa
Khwarizmi”
 Wrote book on linear
equation
 Introduce number 0
Analysis part
Asymptotic Notation
 Measurement of “efficiency” of algorithms
 How to compare two algorithms
 How to predict behavior of the algorithm
Big O analysis
 How to determine Big O of some code
 Recurrent Relation
NP-Complete
 What computer could solve
 Efficiently
 Inefficiently
 The difference between “Efficiency” and “Inefficiency”
Synthesis part
Divide and Conquer
 Solve a problem instance by dividing into smaller
instance
 Based on induction
Dynamic Programming
 Reduce redundancy computation
 For the case when there are several overlapping
subproblem
Greedy Algorithm
 Solve the problem by doing the best for the current
step
 Proof of correctness
Graph Algorithm
 Algorithm related to graph structure
 Shortest Path
 Minimal Spanning Tree
Search
 Solve the problem by enumeration
 Systematical enumeration
 Performance improvement