Concept of Recursion

COS2611 - Programming: Data Structures · Recursion

Concept of Recursion

Recursion is a programming technique where a function calls itself to solve a problem. This approach is useful for solving problems that can be divided into smaller, simpler subproblems. Each recursive call works on a smaller instance of the original problem until a base case is reached. A base case is a condition that stops the recursion.

Understanding Recursion

A recursive function must have two main components: the base case and the recursive case. The base case is the simplest instance of the problem, which can be solved directly without further recursion. The recursive case is where the function calls itself with a modified argument, gradually approaching the base case.

Remember: Every recursive function must have a base case to prevent infinite recursion.

Example: Factorial Function

The factorial of a non-negative integer n, denoted as n!, is the product of all positive integers from 1 to n. It can be defined recursively as follows:

  • Base case: 0! = 1
  • Recursive case: n! = n × (n - 1)! for n > 0

Here is how you can implement the factorial function using recursion in C++:

int factorial(int n) {  // Function to calculate factorial of n  if (n == 0) {    return 1;  }  return n * factorial(n - 1);}

Let’s see how the function works when we calculate 4!:

  1. factorial(4) calls factorial(3)
  2. factorial(3) calls factorial(2)
  3. factorial(2) calls factorial(1)
  4. factorial(1) calls factorial(0)
  5. factorial(0) returns 1
  6. factorial(1) returns 1 × 1 = 1
  7. factorial(2) returns 2 × 1 = 2
  8. factorial(3) returns 3 × 2 = 6
  9. factorial(4) returns 4 × 6 = 24

The final result is 4! = 24.

Watch out: If the base case is not defined correctly, the function may enter an infinite loop, leading to a stack overflow error.

Visualising Recursion

Visualising how recursion works can help you understand the flow of execution. You can think of the recursive calls as a stack of function calls. Each call is added to the stack until the base case is reached. Once the base case is reached, the calls begin to return, unwinding the stack.

Advantages of Recursion

Recursion can make code easier to read and write. It allows you to express complex problems in a simpler way. For example, problems involving trees or graphs are often easier to solve using recursion.

Disadvantages of Recursion

Despite its advantages, recursion has some drawbacks. Recursive functions can consume a lot of memory due to the function call stack. If the recursion depth is too high, it may lead to stack overflow. Additionally, recursive functions may be less efficient than their iterative counterparts for some problems.

Example: Fibonacci Sequence

The Fibonacci sequence is another classic example of recursion. The sequence is defined as follows:

  • Base case: fib(0) = 0
  • Base case: fib(1) = 1
  • Recursive case: fib(n) = fib(n - 1) + fib(n - 2) for n > 1

Here is the C++ implementation:

int fibonacci(int n) {  // Function to calculate the nth Fibonacci number  if (n == 0) {    return 0;  }  if (n == 1) {    return 1;  }  return fibonacci(n - 1) + fibonacci(n - 2);}

Calculating fibonacci(4) works as follows:

  1. fibonacci(4) calls fibonacci(3) and fibonacci(2)
  2. fibonacci(3) calls fibonacci(2) and fibonacci(1)
  3. fibonacci(2) calls fibonacci(1) and fibonacci(0)
  4. fibonacci(1) returns 1
  5. fibonacci(0) returns 0
  6. fibonacci(2) returns 1 + 0 = 1
  7. fibonacci(1) returns 1
  8. fibonacci(3) returns 1 + 1 = 2
  9. fibonacci(2) returns 1
  10. fibonacci(4) returns 2 + 1 = 3

The final result is fibonacci(4) = 3.

Recursion vs Iteration

Recursion and iteration are two different approaches to solving problems. Iteration uses loops to repeat a block of code until a condition is met. While recursion can be more elegant for certain problems, iteration can be more efficient in terms of memory usage.

Tip: When deciding between recursion and iteration, consider the problem at hand and the potential impact on performance.

Conclusion

Recursion is a powerful tool in programming. Understanding how to implement recursive functions is essential for solving complex problems efficiently. Practice writing recursive functions to become comfortable with this technique.

Summary

  • Recursion involves a function calling itself to solve a problem.
  • Every recursive function must have a base case.
  • Recursive functions can be easier to read and write.
  • Recursion can consume more memory than iteration.

Check your understanding

  1. What is the base case in a recursive function?
  2. Explain how the factorial function is calculated using recursion.
  3. What are the advantages of using recursion?
  4. How does the Fibonacci sequence illustrate the concept of recursion?