Solutions to Midterm Exam: Algorithms and Complexity, Exams of Algorithms and Programming

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

2012/2013

Uploaded on 04/02/2013

shameem_99
shameem_99 🇮🇳

4.4

(12)

58 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS170 Solutions to the First Midterm
1.
(2 points each question) True or false?
Circle the correct answer. No explanation
required. No points will be subtracted for incorrect answers, so guess all you want.
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.
F
d
log
n
e
!is
O
(
n
2
)(
Note:
! denotes factorial)
d
log
n
e
! consists of log
n
terms, most of which are considerably larger than four.
Therefore, it will be orders of magnitude bigger than 4
log
n
=
n
2
.
F
n
sin
2
n
is
O
(
p
n
)
As
n
varies, sin
2
n
oscillates between
,
1 and 1. Thus the expression is innitely
often equal to
n
, whichisnot
O
(
p
n
).
T The cubes of the 63rd roots of unity are the 21st roots of unity.
For the FFT, we needed the fact that the squares of the 2
n
th roots of unity are the
n
th roots of unity. This follows by similar reasoning.
F If
n
is not prime, then for any positiveinteger
a
which is less than
n
and relatively
prime to
n
,
a
n
,
1
6
=1 (mod
n
).
For instance, 1
n
,
1
=1 (mod
n
).
T Carmichael numbers are never prime.
Carmichael numbers constitute bad cases for the simple primality tester presented
in class.
T 27
45
=82
85
(mod 55)
(55) = 40 and 27 = 82 (mod 55), so 27
45
=27
5
(mod 55) and 82
85
=27
85
=
27
5
(mod 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 number on the right.
T If
f
=
O
(
g
) then
f
2
=
O
(
g
2
).
If lim
n
!1
f
(
n
)
g
(
n
)
c
for some constant
c
, then lim
n
!1
f
(
n
)
2
g
(
n
)
2
c
2
.
F If
f
=
O
(
g
) then 2
f
=
O
(2
g
).
For instance 2
n
=
O
(
n
) but 2
2
n
6
=
O
(2
n
).
2. (
10 points
) Alice wants to write a message that looks likeitwas digitally signed by
Bob. She notices that Bob's public RSA key is (17, 391). To what exponent should she
raise her message? (Answer of the form
x
,
1
mody
gets half credit.)
Solution:
When Bob signs a message, he computes
M
d
mod
n
, where (
d; n
) is his private
key. Recall that if
n
=
pq
, then
d
=
e
,
1
mod (
p
,
1)(
q
,
1), where (
e; n
) is Bob's public
key.
Now, notice that 391 = 17
23, and so
d
=17
,
1
mod (17
,
1)(23
,
1) = 17
,
1
mod 352. To
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.
In particular, we will have
x
= 145 and
y
=
,
7, hence 17
145 mod 352 = 1, and so
d
=17
,
1
mod 352 = 145.
Sergey Ioe graded this problem.
1
pf3
pf4

Partial preview of the text

Download Solutions to Midterm Exam: Algorithms and Complexity and more Exams Algorithms and Programming in PDF only on Docsity!

CS170 Solutions to the First Midterm

  1. (2 p oints each question) True or false? Circle the correct answer. No explanation required. No p oints will b e subtracted for incorrect answers, so guess all you want.

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.

F dlog n e! is O (n^2 ) (Note:! denotes factorial)

dlog n e! consists of log n terms, most of which are considerably larger than four.

Therefore, it will b e orders of magnitude bigger than 4 log^ n^ = n^2. F nsin^

 2 n^ is O (

p

n)

As n varies, sin  2 n oscillates b etween 1 and 1. Thus the expression is in nitely

often equal to n, which is not O (

p

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

prime to n, an^1 6 = 1 (mo d n).

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

If lim n!1^ f g^ ( (nn)) ^ c^ for^ some^ constant^ c,^ then^ lim^ n!1^ f^ (n)

2

g (n)^2 ^ c

F If f = O (g ) then 2 f^ = O (2g^ ).

For instance 2 n = O (n) but 22 n^6 = O (2n^ ).

  1. ( 10 p oints) Alice wants to write a message that lo oks like it was digitally signed by Bob. She notices that Bob's public RSA key is (17, 391). To what exp onent should she raise her message? (Answer of the form x^1 mody gets half credit.)

Solution: When Bob signs a message, he computes M d^ mo d n, where (d; n) is his private

key. Recall that if n = pq , then d = e^1 mo d (p 1)(q 1), where (e; n) is Bob's public

key.

Now, notice that 391 = 17  23, and so d = 17 ^1 mo d (17 1)(23 1) = 17 ^1 mo d 352. To

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.

In particular, we will have x = 145 and y = 7, hence 17  145 mo d 352 = 1, and so

d = 17 ^1 mo d 352 = 145. Sergey Io e graded this problem.

  1. ( 10 p oints) Give a brief description of an ecient (p olynomial time) algorithm for computing a(bc^ )^ (mo dp) for p prime. Brie y explain why it is correct and why it is p olynomial.

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(p1) mo d p.

Let us rst compute bc^ mo d (p 1). This can b e done eciently by using rep eated

squaring. Recall from class that this will involve O (log c) multiplications of numb ers

b etween 0 and p, and taking the results mo dulo p 1. Each of these op erations takes

O (log^2 p) time. Thus, bc^ mo d (p 1) can b e computed in O (log c  log 2 p + log b  log p)

time (the extra log a  log p accounts for the initial computation of b mo d (p 1)).

Now, we need to compute ab

c mo d (p1) mo d p. This can b e done as ab ove, using rep eated

squaring. Notice that bc^ mo d (p 1) < p, thus the exp onentiation will take O (log p 

log 2 p + log a  log p) time.

The total running time of this algorithm is O (log c  log 2 p + log b  log p + log 3 p + log a 

log p) = O (n^3 ), where n is the numb er of bits in the input. Sergey Io e graded this problem.

  1. (10 p oints) Alice wishes to send 20 integers (expressed mo dulo p for some large prime p) to Bob over a noisy channel. She knows that at most 12 of the integers she sends will b e corrupted during transmission.

 (5 p oints) Using the Berlekamp-Welch error correction scheme, Alice lets the 20

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

k 

n (d + 1)

whereby we need n  44.

 (5 p oints) After Bob correctly recovers the p olynomial, he loses two of the co e-

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

0 ; : : : ; p 1. Therefore there are p^2 choices.

Sanjoy Dasgupta graded this problem.

  1. ( 15 p oints) Recall Shamir's Secret Sharing scheme discussed in class.

(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

each p osition 1 < i  n of the array, compare ai with the top element of the stack, t. If

ai = t, then push ai onto the stack and increment i. If ai 6 = t, then increment i and p op

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.