CmSc 250 Intro to Algorithms
The Big-Oh and the other notations in Algorithm Analysis
The complexity of an algorithm is measured by the operations needed to solve
the corresponding problem.
We are concerned with estimating the complexity of
algorithms, where the number of operations
depends on the size of the input.
Examples of such algorithms are:
If N is the number of the elements to be processed by an algorithm (N is the
size of the input)
then the number of operations can be represented as a
function of N: f(N)
(sometimes we use small n)
We can compare the complexity of two algorithms by comparing the
corresponding functions.
Moreover, we are interested what happens with the
functions for large N,
i.e. we are interested in the asymptotic growth of these
functions.
Each particular growing function has its own speed of growing.
Ssome functions grow faster, others grow slower.
The question is: ? Is there a way to compare the rate of growth of two functions?
The rate of growth of a function is called
asymptotic growth.
We can compare functions
by studying their asymptotic growth.
Given a function f(n), all other functions fall into three classes:
lim(f(n)/g(n)) = c, 0 < c < ∞, n → ∞
Notation: f(n) = Θ(g(n)) , pronounced "theta"
lim(f(n)/g(n)) = 0, n → ∞
Notation: f(n) = o(g(n)), pronounced "little oh"
lim(f(n)/g(n)) = ∞, n → ∞
Notation: f(n) = ω (g(n)), pronounced "little omega"
Obviously, case 2 is the reverse of case 3:
if
Examples :
Compare n and n2
lim( n/n2 ) = 0, n → ∞, hence n = o(n2),
lim( n2/n ) = ∞, n → ∞, hence n2 = ω(n)
Compare n and log n
lim( (log n) / n ) = 0, n → ∞, hence log n = o(n),
lim( n / log n ) = ∞, n → ∞, hence n = ω (log n),
f(n) and g(n) have the same rate of growth if
lim( f(n)/g(n) ) = c, 0 < c < ∞, n → ∞
Let Θ( f(n) ) be the set
of all functions, that grow with the rate of f(n).
If g(n) has the same rate of growth,
then g(n) є
Θ( f(n) ).
Consider the properties of the relation: "having the same rate of growth":
Hence this is a relation of equivalence and it gives a
partition over the set of all functions - classes of equivalence.
Thus functions in one and the same class are equivalent with respect to their growth.
The important result is:
Two algorithms have same complexity,
if the functions representing the number of operations
have same rate of growth,
i.e. if they belong to one and the same class of equivalence.
Among all functions in a given class of equivalence we choose
the simplest one
to represent the complexity for that class.
Note: In Theory of Algorithms we use '=' instead of 'є' : g(n) = Θ(f(n))
Here are some examples:
lim( n/((n+1)/2)) = 2, same rate of growth.
(n+1)/2 = Θ(n) - rate of growth of a linear function
lim( n2 / (n2 + 6n) ) = 1, same rate of growth.
n2 + 6n = Θ(n2) - rate of growth of a quadratic function
lim( log n / log n2 ) = lim( log n / 2log n) = 1/2, same rate of growth.
log n2 = Θ(log n) - rate of growth of a logarithmic function
Other examples:
Compare n2 and n2 + log n2
They have same rate of growth: Θ(n2). Here is how we show this:
lim(n2 /(n2+ log n2)) = lim(n2 /(n2+ 2log n)) = lim(2n /(2n+ (2loge)/n)) =
lim(n /((n2+ loge)/n))= lim(n2 /(n2+ loge))= lim(2n /(2n))= lim(2/2) = 1
(L'Hopital's rule has been used to find the limit. (log n)' = (log e)/n )
Thus, for any two functions f(n) and g(n) one of the following is true:
a. same rate of growth: g(n) = Θ(f(n))
b. different rate of growth:
either g(n) = o(f(n)), g(n) grows slower than f(n), and hence f(n) = ω(g(n))
or g(n) = ω(f(n)), g(n) grows faster than f(n), and hence f(n) = o(g(n))
The Big-Oh notation is used to simplify our reasoning about growth rates.
f(n) = O( g(n) ) if f(n) grows with the same rate or slower than g(n).
i.e. if f(n) = Θ(g(n)) or f(n) = o(g(n)), then we write f(n) = O(g(n))
Thus n+5 = Θ(n) = O(n) = O(n2) = O(n3) = O(n5)
While all the equalities are technically correct, we would like to have the
closest estimation:
However, the general practice is to use the Big-Oh notation and to write:
n+5 = O(n)
Big-Omega notation is the inverse of the Big-Oh:
If g(n) = O(f(n)), then f(n) = Ω (g(n))
Here we say that f(n) grows faster or with the same rate as g(n), and write
f(n) = Ω(g(n))
In the analysis of algorithms we shall use mainly the Big-Oh estimate.
Let T(N) (and also T1(N), T2(N)) denote the run time of an algorithm for input of size N
Rule 1:
T1(N) + T2(N) = max( O( f(N) ), O( g(N) ))
where max( O( f(N) ), O( g(N) )) is the faster growing function.
T1(N) * T2(N) = O( f(N) * g(N) )
Rule 2: If T(N) is a polynomial of degree k, then T(N) = Θ(Nk)
Rule 3: logkN = O(N) for any constant k.
Examples:
x2 = O(x2), actually x2 = Θ (x2)
2x2 = O(2x2) = O(x2), we do not consider coefficients
x2 + x = O(x2), we disregard any lower-order term
xlog(x) = O(xlog(x))
Typical growth rates:
|
C |
constant, we write O(1) |
|
logN |
logarithmic |
|
log2N |
log-squared |
|
N |
linear |
|
NlogN |
|
|
N2 |
quadratic |
|
N3 |
cubic |
|
2N |
exponential |
|
N! |
factorial |
The following figure illustrates the notations above:
For a given function f(n) we have;
|
Θ(f(n)) |
the set of all function with same rate of growth as f(n) |
|
o(f(n)) |
is the set of all functions that grow much slower than f(n) |
|
ω(f(n)) |
the set of all functions that grow much faster than f(n) |
|
O(f(n)) |
Θ(f(n)) È o(f(n)) same or slower |
|
Ω (f(n)) |
Θ(f(n)) È ω(f(n)) same or faster |
Learning goals
Exam-like questions and problems with solutions
n2, 2n, nlogn, n!, n6 + n2, n4, n6 - n3, 4n, n, nn, 22n.