Analysis of Algorithms and Asymptotics
Analysis of Algorithms and Asymptotics
Analysis of Algorithms and Asymptotics
Asymptotics
Analysis of Algorithms
• Correctness:
The algorithm terminates with the correct answer
• Performance
– Mainly Running time (Time complexity)
– Use of other resources (space, …)
1
Time Complexity
Analysis
Cost of each operation depends on machine
Simplification 1: machine-independent analysis:
assume all operations unit cost →
can add the costs of the different steps
2
Asymptotic Analysis
Simplification 2: Look at growth of T(n) as
n goes to infinity; focus on dominant term
- Example: 3n² +7n +10
Dominant term: 3n²
3
But .. caution:
Asymptotic Notations:
Theta, Big-Oh, Omega
Theta: ( g ( n)) { f ( n) | constants c1 , c2 0 and n0 s.t. n n0 :
c1 g ( n) f ( n) c2 g ( n) }
c2 g ( n) f(n)
c1 g ( n)
n0 n
4
Asymptotic Notations:
Theta, Big-Oh, Omega
Big-Oh: O( g (n)) { f (n) | constant c 0 and n0 s.t. n n0 :
(Order) 0 f ( n ) c g ( n) }
Example:
f(n) 5n O(n 2 )
cg(n)
but not vice-versa
n0 n
Asymptotic Notations:
Theta, Big-Oh, Omega
Example:
f(n)
5n 2 ( n )
c g(n) but not vice-versa
n0 n
5
Asymptotic Notations:
little-oh, little-omega
little-oh: o ( g ( n )) { f ( n ) | constant c 0 n 0 s.t. n n 0 :
0 f (n) c g (n) }
6
Example: Polynomials
(0 )c d n c o( n d )
Ex : n 3.2 o( n 3.3 )
Proof: nc 1
d
d c 0
n n
Example: logarithms
• log10 n = log2 n )
• Proof: log10n = log2n / log210 = log2n / 3.32
7
Logs vs. powers/roots
• logn = o(nc) for all c>0
1
ln n 1
lim c lim cn1 lim c 0
n n n cn n cn
n n log n n 2 n 3 2 n 3 n n !
polynomial exponential
8
Properties
f (n) o( g (n)) f (n) O( g (n))
f (n) ( g (n)) f (n) ( g (n))
f (n) ( g (n)) f (n) O( g (n)), f (n) ( g (n))
f (n) ( g (n)) f (n) O( g (n)), f (n) ( g (n))
Transitivity:
f O( g ) and g O(h) f O(h)
same for o, , ,