RECURSION
CONTENTS
Introduction
Recursive solution and recursive functions
Examples
Backtracking algorithm
Data Structures and Algorithms 2 FIT – HNUE 2021
INTRODUCTION
Data Structures and Algorithms 3 FIT – HNUE 2020
RECURSION
• Recursion (adjective: recursive) occurs when a thing is defined in
terms of itself or of its type.
• Example:
Recursion can be defined by two
• Natural number properties:
• 1 is a natural number •A simple base case (or cases) — a
• 𝑥 is a natural number If (𝑥 − 1) is a natural number terminating scenario that does not
• Factorial: n! use recursion to produce an answer
• 0! = 1 •A recursive step — a set of rules
that reduces all successive cases
• 𝑛! = 𝑛 ∗ 𝑛 − 1 !
toward the base case
𝑛
• 𝑆 𝑛 = σ𝑘=1 𝑎𝑘 ; Recursion of 𝑆 𝑛 ???
Data Structures and Algorithms 4 FIT – HNUE 2021
RABBIT REPRODUCTION
• The problem assumes the following
conditions:
• Begin with one male rabbit and female
rabbit that have just been born.
• Rabbits reach sexual maturity after one
month.
• The gestation period of a rabbit is one
month.
• After reaching sexual maturity, female
rabbits give birth every month.
• A female rabbit gives birth to one male
rabbit and one female rabbit.
• Rabbits do not die.
𝟏 𝒏≤𝟐
𝑭𝒊𝒃 𝒏 = ቊ
𝑭𝒊𝒃 𝒏 − 𝟏 + 𝑭𝒊𝒃 𝒏 − 𝟐 𝒏>𝟐
Data Structures and Algorithms 5 FIT – HNUE 2021
FIBONACI IN NATURE
Data Structures and Algorithms 6 FIT – HNUE 2021
RECURSIVE SOLUTIONS AND FUNCTIONS
Data Structures and Algorithms 7 FIT – HNUE 2020
RECURSIVE SOLUTION
• Recursion is a process for solving problems by subdividing a larger
problem into smaller cases of the problem itself and then solving the
smaller, more trivial parts.
• All recursive solutions must satisfy three rules or properties:
• A recursive solution must contain a base case.
• A recursive solution must contain a recursive case.
• A recursive solution must make progress toward the base case.
Data Structures and Algorithms 8 FIT – HNUE 2021
RECURSIVE FUNCTIONS
• Recursion solves such recursive
problems by using functions that call
themselves from within their own code.
• Most computer programming languages
support recursion by allowing a function
to call itself from within its own code.
def printRev(n):
if n > 0:
print(n)
printRev(n-1)
Data Structures and Algorithms 9 FIT – HNUE 2021
RECURSIVE FUNCTIONS
def printRev(n):
if n > 0:
printRev(n-1)
print(n)
Data Structures and Algorithms 10 FIT – HNUE 2021
FORMAT OF A RECURSIVE FUNCTION
Framework of recursive function
1 def RecAlg(input)
2 if (Input is the smallest) // the base case
3 return some base case value
4 else // the recursive case
5 return (some work and then a recursive call)
Data Structures and Algorithms 11 FIT – HNUE 2021
FACTORIAL
• The factorial function:
1 𝑛=0
𝑛! = ቊ
𝑛∗ 𝑛−1 ! 𝑛 >0
• The recursive function:
1 def fact(n):
2 assert n >= 0
3 if n < 1:
4 return 1
5 else:
6 return n * fact(n - 1)
Data Structures and Algorithms 12 FIT – HNUE 2021
RECURSIVE CALL TREES
# A sample program containing three functions
1 def main():
2 y = foo(3)
3 bar(2)
4
5 def foo(x):
6 if x % 2 != 0:
7 return 0
8 else:
9 return x + foo(x-1)
10
11 def bar(n):
12 if n > 0 :
13 print(n)
14 bar(n-1)
Data Structures and Algorithms 13 FIT – HNUE 2021
FIBONACI
• The nth Fibonacci number can be computed by the recurrence relation
(for 𝑛 ≥ 0):
𝑓𝑖𝑏 𝑛 − 1 + 𝑓𝑖𝑏(𝑛 − 2) 𝑛>1
𝑓𝑖𝑏 𝑛 = ቊ
𝑛 𝑛 = 1, 𝑛 = 0
• The recursive function:
#The fib() recursive function
1 def fib(n):
2 if n <= 1:
3 return 1
4 else:
5 return fib(n - 1) + fib(n - 2)
Data Structures and Algorithms 14 FIT – HNUE 2021
RECURSIVE CALL TREE FOR FIB(6)
Data Structures and Algorithms 15 FIT – HNUE 2021
DESIGN A RECURSIVE ALGORITHM
• All recursive solutions must satisfy three rules or properties:
• A recursive solution must contain a base case.
• A recursive solution must contain a recursive case.
• A recursive solution must make progress toward the base case.
• To design a recursive algorithm, need answer these questions:
• Is it possible to define the problem as a problem of the same type, but
"smaller", and how is it defined?
• How is the size of the problem reduced at each recursive call?
• What special case of the problem would be considered a degenerate case?
Data Structures and Algorithms 16 FIT – HNUE 2021
𝑛
𝑆 𝑛 = 𝑎𝑘
𝑘=1
Data Structures and Algorithms 17 FIT – HNUE 2021
THE ARRAY IS IN SORTED ORDER
• Problem: Given an array, check whether the array is in sorted order
with recursion.
Data Structures and Algorithms 18 FIT – HNUE 2021
TOWERS OF HANOI
• The Towers of Hanoi puzzle, invented by the French mathematician Edouard Lucas in
1883.
• Consists of a board with three vertical poles and a stack of disks.
• The diameter of the disks increases as we progress from the top to bottom, creating a
tower structure.
• The objective is to move all of the disks from the starting pole to one of the other two poles
to create a new tower. There are, however, two restrictions: (1) only one disk can be
moved at a time, and (2) a larger disk can never be placed on top of a smaller disk.
Data Structures and Algorithms 19 FIT – HNUE 2021
TOWERS OF HANOI – 1 DISK
• Very easy! 1 move.
Data Structures and Algorithms 20 FIT – HNUE 2021
TOWERS OF HANOI – 2 DISKS
• How to move 1, Move disk 1 from A to Move disk 1 from B to
2 from A to C ??? B, 2 from A to C C
Next step?? Complete!
Data Structures and Algorithms 21 FIT – HNUE 2021
TOWERS OF HANOI – 3 DISKS
• Idea:
• Move disk 1,2 from A to B using C (recursion)
• Move disk 3 from A to C
• Move disk 1,2 from B to C using A (recursion)
• 4 disks???
Data Structures and Algorithms 22 FIT – HNUE 2021
TOWERS OF HANOI – N DISKS
• If n = 1: Move 1 disk from A to C // base case
• Else: // recursion
• Move the top n-1 disks from pole A to pole B using pole C.
• Move the remaining disk from pole A to pole C.
• Move the n-1 disks from pole B to pole C using pole A.
Recursive solution for the Towers of Hanoi puzzle.
1 def move(n, src, dest, temp):
2 if n >= 1:
3 move(n - 1, src, temp, dest)
4 print( "Move %d -> %d" % (src, dest))
5 move(n - 1, temp, dest, src)
Data Structures and Algorithms 23 FIT – HNUE 2021
MOVE( 3, 1, 3, 2 )
• Move 1 -> 3
• Move 1 -> 2
The first four • Move 3 -> 2
moves in solving • Move 1 -> 3
the Towers of • Move 2 -> 1
Hanoi puzzle with
three disks. • Move 2 -> 3
• Move 1 -> 3
• …………….
Data Structures and Algorithms 24 FIT – HNUE 2021
COMPEXITY OF RECURSIVE ALGORITHM
• Recurrence Tree Method:
• Draw a recurrence tree and calculate the time taken by every level of tree.
• Sum the work done at all levels
Data Structures and Algorithms 25 FIT – HNUE 2021
COMPLEXITY OF RECURSIVE ALGORITHM
• Two steps method:
• Identify 𝑇(𝑛) – running time
• Solving 𝑇(𝑛) – solving recurrence
• Example:
• Factorial:
𝑐 𝑛=0
𝑇 𝑛 =ቊ
𝑇 𝑛−1 +𝑐 𝑛>0
• Move – Hanoi tower:
𝑐 𝑛=1
𝑇 𝑛 =ቊ
2∗𝑇 𝑛−1 +𝑐 𝑛>1
Data Structures and Algorithms 26 FIT – HNUE 2021
SOLVING 𝑇(𝑛)
• Iteration Method
• Master method
• Intelligent guesswork (Substitution Method)
Data Structures and Algorithms 27 FIT – HNUE 2021
INTERATION METHOD
• Iterating the recurrence until the initial condition is reached.
• Example: T(n) of factorial
𝑇 𝑛 =𝑇 𝑛−1 +1
=𝑇 𝑛−2 +2
=𝑇 𝑛−3 +3
= ⋯……………
=𝑇 𝑛−𝑛 +𝑛
= 𝑛 + 1 = 𝑂(𝑛)
• Tower Hanoi: T(n) = ???
• T(n) = 3T(n/4) + n , T(1) = (1).
Data Structures and Algorithms 28 FIT – HNUE 2021
MASTER METHOD
• The reducer master theorem
Suppose constants 𝒂 ≥ 𝟏, 𝒃 > 𝟏. Let T(n) be a recursive formula
𝑻(𝒏) = 𝒂 𝑻(𝒏/𝒃) + 𝒄 𝒏𝒌 𝒏≥𝟎
Then we have three cases:
1. If 𝒂 > 𝒃𝒌 then 𝑻(𝒏) = (𝒏𝒍𝒐𝒈𝒃 𝒂 )
2. If 𝒂 = 𝒃𝒌 then 𝑻(𝒏) = ( 𝒏𝒌 𝒍𝒐𝒈 𝒏)
3. If 𝒂 < 𝒃𝒌 then 𝑻(𝒏) = ( 𝒏𝒌 )
• Example
Data Structures and Algorithms 29 FIT – HNUE 2021
INTELLEGENT GUESSWORK (SUBSTITUTION METHOD)
• The substitution method for solving recurrences is famously described
using two steps:
• Guess the form of the solution.
• Use induction to show that the guess is valid.
• Example:
Data Structures and Algorithms 30 FIT – HNUE 2021
EXAMPLE 1
• Give a recursion: 𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑛
• Solving:
• Guess: 𝑇 𝑛 = 𝑂 𝑛2 𝑇(𝑛) ≤ 𝑐𝑛2
• Induction:
• Assume 𝑇 𝑘 ≤ 𝑐𝑘 2 ∀𝑘 < 𝑛
• Prove: 𝑇 𝑛 ≤ 𝑐𝑛2
𝑇 𝑛 =𝑇 𝑛−1 +𝑛
≤𝑐 𝑛−1 2+𝑛
= 𝑐𝑛2 − 2𝑐𝑛 + 𝑐 + 𝑛
= 𝑐𝑛2 + 𝑛 1 − 2𝑐 + 𝑐
≤ 𝑐𝑛2 (𝑐 + 1/2)
Data Structures and Algorithms 31 FIT – HNUE 2021
EXAMPLE 2
• Given a recursion: 𝑇(𝑛) = 3𝑇( 𝑛/3 ) + 𝑛
• Guess: T(n) = O(n log n)
• Induction:
• Assume 𝑇 𝑘 ≤ 𝑐𝑘 log 𝑘 𝑘<𝑛 .
• Prove 𝑇 𝑛 ≤ 𝑐𝑛 log 𝑛
Data Structures and Algorithms 32 FIT – HNUE 2021
EXAMPLE 2
• T(n) = 3T(n/3) + n
3c n/3 log n/3 + n
c n log (n/3) + n
= c n log n - c n log3 + n
= c n log n - n (c log 3 - 1)
c n log n
Data Structures and Algorithms 33 FIT – HNUE 2021
HOW TO GUESS?
• Based on known similar results
• T(n) = 3T(n/3 + 5) + n similar T(n) = 3T(n/3) + n
• Using upper bound and lower bound
• If T(n) = (n) & T(n) = O(n2) => Guess T(n) = (n log n)
• Fibonaci??
Data Structures and Algorithms 34 FIT – HNUE 2021
IMPLEMENT A "MEMORY" FOR RECURSIVE VERSION
• Why Fibonacci 𝑇 𝑛 = 𝑂 2𝑛 ? Speedup??
• Fib(6): call 1 time
• Fib(5): call 1 time
• Fib(4): call 2 times
• Fib(3): call 3 times
• Fib(2): call 5 times
• Fib(1): call 3 times
We can implement a "memory" for our recursive version by using a
dictionary to save the previously calculated values.
Data Structures and Algorithms 35 FIT – HNUE 2021
IMPLEMENT A "MEMORY" FOR RECURSIVE VERSION
• Fibonaci:
• Using F[k] to save the calculated values;
• At the beginning, 𝐹[0] = 0; 𝐹[1] = 1; 𝐹[𝑘] = 0 𝑘 > 1
• Each recursive step:
• If F[k] > 0 then don’t need calculate F[k]
• else calculate F[k-1]+F[k-2] and save F[k]
#The fib() recursive function with memory
1 def fib(n):
2 if F[n] > 0:
3 return F[n]
4 else:
5 return (F[n] = fib(n - 1) + fib(n - 2))
Data Structures and Algorithms 36 FIT – HNUE 2021
BACKTRACKING ALGORITHM
Data Structures and Algorithms 37 FIT – HNUE 2020
CONCEPT
• Backtracking is a form of recursion.
• The usual scenario is that you ore face with a number or options, and
you must choose one or these.
• After you make your choice you will get a new set of options; just what
set of options you gel depends on what choice you made. This
procedure is repeated over and over until you reach a final state.
Data Structures and Algorithms 38 FIT – HNUE 2021
IDEA OF BACKTRACKING
Data Structures and Algorithms 39 FIT – HNUE 2021
THE EIGHT-QUEENS PROBLEM
• For the eight-queens problem, we use
a standard chessboard and eight
queens.
• The objective is to place the eight
queens onto the chessboard in such a
way that
• The queen can move and attack any
playing piece of the opponent by
moving any number of spaces
horizontally, vertically, or
diagonallyeach queen is safe from
attack by the other queens.
• 92 solutions
Data Structures and Algorithms 40 FIT – HNUE 2021
SOLVING FOR FOUR-QUEENS
Data Structures and Algorithms 41 FIT – HNUE 2021
EXPONENTIAL OPERATION (𝑥 𝑛 )
• 28 = 2 ∗ 2 ∗ 2 ∗ 2 ∗ 2 ∗ 2 ∗ 2 = 2 ∗ 2 ∗ 2 ∗ 2 2 = ((2 ∗ 2)2 )2
• a recursive definition:
1 𝑛=0
𝑛 (𝑥 ∗ 𝑥) 𝑛/2 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛
𝑥 =൞
𝑥 ∗ (𝑥 ∗ 𝑥)𝑛/2 𝑛 𝑖𝑠 𝑜𝑑𝑑
• Recursive algorithm:
• Base case?
• Recursive case?
Data Structures and Algorithms 42 FIT – HNUE 2021
EXPONENTIAL OPERATION (𝑥 𝑛 )
The recursive implementation of exp().
1 def exp(x,n):
2 if n == 0:
3 return 1
4 result = exp(x * x, n // 2)
5 if n % 2 == 0:
6 return result
7 else:
8 return x * result
Data Structures and Algorithms 43 FIT – HNUE 2021