Chapter
1.
Algorithm Analysis
In
a classic story, the famous mathematician Archimedes was asked
to
deter-
mine
if
a golden crown commissioned by the king was indeed pure gold, and not
part silver, as an informant had claimed. Archimedes discovered a way to determine
this while stepping into a (Greek) bath. He noted that water spilled out
of
the bath
in proportion to the amount
of
him that went in. Realizing the implications
of
this
fact, he immediately got out
of
the bath and ran naked through the city shouting,
"Eureka, eureka!," for he had discovered an analysis tool (displacement), which,
when combined with a simple scale, could determine
if
theking's
new crown was
good or not. This discovery was unfortunate for the goldsmith, however,
for.
when
Archimedes did his analysis, the crown displaced more water than an equal-weight
lump
of
pure gold, il1dicating that the crown was not, in fact, pure gold.
In this book, we are interested in the design
of
"good" algorithms and data
structures. Simply put,
an
algorithm is a step-by-step procedure for performing
some
task
in a finite amount
of
time, and a data structure is a systematic way
of
organizing and accessing data. These concepts are central
to
computing, but to
be able to classify some algorithms and data structures as "good," we must have
precise ways
of
analyzing them.
The primary analysis tool we will llse
in
this book involves characterizing the
running times
of
algorithms and data structUre operations, with space usage also
being
of
interest. Running time is a natural measure
of
"goodness," since time is a
precious resource. But focusing on running time as a primary measure
of
goodness
implies that we will need to use at least a Ijttle mathematics to describe running
timesWld. compare algorithms.
We
begin this chapter by describing the basic framework needed for analyzing
algorithms, which includes the language for describing algorithms, the computa-
tiOllal
model that language· is intended for, and the main factors we count when
considering running time. We also include a brief discussion
of
how recursive al-
gorithms
3.J:e
analyzed. In Section 1.2, we presentthe main notation
w"e
use to char-
acterize running
times-the
so-called "big-Oh" notation. These tools comprise the
(main
theoretical tools for designing and analyzing algorithms .
.
In
Section 1.3, we take a short break from our development
of
the framework·
for algorithm analysis to review some important mathematical facts, including dis- .
cussions
of
summations, logarithms, proof techniques, and basic probability.
Givel}.
this background and our notation for algorithm analysis, we present some case 'stud-
ies on theoretical algorithm analysis in Section 1.4. We follow these examples in
Section
1.5
by presenting
an
interesting analysis technique, known as amortization,
which allows us to account for the group behavior
of
many.individual operations.
Finally, in Section 1.6, we conclude the chapter by discussing an important and
practical analysis
techniqu~xperimentation.
We
discuss both the main princi-
ples
of
a good experimental
frarn~work
as well as techniques for summarizing and
characterizing data from an exper4nental analysis.
\
'i.
f.·
:;