KEMBAR78
Lec-32 Recursion -Recursion in Computer Science | PPT
A mathematical look
 We are familiar with
f(x) = 3x+5
 How about
f(x) = 3x+5 if x > 10 or
f(x) = f(x+2) -3 otherwise
Calculate f(5)
f(x) = 3x+5 if x > 10 or
f(x) = f(x+2) -3 otherwise
 f(5) = f(7)-3
 f(7) = f(9)-3
 f(9) = f(11)-3
 f(11) = 3(11)+5
= 38
But we have not determined what f(5) is yet!
Calculate f(5)
f(x) = 3x+5 if x > 10 or
f(x) = f(x+2) -3 otherwise
 f(5) = f(7)-3 = 29
 f(7) = f(9)-3 = 32
 f(9) = f(11)-3 = 35
 f(11) = 3(11)+5
= 38
Working backwards we see that f(5)=29
Series of calls
f(5)
f(7)
f(9)
f(11)
Recursion occurs when a function/procedure calls itself.
Many algorithms can be best described in terms of recursion.
Example: Factorial function
The product of the positive integers from 1 to n inclusive is
called "n factorial", usually denoted by n!:
n! = 1 * 2 * 3 .... (n-2) * (n-1) * n
Recursion
Recursion
Recursive Definition
Recursive Definition
of the Factorial Function
n! =
1, if n = 0
n * (n-1)! if n > 0
5! = 5 * 4!
4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1!
1! = 1 * 0!
= 5 * 24 = 120
= 4 * 3! = 4 * 6 = 24
= 3 * 2! = 3 * 2 = 6
= 2 * 1! = 2 * 1 = 2
= 1 * 0! = 1
The Fibonacci numbers are a series of numbers as follows:
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
...
fib(n) =
1, n <= 2
fib(n-1) + fib(n-2), n > 2
Recursive Definition
Recursive Definition
of the Fibonacci Numbers
fib(3) = 1 + 1 = 2
fib(4) = 2 + 1 = 3
fib(5) = 2 + 3 = 5
int BadFactorial(n){
int x = BadFactorial(n-1);
if (n == 1)
return 1;
else
return n*x;
}
What is the value of BadFactorial(2)?
Recursive Definition
Recursive Definition
We must make sure that recursion eventually stops, otherwise
it runs forever:
Using Recursion Properly
Using Recursion Properly
For correct recursion we need two parts:
1. One (ore more) base cases that are not recursive, i.e. we
can directly give a solution:
if (n==1)
return 1;
2. One (or more) recursive cases that operate on smaller
problems that get closer to the base case(s)
return n * factorial(n-1);
The base case(s) should always be checked before the recursive
calls.

Lec-32 Recursion -Recursion in Computer Science

  • 1.
    A mathematical look We are familiar with f(x) = 3x+5  How about f(x) = 3x+5 if x > 10 or f(x) = f(x+2) -3 otherwise
  • 2.
    Calculate f(5) f(x) =3x+5 if x > 10 or f(x) = f(x+2) -3 otherwise  f(5) = f(7)-3  f(7) = f(9)-3  f(9) = f(11)-3  f(11) = 3(11)+5 = 38 But we have not determined what f(5) is yet!
  • 3.
    Calculate f(5) f(x) =3x+5 if x > 10 or f(x) = f(x+2) -3 otherwise  f(5) = f(7)-3 = 29  f(7) = f(9)-3 = 32  f(9) = f(11)-3 = 35  f(11) = 3(11)+5 = 38 Working backwards we see that f(5)=29
  • 4.
  • 5.
    Recursion occurs whena function/procedure calls itself. Many algorithms can be best described in terms of recursion. Example: Factorial function The product of the positive integers from 1 to n inclusive is called "n factorial", usually denoted by n!: n! = 1 * 2 * 3 .... (n-2) * (n-1) * n Recursion Recursion
  • 6.
    Recursive Definition Recursive Definition ofthe Factorial Function n! = 1, if n = 0 n * (n-1)! if n > 0 5! = 5 * 4! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0! = 5 * 24 = 120 = 4 * 3! = 4 * 6 = 24 = 3 * 2! = 3 * 2 = 6 = 2 * 1! = 2 * 1 = 2 = 1 * 0! = 1
  • 7.
    The Fibonacci numbersare a series of numbers as follows: fib(1) = 1 fib(2) = 1 fib(3) = 2 fib(4) = 3 fib(5) = 5 ... fib(n) = 1, n <= 2 fib(n-1) + fib(n-2), n > 2 Recursive Definition Recursive Definition of the Fibonacci Numbers fib(3) = 1 + 1 = 2 fib(4) = 2 + 1 = 3 fib(5) = 2 + 3 = 5
  • 8.
    int BadFactorial(n){ int x= BadFactorial(n-1); if (n == 1) return 1; else return n*x; } What is the value of BadFactorial(2)? Recursive Definition Recursive Definition We must make sure that recursion eventually stops, otherwise it runs forever:
  • 9.
    Using Recursion Properly UsingRecursion Properly For correct recursion we need two parts: 1. One (ore more) base cases that are not recursive, i.e. we can directly give a solution: if (n==1) return 1; 2. One (or more) recursive cases that operate on smaller problems that get closer to the base case(s) return n * factorial(n-1); The base case(s) should always be checked before the recursive calls.

Editor's Notes

  • #3 Visit: tshahab.blogspot.com