Unit Six
Unit Six
Recursion
Introduction
• The process in which a function calls itself directly or indirectly is called recursion
and the corresponding function is called a recursive function.
• Using a recursive algorithm, certain problems can be solved quite easily.
Examples of such problems are Towers of Hanoi
(TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.
• A recursive function solves a particular problem by calling a copy of itself and
solving smaller subproblems of the original problems. Many more recursive calls
can be generated as and when required.
• For solving a problem using recursion, 2 conditions must be satisfied:
- First, the problem must be written in a recursive form. [based on previous
result]
- Second the problem statement must include a stopping condition.
Need of Recursion
• Recursion is an amazing technique with the help of which we can reduce the
length of our code and make it easier to read and write.
• A task that can be defined with its similar subtask, recursion is one of the best
solutions for it. For example; The Factorial of a number.
Properties
• Performing the same operations multiple times with different inputs.
• In every step, we try smaller inputs to make the problem smaller.
• Base condition is needed to stop the recursion otherwise infinite loop will occur.
Advantages
● The code may be much easier to write
● Some problems are inherently recursive in nature.
Disadvantages
● Recursive functions are normally slower than iterative functions.
● May require a lot of memory to hold intermediate result on the stack.
● Difficult to formulate solution
Types of Recursion
Tail Recursion: If a recursive function calling itself and
that recursive call is the last statement in the function then
it’s known as Tail Recursion. After that call the recursive
function performs nothing. The function has to process or
perform any operation at the time of calling and it does
nothing at returning time.
3. If (n==0 || n==1)
return n;
else
return fib(n-1)+fib(n-2);
4. Print, nth Fibonacci number
5. END
Fibonacci Recursion Tree
When N=4
Factorial using Recursion
● The factorial of a number is the product of all the integers from 1 to that number. [ex: 5! =
1*2*3*4*5 = 120 ]
● Factorial is not defined for negative integers and the factorial of zero is 1.
● Tower of Hanoi is a classical mathematical puzzle which consists of three tower pegs and
more than one rings.
● It consists of three rods and a number of disks of different sizes, which can slide onto any
rod. The puzzle starts with the disks in a stack in ascending order of size on one rod, the
smallest at the top, this making a conical shape.
● The objective of the puzzle is to move the entire stack to the last rod, obeying the
following rules:
○ Only one disk can be moved at a time.
○ Each move consists of taking the upper disk from one of the stacks and placing it on
top of another stack or on an empty rod.
○ No larger disk may be placed on top of a smaller disk.
Recursive Solution to TOH
Function recursiveTOH(n,s,a,d) // n=> number of disks, s=> starting pole,
//a=>intermediate pole, d=> destination
pole.
If n==1 then
Display “move s to d”
Return
recursiveTOH(n-1,s,d,a);
Display “move s to d”
recursiveTOH(n-1,a,s,d);
End function
Case 1: For even number of disks:
1. Make the legal move between pegs A & B (in either direction)
2. Make the legal move between pegs A & C (in either direction)
3. Make the legal move between pegs B & C (in either direction)
4. Repeat until complete