# Recursive Algorithms

A recursive algorithm is an algorithm that solves a problem by dividing it into smaller sub-problems of the same nature. The solution of the overall problem is obtained by combining the solutions of the sub-problems. The recursive approach is based on recursive calling, which consists of invoking the same function (or procedure) within the very definition of that function.

• Python
• Algorithms
• Trees
and Graphs
Data Structures
• Numerical
Algorithms
• Searching
& Sorting
• Recursion &
Backtracking

## The generic structure of a recursive algorithm

Recursive algorithms follow a typical structure that includes two main components: the base case and the recurrence relation. Here is a generic structure of a recursive algorithm:

Base case:

• Define one or more base cases that represent situations in which the problem can be solved directly without additional recursive calls.
• Base cases are critical to avoid an infinite loop and ensure termination of the algorithm.

Recurrence Relationship:

• Express the problem as a function of its smaller sub-problems through a recurrence relationship.
• The recurrence relation indicates how the problem of size (n) is related to solving the problem of size (n-1), (n-2), and so on.

Recursive Call:

• Call the function itself with smaller inputs, following the recurrence relationship.
• The recursive call gradually reduces the problem until reaching one of the base cases, allowing the overall problem to be resolved.

Here is an example of pseudocode that follows this structure for calculating the factorial:

Factorial Function(n):
// Base case
If n is 0 or 1:
Return 1

// Recurrence Relationship
Otherwise:
Returns n * Factorial(n - 1)


This pseudocode represents a recursive algorithm for calculating the factorial of (n). The base case handles situations in which (n) is 0 or 1, returning 1. The recurrence relation expresses the factorial of (n) as a function of the factorial of (n-1). The recursive call gradually reduces the problem until the base case is reached.

It is important to note that, when implementing a recursive algorithm in a programming language such as Python, it is crucial to carefully manage the base cases and ensure that the recursive calls progress towards the base cases to avoid an infinite loop.

## The Recurrence Relationship

Recursive algorithms are algorithms that solve by calling themselves with smaller inputs, until reaching a base case that can be solved directly. The recurrence relation is a formal way of expressing the behavior of a recursive algorithm through an equation that describes the problem as a function of its smaller sub-problems.

A classic example of a recursive algorithm is the calculation of the factorial of a number. The recurrence relation for the factorial is:

Here’s what a Python implementation might look like:

def factorial(n):
# Base case
if n == 0 or n == 1:
return 1
else:
# Recurrence relation
return n * factorial(n - 1)

# Example of usage
result = factorial(5)
print("The factorial of 5 is:", result)


Running the code you will get the following result:

The factorial of 5 is: 120

In this case, the base case is and . The recurrence relation indicates that the factorial of a number (n) can be calculated by multiplying (n) by the factorial of the previous number .

Another common example is the algorithm for calculating the Fibonacci sequence. The recurrence relation for the Fibonacci sequence is:

Here is an implementation in Python:

def fibonacci(n):
# Base cases
if n == 0:
return 0
elif n == 1:
return 1
else:
# Recurrence relation
return fibonacci(n - 1) + fibonacci(n - 2)

# Example of usage
result = fibonacci(5)
print("The 5th number in the Fibonacci sequence is:", result)


Running the code you will get the following result:

The 5th number in the Fibonacci sequence is: 5

These examples illustrate the concept of recursive algorithms and the recurrence relationship associated with each problem. However, it is important to note that recursive algorithms can be inefficient for very large inputs due to the overhead of recursive calls. In practice, in some cases, it is preferable to use dynamic programming or memoization techniques to optimize performance.

Let’s see other topics, examples and some considerations regarding recursive algorithms.

### Other Examples of Recursive Algorithms:

#### 1. Towers of Hanoi:

A classic recursive problem is solving the Towers of Hanoi. The objective is to move a number of discs from a starting peg to a destination peg, using an auxiliary peg. The recurrence relation involved in this problem is essential to finding an optimal solution.

def hanoi(n, from_pole, to_pole, auxiliary_pole):
if n == 1:
print(f"Move disk 1 from {from_pole} to {to_pole}")
return
hanoi(n-1, from_pole, auxiliary_pole, to_pole)
print(f"Move disk {n} from {from_pole} to {to_pole}")
hanoi(n-1, auxiliary_pole, to_pole, from_pole)

# Example of usage
hanoi(3, 'A', 'C', 'B')



Running the code you will get the following result:

Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C

#### 2. Calculating the Sum of a List:

A simpler example is calculating the sum of the elements of a list.

def sum_list(lst):
if not lst:
return 0
return lst[0] + sum_list(lst[1:])

# Example of usage
list_of_numbers = [1, 2, 3, 4, 5]
result = sum_list(list_of_numbers)
print("The sum of the list is:", result)



Running the code you will get the following result:

The sum of the list is: 15

## Some Considerations on Recursive Algorithms

Efficiency: Recursive algorithms can be inefficient for complex problems due to the overhead of recursive calls and the possibility of computing the same sub-solutions multiple times. In these cases, strategies such as dynamic programming or memoization can be used to improve performance.

Code Comprehensibility: Recursive algorithms often clearly and concisely express the solution to a problem, reflecting the natural structure of the problem itself. However, they may be more difficult to understand for those who are not familiar with this paradigm.

Recursion Depth: Care must be taken with the depth of recursion, as too many recursive calls can lead to stack overflow (exceeding the capacity of the call stack). This can be mitigated through optimizations or, in some cases, iterations.

Choice of Base Case: Correct definition of base cases is critical. If the base cases are not defined correctly, the algorithm may fail to finish or produce incorrect results.

Debugging Tools: Understanding the execution flow of a recursive algorithm can be facilitated through the use of debugging tools, since it allows you to visualize the sequence of calls and intermediate results.

In general, recursive algorithms are powerful and can be elegant, but it is important to carefully evaluate the choice between a recursive implementation and other techniques based on your specific problem and performance requirements.