12 Recursion
12 Recursion
1. 2.
Recursion is natural
Many natural phenomena are recursion
A smaller part of oneself is embedded in itself
(a)
Tree
(b)
Sometimes,
B
C
A
A
B A
C B
D
D
B
Two persons
C C
Four persons
D
3
Three persons
We could write: 6! = 6 * 5!
Is this correct? Well almost The factorial function is only defined for positive integers. So we should be a bit more precise:
n! = 1 (if n is equal to 1) n! = n * (n-1)! (if n is larger than 1)
Factorial function
The Java equivalent of this definition
a method calling itself
int fac(int number) { if (number <= 1) return 1; else return number * fac(number-1); }
10
Factorial function
Assume the number n is 3, that is, number=3.
fac(3) : 3 <= 1 ? No fac(3) = 3 * fac(2) fac(2) : 2 <= 1 ? fac(2) = 2 * fac(1)
No
fac(1) : 1 <= 1 ? Yes return 11 fac(2) = 2 * 11= 2 return 2 fac(3) = 3 * 2 = 6 return 6 fac(3) has the value 6
11
Factorial function
For certain problems (such as the factorial function), a recursive solution often leads to short and elegant code. Compare the recursive solution with the iterative solution:
Recursive solution
Iterative solution
int fac(int number){ int fac(int number){ int product=1; if (number<=1) while (number>1){ return 1; product = product*number; else number--; return number*fac(number-1); } }
return product; }
12
Recursion
However, we have to pay a price for recursion:
calling a function consumes more time and memory than adjusting a loop counter high performance applications (graphic action games, simulations of nuclear explosions) hardly ever use recursion
In less demanding applications recursion is an attractive alternative for iteration (for the right problems!)
13
Oops!
Oops!
14
Recursion
We must always make sure that the recursion:
A recursive function must contain at least one non-recursive branch The recursive calls must eventually lead to a non-recursive branch
16
Recursion
Recursion is one way to decompose a task into smaller subtasks At least one of the subtasks is a simpler example of the same task
The smallest example of the same task has a non-recursive solution
How many pairs of rabbits can be produced from a single pair in a year's time?
Assumptions:
Each new pair of rabbits becomes fertile at the age of one month; Each pair of fertile rabbits produces a new pair of offspring every month; None of the rabbits dies in that year.
Example:
Initially, God creates a pair of rabbits; After 1 month, the pair of rabbits become fertile; After 2 months, there will be 2 pairs of rabbits; After 3 months, there will be 3 pairs; After 4 months, there will be 5 pairs (since the following month the original pair and the pair born during the first month will both produce a new pair and there will be 5 in all).
18
19
where each number is the sum of the preceding two Recursive definition:
F(0) = 0 (Fibonacci number at 0th position)
F(1) = 1
Pairs
creation 0 1 1 2 3 5
0 1
2
3 4 5
The program asks for an integer, then prints the Fibonacci number at that position
21
Copyright 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc.
F(2)
F(1) F(0)
F(1)
F(1)
F(0)
23
Summary
How to write recursively?
Begin with non-recursive branches, then recursive ones int recursive_fn(parameters){ //the non-recursive branch(s) if (stopping condition) return stopping value; //other stopping conditions if needed //the recursive branch(s) (calling itself) return function of recursive_fn(revised parameters) }
24
Summary
We have briefly introduced three examples (both can be solved by using either a loop or recursion) Sometimes, a problem is more intuitive to solve using recursion
25