Docsity

Intro Algorithms: Analysis, Lecture notes of Algorithms and Programming

Partial preview of the text

Download Intro Algorithms: Analysis and more Lecture notes Algorithms and Programming in PDF only on Docsity!

CSE 421: Intro Algorithms

2: Analysis

Larry Ruzzo

outline

Why big-O: measuring algorithm efficiency

What’s big-O: definition and related concepts

Reasoning with big-O: examples & applications

polynomials exponentials logarithms sums

Polynomial Time

efficiency Our correct TSP algorithm was incredibly slow Basically slow no matter what computer you have As a 2 nd example, for large problems, quicksort is much faster that insertion sort, again no matter what computer you have – n log n vs n 2 matters a lot We want a general theory of “efficiency” that is Simple Objective Relatively independent of changing technology But still predictive – “theoretically bad” algorithms should be bad in practice and vice versa (usually)

defining efficiency “Runs fast on typical real problem instances” Pro: sensible, bottom-line-oriented Con: moving target (diff computers, compilers, Moore’s law) highly subjective (how fast is “fast”? What’s “typical”?)

defining efficiency

Instead:

a) Give up on detailed timing, focus on scaling Nanoseconds matter of course, but we often want to push to bigger problems tomorrow than we can solve today, so an algorithm that scales as n 2 , say, will very likely beat one that grows as 2 n or n 10 or even n 3 , even if the later uses fewer nanoseconds for today’s n. b) Give up on “typical,” focus on worst case behavior Over all inputs of size n, how fast are we on the worst? Removes all debate about “typical” / “average.” Overall, these yield a big win in terms of technology independence, ease of analysis, robustness

computational complexity

The time complexity of an algorithm associates

a number T(n), the worst-case time the

algorithm takes, with each problem size n.

Mathematically,

T: N+ → R

i.e.,T is a function mapping positive integers (problem sizes) to positive real numbers (number of steps). “Reals” so, e.g., we can say sqrt(n) instead of ⎡sqrt(n)⎤ “Positive” so, e.g., log(n) and 2 n /n aren’t problematic

why worst-case analysis? Appropriate for time-critical applications E.g. avionics, nuclear reactors Unlike Average-Case, no debate about what the right definition is If worst ≫ average, then (a) alg is doing something pretty subtle, & (b) are hard instances really that rare? Analysis often much easier Result is often representative of “typical” problem instances Of course there are exceptions…

computational complexity: general goals Asymptotic growth rate, i.e., characterize growth rate of worst-case run time as a function of problem size, up to a constant factor, e.g. T(n) = O(n 2 ) Why not try to be more precise? Average-case, e.g., is hard to define, analyze Technological variations (computer, compiler, OS, …) easily 10x or more Being more precise is much more work A key question is “scale up”: if I can afford this today, how much longer will it take when my business is 2x larger? (E.g. today: cn 2 , next year: c(2n) 2 = 4cn 2 : 4 x longer.)  Big-O analysis is adequate to address this.

O-notation, etc. Given two functions f and g: N+ → R f(n) is O(g(n)) iff there is a constant c > 0 so that f(n) is eventually always ≤ c g(n) f(n) is Ω(g(n)) iff there is a constant c > 0 so that f(n) is eventually always ≥ c g(n) f(n) is Θ(g(n)) iff there is are constants c 1 , c 2

0 so that  eventually always c 1 g(n) ≤ f(n) ≤ c 2 g(n) “Eventually always P(n)” means “∃n 0 s.t.∀n>n 0 P(n) is true.” I.e., there can be exceptions, but only for finitely many “small” values of n. Upper Bounds Lower Bounds Both

Reasoning with big-O: examples & applications polynomials exponentials logarithms sums

0 2 4 6 8 10 12 0 200 400 600 800 1000 1200 1400 examples

Show 10n

2

-16n+100 is Ω(n

2

10n 2 -16n+100 ≥ 10n 2

  • 16n ≥ 10n 2
  • n 2 = 9n 2 for all n ≥ 16  ∴ Ω(n 2 ) [ and also Ω(n), Ω(n

), … ] Therefore also 10n 2 -16n+100 is Θ(n 2 )  [but not Θ(n

) or Θ(n

) ]

asymptotic bounds for polynomials Polynomials:  p(n) = a 0

  • a 1 n + … + a d n d is Θ(n d ) if a d

Proof: p(n) = a 0

  • a 1 n + … + a d n d ≤ |a 0 | + |a 1 |n + … + a d n d ≤ |a 0 |n d
  • |a 1 |n d
  • … + a d n d (for n ≥ 1) = c n d , where c = (|a 0 | + |a 1 | + … + |a d- | + a d ) ∴ p(n) = O(n d ) Exercise: show that p(n) = Ω(n d ) Hint: this direction is trickier; focus on the “worst case” where all coefficients except a d are negative.

more examples: tricks for sums

Example: ∑

1 ≤ i ≤ n

i = Θ(n

2

Proof:

(a) An upper bound: each term is ≤ the max term ∑ 1 ≤ i ≤ n i ≤ ∑ 1 ≤ i ≤ n n = n 2 = O(n 2 ) (b) A lower bound: each term is ≥ the min term ∑ 1 ≤ i ≤ n i ≥ ∑ 1 ≤ i ≤ n 1 = n = Ω(n) This is valid, but a weak bound. Better: pick a large subset of large terms

1 ≤ i ≤ n

i ≥ ∑

n/2 ≤ i ≤ n

n/2 ≥ ⎣n/2⎦

2

= Ω(n

2

properties Transitivity. If f = O(g) and g = O(h) then f = O(h). If f = Ω(g) and g = Ω(h) then f = Ω(h). If f = Θ(g) and g = Θ(h) then f = Θ(h). Additivity. If f = O(h) and g = O(h) then f + g = O(h). If f = Ω(h) and g = Ω(h) then f + g = Ω(h). If f = Θ(h) and g = O(h) then f + g = Θ(h). Proofs are left as exercises.