recursion a function is said to be recursive if it calls itself, either directly or indirectly. void...

Post on 18-Jan-2018

225 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Recursion contd. When writing a recursive function, there are two rules that need to be adhered to. – Every recursive function must have a base case, where the result is known – When a function invokes itself, the value is defined in terms of previously defined function values, and the value used for testing for the base case must change.

TRANSCRIPT

Recursion

A function is said to be recursive if it calls itself, either directly or indirectly.

void repeat( int n ) {cout << n << " ";repeat( --n );

}

Recursion contd.

Simple recursive routines follow a standard pattern. Typically there is a base case ( or cases ) that is tested for, upon entry to the function. Then there is a general recursive case in which one of the variables, often an integer, is passed as an argument in such a way as to ultimately lead to the base case.

Recursion contd.

When writing a recursive function, there are two rules that need to be adhered to.

– Every recursive function must have a base case, where the result is known

– When a function invokes itself, the value is defined in terms of previously defined function values, and the value used for testing for the base case must change.

Recursion contd.

void repeat( int n ) {if( n<0 ) {

cout << "END" << endl;return;

}else {

cout << n << " ";repeat( --n );

}}

Recursion Example

int sum( int n ){

if (n <= 1 )return n; // base caseelsereturn (n + sum( n -1 )); // current value is defined in terms of // a different value of the parameter

}sum(1) 1sum(2) 2 + sum(1) 2 + 1 sum(3) 3 + sum(2) 3 + 2 + 1

Factorial Example

Let’s find the factorial of n where n is a non-negative integer.– Product of integers from 1 to n– If n = 0, then n! is 1

0! = 1 n! = n(n-1)… 3.2.1 for n > 0

int factorial( int n ){

if (n <= 1 )return 1;elsereturn ( n * factorial(n-1 ) );

}

What is the Big-Oh?

Iterative Version of the Factorial Example

int factorial( int n ){

int value = 1;for ( int i=2, i<=n, i++ )

value *= i;return value;

}What is the Big-Oh for this?

Pros and Cons of Recursion

Disadvantages:– Slow – time is taken for function call– Uses more memory – requires space for stack

frame

Advantages:– When it is a simple, elegant solution that is easy to

understand– When the number of recursive calls is a fraction of n

When to Use Recursion

1. Iterative functions are typically faster than their recursive counterparts. So, if speed is an issue, you would normally use iteration.

2. If the stack limit is too constraining then you will prefer iteration over recursion.

3. Some procedures are very naturally programmed recursively, and all but unmanageable iteratively. Here, then, the choice is clear.

Example of When to Use Recursion

Algorithm Power( x, n ) Input: base x, exponent n where n 0 Output: value of xn if n = 0 then return 1if n is odd then y Power(x,(n-1)/2) return x * y * yelse y Power(x,n/2) return y * y

1 if n = 0power(x,n) x * power(x,(n-1)/2)2 if n>0 is odd

power(x,n/2)2 if n>0 is even

top related