Asymptotic Notation
To accurately predict the growth in run time for large instances, weOnce we have a valid T(n) we want to compare the run time of one program with another.
Consider two simple programs whose only variation in T(n) is a coefficient (a loop that had an additional line or two):
Consider: T(n) = 2n2 +3n and T(n) = n2 +3n
Look at their graphs.
Note that coefficients do not make T(n) grow near as fast as various functions of n.
Consider some functions and their common names
Now get some values of these functions for various n. (Notice chosen values of n - since log2 are integers for powers of 2)
Finally, graph them as n gets large.
Now go back and consider how some I gave to you compare. Make a chart of values to see the rate of growth to determine which is < the other as n gets big. (To cheat, see page 111 in text.)
What we are doing is seeing when functions are asymptotically bigger(or smaller) than one another. I.E. which one grows faster than the other.
Note that this is saying that f(n) is asymptotically smaller than or equal to g(n)
e.g. Suppose T(n) = (n + 1) 2
Verify:
(why not n3 ) (lub)
T(n) is O(n2 ) for some program means there are positive constants c
and no such that for n > no we have T(n) <
cn2
verify T(n) = 3n3 + 2n2 is O(n3)
(what does that mean?)
is T(n) also O(n4) ?
* Prove Theorem 3.1 (page 119) ... makes polynomials easy
(can always get a big enough c, no )
Theorem: If f(n)=amnm + ... + a1n + a0 , then f(n) = O(nm).
Proof:
So, f(n) = O(nm) (assuming that m is fixed)
DONE
WHY?
So, given:
One can always "play" for a proper no and c. You only need one to work to show the definition holds, so pick one that is easy to allow you to verify.
f(n) = O(g(n)) says g(n) is an upper bound ... does not say how good.
To be informative, want smallest (least upper bound)
another example:
What about
page 119
Example 3.15 page 120 (and here)
verify
Theta
verify
Example 3.18 page 121 (and here)
if upper = lower --> algorithm is "best possible"
only way to make faster is faster hardware
HOWEVER, algorithmic improvement is more useful than hardware improvement
Big chart of times
Problems: Page 115: numbers 2, 3, 5 (but use the formal definition rather than the "working definition", Page 124: Numbers 9 and 11 (Don't forget that some answers are on the book website)
If you still want more info, see MIT slides (for this class only to slide 18)
Formal Definitions
(Section 3.3 of text)
Def. f(n) = O(g(n)) iff there exists positive constants c and no such that
f(n) < cg(n) for all n , n > no (g is upper bound)
then
T(n) is O(n2)
let no = 1 and c = 4
To show this to me so I know you know what you are talking about your math has to be very clear.
for n > 1,
(n +1)2 < 4n2
(n +1)2 < 4n2
n2 + 2n +1 < 4n2
0 < 4n2 - (n2 + 2n + 1)
0 < 3n2 - 2n - 1 for n > 1
OK
T(n) = 3n3 + 2n2 < cn3 let
c= 5, no = 1, then for all n > 1
3n3 + 2n2 < 5n3
2n2 < 2n3
n2 < n3
1 < n
Discuss
hints for proof:
OK, here is the proof:
then
n * fraction < n* 1.
e.g.,
6n3 + 5n2 +4n + 2 < 6n3 +
5n3 + 4n3 +2n3 = 17n3
Remember:
f(n) = O(g(n)) iff there exists positive constants c and no such that
f(n) < cg(n) for all n , n > no
Our g(n) is nm, let 6n3 + 5n2 +4n + 2 O(?) and what might c be?
Often, we use no = 1 since it makes things easy to see.
4n + 2 O(?) and what might c be?f(n) = 1000n2 + 100n - 6 < 1001n2 for
n > 100
(what is no and what is c?)f(n) = 6 * 2n + n
f(n) = 6 * 2n + n is O(2n) since
f(n) = 6 * 2n + n < 7 * 2n for n
> 4 (what is c, no?)
Omega run in time independent of inputs
time proportional to length of input
if double length => double timeSee here for calculations of recursive algorithms (Data Structures and Algorithms, Aho, Hopcroft, and Ullman, Addison-Wesley, 1983, ISBN 0-201-00023-7, Chapter 9)
iff there exists positive constants c and no such that
f(n) > cg(n) for all n , n > no
(g is lower bound)
(what does that mean?)
iff there exists positive constants c1, c2 and no
such that c1g(n) < f(n) < c2g(n) for all n , n
> no
(what does that mean?)
another chart of times and
and suppose computer improvements