Docsity

Assignment and Recursion-Data Structures and Representation-Assigment, Exercises of Data Structures and Algorithms

Toggle sidebar
bg1
Recursion
Recursion may have the following definitions:
-The nested repetition of identical algorithm is recursion.
-It is a technique of defining an object/process by itself.
-Recursion is a process by which a function calls itself repeatedly until some specified
condition has been satisfied.
When to use recursion
Recursion can be used for repetitive computations in which each action is stated in
terms of previous result. There are two conditions that must be satisfied by any
recursive procedure.
1. Each time a function calls itself it should get nearer to the solution.
2. There must be a decision criterion for stopping the process.
In making the decision about whether to write an algorithm in recursive or non-
recursive form, it is always advisable to consider a tree structure for the problem. If
the structure is simple then use non-recursive form. If the tree appears quite bushy,
with little duplication of tasks, then recursion is suitable.
The recursion algorithm for finding the factorial of a number i s given below,
Algorithm : factorial-recursion
Input : n, the number whose factorial is to be found.
Output : f, the factorial of n
Method : if(n=0)
f=1
else
f=factorial(n-1) * n
if end
algorithm ends.
The general procedure for any recursive algorithm is as follows,
1. Save the parameters, local variables and return addresses.
2. If the termination criterion is reached perform final computation and goto
step 3 otherwise perform final computations and goto step 1
3.
4. Restore the most recently saved parameters, local variable and return
address and goto the latest return address.
docsity.com
pf3

Partial preview of the text

Download Assignment and Recursion-Data Structures and Representation-Assigment and more Exercises Data Structures and Algorithms in PDF only on Docsity!

Recursion

Recursion may have the following definitions: -The nested repetition of identical algorithm is recursion. -It is a technique of defining an object/process by itself. -Recursion is a process by which a function calls itself repeatedly until some specified condition has been satisfied. When to use recursion Recursion can be used for repetitive computations in which each action is stated in terms of previous result. There are two conditions that must be satisfied by any recursive procedure.

  1. Each time a function calls itself it should get nearer to the solution.
  2. There must be a decision criterion for stopping the process.

In making the decision about whether to write an algorithm in recursive or non- recursive form, it is always advisable to consider a tree structure for the problem. If the structure is simple then use non-recursive form. If the tree appears quite bushy, with little duplication of tasks, then recursion is suitable. The recursion algorithm for finding the factorial of a number is given below, Algorithm : factorial-recursion Input : n, the number whose factorial is to be found. Output : f, the factorial of n Method : if(n=0) f= else f=factorial(n-1) * n if end algorithm ends. The general procedure for any recursive algorithm is as follows,

  1. Save the parameters, local variables and return addresses.
  2. If the termination criterion is reached perform final computation and goto step 3 otherwise perform final computations and goto step 1
  1. Restore the most recently saved parameters, local variable and return address and goto the latest return address.

2.4.2 Iteration v/s Recursion Demerits of recursive algorithms

  1. Many programming languages do not support recursion, hence recursive mathematical function is implemented using iterative methods.
  2. Even though mathematical functions can be easily implemented using recursion it is always at the cost of execution time and memory space. For example, the recursion tree for generating 6 numbers in a fibonacci series generation is given in fig 2.5. A fibonacci series is of the form 0,1,1,2,3,5,8,13,…etc, where the third number is the sum of preceding two numbers and so on. It can be noticed from the fig 2.5 that, f(n-2) is computed twice, f(n-3) is computed thrice, f(n-4) is computed 5 times.
  3. A recursive procedure can be called from within or outside itself and to ensure its proper functioning it has to save in some order the return addresses so that, a return to the proper location will result when the return to a calling statement is made.
  4. The recursive programs needs considerably more storage and will take more time.

Demerits of iterative methods:

  1. Mathematical functions such as factorial and fibonacci series generation can be easily implemented using recursion than iteration.
  2. In iterative techniques looping of statement is very much necessary.

Recursion is a top down approach to problem solving. It divides the problem into pieces or selects out one key step, postponing the rest. Iteration is more of a bottom up approach. It begins with what is known and from this constructs the solution step by step. The iterative function obviously uses time that is O(n) where as recursive function has an exponential time complexity. It is always true that recursion can be replaced by iteration and stacks. It is also true that stack can be replaced by a recursive program with no stack.

Every recursive solution involves two major parts or cases, the

second part having three components.

 base case(s) , in which the problem is simple enough to be solved

directly, and

 recursive case(s). A recursive case has three components:

1. divide the problem into one or more simpler or smaller parts of the

problem,

2. call the function (recursively) on each part, and

3. combine the solutions of the parts into a solution for the problem.

A function that calls itself is an important feature of C and such functions are called

recursive functions. The term “recursion” was derived from the Latin word

recursus, which means, “to run back”. “Recursive” thinking may be tough for beginners. In this chapter, I have presented some interesting recursive programs. Few programs are my original work, others are improved version of existing recursive programs.