0% found this document useful (0 votes)
168 views7 pages

CS170 Final Exam Answer Key

An algorithm is presented to optimize the placement of falafel carts at street corners in San Francisco, given profit estimates for deploying carts at each corner based on whether adjacent corners also have carts. The problem is modeled as a dynamic programming problem, defining subproblems to track the optimal profit achieved so far based on the cart configuration at each corner. Recurrence relations are given to calculate the subproblems in linear time. When the corners are on a graph instead of a path, the problem is shown to be NP-complete via a reduction from independent set, by defining the profits such that the optimal solution corresponds to finding an independent set of a given size.

Uploaded by

John Smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
168 views7 pages

CS170 Final Exam Answer Key

An algorithm is presented to optimize the placement of falafel carts at street corners in San Francisco, given profit estimates for deploying carts at each corner based on whether adjacent corners also have carts. The problem is modeled as a dynamic programming problem, defining subproblems to track the optimal profit achieved so far based on the cart configuration at each corner. Recurrence relations are given to calculate the subproblems in linear time. When the corners are on a graph instead of a path, the problem is shown to be NP-complete via a reduction from independent set, by defining the profits such that the optimal solution corresponds to finding an independent set of a given size.

Uploaded by

John Smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

U.C.

Berkeley CS170 : Efficient Algorithms and Intractable Problems


Lecturer: Christos Papadimitriou

Final
December 18, 2009

Final Answer Key

Problem 1 (40 points)


No explanation needed:
What is the solution of the recurrence T (n) = 5T (n/4) + n3 in -notation?
Using the Master Theorem, we obtain T (n) = (n3 ).
An undirected graph with n nodes is connected, and then an edge is deleted. What is the largest
number of connected components the new graph can have?
By deleting an edge, we can at most split the graph into 2 components.
An undirected graph with n nodes is connected, and then a node is deleted. What is the largest number
of connected components the new graph can have?
By deleting a node, the largest number of connected components left is n 1 (e.g. all the other nodes
connected only to the node removed).
A directed graph is strongly connected, and then an edge is deleted. What is the largest number of
strongly connected components the new graph can have?
By deleting an edge, we end up with at most n strongly connected components (e.g. the initial graph
is a SCC cycle).
3,000

What is 75

mod 47?
3000

47 is prime, so 75

3000

(mod 47) = 75

(mod 46)

(mod 47).

Moreover, 46 = 2 23 and it is relatively prime to 5, so 53000 (mod 46) = 53000


get:

(mod 22)

(mod 46). We

3000 (mod 22) = 8;


53000 (mod 46) = 58 (mod 46) = 39;
3000

75

(mod 47) = 739 (mod 47) = 732+4+2+1 (mod 47) = 64 4 2 7 (mod 47) = 21

In RSA, if p = 17, q = 11, and e = 3, what is d?


d = e1 mod (p 1)(q 1) = 31 mod 160 = 107.
In the same RSA, if the message is 7, what is the encoding?
Encoding is 73 mod 187 = 156.
You want to multiply two polynomials of degree 6 using the FFT. What is your going to be?
The degree f the product polynomial is 12, so we need at least 13 points. The smallest N we can use
is 16, so = e2i/16 = ei/8 .
Assume one of the polynomials happens to be x6 + 1, and the result of its FFT is (A0 , A1 , . . .). What
is A0 ? What is A1 ?
A0 = p( 0 ) = p(1) = 1; A1 = p( 1 ) = p() = 1 + e3i/4
1

Extra credit: What is (x a) (x b) (x c) (x z)?


It is 0; the term (x x) appears in the product.
What is the dual of this linear program?
maximize x1 + x2 + x3
subject to
x1 + x3 = 2
x1 + 2x3 4
x1 x2 2
and x1 , x2 , x3 0
The dual is:
minimize 2y1 + 4y2 + 2y3
subject to
y1 + y2 + y3 1
y3 1
y1 + 2y2 1
and y2 , y3 0
Does the original LP have an optimum? If so write down the optimal value and the corresponding
x1 , x2 , x3 . If not, what does this imply for the dual?
No, the primal is unbounded, since x2 can get arbitrarily large. Hence, the dual is infeasible.

