C
OMPLEXITY
AND
B
IG
O N
OTATION
Contents
z
Introduction
{
Test
Yourself #1
{
Test Yourself #2
z
Big

O Notation
z
How to Determine
Complexities
{
Test Yourself #3
{
Test Yourself #4
z
Best

case
and Average

case Complexity
z
When do Constants
Matter?
Introduction
An important question is: How efficient is an algorithm or piece of code? Efficiency covers lots of
resources, including:
z
CPU (time) usage
z
memory usage
z
disk usage
z
network usage
All are important but we will mostly talk about CPU time in 367. Other classes will discuss other
resources (e.g., disk usage may be an important topic in a database class).
Be careful to differentiate between:
1. Performance: how much time/memory/disk/.
.. is actually used when a program is run. This
depends on the machine, compiler, etc. as well as the code.
2. Complexity: how do the resource requirements of a program or algorithm scale, i.e., what
happens as the size of the problem being solved gets larger.
Complexity affects performance but not the other way around.
The time required by a method is proportional to the number of "basic operations" that it performs.
Here are some examples of basic operations:
z
one arithmetic operation (e.g., +, *).
z
one assignment
z
one test (e.g., x == 0)
z
one read
z
one write (of a primitive type)
Page 1 of 10
Complexity and BigO Notation
2008/3/27
http://pages.cs.wisc.edu/~cs3671/topics/Complexity/
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentSome methods perform the same number of operations every time they are called. For example, the
size
method of the
List
class always performs just one operation:
return numItems
; the number of
operations is independent of the size of the list. We say that methods like this (that always perform a
fixed number of basic operations) require
constant time
.
Other methods may perform different numbers of operations, depending on the value of a parameter
or a field. For example, for the array implementation of the
List
class, the
remove
method has to
move over all of the items that were to the right of the item that was removed (to fill in the gap). The
number of moves depends both on the position of the removed item and the number of items in the
list. We call the important factors (the parameters and/or fields whose values affect the number of
operations performed) the
problem size
or the
input size
.
When we consider the complexity of a method, we don't really care about the
exact
number of
operations that are performed; instead, we care about how the number of operations relates to the
problem size. If the problem size doubles, does the number of operations stay the same? double?
increase in some other way? For constanttime methods like the
size
method, doubling the problem
size does not affect the number of operations (which stays the same).
Furthermore, we are usually interested in the
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '06
 OnlineResources
 BigO Notation, Analysis of algorithms, Computational complexity theory, CPU time, CreateList

Click to edit the document details