course-notes - Lecture 1 Introduction to Algorithms...

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

View Full Document Right Arrow Icon
Lecture 1 Introduction to Algorithms Analysis I’m assuming you’ve all had CSCE 350 or the equivalent. I’ll assume some basic things from there, and sometimes quickly review the more important/subtle points. We’ll start with Appendix A in CLRS — Summations. Why? They are essential tools in analyzing the complexity of algorithms. A First Example Consider the following two C code fragments: /* Fragment 1 */ sum = 0; for (i=1; i<n; i*=2) for (j=0; j<i; j++) sum++; and /* Fragment 2 */ sum = 0; for (i=1; i<n; i*=2) for (j=i; j<n; j++) sum++; Note the subtle difference. Is there a difference in running time (order-of-magnitude as a function of n )? Yes there is. Sample 1 runs in time Θ( n ) and Sample 2 runs in time Θ( n log n ), so Sample 2 runs significantly longer. [Recall: f = O ( g ) means f ( n ) is at most a constant times g ( n ) for all n large enough. f = Ω( g ) means f ( n ) is at least a (positive) constant times g ( n ) for all n large enough. (Equivalently, g = O ( f ).) f = Θ( g ) means both f = O ( g ) and f = Ω( g ). “ f = Θ( g )” is an equivalence relation between f and g . Also, log n means log 2 n .] Here’s the intuition: in both fragments, the variable i does not run from 1 to n at an even pace. Since it doubles each time, it spends most of its time being very small compared to n , which makes the first j -loop run faster and the second j -loop run slower. Let’s analyze the running times more rigorously. We generally don’t care about constant factors, so it is enough to find, for each fragment, an upper bound and a lower bound that are within a constant factor of each other. This looseness usually makes life a lot easier for us, since we don’t have to be exact. 1
Image of page 1

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

View Full Document Right Arrow Icon
Claim 1 The running time for Fragment 2 is O ( n log n ) . Proof The body of the inner loop ( j -loop) takes O (1) time. Each time it runs, the j -loop iterates n - i n times, for a time of O ( n ) per execution. The outer i -loop runs about log n many times (actually, exactly log n many times). So the total time for the fragment (including initialization, loop testing, and increment) is O ( n log n ). Claim 2 The running time for Fragment 2 is Ω( n log n ) . Proof Note that for all iterations of the i -loop except the last one, the j -loop iterates at least n/ 2 times (because i < n/ 2 and thus n - i > n - n/ 2 = n/ 2). Thus the sum variable is incremented at least n 2 × (log n - 1) times total, which is clearly Ω( n log n ). Claim 3 The running time for Fragment 1 is Ω( n ) . Proof To get a lower bound, we only need to look at the last iteration of the i -loop! (Digression: the late Paul Erd˝ os, arguably the greatest mathematician of the 20th century, once described the art of mathematical analysis as “knowing what information you can throw away.”) The value of i in the last i -loop iteration must be at least n/ 2. In this iteration (as in all iterations), j runs from 0 through i - 1, so the sum variable is incremented i n/ 2 times.
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