3/18/2009
1
COMPUTER SCIENCE 51
Spring 2009
cs51.seas.harvard.edu
Prof. Greg Morrisett
Prof. Ramin Zabih
Today
•
Complexity of algorithms
–
–
asymptotic growth, bigO notation
–
recurrences
–
HighLevel Goal
•
–
why is mergesort “better” than insertionsort?
–
why is a rbtree “better” than an association list?
–
why is it “better” to use foldl than foldr to sum a
list?
•
One possible comparison:
–
–
problems?
Problems with Measuring
•
If you measured the running time as a
function of the input list length n, you’d see
that:
–
insertionsort runs in worstcase time
k
1
* n
2
+ k
2
for some constants k
1
and k
2
.
–
mergesort runs in worstcase time
c
1
*n*lg
2
n + c
2
for constants c
1
and c
2
.
•
So times are different for different inputs.
•
The constants depend upon many factors:
–
–
The machine used
–
What other processes are running
–
The temperature, your altitude, .
..
Plotting it out
T
ms
(n) = 2*n*lg
2
n + 30
T
is
(n) = 1*n
2
+ 0
Shifting the Curves
T
ms
(n) =
4
*n*lg
2
n + 30
T
is
(n) = 1*n
2
+ 0
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document3/18/2009
2
Asymptotic Complexity
•
Given the two functions:
–
T
is
(n) = k
1
* n
2
+ k
2
and
T
ms
(n) = c
1
*n*lg
2
n + c
2
•
as n grows towards infinity T
is
grows “faster”
than T
ms
regardless
of the constants.
–
in contrast, T(n) = q
1
*n
2
+ q
2
*n + q
3
does
not
grow faster than T
is
independent of the
constants.
•
If we can formalize “faster”, then we have a
robust
way to compare algorithms.
–
Won’t depend upon language, compiler,
machine, OS, etc. as long as these only affect
the constants.
Our Goals
•
Examine a piece of code and determine
it’s worst case running time (or space) as a
function of its inputs.
–
without having to measure it.
–
obviously, in terms of symbolic constants.
–
e.g., T
ms
(n) = c
1
*n*lg
2
n + c
2
•
Be able to compare the symbolic running
time (space) of two functions and
determine which one is asymptotically
better.
–
without resorting to plots.
–
For this, we will utilize bigO notation.
Formal BigO notation
•
Given f : number
→
number:
•
O(f) is the set of all functions g such that:
–
for all n > 0,
–
there exists a number c such that
–
g(n) ≤ c * f(n)
•
If f
∈
O(g) but g is not in O(f), then g grows
faster than f.
–
we’ll write g >> f to reflect this.
•
Example:
–
I claim that
λ
n.10*n
2
+3
∈
O(
λ
n.n
2
)
–
But not in O(
λ
n.n*lg
2
n)
Arguing bigO
•
Claim that
λ
n.10*n
2
+3
∈
O(
λ
n.n
2
)
–
must find a constant c such that for all
n > 0, 10*n
2
+3 ≤ c * n
2
•
When n=1: (
λ
n.10*n
2
+3) 1 = 10*1 + 3 = 13, so need
13 ≤ c
•
When n=2: (
This is the end of the preview. Sign up
to
access the rest of the document.
 Spring '09
 GREGMORRISETT

Click to edit the document details