3 Recurrence
3 Recurrence
Recurrence Relations
Equation or an inequality that characterizes a
function by its values on smaller inputs.
Solution Methods
Substitution Method.
Recursion-tree Method.
Master Method.
Iteration Method
Recurrence relations arise when we analyze the
running time of iterative or recursive algorithms.
cn/2 cn/2
T(n/2) T(n/2)
T(n/4) T(n/4) T(n/4) T(n/4)
Cost of sorting
subproblems.
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn cn
cn/2 cn/2 cn
lg n
cn/4 cn/4 cn/4 cn/4 cn
c c c c c c cn
Total : cnlgn+cn
Recursion Tree for Merge Sort
Continue expanding until the problem size reduces to 1.
cn
•Each level has total cost cn.
•Each time we go down one level,
the number of subproblems doubles,
but the cost per subproblem halves
cn/2 cn/2
⇒ cost per level remains the same.
•There are lg n + 1 levels, height is
lg n. (Assuming n is a power of 2.)
cn/4 cn/4 cn/4 cn/4 •Can be proved by induction.
•Total cost = sum of costs at each
level = (lg n + 1)cn = cnlgn + cn =
Θ(n lgn).
c c c c c c
Practice Examples
Use the recursion-tree method to determine a
guess for the recurrences
T(n) = 2T(n/2) + 1
T(n) = 2T(n/2) + n2
T(n) = 3T(n/4) + Θ(n2).
T(n) = T(n/3) + T(2n/3) + O(n).
T(n) = T(n/10) + T(9n/10) + Θ(n).
T(n) = T(n – 1) + T(n – 2) + Θ(1).
The Master Method
Based on the Master theorem.
“Cookbook” approach for solving recurrences
of the form
T(n) = aT(n/b) + f(n)
• a ≥ 1, b > 1 are constants.
• f(n) is asymptotically positive.
• n/b may not be an integer, but we ignore floors and
ceilings.
Requires memorization of three cases.
The Master Theorem
Theorem:
Let a ≥ 1 and b > 1 be constants, let f(n) be a function, and
Let T(n) be defined on nonnegative integers by the recurrence
T(n) = aT(n/b) + f(n), where we can replace n/b by n/b or n/b.
T(n) can be bounded asymptotically in three cases:
1. If f(n) = O(nlogba–ε) for some constant ε > 0, then T(n) = Θ(nlogba).
2. If f(n) = Θ(nlogba), then T(n) = Θ(nlogbalg n).
3. If f(n) = Ω(nlogba+ε) for some constant ε > 0,
and if, for some constant c < 1 and all sufficiently large n,
we have a·f(n/b) ≤ c f(n), then T(n) = Θ(f(n)).
Recursion tree view
f(n) f(n)
a
… … …
f(n/b2) f(n/b2) f(n/b2) f(n/b2) f(n/b2) f(n/b2) f(n/b2) f(n/b2) f(n/b2) a2f(n/b2)
a a a a a a
… … … … … …
Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(nlogba)
log b n −1
Total: T (n) = Θ(n log b a
)+ ∑ a j f (n / b j )
j =0
The Master Theorem
Theorem:
Let a ≥ 1 and b > 1 be constants, let f(n) be a function, and
Let T(n) be defined on nonnegative integers by the recurrence
T(n) = aT(n/b) + f(n), where we can replace n/b by n/b or n/b.
T(n) can be bounded asymptotically in three cases:
1. If f(n) = O(nlogba–ε) for some constant ε > 0, then T(n) = Θ(nlogba).
2. If f(n) = Θ(nlogba), then T(n) = Θ(nlogbalg n).
3. If f(n) = Ω(nlogba+ε) for some constant ε > 0,
and if, for some constant c < 1 and all sufficiently large n,
we have a·f(n/b) ≤ c f(n), then T(n) = Θ(f(n)).
Master Method – Examples
T(n) = 16T(n/4)+n
a = 16, b = 4, nlogba = nlog416 = n2.
f(n) = n = O(nlogba-ε) = O(n2-ε ), where ε = 1 ⇒ Case 1.
Hence, T(n) = Θ(nlogba ) = Θ(n2).
T(n) = T(3n/7) + 1
a = 1, b=7/3, and nlogba = nlog 7/3 1 = n0 = 1
f(n) = 1 = Θ(nlogba) ⇒ Case 2.
Therefore, T(n) = Θ(nlogba lg n) = Θ(lg n)
Master Method – Examples
T(n) = 3T(n/4) + n lg n
a = 3, b=4, thus nlogba = nlog43 = O(n0.793)
f(n) = n lg n = Ω(nlog43 + ε ) where ε ≈ 0.2 ⇒ Case 3.
Therefore, T(n) = Θ(f(n)) = Θ(n lg n).
T(n) = 2T(n/2) + n lg n
a = 2, b=2, f(n) = n lg n, and nlogba = nlog22 = n
f(n) is asymptotically larger than nlogba, but not
polynomially larger. The ratio lg n is asymptotically less
than nε for any positive ε. Thus, the Master Theorem
doesn’t apply here.
Master Theorem – What it means?
Case 1: If f(n) = O(nlog a–ε) for some constant ε >
b
and if, for some constant c < 1 and all sufficiently large n,
we have a·f(n/b) ≤ c f(n), then T(n) = Θ(f(n)).