


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main points of this past exam are: Guidance Counselor, Conquer, Original Problem, Largest Integer, Array Element, Every Algorithm, Recursive Algorithm, Connected Undirected, Nodes, Uniform Distribution
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



UC Berkeley—CS 170 Solutions to Midterm 1 Lecturer: Tom Henzinger October 8, 2002
Suppose there are three alternatives for dividing a problem of size n into subproblems of smaller size: if you solve 3 subproblems of size n 2 , then the cost for combining the solutions of the subproblems to obtain a solution for the original problem is Θ(n^2
n); if you solve 4 subproblems of size n 2 , then the cost for combining the solutions is Θ(n^2 ); if you solve 5 subproblems of size n 2 , then the cost for combining the solutions is Θ(n log n). Which alternative do you prefer and why? Answer : The first recurrence is T (n) = 3T ( n 2 ) + Θ(n^2.^5 ); since log 2 3 < 2 .5, the master theorem tells us that T (n) = Θ(n^2.^5 ). The second recurrence is T (n) = 4T ( n 2 ) + Θ(n^2 ); since log 2 4 = 2, the master theorem says T (n) = Θ(n^2 log n). The third recurrence is T (n) = 5T ( n 2 ) + Θ(n log n); since log 2 5 > 2, the master theorem says T (n) = Θ(nlog^2 5 ). The second alternative is the best.
Consider the following problem: given an array A[1..n] of distinct integers, and a number 1 ≤ k ≤ n, find any one of the k largest elements in A. For example, if k = 2, it is ok to return the largest or second largest integer in A, without knowing if the return value is the largest or if it is the second largest array element.
(a) Give an algorithm that solves this problem using no more that n − k comparisons of array elements.
(b) Argue that every algorithm that solves this problem must, in the worst case, perform at least n − k comparisons.
Answer :
(a) x := A[1]; for i = 2 to n − k + 1 do if A[i] > x then x := A[i] end; return x.
(b) Suppose an algorithm performs only n − k − 1 comparisons. Then at the end, there are at least k + 1 elements that have not lost a comparison. The algorithm must return one of them, say A[i]. The adversary can choose the other k elements that have not lost a comparison to be larger than A[i], thus proving the algorithm wrong.
You are a guidance counselor in charge of putting high school students into one of two study halls. It doesn’t matter how many students are in each study hall; what does matter is that certain pairs of students do not get along well and would cause a major disruption if they were placed in the same study hall. There are n students and you have a list of b
pairs of students who shouldn’t be placed together. Give an algorithm that determines in time O(n + b) whether it is possible to allocate the students to the two study halls without violating the b constraints. If it is possible to perform such a designation, your algorithm should produce it. (Note that some students may occur multiple times in the list of “bad” pairs, but no student would be paired with him/herself.)
Answer : There is an assignment of students to study halls if and only if the undirected graph (V, E), where V is the set of n students and E is the set of b bad pairs, is bipartite. A modification of DFS will do the job in time O(n + b). The following algorithm computes for each node v a boolean value hall(v): if hall(v) = true, then student v is assigned to the first study hall, otherwise to the second.
for each v ∈ V do visited(v) := false; for each v ∈ V do if not visited(v) then Explore(v, true).
procedure Explore(v, h): visited(v) := true; hall (v) := h; for each (v, w) ∈ E do if visited(w) and hall (w) = h then stop and report “no assignment possible”; if not visited(w) then Explore(v, not h).
Somebody proposes the following recursive algorithm to find a minimum spanning tree (MST) of a connected undirected graph G = (V, E) with edge weights:
First, partition the nodes V into two non-empty sets, S and V − S, so that each of the resulting parts of the graph, call them GS and GV −S , is connected. Second, recursively find a MST TS for the subgraph GS , and a MST TV −S for the subgraph GV −S. Third, construct from TS and TV −S a spanning tree for G by choosing from all edges {v, w} ∈ E with v ∈ S and w ∈ (V − S) one of minimum weight.
Argue that this algorithm always finds a MST of G (for example, by demonstrating that it is an instance of the generic MST algorithm from class), or give a counterexample.
Answer : The algorithm is incorrect. On the following graph, it may return a spanning tree of cost 7, while the MST has cost 6.
2
3 3
1
S V − S
(a) In a tree, every edge by itself is a cut, because it disconnects the graph. Hence the minimum edges are the minimum cuts. The algorithm contracts all edges except for some minimum edge, and thus finds a minimum cut.
(b) For the following graph, the minimum cut has cost 2, but the algorithm finds a cut of cost 3. The first decision of the algorithm, to contract the edge with weight 2, is a wrong choice. 1
1
1 1 1
1
2