You are on page 1of 17

Data Structures and Algorithms - EE 390

Chapter 9
Recursion

Dr. Anirban Dasgupta


Assistant Professor

Department of Electronics And Electrical Engineering, IIT Guwahati


Recursion
• A recursive function is a function that calls itself until a “base
condition” is true, and execution stops
• Inputs become smaller at each step
• A recursive function has at least two parts:
• a base condition
• at least one recursive case
• Advantages:
• only need to define the base condition and the recursive case in a
recursive function
• makes the code pretty simple and short as compared to an iterative
code
Department of Electronics And Electrical Engineering, IIT Guwahati
Recursion: Sum of Natural Numbers
Base Case: Cumsum(1) = 1
Recursion: Cumsum(n) = n + Cumsum(n-1)

Cumsum(5)=5 + Cumsum(4)
=5 + 4 + Cumsum(3)
=5 + 4 + 3 + Cumsum(2)
=5 + 4 + 3 + 2 + Cumsum(1)

Department of Electronics And Electrical Engineering, IIT Guwahati


Recursion: Factorial
Base Case: Factorial(1) = 1
Recursion: Factorial(n) = n × Factorial(n-1)

Factorial(5)=5 × Factorial(4)
=5 × 4 × Factorial(3)
=5 × 4 × 3 × Factorial(2)
=5 × 4 × 3 × 2 × Factorial(1)

Department of Electronics And Electrical Engineering, IIT Guwahati


Recursion: Fibonacci
Base Case: Fibonacci(1) = 1, Fibonacci(0) = 0
Recursion: Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)

Fibonacci(5)=Fibonacci(4) + Fibonacci(3)
=Fibonacci(3) + Fibonacci(2) + Fibonacci(2) + Fibonacci(1)
=Fibonacci(2) + Fibonacci(1) + 2 Fibonacci(2) + Fibonacci(1)
=3 Fibonacci(2) + 2 Fibonacci(1)
=3 (Fibonacci(1) + Fibonacci(0)) + 2 Fibonacci(1)
= 5 Fibonacci(1) + 3 Fibonacci(0)
=5
Department of Electronics And Electrical Engineering, IIT Guwahati
Recursion: Binary Search
Base Case: BS(a) = x
Recursion:
BS(a,i,j,x):
mid=(i+j)//2
if a[mid]==x:
𝑛
return mid 𝑇 + 𝑐, 𝑛 > 1
𝑇 𝑛 =ቐ 2
else: 1, 𝑛 = 1
if a[mid]<x:
return BS(a,i,mid-1,x)
else:
return BS(a,mid+1,j,x)
Department of Electronics And Electrical Engineering, IIT Guwahati
Stack Overflow Error in Recursion
Recursion is implemented using stack as activation records are to be stored in LIFO order

If the base
case is

not not incorrectly


reached defined defined
Department of Electronics And Electrical Engineering, IIT Guwahati
Direct and Indirect Recursion

fun1

fun
fun2

Direct Indirect

Department of Electronics And Electrical Engineering, IIT Guwahati


Solving Recurrences

Substitution Method

Recurrence Tree Method

Master Method
Department of Electronics And Electrical Engineering, IIT Guwahati
Substitution Method
𝑛
𝑇 + 𝑐, 𝑛 > 1
𝑇 𝑛 =ቐ 2
Form the recurrence relation 1, 𝑛 = 1
𝑛
𝑇 𝑛 =𝑇 +𝑐
2
𝑛
=𝑇 +𝑐+𝑐
Use back substitution to reach 4
𝑛
the base case = 𝑇 2 + 2𝑐
2
𝑛
= 𝑇 3 + 3𝑐
2

Advantage: 𝑛
Solve any kind of recurrence relation =𝑇 𝑘
+ 𝑘𝑐
2
𝑘
2 =𝑛
Disadvantage:
𝑇 𝑛 = 𝑇 1 + 𝑐 log 2 𝑛 𝑂(log 2 𝑛)
Sometimes complex derivation
𝑇 𝑛 = 1 + 𝑐 log 2 𝑛

Department of Electronics And Electrical Engineering, IIT Guwahati


Substitution Method
𝑛 × 𝑇 𝑛 − 1 ,𝑛 > 1
𝑇 𝑛 =ቊ
1, 𝑛 = 1
𝑇 𝑛 =𝑛×𝑇 𝑛−1
= 𝑛× 𝑛−1 ×𝑇 𝑛−2
= 𝑛(𝑛 − 1)(𝑛 − 2)𝑇 𝑛 − 3
= n(n − 1)(n − 2)(n − 3)𝑇 𝑛 − 4 𝑂(𝑛!)

