Recurrence Equations
We'll analyzer the computational cost of the following recursive search algorithm.
def search(list, value, index=0):
if list[index] == value:
return index;
if index == len(list) - 1:
return None
return search(list, value, index + 1)
The first step, requires writing out a system of the equation and the base case of the algorithm.
Now let's solve the equation, in four different methods.
Iterative
Idea: - develop the equation and express it as sum of terms depending on and the base case.
Difficulty: - many algebric calculations to do.
Then we calculate the equation when , the base case.
Tree
TODO: draw trees in markdown!
Substitution
Idea: - ipothize a solution for the given recurrence equation - verify (by induction) wether it works
Difficulty: - it's hard to find a solution as close as possible to the real solution - it's used mainly in demonstrations
Let's suppose , and , where and are fixed constants.
This doesn't mean that T(1), which is a is the same as , so we need two constants.
Now we have to prove that is a and a using induction.
where k is to be determined.
Base Case
First, check for which values the base case is verified.
Induction
Then check if the general case is covered by the base case.
We get that , we can always find constants and so that greater than both, so the induction is verified.
where k is to be determined.
Base Case
First, check for which values the base case is verified.
Induction
Then check if the general case is covered by the base case.
We get that , we can always find constants and so that smaller than both, so the induction is verified.
Main
Idea: - It's a set of formulas to solve a recurrence equation
Difficulty: - works only when the equation is in the form with
Theorem
Given
The equation:
There are three cases that can generate by comparing with :
The comparison must be polynomial, by an order of .
Where not to apply it?
In the following examples, the main method cannot be applied.
Ex 1
In this case, is asintotically bigger than n, but not plynomially bigger. In fact
Ex 2
In this case, is asintotically smaller than n, but not plynomially smaller. In fact