Professional Documents
Culture Documents
itself.
An algorithm that calls itself is direct recursive. Algorithm A is said to be indirect recursive if it calls another
same.
Iterative version to compute factorial of n: The factorial of a number n is the product of integer values from 1 to n. The iterative definition to compute n! is shown below,
if n = 0 if n>0.
fact = fact *n
Algorithm factorial (int n) { int fact, i; fact:= 1; for (i=1; i<=n; i++) { fact = fact * i; } return fact; }
properties,
1. The function must have stopping condition. i.e., it should not
Advantage of recursion :
1. Recursion reduces the complexity of the problem. 2. Recursion allows users to write much simpler programs. 3. Program implemented by recursion will be smaller in length. 4. Recursion programs can have any no of nesting levels. 5. Recursion is a top-down programming tool, where the given
problem is divided into smaller modules, each module are then individually attached.
How it works : The recursive function calls are initially pushed on to the stack until the condition is encountered. After the termination condition is encountered, the recursive functions that were pushed onto the stack are popped from the stack one-by-one. So, stack data structure plays the major role in executing recursive functions.
The recursive definition to compute n! is show below, fact (n) = 1 if n=0 n*fact(n-1) if n>0 5! = 5 * 4! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0! 0! = 1 1! = 1 * 0! = 1 2! = 2*1!=2 3! = 3 * 2!=6 4!= 4* 3!= 24 5!= 5* 4!= 120
Fibonacci series : The Fibonacci numbers are a series of numbers such that each number is the sum of the previous two numbers except the first and the second number. Ex : 0,1,1,2,3,5,8,13,21,34,..
To write a recursive definition we should know the base case and general case. Base case : Fib (0) = 0 and Fib (1) = 1 General case : Fib (n) = Fib (n-1) + Fib (n-2)
C program to display n Fibonacci numbers : # include < stdio.h > int fib( int n ) { if ( n==0 ) return 0; if ( n==1 ) return 1; return fib( n-1 ) + fib ( n-2 ); }
void main() { int i, n; printf(Enter the value of n); scanf(%d, &n); printf(Fibonacci numbers are); for ( i=0; i<n,i++ ) { printf(fib(%d) = %d\n,i,fib(i)); } }
Tower of Hanoi :
Here there are 3 needles A, B, & C & n discs, of different diameters in the needle A, & are placed one above the other such that always a smaller disc is placed above the larger disc. The two needles B & C are empty. All the discs from needle A are to be transferred to needle C using needle B as temporary storage. The following rules to be followed while transferring the discs,
only one disc is moved at a time from one needle to another needle. Smaller disc is on top of the larger disc at any time. Only one needle can be used to for storing intermediate disks.
tower(n-1, source, dest, temp); printf(Move the disc from %c to %c, source, dest); tower(n-1, temp, source, dest);
i.e., The base condition is, if n == 1 in source then directly transfer from source to destination
// C function for tower of Hanoi #include < stdio.h > int count = 0; void tower(int n, int source, int temp, int dest) { if( n == 1) { printf(\n move the disc from %c to %c , source, dest); count++; return; } tower (n-1, source, dest, temp); printf(\n Move the disk from %c to %c , source, dest); tower(n-1, temp, source, dest); }
void main() { int n; printf(Enter no. of discs); scanf(%d, &n); tower(n, A, B, C); printf(The total no. of moves = %d, count); }
Tracing:
If n = 3
T(1, A, B, C)
T(2, A, C, B)
A -> B
T(1, C, A, B)
A -> B
C -> B A -> C B -> A B -> C
Tower(3, A, B, C)
A -> C T(1, B, C, A)
T(2, B, A, C)
B -> C
T(1, A, B, C)
A -> C
What is the input size? Get that parameter. Find out the basic operation(s).
What is the number of times the basic operation is executed? Set up the recurrence relation with the escape hatch. Using the formulas or backward substitution, solve the recurrence relation to find its order of growth or the complexity.