Asymptotics and Recurrence Equations: Analysis of Algorithms
Asymptotics and Recurrence Equations: Analysis of Algorithms
Asymptotics and Recurrence Equations: Analysis of Algorithms
Equations
Analysis of Algorithms
Prepared by
Readings
Main Reading Selection:
CLR, Chapter 3, 4 and Appendix A
Goal
To estimate and compare growth rates
of functions
Ignore constant factors of growth
f(n)
lim inf
c
g(n)
n
Use of O Notation
N is O(n2)
sometimes written
n = O(n2)
But n2 is not O(n) so can t use
identities!
The two sides of the equality do not
play a symmetric role
Example
n
1
1
= 1 + + O 2
n -1
n
n
1 1
= 1 + + o
n
n
= 1 + o(1) as n
1 x
i =0
( )
i
d
a
x
=
x
i
i =0
d
( )
i
d +1
a
x
=
o
x
i
i =0
d
i
d
a
x
~
a
x
i
d
i =0
Sums of Powers
for n
n
i =1
1
i ~
n d +1
d +1
d
Or equivalently
n
i =1
1
i =
n d +1 + o n d +1
d +1
d
( )
Examples
n
n2
i ~
2
i =1
n 2
3
n
i ~ 3
i =1
2nd order asymptotic expansion
n
i =1
1
1 d
d +1
i =
n + n + o n d 1
d +1
2
d
( )
f(n) ~ ci g i (n)
i =1
If
and
k
for all k 1
n +1
k =1
f(k) f(x) dx f (k + 1)
k =1
n +1
k =1
f(k) f(x) dx f (k + 1)
k =1
n +1
So
n +1
k =1
ln (x)dx = xln(x) x
ln k = (n + 1) ln (n + 1) - n + (ln(n))
k =1
ln n
Since log(n) =
ln 2
n
n
+ (log n)
So log k = (n + 1) log (n + 1) ln 2
k =1
2
4 ln 2
k =1
n
Harmonic Numbers
n
Hn =
k =1
1
k
1
H n = ln(n) + + O
n
n! = 1 2 3 (n-1) n
n! ~ 2 n n n e-n as n
So
1
log(n!) = n log n - n log e + log (2 n) + (1)
2
= n log n - (n)
Recurrence Equations
(over integers)
Homogenous of degree d
n>d
x n = a1 x n 1 + a 2 x n 2 + ... + a d x n d
Given
constant coefficients
initial values
a1, , ad
x1, x2 , , xd
F1 =1
F0 = c1 + c 2 = 0
F1 = c1 r1 + c 2 r2 = 1
1 1 + 5 1 5
Fn =
5 2 2
1
Fn ~
5
1 + 5
A Useful Theorem
c > 0, d > 0
n=1
c0
If T (n) =
n
d
aT
+
cn
n>1
b
( )
(
)
( )
n logb a
a > bd
a = bd
a < bd
Proof
d
T (n) = cn g(n) + a
log b n
Is solution
2
a
a
g(n) = 1 + d + d + ... +
b
b
a
d
b
log b n-1
Cases
a
(1) a > b g(n) ~ d
b
is last term so
log b n-1
) (
so T(n) = n log b n
d
( )
so T(n) = n d
Example: Mergesort
input list L of length N
if N=1 then return L
else do
let L1 be the first
elements of L
of L
elements
2
M1 Mergesort (L1)
M2 Mergesort (L2)
return
+ T
+ c2 N
2
2
for some constants c1, c2 > 1
N > 1 T(N) 2T
N
2 a
log + b + c 2 N
2
2
Holds if a = c1 + c2 ,
Solution
b = c1
T(N) ( c1 + c2 ) N log N + c1
n = log N, N = 2n
N
n 1 = log N log 2 = log
2
Recurrence equation:
( )
= T ( 2 ) = T(1) = c
X n = T 2n = 2 X n-1 + c 2 2n
X0
X n = O n 2n
so T ( N ) = O (N log N)
Advanced Material
Exact Solution of Recurrence
Relations
x n = a1 x n 1 + a 2 x n 2 + ... + a d x n d
xn = rn
Multiply by
r
n
r
r d a1 r d -1 a 2 r d -2 ... a d = 0
x n = ci rin
i =1
x n ~ ci rin
Where ri is dominant root
ri > rj j i
Other Case
Roots are not distinct
r1= r2 = r3
Then solutions not independent,
so additional terms:
n
1 1
n
1
n
1
n
i i
x n = c r + c 2 n r + c3 n r + c r
i=4
Inhomogenous Recurrence
Equations
x n = a1 x n 1 + a 2 x n 2 + ... + a d x n d + a 0
Nonzero constant term
a0 0
Solution Method
(1) Solve homogenous equation
Yn = a1 Yn 1 + a 2 Yn 2 + ... + a n Yn d
Solution Method
1) Solve homogenous equation
Yn = a1 Yn 1 + a 2 Yn 2 + ... + a n Yn d
2) Case
, add particular solution
a
1
i
a 0
x n = cn=
n
ia
i
Prepared by