






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Intuitively, a recursive function f is one whose output can be defined for a given input by equating its associated output to an expression that includes the output values of f for inputs of smaller size. For example, if f takes as input a nonnegative integer n ≥ 0, then recursive f (n) can be defined by equating it to an expression that includes values f (m 1 ),... , f (ma), where a ≥ 1 and m 1 ,... , ma < n. When this equation is required for computing f (n) for some n, then n is called a recursive case. However, what if n is already “small”? Say n = 0. Then since there are no smaller numbers than n, n is called a base case, and f (n) must be defined by equating it to a constant, such as f (0) = 5, or f (1) = 2, etc..
In what follows we assume that the domain of f is the set of natural numbers N = { 0 , 1 , 2 ,.. .}. Note that functions of this kind are often referred to as sequences, since they may also be expressed as f 0 , f 1 , f 2 ,.. ., where fi is shorthand for f (i), i = 0, 1 , 2 ,.. ..
To summarize, when defining a recursive function, there are two cases to consider:
Base Case when the input n has a size that is one of the smallest possible. In this case f (n) is equated to a constant.
Recursive Case f (n) can be defined by equating it to an expression that uses one or more f -values on inputs that are smaller than n.
Example 1. Consider the following recursive definition. Base Case: f (0) = 1. Recursive Case: for n ≥ 1, f (n) = 3f (n − 1) + 2. Use this definition to compute f (1), f (2), and f (3).
Example 3. Provide a recursive definition for f (n) = n!.
Example 4. Provide a recursive definition for the function f (n) = an, where a > 0 is a real constant.
One application of a recursion tree for a recursively defined function f for input n is that the number of tree nodes equals the number of calls that need to be made to function f in order to compute f (n), assuming that f (n) is recursively implemented. For example, below a recursive implementation of the Fibonacci sequence fn.
int fib(int n) { if(n <= 1) return n;
return fib(n-1) + fib(n-2); }
Example 7. Let Nn equal the number of nodes in the recursion tree for Fibonacci sequence fn. Show that Nn > fn for all n ≥ 0. Conclude that a recursive implementation of fn, such as fib, will require a number of function calls that exceeds fn.
a. f (n) = f (n − 1) + 3 b. f (n) = 2f (n − 1) c. f (n) = 2f^ (n−1) d. f (n) = f (n − 1)^2 + 2f (n − 1) + 1
a. f (1) = 4, f (2) = 7, f (3) = 10, and f (4) = 13. b. f (1) = 2, f (2) = 4, f (3) = 8, and f (4) = 16. c. f (1) = 2, f (2) = 4, f (3) = 16, and f (4) = 2^16 = 65536. d. f (1) = 4, f (2) = 25, f (3) = 676, and f (4) = 458329.
n^2 = ((n − 1) + 1)^2 = (n − 1)^2 + 2(n − 1) + 1 = (n − 1)^2 + 2n − 1 ,
we have f (n) = f (n − 1) + 2n − 1.