Problem Set 6 Solutions

Similar documents
Problem Set 5. MIT students: Each problem should be done on a separate sheet (or sheets) of three-hole punched paper.

Problem Set 5 Solutions

Introduction to Algorithms Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson.

Lecture 3 February 9, 2010

CMSC 754 Computational Geometry 1

Computational Geometry

Computational Geometry. Lecture 17

Computational Geometry

Data Structures and Algorithms CMPSC 465

Algorithms for GIS:! Quadtrees

Problem Set 4 Solutions

Range Searching and Windowing

1 The range query problem

Final Exam in Algorithms and Data Structures 1 (1DL210)

Orthogonal range searching. Orthogonal range search

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2010

Range Searching II: Windowing Queries

Computer Graphics Prof. Sukhendu Das Dept. of Computer Science and Engineering Indian Institute of Technology, Madras Lecture - 24 Solid Modelling

Problem Set 5 Solutions

Orthogonal line segment intersection. Computational Geometry [csci 3250] Laura Toma Bowdoin College

Polygon Partitioning. Lecture03

Pick up some wrapping paper.

4. If you are prompted to enable hardware acceleration to improve performance, click

Commando: Solution. Solution 3 O(n): Consider two decisions i<j, we choose i instead of j if and only if : A S j S i

Lecture 25 of 41. Spatial Sorting: Binary Space Partitioning Quadtrees & Octrees

Line segment intersection (I): Orthogonal line segment intersection. Computational Geometry [csci 3250] Laura Toma Bowdoin College

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

Describe Plane Shapes

Unit 1, Lesson 1: Moving in the Plane

Line segment intersection. Family of intersection problems

Approximation Algorithms for Geometric Intersection Graphs

SECTION 1.2 (e-book 2.3) Functions: Graphs & Properties

Multidimensional Divide and Conquer 2 Spatial Joins

Solutions to problem set 1

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Lesson 18: Slicing on an Angle

INSTRUCTIONS FOR THE USE OF THE SUPER RULE TM

Lecture 3: Art Gallery Problems and Polygon Triangulation

Lecture 2 February 4, 2010

Mathematics Curriculum

15.4 Longest common subsequence

Computational Geometry

Lecture 6: Analysis of Algorithms (CS )

Lesson 21: Surface Area

Using Genetic Algorithms to Solve the Box Stacking Problem

A C E. Applications. Applications Connections Extensions

Computational Geometry

Lecture 3 February 23, 2012

Firstly, I would like to thank ProFantasy for hosting this tutorial on the RPGMaps Blog. Thank you!

Quiz 1 Practice Problems

Lecture 3 February 20, 2007

3 Competitive Dynamic BSTs (January 31 and February 2)

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

Ready to Go On? Skills Intervention Building Blocks of Geometry

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Principles of Algorithm Design

January 10-12, NIT Surathkal Introduction to Graph and Geometric Algorithms

Lecture Summary CSC 263H. August 5, 2016

Grade Common Core Math

Applications of Suffix Tree

Algorithms and Data Structures

Intersection of an Oriented Box and a Cone

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Chapter 3. Graphs. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Comparison Based Sorting Algorithms. Algorithms and Data Structures: Lower Bounds for Sorting. Comparison Based Sorting Algorithms

form are graphed in Cartesian coordinates, and are graphed in Cartesian coordinates.

points are stationed arbitrarily close to one corner of the square ( n+1

Introduction to Algorithms February 24, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Handout 8

Math 462: Review questions

Describing Data: Frequency Tables, Frequency Distributions, and Graphic Presentation

Lecture 17: Solid Modeling.... a cubit on the one side, and a cubit on the other side Exodus 26:13

(Refer Slide Time: 00:02:02)

Geometric Algorithms. Geometric search: overview. 1D Range Search. 1D Range Search Implementations

An angle that has a measure less than a right angle.

Jana Kosecka. Linear Time Sorting, Median, Order Statistics. Many slides here are based on E. Demaine, D. Luebke slides

Introduction to Algorithms December 9, 2005 Massachusetts Institute of Technology Professors Erik D. Demaine and Charles E. Leiserson Handout 33

Multidimensional Indexes [14]

Lower Bound on Comparison-based Sorting

Ensures that no such path is more than twice as long as any other, so that the tree is approximately balanced

Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2005 Simonas Šaltenis E1-215b

Constructing Symmetrical Shapes

9/24/ Hash functions

Algorithms and Data Structures: Lower Bounds for Sorting. ADS: lect 7 slide 1

Number/Computation. addend Any number being added. digit Any one of the ten symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9

Orthogonal range searching. Range Trees. Orthogonal range searching. 1D range searching. CS Spring 2009

CS 373: Combinatorial Algorithms, Spring 1999

K-structure, Separating Chain, Gap Tree, and Layered DAG

Problem Set 6 Solutions

Graph Algorithms (part 3 of CSC 282),

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

3.5D Graphing Rational Functions

1.6 Classifying Polygons

Math 5320, 3/28/18 Worksheet 26: Ruler and compass constructions. 1. Use your ruler and compass to construct a line perpendicular to the line below:

Balanced Trees Part Two

heptagon; not regular; hexagon; not regular; quadrilateral; convex concave regular; convex

Midterm Exam Fundamentals of Computer Graphics (COMP 557) Thurs. Feb. 19, 2015 Professor Michael Langer

Unit 10 Study Guide: Plane Figures

Orthogonal Range Search and its Relatives

Transcription:

Introduction to Algorithms October 29, 2001 Massachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Handout 24 Exercise 6-1. Problem Set 6 Solutions Do exercise 14.1-5 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 6-2. Do exercise 14.2-2 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 6-3. Do exercise 14.3-1 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 LEFT-ROTATE 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 6-4. Do exercise 33.1-4 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(n 2 lg n). Exercise 6-5. Do exercise 33.2-1 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 = Θ(n 2 ) intersections. Problem 6-1. 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 y-axis), so that a representation of a rectangle consists of its minimum and maximum x- and y-coordinates.

