Computer Science 211
Data Structures
Mount Holyoke College
Fall 2009
Topic Notes: Complexity and Asymptotic Analysis
Having now studied one major abstract data type, the
Vector
, we will now step back and look at
important efficiency issues before moving on to more complicated and interesting structures.
Consider these observations:
•
A programmer can use a
Vector
in contexts where an array could be used.
•
The
Vector
hides some of the complexity associated with inserting or removing values
from the middle of the array, or when the array needs to be resized.
•
As a user of a
Vector
, these potentially expensive operations all seem very simple – it’s
just a method call.
•
But.
. programmers who make use of abstract data types need to be aware of the actual costs
of the operations and their effect on their program’s efficiency.
We will now spend some time looking at how Computer Scientists measure the costs associated
with our structures and the operations on those structures.
Costs of
Vector
Operations
When considering
Vector
implementations, we considered two ways to “grow”
Vector
s that
need to be expanded to accomodate new items.
•
When growing by 1 at a time, we saw that to add
n
items, we would have to copy
n
*
n

1
2
items between copies of the array inside the
Vector
implementation.
•
When we doubled the size of the array each time it needed to be expanded, we would have
to copy a total of
n

1
items.
These kinds of differences relate to the tradeoffs made when developing algorithms and data struc
tures. We could avoid all of these copies by just allocating a huge array, larger than we could ever
possibly need, right at the start. That would be very efficient in terms of avoiding the work of
copying the contents of the array, but it is very inefficient in terms of memory usage.
This is an example of a
time vs. space tradeoff
. We can save some time (do less computing) by
using more space (less memory). Or vice versa.
We also observe that the cost to add an element to a
Vector
is not constant! Usually it is – when
the
Vector
is already big enough – but in those cases where the
Vector
has to be expanded, it
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentCS 211
Data Structures
Fall 2009
involves copying over all of the elements already in the
Vector
before adding the new one. This
cost will depend on the number of elements in the
Vector
at the time.
The cost of inserting or removing an element from the middle or beginning of a
Vector
always
depends on how many elements are in the
Vector
after the insert/remove point.
Asymptotic Analysis
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '09
 Data Structures, Big O notation, Analysis of algorithms, Computational complexity theory

Click to edit the document details