Professional Documents
Culture Documents
Recursion
Recursion
Chapter Outline
What is recursion?
Examples of recursion
Advantages and Disadvantages
Recursive definitions
• Sometimes, the best way to solve a problem is by
solving a smaller version of the exact same
problem first
L16 4
Recursion
n! = 1 if n==0
n != n * (n -1) * n-2* ……1 if n>0
To compute the value of 5!
0! = 1
1! = 1
2! = 2*1
3! = 3*2*1
4! = 4*3*2*1
5! = 5*4*3*2*1
………….
L16 5
Recursion
We can define
0! = 1
1! = 1 * 0!
2! = 2 * 1!
3! = 3 * 2!
4! = 4 * 3!
4! = 5 * 4!
Mathematically
n! = 1 if n==0
n != n * (n -1) * n-2* ……1 if n>0
Recursive Definition of Factorial
Function
To compute the value of a recursive
function, e.g. 5!, value of 4! is required. For
4! Value of 3! Is required.
recursion
EG: 5! = 5 · 4! = 5 · 4 · 3!
Recursive Functions
Factorial
recursion
EG: 5! = 5 · 4! = 5 · 4 · 3! = 5 · 4 · 3 · 2!
recursion
EG: 5! = 5 · 4! = 5 · 4 · 3! = 5 · 4 · 3 · 2!
= 5 · 4 · 3 · 2 · 1!
Recursive Functions
Factorial
recursion
EG: 5! = 5 · 4! = 5 · 4 · 3! = 5 · 4 · 3 · 2!
= 5 · 4 · 3 · 2 · 1! = 5 · 4 · 3 · 2 · 1 ·
0!
0! = 1
1! = 1 * 0! = 1* 1 = 1
2! = 2 * 1! = 2* 1 = 2
3! = 3 * 2! = 3* 1 = 6
4! = 4 * 3! = 4* 6 = 24
5! = 5 * 4! = 5* 24 = 120
Algorithm for Factorial
if (n==0)
fact = 1;
else
{
x = n-1;
find the value of x!, call it y;
fact = n*y;
}
n! = 1 if n==0
n != n * (n -1) * n-2* ……1 if n>0
The Simple Recursion Program
Here’s the code. Now, that we understand stacks, we can visualize
the recursion.
work.
Compound Interest Rule: Never duplicate
count(2)
count(1) count(1)
void main()
{ int n;
cout<< "Factorial is " << factorial(4);
}
Recursive invocation
A new activation record is created for every
function invocation
◦ Including recursive invocations
main() int nfactorial = factorial(n);
Recursive invocation
A new activation record is created for every
function invocation
◦ Including recursive invocations
main() int nfactorial = factorial(n);
factorial() n = 0 return 1;
Recursive invocation
A new activation record is created for every
function invocation
◦ Including recursive invocations
main() int nfactorial = factorial(n);
factorial() n = 0 return 1;
Recursive invocation
A new activation record is created for every
function invocation
◦ Including recursive invocations
main() int nfactorial = factorial(n);
factorial() n = 1 return n * 1;
factorial() n = 0 return 1;
Recursive invocation
A new activation record is created for every
function invocation
◦ Including recursive invocations
main() int nfactorial = factorial(n);
factorial() n = 1 return 1 * 1;
Recursive invocation
A new activation record is created for every
function invocation
◦ Including recursive invocations
main() int nfactorial = factorial(n);
factorial() n = 2 return n * 1
factorial() n = 1 return 1 * 1;
Recursive invocation
A new activation record is created for every
function invocation
◦ Including recursive invocations
main() int nfactorial = factorial(n);
factorial() n = 2 return 2 * 1
Recursive invocation
A new activation record is created for every
function invocation
◦ Including recursive invocations
main() int nfactorial = factorial(n);
factorial() n = 3 return n * 2;
factorial() n = 2 return 2 * 1;
Recursive invocation
A new activation record is created for every
function invocation
◦ Including recursive invocations
main() int nfactorial = factorial(n);
factorial() n = 3 return 3 * 2;
Recursive invocation
A new activation record is created for every
method invocation
◦ Including recursive invocations
main() int nfactorial = 6;
factorial() n = 3 return 3 * 2;
Recursive invocation
A new activation record is created for every
method invocation
◦ Including recursive invocations
main() int nfactorial = 6;
Multiplication of Natural Numbers
Recursive definition of multiplication is
a * b = a if b==1 // stopping state
a * b = a * (b -1) + a if b>1
For Example
two numbers
fib(n) = n if n==0 or n==1
Fib(n) = fib(n-1) + fib(n - 2) if n>=2
Function for Fibonacci numbers
int fib(int n)
{
int x,y;
if (n<=1)
return n;
x = fib(n-1);
y = fib(n-2);
return x + y;
}
Fibonacci numbers
0
Infinite recursion
A common programming error when using
recursion is to not stop making recursive
calls.
◦ The program will continue to recurse until
it runs out of memory.
◦ Be sure that your recursive calls are made with
simpler or smaller subproblems, and that your
algorithm has a base case that terminates the
recursion.
Binary search
Compare the entry to the middle element of the list. If the entry matches
the middle element, the desired entry has been located and the search is
over.
If the entry doesn’t match, then if the entry is in the list it must be either
to the left or right of the middle element.
The correct sublist can be searched using the same strategy.
0 1 2 3 4 5 6
10 24 33 45 56 81 95