Вы находитесь на странице: 1из 6

CSE 21 Spring 2018

Group Homework 2
Due: Saturday, April 21, 2017 at 11:59pm

Instructions

This homework should be done in groups of one to three students, without assistance from
anyone besides the instructional staff and your group members. Homework must be submitted
through Gradescope by a single representative of your group and received by 11:59pm on
the due date. There are no exceptions to this rule.

You will be able to look at your scanned work before submitting it. You must type your
solutions. (hand-drawn diagrams are okay.) Your group representative can resubmit your
assignment as many times as you like before the deadline. Only the most recent submission
will be graded.

Students should consult their textbook, class notes, lecture slides, podcasts, group members,
instructors, TAs, and tutors when they need help with homework. You may ask questions
about the homework in office hours, but not on Piazza.

Your work in this class will be evaluated not only on the correctness of your answers, but on
your ability to present your ideas clearly and logically. When asked to explain or justify, present
clearly how you arrived at your conclusions and justify the correctness of your answers with
mathematically sound reasoning. Whether you use formal proof techniques or write a more
informal argument for why something is true, your answers should always be well-supported.
Your goal should be to convince the reader that your results and methods are sound.

Required Reading Rosen Chapter 5 and Sections 8.1-8.3.

Key Concepts recursive structure of a problem, recurrence relations, guess-and-check method


for solving recurrences, unraveling recurrences, Big-O notation, Asymptotic notation.

1. For problems (a)-(e), it is not required to solve the recurrence:


(a) (3 points) In a round-robin tennis tournament with n players, every tennis player plays against
every other player. Let T (n) be the total number of tennis matches taking place among the n
players. Find a recurrence for T (n) and explain in words why T (n) satisfies this recurrence.
Solution: We can think of a tournament with n people as a tournament with n − 1 people, each
playing against each other, plus one additional person who plays against each of the other n − 1
people. So the recurrence is

T (n) = T (n − 1) + (n − 1), with T (2) = 1

or equivalently,
T (n) = T (n − 1) + (n − 1), with T (1) = 0.

(b) (3 points) In a board game, players must place colored tiles in a single row. There are red 1 × 1
tiles, yellow 2 × 1 tiles, blue 2 × 1 tiles, purple 3 × 1 tiles, and green 3 × 1 tiles. Let C(n) be the
number of different n × 1 colored rows that can be created using these tiles. Find a recurrence for
C(n) and explain in words why C(n) satisfies this recurrence.

1
Solution: Consider the first colored tile in the row. If it is a red 1 × 1 tile, then the remainder of
the row is just an n − 1 × 1 row of colored tiles, and the number of those is C(n − 1). If the first
cell is a yellow 2 × 1 tile, then the remainder of the row is an n − 2 × 1 row of colored tiles, and the
number of those is C(n − 2). We can use the same logic for each of the possible first tiles. Then
the total number of ways to fill in the n × 1 row is the number of ways starting with a red tile plus
the number of ways starting with a yellow tile, etc. Therefore, the recurrence is

C(n) = C(n − 1) + C(n − 2) + C(n − 2) + C(n − 3) + C(n − 3)


= C(n − 1) + 2C(n − 2) + 2C(n − 3),
with C(0) = 1, C(1) = 1, C(2) = 3 or equivalently,
C(n) = C(n − 1) + C(n − 2) + C(n − 2) + C(n − 3) + C(n − 3)
= C(n − 1) + 2C(n − 2) + 2C(n − 3),
with C(1) = 1, C(2) = 3, C(3) = 7.

(c) (3 points) Let D(n) be the number of ways to put n dominoes on a 2 × n grid. (see picture for 2
different ways of putting 10 dominoes in a 2 × 10 grid.) Find a recurrence for D(n) and explain in
words why D(n) satisfies this recurrence.

Solution: Each 2 × n grid either ends with a vertical domino or two horizontal dominoes. Suppose
we have a grid that ends with a vertical domino, then the remainder of the grid is a 2 × (n − 1) grid,
and there are D(n − 1) such grids. Now consider a grid which ends in two horizontal dominoes,
then the remainder of the grid is a 2 × (n − 2) grid, and there are D(n − 2) such grids. Since these
two cases are disjoint we can add these together, resulting in the recurrence,

D(n) = D(n − 1) + D(n − 2), withD(0) = 1, D(1) = 1

or equivalently,
D(n) = D(n − 1) + D(n − 2), withD(1) = 1, D(2) = 2

