## Homework #1

#### Mostly O-notation, bounding summations

This homework involves topics that, for the most part, should have been covered in earlier courses (ICS 6D and ICS 46).

# problems
1 GT Exercise R-1.7 on page 42
Order the following list of functions by the big-Oh notation. group together those functions that are θ( ) of one another.
 6n log n 2100 log log n log2 n 2log n 22n ⌈√n⌉ n0.01 1/n 4n3/2 3n0.5 5n ⌊2n log2 n⌋ 2n n log4 n 4n n3 n2 log n 4lg n √(log n)
2 GT Exercise R-1.17 on page 44, but assume that f (n) and g(n) are non-negative functions.
Show that O( max{ f (n), g(n)} ) = O ( f (n)+g(n) ).
3 GT Exercise R-13.2 on page 392
Let G be a simple connected graph with n vertices and m edges. Explain why O(log m) is O(log n).
4 CLRS Exercise C.3-3 on page 1200
A carnival game consists of three dice in a cage. A player can bet a dollar on any of the numbers 1 through 6. The cage is shaken, and the payoff is as follows. If the player's number doesn't appear on any of the dice, he loses his dollar. Otherwise, if his number appears on exactly k of the three dice, for k = 1, 2, 3, he keeps his dollar and wins k more dollars. What is his expected gain from playing the carnival game once?
5 CLRS Exercise A.2-1 on page 1156
Show that ∑k=1 to n ( 1/k2 ) is bounded above by a constant.
6 GT Exercise C-1.4 on page 45
What is the total running time of counting from 1 to n in binary if the time needed to add 1 to the current number i is proportional to the number of bits in the binary expansion of i that must change in going from i to i+1?
7 GT Exercise C-3.12 on page 112 (Note that, in GT, log means log2)
Without using calculus, show that, if n is a power of 2 greater than 1, then, for Hn, the nth harmonic number, Hn ≤ 1 + Hn/2.
Use this fact to conclude that Hn ≤ 1 + ⌈lg n⌉, for any n ≥ 1.
8 GT Exercise C-1.8 on page 45
Al and Bill are arguing about the performance of their sorting algorithms. Al claims that his O(n log n)-time algorithm is always faster than Bill's O(n2)-time algorithm. To settle the issue, they implement and run the two algorithms on many randomly generated data sets. To Al's dismay, they find that if n < 100, the O(n2)-time algorithm actually runs faster, and only when n > 100 is the O(n log n)-time algorithm better.

Explain why this scenario is possible. You may give numerical examples.

9 Resolve the following questions, with proof:
1. n log n = O(n2)?
2. n/log n = O(n)?
3. (log n)363 = O(n)?
4. 2n = O(2n/2)?
5. 3n = O(2n)?
10 The following is known about functions  f and g:
 f (n) = θ(n) ; f (1) = 1 ; f (2) = 2 g(n) = θ(n3) ; g(1) = 1 ; g(2) = 8
Is  f (3) < g(3)?     Explain why or why not.

# hard problems
11 Prove by induction that, for all n ≥ 6, the unit square can be partitioned into n squares (not necessarily of identical size).
12 Consider the following induction "proof" that all sheep in a flock are the same color:

Base case: One sheep. It is clearly the same color as itself.

Induction step: A flock of n sheep. Take a sheep, a, out of the flock. The remaining n-1 are all the same color by induction. Now put sheep a back in the flock, and take out a different sheep, b. By induction, the n-1 sheep (now with a in their group) are all the same color. Therefore, a is the same color as all the other sheep; hence, all the sheep in the flock are the same color.

What is wrong with this "proof"?
13 Consider the following "proof" that the Fibonacci function, F(n), defined as F(1) = 1, F(2) = 2, F(n) = F(n-1) + F(n-2), is O(n):

Base case (n ≤ 2): F(1) = 1, which is O(1), and F(2) = 2, which is O(2).

Induction step (n > 2): Assume the claim is true for all n' < n. Consider n. F(n) = F(n-1)+F(n-2). By induction, F(n-1) is O(n-1) and F(n-2) is O(n-2). Then, F(n) is O((n-1) + (n-2)), by the identity presented in Exercise R-1.16. Therefore, F(n) is O(n), since O((n-1)+(n-2)) is O(n).

What is wrong with this "proof"?
14 You are given values x, y1, ..., yn that are distinct integers randomly chosen with equal probability from 1 to 2n. The following is an efficient comparison-based algorithm that determines whether x has value greater than all of the yj.

Compare x one at a time to each of yj until either
(1) a comparison indicates that one of the yj > x, in which case return the answer NO, or
(2) comparisons with every one of the yj indicate that x > yj, in which case return the answer YES.

Determine the average-case number of comparisons performed by this algorithm.