


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The solutions to midterm 2 of cs 173, spring 2008. The problems include true/false statements, short answers, recursive definitions, a recurrence relation, and algorithms. Students are expected to understand concepts related to sets, functions, logarithms, factorials, recursion, binary search, and induction.
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Label each of the following statements as true or false.
(a) ∃x ∈ Z, ∀y ∈ Z, x < y Solution: False. The statement claims that there is one particular integer x which is smaller than all integers (including itself).
(b) n + log 2 n is O(n)
Solution: True. As n gets large, log 2 n becomes very small compared to n so we can ignore that term.
(c) n! is O(2n) Solution: False. First, we showed in lecture 16 that 2n^ < n! when n ≥ 4. Second, as n gets large, the n! increases at a rate proportional to n whereas 2n^ increases only by a factor of 2. This is perhaps the best way to remember which way this inequality goes.
(d) n log 8 n is Θ(n log 2 n)
Solution: True. To change the base of a log, you multiply by a fixed constant. By “constant”, I mean a number which depends on the two bases (2 and 8) but not on the input n. Constant multipliers don’t change big-O growth.
(e) The function g : Z → R defined by g(x) = x + 1 is onto. Solution: False. How can it be onto, since there are a lot fewer integer inputs than there are real number outputs? Or, said another way, the output of g is always an integer, so non-integer values (e.g. 3.1415) can never be outputs of g.
(a) The number of ways to pick a k-element subset from a set containing n elements is written C(n, k) or
n k
. Give an equation for computing this quantity using factorials.
Solution:
n! k!(n − k)!
BTW, this won’t be the last time someone expects you to remember this particular formula. It’s a good one to get memorized.
(b) Let f : N → Z be defined by f (x) = 7x + 12. Prove that f is one-to-one.
Solution: Let x and y be two natural numbers and suppose that f (x) = f (y). Since f (x) = f (y), 7x + 12 = 7y + 12 by the definition of f. So, by high school algebra, 7 x = 7y and therefore x = y. So, we’ve shown if f (x) = f (y) then x = y. This means that f is one-to-one.
(a) Here is a recursive definition of a set S, which contains pairs of numbers:
Give a non-recursive definition for the set S. Explain briefly and/or show your work. Solution: Since (2, 1) ∈ S and (2, 1) ∈ S, (2, 2) ∈ S by (3). Since (1, 1) ∈ S and (2, 1) ∈ S, (1, 2) ∈ S by (3). Since (2, 2) ∈ S, then (4, 1) ∈ S by (2). So if we feed (4, 1) and (2, 1) to (3), we find that (4, 2) ∈ S. If we repeat this process, we can keep multiplying the first coordinate by 2. So S must contain every pair of the form (2n, 2) or (2n, 1), where n ≥ 0. But, if (2n, 2) and (2m, 2) are in S, then (3) implies that (2n, 2 m) is in S. So S is the set of all pairs of the form (2n, 2 m), where m, n ≥ 0.
(b) Find a closed-form solution for the following recurrence relation with the given initial condition. A closed-form solution is a function T (n) that yields that same values as the recurrence relation but is non-recursive. You should be able to find the solution by unrolling the recurrence and then applying a formula you have seen before to find a closed form for a summation. Show your work. T (n) = T (n − 1) + 2n with initial condition T (0) = 0 Solution:
T (n) = T (n − 1) + 2n = T (n − 2) + 2(n − 1) + 2n = T (n − 3) + 2(n − 2) + 2(n − 1) + 2n = T (n − 4) + 2(n − 3) + 2(n − 2) + 2(n − 1) + 2n = T (0) + 2(1 + 2 +... + n) = 2(1 + 2 +... + n)
= 2
n(n + 1) 2 = n(n + 1)
Let’s define a sequence of numbers xn as follows:
Base: x 1 = 1, x 2 = 7
Induction: for every n ≥ 2, xn+1 = 7xn − 12 xn− 1
Use induction to prove that xi = 4n^ − 3 n^ for every integer n ≥ 1. Hint: the algebra in the inductive step should work out easily.
Clarification posted during exam: You need to prove xn = 4n^ − 3 n, not xi = 4n^ − 3 n.
Solution: Proof by induction on n.
Base: If n = 1 then 4n^ − 3 n^ = 4 − 3 = 1 and x 1 is defined to be 1.
If n = 2 then 4n^ − 3 n^ = 16 − 9 = 7 and x 2 is defined to be 7.
Induction: Suppose that xk = 4k^ − 3 k^ for every k in the range [1, n], where n ≥ 2. We need to show that xn+1 = 4n+1^ − 3 n+1.
xn+1 is defined to be 7xn − 12 xn− 1. By the inductive hypothesis xn = 4n^ − 3 n^ and xn− 1 = 4 n−^1 − 3 n−^1. Substituting in these values, we get that
xn+1 = 7 xn − 12 xn− 1 = 7(4n^ − 3 n) − 12(4n−^1 − 3 n−^1 ) = 7 · 4 n^ − 7 · 3 n^ − 12 · 4 n−^1 + 12 · 3 n−^1 = 7 · 4 n^ − 7 · 3 n^ − 3 · 4 n^ + 4 · 3 n = 7 · 4 n^ − 7 · 3 n^ − 3 · 4 n^ + 4 · 3 n = (7 − 3) · 4 n^ − (7 − 4) · 3 n = 4 · 4 n^ − 3 · 3 n = 4 n+1^ − 3 n+
So xn+1 = 4n+1^ − 3 n+1, which is what we needed to show.
Commentary on solution: The hard part is setting up the assumptions and goal of the inductive step. If you’ve done that right, the algebra in the inductive step is fairly straightforward. (Or, if it didn’t work out due to exam pressure, you’ll still get a lot of partial credit.)
You need to use a “strong” hypothesis at the start of the inductive step, because the inductive step needs to refer back to results for two previous values: n and n − 1. This is also why we needed two base cases.