You are on page 1of 18

MODULE 1: RECURSION

Note: Some of these slides have been adapted from


slides freely available on the Internet, geeksforgeeks and
2801ICT Computing Algorithms – Module 1: Recursion 1 also from the slides accompanying the course textbook.
Module Topics
1) What is recursion?
2) Types of recursion
3) Stack Overflow 
4) Tower of Hanoi

2801ICT Computing Algorithms – Module 1: Recursion 2


What is recursion?

The process in which a function calls itself directly or


indirectly is called recursion and the corresponding
function is called as recursive function.

2801ICT Computing Algorithms – Module 1: Recursion Topic: What is recursion?


3
Types of Recursion
Recursion are mainly of two types
Tail Recursion
Head Recursion
o Direct Recursion Tree Recursion
Nested Recursion

o Indirect Recursion

2801ICT Computing Algorithms – Module 1: Recursion Topic: Recursion Types


4
Direct Recursion: Tail Recursion
If a recursive function calling itself and that recursive call is the
last statement in the function then it’s known as Tail Recursion.
After that call the recursive function performs nothing.
What is the output of this code
fragment?

Can you draw a tracing tree showing


how the calls are made and how the
outputs are produced?

What is the time and space complexity of this


code fragment (tail recursion)?

2801ICT Computing Algorithms – Module 1: Recursion Topic: Tail Recursion


5
Direct Recursion: Tail Recursion (Contd.)
Can you draw a tracing tree showing
how the calls are made and how the
outputs are produced?

2801ICT Computing Algorithms – Module 1: Recursion Topic: Tail Recursion


6
Direct Recursion: Tail Recursion (Contd.)
What is the time and space complexity of this
code fragment (tail recursion)?

Can we rewrite this code fragment to have


better space complexity?

What is the space


complexity now?
Home Work 

2801ICT Computing Algorithms – Module 1: Recursion Topic: Tail Recursion


7
Direct Recursion: Head Recursion
If a recursive function calling itself and that recursive call is the
first statement in the function, then it’s known as Head
Recursion. There’s no statement, no operation before the call.
What is the output of this
code fragment?

Can you draw a tracing tree showing


how the calls are made and how the
outputs are produced?

What is the time and space complexity of this


code fragment (head recursion)?

2801ICT Computing Algorithms – Module 1: Recursion Topic: Head Recursion


8
Direct Recursion: Head Recursion (Contd.)
Can you draw a tracing tree showing
how the calls are made and how the
outputs are produced?

2801ICT Computing Algorithms – Module 1: Recursion Topic: Head Recursion


9
Direct Recursion: Head Recursion (Contd.)
What is the time and space complexity of this
code fragment (head recursion)?

Can we rewrite this code fragment to have


better space complexity?

What is the space


complexity now?
Home Work 

2801ICT Computing Algorithms – Module 1: Recursion Topic: Head Recursion


10
Direct Recursion: Tree Recursion
If a recursive function calling itself for one time then it’s known as
Linear Recursion. Otherwise if a recursive function calling itself
for more than one time then it’s known as Tree Recursion.
What is the output of this
code fragment?

Can you draw a tracing tree showing


how the calls are made and how the
outputs are produced?

What is the time and space complexity of this


code fragment (tree recursion)?

2801ICT Computing Algorithms – Module 1: Recursion Topic: Tree Recursion


11
Direct Recursion: Tree Recursion (Contd.)

Can you draw a tracing tree showing


how the calls are made and how the
outputs are produced?

2801ICT Computing Algorithms – Module 1: Recursion Topic: Tree Recursion


12
Direct Recursion: Tree Recursion (Contd.)
What is the time and space complexity of this
code fragment (tree recursion)?

20
21
22
23

T(n)=20+21 +…+ 2n =2n+1 – 1≤2n+1=2x2n= O(2n)


Can you explain why space complexity is O(n)?
Home Work 

2801ICT Computing Algorithms – Module 1: Recursion Topic: Tree Recursion


13
Direct Recursion: Nested Recursion
In this recursion, a recursive function will pass the parameter as a
recursive call. That means “recursion inside recursion”.

What is the output of this


code fragment?

Can you draw a tracing tree showing


how the calls are made and how the
outputs are produced?

What is the time and space complexity of this


code fragment (nested recursion)?
Home Work 

2801ICT Computing Algorithms – Module 1: Recursion Topic: Nested Recursion


14
Direct Recursion: Nested Recursion (Contd.)
Can you draw a tracing tree showing
how the calls are made and how the
outputs are produced?

2801ICT Computing Algorithms – Module 1: Recursion Topic: Nested Recursion


15
Indirect Recursion
In this recursion, there may be more than one functions and they
are calling one another in a circular manner.
What is the output
of this code
fragment?

From the above diagram fun(A) is calling


for fun(B), fun(B) is calling for fun(C) and Home Work 
fun(C) is calling for fun(A) and thus it Can you draw a tracing tree
makes a cycle. showing how the calls are
made and how the outputs
are produced?

2801ICT Computing Algorithms – Module 1: Recursion Topic: Indirect Recursion


16
Why Stack Overflow error occurs in recursion?
If the base case is not reached or not defined, then the stack
overflow problem may arise.

Can you give an example


and explain why there will be
an stack overflow?

Why does stack overflow


eventually?

2801ICT Computing Algorithms – Module 1: Recursion Topic: Stack Overflow


17
Tower of Hanoi
• There are 3 pegs
• There are N disks stacked on the first peg. (The disks
have a hole in the center).
Problem Hanoi(N):
•Move the N disks from peg 1 to peg 3
•You must obey a set of rules.

Tower of Hanoi Rules: Initially situation

•You can only move one disk at a time (from any peg to any other peg), and
•You may not stack a smaller disk on top of a larger disk

Move 3 disks from peg 1 to peg 3: what are the (min) moves? Home Work 

http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/13/hanoi.html

2801ICT Computing Algorithms – Module 1: Recursion Topic: Indirect Recursion


18

You might also like