Massachusetts Institute of Technology Handout 48 6.042J/18.062J: Mathematics for Computer Science May 3, 2000 Professors David Karger and Nancy Lynch

Quiz 2 Solutions

Problem 1 [10 points]

Consider n > 1 people that attend a party. Prove that there must be two people who greet the same number of distinct people; assuming no one greets his/her self.

Solution. A straightforward application of the pigeonhole principle. Since no person can greet himself, the number of people he can greet must be an integer from 0 to n − 1. If no two people greet the same number of people, then every one of these n numbers from 0 to n − 1 must equal the number of greetings some person made. But that would mean that some person greets 0 people while some other person greets n − 1 people, i.e., all the other people. This is a contradiction.

Handout 48: Quiz 2 Solutions  2  Name:

Problem 2 [20 points] Counting

For the problems below, you may leave products and binomial coefficients in your final answer. Moreover, you should assume that a deck of cards consists of 52 cards; that is, 4 suits of 13 cards each (2, 3, 4, …, 9, 10, J, Q, K, A).

(a) [5 points] In poker, a flush is a set of five cards, all of the same suit. How many distinct flushes are there?

Solution. 4 · \binom{13}{5}: 4 for the suit, \binom{13}{5} for the cards chosen within that suit.

(b) [5 points] In poker, a straight is a set of five cards that, when the suits are ignored, form a five-in-a-row sequence, for example 5, 6, 7, 8, 9 or 8, 9, 10, J, Q. An ace can start a straight (A, 2, 3, 4, 5) or end it (10, J, Q, K, A) but cannot wrap around (no Q, K, A, 2, 3). How many distinct straights are there?

Solution. 4^{5} · 10: 4 suits for each card, and we can start the straight at any one of 10 cards (A through 10).

(c) [5 points] A straight flush is a set of five cards that is both a straight and a flush: 5 cards in a row, all the same suit. How many are there?

Solution. 4 · 10

(d) [5 points] How many 5-card sets are either a straight or a flush (or both)?

Solution. Inclusion-exclusion: 4^{5} · 10 + 4\binom{13}{5} − 4 · 10 Problem 3 [10 points] Counting

Consider placing n distinguishable books on r distinguishable shelves.

(a) [5 points] If the order of books on each shelf doesn’t matter, how many distinct placements are there?

Solution. Product rule: r shelves for each of n books, so r^{n}.

(b) [5 points] If order on the shelf matters, how many distinct placements are there?

Solution. For the first book there are r choices. For the next book there are r + 1 choices since we can think of the first book as splitting the shelf it is put on into two shelves. Similarly for the third book there are r + 2 choices. In this way we see that the total number of ways, by the product rule, is r × (r + 1) × (r + 2) … (n + r − 1).

Problem 4 [10 points] Order of Growth

(a) [3 points] From lecture, we know that any comparison-based sorting algorithm 𝔸 must take Ω(n log n) steps on n elements. Which one of the following is a consequence of this fact?

a) There is some constant c > 0 for which all executions of 𝔸 will perform exactly cn log n steps.

b) There is some constant c > 0 for which all executions of 𝔸 will perform at least cn log n steps.

c) There is some constant c > 0 for which some executions of 𝔸 will perform at least cn log n steps.

d) There is some constant c > 0 for which all executions of 𝔸 will perform at most cn log n steps.

e) There is some constant c > 0 for which some executions of 𝔸 will perform at most cn log n steps.

Solution. c is the correct answer. Ω-notation deals only with worst-case lower bounds; Ω(n log n) is a lower bound on the number of steps required for the worst-case input to 𝔸.

(b) [3 points] Prove that 10n^{2} = O(n^{3}).

Solution. 10n^{2} ≤ c · n^{3} for all n ≥ 1 when c ≥ 10, so the theorem follows.

(c) [4 points] Prove that n^{3} is not O(10n^{2}).

