Title: Recursion and Recurrence Relation
1Recursion and Recurrence Relation
- Pradondet Nilagupta
- (pom_at_ku.ac.th)
- Department of Computer Engineering
- Kasetsart University
2What is Recursion?
- A procedure defined in terms of (simpler versions
of) itself - Components
- Base case
- Recursive definition
- Convergence to base case
3Unary Recursion
- Functions calls itself once (at most)
- Usual format
- function RecursiveFunction ( ltparameter(s)gt )
-
- if ( ltbase casegt ) then
- return ltbase valuegt
- else
- return RecursiveFunction ( ltexpressiongt )
-
- Winding and unwinding the stack frames
4Example Factorial
- Given n ? 0
- n! n ? (n - 1) ? (n - 2) ? ... ? 2 ? 1
- 0! 1
- Example
- 5! 5 ? 4 ? 3 ? 2 ? 1 120
5Example Factorial
- Problem Write a recursive function Factorial(n)
which computes the value of n! - Base Case
- If n 0 or n 1
- Factorial(n) 1
6Example Factorial
- Recursion
- n! n ? (n - 1) ? (n - 2) ? ... ? 2 ? 1
- If n gt 1
- Factorial(n) n ? Factorial(n - 1)
7Example Factorial
Factorial(4)
8Example Factorial
- The Factorial function can be defined recursively
as follows - Factorial(0) 1
- Factorial(1) 1
- Factorial(n) n ? Factorial(n - 1)
9Example Factorial
Base case
function Factorial ( n ) if ( n is less
than or equal to 1 ) then return 1
else return n ? Factorial ( n - 1 )
General Case
Convergence
10Example factorl.c
- Computes the factorial of a number
- function Factorial ( n )
-
- if ( n is less than or equal to 1)
- then
- return 1
- else
- return n ? Factorial ( n - 1 )
-
- / Compute the factorial of n /
- int factorial ( int n )
-
- if ( n lt 1 )
-
- return 1
-
- else
-
- return n factorial(n-1)
-
-
11Example Frames during calculation of
factorial(4)
printf(d, factorial(4))
int factorial ( int n ) if ( n lt 1 )
return 1 else return n
factorial( n - 1 )
4
4
4
12Example testprog.c
- include ltstdio.hgt
- include factorl.c
- / Main program for testing factorial() function
/ - int main(void)
-
- int n
- printf("Please enter n ")
- scanf("d", n)
- printf("d! is d\n", n, factorial(n))
- return 0
13N-ary Recursion
- Sometimes a function can only be defined in terms
of two or more calls to itself. - Efficiency is often a problem.
14Example Fibonacci
- A series of numbers which
- begins with 0 and 1
- every subsequent number is the sum of the
previous two numbers - 0, 1, 1, 2, 3, 5, 8, 13, 21,...
- Write a recursive function which computes the
n-th number in the series (n 0, 1, 2,...)
15Fibonacci Number
- Suppose a newly-born pair of rabbits, one male,
one female, are put in a field. - Rabbits are able to mate at the age of one month
so that at the end of its second month a female
can produce another pair of rabbits. - Suppose that our rabbits never die!!!!
16How many pairs will there be in one year?
17Family Tree
18The number of pairs of rabbits as a function of
time
- F(1) 1 -- we start with one pair
- F(2) 1 -- they're too young to have children
the first year - F(3) 2 -- in the second year, they have a pair
of children - F(4) 3 -- in the third year, they have another
pair - F(5) 5 -- we get the first set of grandchildren
19Family Tree of Rabbits
- Source http//www.mcs.surrey.ac.uk/Personal/R.Knot
t/Fibonacci/fibnat.html
20Algorithm 1
- int fib(int n)
-
- if (n lt 1)
- return n
- else
- return fib(n-1) fib(n-2)
-
- Time Complexity
- T(n) T(n-1) T(n-2) 2
21Example Computation of fib(4)
int fib ( int n ) if ( n lt 1 ) return
n else return fib( n - 2 ) fib( n -
1 )
2
2
2
2
fib(4)
fib(2)
fib(3)
22Algorithm 2 Dynamic Programming
- int fib(int n)
-
- int fn1
- f1 f2 1
- for (int i 3 i lt n i)
- fi fi-1 fi-2
- return fn
-
23Algorithm 3
- int fib(int n)
-
- int a 1, b 1
- for (int i 3 i lt n i)
-
- int c a b a b b c
-
- return b
-
24Algorithm 4
- int fib(int n)
-
- int M22 1,0,0,1
- for (int i 1 i lt n i)
- M M 1,1,1,0
- return M00
-
25Algorithm 5
- int M22 1,00,1
- int fib(int n)
-
- matpow(n-1)
- return M00
-
- void matpow(int n)
-
- if (n gt 1)
-
- matpow(n/2)
- M MM
-
- if (n is odd)
- M M1,11,0
-
26Fibonacci numbers and the Golden Number
- If we take the ratio of two successive numbers in
Fibonacci's series, (1, 1, 2, 3, 5, 8, 13, ..)
and we divide each by the number before it, we
will find the following series of numbers - 1/1 1, 2/1 2, 3/2 15, 5/3
1666..., 8/5 16, 13/8 1625, 21/13
161538...
27Ratios on a Graph
28Fibonacci Rectangles and Shell Spirals
29Fibonaccci number and Nature
30Nautilus sea shell
31Plants