Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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's time 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 = sum I (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 1.Explain O(1) 2. 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))) 3. 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)) 4. Page Page


Download ppt "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."

Similar presentations


Ads by Google