Problem 2 (30 points)


You are given a graph (V, E) with positive edge weights, and its minimum spanning tree. Now you are told
that the length of edge [u, v] (it may or may not be on the tree) has changed (increased or decreased) to
`new . Describe an O(|E|) algorithm for finding the new minimum spanning tree.
For the case where the weight decreases we do the following:
If the edge e = {u, v} with decreased weight is in the tree T , then we return T .
Otherwise, we add the edge e = {u, v} to the tree T , creating a single simple cycle. We can determine
all edges in the cycle in O(|V |) time by running a DFS from node u (using only edges in T ) until we
reach node v. Once we have found all edges in the simple cycle, we then delete the highest cost edge
e0 from the cycle (with e0 = e possible) to obtain a new spanning tree T 0 .
For the case where the weight increases we do the following:
If the edge e with increased weight is not in T , then return T .
Otherwise, we remove e = {u, v} from the tree to obtain two connected components, one containing u
and one containing v. We can then run two BFSs in O(|V | + |E|) time (only using tree edges) to label
the nodes in us component L1, and label the nodes in vs component L2. Using these labels, we can
then iterate over all edges to nd the least cost edge e0 crossing from us component to vs component.
We then return the tree T 0 = T e + e0 .
What if you are told that the lengths of m edges have changed? How fast could you find the new minimum
spanning tree? Explain very briefly.
We can run the above algorithm m times using as an input MST, the MST computed at the previous iteration. The crucial observation is that each run of the algorithm must be done after the previous one is
completed. This gives a total running time of O(m|E|).
For which values of m is the algorithm of the previous question faster than finding the MST from scratch?
For values m log |V | (or log |V | if we use path compression) it is better to use the algorithm of the
previous question. For larger values of m its better to build the MST from scratch.

Problem 3 (15 points)


Recall that the Rudrata Path problem (Given a graph (V, E), find a path (no repeated nodes) of length
(number of edges) |V | 1) is NP-complete. The half-Rudrata
Path problem is the following: Given a
j k

graph (V, E), find a path (no repeated nodes) of length

|V |
2

(number of edges). Prove that it is NP-complete.

The problem is in NP: given


j a kset of nodes it is easy (i.e. can be done in polynomial time) to check that
they form a path of length |V2 | .
There are two obvious reductions.
The first one takes an instance of Rudrata Path and copies it; the two copies are independent in the sense
that we add no edges betweenjthem.
k This is a valid instance for half-Rudrata Path and it is easy to see
|V |
that it has a a path of length 2 iff the initial Rudrata Path instance had a path of length |V | 1.
An alternative reduction is to take an instance of Rudrata Path and add |V | singleton nodes (that are
not
anywhere). Again, this is an instance of half-Rudrata Path and it has a a path of length
j connected
k
|V |
2

iff the initial Rudrata Path instance had a path of length |V | 1.

Problem 4 (45 points)


You are the CEO of Falafel International. Your company is planning to deploy falafel carts at street corners
in San Francisco. You are considering n corners 1, 2, . . . , n, aligned on a path, and you want to decide for
each one of them whether to deploy a cart there or not. For each street corner i your marketing people have
given you three numbers, ai , bi , and ci , possibly negative, which are the estimated daily profits from street
corner i if a cart is deployed there and, respectively, none, or one, or two of the adjacent corners i 1 and
i + 1 also have a cart (obviously, c1 and cn have no meaning, and can be anything). You want to use the
latest corporate planning trend called Dynamic Programming to find the best possible allocation of carts to
street corners.
(a) Define a subproblem, write the recurrence equation, show how to recover the optimum solution, and tell
us how long your algorithm takes to run.
We will define a series of subproblems encoding the optimum so far, under the condition that at current
position n we have: no cart (Z[i]), one cart with no neighbors (O[i]), one cart with only a right neighbor
(R[i]), one cart with only a left neighbor (L[i]), one cart with both neighbors (B[i]). The update rules are as
follows:
Z[i] = max(Z[i 1], O[i 1], R[i 1])
O[i] = Z[i 1] + ai
R[i] = max(L[i 1], B[i 1]) + bi
L[i] = Z[i 1] + bi
B[i] = max(L[i 1], B[i 1]) + ci
Of course, we need to satisfy the constraints at i = 0 (all arrays there are undefined except Z[0] = 0, O[0] =
a0 , R[0] = b0 . The value of the optimal solution is the maximum of Z[n], O[n] and R[n]. In order to recover
the solution, we can keep an extra array for each Z, R and B subproblem, that indicates where we came
from. The total running time is linear, because for every i, we only look at a constant number of elements.
(b) Suppose now that the possible corners lie on a graph G, instead of a path, and again you are given profit
estimates ai , bi , and ci , in the case in which none, or any one, or any two or more of the adjacent corners
are occupied by carts. Show that now finding the optimum allocation is NP-complete.
First well show that the search problem is in NP. Given a cart assignment, how to we check it has a certain
value? Easy, just look at each vertex and add the amount generated by it - polynomial time. Now we show
completeness, by reducing INDEPENDENT SET to our CART problem.
For a given INDEPENDENT SET problem with graph G=(V, E) and value k, we create a CART problem
with the same graph G and objective k. For each vertex, we set ai = 1, bi = |V |, ci = |V |. We need to
show that an independent of size k generates a CART of value k, and viceversa. First direction is easy, all
vertices in the independent set S have no neighbors in S, so if we choose those in CART we make a profit of
k. For the other direction, we notice, the following fact: in our CART solution, we never choose two adjacent
vertices (because then the total value would be negative), so any CART solution of value k defines k vertices
with no edges between them (i.e. an independent set)

Problem 5 (40 points)


True or False? Circle the right answer. No explanation needed. No points will de deducted for wrong
answers, so give us your best guess.
F

The following is a legitimate choice for RSA: N = pq = 35, e = 3.

Computing ab mod c for n-bit numbers a, b, c can be done in O(n2.99 ) time.

If from a particular vertex v in an undirected graph there is a unique path to every other vertex, then
the graph is a tree.

The shortest paths between all pairs of vertices in a graph with both positive and negative weights can
be found in O(|V |3 ) time.

If a graph has no negative cycles reachable from s, then Dijkstras algorithm works in finding shortest
paths from s.

The Bellman-Ford algorithm takes time O(|V ||E|).

If all weights in a graph are different, then there is a unique shortest path from s to t.

A set of n Horn clauses can be tested for satisfiability in polynomial time.

(
x y) is a Horn clause.

(x y) is a Horn clause.

Because of recursion overhead, recursion with memoization is always slower than direct dynamic programming.

The dynamic programming algorithm for the traveling salesman problem may take time proportional
to n! for some instances.

There is a polynomial algorithm for linear programming.

Simplex is a polynomial-time algorithm.

If in a network we increase the capacity of an edge in the minimum cut, the maximum flow gets
increased.

If in a network we decrease the capacity of an edge in the minimum cut, the maximum flow gets
decreased.

If in a network we increase the capacity of an edge which is not in the minimum cut, the maximum
flow does not get increased.

In a zero-sum game it is always possible for both players to play best response to what the other is
playing.
For the remaining questions there are four possible answers: 1. true (T); 2. false (F) 3. true iff P =
NP (=); and 4. true iff P 6= NP (6=). In each case, choose the right one.

3sat is NP-complete.

Hornsat (finding a satisfying truth assignment for a set of Horn clauses) is NP-complete.

There is a polynomial reduction from Hornsat to 3sat.


6

There is a polynomial reduction from 3sat to Hornsat.

3sat is in P.

Hornsat is in P.

There is no known polynomial algorithm for 3sat.

There is no known polynomial algorithm for Hornsat.

factoring is in NP.

6=

There is no polynomial time algorithm for computing the optimum tour of an instance of the traveling
salesman problem.

There is a polynomial time algorithm for telling whether in an unweighted graph there is a path of
length 10 from s to t.

There is a polynomial reduction from maximum flow to linear programming.

There is a polynomial reduction from minimum spanning tree to linear programming.

There is a polynomial reduction from integer linear programming to linear programming.

You might also like