Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Asymptotes: Why? How to describe an algorithm’s running time? (or space, …) How does the running time depend on the input? T(x) = running time for instance.

Similar presentations


Presentation on theme: "1 Asymptotes: Why? How to describe an algorithm’s running time? (or space, …) How does the running time depend on the input? T(x) = running time for instance."— Presentation transcript:

1 1 Asymptotes: Why? How to describe an algorithm’s running time? (or space, …) How does the running time depend on the input? T(x) = running time for instance x Problem: Impractical to use, e.g., “15 steps to sort [3 9 1 7], 13 steps to sort [1 2 0 3 9], …” Need to abstract away from the individual instances.

2 2 Asymptotes: Why? Standard solution: Abstract based on size of input. Problem: Time also depends on other factors. E.g., on sortedness of array. How does the running time depend on the input? T(n) = running time for instances of size n

3 3 Asymptotes: Why? Solution: Provide a bound over these instances. Worst caseT(n) = max{T(x) | x is an instance of size n} Best caseT(n) = min{T(x) | x is an instance of size n} Average caseT(n) =  |x|=n Pr{x}  T(x) Most common. Default. Determining the input probability distribution can be difficult.

4 4 Asymptotes: Why? What’s confusing about this notation? Two different kinds of functions: T(instance) T(size of instance) Won’t use T(instance) notation again, so can ignore. Worst caseT(n) = max{T(x) | x is an instance of size n} Best caseT(n) = min{T(x) | x is an instance of size n} Average caseT(n) =  |x|=n Pr{x}  T(x)

5 5 Asymptotes: Why? Problem: T(n) = 3n 2 + 14n + 27 Too much detail: constants may reflect implementation details & lower terms are insignificant. n3n 2 14n+17 1331 10300157 10030,0001,417 10003,000,00014,017 10000300,000,000140,017 Solution: Ignore the constants & low-order terms. (Omitted details still important pragmatically.) 3n 2 > 14n+17  “large enough” n

6 6 Upper Bounds Creating an algorithm proves we can solve the problem within a given bound. But another algorithm might be faster. E.g., sorting an array. Insertion sort  O(n 2 ) As shown in COMP 280.

7 7 Lower Bounds Sometimes can prove that we cannot compute something without a sufficient amount of time. That doesn't necessarily mean we know how to compute it in this lower bound. E.g., sorting an array. # comparisons needed in worst case   (n log n) Will prove this soon…

8 8 Definitions: O,  T(n) T(n)  O(g(n))   constants c,n 0 > 0 such that  n  n 0, T(n)  c  g(n) c  g(n) n0n0 T(n)   (g’(n))   constants c’,n’ 0 > 0 such that  n  n’ 0, T(n)  c’  g’(n) c’  g’(n) n’ 0

9 9 Examples: O,  2n+13  O( ? ) O(n) Also, O(n 2 ), O(5n), … Can always weaken the bound. 2 n  O(n) ?  (n) ? Given a c, 2 n  c  n, for all but small n.  (n), not O(n). n log n  O(n 5 ) ? No. Given a c, log n  c  5, for all large enough n. Thus,  (n 5 ). 2n+13   ( ? )  (n), also  (log n),  (1), …

10 10 Definitions:  T(n)   (g(n))  T(n)  O(g(n)) and T(n)   (g(n)) Ideally, find algorithms that are asymptotically as good as possible.

11 11 Notation O(),  (), … are sets of functions. But common to abuse notation, writing T(n) = O(…) instead of T(n)  O(…) as well as T(n) = f(n) + O(…)


Download ppt "1 Asymptotes: Why? How to describe an algorithm’s running time? (or space, …) How does the running time depend on the input? T(x) = running time for instance."

Similar presentations


Ads by Google