0% found this document useful (0 votes)
24 views22 pages

CH 4 - Recursion

Process by which a function calls itself repeatedly, until some specified condition has been satisfied.

Uploaded by

dital38028
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)
24 views22 pages

CH 4 - Recursion

Process by which a function calls itself repeatedly, until some specified condition has been satisfied.

Uploaded by

dital38028
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/ 22

CH 4

Recursion
Recursion - Introduction

- Process by which a function calls itself repeatedly, until some specified


condition has been satisfied.
- Mostly used for repetitive computations in which each action is stated in terms
of a previous result.
- 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.
- Recursion is based on Divide and conquer strategy.
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
Recursion vs Iteration
Tail Recursion
● A special form of recursion where the last operation of a function is a recursive
call. The recursion may be optimized away by executing the call in the current
stack frame and returning its result rather than creating a new stack frame.
● The tail recursive functions are considered better than non tail recursive functions
as tail-recursion can be optimized by compiler.
● Compilers usually execute recursive procedures by using a stack.
● This stack consists of all the pertinent information, including the parameter values,
for each recursive call.
● When a procedure is called, its information is pushed onto a stack, and when the
function terminates, the information is popped out of the stack. Thus for the non-
tail-recursive functions, the stack depth (maximum amount of stack space used at
any time during compilation) is more.
Tail Recursion[2]
● The idea used by compilers to optimize tail-recursive functions is simple,
since the recursive call is the last statement, there is nothing left to do in the
current function, so saving the current function's stack frame is of no use.
● We can use factorial using recursion, but the function is not tail recursive.
Long fact(int n){
if(n<=1)
Return 1;
Return n*fact(n-1);
}
Tail Recursion[3]
Long fact(int n){

if(n<=1)

Return 1;

Return n*fact(n-1);

● We can make it tail recursive by adding some additional parameters.

Long fact(long n,long a){

if(n==0)

Return a;

Return fact(n-1,a*n);
Recursively defined function

● A function is said to be recursively defined if the function refers to itself such


that:
○ There are certain arguments, called base values, for which the function does
not refer to itself.
○ Each time the function refers to itself, the argument of the function must be
closer to a base value.
Recursively defined function - example [1] - Factorial

● 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.
Recursively defined function - example [1] - Factorial

factorial(5)
Recursively defined function - ex[2] - Fibonacci Number
● Fibonacci numbers are the numbers appearing in the following integer
sequence: 0,1,1,2,3,5,8,13,21,34,55,89,144, ……………..
● This can be defined mathematically as:
Fn = Fn-1 + Fn-2 with F0=0 and F1=1
Fibonacci as recursive function:
def fibonacci(n):
If n==0:
Return 0
Else if n==1:
Return 1
Else:
Return fibonacci(n-1) + fibonacci(n-2)
Recursively defined function - ex[2] - Fibonacci Number

fibonacci(4):
Recursively defined function - ex[3] - GCD

● 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


Recursively defined function - ex[3] - GCD

● Recursive definition of GCD:


Int gcd(int a, int b){
if(a==0||b==0)
Return 0;
Else if(a == b)
Return a;
Else if(a > b)
Return gcd(a-b, b);
Else
Return gcd(a, b-a);
Recursively defined function - ex[3] - Tower of Hanoi

● 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.
Recursively defined function - ex[3] - Tower of Hanoi[2]

● 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.
Recursively defined function - ex[3] - Tower of Hanoi[3]
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
Recursively defined function - ex[3] - Tower of Hanoi[4]
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.
Recursively defined function - ex[3] - Tower of Hanoi[5]

Example:
Recursively defined function - ex[3] - Tower of Hanoi[6]

Example:
Applications of Recursion

● Sorting of data
● Solving problems based on tree structure
● Calculating mathematical series and sequences
● Drawing and detecting fractals and graphical objects
Finding efficiency of a recurrence relation

Steps to formulate the recurrence relation

1. Find the input size of the problem.


2. Find the number of subproblems and its input size
3. Write recurrence relation based on subproblems
4. Solve the recurrence relation.

You might also like