2 Handout 24: Problem Set 6 Solutions (a) Give an O(n lg n)-time algorithm that decides whether a set of rectangles so represented contains two rectangles that overlap. Your algorithm need not report all intersecting 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 maintaining the set of rectangles intersected by the line in an interval tree. Details: First sort the x-coordinates of the rectangles. Scan the sorted x-coordinate list from lowest to highest. When an x-coordinate of a left edge is found, add the interval corresponding to the y-coordinates of that edge into the interval tree and check for overlap. When an x-coordinate of a right edge is found, delete the interval corresponding to the y-coordinates 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 x-coordinates, and 2n O(lg n) to maintain the interval tree, for a total running time of O(n lg n). Problem 6-2. GPS map display After graduating from MIT, you decide to join GiPSy, a startup that hopes to make money by selling an affordable hand-held GPS (Global Positioning System) device. The device picks up time-stamped messages from 4 geostationary satellites and uses them to calculate its precise coordinates 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 = (r x1, r y1 ), (r x2, r y2 ) is a line segment, either horizontal or vertical, specified by the coordinates of its endpoints, (r x1, r y1 ) and (r x2, r y2 ). Thus, for every road r = (r x1, r y1 ), (r x2, r y2 ), we have either r x1 = r x2 (vertical) or r y1 = r y2 (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 Texas St (x1, y1) Figure 1: A sketch of a GiPSy device. Washington St., Vegas St., and Albany St. are Type-2 roads. Cambridge St. and Texas St. are Type-1 roads. The view rectangle is determined by the coordinates of the lower-left and upper-right corners, as shown. A map M = {r 1, r 2,..., r n } is a set of n roads. A view rectangle V = (V x1, V y1 ), (V x2, V y2 ) specifies the rectangular region that should be displayed by giving the coordinates of the rectangle s lower-left and upper-right corners, (V x1, V y1 ) and (V x2, V y2 ) 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.

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 worst-case preprocessing time, worst-case query time, and worst-case space occupied 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 Type-1 roads. (Hint: Use a two-dimensional range tree.) Solution: Place the endpoints of the roads into a two-dimensional 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 2-D range trees: O(n lg n) preprocessing time, O(lg 2 n + k) query time, and O(n lg n) space. The rest of the problem is about finding Type-2 roads. Without loss of generality, we focus on finding horizontal Type-2 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 Type-1 or Type-2. (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 Type-2 horizontal roads. Solution: Part (a) identifies all of the Type-1 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 Type-2 horizontal roads, because every Type-2 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 characterization. A horizontal road r = (r x1, r y ), (r x2, r y ) straddles the view rectangle V = (V x1, V y1 ), (V x2, V y2 ) if it satisfies two properties: 1. r x1 V x1 r x2, 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. V y1 r y V y2, 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 5 (c) Draw a picture showing a view rectangle V and an example of each of the following kinds of visible horizontal roads: 1. straddling and Type-2, 2. straddling but not Type-2, 3. horizontally straddling but not vertically straddling, 4. vertically straddling but not horizontally straddling, and 5. neither horizontally straddling nor vertically straddling (but still visible). 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 one-dimensional range tree.) Solution: Place the endpoints of all horizontal roads in a 1-D range tree based on their y-coordinates. We can then use this range tree to find all the roads whose y-coordinates lie within the vertical extent of the view rectangle V, that is V y1 r y V y2. 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 candidate 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 worst-case 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 1-D range tree based on their y-coordinates, 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 1-D RANGE QUERY presented in lecture, which runs in O(lg n) time. (h) Give an efficient method for finding all the straddling horizontal roads. (Hint: Combine interval trees with range trees.) Solution: As we have seen above, the range tree based on the y-coordinates 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 pre-emptively construct an interval tree for each one of the nodes of the range tree. The interval tree associated with each node stores the x-coordinate 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 y-coordinate), 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 [V x1, V x2 ]). 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 7 Pre-processing 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 ( n n lg n + 2 2 lg n ) ( n + 4 2 4 lg n ) +... 0 = n [lg n + lg n 4 2 + lg n ] 4 +... + 0 = 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 lg 2 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 2-D 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 lg 2 n) time to build the interval trees, for a total of O(n lg 2 n) time for the pre-processing 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(lg 2 n+ K lg n) where K is the total number of visible roads. Thus the total running time for each query is O(lg 2 n + K lg n).