70 likes | 156 Views
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
E N D
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?
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
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?
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
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?
Example • Binary search • Best case complexity • Worst case complexity
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.