0% found this document useful (0 votes)
20 views17 pages

Unit Six

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

Unit Six

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

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.

Head Recursion: If a recursive function calling itself and


that recursive call is the first statement in the function
then it’s known as Head Recursion. There’s no statement,
no operation before the call. The function doesn’t have to
process or perform any operation at the time of calling
and all operations are done at returning time.
• If a recursive function calling itself for one time then it’s known as Linear Recursion.
Otherwise if a recursive function calling itself for more than one time then it’s known
as Tree Recursion.
Assignment
• Write down the differences between Recursion and Iteration.
Fibonacci Series using Recursion
A Fibonacci sequence is the sequence of integer in which each element in the sequence is the sum of the two
previous elements.
Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1
respectively.
Fn = Fn-1 + Fn-2
E.g. F8 = 0, 1, 1, 2, 3, 8, 13 or, F8 = 1, 1, 2, 3, 5, 8, 13, 21
Recursive algorithm to get Fibonacci sequence:
1. START
2. Input the non-negative integer ‘n’

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.

Recursive definition of factorial function f:


f(0) = 1, f(1) = 1 => Base case(s):- this doesn’t require recursion.
f(n) = n*f(n-1) => Recursive case:- the function calls itself, with simpler instance.
factorial(5)
GCD using Recursion
● GCD or HCF of two numbers is the largest number that divides both of
them.
● Ex: GCD of 63 & 42:
The factors of 63 are (7,3) ie: 63 = 7 * 3 * 3
The factors of 42 are (7,3,2) ie: 42 = 7 * 3 * 2
So the GCD of 63 and 42 is 21 [7*3=21]

Find the GCD of 192, 64


Tower of Hanoi using Recursion

● 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

Case 2: For an odd number of disks:


1. Make the legal move between pegs A & C (in either direction)
2. Make the legal move between pegs A & B (in either direction)
3. Make the legal move between pegs B & C (in either direction)
4. Repeat until complete
In either case, a total of 2n -1 moves are required.
Example
Application
● Sorting of data
● Solving problems based on tree structure
● Calculating mathematical series and sequences
● Drawing and detecting fractals and graphical objects

You might also like