Theoretical Algorithms - Efficient Algorithms and Intractable Problems - Solved Exams, Exams of Algorithms and Programming

Main points of this past exam are: Theoretical Algorithms, Number Theoretical, Asymptotic Upper, Upper Bound, Encoding, Algorithm Design, Sorted Arrays, Fft Matrix, Depth Rst Search, Starting Point

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shameem_99
shameem_99 🇮🇳

4.4

(12)

58 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 170 Spring 2008 - Solutions to Midterm #1
Problem 1
1. 2n= Ω(n!).
False: lim
n→∞(2n/n!) = lim
n→∞
2
1·2
2·. . . ·2
n2·lim
n→∞
2
n= 0.
2. 3log2n= Θ(n).
False: 3log2n= 3(1
2log2n)=n(1
2log23) = (n)log23.
3. If f(n) = O(g(n)), then g(n) = Ω(f(n)).
True: By definition of (this is in fact how your textbook defines Ω).
4. Pn
j=1 j=O(nlog n).
False:Pn
j=1 j=1
2n(n+ 1) = Θ(n2).
5. If T(n) = 4T(n/2) + O(n), then T(n) = O(n3).
True: By the master theorem, T(n) = O(n2). Since O-notation represents
an upper bound, T(n) is also O(n3).
6. If T(n) = 4T(n/2) + O(n), then T(n) = O(n2log n).
True: By the master theorem, T(n) = O(n2). Since O-notation represents
an upper bound, T(n) is also O(n2log n).
7. If an11 mod nfor all positive integers a < n, then nis prime.
False: Carmichael numbers are rare composite numbers nsatisfying Fer-
mat’s test for all positive integers a<n.
8. The probability that an n-bit integer is prime is roughly 2n.
False: By Lagrange’s theorem, the number of primes less than an n-bit
integer x, for large n, is roughly x
ln x. Therefore, the probabilty that an
n-bit integer is prime is roughly (x/ ln x)
x=1
ln x1
ln(2n)=1
nln 2 1.44
n.
9. Any DAG with a unique source and sink has a unique topological ordering.
False: Consider a directed graph with four nodes A, B, C, and D and four
edges (A,B), (A,C), (B,D), and (C,D). There are two possible topological
orderings: A,B,C,D and A,C,B,D.
1
pf3
pf4
pf5

Partial preview of the text

Download Theoretical Algorithms - Efficient Algorithms and Intractable Problems - Solved Exams and more Exams Algorithms and Programming in PDF only on Docsity!

CS 170 Spring 2008 - Solutions to Midterm

Problem 1

  1. 2n^ = Ω(n!). False: lim n→∞ (2n/n!) = lim n→∞

2 1 ·^

2 2 ·^...^ ·^

2 n ≤^2 ·^ nlim→∞

2 n = 0.

  1. 3log^2

√n = Θ(

n). False: 3log^2

√n = 3(^ (^12) log 2 n) = n(^ (^12) log 2 3) = (

n)log^2 .

  1. If f (n) = O(g(n)), then g(n) = Ω(f (n)). True: By definition of Ω (this is in fact how your textbook defines Ω).

∑n j=1 j^ =^ O(n^ log^ n). False:

∑n j=1 j^ =^

