Hello, Big-O

Resources

Big-O cheatsheet

Algorithm analysis: introduction

Best, Worst, and Average Cases

The sequential algorithm: searching an array containing $n$ integers to find the one with a particular value $K$.
Best case: one integer is examined.
Worst case: all $n$ integers need to be examined.
Average case: $n/2$.
For real-time applications, worst-case analysis. Otherwise, average-case analysis if the data distribution is known, or worst-case analysis if not.

Asymptotic Analysis

Asymptotic analysis is used for most algorithm comparisons. Asymptotic analysis refers to the study of an algorithm as the input size "gets big" or reaches a limit (in the calculus sense; so all constant factors can be ignored).

Big-O, Big-Omega, and Big-Theta

The method of limits

Given two functions $f(n)$ and $g(n)$, which one grows faster (or how we know $f(n)$ is in $O(g(n))$, $\Omega(g(n))$ or $\Theta(g(n))$)?
$lim_{n\to\infty} \frac{f(n)}{g(n)}$
If the limit goes to $\infty$, then $f(n)$ is in $\Omega(g(n))$; if the limit goes to zero, then $f(n)$ is in $O(g(n))$; if the limit goes to a constrant (other than zero), then $f(n) = \Theta(g(n))$ because both grow at the same rate.
Example: $f(n) = 2nlogn$ and $g(n) = n^2$, then $f(n)$ is in $O(g(n))$ (or $g(n)$ is in $\Omega(f(n))$).

Simplifying rules

  1. If $f(n)$ is in $O(g(n))$ and $g(n)$ is in $O(h(n))$, then $f(n)$ is in $O(h(n))$.
  2. If $f(n)$ is in $O(kg(n))$ for any constant $k > 0$, then $f(n)$ is in $O(g(n))$.
  3. If $f_1(n)$ is in $O(g_1(n))$ and $f_2(n)$ is in $O(g_2(n))$, then $f_1(n) + f_2(n)$ is in $O(max(g_1(n), g_2(n)))$.
  4. If $f_1(n)$ is in $O(g_1(n))$ and $f_2(n)$ is in $O(g_2(n))$, then $f_1(n)f_2(n)$ is in $O(g_1(n)g_2(n))$.
These rules also appy to $\Omega$ and $\Theta$ notations.

The first three rules say that one can ignore all the constants and lower-order terms to determine the asymptotic growth rate for any cost function.

The fourth rule can be used to analyze simple loops in programs.

Calculating the running time for a program

Comparison of the algorithms

constant < Logarithmic< linear < polynominal ($n^k$; quadratic, cubic, etc) < exponential



A faster computer, or a faster algorithm?





Space/time tradeoff

One can often gain an improvement in space requirements in exchange for a penalty in running time. There are many situations where this is a desirable tradeoff.