


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
Solutions to the first midterm exam questions related to algorithms and complexity. It covers topics such as recurrence relations, polynomial time algorithms, secret sharing schemes, and median-finding. Students can use this document as a study resource for understanding the concepts and techniques required for solving problems in algorithms and complexity.
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



CS170 Solutions to the First Midterm
T The solution to the recurrence T (n) = 5 T ( n 3 ) + n^3 is (n^3 ).
The master theorem yields this at once. Or you could expand out the recurrence.
T The solution to the recurrence T (n) = 4 T ( n 2 ) + n^2 log n is (n^2 log^2 n).
If you plug in this solution, you will nd that it works. Alternatively, you could directly use our two other techniques.
Therefore, it will b e orders of magnitude bigger than 4 log^ n^ = n^2. F nsin^
2 n^ is O (
n)
often equal to n, which is not O (
n ).
T The cub es of the 63rd ro ots of unity are the 21st ro ots of unity.
For the FFT, we needed the fact that the squares of the 2 nth ro ots of unity are the nth ro ots of unity. This follows by similar reasoning. F If n is not prime, then for any p ositive integer a which is less than n and relatively
For instance, 1 n ^1 = 1 (mo d n).
T Carmichael numb ers are never prime. Carmichael numb ers constitute bad cases for the simple primality tester presented in class.
T 2745 = 8285 ( mo d 55)
(55) = 40 and 27 = 82 (mo d 55), so 2745 = 275 (mo d 55) and 8285 = 2785 = 275 (mo d 55). F There exist integers x and y such that 273 x + 42 y = 7. Three divides the expression on the left but not the numb er on the right.
T If f = O (g ) then f 2 = O (g 2 ).
2
F If f = O (g ) then 2 f^ = O (2g^ ).
Solution: When Bob signs a message, he computes M d^ mo d n, where (d; n) is his private
key.
compute this, we use the Extended Euclid algorithm, with inputs 17 and 352. Because 17 and 352 are relatively prime, Euclid's will yield a pair (x; y ) such that 17 x + 352 y = 1.
d = 17 ^1 mo d 352 = 145. Sergey Io e graded this problem.
Solution: For your general edi cation, here is a much more detailed time analysis than you needed to give:
If a is divisible by p, then ab
c mo d p = 0 b
c mo d p = 0. In what follows we assume that p do esn't divide a. By the Fermat's Little Theorem, ap ^1 mo d p = 1. Therefore, ab
c mo d p = ab
c mo d(p 1) mo d p.
squaring. Recall from class that this will involve O (log c) multiplications of numb ers
Now, we need to compute ab
c mo d (p 1) mo d p. This can b e done as ab ove, using rep eated
log p) = O (n^3 ), where n is the numb er of bits in the input. Sergey Io e graded this problem.
integers b e the values of a p olynomial at the p oints 1 through 20. What is the minimum numb er of integers she should send to insure that Bob can recover the original 20 integers? Solution: Since there are 20 integers, a p olynomial of degree d = 19 should b e used. The numb er of mistakes that we need to accommo date is k = 12. We saw in class that if we send n integers then
cients. How many p olynomials, as a function of p, are consistent with the informa- tion that Bob now has? Solution: Each of the two missing co ecients can have any value in the range
Sanjoy Dasgupta graded this problem.
(13 p oints) Give a O (n log n) time divide-and-conquer algorithm for this task.
A. Brief description of the algorithm.
Solution: The following algorithm outputs the ma jority element if there is one, and outputs FALSE otherwise:
Given an array N of size n, divide it into two arrays, A and B , of size n 2.
Now given solutions to the two sub-problems, we merge them as follows:
If b oth A and B have ma jority elements and they're equal, then we output that element as the ma jority element for N. If neither has a ma jority element, we output FALSE. If only one of the sub-arrays has a ma jority element, or if they have di erent ma jority elements, we compare it/them to each of the elements of N in turn, keeping count of the numb er of matches. If one of these \sub-ma jority" elements o ccurs more than n 2 times, we output it as the ma jority element for N. If not, then we output FALSE.
B. Justi cation of correctness.
If m is a ma jority element for N , it must necessarily b e a ma jority element for at least one of A,B.
C. Running time and justi cation.
Merging subproblems involves at most 2 n + 1 comparisons. Thus, our recurrence is
T (n) = 2 T (
n 2
) + O (n)
which yields a running time of O (n log n) by the Master theorem.
Sara Robinson graded this problem.
Grading criteria: If you showed you understo o d divide-and-conquer but didn't give a correct merge you got 2-4 p oints. If you computed a correct running time for your incorrect algorithm you got a few additional p oints.
Many of you gave a randomized algorithm, similar to median nding, which has n^2 worst-case time but linear exp ected time. I gave a lot of partial credit for this answer (dep ending on the clarity of the explanation) although it's not a divide-and-conquer algorithm.
An algorithm no b etter than doing all p ossible comparisons in n^2 time got 0 p oints.
Extra Credit: Give a linear time algorithm for this task.
(Note: No partial credit. Attempt this problem only if you have time to spare.)
Solution: Create a stack and push a 1 , the rst value of the array, onto it. Now for
t o the stack.
At the end, If the stack is empty, output FALSE. If not, then output the top element of the stack.
Sara Robinson graded this problem.