(d) (3 points) Any binary string can be broken into contiguous chunks of the same character, called
runs. For example, 001110100000 has:
• a run of 0s of length 2
• a run of 1s of length 3
• a run of 0s of length 1
• a run of 1s of length 1
• a run of 0s of length 5
Let B(n) be the number of length n binary strings where each run of 1s has even length. Find a
recurrence for B(n) and explain in words why B(n) satisfies this recurrence.

Solution: Every binary string either starts with 0 or 1. Suppose we have a binary string where
each run of 1s has even length, and the string starts with 0. Then the remainder of the string is
a length n − 1 binary string where each run of 1s has even length, and there are B(n − 1) such
strings. Now, suppose we have a binary string where each run of 1s has even length, and the string
starts with 1. Then the second bit of the string must also be 1, otherwise the string would start
with a run of length one, which is odd. After these first two 1s, the remainder of the string is

2
a length n − 2 binary string where each run of 1s has even length, and there are B(n − 2) such
strings. Therefore, the recurrence is

B(n) = B(n − 1) + B(n − 2), with B(0) = 1, B(1) = 1

or equivalently,
B(n) = B(n − 1) + B(n − 2), with B(1) = 1, B(2) = 2.

(e) (3 points) Let S(n) be the number of subsets of {1, 2, . . . , n} having the following property: there
are no three elements in the subset that are consecutive integers. Find a recurrence for S(n) and
explain in words why S(n) satisfies this recurrence.

Solution: Consider all subsets of {1, 2, . . . , n} having the property that no three elements in the
subset are consecutive integers. Each such subset falls in one of two cases, based on whether the
subset contains the largest possible integer, n.
Case 1: The subset does not contain n. In this case, we can think of the subset as being a subset of
{1, 2, . . . , n − 1} having the property that no three elements in the subset are consecutive integers.
Therefore, the number of subsets that fall into this case is S(n − 1).
Case 2: The subset contains n. Then this subset falls into one of the following cases, based on
whether it also contains n − 1:
Case 2a: The subset contains n and n − 1. In this case, we know n − 2 must not be an element
of the subset, otherwise it would contain three consecutive integers. Then, we can think of the
subset as a subset of {1, 2, . . . , n − 3} having the property that no three elements in the subset are
consecutive integers, together with n and n − 1. Therefore, the number of subsets that fall into
this case is S(n − 3).
Case 2b: The subset contains n but not n − 1. In this case, we can think of the subset as being a
subset of {1, 2, . . . , n − 2} having the property that no three elements in the subset are consecutive
integers, together with n. Therefore, the number of subsets that fall into this case is S(n − 2).
Then the recurrence is

S(n) = S(n − 1) + S(n − 2) + S(n − 3), with S(0) = 1, S(1) = 2, S(2) = 4

or equivalently,

S(n) = S(n − 1) + S(n − 2) + S(n − 3), with S(1) = 2, S(2) = 4, S(3) = 7.

2. (a) (6 points) Suppose a function f (n) is defined by the following recursive formula.

f (n) = 5 ∗ f (n − 1) − 6 ∗ f (n − 2), f (0) = 1, f (1) = 2

Use the guess-and-check method to get a closed-form formula for f (n). That is, guess a formula
for f (n) and use induction to prove that your guess is correct.
Solution: The following table suggests that the formula for f (n) may be f (n) = 2n .
n 0 1 2 3 4 5
f(n) 1 2 4 8 16 32
We can prove this guess by strong induction on n. For the base cases, when n = 0, we have
f (0) = 1 as given by the recurrence and 20 = 1, so the formula holds. Similarly, when n = 1,
we have f (1) = 2 as given by the recurrence and 21 = 2, so the formula holds. Assume as
our strong inductive hypothesis that f (k) = 2k , for any value of k between 1 and n − 1. Then

3
f (n) = 5 ∗ f (n − 1) − 6 ∗ f (n − 2) by the recursive formula,and we know from applying the inductive
hypothesis that f (n − 1) = 2n−1 and f (n − 2) = 2n−2 . Then

f (n) = 5 ∗ f (n − 1) − 6 ∗ f (n − 2)
= 5 × 2n−1 − 6 × 2n−2
= 2n−2 (5 × 2 − 6)
= 2n−2 × 4
= 2n−2 × 22
= 2n

Therefore by induction we have shown that f (n) = 2n .


