BBT 1201: Data
Structures and
Algorithms
Lecture 5: Recursion
What is recursion
Recursion is a programming technique in
which procedures call themselves.
Recursive procedures generally solve a
problem by reducing the problem to an
instance of the same problem, but with a
smaller input
Formally a recursive procedure is one that
calls itself
Components of a Recursive
Procedure
A recursive procedure has the following
components
A base case
A recursive case
The general format of a recursive procedure
is
if (answer is known)
provide the answer (base value)
else
make a recursive call using a smaller
instance (recursive case)
Example: Factorial (2)
Definition:
Given n >= 0:
Example:
n! = n (n - 1) (n - 2) ... 2 1
0! = 1; 1! = 1
5! = 5 4 3 2 1 = 120
Formally, the function can be defined as:
1
if n<=1
n! =
n* (n -1)! if n>1
Example: Factorial
Write a recursive function Factorial(n) which
computes the value of n!
Analysis
Base Case:
If n = 0 or n=1: Factorial(n) = 1
Recursion:
n! = n (n - 1) (n - 2) ... 2 1
(n - 1)!
If n > 1: Factorial(n) = n Factorial(n - 1)
Solution
Computes the factorial
function Factorial ( n
if ( n is less than or
return 1
else
return n Factorial
of a number
)
equal to 1) then
( n 1)
/* Compute the factorial of n */
int factorial ( int n )
{
if ( n <= 1 )
return 1;
else
return n * factorial(n-1);
}
Example: Fibonacci
Fibonacci series
A series of numbers which
begins with 0 and 1
every subsequent number is the sum of the previous two
numbers
Fk = Fk-1 + Fk-2, F0 = 0, F1 = 1
example: 0, 1, 1, 2, 3, 5, 8, 13, 21,...
Formally
F(n) =
0,
if n=0
1,
if n=1
F(n-1) + F(n-2) if n>1
Example: Fibonacci (2)
The Fibonacci series can be defined
recursively as follows:
Base case
Fibonacci(0) = 0
Fibonacci(1) = 1
Recursive
Fibonacci(n) = Fibonacci(n - 2) + Fibonacci(n - 1)
Solution
function Fibonacci ( n )
if ( n is less than or equal to 1 ) then
return n
else
return Fibonacci ( n - 2 ) + Fibonacci ( n - 1 )
/* Compute the n-th Fibonacci number */
long fib ( int n )
{
if ( n <= 1 )
return n ;
else
return fib( n - 2 ) + fib( n - 1 );
}
Exercises
1.
2.
Write a recursive function to calculate the
sum of the first n natural numbers
Use recursion to implement the power
function that takes two integers
Hint: power (a, b) = a* power (a, b-1)
Stack and procedure calls
To understand how recursion works at run time, we
need to consider what happens when a procedure is
called
When a procedure is called, a block of memory is
allocated to it in the stack.
This block of memory (termed the stack frame or
activation record) will contain:
the procedures local variable
the procedures parameters
return address
Local Variables
Parameters
Return Address
Handling Recursive calls
Recursive procedure call is handled like any
other procedure call.
Each recursive call has an activation record
on the stack, which stores values of
parameters and local variables.
When the base case is reached return is
made to previous call
Recursion vs. Iteration
Both are based on the control structures
Repetition (Iteration): explicitly uses repetition
(loop).
Selection (Recursion): implicitly use repetition by
successive function calls
Both involve termination
Iteration: loop condition fails
Recursion: base case recognized
Recursion vs. Iteration (2)
Any problem that can be solved recursively
can also be solved iteratively with a stack.
Recursion has an overhead of repeated
function calls which is expensive in terms of
processor time and memory usage
If used properly a recursive approach can
lead to smaller code size and elegant
program (at the case of performance
penalty.)