RECURSIONS1F18BSCS0019

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

RECURSION:

The process in which a function calls itself directly or indirectly


is called recursion and the corresponding function is called as
recursive function. Using recursive algorithm, certain problems
can be solved quite easily.
Another definition of Recursion is the process of defining a
problem (or the solution to a problem) in terms of (a simpler
version of) itself.
For example, we can define the operation "find your way home"
as:
1. If you are at home, stop moving.
2. Take one step toward home.
3. "find your way home".
Here the solution to finding your way home is two steps (three
steps). First, we don't go home if we are already home.
Secondly, we do a very simple action that makes our situation
simpler to solve. Finally, we redo the entire algorithm.
Another example of recursion would be finding the maximum
value in a list of numbers. The maximum value in a list is either
the first number or the biggest of the remaining numbers.

Parts of a Recursive Algorithm:


All recursive algorithms must have the following:
1. Base Case (i.e., when to stop)
2. Work toward Base Case
3. Recursive Call (i.e., call ourselves)
The "work toward base case" is where we make the problem
simpler. The recursive call, is where we use the same algorithm
to solve a simpler version of the problem. The base case is the
solution to the "simplest" possible problem (For example, the
base case to adding a list of numbers would be if the list had
only one number... thus the answer is the number).
Recursive thinking is really important in programming. It helps
you break down bit problems into smaller ones. Often, the
recursive solution can be simpler to read than the iterative one.
Recursion is used all the time, in nearly field, in nearly every
language. :)
It is hard, and you won't get it right away, but it's good to know
something about. If you collaborate, the other programmers will
probably use it at some point and you'll to be able to read their
code (if nothing else).
But if you have a taste, you can probably move on and review
this stuff when you see it again. Computer science students
usually take a term worth of classes to really nail it.
Base Case:
Here you define a base case which for a numerical problem is
usually given - for example 1! = 1 or first two numbers in a
Fibonacci series are 0 and 1. Also, the way of getting a next
value is also given - say factorial(n) = n * factorial(n-1) or
fibonacci(n) = fibonacci(n-1) + fibonacci(n-2), similar function
definitions can be given for calculating exponential too. If you
notice, though the non base condition is simple - it has to do
something when it returns from the base case and this something
needs to be kept on a stack. This is one major reason that most
recursive functions blow up if the stack depth increases beyond
a certain limit.
1. function factorial(n) = {
2. if(n <= 1) return 1
3. else return n * factorial(n-1)
4. }

Terminating:
A recursive termination is a condition that, when met, will cause
the recursive function to stop calling itself. Because of
the termination condition, countDown(1) does not call
countDown(0) -- instead, the “if statement” does not execute, so
it prints “pop 1” and then terminates.
Stack case:
Recursive functions use something called the call stack.When a
program calls a function, that function goes on top of the
call stack. This similar to a stack of books. You add things one
at a time. Then, when you are ready to take something off, you
always take off the top item.

Recursive Tree:
In graph theory, a recursive tree (i.e., unordered tree) is a non-
planar labeled rooted tree. A size-n recursive tree is labeled by
distinct integers 1, 2, ..., n, where the labels are strictly
increasing starting at the root labeled 1. Recursive trees are non-
planar, which means that the children of a particular node are
not ordered. E.g. the following two size-three recursive trees are
the same.

Implementation:
Recursive functions are functions that call themselves. There are
two main parts to recursive functions:
1. general (recursive) case--the case for which the solution is
expressed in terms of a smaller version of itself. In other
words, here, the problem space is made smaller and
smaller. (the smaller problem space is sent to the calling
function)
2. base case--the case for which the solution can be stated
nonrecursively. Here, a solid solution is found.
Let's take an example of recursion using the factorial for a
positive integer.
n factorial can be represented by:
n!=n * (n-1) * (n-2) * ... * 1
For instance, 4! can be written as
4*3*2*1
By regrouping this, you can get: 4 * (3 * 2 * 1). Note that 3!=3 *
2 * 1, and by substitution, you can represent 4! by
4 * (3!)
You can generalize this reasoning to form the following
recursive definition of factorial:
n!=n * (n-1)!
where 0! is 1. Using this, you can evaluate 4! with the following
sequence of computations:
4!=4 * (3!)
  =4 * (3 * (2!))
  =4 * (3 * (2 * (1!)))
  =4 * (3 * (2 * (1 * (0!))))
  =4 * (3 * (2 * (1 * (1))))
The first four steps in this computation are recursive, with n!
being evaluated in terms of (n - 1)!. The final step (0!=1) is not
recursive. This demonstates the two main parts of recursive
functions:
1 if n = 0 (base case)
n! = {
n * (n - 1)!       if n > 0 (recursive step)
Notice that in the recursive step, you are dealing with a smaller
and smaller problem space by calculating (n-1)!
Notice that in the recursive step, you are dealing with a smaller
and smaller problem space by calculating (n-1)!
This factorial function can be represented by the following code:
long factorial (int n)
// Computes n! using recursion.
{
long result; // Result returned

if ( n == 0 )
result = 1;
else
result = n * factorial (n-1);
return result;
}
Let's look at the call factorial(4) . Because 4 is not equal to 0
(the base case), the factorial() function calls factorial(3). The
recursive calls continue until the base case is reached--that is,
until n equals 0.
factorial(4)
 ↓ RECURSIVE STEP
4 * factorial (3)
 ↓ RECURSIVE STEP
3 * factorial (2)
  ↓ RECURSIVE STEP
2 * factorial (1)
 ↓ RECURSIVE STEP
1 * factorial (0)
 ↓ BASE CASE
  1
The calls to factorial() are evaluated in reverse order. The
evaluation process continues until the value 24 is returned by the
call factorial (4).
factorial(4)
  ↑ RESULT 24
4 * factorial (3)
   ↑ RESULT 6
3 * factorial (2)
   ↑ RESULT 2
2 * factorial (1)
  ↑ RESULT 1
1 * factorial (0)
   ↑ RESULT 1
   1
The calls to factorial() are evaluated in reverse order.

You might also like