Solution. Assume ∃c such that for all n > N, n^{3} < c · 10n^{2}. But this implies that n^{3}/n^{2} = n < 10c = c · 10n^{2}/n^{2}, which is not true for any n ≥ 10c, so it is not true for all n > N. This is a contradiction.

Handout 48: Quiz 2 Solutions  5  Name:

Problem 5 [10 points] Summations

(a) [5 points] Determine the closed-form expression for the summation:

∑_{k=0}^{∞} ∑_{j=0}^{∞} x^{k+j}y^{k−j}

Solution. Rearrange:

∑_{k=0}^{∞} ∑_{j=0}^{∞} x^{k+j}y^{k−j} = ∑_{k=0}^{∞} ∑_{j=0}^{∞} x^{k}x^{j}y^{k}y^{−j}
= ∑_{k=0}^{∞} x^{k}y^{k} ∑_{j=0}^{∞} x^{j}y^{−j}
= (∑_{k=0}^{∞}(xy)^{k}) (∑_{j=0}^{∞} (\frac{x}{y})^{j})
= \frac{1}{1 − xy} · \frac{1}{1 − x/y}

when |xy| < 1 and |x/y| < 1.

(b) [5 points] Prove a tight asymptotic bound (Θ) for the expression:

∑_{k=2}^{n} \frac{1}{k ln k}

Hint: \frac{1}{x ln x} = \frac{d}{dx} ln ln x Solution. ∑_{k=2}^{n} \frac{1}{k ln k} = ∑_{k=2}^{n} \frac{d}{dk} ln ln k, so we use the integral method:

∫_{2}^{n} \frac{d}{dk} ln ln k dk ≤ ∑_{k=2}^{n} \frac{d}{dk} ln ln k ≤ ∫_{2}^{n} \frac{d}{dk} ln ln(k + 1) dk
ln ln k|_{2}^{n} ≤  ≤ ln ln(k + 1)|_{2}^{n}
ln ln n − ln ln 2 ≤  ≤ ln ln(n + 1) − ln ln 3
ln ln n − ln ln 2 ≤  ≤ ln ln(n + 1)

In the limit as n → ∞, the left term is dominated by ln ln n, so it is lower bounded by c ln ln n for some constant 0 < c < 1. Thus, the center is Ω(ln ln n) and O(ln ln n).

Problem 6 [10 points] Binomial Coefficients

Prove that

∑_{k=1}^{n}k ( \begin{matrix}n \\ k \end{matrix} ) = n2^{n−1}

(a) [5 points] using a combinatorial argument. Hint: Consider choosing a committee that is headed by a leader.

Solution. Consider the number of ways of picking a team of size k from a group of n people and designating one of them captain. This can be done in k\binom{n}{k} ways, for all k = 0, 1, …, n. But the total number of ways of doing this can also be counted differently. First select the captain - n ways - then select an arbitrary subset of the rest - 2^{n−1} ways. Hence the result follows.

(b) [5 points] by finding a closed-form expression for ∑_{k=1}^{n} ( \begin{matrix}n \\ k \end{matrix} ) kx^{k} and evaluating it at

x = 1.

Solution. Differentiating

(1 + x)^{n} = ∑_{k=0}^{n} \binom{n}{k}x^{k}

we get

n(1 + x)^{n−1} = ∑_{k=1}^{n} k\binom{n}{k}x^{k−1}

and evaluating it at x = 1 we get the desired result.

Handout 48: Quiz 2 Solutions  7  Name:

Problem 7 [10 points] Recurrences Consider the following recurrence to which the master theorem does not apply:

T (n) = 2T (n/2) + n log_{2} n

(a) [3 points] Perform two iterations of plug-and-chug to reveal a pattern for this recurrence. Solution. See below.

(b) [7 points] Determine a tight asymptotic bound (Θ) for this recurrence. You may assume n is a power of two if it is convenient. Solution. The master theorem does not apply (c.f. CLR, p.63). So, we expand:

