Notes 6 --- Complexity

Fundamentals:
Complexity is:

The solution is:
  1. To count only the most frequent or most costly operations.
  2. To look only for the "leading term" of the cost function.
  3. To report the result as parameterized by the size of the input.
There are at least five measures of complexity of interest:
Amortized complexity is beyond the scope of this course, and determining the typical input set is subjective. We will concentrate on worst-case, and to a lesser extent, average-case, analysis.

Complexity notation:
We will later discuss in some detail determining the complexity of an algorithm, and will in the meantime, as we have already done, use complexity notation. What we need for now, however, is a means of comparing complexity functions, or in particular, comparing the complexity of a given algorithm with a standard complexity class.

Definition of Complexity
"Standard" complexity classes include O (f(n)) (for the following functions f of n):

We can simplify the definition of complexity as follows: One function f has complexity less than or equal to another function g if, for large enough values of n, f(n) is always less than or equal to some constant multiple of g(n).

A few results:
(For convenience, these results are stated in terms of absolute inequalities, but should be translated into statements about O-classes.)

  1. n^r < = n^r lg^s n < = n^(r+t) for r, s, t positive
  2. a^n b^n for 1 < a < = b
  3. n^r < = n^lg n < = a^n for any r and a > 1
  4. a^n < = n! < = n^n < = a^b^n for any 1 < a, b
A typical complexity analysis:
Consider the following program fragment:

                    for i := 1 to n do
                       for j := 1 to i do
                          sum := sum + A [i,j];
         3_{ i = 1 }^n i times, or n (n+1)/2, or O ( n^2 ).