- 75 Views
- Uploaded on
- Presentation posted in: General

Cs212: DataStructures

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Cs212: DataStructures

Lecture 4: Recursion

- The Concept of Recursion
- Why recursion?
- Factorial – A case study
- Content of a Recursive Method
- Recursion vs. iteration

- repetition can be achieved by writing loops, such as for loops and while loops.
- Another way to achieve repetition is through recursion, which occurs when a function calls itself.
- A recursive function is a function that calls itself , either directly or indirectly ( through another function).
- For example :
void myFunction( int counter)

{if(counter == 0) return;else {myFunction(--counter); return;}

}

- Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first
- Recursion is a technique that solves a problem by solving a smaller problem of the same type
- Allows very simple programs for very complex problems

=

3*2*1

=

6

- Factorial definition: the factorial of a number is product of the integral values from 1 to the number.

factorial 3

=

product [4..1]

=

product [4,3,2,1]

=

4*3*2*1

=

24

- Iteration algorithm definition (non-recursive)

1 if n = 0

Factorial(n) =

n*(n-1)*(n-2)*…*3*2*1 if n > 0

factorial 4

=

3 * factorial 2

=

3 * (2 * factorial 1)

=

3 * (2 * (1 * factorial 0))

=

3 * (2 * (1 * 1))

=

3 * (2 * 1)

=

3 * 2

=

6

7

- Recursion algorithm definition

1 if n = 0

Factorial(n) =

n * ( Factorial(n-1) ) if n > 0

factorial 3

- Base case(s).
- One or more stopping conditions that can be directly evaluated for certain arguments
- there should be at least one base case.

- Recursive calls.
- One or more recursive steps, in which a current value of the method can be computed by repeated calling of the method with arguments (general case).
- The recursive procedure call must use a different argument that the original one: otherwise the procedure would always get into an infinite loop…

void myFunction( int counter){if( counter == 0)…........else

{………myFunction(--counter);

}}

Use an if-else statement to distinguish between a Base case and a recursive step

Recursive factorialAlgorithm

public static intrecursiveFactoria(intn) {

if (n == 0)

return 1; // base case

else

return n *recursiveFactorial(n-1): // recursive case

}

public static intrecursiveFactoria(int n) {

i = 1

factN= 1

for(int i=1; i<= n; i++)

factN= factN * i

i = i + 1

return factN

}

recursive implementation of the factorial function is somewhat simpler than the iterative version in this case there is no compelling reason for preferring recursion over iteration.

For some problems, however, a recursive implementation can be significantly simpler and easier to understand than an iterative implementation.

- Each recursive call is made with a new arguments Previous calls are suspended
- Each entry of the trace corresponds to a recursive call.
- Each new recursive function call is indicated by an arrow to the newly called function.
- When the function returns, an arrow showing this return is drawn and the return value may be indicated with this arrow.

- Iteration can be used in place of recursion
- An iterative algorithm uses a looping construct
- A recursive algorithm uses a branching structure(if-statement)

- Recursive solutions are often less efficient, in terms of both time and space, than iterative solutions
- Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code

- The simplest form of recursion is linear recursion, where a method is defined so that it makes at most one recursive call each time it is invoked.

A =

Algorithm LinearSum(A , n )

input

A integer array Aand an integer n>=1, such that A has at least n elements

output

The sum of the first n integers in A

if (n = 1) then

return A[0]

else

return LinearSum(A, n - 1) + A[n - 1]

n = 5

Algorithm sum ( n <integer>)

Input

n is an integer number

o

is returned

if (n == 1)

return 1;

else

return n + sum(n-1);

End IF

- For n =3

Sum (3)

return 3+Sum(2)

return 3+3

return 6

Sum(2)

return 2+ sum(1)

return 2+1

return 3

return 1

Sum(1)

return 1

Algorithm printHelloWorld( n <integer>)

Input

n is an integer number

Output

“Hello World” printed n times

If ( n >0 )

{

Print “Hello World”

printHelloWorld( n – 1 );

}

hello world

return

printHelloWorld(3)

Print hello world

printHelloWorld(2)

hello world

printHelloWorld(2)

Print hello world

printHelloWorld(1)

return

hello world

printHelloWorld(1)

Print hello world

printHelloWorld(0)

return

return

- printHelloWorld(0)

- When an algorithm makes two recursive calls, we say that it uses binary recursion. These calls can, for example, be used to solve two similar halves of some problem,

- Let us consider the problem of computing the kth Fibonacci number.
- {0, 1, 1, 2, 3, 5, 8, 13, 21, 34,….}
- Fibonacci numbers are recursively defined as follows:
- F0 = 1
- F1 = 1
- Fi = Fi−1 + Fi−2.

Algorithm BinaryFib(k):

Input:

Nonnegative integer k

Output:

The kth Fibonacci number f

if(k< I) thenreturn k

elsereturn BinaryFib(k - 1) + BinaryFib(k -2)

what is the output of fun(16)?

Algorithm fun(int X)

if ( x <=4 )

return 1

else

return fun(x/4) + fun (x/2)

Result = 3

- Generalizing from binary recursion, we use multiple recursion when a method may make multiple recursive calls, with that number potentially being more than two.