This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Introduction to Algorithms Massachusetts Institute of Technology SingaporeMIT Alliance Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Day 23 6.046J/18.410J SMA5503 Handout 24 Problem Set 6 Solutions
Exercise 61. Do exercise 14.15 on page 307 of CLRS. Solution: First find the rank of x. Add i to this value, and find the element with this rank. This takes 2O(lg n) + 1 time. Exercise 62. Do exercise 14.22 on page 310 of CLRS. Solution: Yes, since the black height of a node can be computed from the information at the node and its two children. According to Theorem 14.1 (page 309 of CLRS) insertion and deletion can be still performed in O(lg n) time. Exercise 63. Do exercise 14.31 on page 316 of CLRS. Solution: Assume that before the rotation x has as its left child and y as its right child, and y has as its left child and as its right child. The rotation changes the pointers as in the L EFTROTATE procedure as shown on page 278 of CLRS. In addition, at the end it sets max[y] max[x] and max[x] max(max, max, high[int[x]]) in that order. This takes O(1) time. Exercise 64. Do exercise 33.14 on page 946 of CLRS. Solution: For each point, sort the others by their polar angle relative to that point, and check if any two adjacent points in the sorted order have the same angle. For each point, we need O(n) time to compute the polar angles of all the other points, O(n lg n) time to sort them and O(n) time to check whether any two adjacent points have the same angle. We repeat the process for O(n) points, which gives us a total running time of O(n2 lg n). Exercise 65. Do exercise 33.21 on page 946 of CLRS. Solution: We can show this by construction: consider a regular polygon with n sides. If n is even, for each side of the polygon there is exactly one other side parallel to it (think of a square, or a hexagon). If n is odd, there are no parallel sides (think of a triangle or a pentagon). In any case, of all pairwise n(n  1)/2 combinations of sides, there are at most n/2 pairs of sides which are parallel to each other. The remaining n(n  1)/2  n/2 are not parallel. So if we extend them sufficiently in both directions, at some point they will intersect (pairwise). Thus we will have n segments with n(n  1)/2  n/2 = (n2 ) intersections. Problem 61. Overlapping rectangles VLSI databases commonly represent an integrated circuit as a collection of rectangles. Assume that each rectangle is rectilinearly oriented (sides parallel to the x and yaxis), so that a representation of a rectangle consists of its minimum and maximum x and ycoordinates. 2 Handout 24: Problem Set 6 Solutions (a) Give an O(n lg n)time algorithm that decides whether a set of rectangles so repre sented contains two rectangles that overlap. Your algorithm need not report all inter secting pairs, but it must report that an overlap exists if one rectangle entirely covers another, even if the boundary lines do not intersect. (Hint: Move a "sweep" line across the set of rectangles by replacing one of the two spatial dimensions with time. At all times maintain the collection of rectangles pierced by the sweep line.) Solution: Idea: The main idea here is to move a sweep line from left to right, while maintain ing the set of rectangles intersected by the line in an interval tree. Details: First sort the xcoordinates of the rectangles. Scan the sorted xcoordinate list from lowest to highest. When an xcoordinate of a left edge is found, add the in terval corresponding to the ycoordinates of that edge into the interval tree and check for overlap. When an xcoordinate of a right edge is found, delete the interval cor responding to the ycoordinates of that edge from the interval tree. The interval tree always contains the set of "open" rectangles intersected by the sweep line. (b) Argue that your algorithm indeed runs in O(n lg n) time in the worst case. Solution: This implementation requires O(n lg n) time to sort the xcoordinates, and 2n O(lg n) to maintain the interval tree, for a total running time of O(n lg n). Problem 62. GPS map display After graduating from MIT, you decide to join GiPSy, a startup that hopes to make money by selling an affordable handheld GPS (Global Positioning System) device. The device picks up timestamped messages from 4 geostationary satellites and uses them to calculate its precise co ordinates on the globe by taking into account the orbital position of each satellite (included in the messages) and the time it took each message to reach the device. The device has a rectangular LCD screen which displays the user's exact location on the map, as well as all the nearby streets; see Figure 1. The device must be able to update the map display in real time because the user may be moving and may zoom in or out. GiPSy's first model will only work in cities whose streets are arranged in a grid (such as Manhattan). If the first model proves to be a success in those markets, GiPSy hopes to secure the necessary funding to develop a model that can work in all cities. Your task is to figure out how the city map should be preprocessed and stored on the device, in order to quickly answer queries about what streets are in the vicinity of the user. More precisely, the problem is defined as follows: A road r = (rx1 , ry1 ), (rx2 , ry2 ) is a line segment, either horizontal or vertical, spec ified by the coordinates of its endpoints, (rx1 , ry1 ) and (rx2 , ry2 ). Thus, for every road r = (rx1 , ry1 ), (rx2 , ry2 ), we have either rx1 = rx2 (vertical) or ry1 = ry2 (horizontal). Handout 24: Problem Set 6 Solutions 3 (x2, y2) Washington St Parking Lot X you are here Cambridge St Parking Lot Albany St Vegas St (x1, y1) Figure 1: A sketch of a GiPSy device. Washington St., Vegas St., and Albany St. are Type2 roads. Cambridge St. and Texas St. are Type1 roads. The view rectangle is determined by the coordinates of the lowerleft and upperright corners, as shown. A map M = {r1 , r2 , . . . , rn } is a set of n roads. A view rectangle V = (Vx1 , Vy1 ), (Vx2 , Vy2 ) specifies the rectangular region that should be displayed by giving the coordinates of the rectangle's lowerleft and upperright corners, (Vx1 , Vy1 ) and (Vx2 , Vy2 ) respectively. A road r is visible in the view rectangle V if it intersects the interior of the rectangle V . There are two types of visible roads r: can appear on the rectangular display: Type 1: One or both endpoints of the road r are inside the view rectangle V . Type 2: The road r crosses the view rectangle V but both its endpoints lie outside the view rectangle V . The goal of a clipping query is to report all the visible roads for a given view rectangle V . Because the map M does not change often, we are free to spend a reasonable amount of time preprocessing the map M into a data structure that supports queries efficiently, using a reasonable amount of auxiliary space. Texas St 4 Handout 24: Problem Set 6 Solutions In the problem parts that follows, you will often be called upon to "give an efficient method" for supporting a particular type of query. For each such problem part, you must do the following: 1. Give an efficient algorithm for preprocessing the map M into a data structure. 2. Give an efficient algorithm for using this data structure to answer the query for a given view rectangle V . 3. Analyze the worstcase preprocessing time, worstcase query time, and worstcase space oc cupied by the data structure. In all cases, the analysis should be in terms of the total number n of roads on the map M and the number k of visible roads in the view rectangle V . Optimizing the query time is most important; the preprocessing time is secondary. (a) Give an efficient method for finding all Type1 roads. (Hint: Use a twodimensional range tree.) Solution: Place the endpoints of the roads into a twodimensional range tree. A range query on the view rectangle will return exactly the desired roads. For n roads, there are 2n endpoints, so the running time is asymptotically the same as for 2D range trees: O(n lg n) preprocessing time, O(lg2 n + k) query time, and O(n lg n) space. The rest of the problem is about finding Type2 roads. Without loss of generality, we focus on finding horizontal Type2 roads. A horizontal road r straddles a view rectangle V if it crosses the left edge of V . Thus, every straddling road r is visible. Depending on whether the right endpoint of a straddling horizontal road r is inside the view rectangle, a straddling horizontal road r may be Type1 or Type2. (b) Suppose that we knew how to compute the set of straddling horizontal roads for a given map M and view rectangle V . Give an efficient algorithm to convert the set of straddling horizontal roads into the set of Type2 horizontal roads. Solution: Part (a) identifies all of the Type1 horizontal roads. Mark these roads. Now run through the set of straddling horizontal roads and remove any roads that have been marked. Then we obtain the set of Type2 horizontal roads, because every Type2 horizontal road is a straddling horizontal road. The running time is proportional to the number of straddling horizontal roads, which is at most k. Thus, our goal is to identify which horizontal roads are straddling. We use the following character ization. A horizontal road r = (rx1 , ry ), (rx2 , ry ) straddles the view rectangle V = (Vx1 , Vy1 ), (Vx2 , Vy2 ) if it satisfies two properties: 1. rx1 Vx1 rx2 , i.e., the road r crosses the vertical line extending the left edge of the view rectangle V . We say that r is horizontally straddling. 2. Vy1 ry Vy2 , i.e., the road r falls within the vertical extent of the view rectangle V . We say that r is vertically straddling. Handout 24: Problem Set 6 Solutions (c) Draw a picture showing a view rectangle V and an example of each of the following kinds of visible horizontal roads: 1. 2. 3. 4. 5. straddling and Type2, straddling but not Type2, horizontally straddling but not vertically straddling, vertically straddling but not horizontally straddling, and neither horizontally straddling nor vertically straddling (but still visible). 5 Solution: See Figure 2
5 1 2 4 3 Figure 2: Different kinds of horizontal roads A simple approach for computing the set of straddling horizontal roads is to compute the set of vertically straddling horizontal roads, and remove from this set any roads that are not horizontally straddling. (d) Give an efficient method for finding all vertically straddling horizontal roads. (Hint: Use a onedimensional range tree.) Solution: Place the endpoints of all horizontal roads in a 1D range tree based on their ycoordinates. We can then use this range tree to find all the roads whose ycoordinates lie within the vertical extent of the view rectangle V , that is Vy1 ry Vy2 . The preprocessing time to build the range tree is O(n lg n), and the tree requires O(n) space. Each subsequent query takes time O(lg n + p) where p is the number of candi date roads on the map M . Once we have the set of vertically straddling horizontal roads, we can simply run through the list and remove any roads that are not horizontally straddling. This filtering results in the set of straddling horizontal roads. 6 Handout 24: Problem Set 6 Solutions (e) Analyze the worstcase running time of this filtering algorithm. Solution: The running time is linear in the number of vertically straddling horizontal roads, which can be as high as (n). (f) Explain why this approach is slow in the worst case. Solution: The worst case occurs when all roads on the map are vertically straddling. In that case, the running time is (n), even though few of these roads may actually be horizontally straddling as well (and hence visible). Our final goal is to develop a faster method for computing the set of straddling horizontal roads. (g) Show that instead of explicitly enumerating all vertically straddling horizontal roads in part (d), we can find O(lg n) nodes in the range tree whose descendants contain all of the vertically straddling horizontal roads and no other roads. Give an efficient algorithm to find these O(lg n) nodes. Solution: If we insert all horizontal roads into a 1D range tree based on their ycoordinates, then as we have seen in lecture, there exist O(lg n) subtrees in the range tree whose nodes contain the result of the query and nothing else. In order to find the nodes on which these O(lg n) subtrees are rooted, we can use the procedure 1D R ANGE Q UERY presented in lecture, which runs in O(lg n) time. (h) Give an efficient method for finding all the straddling horizontal roads. (Hint: Com bine interval trees with range trees.) Solution: As we have seen above, the range tree based on the ycoordinates of the horizontal roads returns O(lg n) subtrees per query. These subtrees contain all the vertically straddling horizontal roads. In order to find which of those roads are also horizontally stradddling (and hence in view of the rectangle), we can "preemptively" construct an interval tree for each one of the nodes of the range tree. The interval tree associated with each node stores the xcoordinate intervals of all the roads in the entire subtree rooted at that node. This way, when we get the O(lg n) subtrees after issuing a query to the range tree (based on ycoordinate), we can issue separate queries to the interval trees of each subtree root to find all the roads which intersect the horizontal range of the rectangle (that is, the interval [Vx1 , Vx2 ]). Query time: Since each interval tree stores at most O(n) points, each query to the interval tree takes time O(k lg n) where k is the number of roads returned by that interval tree. Note that the time taken for a query that returns no roads (k = 0) is O(lg n) and not O(0). So a more precise description of the running time would be O(lg n + k lg n). We issue O(lg n) such queries, for a total running time of O(lg 2 n + K lg n) where K is the total number of straddling roads. Handout 24: Problem Set 6 Solutions Preprocessing Time: Building each interval tree takes time O(m lg m) where m is the number of nodes it contains. So we need O(n lg n) time to build the interval tree for the root of the range tree, O (2(n/2) lg(n/2)) for the roots of its subtrees etc. Since 7 n n n n n n n lg n + 2 lg +4 lg + . . . 0 = n lg n + lg + lg + . . . + 0 2 2 4 4 2 4 = n [lg n + lg n  1 + lg n  2 + . . . + lg n  lg n] (lg n)2 n (lg n)(lg n)  2 = n lg2 n 2 the time it takes to initially build all these trees is is O(n lg2 n). This can also be seen by considering the recurrence T (n) = 2T (n/2) + (n lg n) which falls under case 2 of the Master Theorem and gives us the same answer. Space: Storing these interval trees takes O(n lg n) space since we have O(n) points and each point is stored in exactly one interval tree at each level of the original range tree (similar analysis as for 2D range trees). (i) Conclude by analyzing the entire method for answering clipping queries. Solution: We need O(n lg n) time to build the range tree and O(n lg2 n) time to build the interval trees, for a total of O(n lg2 n) time for the preprocessing stage. The space we need for the range tree is O(n), and we need another O(n lg n) for the interval trees, for a total of O(n lg n). Each query to the range tree takes O(lg n) and returns O(lg n) subtrees. As we saw above, the time it takes takes to query the associated O(lg n) interval trees is O(lg2 n+ K lg n) where K is the total number of visible roads. Thus the total running time for each query is O(lg2 n + K lg n). ...
View Full
Document
 Fall '04
 PiotrIndykandCharlesE.Leiserson
 Algorithms

Click to edit the document details