Asymptotic Analysis
“But if there’s something that frightens you, there are those that
turn their eyes away and there are those who try to see through the
fear and conquer it.”
— The Big O
Computer Science classes often study the performance of algorithms
through the lens of Asymptotic Analysis. Many proofs in
asymptotic analysis are sloppy; we take shortcuts, work with
approximations, and almost never refer back to the official definitions.
The only excuse is that, in practice, this still usually produces the
right answer! In this section, we’ll be careful and precise about
asymptotic analysis, which is great practice for writing proofs and
working with quantifiers.
If f and g are functions from
N to N, we say
f∈O(g)iff∃c>0 ∃m≥0 ∀i≥m (f(i)≤c⋅g(i))
(where c
is a real number, and m and i are natural numbers).
That is, f∈O(g) is true iff f stays below the scaled function
c⋅g once we reach inputs larger than m.
Intutively, f∈O(g) means that f grows no faster than [a constant
multiple of] g as the input value becomes arbitrarily large.
Example
Theorem: 2n+1∈O(n).
That is to say, (n↦2n+1)∈O(n↦n).
Proof (heavily annotated):
To show:
∃c>0 ∃m≥0 ∀i≥m (2i+1≤ci).
Put
c:=3 and
m:=2.
To show:
∀i≥m (2i+1≤ci).
Let
i≥m=2 be given.
To show:
2i+1≤ci.
Then
2i+1≤2i+i=3i=ci.
Thus:
∀i≥m (2i+1≤ci).
So
∃c>0 ∃m≥0 ∀i≥m (2i+1≤ci).
Therefore,
2n+1∈O(n). QED
Proof (traditional compressed form):
Put c:=3 and m:=2. Then for all i≥m=2, 2i+1≤2i+i=3i=ci as required. QED
There are three things to note about this example:
- The annotated proof is much more verbose then you would ever see in
a real-life proof. But when first practicing these proofs, the
annotations are extremely helpful to keep track of where we are in
the proof, and where we should be going.
- One might wonder how we knew that c should be 3 and m should
be 2. The answer is that the author of this proof didn’t
know when the proof was first being constructed; it was only after
playing around with the algebra that follows that it became clear
that 3 and 2 would ensure that 2i+1≤ci for all i≥m.
The order that the proof is presented is often not the order in
which the pieces were figured out!
- Infinitely many other choices of c and m would have worked as
well (though not all of choices). However, since we are proving an
existentially quantified formula, it suffices to find a single
example that works.
Example
Theorem: 100n+1000∈O(n).
That is to say, (n↦100n+1000)∈O(n↦n).
Proof (heavily annotated):
To show:
∃c>0 ∃m≥0 ∀i≥m (100i+1000≤ci).
Put
c:=200 and
m:=10.
To show:
∀i≥m (100i+1000≤ci).
Let
i≥m=10 be given.
To show:
100i+1000≤ci.
so
1000≤100i,
and hence
100i+1000≤200i=ci.
Thus:
∀i≥m (100i+1000≤ci).
So
∃c>0 ∃m≥0 ∀i≥m (100i+1000≤ci).
Therefore,
100n+1000∈O(n). QED
Definition
If f and g are functions from
N to N, we define the function f+g
“pointwise.” That is, for every k∈N ,
(f+g)(k):=f(k)+g(k)
Example
Theorem: If f∈O(h) and g∈O(h) then f+g ∈ O(h).
Proof (heavily annotated):
To show:
∀f,g:N→N (f∈O(h)∧g∈O(h) → f+g∈O(h)).
Let
f∈O(h) and
g∈O(h) be given.
That is, assume that
∃c>0 ∃m≥0 ∀i≥m (f(i)≤ch(i))and that
∃c>0 ∃m≥0 ∀i≥m (g(i)≤ch(i)) .
To show:
f+g∈O(h).
That is, to show:
∃c>0 ∃m≥0 ∀i≥m (f(i)+g(i)≤ch(i)) .
By assumption, there exist
c1 and
m1 such that
∀i≥m1 (f(i)≤c1h(i)) and there exist
c1 and
m1 such that
∀i≥m2 (g(i)≤c2h(i)).
Put
c:=c1+c2 and
m:=max{m1,m2}.
To show:
∀i≥m (f(i)+g(i)≤ch(i)) Let
i≥m be given.
To show:
f(i)+g(i)≤ch(i) Since
i≥m≥m1,
f(i)≤c1h(i).
Since
i≥m≥m2,
g(i)≤c2h(i).
So
(f+g)(i)=f(i)+g(i)≤(c1+c2)h(i)=ch(i).
Thus
∀i≥m (f(i)+g(i)≤ch(i)) Then
∃c>0 ∃m≥0 ∀i≥m (f(i)+g(i)≤ch(i)) .
So
∀f,g:N→N (f∈O(h)∧g∈O(h) → f+g∈O(h)) .
Therefore,
f+g∈O(h) for all
f and
g. QED.