CS3110 Recitation 18
Review of Asymptotic Complexity
•
Asymptotic Analysis
•
WorstCase and AverageCase Analysis
•
Order of Growth and BigO Notation
•
Comparing Orders of Growth
•
Binary Search Trees
•
Exercises
Asymptotic Analysis
When analyzing the running time or space usage of programs, we usually try to estimate the time or space as
function of the input size. For example, when analyzing the worst case running time of a function that sorts a
list of numbers, we will be concerned with how long it takes as a function of the length of the input list. For
example, we say the standard insertion sort takes time
T
(
n
) where
T
(
n
)
= c*n
2
+k
for some constants
c and k
.
In contrast, merge sort takes time
T
′(
n
)
= c
′
*n*log
2
(
n
) +
k
′
.
The
asymptotic
behavior of a function
f(n)
(such as
f(n)=c*n
or
f(n)=c*n
2
, etc.) refers to the growth of
f(n)
as
n
gets large. We typically ignore small values of
n
, since we are usually interested in estimating how slow
the program will be on large inputs. A good rule of thumb is: the slower the asymptotic growth rate, the
better the algorithm (although this is often not the whole story).
By this measure, a linear algorithm (
i.e., f(n)=d*n+k
) is always asymptotically better than a quadratic one
(
e.g., f(n)=c*n
2
+q
). That is because for any given (positive)
c,k,d
, and
q
there is always some
n
at which the
magnitude of
c*n
2
+q
overtakes
d*n+k
. For moderate values of
n
, the quadratic algorithm could very well
take less time than the linear one, for example if
c
is significantly smaller than
d
and/or
k
is significantly
smaller than
q
. However, the linear algorithm will always be better for sufficiently large inputs. Remember to
THINK BIG
when working with asymptotic rates of growth.
WorstCase and AverageCase Analysis
When we say that an algorithm runs in time
T(n)
, we mean that
T(n)
is an upper bound on the running time
that holds for all inputs of size
n
. This is called
worstcase analysis
. The algorithm may very well take less
time on some inputs of size
n
, but it doesn't matter. If an algorithm takes
T(n)=c*n
2
+k
steps on only a single
input of each size
n
and only
n
steps on the rest, we still say that it is a quadratic algorithm.
A popular alternative to worstcase analysis is
averagecase analysis
. Here we do not bound the worst case
running time, but try to calculate the expected time spent on a randomly chosen input. This kind of analysis is
generally harder, since it involves probabilistic arguments and often requires assumptions about the
distribution of inputs that may be difficult to justify. On the other hand, it can be more useful because
sometimes the worstcase behavior of an algorithm is misleadingly bad. A good example of this is the
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
popular quicksort algorithm, whose worstcase running time on an input sequence of length
n
is proportional
to
n
2
but whose expected running time is proportional to
n
log
n.
This is the end of the preview.
Sign up
to
access the rest of the document.
 '07
 GIAMBATTISTA,A
 Binary Search, Big O notation, Analysis of algorithms

Click to edit the document details