Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Blog on Recursion - Mastering Recursion: Concepts, Problems, and Optimization #116

Merged
merged 4 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,29 @@ We are grateful to all the contributors who have helped improve this project. Yo
<sub><b>Ananya Gupta</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/narendra-dhangar">
<img src="https://avatars.githubusercontent.com/u/161828345?v=4" width="100;" alt="narendra-dhangar"/>
<br />
<sub><b>Narendra Dhangar </b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/IRFANSARI">
<img src="https://avatars.githubusercontent.com/u/71096605?v=4" width="100;" alt="IRFANSARI"/>
<br />
<sub><b>Irfan Ansari</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/yashksaini-coder">
<img src="https://avatars.githubusercontent.com/u/115717039?v=4" width="100;" alt="yashksaini-coder"/>
<br />
<sub><b>Yash Kumar Saini</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/yogeswari05">
<img src="https://avatars.githubusercontent.com/u/137740335?v=4" width="100;" alt="yogeswari05"/>
Expand Down Expand Up @@ -154,21 +161,14 @@ We are grateful to all the contributors who have helped improve this project. Yo
<sub><b>Shalini Bhandari</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Mohith1490">
<img src="https://avatars.githubusercontent.com/u/141254298?v=4" width="100;" alt="Mohith1490"/>
<br />
<sub><b>Mohith Singh</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/narendra-dhangar">
<img src="https://avatars.githubusercontent.com/u/161828345?v=4" width="100;" alt="narendra-dhangar"/>
<br />
<sub><b>Narendra Dhangar </b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Kratik1093">
Expand Down
16 changes: 15 additions & 1 deletion blog/authors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,18 @@ ajay-dhangar:
linkedin: ajay-dhangar
github: ajay-dhangar
stackoverflow: 18530900
newsletter: https://ajay-dhangar.github.io
newsletter: https://ajay-dhangar.github.io

narendra-dhangar:
name: Narendra Dhangar
title: B.Tech (CSE) Student
url: https://www.instagram.com/narendra_dh._05/
image_url: https://github.com/narendra-dhangar.png
email: [email protected]
page: true # Turns the feature on
description: >
A dedicated Computer Science and Engineering student with a passion for problem-solving and algorithms. I am constantly exploring various fields of computer science, from data structures and algorithms to web development and machine learning. As an avid learner, I enjoy tackling challenging problems and sharing knowledge with the community. Eager to contribute to open-source projects and make an impact in the tech world.

socials:
linkedin: narendra-dhangar-880558297
github: narendra-dhangar
134 changes: 134 additions & 0 deletions blog/mastering-recursion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
slug: mastering-recursion
title: 'Mastering Recursion: Concepts, Problems, and Optimization'
authors: [narendra-dhangar]
tags: [recursion, algorithms, dsa, optimization, narendra-dhangar]
---

Recursion is a fundamental concept in programming and problem-solving. It provides an elegant solution to many problems, yet understanding recursion requires a solid grasp of the underlying principles. In this blog, we will dive deep into the concept of recursion, explore common recursive problems, and look at techniques for optimizing recursive algorithms.

<!-- truncate -->

## What is Recursion?

Recursion is a process where a function calls itself to solve a problem. A recursive function breaks down a problem into smaller, easier-to-solve sub-problems.

### Structure of a Recursive Function:
A typical recursive function consists of two main parts:
1. **Base Case**: The condition under which the recursion ends. Without this, the function would call itself indefinitely, leading to a stack overflow.
2. **Recursive Case**: The part where the function calls itself to solve smaller instances of the problem.

### Example of Recursion:
Here’s a simple example of recursion, calculating the factorial of a number `n`:

```javascript
function factorial(n) {
if (n === 0) {
return 1; // Base case: factorial of 0 is 1
}
return n * factorial(n - 1); // Recursive case
}
```

In this example, the problem of calculating the factorial is broken down into multiplying `n` by the factorial of `n - 1`, until it reaches the base case.

---

## Key Concepts in Recursion:

1. **Base Case and Recursive Case**: These are the most critical components of any recursive function. Without a base case, your function will recurse infinitely.

2. **Stack Memory**: Every time a recursive function is called, its execution is stored in the stack. When the base case is reached, the function starts returning, popping values off the stack.

3. **Recursive Tree**: Visualizing recursion as a tree can help in understanding how recursive calls are executed. Each recursive call splits into further sub-calls until the base case is hit.

### Common Pitfalls:
- **Missing Base Case**: Leads to infinite recursion.
- **Redundant Computations**: In some problems, recursive calls recompute the same values multiple times (like in the Fibonacci sequence).

---

## Recursion Problems:

### 1. Fibonacci Series:

The Fibonacci series is a classic problem to demonstrate recursion. The nth Fibonacci number is the sum of the two preceding numbers.

**Recursive Fibonacci Solution**:

```javascript
function fibonacci(n) {
if (n <= 1) {
return n; // Base case
}
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive case
}
```

While this solution is intuitive, it performs redundant computations and can be very slow for larger values of `n`.

---

## Optimizing Recursive Algorithms:

### 1. **Memoization**:
Memoization stores the results of expensive function calls and reuses them when the same inputs occur again, reducing the time complexity of the function.

**Optimized Fibonacci with Memoization**:

```javascript
function fibonacci(n, memo = {}) {
if (n in memo) {
return memo[n]; // Return stored result if available
}
if (n <= 1) {
return n; // Base case
}
memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo); // Recursive call with memoization
return memo[n];
}
```
This optimized version reduces the time complexity from $O(2^n)$ to $O(n)$.

### 2. **Tail Recursion**:
Tail recursion is a specific form of recursion where the recursive call is the last thing executed by the function. Tail-recursive functions can be optimized by some compilers or interpreters to prevent stack overflow.

**Tail-Recursive Factorial**:

```javascript
function tailFactorial(n, accumulator = 1) {
if (n === 0) {
return accumulator; // Base case
}
return tailFactorial(n - 1, n * accumulator); // Tail-recursive call
}
```

### 3. **Recursion to Iteration**:
For some problems, recursion can be replaced with iteration to avoid deep recursion, which can lead to stack overflow. Converting recursive functions to iterative ones ensures better memory management.

---

## Problem-Solving with Recursion:

Recursion is widely used to solve various algorithmic problems, such as:

1. **Binary Search**:
A divide-and-conquer algorithm that works efficiently by dividing the array into halves.

2. **Towers of Hanoi**:
A mathematical problem that requires moving disks between rods under certain conditions.

3. **Permutations and Combinations**:
Recursion is used to generate all possible arrangements of a set of items.

4. **Tree Traversal**:
Recursion is the natural way to traverse tree structures, such as in **pre-order**, **in-order**, and **post-order** traversals.

---

## Conclusion:

Recursion is a powerful tool in a developer's arsenal. However, it is essential to understand when and how to use it effectively. Optimizing recursive algorithms with techniques like memoization, tail recursion, or converting them into iterative solutions ensures they remain efficient even for large inputs.

With practice, mastering recursion will open doors to solving complex problems in a more structured and elegant way.