Professional Documents
Culture Documents
Fall 2019
Discussion 12 - Notes
1 Definitions
• Big-O:
• Big-Ω:
• Big-Θ:
• Master Theorem:
Solution:
• Big-O: A function f (x) is O(g(x)) if there exists a C > 0 and k such that
for all x > k, |f (x)| ≤ C|g(x)|. In other words, the growth rate of g(x) as
x goes to infinity is an upper bound of the growth rate of f (x) as x goes to
infinity.
• Big-Ω: A function f (x) is Ω(g(x)) if there exists a C > 0 and k such that
for all x > k, C|g(x)| ≤ |f (x)|. In other words, the growth rate of g(x) as
x goes to infinity is a lower bound of the growth rate of f (x) as x goes to
infinity. This is the reverse of Big-O, so f (x) is Ω(g(x)) if and only if g(x)
is O(f (x)).
1
1. Section 3.2 Problem 6
Prove that (x3 + 2x)/(2x + 1) is O(x2 ).
Solution:
Let C = 32 , k = 1 (this is one possible set of witnesses).
We can use the following inequalities.[Note that making the denominator of a
fraction smaller makes the fraction larger.]
x3 + 2x x3 + 2x x3 + 2x3 3
< ≤ = x2
2x + 1 2x 2x 2
Therefore we have shown that C = 3/2 and k = 1 are valid witnesses. Thus
(x3 + 2x)/(2x + 1) is O(x2 ) since for all x > 1, |(x3 + 2x)/(2x + 1)| ≤ | 23 x2 |.
2. Not in Book 1
Prove that 5n is not O(3n )
Solution:
We can prove this by contradiction.
First we state that 5n is O(3n ), then there exist c and k such that for all n > k
5n < c3n .
Now, using the properties of logarithms we have:
As we can see this is false for n greater than log5/3 (c) which implies a contra-
diction. Therefore 5n is not O(3n ).
2
3. Section 3.2 Problem 18
Let k be a positive integer. Show that 1k + 2k + · · · + nk is O(nk+1 ).
Solution: 1k + 2k + · · · + nk ≤ nk + nk + · · · + nk = n · nk = nk+1
a) (n2 + 8)(n + 1)
b) (n log n + n2 )(n3 + 2)
Solution:
a) The fastest growing term in (n2 + 8) is n2 and the fastest growing term in
(n + 1) is n, thus the function is O(n2 · n) = O(n3 )
b) The fastest growing term in (n log n + n2 ) is n2 and the fastest growing term
in (n3 + 2) is n3 , thus the function is O(n2 · n3 ) = O(n5 )
c) The fastest growing term in (n! + 2n ) is n! and the fastest growing term in
n3 + log(n2 + 1) is n3 , thus the function is O(n3 n!)
Solution:
Note that for these types of problems, there are many possible values for C and
k, and we just show a few possibilities below.
3
(e) log2 x = log2 10 log10 x, and log10 x = log10 2 log2 x.* So for x > 1,
log2 x ≤ (log2 10) log10 x, and for x > 1, log10 x ≤ (log10 2) log2 x.
* You can see this by setting up the equality 10log10 x = 2log2 x , and then
substituting 10 with 2log2 10 (or alternatively substituting in the reverse log
expression for 2).
Solution:
a.) Initial Values:
power = 1, y = 1
Values after first iteration of loop (i = 1)
power = 2, y = 1 + 2 ∗ 1 = 3
Values after first iteration of loop (i = 2)
power = 4, y = 3 + 4 ∗ 3 = 15
Return value = 15
b.) Each iteration of the loop has 1 addition and 2 multiplications. The loop
runs n times (where n is the degree of the polynomial). Thus, there are 2n
multiplcations and n additions are used to evaluate the polynomial using this
method.
4
7. Section 3.3 Exercise 34
How many comparisons does the selection sort use to sort n items? Use your answer
to give a big-O estimate of the complexity of the selection sort in terms of number of
comparisons for the selection sort.
Solution: It takes n−1 comparisons to find the least element in the list, then n−2
comparisons to find the least element among the remaining elements, and so on.
Thus the total number of comparisons is (n−1)+(n−2)+· · ·+2+1 = n(n−1)/2,
which is O(n2 )
8. Exercise 8.1.12
a) Find a recurrence relation for the number of ways to climb n stairs if the person
climbing the stairs can take one, two, or three stairs at a time.
b) What are the initial conditions?
c) In how many ways can this person climb 8 stairs?
Solution:
a) Let an be the number of ways to climb n stairs. In order to climb n stairs, a
person must either start with a step of one stair and then climb n − 1 stairs
(and this can be done in an−1 ways), start with a step of two stairs and then
climb n − 2 stairs (and this can be done in an−2 ways), or start with a step of
three stairs and then climb n − 3 stairs (and this can be done in an−3 ways).
From this analysis we can immediately write down the recurrence relation,
valid for all n ≥ 3 : an = an−1 + an−2 + an−3 .
c)
a8 = 81
a3 =2+1+1=4
a4 =4+2+1=7
a5 = 7 + 4 + 2 = 13
a6 = 13 + 7 + 4 = 24
a7 = 24 + 13 + 7 = 44
a8 = 44 + 24 + 13 = 81
5
9. Exercise 8.1.54 (b)
Use Algorithm 1 to determine the maximum number of total attendees in the talks
in Example 6 if wi , the number of attendees of talk i, i = 1, 2, ..., 7, is
(b) 100, 5, 10, 20, 25, 40, 30.
Solution: In the textbook example, talk p(j) is the talk ending latest among
talks compatible with talk j that end before talk j ends, if such a talk exists, and
p(j) = 0 if there are no such talks.
Using example 6, p(1) = 0, p(2) = 0, p(3) = 1, p(4) = 0, p(5) = 0, p(6) = 2, p(7) =
4.
Using algorithm 1, you would get the following:
Solution:
(a) Let an be the number of such sequences. Each such sequence ends with either
(0) (1)
a 0 or a 1. Let an count those that end with a 0 and let an count those that
(0) (1)
end with a 1. So, an = an + an .
6
If a sequence of some length n − 1 ends with a 0, we can append only 1 to it. If
the sequence ends with a 1, we can append a 0 or a 1 to it. Therefore,
(0) (1)
an = an−1 + 2an−1
(0) (1) (1)
= an−1 + an−1 + an−1
(1)
= an−1 + an−1
(1) (1)
Observe that an = an−1 which means an−1 = an−2 . This gives an = an−1 + an−2
which is a linear homogeneous recurrence relation with constant coefficients and
of degree 2. Therefore, we need two initial conditions to solve this which is easy
to do so. There are two binary strings of length 1 without consecutive 0’s namely
0 and 1 and there are three binary strings of length 2 without consecutive 0’s
namely 01, 10 and 11. So the desired recurrence relation with its initial conditions
is:
an = an−1 + an−2 for n ≥ 3, a1 = 2, a2 = 3
b) The characteristic√equation of this
√
relation is r2 − r − 1 = 0 which has two
roots namely r1 = 1+2 5 and r2 = 1−2 5 . Using Theorem 3, the general solution is
√ √
1 + 5 n 1 − 5 n
an = c 1 + c2
2 2
√
3+√ 5
Using the initial conditions, you can find c1 and c2 ; in this case c1 = 2 5
and
√
5−3
c2 = √ .
2 5
Therefore,
√ √ √ √
3 + 5 1 + 5 n 5 − 3 1 − 5 n
an = √ + √
2 5 2 2 5 2
You can do the algebra to verify that you do get a1 = 2 and a2 = 3 (it might be
an instructive exercise).
7
an = c1 (2)n + c2 (5)n = an . Plugging in the initial conditions:
2 = c1 + c2
1 = 2c1 + 5c2
13. 8.2.20
Find the general form of the solutions of the recurrence relation an = 8an−2 − 16an−4 .