cs32s10dis6 - CS32: Introduction to Computer Science II...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Discussion 1B Notes (Week 7, May 14) TA: Brian Choi ([email protected]) 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 often not a simple 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 Big-O analysis or asymptotic analysis . The question big-O 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? Big-O analysis gives you a rough idea the dependency of running time on the input size. Big-O 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)) “big-O of g(x)”. For example, if O(x 2 ) is read “big-O of x squared”. Big-O is supposed to give you an upper-bound to the function. Here are some examples: Because the way big-O is defined, a function can take on big-O 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: Big-O gives you the upper-bound on the function’s growth. We use big-O to measure the algorithm’s performance. It is the running time of an algorithm with respect to the input size, usually denoted n. If the algorithm’s complexity is known to be O(g(n)), then we say the algorithm requires time proportional to g(n). Now let’s take a look at some algorithms and do some real big-O analysis. CS32: Introduction to Computer Science II Spring 2010 Copyright 2010 Brian Choi Week 7, Page 1/7 f ( x ) is O ( g ( x )) as x →∞ x o ,c> 0 such that f ( x ) cg ( x ) for x>x 0 If f(x) = x 2 +5 x, then f ( x )= O ( x 2 ) . If f(x) = x 2 5 x, then f ( x )= O ( x 2 ) . If f(x) = x 2 5 x, then f ( x )= O ( x 3 ) . If f(x) = 5x 100 + 50 x, then f ( x )= O ( x 100 ) . If f ( x )=5 , then f ( x )= O (1) .
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: Linear Search Problem: Given an (unsorted) array of n elements and a given value v, how would you find the element of the value v in the array, and how long will it take? Because the array is not sorted, the only option for us is to go through all elements one by one, by traversing
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/09/2012 for the course CS 32 taught by Professor Davidsmallberg during the Spring '08 term at UCLA.

Page1 / 7

cs32s10dis6 - CS32: Introduction to Computer Science II...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online