You are on page 1of 21

Recursion

DR. SHWETA SHARMA


Recursive Algorithms
 Recursion is a way of solving problems by having a function call itself

 A recursive algorithm calls itself which usually passes the return value as a
parameter to the algorithm again. This parameter is the input while the
return value is the output

 Recursive algorithm is a method of simplification that divides the problem


into sub-problems, again divides sub-problems into sub-sub problems and
then join by comparing

DR. SHWETA SHARMA, PEC CHANDIGARH 2


Recursive Algorithms
Recursion is a process in which the function calls itself indirectly or directly in order to
solve the problem. The function that performs the process of recursion is called a
recursive function.

Example: When you are calculating how much money you have in a box filled with Rs. 100
notes. If there are too many notes, then you might just ask your friend to do the same
work by dividing the entire stack into two. Once you both are done with counting, you will
just add up both the results in order to get the total amount.

DR. SHWETA SHARMA, PEC CHANDIGARH 3


Example: Factorial
n!= n(n-1)(n-2)…..4.3.2.1
n!= n(n-1)(n-2)…..4.3.2.1
5!= 5.4.3.2.1
4!=4.3.2.1
5!= 5.4!
3!=3.2.1
2!=2.1
n!= n(n-1)!
1!=1
0!=1
DR. SHWETA SHARMA, PEC CHANDIGARH 5
DR. SHWETA SHARMA, PEC CHANDIGARH 6
Factorial using Iteration
main()
{
int i,fact=1,number;
printf("Enter a number: ");
Time Complexity = O(n)
for(i=1; i<=number; i++){
fact=fact*i;
}
printf("Factorial of %d is: %d", number,
fact);
}
DR. SHWETA SHARMA, PEC CHANDIGARH 7
Factorial using recursion
factorial(int n)
{
if (n == 0)
return 1;
else
Time Complexity = O(n)
return(n * factorial(n-1));
}
main()
{
printf("Enter a number: ");
fact = factorial(number);
printf("Factorial of %d is %d", number, fact);
}
DR. SHWETA SHARMA, PEC CHANDIGARH 8
Difference
RECURSION ITERATION

Process of calling a function itself within its Loops are used to execute the set of instructions
own code repetitively until the condition is false

Termination condition is defined within the  Termination condition is defined in the definition
recursive function of the loop
It is slower than iteration due to the overhead It is faster than recursion.
of repeated function calls
It has to update and maintain the stack.
There is no utilization of stack.

DR. SHWETA SHARMA, PEC CHANDIGARH 9


Difference
RECURSION ITERATION

 Recursive code is generally smaller and Iterative code is generally bigger


simpler
Slower
Faster

Uses more memory


Uses less memory

DR. SHWETA SHARMA, PEC CHANDIGARH 10


Ques. 1 Find the output
int Rec(int a, int b)
{
if(a==0)
return b;
else
return Rec(a-1, a+b);
}
main()
{
Rec(5,2) b=17
}

DR. SHWETA SHARMA, PEC CHANDIGARH 11


Ques. 2 Find the output
int Rec(int n)
{
if(n==1)
return 0;
else
return 1+ Rec(n/2);
}
main()
{
Rec(5) 2
}

DR. SHWETA SHARMA, PEC CHANDIGARH 12


Ques. 3 Find the output
void Rec(int n)
{
if(n==0)
return;
n=20
else
Rec(n/2);
10100
Printf(“%d”, n%2)
}

DR. SHWETA SHARMA, PEC CHANDIGARH 13


Recurrence Relations

A recurrence relation is an equation which represents a sequence based on


some rule

It helps in finding the subsequent term (next term) dependent upon the
preceding term (previous term). If we know the previous term in a given series,
then we can easily determine the next term.

𝑛
T(n) = 2T ( ) +n
2

DR. SHWETA SHARMA, PEC CHANDIGARH 14


Recurrence Relations

A method to find the complexity of Recurrence Relations:

Recursion Tree
Master Theorem

DR. SHWETA SHARMA, PEC CHANDIGARH 15


Recursion Tree

Formulas:
𝑛
1) 𝑖=0 1 = (n+1)

𝑛 𝑛(𝑛+1)
2) 𝑖=0 𝑖 =
2

𝑛 2 𝑛(𝑛+1)(2𝑛+1)
3) 𝑖=0 𝑖 =
6

DR. SHWETA SHARMA, PEC CHANDIGARH 16


Recurrence Tree
𝑛 𝑖
4) 𝑖=0 𝑥 =

1
x<1 
1−𝑥

x=1  n+1

𝑥 𝑛+1 −1
x>1 
𝑥−1

DR. SHWETA SHARMA, PEC CHANDIGARH 17


Ques 1
𝑛
T(n) = 2T ( ) +n
2

Ans: nlog2n

DR. SHWETA SHARMA, PEC CHANDIGARH 18


Ques 2
𝑛
T(n) = T ( ) +1
2

Ans: log2n

DR. SHWETA SHARMA, PEC CHANDIGARH 19


Ques 3
𝑛
T(n) = 2T ( ) +1
2

Ans: n

DR. SHWETA SHARMA, PEC CHANDIGARH 20


Ques 4

T(n) = T (n-1) +n

Ans: n2

DR. SHWETA SHARMA, PEC CHANDIGARH 21


Thank you!

DR. SHWETA SHARMA, PEC CHANDIGARH 22

You might also like