04-1/26
•
Algorithm costs: time, space, and energy
•
Best case, worst case, average case analysis
•
Counting instructions and asymptotic analysis
•
Big-O, big-Omega, big-Theta notation
•
Introduction to algorithm measurement
CSE 12
Analysis and Measurement of Algorithms
Writing good software
•
Keep in mind the characteristics of good software:
–
robustness
: a program’s ability to spot exceptional
conditions and deal with them or shutdown
gracefully
–
correctness
: does the program do what it is
supposed to do?
–
efficiency
: all programs use resources (time and
space and energy, i.e. CPU cycles and memory
and battery or wall power); how can we measure
efficiency so that we can compare algorithms?
Cost functions
An algorithm’s performance can be described by:
–
time
complexity or cost – how long it takes to
execute. In general, less time is better!
–
space
complexity or cost – how much computer
memory it uses.
In general, less space is better!
–
energy
complexity or cost – how much energy
uses.
In general, less energy is better!
•
Costs are usually given
as functions of the size of the
input
to the algorithm
•
A big instance of the problem will probably take more
resources to solve than a small one, but how much
more?
Determining algorithm costs
•
For a given algorithm, if the size of the input
is
n
, we
would like to know:
x
T(
n
) , the time cost of solving the problem
x
S(
n
) , the space cost of solving the problem
x
E(
n
) , the energy cost of solving the problem
•
Two approaches:
–
We could implement the algorithm and run it and
measure
the time, memory, and energy usage
–
Or we can
analyze
the written algorithm
Algorithm cost analysis
•
To perform algorithm time cost analysis:
–
Write down the algorithm that solves the problem
–
Decide what “size of the problem” means for this kind of problem
–
Count
up the number of instructions the algorithm would execute,
as a function of
n
, the size of the problem it is solving
•
Note that different languages for writing the same
algorithm might require a different number of instructions
–
Java will probably require fewer than C which will require fewer
than assembly language, etc.
•
So, a
precise
count of the number of instructions is,
arguably, too much detail… we will need to consider how
to abstract away from nonessential detail
Algorithm cost cases
•
Sometimes it’s useful to distinguish among different
cases
that can occur when running a given algorithm to
solve a problem of size
n
•
Best case
:
for all inputs of size
n
, which one has the
lowest cost?