Analyzing Running Time (Chapter 2) What is efficiency? Tools: - - PowerPoint PPT Presentation

analyzing running time chapter 2
SMART_READER_LITE
LIVE PREVIEW

Analyzing Running Time (Chapter 2) What is efficiency? Tools: - - PowerPoint PPT Presentation

Analyzing Running Time (Chapter 2) What is efficiency? Tools: asymptotic growth of functions Practice finding asymptotic running time of algorithms Is My Algorithm Efficient? Idea: Implement it, time how long it takes. Problems? Effects of


slide-1
SLIDE 1

Analyzing Running Time (Chapter 2)

What is efficiency? Tools: asymptotic growth of functions Practice finding asymptotic running time of algorithms

slide-2
SLIDE 2

Is My Algorithm Efficient?

Idea: Implement it, time how long it takes. Problems? Effects of the programming language? Effects of the processor? Effects of the amount of memory? Effects of other things running on the computer? Effects of the input values? Effects of the input size?

slide-3
SLIDE 3

Worst-Case Running Time

Worst-case running time: bound the largest possible running time on any input of size N Seems pessimistic, but: Effective in practice Hard to find good alternative (e.g., average case analysis)

slide-4
SLIDE 4

Brute Force

Good starting point for thinking about efficiency: can we do better than a “brute force” solution? What is the brute force solution for the stable matching problem?

slide-5
SLIDE 5

Worst-case: Gale-Shapley vs. Brute Force

# Colleges N G-S N2 Brute Force N!

4 8 16 16 64 256 24 40,320 20,922,789,888,000

slide-6
SLIDE 6

Working Definition of Efficient

Efficient = better than brute force Desired property: if input size increases by constant factor algorithm slows down by a constant factor E.g. size N -> 2N, time T -> 4T

slide-7
SLIDE 7

Polynomial Time

Definition: an algorithm runs in polynomial time if Number of steps is at most c * Nd, where N is input size, and c and d are constants Does this satisfy desired property?

If there is no polynomial time solution, we say there is no efficient solution.

slide-8
SLIDE 8

Running Times as Functions of Input Size

slide-9
SLIDE 9

Asymptotic Growth: Big O(), Ω(), Θ()

Goal: build tools to help coarsely classify algorithm’ s running times Running time = number of primitive “steps” (e.g., line of code or assembly instruction) Coarse: is too detailed

1.62n2 + 3.5n + 8

slide-10
SLIDE 10

Notation

Let T(n) be a function that defines the worst-case running time of an algorithm. For the remainder of the lecture, we assume that all functions T(n), f(n), g(n), etc. are nonnegative

slide-11
SLIDE 11

Big O Notation

T(n) is O(f(n)) if T(n) ≤ c · f(n), where c ≥ 0 for all n ≥ n0 (Example on board) O(n) is the asymptotic upper bound of T(n).

slide-12
SLIDE 12

3n + 2 4n n0 = 2

Visualizing Asymptotics

T(n) = 3n + 2 is O(n)

slide-13
SLIDE 13

Ω Notation

T(n) is Ω(f(n)) if T(n) ≥ c · f(n), where c ≥ 0 for all n ≥ n0 (Example on board) Ω(n) is the asymptotic lower bound of T(n).

slide-14
SLIDE 14

3n + 2 n n0 = 0

Visualizing Asymptotics

T(n) = 3n + 2 is Ω(n)

slide-15
SLIDE 15

Θ Notation

T(n) is Θ(f(n)) if T(n) is O(n) and T(n) is Ω(n) (Example on board) Θ(n) is the asymptotic tight bound of T(n).

slide-16
SLIDE 16

3n + 2 n

Visualizing Asymptotics

T(n) = 3n + 2 is Θ(n) 4n

slide-17
SLIDE 17

O(), Ω(), Θ()

O() - upper bound T(n) ≤ c · f(n), where c ≥ 0 for all n ≥ n0 Ω() - lower bound T(n) ≥ c · f(n), where c ≥ 0 for all n ≥ n0 Θ() - tight bound Both O() and Ω()

slide-18
SLIDE 18

Properties of O(), Ω(), Θ()

slide-19
SLIDE 19

Transitivity

Claim: (a) If f = O(g) and g = O(h), then f = O(h) (b) If f = Ω(g) and g = Ω(h), then f = Ω(h) (b) If f = Θ(g) and g = Θ(h), then f = Θ(h) Prove (a) on board

slide-20
SLIDE 20

Additivity

Claim: (a) If f = O(h) and g = O(h), then f+g = O(h) (b) If f1, f2, ..., fk are each O(h), then f1 + f2 + ...+ fk is O(h) (c) If f = O(g), then f+g = Θ(g) Prove (a) on board; discuss (c)

slide-21
SLIDE 21

Asymptotic Bounds For Common Functions

slide-22
SLIDE 22

Polynomial Time

Claim: Let T(n) = c0 + c1n + c2n2 + ... + cdnd, where cd is positive. Then T(n) is Θ(nd). Proof: repeated application of the additivity rule (c) New definition of polynomial-time algorithm: running time T(n) is O(nd)

slide-23
SLIDE 23

Logarithm Review

Defn: is the unique number c s.t.

logb(a) bc = a

“log base b of a”: Informally, the number of times you can divide a into b parts before each has size one Facts:

logb(bn) = n blogb (n) = n loga(n) = logb(n) logb(a)

loga(n) = Θ(logb(n))

slide-24
SLIDE 24

Other Asymptotic Orderings

Logarithms: logan is O(nd), for all bases a and all degrees d ➔ All logarithms grow slower than all polynomials

25 50 75 100 1 2 3 4 5 6 7 8 9 10

y = x2 y = log2 x

slide-25
SLIDE 25

Other Asymptotic Orderings

Exponential functions: nd is O(rn) when r > 1 ➔ Polynomials grow no more quickly than exponential functions.

y = x2 y = 2x

375 750 1125 1500 1 2 3 4 5 6 7 8 9 10

slide-26
SLIDE 26

A Harder Example

Which of these grows faster? n4/3 n(log n)3

slide-27
SLIDE 27

Recap

What you should know Polynomial time = efficient Definitions of O(), Ω(), Θ() Transitivity and additivity. Basic proof techniques How to “sort” functions: log(n), polynomials, n*log(n), exponentials