Back

Recursion

Idealogic’s Glossary

Recursion is a method of calling a function in the course of solving a problem so as to solve a smaller problem. It is especially helpful for problems which can be divided into several subproblems of the same type. The function keeps on calling itself with a similar but different problem until it gets to a certain point where the problem cannot be subdivided any further; this is called the base case. Recursion is a very useful technique in a number of algorithms where a problem can be decomposed into sub-problems in some ways, for example, in tree traversal, sorting or any mathematical operations.

Key Concepts of Recursion

1. Base Case:

- Base case is the point which puts an end to the recursing of the method or function. It usually deals with the first case of the problem and avoids the function to call itself over and over again.

2. Recursive Case:

- In the recursive case, the function calls itself with a slightly different problem from the one which was presented at the beginning. This involves the ability to divide the problem into sub problems which are solved and which are closer to the base case.

3. Call Stack:

- Every recursive call is stored in the stack, or the set of active function calls. The call stack stores all the function’s state – variables and the address to which the execution should return. When the base case is reached the stack is popped and the function returns to the state that it was in when the call was made.

4. Direct and Indirect Recursion:

- The direct recursion is the situation where a function calls another function that is the same with the calling function. It is called indirect recursion where a function calls another function and that function in turn calls the original function which was called initially.

5. Tail Recursion:

- Recursion is a concept where a function calls itself again within its own body and tail recursion is one type of recursion in which recursive call occurs at the end of the function. This proves helpful since it helps in avoiding the expenses that come with recursion.

Common Use Cases for Recursion

1. Mathematical Computations:

- Most often recursion is used when working with problems that are in a way reducible to a set of repeated sub-problems such as factorials and sequences.

2. Tree and Graph Traversal:

- Recursion is useful in solving problems that involve hierarchical data structures such as, trees and graphs since it enables one to move to the next level of the structure.

3. Divide and Conquer Algorithms:

- Recursion is a technique where a problem is broken down into the sub-problems, these sub-problems are solved recursively and then solutions of sub-problems are combined.

4. Dynamic Programming:

- Recursion has been employed in dynamic programming for the purpose of decomposing a problem into sub-problems which are related and overlapping.

5. Parsing and Expression Evaluation:

- Recursive methods are employed in languages analyzing and expressions evaluation according to the grammar rules.

Advantages of Recursion

1. Simplifies Complex Problems:

- Recursion is the process of dividing a problem into sub problems until the simplest of problems are found thus leading to better solutions.

2. Natural Fit for Recursive Data Structures:

- Recursive functions are especially used when dealing with some data structures which are constructed using recursion such as trees and graphs.

3. Eliminates Need for Explicit Stacks:

- Recursion itself takes care of the function call stack that is otherwise has to be controlled manually.

4. Facilitates Divide and Conquer:

- Recursion is important in the area of algorithms since it involve breaking down a problem into sub-problems and solving it efficiently.

Disadvantages and Considerations

1. Performance Overhead:

- As for recursive functions, calling a function from within itself may slow down program’s performance because of the many function calls that are made and the use of the stack.

2. Risk of Stack Overflow:

- Deep or infinite recursion is one of the causes of stack overflow, when the call stack space is exceeded.

3. Difficult to Debug:

- Recursive functions are not easy to debug and this is especially the case when working with recursive functions.

4. Inefficient for Simple Problems:

- Iteration is better than recursion for problems that do not have to be divided into sub- problems.

5. Memory Usage:

- But again, recursive functions consume a lot of memory especially when depth of recursion is high due to the stack that is assigned to it.

Conclusion

To put in simply words, Recursion is a method of solving a problem where a function calls itself to solve a similar but smaller problem. It is well suited to problems that can be easily decomposed into sub-problems such as graph traversal and general algorithm design. Recursion is great and can give elegant and efficient solutions but it has its pitfalls and complications for example, performance and stack issues, so one must be very cautious when using it.