CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method...

Post on 22-Dec-2015

236 views 3 download

transcript

CHAPTER 10

Recursion

2

Recursive Thinking

Recursion is a programming technique in which a method can call itself to solve a problem

A recursive definition is one that describes something in terms of simpler versions of itself.

3

Recursive Definitions

Ordering property: "Simpler" means that we can order the versions by some criterion.

Simplest version: Minimal / base case of the recursive object/procedure

4

Recursive Definitions - Examples Consider the following list of numbers:

24, 88, 40, 37

Such a list can be defined recursively:

A LIST is a: numberor a: number comma LIST

That is, a LIST can be a number, or a number followed by a comma followed by a LIST

The concept of a LIST is used to define itself

5

FIGURE 10.1 Tracing the recursive definition of a list

6

Recursive Definitions - Examples

Consider the function f(n) = an.

Initial condition: f(0) = 1

Recursive rule:f(n) = a * f(n-1)

7

Recursive Definitions - Examples

Consider the function f(n) = n!n! = 1 * 2 * 3 * … * (n-2) * (n-1) * n 0! = 1 by definition

Initial condition: f(0) = 1

Recursive rule:f(n) = n * f(n-1)

8

Recursive Methods in Programming 1

A recursive method must have at least one base, or stopping, case

Each successive call to itself must be a "smaller version of itself" so that a base case is eventually reached.

9

Recursive Methods in Programming - Example

boolean BinarySearch(A, Start, End, P)find M = A[middle]

If (M = P) return true <<<<< the baseelse if (End - Start = 0) return false <<<<< the base

else if (P > M) BinarySearch(A, middle+1, End, P) else BinarySearch(A, Start, middle, P)

Here the terminating condition is End - Start = 0 or M = P

10

Recursive Methods in Programming 2

A method in Java can invoke itself; if set up that way, it is called a recursive method

The code of a recursive method must be structured to handle both the base case and the recursive case

Each call sets up a new execution environment, with new parameters and new local variables

As always, when the method completes, control returns to the method that invoked it

11

How to design a recursive method 1

A recursive method must have: one or more base cases one or more recursive cases which make the

problem smaller General Idea:

determine your base cases first (What can you solve?)

determine how to simplify the problem to get to a base case

12

How to design a recursive method 2

The base case identifies a condition of the parameters in which the result can be immediately computed. 

The recursive case identifies the situation, in which the result must be expressed in terms of the solution to a simpler problem. 

13

How to design a recursive method 3

To distinguish between the base case and the recursive case, a recursive method always can be expressed in the form of an if...else statement as follows:

    if (base case)        // compute and return the result directly     else       // re-call the method to obtain the solution to a       // simpler problem, and compute the result from

// that solution

14

Infinite Recursion

All recursive definitions must have a non-recursive part. If they don't, there is no way to terminate the recursive path

A definition without a non-recursive part causes infinite recursion.

This problem is similar to an infinite loop -- with the definition itself causing the infinite "looping"

15

How to avoid infinite recursion

Take special care to ensure that the base case is properly implemented.

Make sure that each subsequent recursive call represents progress toward the base case

16

Recursive Programming – Example 2

Consider the problem of computing the sum of all the numbers between 1 and N, inclusive

If N is 5, the sum is 1 + 2 + 3 + 4 + 5 This problem can be expressed recursively as:

The sum of 1 to N is N plus the sum of 1 to N-1

17

FIGURE 10.2 The sum of the numbers 1 through N, defined recursively

18

Recursive Programming – the code

public int sum (int num){ int result; if (num == 1) result = 1; else result = num + sum(num-1); return result;}

19

FIGURE 10.3 Recursive calls to the sum method

20

Indirect Recursion

A method invoking itself is considered to be direct recursion

A method could invoke another method, which invokes another, etc., until eventually the original method is invoked again

For example, method m1 could invoke m2, which invokes m3, which invokes m1 again

This is called indirect recursion

It is often more difficult to trace and debug

21

FIGURE 10.4 Indirect recursion

22

Verifying Recursive Methods

Trace the recursive method – difficult

Draw diagram of the recursive calls – can be done for small examples only

Use a method based on algorithm specification

23

Verifying Recursive Methods

Develop a precise specification of what the algorithm has to do

Check the base case – make sure the result meets the specification

Check each recursive case and make sure it is for a smaller version of the problem, so that eventually the base case will be executed

Assuming that the recursive call returns correct results, check to see if the recursive case meets the specifications

24

Recursion vs. Iteration

Just because we can use recursion to solve a problem, doesn't mean we should For instance, we usually would not use recursion

to solve the sum of 1 to N The iterative version is easier to understand

(in fact there is a formula that is superior to both recursion and iteration in this case)

You must be able to determine when recursion is the correct technique to use

25

Recursion vs. Iteration

Every recursive solution has a corresponding iterative solution For example, the sum of the numbers between 1

and N can be calculated with a loop

Recursion has the overhead of multiple method invocations

However, for some problems recursive solutions are often more simple and elegant than iterative solutions

26

When to Use Recursion

Recursion is preferred when each next call substantially reduces the input size

Example: Recursive sum calls reduce the input by one:

result = num + sum(num-1); Recursive binary search reduces the input by a

factor of 2 if (P > M) BinarySearch (A, middle+1, End, P) else BinarySearch (A, Start, middle, P)

27

Example: Computing an

Compute 230 with 7 multiplications only (((22 *2 )2 * 2)2 *2)2

Iteration will require 29 multiplications Recursive solution:

int pow(a,N) if N = 1 return a // terminating condition else

if N is even return pow(a, N/2) * pow(a,N/2) else return a*pow(a,N/2)*pow(a,N/2)

28

Analysis of recursive methods

Solving recurrence relationspublic int sum (int num)

{ if (num == 1) return 1;

else return num + sum(num-1); }

Recurrence relation: T(N) = T(N-1) + 1 The time to compute the sum of N integers is

equal to the time to compute the sum of N-1 integers plus a constant

29

Solving recurrence relations T(N) = T(N-1) + 1 Telescoping:

T(N) = T(N-1) + 1 T(N-1) = T(N-2) +1 ….. T(2) = T(1) +1

Add the left and the right sidesT(N) + T(N-1) + …. T(2) = T(N-1) + T(N-2) +…T(1) + 1 + 1 + …. + 1After crossing the equal terms

T(N) = T(1) + 1 + 1 + … + 1 = NTherefore the complexity is O(N), same as with iteration