Transcript Chapter 6
CS 345: Chapter 6 Efficiency of Algorithms Winter, 2016 Complexity Measures of Time and Space • In Computer Science, there is almost always a trade-off between time and space. • One type of measurement for time and space requirements is called an Asymptotic Measure. • There are 6 asymptotic measures; we will consider 3 of them. Big-O: The Upper Bound f(n) = O(g(n)) positive constants c and n0 0 f(n) cg(n), n n0 Big-Omega: The Lower Bound f(n) = (g(n)) positive constants c and n0 0 cg(n) f(n), n n0 Big-Theta: The Tight Bound f(n) = (g(n)) positive constants c1, c2, and n0 0 c1g(n) f(n) c2g(n), n n0 Note: f(n) = (g(n)) iff f(n) = (g(n)) and f(n) = (g(n)) Other Considerations • It is important to remember that an asymptotic measure relates to growth rate. • An algorithm that is (n2) may be faster on small data sets than an algorithm that is (n log n). • It is important to remember that n is some measure of the input size. That is, asymptotic measures relate growth rate to input size. Calculating Time Complexity • What constitutes an input size? This is not always an obvious value. • For an iterative algorithm, generally count loops. For example, Bubble Sort or Warshall’s algorithm. • For a recursive algorithm, solve a recurrence relation. For example, Towers of Hanoi. Making Improvements • Compiler Optimizations (tweaking) – – – – Removing constants from loops. Constant folding. Loop Unrolling. Strength Reduction. • Note, however, tweaking does not change the asymptotic measure of an algorithm. Bounding An Algorithmic Problem • Given an algorithmic problem P, a lower bound on P is a proof that any algorithm, even one that has not been discovered yet, will require at least this many steps. • An upper bound on P means that an algorithm has been demonstrated that takes not more than this many steps. Continued • If the upper bound and the lower bound are equal, the problem P is closed from the standpoint of asymptotic measures. • If the problem P is not closed, there is an algorithmic gap.