Recursion
Mirza Mohammad Lutfe Elahi
CSE 115 Programming Language I ECE@NSU
2
Outline
• Function Calls
• Nature of Recursion
• Tracing Recursive Function Call
CSE 115 Programming Language I ECE@NSU
3
Look Back at Functions
double distance(double x1, double y1, double x2, double y2);
int main(void)
{
double mileage = distance(0, 0, 3, 4);
printf(“Mileage: %lf\n”, mileage);
return 0;
}
double distance(double x1, double y1, double x2, double y2)
{
double dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return dist;
}
CSE 115 Programming Language I ECE@NSU
4
Look Back at Functions
double distance(double x1, double y1, double x2, double y2);
int main(void)
{
double mileage = distance(0, 0, 3, 4);
printf(“Mileage: %lf\n”, mileage);
return 0;
}
double distance(double x1, double y1, double x2, double y2)
{
double dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return dist;
}
main
CSE 115 Programming Language I ECE@NSU
5
Look Back at Functions
double distance(double x1, double y1, double x2, double y2);
int main(void)
{
double mileage = distance(0, 0, 3, 4);
printf(“Mileage: %lf\n”, mileage);
return 0;
}
double distance(double x1, double y1, double x2, double y2)
{
double dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return dist;
}
main distance
(0,0,3,4)
CSE 115 Programming Language I ECE@NSU
6
Look Back at Functions
double distance(double x1, double y1, double x2, double y2);
int main(void)
{
double mileage = distance(0, 0, 3, 4);
printf(“Mileage: %lf\n”, mileage);
return 0;
}
double distance(double x1, double y1, double x2, double y2)
{
double dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return dist;
}
main distance sqrt
(0,0,3,4) (25)
CSE 115 Programming Language I ECE@NSU
7
Look Back at Functions
double distance(double x1, double y1, double x2, double y2);
int main(void)
{
double mileage = distance(0, 0, 3, 4);
printf(“Mileage: %lf\n”, mileage);
return 0;
}
double distance(double x1, double y1, double x2, double y2)
{
double dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return dist;
}
(5)
main distance sqrt
(0,0,3,4) (25)
CSE 115 Programming Language I ECE@NSU
8
Look Back at Functions
double distance(double x1, double y1, double x2, double y2);
int main(void)
{
double mileage = distance(0, 0, 3, 4);
printf(“Mileage: %lf\n”, mileage);
return 0;
}
double distance(double x1, double y1, double x2, double y2)
{
double dist = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return dist;
}
(5)
main distance
(0,0,3,4)
CSE 115 Programming Language I ECE@NSU
9
Factorial – Iterative Definition
n! = n * (n-1) * (n-2) * … * 2 * 1 for any integer n>0
0! = 1
int factorial(int n)
{
int fval, i;
fval = 1;
for(i = n; i >= 1; i--)
{
fval = fval * i;
}
return fval;
}
CSE 115 Programming Language I ECE@NSU
10
The Nature of Recursion
1. One or more simple cases of the problem (called the stopping
cases or base case) have a simple non-recursive solution.
2. The other cases (general cases) of the problem can be
reduced (using recursion) to problems that are closer to
stopping cases.
3. Eventually the problem can be reduced to base cases only,
which are relatively easy to solve.
In general:
if (base case)
solve it
else
reduce the problem using recursion // general case
CSE 115 Programming Language I ECE@NSU
11
Factorial – Recursive Definition
• To define n! recursively, n! must be defined in terms of the
factorial of a smaller number.
• Observation (problem size is reduced):
n! = n * (n-1)!
• Base case: 0! = 1
• We can reach the base case, by subtracting 1 from n if n is a
positive integer.
n! = 1 if n = 0
n! = n*(n-1)! if n > 0
CSE 115 Programming Language I ECE@NSU
12
Recursive Factorial Function
n! = 1 if n = 0
n! = n*(n-1)! if n > 0
int factorial(int n)
{
if(n == 0)
return 1;
else
return (n * factorial(n-1));
}
CSE 115 Programming Language I ECE@NSU
13
Tracing factorial(4)
Factorial(4)
CSE 115 Programming Language I ECE@NSU
14
Tracing factorial(4)
Factorial(4)
call
Factorial (4)
CSE 115 Programming Language I ECE@NSU
15
Tracing factorial(4)
Factorial(4) = 4 * Factorial(3)
call
Factorial (4)
call
Factorial (3)
CSE 115 Programming Language I ECE@NSU
16
Tracing factorial(4)
Factorial(4) = 4 * Factorial(3)
= 4 * (3 * Factorial(2))
call
Factorial (4)
call
Factorial (3)
call
Factorial (2)
CSE 115 Programming Language I ECE@NSU
17
Tracing factorial(4)
Factorial(4) = 4 * Factorial(3)
= 4 * (3 * Factorial(2))
= 4 * (3 * (2 * Factorial(1)))
call
Factorial (4)
call
Factorial (3)
call
Factorial (2)
call
Factorial (1)
CSE 115 Programming Language I ECE@NSU
18
Tracing factorial(4)
Factorial(4) = 4 * Factorial(3)
= 4 * (3 * Factorial(2))
= 4 * (3 * (2 * Factorial(1)))
= 4 * (3 * (2 * (1 * Factorial(0))))
call
Factorial (4)
call
Factorial (3)
call
Factorial (2)
call
Factorial (1)
call
Factorial (0)
CSE 115 Programming Language I ECE@NSU
19
Tracing factorial(4)
Factorial(4) = 4 * Factorial(3)
= 4 * (3 * Factorial(2))
= 4 * (3 * (2 * Factorial(1)))
= 4 * (3 * (2 * (1 * Factorial(0))))
= 4 * (3 * (2 * (1 * 1)))
call
Factorial (4)
call
Factorial (3)
call
Factorial (2)
call
Factorial (1)
call return 1
Factorial (0)
CSE 115 Programming Language I ECE@NSU
20
Tracing factorial(4)
Factorial(4) = 4 * Factorial(3)
= 4 * (3 * Factorial(2))
= 4 * (3 * (2 * Factorial(1)))
= 4 * (3 * (2 * (1 * Factorial(0))))
= 4 * (3 * (2 * (1 * 1)))
= 4 * (3 * (2 * 1))
call
Factorial (4)
call
Factorial (3)
call
Factorial (2)
call return 1*1 = 1
Factorial (1)
call return 1
Factorial (0)
CSE 115 Programming Language I ECE@NSU
21
Tracing factorial(4)
Factorial(4) = 4 * Factorial(3)
= 4 * (3 * Factorial(2))
= 4 * (3 * (2 * Factorial(1)))
= 4 * (3 * (2 * (1 * Factorial(0))))
= 4 * (3 * (2 * (1 * 1)))
= 4 * (3 * (2 * 1))
= 4 * (3 * 2)
call
Factorial (4)
call
Factorial (3)
call return 2*1 = 2
Factorial (2)
call return 1*1 = 1
Factorial (1)
call return 1
Factorial (0)
CSE 115 Programming Language I ECE@NSU
22
Tracing factorial(4)
Factorial(4) = 4 * Factorial(3)
= 4 * (3 * Factorial(2))
= 4 * (3 * (2 * Factorial(1)))
= 4 * (3 * (2 * (1 * Factorial(0))))
= 4 * (3 * (2 * (1 * 1)))
= 4 * (3 * (2 * 1))
= 4 * (3 * 2)
= 4 * 6
call
Factorial (4)
call return 3*2 = 6
Factorial (3)
call return 2*1 = 2
Factorial (2)
call return 1*1 = 1
Factorial (1)
call return 1
Factorial (0)
CSE 115 Programming Language I ECE@NSU
23
Tracing factorial(4)
Factorial(4) = 4 * Factorial(3)
= 4 * (3 * Factorial(2))
= 4 * (3 * (2 * Factorial(1)))
= 4 * (3 * (2 * (1 * Factorial(0))))
= 4 * (3 * (2 * (1 * 1)))
= 4 * (3 * (2 * 1))
= 4 * (3 * 2)
= 4 * 6
call
return 4*6 = 24 final answer
= 24
Factorial (4)
call return 3*2 = 6
Factorial (3)
call return 2*1 = 2
Factorial (2)
call return 1*1 = 1
Factorial (1)
call return 1
Factorial (0)
CSE 115 Programming Language I ECE@NSU
24
Important Notes
• A stack is used to keep track of function calls.
• Whenever a new function is called, all its parameters and local
variables are pushed onto the stack along with the memory
address of the calling statement (this gives the computer the
return point after execution of the function)
CSE 115 Programming Language I ECE@NSU
25
Pitfalls of Recursion
• If the recursion never reaches the base case, the recursive calls
will continue until the computer runs out of memory and the
program crashes. Experienced programmers try to examine the
remains of a crash. The message “stack overflow error” or
“heap storage exhaustion” indicates a possible runaway
recursion.
• When programming recursively, you need to make sure that the
algorithm is moving toward the base case. Each successive call
of the algorithm must be solving a simpler version of the
problem.
• Any recursive algorithm can be implemented iteratively, but
sometimes only with great difficulty. However, a recursive
solution will always run more slowly than an iterative one
because of the overhead of opening and closing the recursive
calls.
CSE 115 Programming Language I ECE@NSU
26
Fibonacci’s Problem
“Fibonacci”
(Leonardo de Pisa)
1170-1240
CSE 115 Programming Language I ECE@NSU
27
Fibonacci’s Problem
CSE 115 Programming Language I ECE@NSU
28
Rabbit Rules
1. All pairs of rabbits consist of a male and female
2. One pair of newborn rabbits is placed in hutch on January 1
3. When this pair is 2 months old they produce a pair of baby
rabbits
4. Every month afterwards they produce another pair
5. All rabbits produce pairs in the same manner
6. Rabbits don’t die
CSE 115 Programming Language I ECE@NSU
29
Fibonacci’s Problem
How many pairs of rabbits will there be 4 months later?
CSE 115 Programming Language I ECE@NSU
30
Fibonacci’s Problem
Jan 1 0
1
Feb 1
2
Mar 1 0
CSE 115 Programming Language I ECE@NSU
31
Fibonacci’s Problem
Mar 1 2
0
3
Apr 1
1 0
4
May 1 2 1 0
0
CSE 115 Programming Language I ECE@NSU
32
In General
Pairs this Pairs last Pairs of
month = month + newborns
Pairs this Pairs last Pairs 2
month = month + months ago
CSE 115 Programming Language I ECE@NSU
33
Fibonacci’s Problem
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
where each number is the sum of the preceding two.
• Recursive definition:
– F(0) = 0;
– F(1) = 1;
– F(number) = F(number-1)+ F(number-2);
CSE 115 Programming Language I ECE@NSU
34
Fibonacci’s Problem
F(0) = 0;
F(1) = 1;
F(number) = F(number-1)+ F(number-2);
int fibonacci(int n)
{
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return (fibonacci(n - 1) + fibonacci(n - 2));
}
CSE 115 Programming Language I ECE@NSU
35
Fibonacci’s Problem
F(0) = 0;
F(1) = 1;
F(number) = F(number-1)+ F(number-2);
int fibonacci(int n)
{
if(n == 0)
return 0;
else if(n == 1) Base case
return 1;
else
return (fibonacci(n - 1) + fibonacci(n - 2));
}
CSE 115 Programming Language I ECE@NSU
36
Fibonacci’s Problem
F(0) = 0;
F(1) = 1;
F(number) = F(number-1)+ F(number-2);
int fibonacci(int n)
{
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
return (fibonacci(n - 1) + fibonacci(n - 2)); General case
}
CSE 115 Programming Language I ECE@NSU