0% found this document useful (0 votes)
15 views9 pages

DSA Assignment - 4

Uploaded by

secretscorpio99
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)
15 views9 pages

DSA Assignment - 4

Uploaded by

secretscorpio99
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/ 9

UNIK KHANAL

1. Explain the Tower of Hanoi Algorithm. / State TOH Problem.

The Tower of Hanoi is a classic problem in computer science and mathematics that
involves moving a stack of disks from one peg to another peg, using a third peg as an
intermediate, subject to the following rules:

1. Only one disk can be moved at a time.


2. A larger disk cannot be placed on top of a smaller disk.

The problem can be solved recursively. Here's an explanation of the recursive algorithm for
the Tower of Hanoi problem:

1. Base Case: If there is only one disk to move, simply move it from the source peg to the
destination peg.

2. Recursive Case: If there are more than one disk, follow these steps recursively:

● Move the top `n-1` disks from the source peg to the auxiliary peg, using the
destination peg as the intermediate.
● Move the largest disk (the `n`th disk) from the source peg to the destination peg.
● Move the `n-1` disks from the auxiliary peg to the destination peg, using the source
peg as the intermediate.

The above steps ensure that all the disks are moved from the source peg to the destination
peg, obeying the rules of the Tower of Hanoi puzzle.
UNIK KHANAL

2. Write a Recursive Program to find the nth Fibonacci Number.

#include<stdio.h>
int fib(int); // Function declaration
int main()
{
int n; // Declaration of variable
scanf("%d",&n);
printf("The %d term in the fibonacci series is: %d ",n,fib(n)); // Calling function
}
int fib(int n) // Function declaration
{
if(n==0)
{
return 0;
}
if(n==1||n==2)
{
return 1;
}
return fib(n-1)+fib(n-2); // Recursive function
}
UNIK KHANAL

3. Write a Recursive Program to Find GCD of Two Numbers.

#include <stdio.h>
int hcf(int n1, int n2);
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
int hcf(int n1, int n2)
{
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}

OUTPUT:

Enter two positive integers: 366


60

G.C.D of 366 and 60 is 6.


UNIK KHANAL

4. Define a Recursive Algorithm? How do you Implement Recursive Algorithms while


writing Computer Programs ? / Explain a recursive algorithm to solve the problem.

A recursive algorithm is an algorithm that solves a problem by breaking it down into


smaller instances of the same problem. In a recursive algorithm, the solution to the
problem depends on solutions to smaller instances of the same problem. Recursive
algorithms typically have two parts:

● Base case: This is the simplest form of the problem that can be solved directly
without recursion. It serves as the termination condition for the recursion.

● Recursive case: This is where the problem is divided into smaller subproblems, and
the algorithm calls itself recursively to solve each subproblem.

When implementing recursive algorithms in computer programs, you follow these steps:

● Identify the base case: Define the simplest possible input for which the solution is
known without further recursion. This is the base case that allows the recursion to
terminate.

● Divide the problem: Break down the main problem into smaller, similar subproblems
that can be solved recursively.

● Call the function recursively: Call the same function with the smaller subproblems
as input. Each recursive call should be closer to the base case, ensuring that the
recursion will eventually terminate.

● Combine solutions: Combine the solutions of the smaller subproblems to solve the
original problem.
UNIK KHANAL

5. Write a Recursive Program to find Factorial of a Number.

#include <stdio.h>

int factorial(int num)


{
if (num < 0)
return -1;
else if (num == 0)
return 1;
else
return num * factorial(num - 1);
}
int main()
{
int num;
printf("Enter a number to calculate factorial: ");
scanf("%d", &num);
int fact = factorial(num);
if (fact == -1)
{
printf("Invalid number input");
return 1;
}
printf("Factorial of %d = %d", num, fact);
return 0;
}
UNIK KHANAL

6. What is Recursion ?

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. It is essential to know that we should provide a certain case in order to
terminate this recursion process. So we can say that every time the function calls itself
with a simpler version of the original problem.

Properties of Recursion are:

● 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.
UNIK KHANAL

7. Write a Recursion Tree when no. of Disks are Four.

To visualize the recursion tree for the Tower of Hanoi problem with 4 disks, we can follow
the recursive algorithm and observe the sequence of moves:

Let's label the pegs as follows:


Source peg: A
Auxiliary peg: B
Destination peg: C

Here's the sequence of moves for the Tower of Hanoi problem with 4 disks:

• Move disk 1 from A to C.


• Move disk 2 from A to B.
• Move disk 1 from C to B.
• Move disk 3 from A to C.
• Move disk 1 from B to A.
• Move disk 2 from B to C.
• Move disk 1 from A to C.
• Move disk 4 from A to B.
• Move disk 1 from C to B.
• Move disk 2 from C to A.
• Move disk 1 from B to A.
• Move disk 3 from C to B.
• Move disk 1 from A to C.
• Move disk 2 from A to B.
• Move disk 1 from C to B.
UNIK KHANAL

8. Write Merits and Demerits of Recursive Function over Non-Recursive Function.

Here are some merits and demerits of using recursive functions over non-recursive
functions:

Merits of Recursive Functions:

1. Elegant and intuitive: Recursive solutions often closely mirror the problem statement,
making them easier to understand and implement, especially for problems that have a
recursive nature.

2. Simplifies complex problems: Recursive functions can simplify the solution for
problems with inherent self-similarity or that can be broken down into smaller, similar
subproblems.

3. Reduces code size: Recursive functions can often be more concise than their iterative
counterparts, reducing the number of lines of code and making the codebase more
maintainable.

4. Fosters code reuse: Recursive functions can be more modular, allowing parts of the
function to be reused in other contexts, potentially reducing duplication of code.

5. Tail recursion optimization: Some languages and compilers optimize tail-recursive


functions, reducing the overhead of recursion and making them as efficient as iterative
solutions.
UNIK KHANAL

Demerits of Recursive Functions:

1. Stack overflow: Recursive functions can lead to stack overflow errors if not
implemented carefully, especially for deep or infinite recursion, where the call stack grows
excessively large.

2. Performance overhead: Recursive function calls typically incur more overhead compared
to their iterative counterparts due to the additional function call overhead and stack
manipulation.

3. Difficult to debug: Recursive functions can be harder to debug than iterative solutions,
especially for complex recursion depths, as it can be challenging to track the flow of
execution.

4. Potential for inefficiency: In some cases, recursion may lead to inefficient solutions,
especially if the problem can be solved more efficiently using iteration or other techniques.

5. Limited support: Not all programming languages or environments handle recursion well,
and some may impose limitations on recursion depth or stack size, which can constrain the
use of recursive functions.

You might also like