1 / 7

Time Analysis

Time Analysis. Since the time it takes to execute an algorithm usually depends on the size of the input, we express the algorithm's time complexity as a function of the size of the input

sahkyo
Download Presentation

Time Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Time Analysis • Since the time it takes to execute an algorithm usually depends on the size of the input, we express the algorithm'stime complexity as a function of the size of the input • Question: For a given algorithm, will the same inputs size result in the same number of steps to run the program? • Yes or no?

  2. No • Consider a linear search on an array that does not contain the target as opposed to an array where the target is the first element

  3. Time Analysis (cont’d) • Best case analysis • Given the algorithm and input of size n that makes it run fastest (compared to all other possible inputs of size n), what is the running time? • Worst case analysis • Given the algorithm and input of size n that makes it run slowest (compared to all other possible inputs of size n), what is the running time? • A bad worst-case complexity doesn't necessarily mean that the algorithm should be rejected • Average case analysis • Given the algorithm and a typical, average input of size n, what is the running time?

  4. Worst-Case Analysis • Worst case running time: Obtain bound on largest possible running time of algorithm on input of a given size n • Generally captures efficiency in practice • Average case running time: Obtain bound on running time of algorithm on random input as a function of input size n • Cavg = sumI(P(input_i)*step(input_i)) • Hard (or impossible) to accurately model real instances by random distributions • Algorithm tuned for a certain distribution may perform poorly on other inputs

  5. Time Analysis (cont’d) • Example: Consider searching sequentially an unordered array to find a number • Best Case? O(1) • Worst Case? O(n) • Average Case? O(n) why?

  6. Example • Binary search • Best case complexity • Worst case complexity

  7. Chap 2 Exercise • Explain O(1) • Assume f1(n) is O(g1(n)) and f2(n) is o(g2(n)), prove that f1(n) + f2(n) is O(max(g1(n), g2(n))) • Find functions f1 and f2 such that both f1(n) and f2(n) are O(g(n)), but f1(n) is not O(f2(n)) • Page 74 10. • Page 74 11.

More Related