You are on page 1of 8

EECS 203: Discrete Mathematics

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)).

• Big-Θ: A function f (x) is Θ(g(x)) if f (x) is O(g(x)) and f (x) is Ω(g(x)).


This means that f (x) and g(x) grow at the same rate as x goes to infinity.

• Master Theorem: If T (n) = aT ( nb ) + nd , then



Θ(n )
 d
( bad < 1)
T (n) = Θ(nd log n) ( bad = 1)

Θ(nlogb a ) ( bad > 1)

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:

log3 (5n ) < log3 (c3n )


log3 (5n ) < log3 (c) + log3 (3n )
log3 (5n ) − log3 (3n ) < log3 (c)
5n
log3 ( n ) < log3 (c)
3
5 n
log3 (( ) ) < log3 (c)
3
5
n log3 ( ) < log3 (c)
3
log3 (c)
n<
log3 ( 53 )
n < log5/3 (c)

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

4. Section 3.2 Problem 25 (a, c)


Give as good a big-O estimate as possible for each of these functions

a) (n2 + 8)(n + 1)

b) (n log n + n2 )(n3 + 2)

c) (n! + 2n )(n3 + log(n2 + 1))

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!)

5. Section 3.2 Problem 30 (a, e)


Show that each of these pairs of functions are of the same order.
(a) 3x + 7, x
(e) log10 x, log2 x

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.

(a) For x > 1, x < 3x + 7, so C = 1 and k = 1 are witnesses to show x


is O(3x + 7).
For x > 7, 3x + 7 < 4x, so C = 4 and k = 7 are witnesses to show 3x + 7 is O(x).

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).

6. Section 3.3 Exercise 13


The conventional algorithm for evaluating a polynomial an xn +an−1 xn−1 +...+a1 x+a0
at x = c can be expressed in pseudocode by
procedure polynomial(c, a0 , a1 , a2 , ..., an : real numbers)
power := 1
y := a0
for i := 1 to n
power := power ∗ c
y := y + ai ∗ power
return y {y = an xn + an−1 xn−1 + ... + a1 x + a0 }

a.) Evaluate 3x2 + x + 1 at x = 2 by working through each step of the algorithm


showing the values assigned at each assignment step.
b.) Exactly how many multiplications and additions are used to evaluate a polynomial
of degree n at x = c? (Do not count additions used to increment the loop variable.)

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 .

b) a0 = 1, a1 = 1, a2 = 2. There is one way to climb up zero stairs (do nothing),


one way to go up one stair (one step), and two ways to go up two stairs (one
step twice or two step once).

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:

k p(k) OPT(k) T(k)


0 n/a 0 ∅
1 0 100 {J1 }
2 0 max(100, 5) = 100 {J1 }
3 1 max(100, 100 + 10) = 110 {J1 , J3 }
4 0 max(110, 20) = 110 {J1 , J3 }
5 0 max(110, 25) = 110 {J1 , J3 }
6 2 max(110, 40 + 100) = 140 {J1 , J6 }
7 4 max(140, 30 + 110) = 140 {J1 , J6 } or {J1 , J3 , J7 }

The maximum number of attendants is 140

10. 8.1 Not in Book


(a) Derive a recurrence relation for the number of binary strings of length n that do
not have consecutive 0’s. You must explain how you derive the relation.
(b) Find the solution to the recurrence relation you found in part (a).

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).

11. 8.2.4 (b)


Solve this recurrence relation together with the initial conditions given.
b) an = 7an−1 − 10an−2 for n ≥ 2, a0 = 2, a1 = 1

Solution: The characteristic equation for this recurrence relation is r2 − 7r +


10 = 0 which has the roots i.e. r = 2, 5. The general form of the solution is

7
an = c1 (2)n + c2 (5)n = an . Plugging in the initial conditions:

2 = c1 + c2
1 = 2c1 + 5c2

Solving these equations, we get c1 = 3 and c2 = −1. Therefore, an = 3 · 2n − 5n .

12. Exercise 8.2.14


Find the solution to an = 5an−2 − 4an−4 with a0 = 3, a1 = 2, a2 = 6, and a3 = 8.
(Hint: Find the characteristic equation first.)

Solution: The characteristic equation is r4 − 5r2 + 4 = 0. This factors as (r2 −


1)(r2 − 4) = (r − 1)(r + 1)(r − 2)(r + 2) = 0, so the roots are 1, −1, 2, and −2.
Therefore the general solution is an = α1 + α2 (−1)n + α3 2n + α4 (−2)n . Plugging
in initial conditions gives 3 = α1 + α2 + α3 + α4 , 2 = α1 − α2 + 2α3 − 2α4 ,
6 = α1 + α2 + 4α3 + 4α4 , and 8 = α1 − α2 + 8α3 − 8α4 . The solution to this
system of equations is α1 = α2 = α3 = 1 and α4 = 0. Therefore the answer is
an = 1 + (−1)n + 2n .

13. 8.2.20
Find the general form of the solutions of the recurrence relation an = 8an−2 − 16an−4 .

Solution: The characteristic equation is r4 − 8r2 + 16.


2
This can be factored to (r2 − 4) = (r − 2)2 (r + 2)2 .
Therefore the roots of this equation are 2, 2, -2 and -2.
So the general solution can be written as an = α1 2n +α2 n2n +α3 (−2)n +α4 n(−2)n

You might also like