Professional Documents
Culture Documents
Recursion: Mirza Mohammad Lutfe Elahi
Recursion: Mirza Mohammad Lutfe Elahi
Outline
• Function Calls
• Nature of Recursion
int main(void)
{
double mileage = distance(0, 0, 3, 4);
return 0;
}
int main(void)
{
double mileage = distance(0, 0, 3, 4);
return 0;
}
main
int main(void)
{
double mileage = distance(0, 0, 3, 4);
return 0;
}
main distance
(0,0,3,4)
CSE 115 Programming Language I ECE@NSU
6
int main(void)
{
double mileage = distance(0, 0, 3, 4);
return 0;
}
int main(void)
{
double mileage = distance(0, 0, 3, 4);
return 0;
}
(5)
int main(void)
{
double mileage = distance(0, 0, 3, 4);
return 0;
}
(5)
main distance
(0,0,3,4)
int factorial(int n)
{
int fval, i;
fval = 1;
return fval;
}
In general:
if (base case)
solve it
else
reduce the problem using recursion // general case
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));
}
Tracing factorial(4)
Factorial(4)
Tracing factorial(4)
Factorial(4)
call
Factorial (4)
Tracing factorial(4)
Factorial(4) = 4 * Factorial(3)
call
Factorial (4)
call
Factorial (3)
Tracing factorial(4)
Factorial(4) = 4 * Factorial(3)
= 4 * (3 * Factorial(2))
call
Factorial (4)
call
Factorial (3)
call
Factorial (2)
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)
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.
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
Fibonacci’s Problem
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
Fibonacci’s Problem
How many pairs of rabbits will there be 4 months later?
Fibonacci’s Problem
Jan 1 0
1
Feb 1
2
Mar 1 0
Fibonacci’s Problem
Mar 1 2
0
3
Apr 1
1 0
4
May 1 2 1 0
0
In General
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);
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));
}
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));
}
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
}