0% found this document useful (0 votes)
14 views

Recursion

notes of recursion
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Recursion

notes of recursion
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Recursion

Recursion
• Recursion is an implicit application of STACK ADT.
• A recursive function is a function that calls itself to solve a smaller
version of its task until a final call is made which does not require a
call to itself.
• Every recursive solution has two major cases: the base case in
which the problem is simple enough to be solved directly without
making any further calls to the same function.
• Recursive case, in which first the problem at hand is divided into
simpler subparts. Second the function calls itself but with subparts
of the problem obtained in the first step. Third, the result is
obtained by combining the solutions of simpler sub-parts.
Types of Recursion
• Any recursive function can be characterized based on:

▪ whether the function calls itself directly or indirectly (direct or


indirect recursion).

▪ whether any operation is pending at each recursive call


(tail-recursive or not).

▪ the structure of the calling pattern (linear or tree-recursive).

Recursion

Indirec Linea
Direct Tree Tail
t r
Direct Recursion
• A function is said to be directly recursive if it explicitly calls itself.
• For example, consider the function given below.

int Func( int n)


{
if(n==0)
retrun n;
return (Func(n-1));
}
Indirect Recursion
• A function is said to be indirectly recursive if it contains a call to
another function which ultimately calls it.
• Look at the functions given below. These two functions are
indirectly recursive as they both call each other.

int Func1(int n) int Func2(int x)


{ {
if(n==0) return Func1(x-1);
return n; }
return Func2(n);
}
Linear Recursion
• Recursive functions can also be characterized depending on the
way in which the recursion grows: in a linear fashion or forming a
tree structure.
• In simple words, a recursive function is said to be linearly recursive
when no pending operation involves another recursive call to the
function.
• For example, the factorial function is linearly recursive as the
pending operation involves only multiplication to be performed
and does not involve another call to fact() function.
Tree Recursion
• A recursive function is said to be tree recursive (or non-linearly
recursive) if the pending operation makes another recursive call to
the function.
• For example, the Fibonacci function Fib in which the pending
operations recursively calls the Fib function.

int Fibonacci(int num)


{
if(num <= 2)
return 1;
return ( Fibonacci (num - 1) + Fibonacci(num – 2));
}
Tail Recursion
• A recursive function is said to be tail recursive if no operations are
pending to be performed when the recursive function returns to
its caller.
• That is, when the called function returns, the returned value is
immediately returned from the calling function.
• Tail recursive functions are highly desirable because they are much
more efficient to use as in their case, the amount of information
that has to be stored on the system stack is independent of the
number of recursive calls.

int Fact(n) int Fact1(int n, int res)


{ {
return Fact1(n, 1); if (n==1)
} return res;
return Fact1(n-1, n*res);
}
Fibonacci Series
• The Fibonacci series can be given as:
0 1 1 2 3 5 8 13 21 34 55……
• That is, the third term of the series is the sum of the first and
second terms. Similarly, fourth term is the sum of second and third
terms, so on and so forth.
• A recursive solution to find the nth term of the Fibonacci series can
be given as:
FIB(n) = 1, if n<=2

FIB (n - 1) + FIB (n - 2), otherwise FIB(7)

FIB(6) FIB(5)

FIB(5) FIB(4) FIB(4) FIB(3)

FIB(4) FIB(3) FIB(3) FIB(2) FIB(3) FIB(2) FIB(2) FIB(1)

FIB(3) FIB(2) FIB(2) FIB(1) FIB(2) FIB(1) FIB(2) FIB(1)

FIB(2) FIB(1)
Pros and Cons of Recursion
Pros
• Recursive solutions often tend to be shorter and simpler than
non-recursive ones.
• Code is clearer and easier to use.
• Follows a divide and conquer technique to solve problems.
• In some (limited) instances, recursion may be more efficient.
Cons
• For some programmers and readers, recursion is a difficult
concept.
• Recursion is implemented using system stack. If the stack space on
the system is limited, recursion to a deeper level will be difficult to
implement.
• Aborting a recursive process in midstream is slow.
• Using a recursive function takes more memory and time to
execute as compared to its non-recursive counterpart.
• It is difficult to find bugs, particularly when using global variables.
Tower of Hanoi
Tower of Hanoi is one of the main applications of a recursion. It says, "if you can solve
n-1 cases, then you can easily solve the nth case"

A B C A B C

If there is only one ring, then simply move the ring from source to the destination

A B C
A B C A B C

If there are two rings, then first move ring 1 to the


spare pole and then move ring 2 from source to the
destination. Finally move ring 1 from the source to the
A B C
destination
Tower of Hanoi
Consider the working with three rings.

A B C
A B C
A B C

A B C A B C
A B C

A B C A B C
Algorithm to move n disks from A
to C using B as auxiliary
1. If n=1, move single disk from A to C & stop
2. Move top n-1 disks from A to B using C as
auxiliary
3. Move the remaining disk from A to C
4. Move the n-1 disks from B to C using A as
auxiliary
void towers(int n, char from, char to, char aux)
{
if (n==1)
{ printf(“move disk 1 from %c to %c”, from, to);
return;
}
towers(n-1, from, aux, to)
printf(“move disk %d from %c to %c”, n, from, to);
towers(n-1, aux, to, from);
}

You might also like