Recursion and Recurrence Relation - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Recursion and Recurrence Relation

Description:

A procedure defined in terms of (simpler versions of) itself. Components: Base case ... 30. 8/16/09. Nautilus sea shell. ???????????????????????. 31. 8/16/09. Plants ... – PowerPoint PPT presentation

Number of Views:423
Avg rating:3.0/5.0
Slides: 32
Provided by: pradondet
Category:

less

Transcript and Presenter's Notes

Title: Recursion and Recurrence Relation


1
Recursion and Recurrence Relation
  • Pradondet Nilagupta
  • (pom_at_ku.ac.th)
  • Department of Computer Engineering
  • Kasetsart University

2
What is Recursion?
  • A procedure defined in terms of (simpler versions
    of) itself
  • Components
  • Base case
  • Recursive definition
  • Convergence to base case

3
Unary 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

4
Example Factorial
  • Given n ? 0
  • n! n ? (n - 1) ? (n - 2) ? ... ? 2 ? 1
  • 0! 1
  • Example
  • 5! 5 ? 4 ? 3 ? 2 ? 1 120

5
Example 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

6
Example Factorial
  • Recursion
  • n! n ? (n - 1) ? (n - 2) ? ... ? 2 ? 1
  • If n gt 1
  • Factorial(n) n ? Factorial(n - 1)

7
Example Factorial
  • Convergence

Factorial(4)
8
Example Factorial
  • The Factorial function can be defined recursively
    as follows
  • Factorial(0) 1
  • Factorial(1) 1
  • Factorial(n) n ? Factorial(n - 1)

9
Example 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
10
Example 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)

11
Example 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
12
Example 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

13
N-ary Recursion
  • Sometimes a function can only be defined in terms
    of two or more calls to itself.
  • Efficiency is often a problem.

14
Example 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,...)

15
Fibonacci 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!!!!

16
How many pairs will there be in one year?
17
Family Tree
18
The 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

19
Family Tree of Rabbits
  • Source http//www.mcs.surrey.ac.uk/Personal/R.Knot
    t/Fibonacci/fibnat.html

20
Algorithm 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

21
Example 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)


22
Algorithm 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

23
Algorithm 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

24
Algorithm 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

25
Algorithm 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

26
Fibonacci 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...

27
Ratios on a Graph
28
Fibonacci Rectangles and Shell Spirals
29
Fibonaccci number and Nature
30
Nautilus sea shell
31
Plants
Write a Comment
User Comments (0)
About PowerShow.com