𝑛!
= 𝑇 𝑛−𝑘
𝑛−𝑘 !
1=𝑛−𝑘
𝑇 𝑛 = 𝑇 1 𝑛!
𝑇 𝑛 = 𝑛!
Department of Electronics And Electrical Engineering, IIT Guwahati
Recurrence Tree

No. of branches = No. of choices

Department of Electronics And Electrical Engineering, IIT Guwahati


Recurrence Tree Method
𝑛
Consider the recurrence 𝑇 𝑛 = 2𝑇 + 𝑐𝑛
2
𝑛
𝑛 8
4 𝑛
𝑛
2
8
𝑛 𝑐𝑛 log 𝑛
𝑛 8
4 𝑛
𝑛 8
𝑛
𝑛 8
4 𝑛
𝑛 8
𝑛
𝑂(𝑛 log 𝑛)
2
𝑛 8
4 𝑛 Height of the tree
8
𝑐𝑛 𝑐𝑛 𝑐𝑛 𝑐𝑛 … 𝑐𝑛
Department of Electronics And Electrical Engineering, IIT Guwahati
Master Method
𝑛
If the recurrence is of the form 𝑇 𝑛 = 𝑎𝑇 + 𝑓 𝑛 , 𝑎 ≥ 1, 𝑏 > 1
𝑏
𝑛 = size of input data Solution is:
𝑎 = number of subproblems in the recursion 𝑇 𝑛 = 𝑛logba [𝑢 𝑛 ]
𝑛
= size of each subproblem 𝑢 𝑛 depends on ℎ 𝑛
𝑏
𝑓 𝑛 = cost of the work done outside the recursive call 𝑓(𝑛)
ℎ 𝑛 = log a
𝑎 ≥ 1 and 𝑏 > 1 are constants 𝑛 b
𝑓(𝑛) is an asymptotically positive function The relationship between 𝑢 𝑛 and ℎ 𝑛 is
ℎ 𝑛 𝑢 𝑛
Advantage: 𝑛𝑟 , 𝑟 > 0 𝑂(𝑛𝑟 )
Faster derivation 𝑛𝑟 , 𝑟 < 0 𝑂(1)
Disadvantage: log 2 𝑛 𝑖 , 𝑖 ≥ 0 log 2 𝑛 𝑖+1
Only solves relations with specific form 𝑖+1

Department of Electronics And Electrical Engineering, IIT Guwahati


Master Method
For example consider the recurrence
𝑛
𝑇 𝑛 = 8𝑇 + 𝑛2
2
ℎ 𝑛 𝑢 𝑛
Solution is:
𝑛𝑟 , 𝑟 > 0 𝑂(𝑛𝑟 )
𝑇 𝑛 = 𝑛logba 𝑢 𝑛
𝑛𝑟 , 𝑟 < 0 𝑂(1)
𝑎 = 8, 𝑏 = 2
log 2 𝑛 𝑖 , 𝑖 ≥ 0 log 2 𝑛 𝑖+1
𝑇 𝑛 = 𝑛log2 8 𝑢 𝑛 = 𝑛3 𝑢 𝑛
𝑖+1
𝑓(𝑛) 𝑛2
ℎ 𝑛 = log a = 3 = 𝑛−1
𝑛 b 𝑛

𝑇 𝑛 = 𝑛3 . 1 = 𝑂(𝑛3 )

Department of Electronics And Electrical Engineering, IIT Guwahati


Master Method Limitations

The master theorem cannot be used if:

• 𝑇(𝑛) is not monotone. e.g. 𝑇(𝑛) = sin 𝑛


• 𝑓(𝑛) is not a polynomial. e.g. 𝑓(𝑛) = 2𝑛
• 𝑎 is not a constant. e.g. 𝑎 = 2𝑛
• 𝑎 < 1

Department of Electronics And Electrical Engineering, IIT Guwahati


References
• Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 3rd
edition, The MIT Press, McGraw-Hill, 2001.
• C. Cherniavsky and J. A. Storer, An Introduction to Data Structures and
Algorithms, 2nd edition. Birkhauser Boston, 2001.

Department of Electronics And Electrical Engineering, IIT Guwahati

You might also like