(b) (6 points) Suppose a function g(n) is defined by the following recursive formula.

g(n) = g(n − 1) + 2n, g(1) = 2.

Use the unraveling method to get a closed-form formula for g(n). Make sure to show your work.
Solution:

g(n) = 2n + g(n − 1)
= 2n + 2(n − 1) + g(n − 2)
= 2n + 2(n − 1) + 2(n − 2) + g(n − 3)
= 2n + 2(n − 1) + 2(n − 2) + 2(n − 3) + g(n − 4)
..
.
= 2n + 2(n − 1) + · · · + 2(n − (k − 1)) + g(n − k)
..
.
= 2n + 2(n − 1) + · · · + 2(n − ((n − 1) − 1)) + g(n − (n − 1)) letting k=n-1
= 2n + 2(n − 1) + · · · + 2(2) + g(1)
= 2n + 2(n − 1) + · · · + 2(2) + 2
= 2 ∗ (1 + 2 + . · · · + n − 1 + n)
n(n + 1)
=2∗
2
= n(n + 1)

3. Prove the following, using either the definition of Big-O or a limit argument.
(a) log2 (n) ∈ O(n/ log2 (n))

4
log2 (n)
Solution: We will calculate the limit limn→∞ (n/ log2 (n)) and show that it goes to 0.

2
log2 (n) (log2 (n))
lim = lim
n→∞ (n/ log2 (n)) n→∞ n
d 2
dn (log2 (n))
= lim d
dn n
n→∞

2 log2 (n)(1/n) log2 (e)


= lim
n→∞ 1
log2 (n)
= 2 log2 (e) lim
n→∞ n
d
log (n)
= 2 log2 (e) lim dn d 2
dn n
n→∞

log2 (e)/n
= 2 log2 (e) lim
n→∞ 1
2 1
= 2 (log2 (e)) lim
n→∞ n
= 0

(b) 2n ∈ O(n!)
Solution: Let C = 1 and k = 3 then we will show that 2n < (1)n! for all n > 3:
Base Case: 24 < 4!
Inductive Hypothesis: Suppose that for some n > 3, 2n < n!.
Inductive step:

2n+1 = 2(2n ) < 2(n!) < (n + 1)(n!) = (n + 1)!

Conclusion: Therefore 2n < (1)n! for all n > 3.


(c) log2 (n2 ) + log2 (100n10 ) ∈ O(log2 (n))
log2 (n2 )+log2 (100n10 )
Solution: We will calculate the limit limn→∞ log2 (n) and show that it is
equal to a positive constant.

log2 (n2 ) + log2 (100n10 ) 2 log2 (n) + 10 log2 (100) + 10 log(n)


lim = lim
n→∞ log2 (n) n→∞ log2 (n)
12 log2 (n) 10 log2 (100)
= lim +
n→∞ log2 (n) log2 (n)
10 log2 (100)
= 12 + lim
n→∞ log2 (n)
= 12

(d) n1/2 ∈ O(n2/3 )


n1/2
Solution: We will calculate the limit limn→∞ n2/3
and show that it is equal to 0.

n1/2
lim = lim n1/2−2/3
n→∞ n2/3 n→∞

= lim n−1/6
n→∞
= 0

(e) log3 (n) ∈ Θ(log2 (n))

5
log3 (n)
Solution: We will calculate the limit limn→∞ log2 (n) and show that it is equal to a
positive constant.

log3 (n) log2 (n)/ log2 (3)


lim = lim
n→∞ log2 (n) n→∞ log2 (n)
= 1/ log2 (3)
≈ 0.68

Since the limit of the quotient of the two functions is a constant greater than zero, it
follows that they are big-Θ of each other.
(f) 2n ∈ O(3n /n2 )
2n
Solution: We will calculate the limit limn→∞ 3n /n2 and show that it is equal to 0.

2n n2 2n
lim = lim
n→∞ 3n /n2 n→∞ 3n

n2
= lim
n→∞ (3/2)n
d 2
dn n
= lim
d n
dn (3/2)
n→∞

2n
= lim
n→∞ (3/2)n ln(3/2)
2 n
= lim
ln(3/2) n→∞ (3/2)n
d
2 n
= lim d dn n
ln(3/2) n→∞ dn (3/2)
2 1
= lim
ln(3/2) n→∞ (3/2)n ln(3/2)
2 1
= lim
(ln(3/2)) n→∞ (3/2)n
2

= 0

Вам также может понравиться