big-o analysis asymptotics 1cs3114/spring18/wmcquain/notes/t09... · inequality does, in fact,...
TRANSCRIPT
![Page 1: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/1.jpg)
Asymptotics
Data Structures & Algorithms
1
CS@VT ©2000-2017 McQuain
Big-O Analysis
Definition: Suppose that f(n) and g(n) are nonnegative functions of n. Then we say
that f(n) is O(g(n)) provided that there are constants C > 0 and N > 0 such
that for all n > N, f(n) Cg(n).
Big-O expresses an upper bound on the growth rate of a function, for sufficiently large
values of n.
By the definition above, demonstrating that a function f is big-O of a function g requires
that we find specific constants C and N for which the inequality holds (and show that the
inequality does, in fact, hold).
![Page 2: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/2.jpg)
Asymptotics
Data Structures & Algorithms
2
CS@VT ©2000-2017 McQuain
Big-O Example
Consider the following function:25 5
( ) 22 2
T n n n
We might guess that:
We could easily verify the guess by induction:
If n = 2, then T(2) = 17 which is less than 20, so the guess is valid if n = 2.
Assume that for some n ≥ 2, T(n) ≤ 5n2. Then:
2 2
2
2
2 2
5 5 5 5 5 5( 1) ( 1) ( 1) 2 5 2
2 2 2 2 2 2
5 52 5 5
2 2
5 5 5 by the inductive assumption
5 10 5 5( 1)
T n n n n n n
n n n
n n
n n n
Thus, by induction, T(n) ≤ 5n2 for all n ≥ 2. So, by definition, T(n) is O(n2).
2( ) 5 for all 2T n n n
![Page 3: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/3.jpg)
Asymptotics
Data Structures & Algorithms
3
CS@VT ©2000-2017 McQuain
Making the Guess
The obvious question is "how do we come up with the guess in the first place"?
Here's one possible analysis (which falls a bit short of being a proof):
2
2 2
2
5 5( ) 2
2 2
5 52 2 (replace n with n^2, subtract 2)
2 2
5
T n n n
n n
n
The middle step seems sound since if n ≥ 2 then n ≤ n2, substituting n2 will thus add at
least 5 to the expression, so that subtracting 2 should still result in a larger value.
![Page 4: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/4.jpg)
Asymptotics
Data Structures & Algorithms
4
CS@VT ©2000-2017 McQuain
Growth Rates
What does this big-O information really tell us?
Consider what happens with the following functions if we double the size of the input:
function input output effect on output
log n K log K
2K 1 + log K increases by 1
n K K
2K 2K doubles
n log n K K log K
2K 2K log K + 2K somewhat more than doubles
n2 K K2
2K 4K2 increases by factor of 4
So, we have an intuitive limit on the growth rate of T(n)… doubling the size of the
input to the algorithm could increase the number of operations by up to a factor of 4.
![Page 5: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/5.jpg)
Asymptotics
Data Structures & Algorithms
5
CS@VT ©2000-2017 McQuain
Big-O Theorems
For all the following theorems, assume that f(n) is a non-negative function of n and that K
is an arbitrary positive constant.
Theorem 2: A polynomial is O(the term containing the highest power of n)
Theorem 3: K*f(n) is O(f(n)) [i.e., constant coefficients can be dropped, reflexivity]
Theorem 1: K is O(1)
)7( is 1000537)( 424 nOnnnnf
)( is 7)( 44 nOnng
Theorem 4: If f(n) is O(g(n)) and g(n) is O(h(n)) then f(n) is O(h(n)). [transitivity]
)( is 1000537)( 424 nOnnnnf
![Page 6: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/6.jpg)
Asymptotics
Data Structures & Algorithms
6
CS@VT ©2000-2017 McQuain
Big-O Theorems
Theorem 5: Each of the following functions is strictly big-O of its successors:
K [constant]
logb(n) [always log base 2 if no base is shown]
n
n logb(n)
n2
n to higher powers
2n
3n
larger constants to the n-th power
n! [n factorial]
nn
smaller
larger
)2( and )( and ))log(( is )log(3)( 2 nOnOnnOnnnf
![Page 7: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/7.jpg)
Asymptotics
Data Structures & Algorithms
7
CS@VT ©2000-2017 McQuain
Big-O Theorems
Theorem 6: In general, f(n) is big-O of the dominant term of f(n), where
“dominant” may usually be determined from Theorem 5.
Theorem 7: For any base b, logb(n) is O(log(n)).
)( is 10005)log(37)( 22 nOnnnnnf
)3( is 100000037)( 4 nn Onng
)( is ))log((7)( 2nOnnnnh
![Page 8: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/8.jpg)
Asymptotics
Data Structures & Algorithms
8
CS@VT ©2000-2017 McQuain
Big-Omega
In addition to big-O, we may seek a lower bound on the growth of a function:
Definition: Suppose that f(n) and g(n) are nonnegative functions of n. Then we say
that f(n) is (g(n)) provided that there are constants C > 0 and N > 0 such
that for all n > N, f(n) Cg(n).
Big- expresses a lower bound on the growth rate of a function, for sufficiently large
values of n.
Analagous theorems can be proved for big-Ω.
![Page 9: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/9.jpg)
Asymptotics
Data Structures & Algorithms
9
CS@VT ©2000-2017 McQuain
Big-O and Big-Ω Meet
Recall the function:25 5
( ) 22 2
T n n n
We concluded earlier that T(n) is O(n2); in fact, that T(n) <= 5n2 if n >= 2.
It's also easy to show that T(n) is Ω(n2); in fact, that T(n) >= 2n2 if n >= 0.
We have the situation shown at right:
The function T(n) is "trapped" between
an upper and lower bound, from n = 2
onwards…
… and the upper bound and the lower
bound both depend on the same
underlying function, n2.
![Page 10: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/10.jpg)
Asymptotics
Data Structures & Algorithms
10
CS@VT ©2000-2017 McQuain
Big-Theta
Finally, we may have two functions that grow at essentially the same rate:
Definition: Suppose that f(n) and g(n) are nonnegative functions of n. Then we say
that f(n) is (g(n)) provided that f(n) is O(g(n)) and also that f(n) is
(g(n)).
If f is (g) then, from some point on, f is bounded below by one multiple of g and
bounded above by another multiple of g (and vice versa).
So, in a very basic sense f and g grow at the same rate.
Alernative: Suppose that f(n) and g(n) are nonnegative functions of n. Then we say
that f(n) is (g(n)) provided that there exist constants C1 > 0, C2 > 0 and
N > 0 such that, for all n > N, C1g(n) ≤ f(n) ≤ C2g(n).
![Page 11: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/11.jpg)
Asymptotics
Data Structures & Algorithms
11
CS@VT ©2000-2017 McQuain
Big-O and Big-Ω Meet Redux
Recall the function:25 5
( ) 22 2
T n n n
T(n) is O(n2) and Ω(n2).
So, T(n) grows in the same way that n2 grows…
![Page 12: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/12.jpg)
Asymptotics
Data Structures & Algorithms
12
CS@VT ©2000-2017 McQuain
Order and Limits
The task of determining the order of a function is simplified considerably by the
following result:
Theorem 8: f(n) is (g(n)) if
ccng
nf
n0 where
)(
)(lim
Recall Theorem 7… we may easily prove it (and a bit more) by applying Theorem 8:
)ln(
)2ln(
)ln(
)2ln(lim
)2ln(
1
)ln(
1
lim)log(
)(loglim
bb
n
bn
n
n
nn
b
n
The last term is finite and positive, so logb(n) is (log(n)) by Theorem 8.
Corollary: if the limit above is 0 then f(n) is strictly O(g(n)), and
if the limit is then f(n) is strictly (g(n)).
![Page 13: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/13.jpg)
Asymptotics
Data Structures & Algorithms
13
CS@VT ©2000-2017 McQuain
Order and Limits
The converse of Theorem 8 is false. However, it is possible to prove:
Theorem 9: If f(n) is (g(n)) then
provided that the limit exists.
ccng
nf
n0 where
)(
)(lim
A similar extension of the preceding corollary also follows.
![Page 14: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/14.jpg)
Asymptotics
Data Structures & Algorithms
14
CS@VT ©2000-2017 McQuain
More Theorems
Many of the big-O theorems may be strengthened to statements about big-:
Theorem 11: A polynomial is (the highest power of n).
Theorem 10: If K > 0 is a constant, then K is (1).
kkk
kknk
k
k
naa
n
a
n
a
n
a
n
nanaa
1
1
1010 limlim
proof: Suppose a polynomial of degree k. Then we have:
Now ak > 0 since we assume the function is nonnegative. So by Theorem 8, the
polynomial is (nk).
QED
![Page 15: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/15.jpg)
Asymptotics
Data Structures & Algorithms
15
CS@VT ©2000-2017 McQuain
More Theorems
Theorems 3, 6 and 7 can be similarly extended.
Theorem 12: K*f(n) is (f(n)) [i.e., constant coefficients can be dropped]
Theorem 13: In general, f(n) is big- of the dominant term of f(n), where
“dominant” may usually be determined from Theorem 5.
Theorem 14: For any base b, logb(n) is (log(n)).
![Page 16: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/16.jpg)
Asymptotics
Data Structures & Algorithms
16
CS@VT ©2000-2017 McQuain
Strict Comparisons
For convenience, we will say that:
- f is strictly O(g) if and only if f is O(g) but f is not Θ(g)
- f is strictly Ω(g) if and only if f is Ω(g) but f is not Θ(g)
For example, n log n is strictly O(n2) by Theorem 8 and its corollary, because:
01
/1lim
loglim
loglim
2
n
n
n
n
nn
nnn
![Page 17: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/17.jpg)
Asymptotics
Data Structures & Algorithms
17
CS@VT ©2000-2017 McQuain
Big- Is an Equivalence Relation
Theorem 17: If f(n) is (g(n)) and g(n) is (h(n)) then f(n) is (h(n)). [transitivity]
Theorem 16: If f(n) is (g(n)) then g(n) is (f(n)). [symmetry]
Theorem 15: If f(n) is (f(n)). [reflexivity]
By Theorems 15–17, is an equivalence relation on the set of positive-valued functions.
The equivalence classes represent fundamentally different growth rates.
Algorithms whose complexity functions belong to the same class are essentially
equivalent in performance (up to constant multiples, which are not unimportant in
practice).
![Page 18: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/18.jpg)
Asymptotics
Data Structures & Algorithms
18
CS@VT ©2000-2017 McQuain
Applications to Algorithm Analysis
Ex 1: An algorithm with complexity function
is (n2) by Theorem 10.32
5
2
3)( 2 nnnT
Ex 2: An algorithm with complexity function
is O(n log(n)) by Theorem 5.
Furthermore, the algorithm is also (n log(n)) by Theorem 8 since:
2log4log3)( nnnnT
3log
243lim
log
)(lim
nnnnn
nT
nn
For most common complexity functions, it's this easy to determine the big-O and/or
big- complexity using the given theorems.
![Page 19: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/19.jpg)
Asymptotics
Data Structures & Algorithms
19
CS@VT ©2000-2017 McQuain
Complexity of Linear Storage
For a contiguous list of N elements, assuming each is equally likely to be the target of a
search:
- average search cost is (N) if list is randomly ordered
- average search cost is (log N) is list is sorted
- average random insertion cost is (N)
- insertion at tail is (1)
For a linked list of N elements, assuming each is equally likely to be the target of a
search:
- average search cost is (N), regardless of list ordering
- average random insertion cost is (1), excluding search time
![Page 20: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/20.jpg)
Asymptotics
Data Structures & Algorithms
20
CS@VT ©2000-2017 McQuain
Most Common Complexity Classes
Theorem 5 lists a collection of representatives of distinct big-Θ equivalence classes:
K [constant]
logb(n) [always log base 2 if no base is shown]
n
n logb(n)
n2
n to higher powers
2n
3n
larger constants to the n-th power
n! [n factorial]
nn
Most common algorithms fall into one of these classes.
Knowing this list provides some knowledge of how to compare and choose the right algorithm.
The following charts provide some visual indication of how significant the differences are…
![Page 21: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/21.jpg)
Asymptotics
Data Structures & Algorithms
21
CS@VT ©2000-2017 McQuain
Graphical Comparison
Common Growth Curves
0
200
400
600
800
1000
1200
1 2 3 4 5 6 7 8 9 10 11
n (input size)
log n
n
n log n
n^2
n^3
2^n
10^n
![Page 22: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/22.jpg)
Asymptotics
Data Structures & Algorithms
22
CS@VT ©2000-2017 McQuain
Lower-order Classes
Low-order Curves
0
20
40
60
80
100
120
1 3 5 7 9
11
13
15
17
19
21
23
n (input size)
log n
n
n log n
n2
For significantly large
values of n, only these
classes are truly
practical, and whether
n2 is practical is
debated.
![Page 23: Big-O Analysis Asymptotics 1cs3114/Spring18/wmcquain/Notes/T09... · inequality does, in fact, hold). Asymptotics Data Structures & Algorithms 2 CS@VT ©2000-2017 McQuain Big-O Example](https://reader034.vdocument.in/reader034/viewer/2022050301/5f6a3c241302174b947deab7/html5/thumbnails/23.jpg)
Asymptotics
Data Structures & Algorithms
23
CS@VT ©2000-2017 McQuain
Proof of Theorem 8
Theorem 8: f(n) is (g(n)) if
ccng
nf
n0 where
)(
)(lim
Suppose that f and g are non-negative functions of n, and that
ccng
nf
n0 where
)(
)(lim
Then, from the definition of the limit, for every ε > 0 there exists an N > 0 such that
whenever n > N:
( ) from which we have ( ) ( ) ( ) ( ) ( )
( )
f nc c g n f n c g n
g n
Let ε = c/2, then we have that:3
( ) ( ) whence is ( )2
cf n g n f g
( ) ( ) whence is ( )2
cg n f n f g
Therefore, by definition, f is Θ(g).