Docsity

U.C. Berkeley CS170 Midterm 1 Solutions: Algorithms, Exams of Algorithms and Programming

Toggle sidebar
bg1
U.C. Berkeley — CS170 : Algorithms Midterm 1
Lecturer: Christos Papadimitriou October 9, 2007
Midterm 1 Solutions
Problem 1 (15 points)
Do a depth-first search of the graph below. Process nodes (and edges out of a node) in alphabetic order.
Show the pre and post order numbers. See Below How many strongly connected components are there (no
need to show how to find them)? 4. What is the graph after you shrink them? See Below What is the
minimum number of edges you have to add to make the graph strongly connected? 2.
1
pf3
pf4
pf5

Partial preview of the text

Download U.C. Berkeley CS170 Midterm 1 Solutions: Algorithms and more Exams Algorithms and Programming in PDF only on Docsity!

U.C. Berkeley — CS170 : Algorithms Midterm 1 Lecturer: Christos Papadimitriou October 9, 2007

Midterm 1 Solutions

Problem 1 (15 points)

Do a depth-first search of the graph below. Process nodes (and edges out of a node) in alphabetic order. Show the pre and post order numbers. See Below How many strongly connected components are there (no need to show how to find them)? 4. What is the graph after you shrink them? See Below What is the minimum number of edges you have to add to make the graph strongly connected? 2.

Problem 2 (15 points)

  1. (10 points) In an RSA cryptosystem, p = 5 and q = 7. Find the smallest encryption exponent e > 1, which is legal for RSA encryption, and the corresponding d. Encrypt the message 3. How many legal (e, d) pairs are there, not including (e = 1, d = 1)? Would they all result in different encryptions of 3?
  2. (5 points) What is the FFT of (1, 0 , 0 , 1)? What is the appropriate value of ω in this case? And of which sequence is (1, 0 , 0 , 1) the FFT?

Solution to Part 1

The public exponent e must be relatively prime to (p − 1)(q − 1) = 24. The smallest such e > 1 is e = 5. The private key d is the inverse modulo 24 of e, so the corresponding d is 5−^1 mod 24 = 5 since 5 × 5 = 25 ≡ 1 mod 24. The encrypted message is me^ mod N , where m = 3, e = 3, and N = pq = 35. This gives 3^5 mod 35 ≡ 33.

For an (e, d) pair to be valid, e must be relatively prime to (p − 1)(q − 1) = 24. We also assume 1 < e < 24. This gives 7 possible values for e: 5, 7, 11, 13, 17, 19, and 23. For each e, there is a unique inverse modulo 24, and hence a unique d. Therefore, there are 7 valid (e, d) pairs.

They do not all result in different encryptions of m = 3 because 3^12 ≡ 1 mod 35. Therefore, 3^5 mod 35 and 3^17 mod 35 are the same encrypted message. Similarly, 3^7 mod 35 and 3^19 mod 35 are the same, as well as 3^11 mod 35 and 3^23 mod 35.

Solution to Part 2

When n = 4, ω = e(2πi/4)^ = i, so the FFT matrix M 4 (ω) is

   

1 i − 1 −i 1 − 1 1 − 1 1 −i − 1 i

Therefore, the FFT of

 is

1 i − 1 −i 1 − 1 1 − 1 1 −i − 1 i

 ×

1 − i 0 1 + i

When n = 4, the inverse FFT matrix is 14 M 4 (ω−^1 ), which is

1 −i − 1 i 1 − 1 1 − 1 1 i − 1 −i

Problem 3 (Dijkstra) (15 points)

We are given a directed graph G with positive weights on its edges. We wish to find a shortest path from s to t, and, among all shortest paths, we want the one in which the longest edge is as short as possible. How would you modify Dijkstra’s algorithm to this end?

Solution to Problem 3

When Dijkstra’s algorithm runs, it normally uses a variable dist(u) to store the current best shortest path distance to a node u ∈ V , and prev(u) to store the last node needed to reach node u ∈ V in the current best shortest path found. In order to solve this new problem, we need to create a new variable long(u) for all nodes u ∈ V , which keeps track of the length of the longest edge which needs to be used in order to reach node u in the current best shortest path. To this end, we can define our algorithm as follows:

Initialize Variables For all u ∈ V dist(u) = ∞ long(u) = ∞ prev(u) = nil dist(s) = 0 long(s) = 0 H = makequeue(V )

Begin Main Loop While H is not empty u = deletemin(H) for all edges (u, v) ∈ E if (dist(v) > dist(u) + l(u,v)) OR ((dist(v) == dist(u) + l(u,v)) AND (long(v) > max(long(u), l(u,v)))) dist(v) = dist(u) + l(u,v) long(v) = max(long(u),l(u,v)) prev(v) = u

Note that at the end of the algorithm prev(u) stores the last edge used in the shortest path to node u, where the shortest path chosen is a path amongst all shortest paths, where the longest edge in the path is as small as possible. Furthermore, we can reconstruct the requisite shortest path from s to t using the prev(u) pointers.

Problem 4 (Divide-and-Conquer) (10 points)

How many lines does this algorithm print? Write a recurrence and solve it.

function printalot(n: an integer power of 2) if n > 1 { printalot(n/ 2 ) printalot(n/ 2 ) printalot(n/ 2 ) for i = 1 to n^4 do printline(‘‘are we done yet?’’) }

Solution to Problem 4

If T (n) is the number of lines printed for input n, then T (n) = 3T (n/2) + n^4 with a base case of T (1) = 0.

For an asymptotic solution, we can apply the master theorem with nlogb^ a^ = nlog^2 3 and f (n) = n^4. Since f (n) polynomially dominates nlogb^ a^ (see handout on website), T (n) = Θ(f (n)) = Θ(n^4 ) by the master theorem.

For an exact solution, we expand the recurrence as follows:

T (n) = n^4 + 3(n/2)^4 + 9(n/4)^4 +... + 3k(2)^4 ,

where k = log 2 n − 1 since T (1) = 0.

This gives T (n) =

∑k i=0 3

i(n/ 2 i) (^4) = n 4 ∑k i=0(3/16)

i (^) = n (^4) (( 3 16

log 2 n − 1)/( 163 − 1)) (by the geometric series formula and using k = log 2 n − 1 ). This can be further simplified to T (n) = 1613 n^4 (1 − nlog^2 (3/16)) = 16 13 (n

(^4) − nlog (^2 3) ).

  • False Calculating a^53 mod b takes 6 multiplications. Solution: It takes 8 multiplications: a ∗ a^26 ∗ a^26 , a^26 = a^13 ∗ a^13 , a^13 = a ∗ a^6 ∗ a^6 , a^6 = a^3 ∗ a^3 , and a^3 = a ∗ a ∗ a.
  • False (not graded) The word “algorithm” comes from a term in Arabic meaning “tent-pitching method.” Solution: The word comes from the name Al Khwarizmi, a mathematician from the ninth century who lived in Baghdad and who laid out the basic steps for arithmetic operations. However, he may have come from a long and noble line of tent pitchers.