CSE 101 Homework 2 - UCSD VLSI CAD Laboratory
Transcription
CSE 101 Homework 2 - UCSD VLSI CAD Laboratory
CSE 101 Homework 2 January 20, 2015 CSE 101: Design and Analysis of Algorithms Winter 2015 Homework 2 Due: January 29, 2015 1. (DPV textbook, Problem 2.4.) Suppose you are choosing between the following three algorithms: (a) Algorithm A solves problems by dividing them into five subproblems of half the size, recursively solving each subproblem, and the combining the solutions in linear time. (b) Algorithm B solves problems of size n by recursively solving two subproblems of size n − 1 and then combining the solutions in constant time. (c) Algorithm C solves problems of size n by dividing them into nine subproblems of size n/3, recursively solving each subproblem, and then combining the solutions in O(n2 ) time. What are the recurrence relations and running times of each of these algorithms (in big-O notation) and which would you choose? 2. Suppose you are given a directed graph with weighted edges. You know that one edge has negative weight and that all the other edges have positive weights. Find a linear-time (O(|V | + |E|), where |V | is the number of vertices and |E| is the number of edges in the graph) algorithm to determine whether the graph contains a negative cycle. 3. (DPV textbook, Problem 4.22.) You are given two sorted lists of size m and n. Give an O(log m+log n)time algorithm for computing the k th smallest element in the union of the two lists. 4. Suppose you are given a set of n intervals [x1 , y1 ], [x2 , y2 ], ..., [xn , yn ], with [xi , yi ] being the interval from xi to yi , inclusive. Give an O(n log n) algorithm to find whether or not the set contains at least one interval that is completely contained within another distinct interval. That is, find if there exists some i, j ≤ n, i 6= j such that xi ≤ xj and yi ≥ yj . 5. Prove or provide a counterexample to each of the following statements. (a) Suppose Dijkstra’s algorithm successfully finds a shortest path from some vertex s to some vertex t in a graph with uniform (i.e., identical) positive edge weights. If we add a real value k ≥ 0 to the weight of each edge, the path we found before will still be a shortest path. (b) Suppose Dijkstra’s algorithm successfully finds a shortest path from some vertex s to some vertex t on a directed graph with positive edge weights. If we add a real value k ≥ 0 to the weight of each edge, the path we found before will still be a shortest path. (c) (DPV textbook, problem 4.9, modified) Given a graph in which the only negative-weight edges are those that leave s (all other edge weights are positive), Dijkstra’s algorithm will correctly identify the shortest s − t path. 6. (DPV textbook, Problem 4.20.) There is a network of roads G = (V, E) connecting a set of cities V . Each road in E has an associated length le . There is a proposal to add one new road to this network, 0 and there is a list E of pairs of cities between which the new road can be built. Each such potential 0 0 road e ∈ E has an associated length. As a designer for the public works department you are asked 1 0 0 to determine the road e ∈ E whose addition to the existing network G would result in the maximum decrease in the driving distance between two fixed cities s and t in the network. Give an efficient algorithm for solving this problem. 7. Consider the following game played on a triangular board with base of size n and height of size n. You start at the apex of the triangular board and make your way to the bottom. At each iteration, you can (i) move one square down, or (ii) move diagonally one square down and one square to the left. Each square is labeled with a positive number, and your score is the sum of the numbers of all of the n grid squares that you pass over (including the starting square). The goal is to find a sequence of moves resulting in the lowest score possible upon reaching the bottom row. For example, a board with n = 5 might look like the following with a best possible score of 26 highlighted. 9 7 18 8 20 17 6 12 15 1 4 11 2 41 5 9 7 18 8 20 17 6 12 15 1 4 11 2 41 5 (a) Describe how you could transform this into a shortest-path graph problem. Clearly state how to build the graph, and what nodes and edges in the graph represent (e.g. add an . . . ). (b) State how many nodes and edges are in your constructed graph, using big-O functions of n. (c) Between which two nodes in your constructed graph are you looking for the shortest path? (d) What would you change in order to find the sequence of moves resulting in the highest score? EXTRA CREDIT. (Question 8 is worth 0.1 homeworks and Question 9 is worth 0.2 homeworks. (Recall: Each homework is 4 out of the 110 points achievable in the class.)) 8. In the Tower of Hanoi puzzle, suppose our goal is to transfer all n disks from peg 1 to peg 3, but now we cannot move a disk directly between peg 1 and 3. Each move of a disk must be a move either to peg 2 or from peg 2. As usual, we cannot place a disk on top of a smaller disk. (a) Find a recurrence relation for the minimum number of moves required to solve the Tower of Hanoi puzzle for n disks, given this added restriction. (b) Solve this recurrence relation to find a formula for the minimum number of moves required to solve the puzzle for n disks. 9. Watermelon Testing. A new breed of watermelon is very difficult to shatter even when dropped from great heights. To determine the number of stories that must be added to Urey Hall at UCSD, we want to experimentally determine the highest floor in a 100-story building from which such a watermelon can be dropped without breaking. You have exactly two identical examples of this new watermelon breed. A given watermelon can be dropped multiple times, unless it breaks (after which time you can’t drop that watermelon any more). What is the minimum number of droppings that is guaranteed to determine the highest safe floor in all cases? And, give a justification for your answer! [Notes. (i) If you had an unlimited number of examples of the new watermelon (or, at least, dlog2 100e examples), you’d use binary search: drop from Floor 50, then drop from Floor 25 or 75, etc. But, unfortunately, you have only two examples to work with. (ii) If you had only one example of the new watermelon to work with, you’d use linear search: drop from Floor 1, drop from Floor 2, etc. But, fortunately, you have two examples to work with.] 2