1 2 n(n^ + 1) = Θ(n

  1. If T (n) = 4T (n/2) + O(n), then T (n) = O(n^3 ). True: By the master theorem, T (n) = O(n^2 ). Since O-notation represents an upper bound, T (n) is also O(n^3 ).
  2. If T (n) = 4T (n/2) + O(n), then T (n) = O(n^2 log n). True: By the master theorem, T (n) = O(n^2 ). Since O-notation represents an upper bound, T (n) is also O(n^2 log n).
  3. If an−^1 ≡ 1 mod n for all positive integers a < n, then n is prime. False: Carmichael numbers are rare composite numbers n satisfying Fer- mat’s test for all positive integers a < n.
  4. The probability that an n-bit integer is prime is roughly 2−n. False: By Lagrange’s theorem, the number of primes less than an n-bit integer x, for large n, is roughly (^) lnx x. Therefore, the probabilty that an n-bit integer is prime is roughly (x/^ xln x)= (^) ln^1 x ≈ (^) ln(2^1 n) = (^) n 1 ln 2 ≈ 1. n^44.
  5. Any DAG with a unique source and sink has a unique topological ordering. False: Consider a directed graph with four nodes A, B, C, and D and four edges (A,B), (A,C), (B,D), and (C,D). There are two possible topological orderings: A,B,C,D and A,C,B,D.
  1. Breadth first search and depth first search produce the same tree on con- nected undirected graphs if and only if the graph is a tree. True: Suppose the input graph is an undirected tree T. Both DFS and BFS must produce a tree, so they must contain all the edges of T (all trees have |V | − 1 edges). Since two trees must be identical if they have the same root and same edges, both DFS and BFS will produce T. Conversely, suppose the input graph G is undirected and connected but is not a tree. Then G must contain a cycle C. Suppose C consists of the k nodes v 1 , v 2 ,... , vk, i.e. C is the cycle v 1 → v 2 →... → vk → v 1. Now in the DFS tree, nodes v 1 , v 2 ,... , vk will all be on the same path from the root to a leaf. Why? Suppose vf is the first of these nodes to be visited. Then, the remaining nodes must be visited at some point during explore(vf ) since the vi are all strongly connected. However, in the BFS tree, nodes v 1 , v 2 ,... , vk will form at least two branches, braching from the node first visited (imagine performing BFS on the cycle). Therefore, BFS and DFS produce the same tree iff the input graph is a tree.

Problem 2.1 (5 points each)

The number of bits in an integer x is Θ(log x). Therefore, the number of bits in ab^ is Θ(log(ab)) = Θ(b log a). Now log a = O(n) and b = O(2n) since both a and b are n-bit numbers, so b log a = O(n · 2 n). Thus, O(n · 2 n) is an upper bound on the number of bits in ab. Similarly, the number of bits in ab

c is Θ(log(a(b

c) )) = Θ(bc^ log a). Again, log a = O(n), b = O(2n), and c = O(2n) since a, b, and c are n-bit numbers, so bc^ log a = O(n · (2n)(

n) ) = O(n · 2 (n·^2

n) ). Thus, O(n · 2 (n·^2

n) ) is an upper bound on the number of bits in ab c .

Problem 2.2 (5 points each)

(a) The secret key d equals e−^1 mod (p − 1)(q − 1). Since the n-bit primes p and q are given as input, d can be found in O(n^3 ) time using the extended Euclid’s algorithm.

(b) The ciphertext c equals me^ mod pq. Therefore, c can be computed in O(n^2 log e) time using the repeated squaring method for modular expo- nentiation.

Problem 3.

First off, o(kl) means asymptotically faster than O(kl). This threw most people off, and we gave no credit for Ω(kl) solutions since they were trivial.

number of each DFS run. If there exists a pair of nodes u and v, satisfying pre(v) < pre(u) < post(u) < post(v) for one DFS run and pre(u) < pre(v) < post(v) < post(u) for another DFS run, then u and v are on a cycle. If there doesn’t exist such a pair of nodes, then output “No two nodes are on a cycle”.

4.2 (4 pts)

pre(u) < post(u) < pre(v) < post(v). Suppose v is explored before u. Since u is reachable from v and this is a directed graph, we must have pre(v) < pre(u) < post(u) < post(v), contradicting the assumption that the intervals are disjoint. Therefore, u must be explored before v giving pre(u) < post(u) < pre(v) < post(v). It should also be pointed out that in case of an undirected graph, both pre(u) < post(u) < pre(v) < post(v) and pre(v) < post(v) < pre(u) < post(u) are possible. However, we gave full credit if you only answered the directed graph case.

4.3 (6 pts)

In the case of a connected graph, pre(s) = 1 and post(s) = 2|V |, where |V | is the total number of nodes in the graph. In the case of an unconnected graph, pre(s) = 1 and post(s) = 2|C| < 2 |V |, where |C| is the number of nodes of the connected component containing the starting point s (i.e. the number of the nodes reachable from the starting point, including the starting point itself).

Problem 5

Solution 1 (Full Credit)

Start from any node and run Dijkstra’s algorithm. Then k + 1 times, do the following: update all the negative edges (not strictly necessary but easier to prove correctness), place everything back in the priority queue, and run the loop of Dijkstra’s that pulls nodes out and updates the adjacent edges. The graph has a negative cycle if and only if any dist(v) changes in the last iteration (i.e. the k + 1 iteration). The top level algorithm NegativeCycle, which uses subroutines DijkstraLoop and UpdateNegativeEdges, is given below.

Algorithm 1: NegativeCycle Input: Input graph G Output: True if G contains a negative cycle, False otherwise 1 s = any vertex ∈ V ; 2 for all u ∈ V do 3 dist(u) = ∞; 4 end 5 dist(s) = 0; 6 Q = makequeue(V, dist); 7 DijkstraLoop(G, Q); 8 for i = 1 to k do 9 U pdateN egativeEdges(G, dist); 10 Q = makequeue(V, dist); 11 DijkstraLoop(G, dist, Q); 12 end 13 if U pdateN egativeEdges(G, dist) then 14 return True 15 else 16 Q = makequeue(V, dist); 17 return DijkstraLoop(G, dist, Q) 18

Algorithm 2: DijkstraLoop Input: Input graph G, array of dist values dist, priority queue Q Output: True if any dist value is updated, False otherwise 1 retV alue = F alse; 2 while Q is not empty do 3 u = deletemin(H); 4 for all edges (u, v) ∈ E do 5 if dist(v) > dist(u) + l(u, v) then 6 dist(v) = dist(u) + l(u, v); 7 decreasekey(H, v); 8 retV alue = T rue; 9 10 end 11 end 12 return retValue

For the runtime, we have Θ(k) iterations of Dijkstra’s algorithm (which each take O(m + n log n) time using a Fibonacci heap to implement the priority queue) and Θ(k) updates of the negative edges (which each take O(m) time). So the total running time is O(k(m + n log n)).

Solution 2 (3/4 Credit)

Remove all the negative edges. Find a single source shortest path from each endpoint of a negative edge to each other endpoint (using Dijkstra’s algorithm). Construct a graph on the (at most) 2k nodes with edge distances that correspond to the shortest path distances between the corresponding point in the positive edge graph. Put in the negative edges between the original nodes. This is a graph on 2k nodes with O(k^2 ) edges. One can check for a negative cycle in the is graph in time O(k^3 ). Any negative cycle in the new graph corresponds to a negative cycle in the original since any edge in this graph corresponds to a path in the original graph. Moreover, any negative cycle C in the original graph must contain one or more negative edges (denote the set of negative edges by N ). Suppose C does not consist of positive edge shortest paths between endpoints of negative edges. Then the cycle C′^ formed by connecting the endpoints of N using positive edges shortest paths has weight less than C. Thus, C′^ is a negative cycle. This shows that if there is a negative cycle C in the original graph, then there is a negative cycle consisting of positive edge shortest paths between endpoints of negative edges, i.e. a negative cycle in the new graph. Thus, our algorithm finds a negative cycle if and only if a negative cycle existed in the original graph. We have Θ(k) iterations of Dijkstra’s algorithm following by performing Bellman-Ford on graph with Θ(k) nodes and Θ(k^2 ) edges. Thus, the running time for this is O(k(m + n log n) + k^3 ) by implementing the priority queue with a Fibonacci heap.