cs32w11dis6 - CS32 Introduction to Computer Science II...

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

View Full Document Right Arrow Icon
Discussion 2C Notes (Week 7, February 18) TA: Brian Choi ([email protected]) Section Webpage: 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 an economic use of available resources (e.g. time and memory space) is often not a simple task (Because memory and hard disks are cheap these days, we’re usually more interested in how fast a code can run than how much memory it takes). How can we measure this “efficiency”? This is done through what is called Big-O analysis or asymptotic analysis . The question Big-O analysis attempts to answer is the following: 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 larger -- without thinking much, you know sorting 1000 items in an array will take more time than sorting 10 items in an array. But how longer? If sorting 10 items took your program 10ms, will sorting 1000 items take 1 second? Or 10 seconds? Big-O analysis gives you a rough idea about how the running time of your program is related to 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 the 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 us take a look at some algorithms and do some real big-O analysis. CS32: Introduction to Computer Science II Winter 2011 Copyright 2011 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) .
Image of page 1

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

View Full Document Right 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 the array. Here’s a C++-like pseudocode: linear_search (array arr[], size n, value v) { for (i = 0 to n-1) { if (arr[i] == v) return i } return -1 } Try big-O analysis for three cases: the best case, average case, and worst case.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern