Recursion Basics
Recursion Basics
Lecture 21
Recursion
FIT 1008
Introduction to Computer Science
COMMONWEALTH OF AUSTRALIA
Copyright Regulations 1969
WARNING
This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act).The material in this communication may be subject to copyright under the Act.Any further
reproduction or communication of this material by you may be the subject of copyright protection under the Act.
Do not remove this notice.
Objectives for this lecture
def solve(problem):
if problem is simple:
Base cases
Solve problem directly
else:
Decompose problem into subproblems p1, p2,…
solve(p1)
solve(p2) Recursive calls
solve(p3)…
Combine the subsolutions to solve problem
•n! = (n-1)!*n
Convergence
Combination
Key idea for complexity of recursion:
• How many recursive calls do we make in each version?
• How much work do we do per call?
Terminology
• Arity:
• Unary: a single recursive call (all previous code)
• Binary: two recursive calls (recursive sorts, later…)
• n-ary: n recursive calls
• Tail-recursion:
• Where the result of the recursive call is the result of the function.
Nothing is done in the “way back”. Closest to iteration - can be
transformed without “storing”. Useful for compiler optimisation.
Tail recursive version of factorial
n=0 0
n=1 1
n=2 1
n=3 2
n=4 3
n=5 5
Fib(5)
Fibonacci
First. Second.
Fib(5) Complexity: O(2n)
5
Fib(4)
Fib(3) 3
2
Fib(0) Fib(1)
0 1 Fib(1) Fib(2)
1 1
Fib(0) Fib(1)
0 1
Fib(0) Fib(1)
0 1
Fib(5)
5 Complexity: O(2n)
Fib(4)
Fib(3) 3
2
Fib(0) Fib(1)
0 1 Fib(1) Fib(2)
1 1
Fib(0) Fib(1)
0 1
fib(7)
Observations
fib_aux(7, 0, 1)
fib_aux(6, 1, 1) • n decreases each call
• before_last = last
fib_aux(5, 1, 2)
• last = before_last +last
fib_aux(4, 2, 3)
• base case n =0
fib_aux(3, 3, 5)
• return before_last
fib_aux(2, 5, 8)
fib_aux(1, 8, 13)
fib_aux(0, 13, 21)
0 1 2 3 4 5 6 7 8
0 1 1 2 3 5 8 13 21
fib(5)
fib_aux(5, 0, 1)
fib_aux(4, 1, 0+1)
fib_aux(3, 1, 1+1)
fib_aux(2, 2, 2+1)
fib_aux(1, 3, 3+2)
fib_aux(0, 5, 5+3)
return 5
Complexity: O(n)
What happens with
Recursion at the Low Level?
MIPS Recursion:
a function calling a function
result
saved $fp
call 2
saved $ra
arg 1 (p)
result
saved $fp
call 1
saved $ra
arg 1 (p)
n
• Tail-recursion:
• Where the result of the recursive call is the result of the
function. Nothing is done in the “way back”. Closest to
iteration - can be transformed without “storing”. Useful for
compiler optimisation.