Algorithm Running Times
Algorithm Running Times
O-notation
O-notation characterizes an upper bound on the asymptotic behavior of
a function. In other words, it says that a function grows no faster than a
certain rate, based on the highest-order term. Consider, for example, the
function 7n3 + 100n2 – 20n + 6. Its highest-order term is 7n3, and so we
say that this function’s rate of growth is n3. Because this function grows
no faster than n3, we can write that it is O(n3). You might be surprised
that we can also write that the function 7n3 + 100n2 – 20n + 6 is O(n4).
Why? Because the function grows more slowly than n4, we are correct
in saying that it grows no faster. As you might have guessed, this
function is also O(n5), O(n6), and so on. More generally, it is O(nc) for
any constant c ≥ 3.
Ω-notation
Ω-notation characterizes a lower bound on the asymptotic behavior of a
function. In other words, it says that a function grows at least as fast as
a certain rate, based — as in O-notation—on the highest-order term.
Because the highest-order term in the function 7n3 + 100n2 – 20n + 6
grows at least as fast as n3, this function is Ω(n3). This function is also
Ω(n2) and Ω(n). More generally, it is Ω(nc) for any constant c ≤ 3.
Θ-notation
Θ-notation characterizes a tight bound on the asymptotic behavior of a
function. It says that a function grows precisely at a certain rate, based
—once again—on the highest-order term. Put another way, Θ-notation
characterizes the rate of growth of the function to within a constant
factor from above and to within a constant factor from below. These
two constant factors need not be equal.
If you can show that a function is both O(f (n)) and Ω(f (n)) for some
function f (n), then you have shown that the function is Θ(f (n)). (The
next section states this fact as a theorem.) For example, since the
function 7n3 + 100n2 – 20n + 6 is both O(n3) and Ω(n3), it is also Θ(n3).