Click here for a blank version of this page for practice.
What is an algorithm?
- A step by step process that solves a problem
- Gives the correct output for all inputs
- Complete the process in finite time
Real time: Can be measured by having a virtual stopwatch to count how long a program takes to run. Will get different results on different machines.
Scale/Growth Rate: Count the amount of iterations the loops take. Will get same results on different machines.
We can categorize the growth rate into an asymptotic complexity class:
| Name | Function | |
|---|
| Constant | n2 | Slower growth rate |
| Logarithmic | logn | |
| Linear | n | |
| Poly-logarithmic | logkn | |
| Quadratic | n2 | |
| Cubic | n3 | |
| Exponential | Cn | Higher growth rate |
Big O notation
Consider the equation:
g(n)=O(f(n))
In this case, = has the meaning of ∈.
Thus, it translates to:
g(n) is in the set of all functions whose growth rate is at most f(n).
In order to determine the validity of that statement, prove that:
g(n)≤C⋅f(n)Where C>0 and n0>0For all n
f(n) can be any function, but it is proper to use one of the asymptotic complexity classes.
Examples
Prove or disprove the following statements.
(a) n=O(n)
n≤C⋅nSuppose C=1 and n0=1n≤1⋅n⟹n≤nThus, the statement is true.
(b) 5n=O(n)
5n≤C⋅nSuppose C=5 and n0=15n≤5⋅n⟹5n≤5nThus, the statement is true.
(c) 10n+3=O(n)
10n+3≤C⋅nSuppose C=13 and n0=110n+3≤13n⟹10n+3≤10n+3n⟹3≤3nThus, the statement is true.
(d) n=O(n)
n≤C⋅nSuppose C=1 and n0=1n≤n⋅n⟹1≤nThus, the statement is true.
(e) 2n2+7=O(n3)
2n2+7≤C⋅n3Suppose C=9 and n0=12n2+7≤9n3Thus, the statement is true.
(f) 2n2+7=O(n2)
2n2+7≤C⋅n2Suppose C=9 and n0=12n2+7≤9⋅n2⟹2n2+7≤9n2Thus, the statement is true.
(g) 2n2+7=O(n)
2n2+7≤C⋅nSuppose C=10 and n0=1
Thus, the statement does not hold at large values of n.
Big Ω Notation
Recall that O(f(n)) represents an upper bound.
Ω(f(n)) represents a lower bound.
In order to prove if g(n)=Ω(f(n)):
g(n)≥C⋅f(n)Where C>0 and n0>0For all n
Examples
Prove or disprove the following statements.
(h) 3n+2=Ω(n)
3n+2≥C⋅nSuppose C=3 and n0=13n+2≥3nThus, the statement is true.
(i) n2+13n=Ω(n)
n2+13n≥C⋅nSuppose C=1 and n0=1n2+13n≥nThus, the statement is true.However, this is a weak statement and can be improved (see below)
(j) n2+13n=Ω(n3)
n2+13n≥C⋅n3Suppose C=1,000,000 and n0=1n2+13n≥1,000,000nThus, the statement is false.This statement is too much and is no longer valid
(k) n2+13n=Ω(n2)
n2+13n≥C⋅n2Suppose C=1 and n0=1n2+13n≥n2Thus, the statement is true and strong.
Big Θ Notation
Recall that O(f(n)) represents an upper bound and Ω(f(n)) represents a lower bound.
Θ(f(n)) represents an exact bound. In other words,
g(n)=Θ(f(n))⇔g(n)=O(f(n))∧g(n)=Ω(f(n))
Examples
Prove or disprove the following statements.
(l) 13n+2=Θ(n)
13n+2=O(n) is true13n+2=Ω(n) is trueThus, the statement is true.
(m) 13n+2=Θ(n2)
13n+2=O(n2) is true13n+2=Ω(n2) is falseThus, the statement is false.
(n) 13n+2=Θ(n)
13n+2=O(n) is false13n+2=Ω(n) is trueThus, the statement is false.
Comparing Growth Rates
In order to compare the growth rate of two functions, we can also use limits.
This is a more matematical and precise method.
n→∞limf(n)g(n)=n→∞limf′(n)g′(n)=n→∞limf′′(n)g′′(n)=LL=∞⟹g(n)>f(n)L=C>0⟹g(n)=f(n)L=0⟹g(n)<f(n)
If the limit of an expression results in an indeterminate form:
n→∞limf(n)g(n)=00orn→∞limf(n)g(n)=∞∞
Then you may take the derivative of the numerator and denominator and attempt to find the limit again:
n→∞limf(n)g(n)=n→∞limf′(n)g′(n)
Examples
Prove or disprove the following statements.
(o) 2n2+7=O(n)
L=n→∞limn2n2+7=n→∞limnn(2n+n7)=n→∞lim(2n+n7)=∞Since L=∞⟹g(n)>f(n),2n2+7 is asymptotically larger than nThus, the statement is false.
(p) 2n2+7=O(n3)
L=n→∞limn32n2+7=n→∞lim3n24n=n→∞lim3n4=∞4=0Since L=0⟹g(n)<f(n),n3 is asymptotically larger than 2n2+7Thus, the statement is true.
(q) 2n2+7=O(n2)
L=n→∞limn22n2+7)=n→∞lim2n4n=2Since L=C=2>0⟹g(n)=f(n),2n2+7 and n2 have the same growth rate Thus, the statement is true.
Logarithms
Consider the equation Θ(logn).
Typically, logn implies a base of 10.
Is this still true?
The base of a log can be changed via the formula:
logba=logxblogxa
Therefore, Θ(logn) contains all log functions of any base, since we can easily convert from one to another.