4 - Recurrences
4 - Recurrences
4 - Recurrences
Sergey Bereg
Recurrences
A recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs.
Example. M ERGE -S ORT, the worst case running time is
Θ(1) if n = 1
T (n) =
2T (n/2) + Θ(n) if n > 1
Solution: T (n) = Θ(n lg n).
M ERGE -S ORT(A, p, r)
// Input: A[p..r] is the array of n numbers.
// Output: A[p..r] with sorted numbers.
1 if p < r then
2 q = b(p + r)/2c
3 M ERGE -S ORT(A, p, q)
4 M ERGE -S ORT(A, q + 1, r)
5 M ERGE(A, p, q, r)
Technicalities
We neglect certain technical details. For example, the actual recurrence for M ERGE -S ORT should be
Θ(1) if n = 1
T (n) =
T (dn/2e) + T (bn/2c) + Θ(1) if n > 1
We usually drop ceiling and floor function.
Example: T (n) = aT (n/b) + f (n).
Boundary conditions. The running time on a constant-sized input is always a constant, so T (n) = Θ(1)
for sufficiently small n. It is assumed by default. So we write the recurrence for M ERGE -S ORT as
Substitution Method
1. Guess the form of the solution.
1
T (n) ≤ 3(cbn/3c lg(bn/3c)) + 5n
≤ cn lg(n/3) + 5n (since bxc ≤ x)
≤ cn lg n − cn lg 3 + 5n
Subtleties
T (n) = T (dn/2e) + T (bn/2c) + 1.
Right guess T (n) = O(n).
Try substitution method T (n) ≤ cn.
Changing variables
√ √
T (n) = 2T (b nc) + lg n. For simplicity T (n) = 2T ( n) + lg n.
Rename m = lg n. Then n = 2m .
T (2m ) = 2T (2m/2 ) + m
Rename S(m) = T (2m ). New recurrence S(m) = 2S(m/2) + m.
S(m) = O(m lg m) and T (n) = S(lg n) = O(lg n lg lg n).
2
Recursion-tree method
T (n) = 3T (bn/4c) + n2
n2
2
n
( n4 )2 ( n4 )2 ( n4 )2
T ( n4 ) T ( n4 ) T ( n4 )
n n n n n n n n n
T ( 16 ) T ( 16 ) T ( 16 ) T ( 16 ) T ( 16 ) T ( 16 ) T ( 16 ) T ( 16 ) T ( 16 )
n2 n2
3
( 16 )n2 ( n4 )2 ( n4 )2 ( n4 )2
3 2 2 n 2
( 16 )n ( 16 )
...
3 k 2
( 16 ) n T ( 4nk )
3k
Recursion-tree method
T (n) = 3T (bn/4c) + n2 .
Select k = log4 n. Then 3k = 3log4 n = nlog4 3 < n and T (n/4k ) = O(1).
2 k
3 2
2 3 2 3
T (n) = n + n + n + ··· + n2 + O(3k )
16 16 16
k
3 i
X
2
= n + O(n)
16
i=0
∞
3 i
X
2
< n + O(n)
16
i=0
1
= n2 + O(n)
1 − 3/16
= O(n2 ).
3
Master Method
Theorem. Let a ≥ 1 and b > 1 be constants, f be a non-negative function, and T (n) is defined by the
recurrence
T (n) = aT (n/b) + f (n).
1. If f (n) = O(nlogb a−ε ) for some constant ε > 0, then T (n) = Θ(nlogb a ).
2. If f (n) = Θ(nlogb a ), then T (n) = Θ(nlogb a lg n).
3. If f (n) = Ω(nlogb a+ε ) for some constant ε > 0, and if af (n/b) ≤ cf (n) for some constant c < 1
and all sufficiently large n, then T (n) = Θ(f (n)).