CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2 The Concept of Recursion Why...

Post on 01-Jan-2016

223 views 1 download

transcript

CS212: DATASTRUCTURES

Lecture 3: Recursion

1

2

Lecture Contents

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

3

The Concept of Recursion

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       {       cout <<counter<<endl;       myFunction(--counter);       return;       }}

4

Why recursion?

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

5

Factorial – A case study

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

factorial 3

3*2*1=

6=

6

Factorial – A case study

Iteration algorithm definition (non-recursive)

factorial 4

product [4..1]=

product [4,3,2,1]=

4*3*2*1=

24=

1 if n = 0 Factorial(n) =

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

Factorial – A case study

Recursion algorithm definition

7

factorial 3

3 * factorial 2=

3 * (2 * factorial 1)=

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

=

3 * (2 * (1 * 1))=

3 * (2 * 1)=

=

6

3 * 2=

1 if n = 0 Factorial(n) =

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

7

8

Recursive Computation of n!

9

Recursive Computation of n!

10

11

Content of a Recursive Method

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…

Recursive calls Base case

12

Content of a Recursive Method

void myFunction( int counter){if(counter == 0)     return ;else {       cout <<counter<<endl;       myFunction(--counter);

       return;  }}

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

and a recursive step

13

Iterative factorial algorithm

Algorithm iterativeFactorial (val n <integer>)

Calculates the factorial of a number using a loop.

Pre n is the number to be raised factorially

Return n! is returned

1 i = 1

2 factN = 1

3 loop (i <= n)

1 factN = factN * i

2 i = i + 1

4 end loop

5 return factN

end iterativeFactorial

Algorithm recursiveFactorial (val n <integer>)

Calculates the factorial of a number using recursion

Pre n is the number to be raised factorially

Return n! is returned

1 if (n equal 0)1 return 1

2 else 1 return (n *

recursiveFactorial(n - 1))3 end ifend recursiveFactorial

Recursive factorial Algorithm

14

Tracing Recursive Method

recursiveFactorial (4)

Each recursive call is made with a new, independent set of arguments Previous calls are suspended

15

Recursion vs. iteration

Iteration can be used in place of recursion An iterative algorithm uses a looping

construct A recursive algorithm uses a branching

structure 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

16

A Simple Example of Recursion

Algorithm LinearSum(A <integer>, n <integer>)

pre A integer array A and an integer

n, such that A has at least n elements

post The sum of the first n integers in

Aif n = 1 then sum= A[0]else sum= LinearSum(A, n - 1) + A[n

- 1]End IFReturn sumend LinearSum

LinearSum (A,5)

LinearSum (A,1)

LinearSum (A,2)

LinearSum (A,3)

LinearSum (A,4)

call

call

call

call return A[0] = 4

return 4 + A[1] = 4 + 3 = 7

return 7 + A[2] = 7 + 6 = 13

return 13 + A[3] = 13 + 2 = 15

call return 15 + A[4] = 15 + 5 = 20

17

References:• Text book, chapter6: Recursion

End Of Chapter