Name: Adnan Ahmed
Registration No.: 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
Resursion
The process in which 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.
How recursion work
Recursion is 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.
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.
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;
}
Continue..
factorial(4) → 4 × factorial(3)
factorial(3) → 3 × factorial(2)
factorial(2) → 2 × factorial(1)
factorial(1) → 1 × factorial(0)
factorial(0) = 1 (Base Case Reached)
Illustrate code
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;
}
Application of Recursion
Tree and graph traversal
Sorting algorithms
Divide-and-conquer algorithms
Fractal generation
Backtracking algorithms
Advantage & Disadvantage
Advantages of Recursion Disadvantage of Recursion
Simplies code for self-referential problems Performance overhead due to repeated function calls and stack frame
creation.
Readability Stack overflow risk with deep recursion
Elegant divide-and-conquer solutions Ineciency for non-hierarchical problems (e.g., iterative loops often
outperform recursion).
Conclusion
Recursion is a powerful 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 overflow (from excessive depth) or redundant
calculations.
THANK YOU!!!

12200224070_Adnan_Ahmed_DAAbhbhbh_63.pdf

  • 1.
    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 final 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; }
  • 6.
    Continue.. factorial(4) → 4× factorial(3) factorial(3) → 3 × factorial(2) factorial(2) → 2 × factorial(1) factorial(1) → 1 × factorial(0) factorial(0) = 1 (Base Case Reached)
  • 7.
  • 8.
    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; }
  • 9.
    Application of Recursion Treeand graph traversal Sorting algorithms Divide-and-conquer algorithms Fractal generation Backtracking algorithms
  • 10.
    Advantage & Disadvantage Advantagesof Recursion Disadvantage of Recursion Simplifies code for self-referential problems Performance overhead due to repeated function calls and stack frame creation. Readability Stack overflow risk with deep recursion Elegant divide-and-conquer solutions Inefficiency for non-hierarchical problems (e.g., iterative loops often outperform recursion).
  • 11.
    Conclusion Recursion is apowerful problem-solving tool that elegantly simplifies 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 overflow (from excessive depth) or redundant calculations.
  • 12.