



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




U.C. Berkeley — CS170 : Algorithms Midterm 1 Lecturer: Christos Papadimitriou October 9, 2007
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.
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.
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
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?
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.
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?’’) }
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) ).