Recurrence Relation: Recursive Factorial Example video (13 minutes) (Spring 2021)
Recurrence relations can aid in determining Big-O complexity of recursive functions. A recurrence relation is a relationship in which a function T(...) occurs on both sides of the = sign. For example, T(n) = 1 + T(n / 2).
long factorial(long n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
This function recursively calculates the factorial of a positive number n, or n!. Eg, 3! = 3 * 2 * 1 = 6;
The base case of 0 simply returns 1. (Since we don't want to multiply by 0, and multiplying by 1 preserves the result.)
Returning 1 is a constant time operation.
So T(0) = 1.
The general recursive case multiplies n by the result of the function, called on 'n - 1'.
This is a constant time operation plus an operation that has a running time linked to 'n - 1'.
So, T(n) = 1 + T(n - 1).
We now have our base case and general recursive case:
Base: T(0) = 1
General: T(n) = 1 + T(n - 1)
T(n) = 1 + T(n - 1)
T(n) = 1 + (1 + T((n - 1) - 1))
T(n) = 2 + T(n - 2)
T(n) = 2 + (1 + T((n - 2) - 1))
T(n) = 3 + T(n - 3)
Each iteration, the leading term grows by 1, and the term subtracted from n grows by 1.
T(n) = k + T(n - k)
We know that T(0) = 1. So what value of k will let us substitute in T(0) to this relation?
With the term T(n - k) present, that question can be expressed as n - k = 0, or n = k.
Substituting in n for k ...
T(n) = n + T(n - n)
T(n) = n + T(0)
We know that the base case T(0) = 1. Substitute this in.
T(n) = n + 1.
O(T(n)) = O(n + 1)
O(T(n)) = O(n)
The recursive function is O(n). This is intuitive, since each term between n and 0 must be accessed to calculate the factorial.
Thanks to Brian Foster for writing up these notes.