Recursion and Iteration (OCR A-Level Computer Science): Revision Notes
Recursion and Iteration
Overview
Recursion and iteration are two fundamental approaches for solving problems that involve repetitive tasks or processes. Both techniques allow programmes to repeat a set of instructions, but they do so in different ways. Understanding their principles, benefits, and drawbacks is essential for selecting the most appropriate approach for a given problem.
Recursion
Definition:
- Recursion occurs when a function calls itself to solve smaller instances of the same problem.
- Each recursive call works on a smaller problem until a stopping condition (also known as a base case) is met.
Key Features of Recursion:
- Base Case: The condition that stops the recursion.
- Recursive Case: The part of the function that calls itself with a smaller or simpler input.
- Call Stack: Each recursive call is added to the call stack. The stack stores the current state of each call and unwinds once the base case is reached.
Example of Recursion: Factorial Function
Pseudocode:
FUNCTION factorial(n)
IF n == 0 THEN
RETURN 1 // Base case
ELSE
RETURN n * factorial(n - 1) // Recursive case
ENDIF
END FUNCTION
Tracing the Recursive Function (factorial(3)):
- factorial(3) → 3 * factorial(2)
- factorial(2) → 2 * factorial(1)
- factorial(1) → 1 * factorial(0)
- factorial(0) → 1 (base case)
- Result: 3 * 2 * 1 = 6
Iteration
Definition:
Iteration uses loops (e.g., FOR, WHILE) to repeatedly execute a block of code until a condition is met.
Key Features of Iteration:
- Loop Condition: A condition that determines whether the loop should continue.
- Loop Body: The set of instructions that are executed during each iteration.
- Termination: The loop ends when the condition is no longer true.
Example of Iteration: Factorial Function
Pseudocode:
FUNCTION factorial(n)
DECLARE result = 1
FOR i = 1 TO n
result = result * i
ENDFOR
RETURN result
END FUNCTION
Tracing the Iterative Function (factorial(3)):
- i = 1, result = 1 * 1 = 1
- i = 2, result = 1 * 2 = 2
- i = 3, result = 2 * 3 = 6
Comparison of Recursion and Iteration
| Aspect | Recursion | Iteration |
|---|---|---|
| Approach | Function calls itself. | Uses loops to repeat instructions. |
| Base Case/Condition | Requires a base case to stop recursion. | Loop condition determines when the loop stops. |
| Memory Usage | Uses more memory due to the call stack. | Uses less memory as it doesn't rely on the call stack. |
| Readability | Often more elegant and easier to read for complex tasks. | May require more lines of code for certain problems. |
| Performance | Can be slower due to function call overhead. | Generally faster and more efficient. |
| Risk of Errors | Risk of stack overflow if the base case is missing. | Risk of infinite loops if loop condition is incorrect. |
Translating Between Recursion and Iteration
Some problems can be solved using either recursion or iteration. Here's how to convert between them:
Recursive to Iterative:
- Identify the base case and recursive case.
- Replace recursive calls with a loop that replicates the function calls.
Recursive Factorial Example:
FUNCTION factorial(n)
IF n == 0 THEN
RETURN 1
ELSE
RETURN n * factorial(n - 1)
ENDIF
END FUNCTION
Iterative Equivalent:
FUNCTION factorial(n)
DECLARE result = 1
FOR i = 1 TO n
result = result * i
ENDFOR
RETURN result
END FUNCTION
Iterative to Recursive:
- Replace loop logic with recursive calls.
- Include a base case to prevent infinite recursion.
Iterative Sum Example:
FUNCTION sum(n)
DECLARE total = 0
FOR i = 1 TO n
total = total + i
ENDFOR
RETURN total
END FUNCTION
Recursive Equivalent:
FUNCTION sum(n)
IF n == 0 THEN
RETURN 0 // Base case
ELSE
RETURN n + sum(n - 1) // Recursive case
ENDIF
END FUNCTION
Benefits and Drawbacks of Recursion
Benefits of Recursion:
- Simplifies Code for Complex Problems:
- Recursion can provide a clear, elegant solution for problems like tree traversal or divide-and-conquer algorithms (e.g., quicksort, mergesort).
- Natural Fit for Some Problems:
- Problems that inherently involve repetitive sub-problems, such as factorials, Fibonacci sequences, and graph traversal.
Drawbacks of Recursion:
- Higher Memory Usage:
- Each recursive call uses stack memory, which can lead to stack overflow for deep recursions.
- Performance Overhead:
- Recursive calls involve function call overhead.
Benefits and Drawbacks of Iteration
Benefits of Iteration:
- More Efficient:
- Uses less memory and is generally faster since it avoids the overhead of function calls.
- Better for Large Data Sets:
- Avoids stack overflow issues.
Drawbacks of Iteration:
- Less Intuitive for Complex Problems:
- Iterative solutions for problems like tree traversal may be harder to implement and read.
- More Code:
- May require additional variables and lines of code for certain tasks.
Trace Tables for Recursion and Iteration
Purpose of Trace Tables:
Trace tables help track the values of variables at each step or call, making it easier to debug and understand the programme's behaviour.
Example: Tracing Recursive Factorial(3):
| Call | n | Return Value |
|---|---|---|
| factorial(3) | 3 | 3 * factorial(2) |
| factorial(2) | 2 | 2 * factorial(1) |
| factorial(1) | 1 | 1 * factorial(0) |
| factorial(0) | 0 | 1 |
Final result:
Key Takeaway
- Recursion involves a function calling itself, suitable for problems with a natural hierarchical structure (e.g., tree traversal).
- Iteration uses loops to repeat tasks, often more efficient for large data sets.
- Both approaches have their benefits and drawbacks, and the choice depends on the problem and constraints.
- Understanding trace tables helps in debugging and verifying the correctness of recursive and iterative solutions.