This preview shows pages 1–2. Sign up to view the full content.
Discussion 3A Notes (Week 7, February 20)
TA: Brian Choi (schoi@cs.ucla.edu)
Section Webpage:
http://www.cs.ucla.edu/~schoi/cs32
Algorithm Complexity
So far we have been mainly concerned with getting things done. From now on we will talk about getting
things done
with efficiency
. Writing programs that make use of available resources (e.g. time and memory
space) wisely is not an easy task. Because memory and hard disks are cheap these days, we’re more interested
in how fast
a code can run than how much memory it takes.
To start with, we need to know how to measure the “efficiency.” This is done through what is called
BigO
analysis
or
asymptotic analysis
. The question bigO analysis attempts to answer is as follows:
Given an input of size n, approximately how long does the algorithm take to finish the task?
It is clear that most known algorithms take longer if the input size n is large. Think of sorting 10 items in an
array, and sorting 1000 items in an array. Apparently, it will take must longer to sort 1000 items, but how
longer? BigO analysis gives you a rough idea the dependency of running time on the input size.
BigO Notation
Let’s start with a formal definition.
Let f(x) and g(x) be two functions of real numbers. Then we say:
if and only if
We read O(g(x)) “bigO of g(x)”. For example, if O(x
2
) is read “bigO of x squared”.
BigO is supposed to give you an upperbound to the function.
Here are some examples:
Because the way bigO is defined, a function can take on bigO notations. f(x) = x
2
+ 5x is O(x
2
), O(x
3
),
O(x
4
), and so on, at the same time. However, we are mostly interested in the tightest bound we can find.
In any case, you don’t have to remember this formal definition. But just remember that:
BigO gives you the
upperbound
on the function’s growth.
We use bigO to measure the algorithm’s performance. It is the running time of an algorithm with respect
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview. Sign up
to
access the rest of the document.
 Winter '08
 DavidSmallBerg
 Computer Science

Click to edit the document details