Types of Recursion

Types of Recursion

Types of Recursion

Types of Recursion

Types of Recursion

Types of Recursion

The mathematical combinations operation is a good example of a function
that can quickly be implemented as a binary recursive function. The number
of combinations, often represented as nCk where we are choosing n
elements out of a set of k elements, can be implemented as follows:

An exponential recursive function is one that, if you were to draw out a
representation of all the function calls, would have an exponential
number of calls in relation to the size of the data set (exponential
meaning if there were n elements, there would be O(an) function
calls where a is a positive number).

A good example an exponentially recursive function is a function to compute
all the permutations of a data set. Let's write a function to take an
array of n integers and print out every permutation of it.

To run this function on an array arr of length n, we'd do
print_permutations(arr, n, 0) where the 0 tells it to start
at the beginning of the array.

In nested recursion, one of the arguments to the recursive function is
the recursive function itself! These functions tend to grow extremely fast.
A good example is the classic mathematical function, "Ackerman's function.
It grows very quickly (even for small values of x and y, Ackermann(x,y) is
extremely large) and it cannot be computed with only definite iteration
(a completely defined for() loop for example); it requires indefinite
iteration (recursion, for example).