# recursion

Document Sample

```					Task: 1 What is recursive algorithm? Explain analysis efficiency of recursive algorithm? Give at
least 2 examples?

Ans: A recursive algorithm is an algorithm which calls itself with "smaller" input values, and
which obtains the result for the current input by applying simple operations to the returned
value for the smaller input. More generally if a problem can be solved utilizing solutions to
smaller versions of the same problem, and the smaller versions reduce to easily solvable cases,
then one can use a recursive algorithm to solve that problem.

For example, the elements of a recursively defined set, or the value of a recursively defined
function can be obtained by a recursive algorithm.
Analysis efficiency of recursive algorithm
Steps in mathematical analysis of recursive algorithms:

 Decide on parameter n indicating input size.
 Identify algorithm’s basic operation
 Determine worst, average, and best case for input of size n
If the basic operation count also depends on other conditions.
 Set up a recurrence relation and initial condition(s) for C(n)-the number of times the
basic operation will be executed for an input of size n
Alternatively count recursive calls.
 Solve the recurrence to obtain a closed form or estimate the order of magnitude of the
solution

Examples of recursive algorithm:
1. Factorial function :-

Definition of factorial function F(n)= n!:
" n ! = 1 • 2 • … • (n-1) • n
" 0! = 1

Algorithm F(n)
//Compute n! recursively
//Input: A nonnegative integer n
//Output: The value of n!
if n = 0 return 1
else return F(n-1) * n
2. Tower of Hanoi :-
Three steps involved are:-

 First move recursively n - 1 disks from peg 1 to peg 2 (with peg 3 as auxiliary)
 Move the largest disk directly from peg 1 to peg 3
 Move recursively n - 1 disks from peg 2 and peg 3 (using peg 1 as auxiliary)
Task 2: Explain Program Design and Analysis Process ?

 Understand completely the problem: Do some examples by hand, think about special
cases, and ask questions if needed. May be you can use a known algorithm for solving it.
 Ascertaining the capabilities of a computational device: Sequential (serial) algorithms
versus parallel algorithms.
 Choosing between exact and approximate problem solving: Exactly unsolvable
problem or slow exact algorithm.
 Deciding on appropriate data structures: Structuring or restructuring data specifying a
problem’s instance is important.
 Algorithm design techniques: Select a general approach to solving problem
algorithmically.
 Methods of specifying an algorithm: Pseudocode is a mixture of a natural language and
a programming language-like constructs.
 Providing an algorithm’s correctness: Use mathematic induction considering selected
inputs.
 Analyzing an algorithm: Time and space efficiencies; and simplicity and generality.
 Coding an algorithm: Implement it as computer program with test and debugging for
its validation.

```
DOCUMENT INFO
Categories:
Tags:
Stats:
 views: 5 posted: 3/1/2012 language: pages: 3