T (n) = 2T (n/2) + n log n
= 2^{2}T (n/2^{2}) + n log \frac{n}{2} + n log n
= 2^{3}T (n/2^{3}) + n log \frac{n}{2^{2}} + n log \frac{n}{2} + n log n
⋮
= 2^{log n}T (n/2^{log n}) + n (log \frac{n}{2^{log n−1}} + ⋯ + log \frac{n}{2^{2}} + log \frac{n}{2} + log n)
= Θ(n) + n (log 2 + log 4 + ⋯ + ⋯ + log \frac{n}{2} + log n)
= Θ(n) + n (1 + 2 + ⋯ + log n)
= Θ(n) + n\frac{(log n)(1 + log n)}{2}
= Θ(n) + Θ(n log^{2} n)
= Θ(n log^{2} n)

Problem 8 [10 points] Recurrences

The Millicent computer virus has been spreading on computers around MIT. Once a copy of Millicent enters a computer, it is copied to 2 new (uninfected) computers after one day, then to 3 new computers the second day, and then detected and removed on the third day. Millicent’s creator installed Millicent on 2 computers on day 0 and 2 additional computers on day 1.

(a) [3 points] Due to a tip from inside the FBI that Millicent might be of extraterrestrial origin, Frohike and Langley have been investigating the growth of the Millicent virus; however, they’ve gotten stuck. Help them by stating a recurrence describing the number of computers newly infected on day n for n > 1.

Solution. T_{n} = 2T_{n−1} + 3T_{n−2}

(b) [4 points] Find a closed form for the recurrence you stated in the previous part.

Solution. The characteristic equation is x^{2} − 2x − 3 = 0, which has roots at 3 and −1. The solution is then of the form

T_{n} = A3^{n} + B(−1)^{n}

We plug in the given initial conditions:

T_{0} = 2 = A + B
T_{1} = 2 = 3A − B

Solving this system yields the coefficients A = B = 1. So, the closed form for this recurrence is simply T_{n} = 3^{n} + (−1)^{n}.

(c) [3 points] Fearing the efforts of the mighty Frohike and suave Langley, Millicent’s creator became frightened and decided to start installing the virus on 4 new computers every day after day 1. State a recurrence for the revised problem and find a closed form solution for the number of computers newly infected on day n.

Solution. The general solution to the homogeneous recurrence is of the form

T_{n} = A3^{n} + B(−1)^{n}

as above. Now we need to find a particular solution to the inhomogeneous recurrence. Since the inhomogeneous term is constant, we guess a constant solution b:

b = 2b + 3b + 5 ⟹ b = −1

Therefore, the inhomogeneous recurrence has the simple particular solution T_{n} = −1. We now need to determine the constants on the complete solution

T_{n} = A3^{n} + B(−1)^{n} − 1

by plugging in the initial conditions and solving the resulting system:

T_{0} = 2 = A + B − 1
T_{1} = 2 = 3A − B − 1

which has the solution A = B = 3/2. The closed form for this recurrence is then

T_{n} = \frac{3}{2}3^{n} + \frac{3}{2}(−1)^{n} − 1

We can perform a sanity check to make us more confident of our answer:

T_{2} = 2 · 2 + 3 · 2 + 4 = 14
T_{2} = (3/2)3^{2} + (3/2)(−1)^{2} − 1 = 14
T_{3} = 2 · 14 + 3 · 2 + 4 = 38
T_{3} = (3/2)3^{3} + (3/2)(−1)^{3} − 1 = 38

Problem 9 [10 points] Countability

(a) [5 points] A function f : ℕ → ℕ is strictly increasing if f(n+1) > f(n) for all n. Prove that the set of such strictly increasing functions is uncountable. Hint: Use diagonalization.

Solution.

To use diagonalization, we must assume that the set is countable, then find a contradiction by performing two steps:

1. Describe an arbitrary enumeration (ordered listing) of all the elements in the set.

2. Construct an item that is in the set but not equal to any element of the enumeration.

