[go: up one dir, main page]

0% found this document useful (0 votes)
7 views4 pages

Recursion Guide

The document provides a comprehensive guide to recursive algorithms, explaining their definition, components, types, advantages, and disadvantages. It includes examples such as factorial calculation, Fibonacci sequence, and binary search, along with comparisons to iteration. Additionally, it outlines the time and space complexities of various recursive algorithms.

Uploaded by

amrendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Recursion Guide

The document provides a comprehensive guide to recursive algorithms, explaining their definition, components, types, advantages, and disadvantages. It includes examples such as factorial calculation, Fibonacci sequence, and binary search, along with comparisons to iteration. Additionally, it outlines the time and space complexities of various recursive algorithms.

Uploaded by

amrendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Recursion in Algorithms –

Comprehensive Guide
Introduction
A recursive algorithm is a powerful technique in data structures and algorithms where a
function calls itself to solve a smaller instance of the same problem. This approach is
particularly useful for tasks that can be broken down into simpler, repetitive subproblems,
like tree traversals, factorial calculation, and the Fibonacci sequence.

What is Recursive Algorithm?


A recursive algorithm is a method in programming where a function calls itself to solve a
smaller version of the same problem. This process continues until the problem becomes
simple enough to be solved directly.

Components of Recursive Algorithm


A recursive algorithm has three key components:

1. Base Case: The condition to stop recursion.


2. Recursive Case: Function calls itself with a simpler problem.
3. Recurrence Relation: Defines how problems are reduced.

Examples of Recursive Algorithm

Factorial Calculation
Base Case: factorial(1) = 1
Recursive Case: factorial(n) = n * factorial(n-1)
Example: factorial(5) = 120

Fibonacci Sequence
Base Case: fibonacci(0) = 0, fibonacci(1) = 1
Recursive Case: fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)

Tower of Hanoi
Base Case: Move the smallest disk directly.
Recursive Case: Move n-1 disks, move nth, move n-1 disks again.
Binary Search
Base Case: If target is found, return index. If array is empty, return -1.
Recursive Case: Search left or right half.

Types of Recursion in Algorithm


 1. Direct Recursion: A function calls itself directly.
 2. Indirect Recursion: A function calls another that calls the original.
 3. Tail Recursion: Recursive call is the last operation.
 4. Non-Tail Recursion: Further operations follow the recursive call.

Advantages of Recursive Algorithms


 Simplifies complex problems
 Reduces code size
 Natural fit for certain problems
 Cleaner, more elegant code

Disadvantages of Recursive Algorithms


 Higher memory usage
 Slower performance
 Risk of infinite recursion
 Harder to debug

Applications of Recursive Algorithms


 1. Sorting Algorithms: Merge Sort, Quick Sort
 2. Search Algorithms: Binary Search
 3. Mathematical Computations: Factorial, Fibonacci
 4. Tree and Graph Traversals: Preorder, DFS
 5. Dynamic Programming: Memoization
 6. Divide and Conquer: Strassen’s, Karatsuba Algorithm

Recursion vs Iteration
Aspect Recursion Iteration

Definition Function calls itself Loop executes repeatedly

Use Cases Tree traversal, factorial Counting, summing

Code Structure Shorter, more readable Longer with loops

Memory Usage Higher (call stack) Lower


Time Complexity Slower (function call Faster
overhead)

Termination Base case Loop condition

Debugging Harder Easier

Optimization Tail recursion Inherent

Ease of Understanding Harder Easier

Examples Factorial, Fibonacci Summing array

Time Complexity of Recursive Algorithms


Algorithm Time Complexity

Factorial O(n)

Fibonacci O(2^n)

Binary Search O(log n)

Merge Sort O(n log n)

Quick Sort O(n log n) avg, O(n²) worst

Tower of Hanoi O(2^n)

Tree Traversals O(n)

Strassen’s Matrix Multiplication O(n^2.81)

Karatsuba Algorithm O(n^1.585)

DFS O(V + E)

N-Queens O(N!)

Sudoku Solver O(9^(n*n))

Space Complexity of Common Recursive Algorithms


Algorithm Space Complexity

Factorial O(n)
Fibonacci O(n)

Binary Search O(log n)

Merge Sort O(n)

Quick Sort O(log n) avg, O(n) worst

Tower of Hanoi O(n)

Strassen’s Matrix Multiplication O(n^2)

Karatsuba Algorithm O(n^log3)

DFS O(V)

N-Queens O(N)

Sudoku Solver O(n*n)

You might also like