This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 The Role of Algorithms in Computing What are algorithms? Why is the study of algorithms worthwhile? What is the role of algorithms relative to other technologies used in computers? In this chapter, we
will answer these questions. A
1.1 Algorithms Informally, an algorithm is any welldeﬁned computational procedure that takes
some value, or Set of values, as input and produces some value, or set of values, as
output. An algorithm is thus a sequence of computational steps that transform the
input into the output. We can also View an algorithm as a tool for solving a wellspeciﬁed computa
tional problem. The statement of the problem speciﬁes in general terms the desired
input/output relationship. The algon'thm describes a speciﬁc computational proce—
dure for achieving that input/output relationship. For example, one might need to sort a sequence of numbers into nondecreasing
order. This problem arises frequently in practice and provides fertile ground for introducing many standard design techniques and analysis tools. Here is how we
formally deﬁne‘the sorting problem: Input: A sequence ofn numbers (a1, a2, . . . , an). Output: A permutation (reordering) (ai, aé, . . . , of) of the input sequence such
thata; 503 5 5a,} For example, given the input sequence (31, 41, 59, 26, 41, 58), a sorting algorithm
returns as output the sequence (26, 31, 41, 41, 58, 59). Such an input sequence is
called an instance of the sorting problem. In general, an instance of a problem
consists of the input (satisfying whatever constraints are imposed in the problem
statement) needed to compute a solution to the problem. Sorting is a fundamental operation in computer science (many programs use it
as an intermediate step), and as a result a large number of good sorting algorit'" i
i
g Chapter 1 The Role of Algorithms in Computing have been developed. Which algorithm is best for a given application depends
on—among other factors—the number of items to be sorted, the extent to which
the items are already somewhat sorted, possible restrictions on the item values, and
the kind of storage device to be used: main memory, disks, or tapes. An algorithm is said to be correct if, for every input instance, it halts with the
correct output. We say that a correct algorithm solves the given computational
problem. An incorrect algorithm might not halt at all on some input instances, or it
might halt with an answer other than the desired one. Contrary to what one might
expect, incorrect algorithms can sometimes be useful, if their error rate can be
controlled. We shall see an example of this in Chapter 31 when we study algorithms
for ﬁnding large prime numbers. Ordinarily, however, we shall be concerned only
with correct algorithms. An algorithm can be speciﬁed in English, as a computer program, or even as
a hardware design. The only requirement is that the speciﬁcation must provide a
precise description of the computational procedure to be followed. What kinds of problems are solved by algorithms? Sorting is by no means the only computational problem for which algorithms have
been developed. (You probably suspected as much when you saw the size of this
' book.) Practical applications of algorithms are ubiquitous and include the follow
ing examples: ' The HumanGenome Project has the goals of identifying all the 100,000 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 devel
oping tools for data analysis. Each of these steps requires sophisticated algo
rithms. While the solutions to the various problems involved are beyond the
scope of this book, ideas from many of the chapters in this book are used in the
solution of these biological problems, 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 arms and retrieve
large amounts of information. In order to do so, clever algorithms are employed
to manage and manipulate this large volume of data. Examples of problems
which must be solved 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 '11 and 32). I .1 Algorithms  Electronic commerce enables goods and services to be negotiated and ex
changed electronically. The ability to keep information such as credit card num
bers, passwords, and bank statements private is essential if electronic commerce
is to be used widely. Publickey cryptography and digital signatures (covered in
Chapter 31) are among the core technologies used and are based on numerical
algorithms and number theory. ° In manufacturing and other commercial settings, it is often important to allo—
cate scarce resources in the most beneficial way. An oil company may wish
to know where to place its wells in order to maximize its expected proﬁt. A
candidate for the presidency of the United States 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 assigncrews 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 shail study in Chapter 29. While 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 concrete problems in this book, including the following:  We are given a road map on which the distance between each pair of adjacent
intersections is marked, and our goal is to determine the shortest route from
one intersection to another. The number of possible routescan 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 Chapter 10 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 a sequence (A1, A2, .. . , A") of n matrices, and we wish to deter
mine their product A1A2    A”. Because matrix multiplicationis associative,
there are several legal multiplication orders. For example, if n = 4, we could
perform the'matrix multiplications as if the product were parén'thesized in any
of the following orders: (A1(A2(A3A4))), (A1((A2A3)A4)), (>(AIA2)(A3A4)),
((A1(A2A3))A4), or (((A1A2)A3)A4). If these matrices are all square (and
hence the same size), the multiplication order will not affect how long the ma
trix multiplications take. If, however, these matrices are of differing sizes (yet
their sizes are compatible for matrix multiplication), then the multiplication
order can make a very big difference. The number of possible multiplication /'./_, Chapter 1 The Role of Algorithms in Computing orders is exponential in n, and so trying all possible orders may take a very
long time. 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 an equation ax E b (mod n), where a, b, and n are integers,
and we wish to ﬁnd all the integers x, modulo n, that satisfy the equation.
There may be zero, one, or more than one such solution. We can simply try
x = 0, 1, . . . , n — 1 in order, but Chapter 31 shows a more efﬁcient method. We are given It 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 33.6 on page 948
for an example.) Any of the 2" 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 33 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 interest
ing algorithms. 1. There are many candidate solutions, most of which are not what we want. Find
ing one that we do want can present quite a challenge. There are practical applications. Of the problems in the above list, shortest
paths 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. Data structures 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. [.1 Algorithms 9 Technique Although you can use this book as a “cookbook” for algorithms, you maysomeday
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. Hard problems 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 NPcomplete. Why are NP—complete problems interesting? First, although no efﬁcient algo
rithm for an NP—complete problem has ever been found, nobody has ever proven
that an efﬁcient algorithm for one cannot exist. In other words, it is unknown
whether or not efﬁcient algorithms exist for NPcomplete problems. Second, the
set of NP—complete problems has the remarkable property that if an efﬁcient al
gorithm exists for any one of them, then efﬁcient algorithms exist for all of them.
This relationship among the NPcomplete problems makes the lack of efﬁcient so—
lutions all the more tantalizing. Third, several NPcomplete problems are similar,
but not identical, to—problems for which we do know of efﬁcient algorithms. A
small change to the problem statement can cause a big change to the efﬁciency of the best known algorithm.
It is valuable to 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. Ifyou can show that the problem is NPcomplete, 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 trucking company with a central warehouse.
Each day, it loads up the truck at the warehouse and sends it around to several 10
cations to make deliveries. At the end of the day, the truck must end up back at
the warehouse 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 the truck. This problem is the wellknown “travelingsalesman
problem,” and it is NPcomplete. It has no known efﬁcient algorithm. Under cer
tain assumptions, however, there are efﬁcient algorithms that give an overall dis
tance that is not too far above the smallest possible. Chapter 35 discusses such
“approximation algorithms.” 10 Chapter 1 The Role of Algorithms in Computing Exercises 1.11 Give a realworld example in which one of the following computational problems
appears: sorting, determining the best order for multiplying matrices, or ﬁnding
the convex hull. 1.1 2
Other than speed, what other measures of efﬁciency might one use in a realworld
setting? ' 1.13
Select a data structure that you have seen previously, and discuss its strengths and
limitations. 1.14
How are the shortestpath and travelingsalesman problems given above similar? How are they different? 1.1 5 Come up with a realworld 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 (i.e., 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 cheap, but it is not free. Computing time is therefore a bounded resource,
and so is space in memory. These resources should be used wisely, and algorithms
that are efﬁcient in terms of time or space will help you do so. 1.2 Algorithms as a technology I I Efﬁciency 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 cm2 to sort n items, where Cl
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 czn lg n, where lgn
stands for log2 n and c; is another constant that also does not depend on n. Insertion
sort usually has a smaller constant factor than merge sort, so that c; < C2. We shall
see that the constant factors can be far less signiﬁcant in the running time than the ‘
dependence on the input size 11. Where merge sort has a factor of lg n in its running
time, insertion sort has a factor of n, which is much larger. Although insertion sort
is usually 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 compensate for
the difference in constant factors. No matter how much smaller Cl is than Cg, 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 one million numbers. Suppose that computer A executes one
billion instructions per second and computer B executes only ten million instruc
tions per second, so that computer A is 100 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 com
puter A, and the resulting code requires 2n2 instructions to sort n numbers. (Here,
cl = 2.) Merge sort, on the other hand, is programmed for computer B by an aver
age programmer using a highlevel language with an inefﬁcient compiler, with the
resulting code taking 50n1g n instructions (so that C2 = 50). To sort one million
numbers, computer A takes 2 106 2 ' tru t‘
w = 2000 seconds ,
109 mstructions/second while computer B takes 50  106 lg 10" instructions . , m 100 seconds .
107 instructions/second By using an algorithm whose running time grows more slowly, even with a poor
compiler, computer B runs 20 times faster than computer A! The advantage of
merge sort is even more pronounced when we sort ten million numbers: where
insertion sort takes approximately 2.3 days, merge sort takes under 20 minutes. In
general, as the problem size increases, so does therelative advantage of merge sort. 12 Chapter I The Role of Algorithms in Computing Algorithms and other technologies The example above shows that algorithms, like computer hardware, are a technol
ogy. Total system performance depends on choosing efﬁcient algorithms as much
as on choosing fast hardware. Just as rapid advances are being made in other com
puter technologies, they are being made in algorithms as well. You might wonder whether algorithms are truly that important on contemporary
computers in light of other advanced technologies, such as  hardware with high clock rates, pipelining, and superscalar architectures,
 easytouse, intuitive graphical user interfaces (GU15),
 objectoriented systems, and ' localarea and widearea networking. The answer is yes. Although there are some applications that do not explicitly
require algorithmic content at the application level (e.g., some simple webbased
applications), most also require a degree of algorithmic content on their own. For
example, consider a webbased service that determines how to travel from one
location to another. (Several such services existed at the time of this writing.) Its
implementation would rely on fast hardware, a graphical user interface, wide—area
networking, and also possibly on object orientation. However, it would also require
algorithms for certain operations, such as ﬁnding routes (probably using a shortest—
path algorithm), rendering maps, and interpolating addresses. Moreover, even an application that does not require algorithmic content at the
application level relies heavily upon algorithms. Does the application rely on fast
hardware? The hardware design used algorithms. Does the application rely on
graphical user interfaces? The design of any GUI relies on algorithms. Does the
application rely on networking? Routing in networks relies heavily on algorithms.
Was the application written in a language other than machine code? Then it was
processed by a compiler, interpreter, or assembler, all of which make extensive use
of algorithms. Algorithms are at the core of most technologies used in contempo
rary computers. Furthermore, with the everincreasing capacities of computers, we use them to
solve larger problems than ever before. As we saw in the above comparison be
tween insertion sort and merge sort, it is at larger problem sizes that the differences
in efﬁciencies between algorithms become particularly prominent. Having a solid base of algorithmic knowledge and technique is one characteristic
that separates the truly skilled programmers from the novices. With modern com
puting technology, you can accomplish some tasks without knowing much about
algorithms, but with a good background in algorithms, you can do much, much
more. ...
View
Full Document
 Spring '08
 SteffenHeber
 Algorithms

Click to edit the document details