Recurrence Relations
A reccurance relation is a function that generates a sequence as a function of the index of the sequence and one or more previous terms of the sequence.
where is the index of the sequence and is the th term of the sequence. The elements are the previous terms of the sequence.
If the reccurance relation only depends on the previous term, then it is called a first order reccurance relation and can be written as:
where is the set of all possible values of the sequence. More generally, a reccurance relation of order can be written as:
When defining a reccurance relation, it is important to specify the initial conditions of the sequence. If the reccurance relation is of order , then the first terms of the sequence must be specified.
There are some very common and well known reccurance relations. For example, the Fibonacci sequence is defined by the second order reccurance relation:
where is the th term of the Fibonacci sequence.
A popular way to visualize reccurance relations is to draw a call or recursion tree, which shows the recursive calls made by the function. The call tree for the Fibonacci sequence is shown below:
If we wanted to code the Fibonacci sequence, we could use the following recursive function:
public int fib(int n) {
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}
However, this function can be shown to have a time complexity of , which is very slow. Instead we can use dynamic programming to improve the time complexity to as shown and discussed here.
Another example is the factorial function, which is defined by the first order reccurance relation:
where is the factorial of . Or as is more commonly written: