0% found this document useful (0 votes)
105 views24 pages

Recursion

Recursion

Uploaded by

kedarprj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views24 pages

Recursion

Recursion

Uploaded by

kedarprj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Recursion

Definition: Process of
Recursion means solving a problem by
“Calling onto itself” reducing it to smaller
versions of itself.
Real life eg:

1. What is the meaning of meaning?


Answer : is meaning – This process Reoccurs

2. eg - Compound Interest / Recurring Deposit

If an amount of 1000 Rs is deposited, which is the principal amount with 10% interest.
Assume that the interest is calculated at the end of the year.

The interest is added with the principal amount and this becomes the new principal
for the next year and this process continues year after year.

This process is called as Recursion


Difference between Iteration and Recursion
(Reducing into Smaller parts)
Real life eg:
Real life eg:
•10 people passing the load from one to another
• climb step 1 pass the load to person 1
•Climbing 10 - 15 steps with a load
• climb step 1 • Person 1 climbs step 2 and pass the load to Person 2
• climb step 2
• Person 2 climbs step 3 and pass the load to
• climb step 3
• climb step 1 Person 3
• ……………… • ……………..
•This process is called Iteration
•This process is called Recursion
•Requires less memory •Requires large memory
•It is a fast process •It is a slow process
Memory Management

Iteration Recursion

Real life eg: Real life eg:


Use 1 glass to give water to 10 different friends. Use 10 glasses to give water to 10 different friends.
• Give 1 glass of water to friend 1 • Use glass 1 to give water to friend 1
• Use the same glass to give water to friend 2 • Use glass 2 to give water to friend 2
• Use the same glass to give water to friend 3 • Use glass 3 to give water to friend 3
• ……………… • ………………
• …………………………. Friend 10 • …………………………. Friend 3

• Same memory is used • Different memory is used every time


Process

Iteration Recursion

It is a fast process It is a slow process


Refer previous eg: Glass of water to 10 friends Refer previous eg: 10 Glasses of water to 10 friends
• Different memory must be used –
• Same memory can be used either with the same • To Close the task –
variable or different variable All the memory has to be closed one after the
other and then come back to the first memory
Advantages of Recursion

• For a recursive function, you only need to define the Eg :


base case and recursive case, so the code is simpler Sum of first 100 natural numbers 1- 100
and shorter than an iterative code.
• 100 + sum of the first 99 natural numbers
• Some problems are inherently recursive, such as
• 99 + sum of the first 98 natural numbers
Graph and Tree Traversal.
• 98 + sum of the first 97 natural numbers
• ……………….4+3
Eg : 10! is 10*9!
• …………..3+2
9! Is 9* 8!
• 2+ sum of the first natural number
8! Is 8*7!
• 2+1…. 1


2*1!
Disadvantages of Recursion

• A recursive program has greater space requirements than an iterative program as each
function call will remain in the stack until the base case is reached.
• It also has greater time requirements because each time the function is called, the stack
grows, and the final answer is returned when the stack is popped completely
• Time Consuming
• Memory Consuming
Types of Recursion
Indirect Recursion – (Output question)
Direct Recursion – (Expected Program question)
void A()
void A()
{
{
B();
………….
}
A();
void B()
}
{
A();
If a function is called in its own body (Encapsulation)
then it is called as the Direct Recursion }
If a function calls another function which calls the previous
function then it is called as the Indirect Recursion
Funny Eg:
Funny Eg:
Self Praising
Someone else is called to Praise
Recursive Methods
• A method in java that calls itself is called recursive method
• Recursion is a programming technique in which a call to a method appears in
that method’s body (i.e, a method calls itself: this is called direct recursion)
• It works on the principle of LIFO (Stack data structure)
[Last memory opened will be the First memory to be closed]

Real life eg:


Ask a person to collect a project work from point A then call him/her to collect
from point B and leaves it there… repeatedly move on till point D –(come back
and collects the project one after the other from D-C-B-A)
Last Memory Opened will be the first Memory to be closed.
Classification of Recursive Methods:
[Link] Recursion :A recursive methods having one or more base cases and is reachable or attainable.

void finite(int n) Output: When n=3,


{ 3
if(n>0) 2
{ 1
[Link](n); 0
finite(--n); 1
[Link](n); 2
}
}
Classification of Recursive Methods:
[Link] Recursion :A recursive methods which has no Base case OR the
base case is not reachable / attainable.
void infinite(int n)
{
if(n>0) Output: When n=3,
{ Infinite never ending - error
[Link](n);
infinite(n--);
[Link](n);
}
}
Classification of Recursive Methods:
3. Augmented Recursion : In augmented recursion, the recursive call, when it
happens, comes before other processing in the function(think of it happening
at the top, or head, of the function)
void augmented(int n) Output: When n=3,
{
0
if(n>0)
1
{
2
augmented(--n);
[Link](n);
// process done after recursive call
}
}
Classification of Recursive Methods:
4. Tail Recursion : A recursive method in which the last statement executed is
the recursive call. The processing occurs before the recursive call.
void tail(int n)
{ Output: When n=3,
if(n>0) 3
{
2
[Link](n); // // process done
before recursive call
1
tail(--n);
}
}
Sample program to illustrate a
recursive method:
Algorithm:
Factorial of a number: N! = 1 * 2 * 3 * ……………… *N

5! = 5 x 4!
4! = 4 x 3!
3! = 3x 2!
2! = 2 x 1!
1! = 1 (known) // base case
Program: Working:

public class factorial Fact(4)


{ 4*fact(3)
/*int fact(int n)
{ 3*fact(2)
if(n<2) // base case factorial of any number less than 2 is 1 2*fact(1)
1
return 1;
else
return n*fact(n-1); // Recursive case*/
As per the stack method, it will return
// or
int fact(int n) 1*2*3*4=24
{
return(n<2)? 1:n*fact(n-1);
}

}
To find the power of a number (ab)
int power(int a, int b) // a b is to be calculated
Algorithm: if 53 is to be calculated, then {
53=5x52 if(b==0)
52= 5x51 return 1; // a0=1
= 5x50 else
=1 return a*power(a,b-1);
}
Or (Using ternary operator)
int power(int a, int b)
{
return(b==0)?1:a*power(a,b-1);
}
To find the GCD / HCF of 2 numbers
int gcd(int p, int q)
{
if(p==0)
Working:
return q;
else
return gcd(q%p,p);
}
}

Output: 4
//Or (Using ternary operator)
/*int gcd(int p, int q)
{
return(p==0)?q:gcd(q%p,p);
}
}*/
To find the sum of the digits of a number
(492 = 4 + 9+ 2 = 15)
Working:
int sum(int n)
{
if(n==0)
return 0;
else
return n%10+sum(n/10);
}
}

//Or (Using ternary operator)


/*int sum(int n)
{
Output: 0+4+9+2 = 15
return(n==0)?0:n%10+sum(n/10);
}
}*/
To generate Fibonacci series : 2,3,5,8,13,21….. N Terms

public class fibo Working:


{
int fibo(int n) // n= nth term
{
if(n==1)
return 2; // as 2 is the first term in the series
else if(n==2)
return 3; // as 3 is the second term in the series
else
return fibo(n-2)+fibo(n-1); Output: So the 4th term will be 3+2+3=8
}
} Generate Series:
void generate(int N)
{
for(int i=1; i<=N; i++)
[Link](fibo(i) + " ");
}
}
To find the largest element in a given array
public class largest
{ Working:
// a= array, low = lower index, up = Upper Index
large(a,0,4) // a[]={2,34,9,7,20}, low=0, up=4 and max = 2
int large(int a[],int low, int up)
{ large(a,1,4) // a[]={2,34,9,7,20}, low=1, up=4 and max = 34
int max;
if(low==up) large(a,2,4) // a[]={2,34,9,7,20}, low=2, up=4 and max = 34
return a[low]; // value in the lower index will be returned
else large(a,3,4) // a[]={2,34,9,7,20}, low=3, up=4 and max = 34
// method call to check the next number ie. low+1
max =large(a,low+1,up); large(a,4,4) // a[]={2,34,9,7,20}, low=4, up=4 and max = 34
if(a[low]>=max)
return a[low];
else Output: 34
return max;
}
}
To reverse a word
Working:

S="INDIA", S1="";
public class Reverse
{
// S and S1 are instance variable
String S="INDIA", S1="";
// i is the index of the first character in the String S
void extract(int i)
{
if(i<[Link]())
{
extract(i+1);//augmented recursion

S1=S1+[Link](i);

}
[Link](S1); Output: AIDNI which is the reverse of INDIA
}
}

You might also like