CH 2algo Analysis - Part2
CH 2algo Analysis - Part2
To compare two algorithms with running times f(n) and g(n),we need a rough measure that
characterizes how fast each function grows.
Compare functions in the limit, asymptotically(for large values of n)
Use Rate of Growth
• The low order terms in a function are relatively insignificant for large n
n4 + 100n2 + 10n + 50 n4
i.e., we say that n4 + 100n2 + 10n + 50 and n4 have the same rate of growth.
At CPU speed 10 9
instructions/second
Hierarchy of Functions
O-notation
10
Examples
2 n2 = O(n3) because
2 n2 ≤ cn3 2 ≤ cn which holds for c=1 and n0 = 1
n2 = O(n2) because
n2 ≤ cn2 which holds for c ≥ 1 which holds for c=1 and n0 = 1
- notation
• Note:
- when we say “the running time is Ω(n2) we mean that the best case running
time is Ω(n2).
Asymptotic notations (cont.)
-notation
14
Example
• 6n log n +n1/2 log n=Θ(n log n )
• 10n2 + 4n + 2 is Θ(n2)
• 6*2n + n2 is Θ(2n)
Logarithms
Binary logarithm
lg n log2 n log k n (log n ) k
Natural logarithm ln n loge n log log n log(log n )
log x y y log x
log xy log x log y
x
log log x log y
y
log b x (log a x) /(log a b)
a logb x x logb a 17
Recurrence Relation
Forming Recurrence Relation
Let the cost of each stage (i.e., the work to divide the problem +
combine solved sub problems) be described by the function f(n)
Then, the Master Theorem gives us a cookbook for the algorithm’s running
time:
The Master Theorem
if where a ≥ 1 &
b>1
Then
Example 1:
so case 1 applies
ε in case 1 is simply the power that can be subtracted from RHS power (n2)
and f(n) remains O(n2)
Example 2:
(binary
search)
a f(n/b)=f(n/3)
= (n/3)2 log(n/3)
=(1/9) n2 (log(n) – log(3))
<(1/9) n2 log(n)
then c=1/9
so case 3 applies
and T(n) is θ(n2 log n).
ε in case 3 is simply the power that can be added from RHS power and f(n) remains Ω(RHS)
Example 4:
so case 1 applies
so case 3 applies
Solution: a = 4 , b = 2
f(n)=n3 , logba=2, thus RHS=n2
Home Work