Chapter 1
INTRODUCTION
1.1 WHAT IS AN ALGORITHM?
The word algorithm comes from the name of a Persian author, Abu Ja’far
Mohammed ibn Musa al Khowarizmi (c. 825 A.D.), who wrote a textbook
on mathematies. This word has taken on a special significance in computer
science, where “algorithm” has come to refer to a method that can be used
by a computer for the solution of a problem. This is what, makes algorithm
different from words such as process, technique, or method.
LGERBRB. Zero or more quantities are externally supplied,
2. GREBHB. At least one quantity is produced
3, GRABER Bach instructio:
4 = If we trace out the instructions of an algorithm, then for
all cases, the algorithm terminates after a finite number of steps.
5. GRREEMEBED very instruction must be very basie so that it can be
‘carricd ont, in principle, by a person Gee EA 1
is not enough that each operation be definite as in criterion Sit aso
must be feasible, a
clear and umambiguous.
An algorithm is composed of a finite set of steps, each of which may
require one or more operations. The possibility of a computer carrying out
these operations necessitates that certain constraints be placed on the type
of operations an algorithm ean include.2) CHAPTER 1, INTRODUCTION
Criteria 1 and 2 require that an algorithm produce one or more outputs
and have zero or more inputs that are externally supplied. According to eri-
terion 3, each operation must be definite. meaning that it must be perfectly
clear what should be done. jor “compute
5/0 GERBERA cause 9 a0 less which of te two posites
should be done or what the result is.
‘The fourth criterion for algorithms we assume in this book is that they
terminate after a finite number of operations. A related consideration is,
the time for termination should be reasonably short. For example,
he algorithitr works
Possible moves and countermoves that could be made from the starting po-
sition. The difficulty with this algorithm is that even using the most modern
Sono i REREAD
very concerned with analyzing the efficiency of each of our algorithms.
Criterion 5 requires that each operation be effective; each step must be
such that it can, at least in principle, be done by a person using pencil and
paper in a finite amount of time. Performing arithmetic on integers is an
effective operation, but
‘Adding two §
re portant cea provedurer i (heap.
Prem of "gal compte "This procedure i designed to control
the execution of jobs, m such a way that when no jobs are available, it
does not terminate but continues in a waiting state until a new job is en-
tered. Though computational procedures include important examples such
as this one, we restrict our study to computational procedures that always
terminate.
‘program is the expression of an algorithm
ina programming language. Sometimes words such as procedure, funetion,
and subroutine are used synonymously for program, Most readers of this
book have probably already programmed and rin some algorithms on a
‘computer. This is desirable because before you study a concept in general,
it helps if you had some practical experience with it. Perhaps you had some
difficulty getting started in formulating an initial solution to a problem, or
perhaps you were tmable to decide which of two algoritlms was better. The
goal of titis book is to teach you how to make these decisions.
y important and active areas of
Creating an algorit hin is
may never be fully automated. A major goal of this book is1.1. WHAT IS AN ALGORITHM? 3
ED have proven to be useful in that they have often
yielded good algorithms. By mastering these design strategies, it will become
easier for you to devise new and useful algorithms. Many of the chapters
of this book are organized around what we believe are the major methods
of algorithm design. The reader may now wish to glance back at the table
of contents to soo what these methods are called, Some of these teclniq
may already be familiar, and some have been found to be so useful that
books have been written about them. js one such
technique. Some of the techniques are especially useful in fields other than
computer seience such as operations research and electrical engineering. In
this book we can only hope to give an introduction to these many approaches
to algorithua formulation. All of the approaches we consider have applica-
tions in a variety of areas ineluding computer science. But some important
designt techniques such as linear, nonlinear, and integer programming are not
covered here as they are traditionally covered in other courses.
refer to this proc
1OF as yet be expressed as a program. It is sufficient to state it im any
precise way. ‘The purpose of the validation is to assure us that this algorithm.
will work correctly independently of the issues concerning the programming.
language it will eventually be written in. Once the validity of the method
has heen shown, a program can be written and a second phase begins. This
phase is referred to as program proving or sometimes as program verification.
A proof of correctness requires that the solution be stated in two form
One forms is usually as a program which is annotated by a set of assertions
about the input and output variables of the program. ‘These assertions
are often expressed in the predicate calculus. ‘The second form is called a
specification, and this may also be expressed in the predicate calculus. A
proof consists of showing that, these two forms are equivalent in that for
every given legal input, they describe the same output. A complete proof
of program correctness requires that each statement of the programming
language be precisely defined and all basic operations be proved correct. All
these details may canse a proof to be very much longer than the program.
This field of study is called analysis
of algorithms, is executed, it iter’s central
processing unit to perform operations and its both imime-
diate and auxiliary) to hold the data.
Which sonietimes requires skill, An important result of
this study is that it allows you to make quantitative judgments about the
value of one algorithin over another. Another result is that it allows you to
predict whether the software will meet any efficiency constraints that exist.4 CHAPTER 1, INTRODUCTION
Questions such as how well does an algorithm perform in the best case, in
. or on the average are typical. For each algorithm in the text,
more nl described in Section 1.3.2.
has pointed out, “debugging can only pomt to the presence of errors, but,
not to their absence.” In cases in which we cannot verify the correctness of
output on sample data, the following strategy can be employed: let more
than one programmer develop programs for the same problem, and compare
the outputs produced by these programs. If the outputs match, then there
is a good chance that they are correct. A proof of correctness is much more
valuable than a thousand tests (if that proof is correct), since it
that the program will work correctly for all
‘These timing figures are useful in that they may confirm a previously done
analysis and point out logical places to perform useful optimization. A
description of the measurement of timing complexity can be found in Section
3.5. For some of the algorithms presented here, we show how to devise a
range of data sets that will be useful for debugging and profiling.
‘These four categories serve to outline the questions we ask about algo-
rithms throughout this book. As we can’t hope to cover all these subjects
completely, we content ourselves with concentrating on design and analysis,
spending less time on program construction and correctness.
EXERCISES
1, Look up the words algorism and algorithn
down their meanings.
your dictionary and write
2. ‘The name al-Khowarizmi (algorithm) literally means “from the town
of Khowarazm.” This city is now known as Khiva, and is located in
Urbekistan. See if you can find this country in an atlas.
the WEB to find out more about al-Khowarizimi, .g., his dates, a
cture, or a stamp.