Name: Adnan Ahmed
RegistrationNo.: 241220120420
University roll no.:12200224070
Class roll: 63
Subject Name: Design & Analysis of Algorithms (PCC-CS404)
Department: Information Technology
College Name: St. Thomasâ college of Engineering and Technology
Date of Creation:17/02/2025
2.
Resursion
The process inwhich a function calls itself directly or indirectly is called recursion
and the corresponding function is called a recursive function.
A recursive algorithm takes one step toward solution and then recursively call itself to further move. The
algorithm stops once we reach the solution.
Since called function may further call itself, this process might continue forever. So it is essential to provide a
base case to terminate this recursion process.
3.
How recursion work
Recursionis a programming technique where a function solves a problem by breaking
it down into smaller, similar subproblems, calling itself repeatedly until it reaches a
base caseâthe simplest instance that can be solved directly.. Each recursive call is
stored in the call stack, which keeps track of pending operations and their
execution contexts. When the base case is encountered, the recursion stops, and
the stack begins to "unwind," resolving each pending call by returning its result to
the previous caller. This step-by-step resolution combines intermediate solutions to
build the ďŹnal result.
4.
Need of Recursion
âRecursion helps in logic building. Recursive thinking helps in solving complex problems by breaking them
into smaller subproblems.
â Recursive solutions work as a basis for Dynamic Programming and Divide and Conquer algorithms.
Problems that can be solved using Recursion such as factorial, Fibonacci series, etc.
5.
Example 1 âFactorial using Recursion
Mathematical formula
n!=nĂ(nâ1)! ; Base Case: 0!=1
code
#include <stdio.h>
// Recursive function to calculate factorial
int factorial(int n) {
if (n == 0) // Base case
return 1;
return n * factorial(n - 1); // Recursive case
}
int main() {
int num = 4;
printf("Factorial of %d is %dn", num, factorial(num));
return 0;
}
Example 2 âFibonacci Series
Mathematical Formula:
F(n)=F(nâ1)+F(nâ2
Base Case: F(0)=0,F(1)=1
Code
#include <stdio.h>
// Recursive function to find Fibonacci number
int fibonacci(int n) {
if (n == 0) // Base case
return 0;
else if (n == 1) // Base case
return 1;
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case
}
int main() {
int n = 6; // Change this value for different results
printf("Fibonacci number at position %d is %dn", n,
fibonacci(n));
return 0;
}
Advantage & Disadvantage
Advantagesof Recursion Disadvantage of Recursion
SimpliďŹes code for self-referential problems Performance overhead due to repeated function calls and stack frame
creation.
Readability Stack overďŹow risk with deep recursion
Elegant divide-and-conquer solutions IneďŹciency for non-hierarchical problems (e.g., iterative loops often
outperform recursion).
11.
Conclusion
Recursion is apowerful problem-solving tool that elegantly simpliďŹes complex tasks
by breaking them into smaller, self-referential subproblems, making it ideal for
scenarios like tree traversals, nested data processing, or divide-and-conquer
algorithms. However, its use demands caution, as improper implementation can lead
to performance pitfalls such as stack overďŹow (from excessive depth) or redundant
calculations.