(Parte 3 de 6)
The Human Genome Project has made great progress toward the goals of identifying all the 100,0 genes in human DNA, determining the sequences of the 3 billion chemical base pairs that make up human DNA, storing this information in databases, and developing tools for data analysis. Each of these steps requires sophisticated algorithms. Although the solutions to the various problems involved are beyond the scope of this book, many methods to solve these biological problems use ideas from several of the chapters in this book, thereby enabling scientists to accomplish tasks while using resources efﬁciently. The savings are in time, both human and machine, and in money, as more information can be extracted from laboratory techniques.
The Internet enables people all around the world to quickly access and retrieve large amounts of information. With the aid of clever algorithms, sites on the Internet are able to manage and manipulate this large volume of data. Examples of problems that make essential use of algorithms include ﬁnding good routes on which the data will travel (techniques for solving such problems appear in
Chapter 24), and using a search engine to quickly ﬁnd pages on which particular information resides (related techniques are in Chapters 1 and 32).
Electronic commerce enables goods and services to be negotiated and exchanged electronically, and it depends on the privacy of personal information such as credit card numbers, passwords, and bank statements. The core technologies used in electronic commerce include public-key cryptography and digital signatures (covered in Chapter 31), which are based on numerical algorithms and number theory.
Manufacturing and other commercial enterprises often need to allocate scarce resources in the most beneﬁcial way. An oil company may wish to know where to place its wells in order to maximize its expected proﬁt. A political candidate may want to determine where to spend money buying campaign advertising in order to maximize the chances of winning an election. An airline may wish to assign crews to ﬂights in the least expensive way possible, making sure that each ﬂight is covered and that government regulations regarding crew scheduling are met. An Internet service provider may wish to determine where to place additional resources in order to serve its customers more effectively. All of these are examples of problems that can be solved using linear programming, which we shall study in Chapter 29.
Although some of the details of these examples are beyond the scope of this book, we do give underlying techniques that apply to these problems and problem areas. We also show how to solve many speciﬁc problems, including the following:
We are given a road map on which the distance between each pair of adjacent intersections is marked, and we wish to determine the shortest route from one intersection to another. The number of possible routes can be huge, even if we disallow routes that cross over themselves. How do we choose which of all possible routes is the shortest? Here, we model the road map (which is itself a model of the actual roads) as a graph (which we will meet in Part VI and Appendix B), and we wish to ﬁnd the shortest path from one vertex to another in the graph. We shall see how to solve this problem efﬁciently in Chapter 24.
We are given two ordered sequences of symbols, X Dh x1;x 2; :::; xmi and
Y Dh y1;y 2; :::; yni, and we wish to ﬁnd a longest common subsequence of X and Y . A subsequence of X is just X with some (or possibly all or none) of its elements removed. For example, one subsequence of hA;B;C;D;E;F;Gi would be hB; C; E; Gi. The length of a longest common subsequence of X and Y gives one measure of how similar these two sequences are. For example, if the two sequences are base pairs in DNA strands, then we might consider them similar if they have a long common subsequence. If X has m symbols and Y has n symbols, then X and Y have 2m and 2n possible subsequences,
8 Chapter 1 The Role of Algorithms in Computing respectively. Selecting all possible subsequences of X and Y and matching them up could take a prohibitively long time unless m and n are very small. We shall see in Chapter 15 how to use a general technique known as dynamic programming to solve this problem much more efﬁciently.
We are given a mechanical design in terms of a library of parts, where each part may include instances of other parts, and we need to list the parts in order so that each part appears before any part that uses it. If the design comprises n parts, then there are nŠ possible orders, where nŠ denotes the factorial function. Because the factorial function grows faster than even an exponential function, we cannot feasibly generate each possible order and then verify that, within that order, each part appears before the parts using it (unless we have only a few parts). This problem is an instance of topological sorting, and we shall see in Chapter 2 how to solve this problem efﬁciently.
We are given n points in the plane, and we wish to ﬁnd the convex hull of these points. The convex hull is the smallest convex polygon containing the points. Intuitively, we can think of each point as being represented by a nail sticking out from a board. The convex hull would be represented by a tight rubber band that surrounds all the nails. Each nail around which the rubber band makes a turn is a vertex of the convex hull. (See Figure 3.6 on page 1029 for an example.) Any of the 2n subsets of the points might be the vertices of the convex hull. Knowing which points are vertices of the convex hull is not quite enough, either, since we also need to know the order in which they appear. There are many choices, therefore, for the vertices of the convex hull. Chapter 3 gives two good methods for ﬁnding the convex hull.
These lists are far from exhaustive (as you again have probably surmised from this book’s heft), but exhibit two characteristics that are common to many interesting algorithmic problems:
1. They have many candidate solutions, the overwhelming majority of which do not solve the problem at hand. Finding one that does, or one that is “best,” can present quite a challenge.
2. They have practical applications. Of the problems in the above list, ﬁnding the shortest path provides the easiest examples. A transportation ﬁrm, such as a trucking or railroad company, has a ﬁnancial interest in ﬁnding shortest paths through a road or rail network because taking shorter paths results in lower labor and fuel costs. Or a routing node on the Internet may need to ﬁnd the shortest path through the network in order to route a message quickly. Or a person wishing to drive from New York to Boston may want to ﬁnd driving directions from an appropriate Web site, or she may use her GPS while driving.
Not every problem solved by algorithms has an easily identiﬁed set of candidate solutions. For example, suppose we are given a set of numerical values representing samples of a signal, and we want to compute the discrete Fourier transform of these samples. The discrete Fourier transform converts the time domain to the frequency domain, producing a set of numerical coefﬁcients, so that we can determine the strength of various frequencies in the sampled signal. In addition to lying at the heart of signal processing, discrete Fourier transforms have applications in data compression and multiplying large polynomials and integers. Chapter 30 gives an efﬁcient algorithm, the fast Fourier transform (commonly called the FFT), for this problem, and the chapter also sketches out the design of a hardware circuit to compute the FFT.
This book also contains several data structures. A data structure is a way to store and organize data in order to facilitate access and modiﬁcations. No single data structure works well for all purposes, and so it is important to know the strengths and limitations of several of them.
Although you can use this book as a “cookbook” for algorithms, you may someday encounter a problem for which you cannot readilyﬁnd a published algorithm (many of the exercises and problems in this book, for example). This book will teach you techniques of algorithm design and analysis so that you can develop algorithms on your own, show that they give the correct answer, and understand their efﬁciency. Different chapters address different aspects of algorithmic problem solving. Some chapters address speciﬁc problems, such as ﬁnding medians and order statistics in Chapter 9, computing minimum spanning trees in Chapter 23, and determining a maximum ﬂow in a network in Chapter 26. Other chapters address techniques, such as divide-and-conquer in Chapter 4, dynamic programming in Chapter 15, and amortized analysis in Chapter 17.
Most of this book is about efﬁcient algorithms. Our usual measure of efﬁciency is speed, i.e., how long an algorithm takes to produce its result. There are some problems, however, for which no efﬁcient solution is known. Chapter 34 studies an interesting subset of these problems, which are known as NP-complete.
Why are NP-complete problems interesting? First, although no efﬁcient algorithm for an NP-complete problem has ever been found, nobody has ever proven
10 Chapter 1 The Role of Algorithms in Computing that an efﬁcient algorithm for one cannot exist. In other words, no one knows whether or not efﬁcient algorithms exist for NP-complete problems. Second, the set of NP-complete problems has the remarkable property that if an efﬁcient algorithm exists for any one of them, then efﬁcient algorithms exist for all of them. This relationship among the NP-complete problems makes the lack of efﬁcient solutions all the more tantalizing. Third, several NP-complete problems are similar, but not identical, to problems for which we do know of efﬁcient algorithms. Computer scientists are intrigued by how a small change to the problem statement can cause a big change to the efﬁciency of the best known algorithm.
You should know about NP-complete problems because some of them arise surprisingly often in real applications. If you are called upon to produce an efﬁcient algorithm for an NP-complete problem, you are likely to spend a lot of time in a fruitless search. If you can show that the problem is NP-complete, you can instead spend your time developing an efﬁcient algorithm that gives a good, but not the best possible, solution.
As a concrete example, consider a delivery company with a central depot. Each day, it loads up each delivery truck at the depot and sends it around to deliver goods to several addresses. At the end of the day, each truck must end up back at the depot so that it is ready to be loaded for the next day. To reduce costs, the company wants to select an order of delivery stops that yields the lowest overall distance traveled by each truck. This problem is the well-known “traveling-salesman problem,” and it is NP-complete. It has no known efﬁcient algorithm. Under certain assumptions, however, we know of efﬁcient algorithms that give an overall distance which is not too far above the smallest possible. Chapter 35 discusses such “approximation algorithms.”
For many years, we could count on processor clock speeds increasing at a steady rate. Physical limitations present a fundamental roadblock to ever-increasing clock speeds, however: because power density increases superlinearly with clock speed, chips run the risk of melting once their clock speeds become high enough. In order to perform more computations per second, therefore, chips are being designed to contain not just one but several processing “cores.” We can liken these multicore computers to several sequential computers on a single chip; in other words, they are a type of “parallel computer.” In order to elicit the best performance from multicore computers, we need to design algorithms with parallelism in mind. Chapter 27 presents a model for “multithreaded” algorithms, which take advantage of multiple cores. This model has advantages from a theoretical standpoint, and it forms the basis of several successful computer programs, including a championship chess program.
1.2 Algorithms as a technology 1
1.1-1 Give a real-world example that requires sorting or a real-world example that requires computing a convex hull.
1.1-2 Other than speed, what other measures of efﬁciency might one use in a real-world setting?
1.1-3 Select a data structure that you have seen previously, and discuss its strengths and limitations.
1.1-4 How are the shortest-path and traveling-salesman problems given above similar? How are they different?
1.1-5 Come up with a real-world problem in which only the best solution will do. Then come up with one in which a solution that is “approximately” the best is good enough.
1.2 Algorithms as a technology
Suppose computers were inﬁnitely fast and computer memory was free. Would you have any reason to study algorithms? The answer is yes, if for no other reason than that you would still like to demonstrate that your solution method terminates and does so with the correct answer.
If computers were inﬁnitely fast, any correct method for solving a problem would do. You would probably want your implementation to be within the bounds of good software engineering practice (for example, your implementation should be well designed and documented), but you would most often use whichever method was the easiest to implement.
Of course, computers may be fast, but they are not inﬁnitely fast. And memory may be inexpensive, but it is not free. Computing time is therefore a bounded resource, and so is space in memory. You should use these resources wisely, and algorithms that are efﬁcient in terms of time or space will help you do so.
12 Chapter 1 The Role of Algorithms in Computing
Different algorithms devised to solve the same problem often differ dramatically in their efﬁciency. These differences can be much more signiﬁcant than differences due to hardware and software. As an example, in Chapter 2, we will see two algorithms for sorting. The ﬁrst, known as insertion sort, takes time roughly equal to c1n2 to sort n items, where c1 is a constant that does not depend on n. That is, it takes time roughly proportional to n2. The second, merge sort, takes time roughly equal to c2nlgn, where lgn stands for log2 n and c2 is another constant that also does not depend on n. Inser- tion sort typically has a smaller constant factor than merge sort, so that c1 <c 2. We shall see that the constant factors can have far less of an impact on the running time than the dependence on the input size n. Let’s write insertion sort’s running time as c1n n and merge sort’s running time as c2n lgn. Then we see that where insertion sort has a factor of n in its running time, merge sort has a factor of lgn, which is much smaller. (For example, when n D 1000,l gn is approximately 10, and when n equals one million, lgn is approximately only 20.) Although insertion sort usually runs faster than merge sort for small input sizes, once the input size n becomes large enough, merge sort’s advantage of lgn vs. n will more than com- pensate for the difference in constant factors. No matter how much smaller c1 is than c2, there will always be a crossover point beyond which merge sort is faster. For a concrete example, let us pit a faster computer (computer A) running inser- tion sort against a slower computer (computer B) running merge sort. They each must sort an array of 10 million numbers. (Although 10 million numbers might seem like a lot, if the numbers are eight-byte integers, then the input occupies about 80 megabytes, which ﬁts in the memory of even an inexpensive laptop computer many times over.) Suppose that computer A executes 10 billion instructions per second (faster than any single sequential computer at the time of this writing) and computer B executes only 10 million instructions per second, so that computer A is 1000 times faster than computer B in raw computing power. To make the difference even more dramatic, suppose that the world’s craftiest programmer codes insertion sort in machine language for computer A, and the resulting code requires 2n2 instructions to sort n numbers. Suppose further that just an average programmer implements merge sort, using a high-level language with an inefﬁcient compiler, with the resulting code taking 50nlg n instructions. To sort 10 million numbers, computer A takes
(Parte 3 de 6)