Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Homework 1 Solutions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

CS3510 Design & Analysis of Algorithms Section A

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.

0. [1 point, only if all parts are completed]

(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.

1. (5 points) Big-O Notation

(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) ,

which together with the first condition gives:

f (n) ≤ 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

22n > C2n .

2. (8 points) Master Theorem


The master theorem applies to algorithms with recurrence relations in the form of

T (n) = aT (n/b) + O(nd )

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.

(a) (2 points) An algorithm with the run-time recurrence:

T (n) = 3T (n/4) + O (n)

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:

T (n) = 8T (n/4) + O n1.5




SOLUTION:

T (n) = 3T (n/4) + O (1)


a = 3, b = 4, d = 0.
logb a = log4 3 ≈ .79 > 0, so total runtime is O(n.79 ).
SOLUTION:
a = 8, b = 4, d = 1.
logb a = log4 8 = 1.5 < 1, so O(n1.5 log n).
(c) (2 points) An algorithm solves problems by diving a problem of size n into 3 sub-problems
of one-fourth the size and recursively solves the smaller sub-problems. It takes constant
time to combine the solutions of the sub-problems.
SOLUTION:

T (n) = 3T (n/4) + O (1)


a = 3, b = 4, d = 0.
logb a = log4 3 ≈ .79 > 0, so total runtime is O(n.79 ).
GRADING:
There was a typo in the August 23 notes (now fixed) that said Master Theorem only applied
for d ≥ 1. Anyone who then claimed that Master Theorem did not apply for this problem
because d < 1 should have still received full points.

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:

T (n) = 2n T (n/2) + O (n) .


Not solvable by Master theorem since a = 2n is not a constant.

3. (3 points) Divide and conquer


After learning about the Stooge sort (https://en.wikipedia.org/wiki/Stooge_sort), Buzz
would like to improve its performance. This led to the Buzzsort, with pseudocode as follows:

(a) If the sequence length is at most 4, sort it using bubble sort.


(b) Else:
i. Divide the list into 5 pieces evenly, by scanning the entire list.
ii. (recursively) sort the first 3/5 of the list.
iii. (recursively) sort the last 3/5 of the list.
iv. (recursively) sort the first 3/5 of the list.

For example, on the input sequence


1, 5, 3, 2, 4
The first recursive sort produces
1, 3, 5, 2, 4,
the second sort produces
1, 3, 2, 4, 5,
and the last produces
1, 2, 3, 4, 5.

(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.

4. (4 points) Fast multiplication and convolution.


We show several additional applications of fast multiplications of integers. A degree d polynomial
is the function
p (x) = a0 + a1 x + a2 x2 + . . . ad xd .
These objects multiply just like integers, except without carries. That is if we multiply degree
d polynomials p and q with coefficients a0 . . . ad and b0 . . . bd respectively, the coefficient of xi in
the result is X
aj · bi−j .
max{0,i−d}≤j≤min{i,d}

(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:

p1 (x) × xk + p2 (x) q1 (x) × xk + q2 (x)


  

= x2k − xk p1 (x) q1 (x) − xk − 1 p2 (x) q2 (x) + xk [p1 (x) + p2 (x)] [q1 (x) + q2 (x)] .
 

So the same divide-and-conquer scheme still works.

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.

You might also like