29
CHAPTER 10 Recursion

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

  • View
    241

  • Download
    3

Embed Size (px)

Citation preview

Page 1: 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

CHAPTER 10

Recursion

Page 2: 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

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.

Page 3: 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

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

Page 4: 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

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

Page 5: 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

5

FIGURE 10.1 Tracing the recursive definition of a list

Page 6: 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

6

Recursive Definitions - Examples

Consider the function f(n) = an.

Initial condition: f(0) = 1

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

Page 7: 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

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)

Page 8: 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

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.

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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

Page 12: 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

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. 

Page 13: 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

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

Page 14: 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

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"

Page 15: 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

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

Page 16: 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

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

Page 17: 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

17

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

Page 18: 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

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

Page 19: 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

19

FIGURE 10.3 Recursive calls to the sum method

Page 20: 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

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

Page 21: 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

21

FIGURE 10.4 Indirect recursion

Page 22: 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

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

Page 23: 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

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

Page 24: 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

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

Page 25: 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

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

Page 26: 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

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)

Page 27: 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

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)

Page 28: 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

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

Page 29: 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

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