



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!




Problem 1. [True or false] (16 points)
Circle TRUE or FALSE. Do not justify your answers on this problem.
(a) TRUE or FALSE: If f (n) = (n + 1 )n/2, then f (n) ∈ O(n^2 ).
(b) TRUE or FALSE: If f (n) = (n + 1 )n/2, then f (n) ∈ Θ(n^2 ).
(c) TRUE or FALSE : If f (n) = (n + 1 )n/2, then f (n) ∈ Θ(n^3 ).
(d) TRUE or FALSE : n^1.^1 ∈ O(n(lg n)^2 ).
(e) TRUE or FALSE: It’s possible to multiply two n-bit integers in O(n^1.^9 ) time. Comment: We saw that it was possible to multiple two n-bit integers in O(n^1.^59 ) time, using a divide- and-conquer algorithm. So it’s certainly possible to multiply them in O(n^1.^9 ) time: just multiply using the divide-and-conquer algorithm, then execute no-op instructions to mark time until the total running time is O(n^1.^9 ).
(f) TRUE or FALSE: If vertices u, v are in the same strongly connected component of a directed graph G, then it is necessarily the case that v is reachable from u in G.
(g) TRUE or FALSE : If vertices u, v are not in the same strongly connected component of a directed graph G, then it is necessarily the case that v is not reachable from u in G. Comment: It’s possible that u, v are in two different SCCs and there is an edge from u to v.
(h) TRUE or FALSE : If we run breadth-first search on a directed graph G starting from vertex s, then depending on the graph, it might visit some vertices that a depth-first search starting from s would not visit. (Assume that we use exactly the breadth-first search algorithm specified in the book.)
Problem 2. [Recurrences] (16 points)
Write the solution to the following recurrences. Express your answer using O(·) notation. Do not justify your answers on this problem. Do not show your work.
(a) Solve the recurrence F(n) = F(dn/ 2 e) + O( 1 ). Answer: F(n) = O(lg n).
(b) Solve the recurrence F(n) = 4 F(dn/ 2 e) + O( 1 ).
Answer: F(n) = O(n^2 ).
(c) Solve the recurrence F(n) = 4 F(dn/ 2 e) + O(n). Answer: F(n) = O(n^2 ).
(d) Solve the recurrence F(n) = 4 F(dn/ 2 e) + O(n^2 ).
Answer: F(n) = O(n^2 lg n). Comment: All of these can be solved using the Master theorem, or by drawing a tree.
ManyMerge(L 1 [ 0 ..n − 1 ], L 2 [ 0 ..n − 1 ],... , Lt [ 0 ..n − 1 ]):
Comment: The running time of this solution satisfies the recurrence T (t) = 2 T (t/ 2 )+O(nt): we recursively invoke ManyMerge twice, in each case with t/2 sorted lists of size n; and then we merge two lists of size nt/2, which takes O(nt) time. The solution to this recurrence is T (t) = O(nt lgt), so the algorithm shown here does indeed achieve the necessary time bound. It is not hard to see why it is correct.
Problem 5. [Depth-first search] (16 points)
Run depth-first search on the directed graph below, starting at vertex A. Whenever there is a choice of the order to explore vertices, use alphabetical order (so A is chosen before B, and B before C, etc.).
(a) Draw the DFS tree that results, in the space provided below. Use solid lines for tree edges, and dotted lines for non-tree edges.
cross
back
(b) Label each non-tree edge in the graph above with “forward”, “back”, or “cross”, according to whether the edge is a forward edge, back edge, or cross edge.
(c) Can the above graph be topologically sorted? Circle YES or NO. Do not justify your answer.
(d) How many strongly connected components does this graph have? Do not justify your answer.
Answer: 3. Comment: The three strongly connected components are: {A}, {E}, and {B,C, D}.
Problem 6. [Short answer] (22 points)
Answer each question below concisely (one short sentence or a number should suffice). Do not justify your answer. Do not show your work.
(a) Suppose we are given a directed graph G = (V, E) represented in adjacency list format, and we want to test whether G is a dag or not, using a method that is as asymptotically efficient as possible. In a sentence, what approach would you use? Answer 1: Use DFS, check for back-edges. Answer 2: Decompose into strongly connected components, check for a SCC with more than one vertex. Comment: There is a cycle (and hence G fails to be a dag) if and only if DFS finds a back edge. There is a cycle (and hence G fails to be a dag) if and only if there is a strongly connected component with more than one vertex.
(b) What’s the running time of your solution in (a), using O(·) notation?
Answer: O(|V | + |E|). (c) Let G = (V, E) be a directed graph with |V | = 1000 vertices, |E| = 5000 edges, and 700 strongly con- nected components. How many vertices does the metagraph have? Answer: 700. Comment: Each vertex in the metagraph corresponds to a strongly connected component in G, so the number of vertices in the metagraph is the same as the number of SCCs in G.
(d) Let G = (V, E) be a dag, where each edge is annotated with some positive length. Let s be a source vertex in G. Suppose we run Dijkstra’s algorithm to compute the distance from s to each vertex v ∈ V , and then order the vertices in increasing order of their distance from s. Are we guaranteed that this is a valid topological sort of G? Circle YES or NO.
(e) Justify your answer to part (d) as follows: If you circled YES, then give one sentence that explains the main idea in a proof of this fact. If you circled NO, then give a small counterexample (a graph with at most 4 vertices) that disproves it. Answer:
C
1
2
3
B S
Comment: Sorting by increasing distance gives S,C, B; but B must precede C in any valid topological sort.