Algorithms: Complexity of Recursive Algorithms
Algorithms: Complexity of Recursive Algorithms
Algorithms: Complexity of Recursive Algorithms
Algorithms
2 / 14
Simplifying recurrences
We want to obtain a formula with no recurrence.
Example: T(n) = (log(n))
Methods:
Substitution method
First, guess the solution and then prove its correctness by induction.
Recursion-tree method
Evaluate characteristics of the recursion tree.
Use the „cookbook“ - Master theorem
Some common forms of recurrences are solved in general by the
Master theorem, we only have to evaluate the conditions of the
theorem.
Algorithms
3 / 14
Substitution method
Two-steps solution:
1. Guess the exact form of the solution.
Typically, one would precompute a set of numerical
results for different input n's and derive a formula.
2. Prove the correctness of the guess.
Mathematical induction is the tool of choice in most
cases.
Algorithms
4 / 14
Substitution method - Example
Example:
T (n) = 2T (n/2) + n
Suppose, we come (somehow) to a guess:
T (n) = O(n log(n))
Using the definition of upper bound "O" we want to prove:
T (n) ≤ cn log (n)
for some suitable c > 0.
State an induction hypothesis (i.e. let the guess be true
for n/2 ): T (n/2) ≤ c (n/2) log (n/2)
Algorithms
5 / 14
Substitution method - Example
Substitute the hypothesis into the original recurrence
T (n) = 2T (n/2) + n and complete the proof in standard manner as
you would do in any other induction proof.
T(n) ≤ 2(c (n/2) log(n/2)) + n
≤ cn log(n/2) + n // due to the hypothesis
= cn log (n) ‐ cn log (2) + n
= cn log (n) ‐ cn + n
≤ cn log (n)
Algorithms
6 / 14
Recursion-tree method - Example
Recurrence:
T (n) = 3T (n/4) + cn2
Iteratively build more and more complete recursion trees:
Sums of the labels in particular tree depths are listed to the right.
By adding sums in all levels we obtain the resulting complexity: O (n2)
Algorithms
8 / 14
Recursion-tree method - Example
Adding the sums in particular depths might be done as follows:
use formula
Algorithms
9 / 14
Using the "cookbook"
Master theorem is applicable to the recurrences of the form:
Algorithms
10 / 14
Using the "cookbook"
Master theorem
Let a ≥ 1, b > 1 be constants, let f (n) be a function and let T (n) be
defined for non-negative integers by the recurrence
T (n) = a T (n/b) + f (n)
where n/b means n/b or n/b . Then the following holds.
Algorithms
11 / 14
Using the "cookbook" Example 1
Example 1:
T (n) = 9T(n/3) + n
The parameters are: a = 9, b = 3, f (n) = n ∈ O(nlog3(9)‐1).
It is the case 1 of Master theorem.
The resulting complexity is thus:
T (n) ∈ Θ(nlog3(9)) = Θ(n2).
Algorithms
12 / 14
Using the "cookbook" Example 2
Example 2:
T (n) = T(2n/3) + 1
The parameters are: a = 1, b = 3/2,
f (n) = 1 = nlog3/2(1) ∈ Θ(nlog3/2(1)) .
It is the case 2 of Master theorem.
The resulting complexity is thus:
T (n) ∈ Θ(nlog3/2(1) log(n)) = Θ(log(n))
Algorithms
13 / 14
Using the "cookbook" Example 3
Example 3: T (n) = 3T(n/4) + n log(n)
The parameters are: a = 3, b = 4,
Algorithms
14 / 14