




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!





UC Berkeley—CS 170 Midterm 1 Solutions Lecturer: Gene Myers
Print your name:
Myers Gene ,
(last) (first)
Sign your name:
Write your section number (e.g. 101):
Write your sid:
One page of notes is permitted. No electronic devices, e.g. cell phones and calculators, are permitted. Do all your work on the pages of this examination. If you need more space, you may use the reverse side of the page, but try to use the reverse of the same page where the problem is stated.
You have 80 minutes. The questions are of varying difficulty, so avoid spending too long on any one question.
In all algorithm design problems, you may use high-level pseudocode.
DO NOT TURN THE PAGE UNTIL YOU ARE TOLD TO DO SO.
Problem Score/Points 1 / 2 / 3 / 4 / 5 / 6 / 7 / Total /
Figure 1: Graph for Problem 1. Assume for all figures that the adjacency lists are ordered so that vertices are ordered in alphabetical order in an adjacency list. For example, the adjacency list of A has the edge to B before the edge to D.
Draw the DFS tree of the graph in Figure 1 that will result if the first call to DFS is on vertex A and label each vertex with its pre(in)- and post(out)-order numbers. As per our standing convention, draw tree edges as solid lines, and back, cross, and forward edges as dashed lines. Put a ’B’, ’C’, or ’F’ on each dashed line according to whether it is a back, cross, or forward edge.
Figure 3: Graph for Problem 3.
(a) Execute Prim’s algorithm on the graph of Figure 3 starting at vertex A. If there are any ties, the vertex with the lower letter comes first. List the edges in the order in which they are added to the tree.
(b) Execute Kruskal’s algorithm on the graph of Figure 3 starting at vertex A. Assume that equal weight edges are ordered lexicographically by the labels of their vertices assuming that the lower labeled vertex always comes first when specifying an edge, e.g. (C, E) is before (C, F ) which in turn is before (D, G). List the edges in the order in which they are added to the developing forest.
For each statement below, say whether it is true or false, and a one sentence and/or one picture explanation.
(a) In an undirected graph, we write s ∼k t if and only if there are k vertex disjoint paths between vertices s and t. Is ∼k is an equivalence relation for all choices of k?
FALSE: S ∼k V and V ∼k T but S 6 ∼k T.
(b) T (n) = 4T (n/2) + (^) logn n is solvable with the Master Theorem.
TRUE: (^) logn n = O(n(log^2 4=2)−) so the root dominated case applies.
(c) Suppose T is a shortest paths tree for Dijkstra’s algorithm. After adding c > 0 to every edge in the graph, T is still a shortest paths tree for the modified graph.
(d) It is possible to find a heaviest weight path of exactly k edges from s to each vertex in a directed graph in O(kE) time.
TRUE: k iterations of the Bellman-Ford inner loop with the negative of the original weights.
(e) The heaviest edge in a graph cannot belong to a minimum spanning tree.
A PERT diagram is a business standard for planning and charting a multi-task project. Each task to be performed is modeled as a vertex. The longest and shortest time, short(v) and long(v), are given for each task v. In addition, there are dependencies between the tasks modeled as directed edges, where v → w if task v must precede task w. For example, in a construction project the electrical and plumbing cannot be done until the framing has been completed.
Given a directed graph for a PERT chart, along with the short and long values for each vertex, give an algorithm that first checks that the diagram is acyclic, and then if so, gives the most optimistic (shortest) and most pessimistic (longest) completion times for the entire project.
(a) Run a DFS on the graph and make sure there are no backedges. Recall from lecture that a directed is cycle free if and only if a DFS of the graph has no backedges.
(b) optimistic ← 0 pessimistic ← 0 For each vertex v in topological order do { if indegree(v) = 0 then { longest(v) ← long(v) shortest(v) ← short(v) } else { longest(v) ← maxw→v(longest(w)) + long(v) shortest(v) ← maxw→v(shortest(w)) + short(v) } if outdegree(v) = 0 then { optimistic ← max(shortest(v), optimistic) pessimistic ← max(longest(v), pessimistic) } }
(c) The algorithm clearly takes O(V + E) time for all aspects. An easy proof by induction shows that shortest(v) is the shortest schedule for completing v and longest(v) is the longest schedule. The topological sort guarantees that preceding values of these quan- tities are available when computing the values for v. optimistic and pessimistic are the best of these values over all sink nodes and so the times for the shortest and longest completion times.
Consider a postively weighted directed graph. Design the most efficient algorithm you can for finding a minimum weight simple cycle in the graph. Be sure to prove that your algorithm is correct and to argue carefully its running time.
Consider a minimum weight simple cycle and consider any edge t → s within it. The portion of the cycle from s to t is a simple path and it must be of minimum cost: if it were not then one could concatenate the better path from s to t with the edge t → s and obtain a cycle of lesser cost, a contradiction.
So we have shown that it certainly suffices to compute the shortest path from s to each vertex t and then check the cost of the cycle this path makes with the edge t → s (if it exists). By checking over all possible sources s and all edges to s we are guaranteed of seeing a minimum cycle at least once. In fact we see a cycle once for every edge in it. In summary we have the pseudo-code.
best ← ∞ For each vertex s do { Run Disjkstra’s algorithm from s (distances from s in array dist) For each edge t → s do best ← min(best, weight(t → s) + dist[t]) }
The algorithm clearly takes O(V (V + E)logV ) time if one uses the heap implementation of Dijkstra, or O(V 3 ) time if one uses the list implementation of the priority queue. Space is O(V + E).