Algorithm Analysis Fundamentals

Types of Analysis

Theoretical vs Empirical Analysis

1. Theoretical Analysis

Time efficiency is analyzed by determining the number of repetitions of the basic operation11. Typically the most time-consuming operation in the innermost loop. as a function of input size (n).

Basic Operation: The operation that contributes most towards the running time of the algorithm.

\boxed{ \text{Running Time: } T ( n ) \approx c_{op} C(n) } \\ \small \textit{where $T$ is running time,} \\ \textit{$n$ is input size,} \\ \textit{$c_{op}$ is execution time for basic operation, and} \\ \textit{$C$ is \# of times basic operation is executed} \\

Note — In short, we care about:

  1. How long the basic operation takes, and
  2. How many times we do it.
Example: Input size & basic operation of some common problems
Problem Input Size Measure Basic Operation
Searching for key in a list of n items # of list’s items (n) Key comparison
Multiplying two matrices Matrix dimensions or total # of elements Multiplication of two numbers
Checking if a given integer n is prime n’s size = # of digits (in binary representation) Division
Typical graph problem # of vertices and/or edges Visiting a vertex or traversing an edge

2. Empirical Analysis

  1. Select a specific (typical) sample of inputs.
  2. Use a:
    1. Physical unit of time (e.g., ms), or
    2. Count # of basic operation executions
  3. Analyze empirical data.

Note — Remember: The size and format of input may impact the time efficiency of an algorithm.

Best/Average/Worst Case

Some algorithms’ efficiency depends on form of input.

\boxed{ \text{Worst Case: } C_{\text{worst}}(n) - \text{max over inputs of size $n$} } \\~\\ \boxed{ \text{Best Case: } C_{\text{best}}(n) - \text{min over inputs of size $n$} } \\~\\ \boxed{ \text{Average Case: } C_{\text{avg}}(n) - \text{avg over inputs of size $n$} }

Note — The average case is NOT the average of the worst and best case!

Example: Worst and Best Case of Sequential Search

Q: Find the worst and best case of this sequential search algorithm.

ALGORITHM SequentialSearch(A[0..n-1],K)
// Searches for given value in given array
// Input: Array (A) and search key (K)
// Output: Index of first element that matches K, or -1 if no matches
  i \leftarrow 0
  while i \lt n and A[i] \ne K do
      i \leftarrow i \text{$+$} 1
  if i \lt n return i
  else return -1

A:

Analysis — Time complexity is O(n) worst-case, \Omega(1) best-case, and \Theta(n) average-case. Space is O(1).

Example: Average Case of Sequential Search

Q: Find the average case of this sequential search algorithm.

A:

Standard Assumptions

  1. Let p be the probability of a successful search.
  1. For every i, the probability of first match occurring in the i-th index is the same.

Cases

Case 1

In the case of a successful search, the probability of the first match occurring in the i-th position of the list is p/n for every i,

Case 2

In the case of an unsuccessful search, the number of comparisons will be n with the probability of such a search being (1-p)

Thus, the total expected comparisons is simply the addition of the above two expectations:

C_{\text{avg}}(n) = \left[ 1 \times \frac{p}{n} + 2 \times \frac{p}{n} + \dots + i \times \frac{p}{n} + \dots + n \times \frac{p}{n} \right] + n (1-p)

Simplifying

Now, we can simplify:

\begin{aligned} C_{\text{avg}}(n) &= \frac{p}{n} [ 1 + 2 + \dots + i + \dots + n ] + n(1-p) \\ &= \frac{p}{n} \frac{n(n+1)}{2} + n(1-p) \\ &= \frac{p(n+1)}{2} + n(1-p) \end{aligned}

Therefore, the average case is:

C_\text{avg}(n) = \frac{p(n+1)}{2} + n(1-p)

Amortized Analysis

Sometimes it’s better to analyze multiple runs rather than a single run.

Aside — Etymology: Amortization is from finance44. Useful for concepts like compounding APR..

Example: Amortized Dynamic Array Resizing

When inserting into a dynamic array, most inserts take O(1) time. Occasionally, the array fills up and must be doubled in size, taking O(n) time. However, because this O(n) work only happens every n insertions, the cost spreads out. The amortized cost per insertion remains O(1).

Asymptotic Analysis

Order of Growth

Asymptotic Analysis: Analysis of order of growth.

Definition — Asymptotic: The nature of a graph/function as it reaches large values.

Order of Growth: The dominant behavior of an algorithm as n \to \infty.

Example: Normal questions that about order of growth

Why? — Looking at the asymptotic order of growth lets us compare functions by ignoring constant factors and small input sizes.

Bounds


  1. Typically the most time-consuming operation in the innermost loop.↩︎

  2. Because we have to traverse the whole array.↩︎

  3. Because the key is found on the very first element.↩︎

  4. Useful for concepts like compounding APR.↩︎