algorithm
algorithm
G.silambarasan
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: An example
INSERTION-SORT(A)
1 for j 2 to length [A]
2 do key A[ j]
3 Insert A[j] into the sortted sequence A[1,..., j-1].
4 i j–1
5 while i > 0 and A[i] > key
6 do A[i+1] A[i]
7 ii–1
8 A[i +1] key
Analysis of INSERTION-SORT(contd.)
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.
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
a b 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)