Recursion
CS 250 Data Structures and Algorithms
Prof. Dr. Faisal Shafait
1 / 32
Recursion Recursion
Learning Objectives
In this lecture you will learn
Basic understanding of Recursion
How recursion can be used for efficient implementation of various
applications
Determine when a recursive solution is appropriate for a problem
2 / 32
Recursion Recursion
Recursion: definition
Recursion is a method of solving problems
involving breaking a problem down into smaller and smaller
subproblems.
until a problem small enough to be solved trivially.
Recursion involves a function calling itself.
3 / 32
Recursion Recursion
Example: iterative sum
Calculate the sum of a list of numbers such as: [1,3,5,7,9].
4 / 32
Recursion Recursion
Example: iterative sum
Calculate the sum of a list of numbers such as: [1,3,5,7,9].
An iterative function that computes the sum is,
LIST-SUM(list):
theSum := 0
for i in list:
theSum = theSum + i
return theSum
5 / 32
Recursion Recursion
Example: recursive sum
Consider a fully parenthesized expression adding pair of numbers,
((((1 + 3) + 5) + 7) + 9)
6 / 32
Recursion Recursion
Example: recursive sum
Consider a fully parenthesized expression adding pair of numbers,
((((1 + 3) + 5) + 7) + 9)
We can also parenthesize the expression the other way around,
(1 + (3 + (5 + (7 + 9))))
7 / 32
Recursion Recursion
Example: recursive sum
Consider a fully parenthesized expression adding pair of numbers,
((((1 + 3) + 5) + 7) + 9)
We can also parenthesize the expression the other way around,
(1 + (3 + (5 + (7 + 9))))
We can use the following sequence of simplifications to compute a
final sum.
total = (1 + (3 + (5 + (7 + 9))))
total = (1 + (3 + (5 + 16)))
total = (1 + (3 + 21))
total = (1 + 24)
total = 25
8 / 32
Recursion Recursion
Example: recursive sum
LIST-SUM(list) = first(list) + LIST-SUM(LIST-REMAINING(list))
9 / 32
Recursion Recursion
Example: recursive sum
LIST-SUM(list) = first(list) + LIST-SUM(LIST-REMAINING(list))
LIST-SUM(list):
if LIST-LEN(numList) = 1:
return list[0]
else:
return list[0] + LIST-SUM(list[1:])
10 / 32
Recursion Recursion
Example: recursive sum
LIST-SUM(list) = first(list) + LIST-SUM(LIST-REMAINING(list))
Series of recursive calls adding a list of numbers
11 / 32
Recursion Recursion
Example: recursive sum
LIST-SUM(list) = first(list) + LIST-SUM(LIST-REMAINING(list))
Series of recursive returns from adding a list of numbers
12 / 32
Recursion Recursion
Recursion: the three laws
All recursive algorithms must obey three important laws:
1 a recursive algorithm must have a base case.
2 a recursive algorithm must change its state and move toward the
base case.
3 a recursive algorithm must call itself, recursively.
13 / 32
Recursion Recursion
Recursion: the three laws
1 A base case is the condition that allows the algorithm to stop
recursing.
a problem small enough to be solved directly.
2 Arrange for a change of state that moves the algorithm toward the
base case.
some data is modified.
usually data gets smaller.
3 The final law is that the algorithm must call itself.
the very definition of recursion.
the logic is not circular; but is an elegant expression to solve a
problem by breaking it down into a smaller and easier problems.
14 / 32
Recursion Recursion
Recursion: question
How many recursive calls are made when computing the sum of the list
[2,4,6,8,10]?
1 6
2 5
3 4
4 3
15 / 32
Recursion Recursion
Recursion: question
Suppose you are going to write a recursive function to calculate the
factorial of a number.
fact(n) returns n ∗ n − 1 ∗ n − 2 ∗ ...
Where the factorial of zero is defined to be 1. What would be the most
appropriate base case?
1 n == 0
2 n == 1
3 n >= 0
4 n <= 1
16 / 32
Recursion Recursion
Recursion: example
To print the characters of a string,
17 / 32
Recursion Recursion
Recursion: example
To print the characters of a string,
PRINT-STRING(string)
if LENGTH(string) = 0: // base case
return
PRINT-CHAR(FIRST(s)) // do some work
PRINT-STRING(REST(s)) // recursive call on simpler problem
18 / 32
Recursion Recursion
Recursion: example
What does this do?
PRINT-STRING(string)
if LENGTH(string) = 0: // base case
return
PRINT-STRING(REST(s)) // recursive call on simpler problem
PRINT-CHAR(FIRST(s)) // do some work
19 / 32
Recursion Recursion
Recursion: example
What does this do?
PRINT-STRING(string)
if LENGTH(string) = 0: // base case
return
PRINT-CHAR(FIRST(s)) // do some work
PRINT-STRING(REST(s)) // recursive call on simpler problem
PRINT-CHAR(FIRST(s)) // do some work
20 / 32
Recursion Recursion
Recursion: example
Compute power for an integer a.
21 / 32
Recursion Recursion
Recursion: example
Compute power for an integer a.
a n can be defined by: a 0 = 1, a n = a × (a n−1 )
POWER(a,n)
if n = 0:
return 1 // Base case
else:
return a * POWER(a,n-1) // Do some work after
22 / 32
Recursion Recursion
Recursion: example
Compute factorial for an integer n.
(
1 n <= 1
FACT (n) =
n ∗ FACT (n − 1) otherwise
23 / 32
Recursion Recursion
Recursion: example
Compute factorial for an integer n.
(
1 n <= 1
FACT (n) =
n ∗ FACT (n − 1) otherwise
FACT(n)
nf := n
while n != 1:
n := n - 1
nf := n * nf
return nf
24 / 32
Recursion Recursion
Recursion: example
Compute factorial for an integer n.
(
1 n <= 1
FACT (n) =
n ∗ FACT (n − 1) otherwise
FACT(n)
nf := n
while n != 1:
n := n - 1
nf := n * nf
return nf
FACT(n)
if n = 1:
return 1
else:
return n \cdot FACT(n-1)
25 / 32
Recursion Recursion
Recursion: example
Give iterative and recursive algorithms for finding the n th term of the
sequence defined by:
a0 = 1, a1 = 3, a2 = 5, an = an−1 + (an−2 )2 + (an−3 )3 .
26 / 32
Recursion Recursion
Recursion: example
Give iterative and recursive algorithms for finding the n th term of the
sequence defined by:
a0 = 1, a1 = 3, a2 = 5, an = an−1 + (an−2 )2 + (an−3 )3 .
I(n):
if n < 3:
return 2n + 1
x := 1, y := 3, z := 5
for i := 1 to n-2:
w := z + y * y + x * x * x
x := y, y := z, z := w
return z
27 / 32
Recursion Recursion
Recursion: example
Give iterative and recursive algorithms for finding the n th term of the
sequence defined by:
a0 = 1, a1 = 3, a2 = 5, an = an−1 + (an−2 )2 + (an−3 )3 .
I(n):
if n < 3:
return 2n + 1
x := 1, y := 3, z := 5
for i := 1 to n-2:
w := z + y * y + x * x * x
x := y, y := z, z := w
return z
R(n):
if n < 3:
return 2n + 1
else:
return R(n-1) + POWER(R(n-2), 2) + POWER(R(n-3), 3)
28 / 32
Recursion Recursion
Recursion: infinite recursion
Recursive equivalent of an infinite loop,
29 / 32
Recursion Recursion
Recursion: infinite recursion
Recursive equivalent of an infinite loop,
TO-INFINITY-AND-BEYOND(x):
return TO-INFINITY-AND-BEYOND(x) //recurred with the same case
30 / 32
Recursion Recursion
Recursion: infinite recursion
Recursive equivalent of an infinite loop,
TO-INFINITY-AND-BEYOND(x):
return TO-INFINITY-AND-BEYOND(x) //recurred with the same case
Sometimes by accident,
R(n):
if n == 1:
return 1
if n mod 2 == 0:
return R( n/2 )
else:
return R( 3*n-1 )
31 / 32
Recursion Recursion
Reading Material
For further reading, please refer to
Data Structures by Weiss: Chapter 8, Sections 8.1–8.3
https://www.tutorialspoint.com/data_structures_algorithms
/recursion_basics.htm
32 / 32