## The Quest to Capture Speed

Companies and researchers spend hundreds of millions of dollars for the fruits of their algorithms. Whether one is indexing websites on the internet for search, folding proteins, or figuring out which warehouse is the most cost-effective to ship a product from, improvements in algorithm speed save immense amounts of money.

It’s no surprise then, that a similarly immense amount of money has gone into the mathematics behind algorithm analysis. Arguably the roughest and most succinct measurement of how fast an algorithm runs is called *order of complexity* of an algorithm. There are two commonly used measures of order of complexity, namely Big-O notation and the more nuanced Big-Theta notation. We begin with the former.

Because an algorithm runs in a discrete number of steps, we call the number of steps it takes an algorithm to complete for any input of size , and then analyze it for real input . In doing so, we may utilize the vast wealth of knowledge in real analysis, and describe runtime in terms of elementary continuous functions and their limits.

**Definition:** We say a real-valued function is if for some (potentially large) input value , there exists a positive number with for all . Equivalently, .

Here is a simple, pedantic example:

Consider an algorithm which accepts as its input a list of numbers and adds together all of the numbers in that list. Given a list of size , we might count up the number of steps taken by our algorithm to arrive at , because there are additions being performed, and for each add there is an instruction fetch, a pointer reference to follow, a value in memory to load, the addition to perform, and the resulting value to store in a register, each for simplicity taking one step. We characterize this algorithm as , because for any , we have giving .

Of course, such a detailed analysis is rarely performed. Generally, we’d say there is an addition to be performed for each element in the list, giving additions and an order of complexity of . Typically, the goal is to have the function be as simple as possible, giving a small number of commonly used orders which facilitate over-the-table discussions about algorithms:

- : the
**constant time**algorithm which always takes a fixed number of steps for any input. For example, looking up a key in a hash table. - :
**logarithmic time**, where at each step of the algorithm the problem size is reduced by a fixed factor. This is common to “divide and conquer” algorithms. - for :
**polynomial time**. If is not a monomial, we conventionally drop all terms but the one of highest degree, because under the limit the highest degree term dominates the runtime. - for :
**exponential time**, where with each increase of the input size, the runtime increases by a factor of . This is extremely slow for any reasonable input size, even when is very close to 1. Many brute-force algorithms (attempting every possibility) result in exponential runtime. - :
**factorial time**, which is often clumped with exponential time, is actually strictly slower than any exponential time algorithm. Considering , as surpasses , we have each , and since , we decrease at each step, and the limit goes to 0 (i.e., factorials grow much faster). Travelling Salesman is a famous problem whose brute-force solution is factorial, but even with cutting-edge methods for optimization, the worst case runtime is still exponential.

And there are additional classes that fall in between these, such as and . And it is easy to prove that constants, and additions of smaller terms are irrelevant under the O. Specifically, for any constant , and similarly if is , then .

In this way we encapsulate the notion of *worst-case runtime*. If is , we say the algorithm which has exact runtime runs no worse than . Unfortunately, these bounds do not need to be tight. We could say, for instance, that an algorithm runs in as well as , and be right on both counts. That is why we need Big-Theta notation, to give tighter bounds on runtime:

**Definition**: We say is if there exist such that whenever , we have . In other words, for an appropriate choice of constants, bounds both from above and below for all sufficiently large inputs.

Now we may only use notation when the bound is reasonable (not too high, not too low). We gain more information by knowing a function is than knowing it is , because we know it cannot grow slower than .

Of course, for more complex algorithms with multiple input variables, the asymptotic runtimes are necessarily phrased in terms of continuous functions of multiple variables. While it’s not much harder to formally develop these characterizations, it’s beyond the scope of this post.

There is a wealth of knowledge out there on the orders of complexity of various algorithms; one simply cannot write a new algorithm without asking how fast it is. For the interested reader, I highly recommend the mathematically inclined book Introduction to Algorithms, by Cormen, et. al. It’s been the standard text on the subject for many years, and it is a wealth of knowledge and a valuable reference on any shelf.

Pingback: Numerical Integration | Math ∩ Programming

Pingback: P vs. NP, A Primer (And a Proof Written in Racket) | Math ∩ Programming

Pingback: Probabilistic Bounds — A Primer | Math ∩ Programming

Hi Jeremy,

Great series of posts.

I just want to react on the equivalence:

f = O(g) lim_\infty |f| / |g| < \infty

I think only () as the limit may be undefined. See for eg:

f(x) = x cos(x)

g(x) = x

What do you think?

LikeLike

The more rigorous definition of Big-O involves a limit superior, and this example shows why. We still

wantto be able to say f = O(g) even when the limit is not defined. Lim sup allows us to do so, because limsup(Cos(x)) = 1.In practice, however, I’d be very surprised to see an algorithm having a runtime involving cosine. But it’s a good example to think about to get a better understanding of big-O.

LikeLike

Ha, nice, I didn’t know about this limsup for big-O.

Actually, this cos effect happens in algorithm, maybe not with cosine, but it’s the same idea. Consider the fast exponentiation algorithm computing a^n (often the first example of Divide and Conquer strategy).

def fast_exp(a, n):

if n == 0: return 1

if n % 2 == 0:

b = fast_exp(a, n/2)

return b * b

return a * fast_exp(a, n – 1)

Then if T(n) is the number of multiplication, a reasonable choice for time complexity analysis, then T(n) <= 2 log n, so we have indeed T(n) = O(log n) but the limit |T(n)| / |log n| is undefined. See the plot http://share.qpleple.com/OtNw (T(n) in blue, log n in green, and 2 log n in red).

LikeLike

Excellent example. This algorithm actually shows up in a project I’m working on right now with elliptic curves, so I should have known this was the example in mind.

LikeLike