{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

This preview shows pages 1–6. Sign up to view the full content.

The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Jav a Looking Ahead C H A P T E R 1 4 The best way to predict the future is to invent it. —Alan Kay, Xerox Palo Alto Research Center, 1972 14.1 Recursion 14.2 Concurrency 14.3 Using the network 14.4 Programming patterns

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Recursion This chapter offers a brief introduction to four topics that are sometimes included an introductory computer science course. The first of these topics is recursion , which is the process of solving a problem by dividing it into smaller subproblems of the same form . The italicized phrase is the essential characteristic of recursion; without it, all you have is a description of stepwise refinement as discussed in Chapter 5. The fact that recursive decomposition generates subproblems that have the same form as the original problem means that recursive programs will use the same method to solve subproblems at different levels of the solution. In terms of the structure of the code, the defining characteristic of recursion is having methods that call themselves, directly or indirectly, as the decomposition process proceeds.
A Simple Illustration of Recursion Suppose that you are the national fundraising director for a charitable organization and need to raise \$1,000,000. One possible approach is to find a wealthy donor and ask for a single \$1,000,000 contribution. The problem with that strategy is that individuals with the necessary combination of means and generosity are difficult to find. Donors are much more likely to make contributions in the \$10 range. Another strategy would be to ask 100,000 friends for \$10 each. Unfortunately, most of us don’t have 100,000 friends. There are, however, more promising strategies. You could, for example, find ten regional coordinators and charge each one with raising \$100,000. Those regional coordinators could in turn delegate the task to local coordinators, each with a goal of \$10,000, continuing the process reached a manageable contribution level.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
A Simple Illustration of Recursion The following diagram illustrates the recursive strategy for raising \$1,000,000 described on the previous slide: Goal: \$1,000,000 Goal: \$100,000 Goal: \$100,000 Goal: \$100,000 Goal: \$100,000 Goal: \$100,000 Goal: \$100,000 Goal: \$100,000 Goal: \$100,000 Goal: \$100,000 Goal: \$100,000 Goal: \$10,000 Goal: \$10,000 Goal: \$10,000 Goal: \$10,000 Goal: \$10,000 Goal: \$10,000 Goal: \$10,000 Goal: \$10,000 Goal: \$10,000 Goal: \$10,000 Goal: \$1000 Goal: \$1000 Goal: \$1000 Goal: \$1000 Goal: \$1000 Goal: \$1000 Goal: \$1000 Goal: \$1000 Goal: \$1000 Goal: \$1000 Goal: \$100 Goal: \$100 Goal: \$100 Goal: \$100 Goal: \$100 Goal: \$100 Goal: \$100 Goal: \$100 Goal: \$100 Goal: \$100
A Pseudocode Fundraising Strategy If you were to implement the fundraising strategy in the form of a Java method, it would look something like this: private void collectContributions(int n) { if (n <= 100) { Collect the money from a single donor. } else { Find 10 volunteers. Get each volunteer to collect n/10 dollars. Combine the money raised by the volunteers.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}