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
Definition: We say a real-valued function
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
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
: 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
In this way we encapsulate the notion of worst-case runtime. If
Definition: We say
Now we may only use
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.
Want to respond? Send me an email, post a webmention, or find me elsewhere on the internet.