Professional Documents
Culture Documents
Algorithm
Algorithm
Introduction
• The methods of algorithm design form one of the core
practical technologies of computer science.
• Speed is fun!
Running Time
Worst-case: (usually)
• T(n) = maximum time of algorithm on any input of
size n.
Average-case: (sometimes)
• T(n) = expected time of algorithm over all inputs of
size n.
• Need assumption of statistical distribution of inputs.
Best-case:
• Cheat with a slow algorithm that works fast on some
input.
Machine-Independent time
Assumptions:
• Each simple operation such as +, -, if ...etc takes exactly
one time step.
• Loops and subroutines are not considered simple
operations.
• Each memory acces takes exactly one time step.
Machine-independent time
BIG IDEA:
• Ignore machine-dependent constants.
• Look at growth of T ( n) as n
“Asymptotic Analysis”
Machine-independent time: An example
n n
T ( n) c1 c2 (n 1) c4 ( n 1) c5 t j c6 (t j 1)
j 2 j 2
n
c7 (t j 1) c8 (n 1).
j 2
Analysis of INSERTION-SORT(contd.)
T (n) c1n c2 (n 1) c4 (n 1) c5 (n 1) c8 (n 1)
(c1 c2 c4 c5 c8 )n (c2 c4 c5 c8 ).
Analysis of INSERTION-SORT(contd.)
•The worst case: The array is reverse sorted
(tj =j for j=2,3, ...,n).
n n(n 1)
j
j 1 2
T (n) an 2 bn c
Growth of Functions
N 0, 1, 2, ...
O-notation
• For a given function g (n) , we denote by O( g (n)) the
set of functions
f (n) : there exist positive constants c and n0 s.t.
O( g (n))
0 f ( n ) cg ( n ) for all n n 0
if and only if
f (n) O( g (n)) and f (n) ( g (n)).
Example 2.
f (n) 3n 2 2n 5 (n 2 )
Because :
3n 2 2n 5 (n 2 )
3n 2 2n 5 O (n 2 )
Example 3.
x 1 x x x x 1
Standard notations and common functions
• Logarithms:
lg n log 2 n
ln n log e n
log k n (log n) k
lg lg n lg(lg n)
Standard notations and common functions
• Logarithms:
For all real a>0, b>0, c>0, and n
ab log b a
• Logarithms:
log b (1 / a ) log b a
a logb c c logb a
1
log b a
log a b
Standard notations and common functions
• Factorials
For n 0 the Stirling approximation:
n
n 1
n! 2n 1
e n
n! o(n n )
n! (2 n )
lg(n!) (n lg n)