Recurrences PDF
Recurrences PDF
Recurrences
Recurrence
A recurrence is an equality or inequality on a function that
uses values of the function on smaller inputs.
Examples:
(
1 if n = 1
T (n) =
T (n − 1) + 1 if n > 1
(
1 if n = 1
T (n) =
2T (bn/2c) + n if n > 1
(
0 if n ≤ 2
T (n) = √
T (b nc) + 1 if n > 2
(
1 if n = 1
T (n) =
T (b2n/3c) + T (bn/3c) + 1 if n > 1
Recurrences
Methods for solving recurrences
Recurrences
The substitution method
Recurrences
Example 1: Guessing the exact solution
(
1 if n = 1
Recurrence T (n) =
2T (n/2) + n if n > 1
Guess T (n) = n log n + n.
Base For n = 1, n log n + n = 1 = T (1).
Induction step Assume T (m) = m log m + m for all m < n.
T (n) = 2T (n/2) + n
n n n
=2 log + + n by the induction hypothesis
2 2 2
n
= n log + n + n
2
= n log n − n log 2 + n + n
= n log n − n + n + n
= n log n + n
Recurrences
Example 2: Guessing upper and lower bounds
(
1 if n = 1
Recurrence T (n) =
2T (n/2) + n if n > 1
Guess T (n) ≤ cn log n for n ≥ 2.
Base T (2) = 2T (1)+2 = 4 ≤ c ·2 log 2 = 2c ⇒ c ≥ 2.
Induction step Assume T (m) ≤ cm log m for all 2 ≤ m < n.
T (n) = 2T (n/2) + n
n n
≤ 2 c log +n
2 2
n
= cn log + n
2
= cn log n − cn + n
≤ cn log n if −cn + n ≤ 0 ⇒ c ≥ 1
Recurrences
Example 3: Revising the guess
Recurrences
Example 3: Revising the guess
Sometimes the guess needs to be revised by subtracting a
lower-order term from the previous guess.
Guess T (n) ≤ cn3 − dn2 .
Induction step
T (n) = 8T (n/2) + n2
n 2
n 3
≤8 c −d + n2
2 2
n3 n2
= 8c · − 8d · + n2
8 4
= cn3 − 2dn2 + n2
= cn3 − dn2 − dn2 + n2
≤ cn3 − dn2 if −dn2 + n2 ≤ 0 ⇒ d ≥ 1
Recurrences
The iteration method
Recurrences
Example 1
(
0 if n = 0
Recurrence T (n) =
T (n − 1) + 1 if n > 0
Iteration
T (n) = T (n − 1) + 1
= T (n − 2) + 1 + 1
= T (n − 3) + 3
..
.
= T (0) + n
=n
Recurrences
Example 2
(
1 if n = 1
Recurrence T (n) =
T (n − 1) + n if n > 1
Iteration
T (n) = T (n − 1) + n
= T (n − 2) + (n − 1) + n
= T (n − 3) + (n − 2) + (n − 1) + n
..
.
= 1 + 2 + ··· + n
n(n + 1)
=
2
Recurrences
Changing free terms
If we are only interested in the asymptotic behavior, the free
terms in the recurrence can be simplified.
(
1 if n = 1
T (n) =
T (n − 1) + n if n > 1
(
3 if n = 1
T2 (n) =
T2 (n − 1) + 3n if n > 1
(
1 if n = 1
T3 (n) = √
T3 (n − 1) + n + 2 n if n > 1
Recurrences
Recurrence tree
Recurrences
Recurrence tree
Recurrences
Recurrence tree
Recurrences
Recurrence tree
1 1
· n log n = n · log3 n ≤ T (n) ≤ n · log3/2 n =
log 3 log(3/2)
· n log n
⇒ T (n) ∈ Θ(n log n).
Recurrences
The master method
Recurrences
The master theorem
Let T (n) = aT (n/b) + f (n) where a ≥ 1, b > 1 and f (n) is
asymptotically positive.
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) ∈ Θ(f (n) · log n).
3 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)).
Recurrences
Example 1 (case 1)
T (n) = 9T (n/3) + n.
We have a = 9, b = 3, f (n) = n.
logb a = log3 9 = 2.
All the conditions of the first case are satisfied:
a = 9 ≥ 1, b = 3 > 1, f (n) = n ≥ 0.
For = 1/2, f (n) ∈ O(nlogb a− ) = O(n3/2 ).
Hence, T (n) ∈ Θ(nlogb a ) = Θ(n2 ).
Recurrences
Example 2 (case 1)
T (n) = 5T (n/2) + n2 .
We have a = 5, b = 2, f (n) = n2 .
logb a = log2 5 ≈ 2.322.
All the conditions of the first case are satisfied:
a = 5 ≥ 1, b = 2 > 1, f (n) = n2 ≥ 0.
For = 0.3, f (n) ∈ O(nlogb a− ) = O(n2.022 ).
Hence, T (n) ∈ Θ(nlogb a ) = Θ(nlog2 5 ).
Recurrences
Example 3 (case 2)
T (n) = T (2n/3) + 1.
We have a = 1, b = 3/2, f (n) = 1.
logb a = log3/2 1 = 0.
All the conditions of the second case are satisfied:
a = 1 ≥ 1, b = 3/2 > 1, f (n) = 1 ≥ 0.
f (n) ∈ Θ(nlogb a ) = Θ(1).
Hence, T (n) ∈ Θ(f (n) · log n) = Θ(log n).
Recurrences
Example 4 (case 3)
Recurrences
Example 5
Recurrences