The emphasized elements are the crucial components; most false diagonalization proofs fail to satisfy one of those conditions.

The simplest diagonalization may be the following: List the functions in some arbitrary enumeration F, where F_{i} is the i^{th} element of the list. Now, for any F_{i}(x), the next value of the function, F_{i}(x + 1), is either 1 greater than F_{i}(x) or more than 1 greater than F_{i}(x). We can now make a new function, F_{d}, where F_{d}(x) = F_{d}(x − 1) + 1 if F_{x}(x) − F_{x}(x − 1) > 1 and F_{d}(x − 1) + 2 otherwise. F_{d} must be in the set, since it is strictly increasing. However, F_{d} cannot be equal to any F_{i}, since it does not increase by the same amount from its value at i − 1 to its value at i.

The proof could also have been done by making F_{d} increase by 1 more than F_{x}(x) − F_{x}(x) from F_{d}(x − 1) to F_{d}(x). We could even construct F_{d}(x) as F_{x}(x) + F_{d}(x − 1).

In fact, an even simpler proof could have been to merely notice that the above analysis suggests a surjection from the set of increasing functions to the set of infinite binary strings, since every increase may either be 1 or more than 1:

\begin{aligned}F_{0} & : & 2 & 3 & 7 & 8 & 9 & ... \\ F_{1} & : & 6 & 7 & 10 & 13 & 22 & ... \\ F_{2} & : & 2 & 4 & 8 & 9 & 16 & ... \\ F_{3} & : & 1 & 5 & 6 & 7 & 40 & ... \end{aligned}  (1)

maps to:

\begin{aligned}F′_{0} & : & 0 & 1 & 0 & 0 & ... \\ F′_{1} & : & 0 & 1 & 1 & 1 & ... \\ F′_{2} & : & 1 & 1 & 0 & 1 & ... \\ F′_{3} & : & 1 & 0 & 0 & 1 & ... \end{aligned}  (2)

This is an injection to the set of infinite bit strings, which we have shown to be uncountable. (We could also have made a surjection from increasing functions to decimal expansions of reals, with the n^{th} digit of r_{i} being F_{i}(n) mod 10...)

(b) [5 points] A function f : ℕ → ℕ is strictly decreasing if f(n + 1) < f(n), for all n such that f(n) > 0, and f(n + 1) = 0, for all n such that f(n) = 0. For example, the function f could be f(0) = 5, f(1) = 3, f(2) = 0, f(3) = 0, · · ·.

Prove that the set of such strictly decreasing functions is countable. Hint: First prove that, for any k ∈ ℕ, the set of strictly decreasing functions for which f(0) = k is countable.

Solution. Suppose f(0) = k. Then, since f(x) decreases by at least 1 for each increment of x, f(k) must be 0. By the definition, f(x) = 0 for all x > k.

We can describe each f where f(0) = k by noting which numbers from 1 to k are in the range. (It can never be larger than k, because the function is decreasing). In fact, this uniquely defines each of the functions. We can view that as a finite bitstring of length k, where position i is 1 if i is in the range and 0 otherwise. Because k can have any value, the decreasing functions map to finite bitstrings of all lengths. This is thus an injection into the set of finite bitstrings. (Actually, a bijection, if we don’t include the bit for k itself). Since we have proven before that the set of finite bitstrings is countable, the set of decreasing functions must also be countable.

We could have instead shown that functions of a given starting k, there are only 2^{k−1} possible different functions, corresponding to whether each number from 1 to k − 1 is in the range or not. Thus, there are a finite number of decreasing functions for each starting k. Since k can only be any natural number, there are a countable number of such sets comprising all of the decreasing functions. A union of countably-many finite sets is countable.

We could even have made a direct injection into the natural numbers or finite bitstrings by mapping each f to f(0) 1’s followed by 0 followed by f(1) 1’s followed by 0 followed by f(2) 1’s followed by 0 followed by … for all f(i) > 0.