Asymptotic Analysis [00K7]
- September 3, 2026
The basic concepts of asymptotic analysis are introduced in a POGIL activity.
Why asymptotic analysis? [00KN]
- September 9, 2026
Why should we examine problems analytically at all? For example, why not just run programs and use a stopwatch to compare them?
- Actually running programs might take too long!
- The analysis lets us analyze problems for all input sizes at once, rather than having to gather data points.
- The analysis is independent of the algorithm implementation, the language in which the program is implemented, and the architecture in which the program is run. We insulate ourselves to all these variables.
- Theoretically efficient almost always implies practical efficiency.
Intuition and motivation for big-O [00KR]
- September 9, 2026
From now on, we’ll assume that n \geq 0, T(n) \geq 0, and that T represents a generic function \mathbb {N} \to \mathbb {R} representing, say, the amount of time an algorithm takes to run on an input of size n. Recall that big-O is “like \leq ”, big-Omega is “like \geq ”, and big-Theta is “like =”. However, if we were to define, say, big-O as “T(n) is O(n^2) if T(n) \leq n^2”, that would be too restrictive for us. For one thing, we don’t want it to matter what computer we run things on. What if it takes 2n^2, or 3n^2? Those are not actually \leq n^2, but we want them to be considered “the same as” n^2. Also, we don’t really care what happens for small values of n (after all, for small values we could just run the algorithm ourselves, by hand), so it’s not necessary that T(n) \leq n^2 (or some multiple) for all values of n, only that it be so eventually. These considerations lead to the following formal definition.
Definition Big-O [00KO]
- September 9, 2026
T(n) is O(g(n)) iff there exists a real constant c > 0 and a natural number n_0 \geq 0 such that for all n \geq n_0, T(n) \leq c \cdot g(n).
Definition Big-Omega [00KP]
- September 9, 2026
The definition of big-Omega is identical, except that we use \geq in place of \leq :
T(n) is \Omega (g(n)) iff there exists a real constant c > 0 and a natural number n_0 \geq 0 such that for all n \geq n_0, T(n) \geq c \cdot g(n).
Definition Big-Theta [00KQ]
- September 9, 2026
T(n) is \Theta (g(n)) iff T(n) is both O(g(n)) and \Omega (g(n)).
Remark [00KS]
- September 9, 2026
Note this is not the same as replacing the \leq in the definition of big-O with an = sign! In that case, T(n) would have to be exactly equal to some constant multiple of g(n), which would be much too restrictive. In the actual definition, T(n) must merely fall in between two different constant multiples of g(n).
Definition Little-o [00KX]
- September 9, 2026
T(n) is o(g(n)) iff T(n) is O(g(n)) but not \Theta (g(n)).
Remark [00KY]
- September 9, 2026
In other words, intuitively, T(n) is o(g(n)) if it grows strictly more slowly than g(n). For example, n+2 is o(n^2).
Example [00KT]
- September 9, 2026
Consider T(n) = 3n^2 + 17n + 8. To show that this is O(n^2) according to the definition, we can reason as follows: \begin {align*} T(n) &= 3n^2 + 17n + 8 \\ &\leq 3n^2 + 17n^2 + 8n^2 \quad (n \geq 1) \\ &= 28n^2 \end {align*} So we can choose c = 28 and n_0 = 1, which satisfies the definition, so T(n) is O(n^2). (In fact we could also pick c = 4 along with a bigger n_0.)
Remark Big-O in the real world [00KU]
- September 9, 2026
A lot of the time people use big-O when what they really mean is \Theta . In this class we will be very careful to use them properly. It’s useful to have all of them at our disposal. For some problems we know exactly how fast they can be solved, so we use \Theta . For some problems we know some O and some \Omega but they are not the same. For example, matrix multiplication: we know it must take \Omega (n^2) time, and there are algorithms that show it is O(n^{2.37\dots }), but no one knows what the theoretical limit is (we will look at matrix multiplication later in the semester).
Limit theorems [00KV]
- September 9, 2026
It is usually really annoying to use these definitions directly. Instead we can often compare rates of growth using limits. Note in the below that L < \infty is used as a shorthand to mean "the limit L exists and does not diverge to \infty ", and L = \infty is used as a shorthand to mean "the limit L diverges to \infty ".
Theorem Asymptotic bounds via limits [00KW]
- September 9, 2026
- If 0 \leq \lim _{n \to \infty } T(n)/g(n) < \infty then T(n) is O(g(n)).
- If 0 < \lim _{n \to \infty } T(n)/g(n) \leq \infty then T(n) is \Omega (g(n)).
- If 0 < \lim _{n \to \infty } T(n)/g(n) < \infty then T(n) is \Theta (g(n)).
- If \lim _{n \to \infty } T(n)/g(n) = 0 then T(n) is o(g(n)).
Remark [00KZ]
- September 9, 2026
Note that the converse is not true, since, for example, T(n) might be O(g(n)) even if the limit does not exist. As a counterexample, let T(n) = 0 when n is even, and 1 when n is odd. Then T(n) is O(1) but \lim T(n)/1 does not exist. But typically this won’t be an issue with the functions we will see in this class!
Arithmetic with big-O [00L0]
- September 9, 2026
The following properties hold for \Omega and \Theta as well.
- k O(f) = O(f) when k is a constant, i.e. does not depend on n
- O(f) + O(g) = O(\max (f,g)) (running algorithms in sequence)
- O(f) O(g) = O(fg) (running nested algorithms)
For example, O(3n^2 + 17n + 8) = O(3n^2) + O(17n) + O(8) = O(n^2) + O(n) + O(1) = O(n^2).
Time Complexity Zoo [00L1]
- September 10, 2026
We now present a “complexity zoo”, with examples of things having each common asymptotic time complexity, with some relevant facts interspersed. Our zoo will be ordered from smallest to biggest; that is, each entry will be little-o of the next thing. For each \Theta (f(n)), we will also record the largest n such that we can process an input of size n using a \Theta (f(n))-time algorithm in approximately 1 second, assuming we can do 10^8 operations per second.
\Theta (1): constant (any input) [00L2]
- September 10, 2026
A \Theta (1) or constant time algorithm is one whose running time does not depend on the size of its input. If we have an algorithm that takes \Theta (1), we can obviously process arbitrary-sized inputs in less than one second.
Examples of \Theta (1) operations include:
- Access an arbitrary index in an array
- Push to or pop from the top of a stack
- Add to or remove from the end of an extensible list (e.g.
ArrayListin Java; Python lists) - Insert, update, or remove from a hash table (e.g.
set+ dictionaries in Python;HashSet/HashMapin Java) - Add to or remove from either end of a deque (double-ended queue)
- Arithmetic (usually! sort of!)
\Theta (\lg n): logarithmic (unimaginably large inputs) [00L3]
- September 10, 2026
We say something is logarithmic if it is proportional to \log n for inputs of size n.
Notation \lg [00L4]
- September 10, 2026
We will use \lg n as an abbreviation for \log _2 n. Two is by far the most common and useful base for logarithms when talking about computers.
If we have an algorithm that takes \Theta (\lg n) time, we could use it to process inputs of size 2^{10^8} in one second, which is unimaginably large.
Examples of things that are \Theta (\lg n):
- Binary search
- The height of a balanced binary tree with n nodes
- The number of bits needed to represent n
Theorem Logarithm base doesn't matter to \Theta [00L9]
- September 11, 2026
\Theta (\log _a n) = \Theta (\log _b n) for all positive real numbers a and b. In other words, up to \Theta , the base of a logarithm doesn't matter; \Theta (\log n), \Theta (\lg n), \Theta (\ln n), etc. are all the same.
Proof.
- September 11, 2026
Theorem Logarithms grow more slowly than any power [00L8]
- September 11, 2026
\log _b n is o(n^x) for all x > 0 and any b > 1.
Proof.
- September 11, 2026
Remark \lg n is much closer to 1 than n [00LA]
- September 11, 2026
This is somewhat surprising! \Theta (\lg n) is actually smaller than n to any positive power: even \Theta (\sqrt [15]{n}) or whatever. \Theta (\lg n) is not “halfway between” \Theta (1) and \Theta (n); in some sense it is much closer to \Theta (1).
For example, the biggest number that fits in a 64-bit integer is about 10^{19}. How long would it take to count that high? At 10^8 per second, it would take 10^{11} seconds or about 3000 years. But if we can process it using something that repeatedly halves, \lg 10^{19} \approx 63.
\Theta (\sqrt n) = \Theta (n^{1/2}) (inputs up to 10^{16}) [00LB]
- September 11, 2026
This will not come up much in this course, but it is definitely a thing. See “square root decomposition”.
\Theta (n): linear (inputs up to 100 million) [00LC]
- September 11, 2026
Examples of things that are \Theta (n):
- Linear search
- Maximum/minimum/sum of a list
- Merge two sorted lists
- Insert or removing at the beginning of an array/list.
\Theta (n \lg n) (inputs up to 4.5 million) [00LD]
- September 11, 2026
Examples of things that are \Theta (n \lg n):
- Mergesort, quicksort
- In general, divide & conquer algorithms with \Theta (n) work at each level. We’ll study this in more detail later in the semester.
- If you are only allowed to compare pairs of elements to see which is greater, sorting a list of n elements takes \Omega (n \lg n). In other words, it's impossible to sort any faster than n \lg n if you are only allowed to compare elements. (If you are allowed extra operations, sorting faster than n \lg n is possible; for example, a list of integers can be sorted in \Theta (n) time.)
\Theta (n^2): quadratic (inputs up to 10 thousand) [00LE]
- September 11, 2026
Examples of things that are \Theta (n^2):
- Two nested loops (often, but not necessarily)
- 1 + 2 + 3 + \dots + n = \frac {n(n+1)}{2} = \Theta (n^2)
- The number of pairs of items chosen out of n, \binom {n}{2} = \frac {n(n-1)}{2}.
- Repeatedly appending n characters to a string one by one
- Insertion sort, bubble sort
\Theta (n^k) (inputs up to \sqrt [k]{10^8}) [00LF]
- September 11, 2026
\Theta (2^n): exponential (up to n = 26) [00LH]
- September 11, 2026
Examples of things that are \Theta (2^n):
- Number of subsets of n things
- Number of bitstrings of length n
- Number of nodes (also number of leaves) in a tree of height n
- 1 + 2 + 4 + 8 + \dots + 2^n = 2^{n+1} - 1, so it is \Theta (2^n)
Theorem [00LI]
- September 11, 2026
n^k is o(r^n) for all integers k \geq 0 and real numbers r > 1.
Proof.
- September 11, 2026
Remark Polynomial vs exponential [00LJ]
- September 11, 2026
There is an insurmountable gulf between polynomial and exponential time. For example, even n^{295} is o(1.001^n)! We usually take this to be the dividing line between “efficient/feasible” and “inefficient/infeasible”. (Of course n^{295} is probably not actually feasible but in practice we don’t see algorithms with running times like that.)
\Theta (n!): factorial (inputs up to 11) [00LK]
- September 11, 2026
n! corresponds to the number of distinct orderings of n things. If an algorithm takes \Theta (n!) it is really, really bad. In fact, k^n is o(n!) for all k.