
















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
Which algorithm design paradigm does this algorithm best represent? 1. Divide-and-conquer. 2. Greedy algorithm. 3. Dynamic programming. 4. Linear programming. 5 ...
Typology: Exercises
1 / 24
This page cannot be seen from the preview
Don't miss anything!

















P RINT your name: , (last) (first)
S IGN your name:
Your Student ID number:
Your Unix account login: cs170-
Name of the person sitting to your left:
Name of the person sitting to your right:
You may consult three double-sided sheet of notes. You may not consult other notes, textbooks, etc. Computers and other electronic devices are not permitted.
There are 16 questions. The last page is page 24.
Reminder: When we ask for an efficient algorithm, we want one whose asymptotic running time is as fast as possible.
Answer all questions. Be precise and concise. Write in the space provided. Good luck!
Do not turn this page until an instructor tells you to do so.
Circle T RUE or FALSE. Do not justify your answers on this problem.
(a) T RUE or FALSE : If we perform DFS on an undirected graph, there are no cross edges.
(b) T RUE or FALSE : If the DFS tree has no back edges, then there are no cycles in the graph.
(c) T RUE or FALSE : Any tree with n vertices is guaranteed to have exactly n 1 edges.
(d) T RUE or FALSE : Suppose G is a directed graph G = (V, E), and define the undirected graph G^0 by G 0 = (V, E 0 ) where E 0 is the set of edges {u, v} such that (u, v) 2 E or (v, u) 2 E. Then, if G is not strongly connected, then it’s guaranteed that G 0 is not connected.
(e) T RUE or FALSE : If G is a strongly connected directed graph, then there is guaranteed to exist a vertex such that deleting that vertex leaves you with a graph that is also strongly connected.
(f) T RUE or FALSE : Suppose we have a directed acyclic graph (dag) G with at least n 1 edges and where a topological sort yields the vertices v 1 ,... , vn , in that order. If we add the edge (vn , v 1 ), then the resulting graph is guaranteed to be strongly connected.
(g) T RUE or FALSE : Dijkstra’s algorithm will always work correctly on any graph that has at most two negative edges.
(h) T RUE or FALSE : If an undirected graph with n vertices has k connected components, then it must have at least n k edges.
(i) T RUE or FALSE : If a graph with integer lengths on the edges (some of them possibly negative) has no negative cycle reachable from s, then Dijkstra’s algorithm works correctly for finding the length of the shortest path from s to every other vertex reachable from s.
(j) T RUE or FALSE : If we have a linear program where all of the coefficients and constants in every linear inequality are integers, then the optimum solution to this linear program must assign an integer value to every variable.
(k) T RUE or FALSE : If we have an undirected graph G = (V, E) where |V | = |E|, then G is guar- anteed to be a tree.
(l) T RUE or FALSE : If G is a graph with positive lengths on the edges, and if all edge lengths are distinct, and if s,t are two vertices, then there is a unique shortest path from s to t.
Answer the following questions, giving a short justification (a sentence or two).
(a) Suppose we have already trained two classifiers. Classifier A is a k-nearest neighbors classifier with a fixed k = 25, using a linear search implementation (not a k-d tree). Classifier B is a random forest classifier with a fixed T = 25 (i.e., 25 decision trees), where each decision tree has depth at most 10. Now we want to know the running time to classify 100 test points. If the number of observations in the training set is very large, which classifier will be faster at this task? In other words, if we consider the asymptotic running time for this task as a function of n, which classifier will have an asymptotically faster running time?
(b) Let n be a power of two. Suppose you don’t know the coefficients of a degree n 1 polynomial f , but you have its values at the nth roots of unity. How quickly could you find the value of f at some given other point x? In other words, what would the asymptotic running time be, as a function of n? You can assume that it takes O( 1 ) time to add or multiply two numbers.
(c) In a strongly connected directed graph with integer weights (some of them possibly negative), is it possible that there are vertices s and t with no shortest path from s to t? Write “yes” or “no” and justify your answer in a sentence or two. Then, if you wrote “yes”, name an algorithm we could use to detect whether this is the case; if you wrote “no”, name an algorithm we could use to detect whether there are two vertices with no longest path from one to the other (i.e., the same task except with shortest replaced by longest).
We have a directed graph G = (V, E) with n vertices and m edges and integer lengths on all the edges. Only k of the edges have negative lengths; all the others have non-negative lengths. There are no negative cycles.
Your friend has a clever idea for computing the length of the shortest path from a vertex s to a vertex t. He will construct a new graph G^0 whose vertices are the endpoints of the negative edges of G, together with s and t. Each edge (u, v) in G 0 represents the length of the shortest path from u to v in G without using any negative-length edge. He’ll use Dijkstra’s algorithm to compute each of these lengths. Then, he’ll add in all of the negative edges to G^0 , and run Bellman-Ford on the result.
What is the asymptotic running time of your friend’s algorithm, as a function of n and m and k? Give a short justification (a sentence or two).
A set of n space stations need your help in building a radar system to track spaceships traveling between them. The ith space station is located in 3D space at coordinates (xi , yi , z (^) i ). The space stations never move. Each space station will have a radar of some power, say ri for the ith space station, where r (^) i is to be determined.
You want to figure how powerful to make each space station’s radar transmitter, so that whenever any spaceship travels in a straight line from one space station to another, it will always be in radar range of either the first space station (its origin) or the second space station (its destination). A radar with power r is capable of tracking space ships anywhere in the sphere with radius r centered at itself. Thus, a space ship is within radar range through its trip from space station i to space station j if every point along the line from (xi , yi , z (^) i ) to (x (^) j , y (^) j , z (^) j ) falls within either the sphere of radius r (^) i centered at (xi , yi , z (^) i ) or the sphere of radius r (^) j centered (x (^) j , y (^) j , z (^) j ). The cost of each radar transmitter is proportional to its power, and you want to minimize the total cost of all of the radar transmitters.
You are given all of the (x 1 , y 1 , z 1 ),... , (xn , yn , zn ) values, and your job is to choose values for r 1 ,... , rn. Express this problem as a linear program.
(a) Describe your variables for the linear program.
(b) Write out the objective function.
(c) Between each pair of space stations, say station i and station j, we need one constraint (one linear inequality). What is it?
Our spaceship’s cook has baked a zero-gravity pizza and cut it into n slices, but the lack of gravity made him clumsy and the pizza wasn’t evenly sliced: the n slices have size s 1 , s 2 ,... , s (^) n. There are n hungry space travelers on board who each want to eat a slice of pizza. Suppose the ith traveler would be happy with any slice whose size is at least t (^) i. Give an efficient algorithm to determine whether it is possible to distribute the pizza slices so everyone is happy.
(a) What algorithm design paradigm is most appropriate for this problem? Circle one of the following.
(a) Divide-and-conquer (b) Greedy algorithm (c) Dynamic programming (d) Linear programming (e) Reduce to network flow (f) None of the above
(b) Show pseudocode for your algorithm. (No proof of correctness needed.)
(c) What is the asymptotic running time of your algorithm? You don’t need to justify your answer.
(c) What is the asymptotic running time of your algorithm from part (b)? Write your answer as a function of n, K, and/or L, where we define L = a 1 + a 2 + · · · + a (^) n. You don’t need to justify your answer.
Consider the discrete knapsack problem where all items have the same value 42. In other words, we have n items, where wi is the weight of the ith item and every item has value 42, and we have a knapsack of capacity C. (This is knapsack without repetition: each item can be chosen at most once.) We want to find a set of items whose total weight is at most C and whose total value is as large as possible.
Devise an efficient algorithm for this problem, then answer the following two questions about it.
(a) What algorithm design paradigm is most appropriate for this problem? Circle one of the following.
(a) Divide-and-conquer (b) Greedy algorithm (c) Dynamic programming (d) Linear programming (e) Reduce to network flow (f) None of the above
(b) What is the asymptotic running time of your algorithm? Justify your answer in a sentence or two.
Oh no! Someone has stolen the draft of Rohit’s paper proving P = NP. The thief is trying to get out of Berkeley before getting caught. The Berkeley police have asked you to help them block the thief’s escape.
The roads and intersections are represented as an unweighted directed graph G = (V, E). We’re also given a subset C ✓ V of possible current locations of the thief’s car and a set P ✓ V of all exit points out of Berkeley. The police want to set up roadblocks on a subset of the edges to prevent the thief from escaping (i.e., from reaching one of the exit points). Clearly the police could just put a roadblock at all the edges to each exit point, but there might be a better way: for example, if the thief is known to be at a particular intersection, they could just block all roads coming out of it. You may assume that roadblocks can be set up instantaneously.
Design an efficient algorithm to find a way to stop the thief using the smallest possible number of roadblocks.
(a) What algorithm design paradigm is most appropriate for this problem? Circle one of the following.
(a) Divide-and-conquer (b) Greedy algorithm (c) Dynamic programming (d) Linear programming (e) Reduce to network flow (f) None of the above
(b) Describe your algorithm. (You don’t need to prove it correct.)
(c) What is the asymptotic running time of your algorithm in part (b)? Your answer can be a function of |C|, |P|, |V |, and/or |E|. You don’t need to justify your answer.
(b) How many vertices does your graph G have?
(c) How many edges does your graph G have? (at most)
(d) What is the asymptotic running time of the resulting algorithm, as a function of m and n?
By exploiting a handy wormhole to travel through time, Daniel knows what the stock price of Intergalactic Starships, Inc. will be on every day over the next n days: on day i, each share of stock costs pi. Daniel starts out with $1000. He plans to pick one day to buy as many shares of stock as he can afford and then sell them all on some subsequent day. Design an efficient algorithm to help Daniel figure out what’s the maximum profit he can achieve. (Warning: Stock prices for starship companies can fluctuate a lot. There is no guarantee that the day with the lowest stock price will come before the day with the highest stock price.)
(a) Show your pseudocode. You don’t need to prove your pseudocode correct.
(b) What is the asymptotic running time of your algorithm? Justify your answer concisely.
The inhabitants of a far off star system have developed a transportation system between their planets. There are n planets in the system. Some planets have a wormhole between them, which allows you to travel between planet i to planet j in ti, j seconds. All times t (^) i, j are positive (you can’t travel back in time). Some pairs of planets may not have a wormhole between them.
You are currently at planet 0 and wish to reach planet n 1. However, your ship’s navigation commands are starting to malfunction. When you are at a planet and tell the ship to go through a certain wormhole, it may misinterpret the command and instead choose a completely random outgoing wormhole instead. Luckily, you know that you that this will happen at most once. You don’t know when the malfunction might happen, but you are going to assume it will happen at the most inconvenient time.
You want to choose a strategy that will minimize your total travel time in the very worst case (no matter when the malfunction happens or which outgoing wormhole it takes you on). Design an efficient algorithm for this. Specifically, your algorithm should output at what time you can be guaranteed to reach planet n 1, if we use the optimal strategy.
You don’t need to prove your algorithm correct or justify the stated running time. You may assume it’s possible to reach planet n 1 from any other planet via some sequence of wormholes.
(a) Describe your main idea, in enough detail that another CS 170 student could implement your algorithm. (No pseudocode or proof of correctness needed.)
(b) What’s the asymptotic running time of your algorithm from part (a), if there are n planets and m wormholes? No justification needed.