15. Recursive Problem Solving

15.2. Recursive Definition

One place you might have already seen recursion is in mathematics. A recursive definition consists of two parts: a recursive part in which the nth value is defined in terms of the (n 1)st value, and a nonrecursive, boundary or base case, which defines a limiting condition.

Factorial: N!

For example, consider the problem of calculating the factorial of n—that is, n! for n 0. As you may recall, n! is calculated as follows:

,,

 

J

In addition, 0! is defined as 1. Let’s now look at some examples for different values of n:

,,

 

 

 

J

As these examples suggest, n! can always be calculated in terms of (n 1)! This relationship might be clearer if we rewrite the previous calculations as follows:

,,

 

 

 

J

The only case in which we can’t calculate n! in terms of (n1)! is when n

is 0. Otherwise, in each case we see that

,,

 

J

This leads to the following recursive definition:

,,

 

J

Note that if we had omitted the base case, the recursion would have continued to (−1)! and (−2)! and so on.

The recursive case uses divide and conquer to break the problem into a smaller problem, but the smaller problem is just a smaller version of the

 

original problem. This combination of self-similarity and divide and con- quer is what characterizes recursion. The base case is used to stop or limit the recursion.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Drawing a Nested Pattern

 

 

 

 

 

 

 

 

Figure 12.2: pattern.


 

 

 

 

 

The nested squares


As another example, consider the problem of drawing the nested boxes pattern in Figure 12.2. The self-similarity occurs in the fact that for this pattern, its parts resemble the whole. The basic shape involved is a square, which is repeated over and over at an ever-smaller scale. A recursive definition for this pattern would be

,,

 

 

J

This definition uses the length of the square’s side to help define the pat- tern. If the length of the side is greater than or equal to 5, draw a square with dimensions side side. Then decrease the length of the side and draw a smaller version of the pattern inside that square. In this case, the side variable will decrease at each level of the drawing. When the length of the side becomes less than 5, the recursion stops. Thus, the length of the side serves as the limit or bound for this algorithm.

You should note that the length of the side functions here like a param- eter in a method definition: It provides essential information for the defi- nition, just as a method parameter provides essential data to the method. Indeed, this is exactly the role that parameters play in recursive meth- ods. They provide essential information that determines the method’s behavior.

 

Figure 12.3 illustrates how we would apply the definition. Suppose the side starts out at 20 and decreases by 5 at each level of recursion. Note that as you move from top to bottom across the four patterns, each pattern contains the one below it. A nestedBoxes(20) can be drawn by first drawing a 20 20 square and then drawing a nestedBoxes(15) pattern inside it. Similarly, a nestedBoxes(15) can be drawn by first drawing a

15 15 square and then drawing a nestedBoxes(10) pattern inside it. And so on.

These examples illustrate the power of recursion as a problem-solving technique for situations that involve repetition. Like the iterative (looping) control structures we studied in Chapter 6, recursion is used to implement repetition within a bound. For recursive algorithms, the bound is defined by the base case, whereas for loops, the bound is defined by the loop’s entry condition. In either case, repetition stops when the bound is reached.

SELF-STUDY EXERCISES

EXERCISE 12.3 You can calculate 2n by multiplying 2 by itself n times. For example, 23 is 2 × 2 × 2. Note also that 20 = 1. Given these facts, write a recursive definition for 2n, for n 0.

EXERCISE 12.4 Generalize your solution to the previous exercise by giving a recursive definition for xn, where x and n are both integers 0.

EXERCISE 12.5 Is the recursive definition given earlier for the nested boxes equivalent to the following recursive definition? Explain.

,,


20

 

 

 

2

 

 

 

nestedBoxes(20) 15

 

15

 

 

nestedBoxes(15) 10

10

nestedBoxes(10) 5

5

nestedBoxes(5)

 

 

Figure 12.3: A trace of the nested

 

 

In this case, the base case (side <= 5) is implicit.


J boxes definition starting with a side of 20 and decreasing the side

by 5 each time.

 

EXERCISE 12.6 Write a recursive definition for the recursive pattern shown in Figure 12.4.