Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

4.0 Lecture 3 Recursion

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Lecture on Recursion

1 Introduction to Recursion
Definition: Recursion is a method of solving a problem where the solution involves solving smaller
instances of the same problem. A recursive function is a function that calls itself in order to solve a
problem.
Key Concepts:
ˆ Base Case: The condition under which the recursive function stops calling itself. It is the simplest
instance of the problem that can be solved directly.
ˆ Recursive Case: The part of the function where the function calls itself with a smaller or simpler
version of the original problem.

2 How Recursion Works


Example 1: Factorial of a Number
The factorial of a number n (denoted as n!) is defined as:
n! = n × (n − 1) × (n − 2) × · · · × 1
We can define the factorial function recursively as:
(
1 if n = 0 or n = 1
Factorial(n) =
n × Factorial(n − 1) if n > 1
Recursive Function in Pseudocode:
function Factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * Factorial(n-1)

2.1 Example 2: Fibonacci Sequence


The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones.
The sequence starts as 0, 1, 1, 2, 3, 5, 8, 13, and so on.
The Fibonacci sequence can be defined recursively as:

0
 if n = 0
Fibonacci(n) = 1 if n = 1

Fibonacci(n − 1) + Fibonacci(n − 2) if n > 1

Recursive Function in Pseudocode:

1
function Fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return Fibonacci(n-1) + Fibonacci(n-2)

3 Advantages and Disadvantages of Recursion


Advantages:

ˆ Simplifies the code for problems that have a natural recursive structure, such as tree traversals or
combinatorial problems.

ˆ Reduces the need for complex loops.

Disadvantages:

ˆ Recursive functions can be less efficient in terms of memory usage due to the overhead of multiple
function calls.

ˆ Without a proper base case, a recursive function can lead to infinite loops and stack overflow errors.

4 Examples in Programming
Example 1: Sum of Array Elements
A recursive function to find the sum of an array of numbers:

function SumArray(arr, n):


if n == 0:
return 0
else:
return arr[n-1] + SumArray(arr, n-1)

Example 2: Binary Search


Binary Search is an algorithm to find the position of a target value within a sorted array. It works by
repeatedly dividing the search interval in half.

function BinarySearch(arr, target, low, high):


if low > high:
return -1
mid = (low + high) / 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
return BinarySearch(arr, target, low, mid-1)
else:
return BinarySearch(arr, target, mid+1, high)

2
5 Homework/Practice Problems
1. Write a recursive function to calculate the greatest common divisor (GCD) of two numbers using
Euclid’s algorithm.

2. Implement a recursive function to compute the sum of the digits of a given number.

3. Write a recursive function to reverse a string.

4. Implement a recursive function to solve the Tower of Hanoi problem with 3 disks.

5. Write a recursive function to check whether a number is a palindrome.

6 Conclusion
Recursion is a powerful tool in programming that helps to solve complex problems by breaking them down
into simpler subproblems. Understanding how to use recursion effectively is essential for solving problems
with a recursive structure.

You might also like