cs212: datastructures lecture 3: recursion 1. lecture contents 2 the concept of recursion why...

17
CS212: DATASTRUCTURES Lecture 3: Recursion 1

Upload: austin-hall

Post on 01-Jan-2016

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

CS212: DATASTRUCTURES

Lecture 3: Recursion

1

Page 2: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

2

Lecture Contents

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

Page 3: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

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;       }}

Page 4: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

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

Page 5: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

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=

Page 6: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

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

Page 7: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

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

Page 8: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

8

Recursive Computation of n!

Page 9: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

9

Recursive Computation of n!

Page 10: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

10

Page 11: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

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

Page 12: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

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

Page 13: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

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

Page 14: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

14

Tracing Recursive Method

recursiveFactorial (4)

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

Page 15: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

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

Page 16: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

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

Page 17: CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a

17

References:• Text book, chapter6: Recursion

End Of Chapter