Homework 1 Solutions
Homework 1 Solutions
Homework 1 Solutions
Homework 1 Solutions
Released 4pm Friday Sep 8, 2017
This homework has a total of 4 problems on 4 pages. Solutions should be submitted to GradeScope
before 3:00pm on Wednesday, September 6, 2017.
It will be marked out of 20, you can earn up to 21 = 1 + 5 + 8 + 3 + 4 points.
Late policy: each student can use up to two late days on each homework, for a total of four late
days across all four homeworks.
If you choose not to submit a typed write-up, please write neat and legibly.
Collaboration is allowed/encouraged on problems, however each student must independently com-
plete their own write-up, and list all collaborators. No credit will be given to solutions obtained
verbatim from the Internet or other sources.
(a) Submit either a scan or photo of your homework in a single pdf file on GradeScope.
(b) Words on the scan are clearly readable.
(c) The answer to each question except question 0 should start on a new page.
(a) (1 point) Formally prove that Big O is transitive by relation. That is, if f (n) ≤ O(g(n))
and g(n) ≤ O(h(n)), then, f (n) ≤ O(h(n)).
SOLUTION:
By definition of big-O, there exists constants C1 and C2 such that for all n we have
f (n) ≤ C1 g (n)
and
g (n) ≤ C2 h (n) .
Multiplying the second one by C1 then gives:
C1 g (n) ≤ C1 · C2 h (n) ,
1
(b) (1 point) Find the complexity of the following set loops, where n is given as input:
i <-- n;
while(i > 1) {
j = i; //%% CAUTION: this DOES NOT START AT 0
while (j < n) {
k <-- 0;
while (k < n) {
k = k + 2;
}
j <-- j * 2;
}
i <-- i / 2;
}
Express your answer using the Θ(·) notation.
SOLUTION:
The innermost lopp takes Θ(n) whenever it is called.
The outer most loop takes Θ(log n) steps, and between steps log n/2 and log n ( a total of
Θ(log n) of steps), we have i < n1/2 .
In each such step, j gets doubled Θ(log n) times before it reaches n. So we get a total of
Θ(log2 n) iterations, each costing Θ(n), for a total of Θ(n log2 n).
(c) (1 point) Prove or disprove: there does not exist a pair of functions f (n) and g(n) such that
f (n) ≤ O(g(n)) and f (n) ≥ Ω(g(n)).
SOLUTION:
False, f (n) = g(n) = n are a pair of such functions.
(d) (1 point) Prove or disprove: n2 log10 n ≤ O(n2.1 ).
SOLUTION:
True. This follows from combining n2 ≤ O(n2 ) with log10 n ≤ O(n0.1 ).
(e) (1 point) Prove or disprove: 22n ≤ O(2n ).
SOLUTION:
False, for any constant C, once we have 2n > C, we get
2
for some constants a > 0, b > 1, and d >= 0
Find the asymptotic (big-O notation) running time of the following algorithms using Master
theorem if possible. State the runtime recurrence if it’s not given, and if Master theorem is
applicable, explicitly state the parameters a, b and d. Otherwise, give a quick reason that the
recurrence relation is not solvable using Master theorem.
SOLUTION:
a = 3, b = 4, d = 1.
logb a = log4 3 ≈ .79 < 1, so O(n).
(b) (2 points) An algorithm with the run-time recurrence:
SOLUTION:
3
(d) (2 points) An algorithm solves problems by diving a problem of size n into 2n sub-problems
of half the size and recursively solves the smaller sub-problems. It takes linear time to
combine the solutions of the sub-problems.
SOLUTION:
(a) (2 points) Write down a runtime recurrence for Buzzsort and analyze its asymptotic running
time.
SOLUTION:
3
T (n) = 3T n + O (n) .
5
This fits into the requirements of Master theorem with a = 3, b = 35 , and d = 1. log 5 3 ≈
3
2.15 > 1, so the running time is O(n2.16 ).
4
(b) (1 point) Give an example sequence on 5 or 10 integers where Buzzsort does not terminate
with the correct answer.
SOLUTION:
on the input sequence
5, 4, 3, 2, 1.
The first recursive sort produces
3, 4, 5, 2, 1,
the second sort produces
3, 4, 1, 2, 5,
and the third produces
1, 3, 4, 2, 5,
which is not sorted.
(a) (1 point) Show that if p(x) and q(x) are degree n polynomials with integer coefficients in
the range [0, n], all coefficients in the product p(x) · q(x) are integers in the range [0, O(n3 )].
SOLUTION:
Each of the aj · bi−j term is at most n2 , n of them gives O(n3 ).
(b) (2 points) Show using an extension of Karatsuba’s algorithm that two degree n polynomials
can still be multiplied in O(n1.6 ) time or better. You may assume that integer arithemtics
involving poly(n) sized numbers take O(1) time.
SOLUTION:
We modify Karatsuba’s algorithm by passing around polynomials. The operations of addi-
tion / subtraction still works in linear time, and the key identity becomes:
= x2k − xk p1 (x) q1 (x) − xk − 1 p2 (x) q2 (x) + xk [p1 (x) + p2 (x)] [q1 (x) + q2 (x)] .
5
(c) (1 points) The ‘shifted dot products’ of two sequences y0 . . . yn and z0 . . . zn for each shift s
is given by
Xn−s
yi zi+s .
i=0
Show (via equations) that the s-shifted dot product is precisely the coefficient of xn−s in
the product of the polynomials with coefficients
a0 = y0 , a1 = y1 , . . . , an = yn .
and
b0 = zn , b1 = zn−1 , . . . , bn = z0 .
Note that the second polynomial takes the coefficients in the reverse order.
Aside: these values are quite useful in performing approximate string matching.
SOLUTION:
Plugging in ai = yi and bi = zn−i into the coefficient for n − s in their polynomial product
gives: X X X
ai bn−s−i = yi zn−(n−s−i) . = yi zs+i .
0≤i≤n−s 0≤i≤n−s 0≤i≤n−s
The last term is exactly the s-shifted dot product between y and z.