Docsity

Recursion Algorithms Implementation, Lab Reports of Data Structures and Algorithms

Toggle sidebar
bg1
18-EE-21
Lab # 07
Title:
Implementation of different Recursion Algorithms
Objective:
To perform different Recursion Algorithm
Equipment Required:
PC/ Laptop
Dev C++ software
Recursive function:
Some computer programming languages allow a module or function to call itself. This
technique is known as recursion. In recursion, a functionαeither calls itself directly or
calls a functionβthat in turn calls the original functionα. The functionαis called
recursive function.
Properties:
A recursive function can go infinite like a loop. To avoid infinite running of recursive function, there are
two properties that a recursive function must have −
Base criteria − There must be at least one base criteria or condition, such that, when this
condition is met the function stops calling itself recursively.
Progressive approach − The recursive calls should progress in such a way that each time a
recursive call is made it comes closer to the base criteria.
Implementation:
Many programming languages implement recursion by means of stacks. Generally, whenever a function
(caller) calls another function (callee) or itself as callee, the caller function transfers execution control to
the callee. This transfer process may also involve some data to be passed from the caller to the callee.
This implies, the caller function has to suspend its execution temporarily and resume later when the
execution control returns from the callee function. Here, the caller function needs to start exactly from
the point of execution where it puts itself on hold. It also needs the exact same data values it was working
on. For this purpose, an activation record (or stack frame) is created for the caller function.
1 | P a g e
pf3
pf4
pf5

Partial preview of the text

Download Recursion Algorithms Implementation and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Lab # 07

 Title:

Implementation of different Recursion Algorithms

 Objective:

 To perform different Recursion Algorithm

 Equipment Required:

 PC/ Laptop  Dev C++ software

 Recursive function:

Some computer programming languages allow a module or function to call itself. This technique is known as recursion. In recursion, a function α either calls itself directly or calls a function β that in turn calls the original function α. The function α is called recursive function.

 Properties:

A recursive function can go infinite like a loop. To avoid infinite running of recursive function, there are two properties that a recursive function must have −  Base criteria − There must be at least one base criteria or condition, such that, when this condition is met the function stops calling itself recursively.  Progressive approach − The recursive calls should progress in such a way that each time a recursive call is made it comes closer to the base criteria.

 Implementation:

Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee. This implies, the caller function has to suspend its execution temporarily and resume later when the execution control returns from the callee function. Here, the caller function needs to start exactly from the point of execution where it puts itself on hold. It also needs the exact same data values it was working on. For this purpose, an activation record (or stack frame) is created for the caller function.

This activation record keeps the information about local variables, formal parameters, return address and all information passed to the caller function.

 Analysis of Recursion:

One may argue why to use recursion, as the same task can be done with iteration. The first reason is, recursion makes a program more readable and because of latest enhanced CPU systems, recursion is more efficient than iterations.

 Time Complexity:

In case of iterations, we take number of iterations to count the time complexity. Likewise, in case of recursion, assuming everything is constant, we try to figure out the number of times a recursive call is being made. A call made to a function is Ο(1), hence the (n) number of times a recursive call is made makes the recursive function Ο(n).

 Tower of Hanoi:

Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one rings is as depicted − These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the larger one. There are other variations of the puzzle where the number of disks increase, but the tower count remains the same.

 Rules:

The mission is to move all the disks to some another tower without violating the sequence of arrangement. A few rules to be followed for Tower of Hanoi are :

2. Write a Program to find the Fibonacci series using recursion? Program Code: #include using namespace std; int fib(int x) { if((x==1)||(x==0)) { return(x); }else { return(fib(x-1)+fib(x-2)); } } int main() { int x , i=0; cout << "Enter the number of terms of series : "; cin >> x; cout << "\nFibonnaci Series : "; while(i < x) { cout << " " << fib(i); i++; } return 0; } **Program Output:

  1. Write a Program to find the sum of n natural number using recursion? Program Code:** #include using namespace std; int add(int n); int main() { int n; cout << "Enter a positive integer: "; cin >> n; cout << "Sum = " << add(n); return 0; } int add(int n) { if(n != 0) return n + add(n - 1);

return 0; } Program Output:

4. Write a Program to find a maximum number using recursion? Program Code: #include using namespace std; int findMaxRec(int A[], int n) { if (n == 1) return A[0]; return max(A[n-1], findMaxRec(A, n-1)); } int main() { int size,A[50]; cout<<"How many Number you want to enter :"; cin>>size; for(int i = 0; i < size; ++i) { cout << "\tEnter Number " << i + 1 << " : "; cin>> A[i]; } cout<<"\n\n\tMaximum Number: "<<findMaxRec(A, size); return 0; } Program Output: