1 / 52

Computational Geometry Chapter 12

Computational Geometry Chapter 12. Range queries. How do you efficiently find points that are inside of a rectangle? Orthogonal range query ([ x 1 , x 2 ], [ y 1 ,y 2 ]): find all points ( x, y ) such that x 1 <x<x 2 and y 1 <y<y 2 Useful also as a multi-attribute database query. y. y 2.

truong
Download Presentation

Computational Geometry Chapter 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computational Geometry Chapter 12

  2. Range queries • How do you efficiently find points that are inside of a rectangle? • Orthogonal range query ([x1, x2], [y1,y2]): find all points (x, y) such that x1<x<x2 and y1<y<y2 • Useful also as a multi-attribute database query y y2 y1 x x1 x2

  3. Preprocessing • How much time such a query would take? • Rules of the game: • We preprocess the data into a data structure • Then, we perform queries and updates on the data structure • Analysis: • Preprocessing time • Efficiency of queries (and updates) • The size of the structure • Assumption: no two points have the same x- coordinate (the same is true for y-coordinate).

  4. T Search path for x2 Search path for x1 … b1 q1 a1 … b2 q2 a2 … Total order of data points 1D range query • How do we do a 1D range query [x1, x2]? • Balanced BSTwhere all data points are stored in the leaves • The size of it? • Where do we find the answer to a query?

  5. 1D range query • How do we find all these leaf nodes? • A possibility: have a linked list of leaves and traverse from q1 to q2 • but, will not work for more dimensions… • Sketch of the algorithm: • Find the split node • Continue searching for x1, report all right-subtrees • Continue searching for x2, report all left-subtrees • When leaves q1 and q2 are reached, check if they belong to the range • Why is this correct?

  6. Analysis of 1D range query • What is the worst-case running time of a query? • It is output-sensitive: two traversals down the tree plus the O(k), where k is the number of reported data points: O(log n + k) • Note, how we worry that the time is not a function of the parameters. This was also the case with pseudo-polynomial (where the runtime was a function of some extra parameter, not typically thought of as part of problem size. Amortized analysis performance guaranteed over time, but a single operation could be more. • What is the time of construction? • Sort, construct by dividing into two, creating the root and conquering the two parts recursively • O(n log n) • Size: O(n)

  7. 1-D Range Searching Balanced Binary Search Tree with data at leaves • Leaves store points in P (in order left to right) • Internal nodes are splitting values. v.xused to guide search. • Left sub tree of V contains all values ≤ v.x • Right sub tree of V contains all values > v.x • Query: [x, x’] • Locate x & x’ in T (search ends at leaves u & u’) • Points we want are located in leaves • In between u & u’ • Possibly in u (if x=u.x) • Possibly in u’ (if x’=u’.x) Leaves of sub trees rooted at nodes V s.t. parent (v) is on search path root to u (or root to u’)

  8. 49 23 80 62 10 37 89 3 19 30 49 59 70 100 89 3 10 19 23 30 37 59 62 70 80 100 105 1-D Range Searching (continued) • Look for node Vsplit where search paths for x & x’ split • Report all values in right sub tree on search path for x’ • Report all values in left sub tree on search path for x • Query: [18:77]

  9. 49 23 80 62 10 37 89 3 19 30 49 59 70 100 89 3 10 19 23 30 37 59 62 70 80 100 105 1-D Range Searching Ctnd. • Find Cost O(logn) • Storage Cost O(n) • Construction Cost O(nlogn) • Can link leaves together. • Time to do search is proportional to number of items found 1-D Range Tree Search path u u’ Idea: If parents knew all descendants, wouldn’t need to link leaves.

  10. 2D Range Search • What if we have a 2D range search? • We often store the data several ways in order to facilitate several types of inquires. For example, we might store a graph as both an adjacency list and an adjacency matrix to get the benefits of both. • We could store the range data two ways, by x and by y, but that wouldn’t really help as we need y within x.

  11. Tx Ty(v) T4 p6 p5 p4 p3 p1 p2 p7 Ty(v) v v p7 p5 p6 p1 p2 p3 p4 p5 p6 p7 P(v) P(v) Range trees 2D Queries - For each internal node vTx let P(v) be set of points stored in leaves of subtree rooted at v.  Set P(v) is stored with v as another balanced binary search tree Ty(v) (descendants by y) on y-coordinate. (have pointer from v to Ty(v))

  12. Tx Ty(v) T4 p6 p5 p4 p3 p1 p2 p7 Ty(v) v v p7 p5 p6 p1 p2 p3 p4 p5 p6 p7 P(v) P(v) Range trees 2D Queries - The diagram below shows what is stored at one node. Show what is stored at EVERY node. Note that data is only stored at the leaves.

  13. How does this help? We can divide up x by main key. How get data by second level key? X1 X2 X2 X1

  14. Range trees – Creation Returns list of sorted y components for efficiency in creation. Build 2D-Range Tree(P) input: a set of P points in the plane, sorted by x output: root of a 2D range tree, set of points sorted by y • if (|P|=1) • then create leaf v. v.tree:= v • else split P into Pleft and Pright parts of equal size by x coordinate around xmid • (Vleft, Yleft):=Build2D-RangeTree(Pleft) • (Vright, Yright):=Build2D-RangeTree(Pright) Yall =merge(Yleft,Yright)); Ytree:=buildTree(Yall) 6. v = new node(xmid,, Vleft, Vright) 7. return (v, Yall) end /* Build2D-RangeTree */ What is the running time of this? O(n log n) to sort At each recursive call: n work, two calls, each half as big; O(n log n)

  15. Range trees Each node is stored at each level – so log n copies of each. A 2D-range tree with n points uses O(nlogn) storage.

  16. Queries in 2D Range Trees • first determine O(log n) sub-trees to search (those w/ x-coord in range, don’t visit kids of internal node) • search each sub-tree Ty for points in y-coord range. both above steps use 1D search algorithm.  so alg identical to 1D Range Query (on x-coords) except replace calls to Report Subtree by 1D Range Query (on y-coords) Complexity: A query with axis-parallel rectangle in range tree for n points takes O( log2 n+ k) time, where k = # reported points

  17. list 2dRangeSearch(x1,x2,y1,y2,root,type) {// returns set of nodes in subtree rooted at root between [x1 x2] in x and [y1 y2] in y // type indicates boundary. So for left boundary, all nodes to right are within x range. if root == null return; M=L=R= 0; if x1 < root.x < x2{ if y1 < root.y <y2 M = root if type = left L = 2dRangeSearch(x1,x2,y1,y2,root.left,left) R = 1dRangeSearch(y1,y2, root.right) else if type = right L = 1dRangeSearch(y1,y2,root.left) R = 2dRangeSearch(x1,x2,y1,y2, root.right,right) else L=2dRangeSearch(x1,x2,y1,y2,root.left, left) R=2dRangeSearch(x1,x2,y1,y2,root.right, right) } else if root.x < x1 then R = 2dRangeSearch(x1,c2,y1,y2,root.right, type) else L = 2dRangeSearch(s1,s2,y1,y2,root.left, type) return L+M+R }

  18. Skip sections12.2 and 12.3

  19. 12.4 Problem: number of line segments which cross • Plane Sweep: think about moving across plane, “sweeping” up points Orthogonal Segment Intersection • We can imagine a set of horizontal line segments and vertical line segments scattered in the plane. We may want to find out which segments intersect each other. In a brute-force method we could search all pairs in O(n2) time. • But it would be nice to have an algorithm which is proportional in time to the actual number of intersections, since most often these segments won’t intersect anything. • While we imagine the vertical line sweeping in a continuous fashion, in fact we only need to jump from encountering one object to encountering the next. We can think of these encounters as events which interrupt the continuous sweep for some event processing.

  20. There are three kinds of objects and event processing steps: • We first have to sort the objects from left to right in terms of x-value, O(n log n). Notice this determines the order we add them to the tree (but the key in the tree is y NOT x) • Left endpoint of horizontal segment, add the segment to the 1Drange-search dictionary. The y-value is the key. • Right endpoint of horizontal segment, remove segment from the range-search dictionary • Vertical segment, search the dictionary for range of y values represented by the vertical segment. We find intersecting horizontal segments.

  21. For each object, we either: • Add something to a range tree – O(log n) • Remove something from a range tree O(log n) • Search a range tree – O(log n + s’) s’ is number of intersections with one line repeat search for each of n vertical lines • So overall, this is an O(n log n + s) operation.

  22. Closest Pairs • make sure that no two objects are too close to each other. • We may have a lot of points scattered over a plane, and want to find the two points which are closest to one another. For instance, a point might be the locus of a moving part in a machine which shouldn’t touch any other, or parts of a circuit which might “leak” voltage if they are too close. • Problem: Given a set of n points in the plane, find a pair of closest points.Brute force: Compute the distance between every pair. O(n2)How can we improve? Can we determine which pairs are interesting?

  23. Divide and Conquer Approach • Since we need to combine the information of the two sets, we have to be concerned with distances between points of different sets

  24. Approach • Sort the points by x-coordinate. • Divide in half by a vertical line which bisects the set. • Find the distances in the two sets • We now need to consider the distances between points of different sets. We only need to consider those in a strip 2d in width along the bisection line. • For any given point, there are a small number of points on the other side which can be less than d apart. This is because in each strip points are at least d apart (as it was the minimum).

  25. In fact, there are only at most six points on one side of the strip that could be less than d apart. • (d is returned from recursive calls) • Sort the points in the strip by the y coordinate. • We check each point with a constant number of its neighbors.The text says there are at most 6 neighbors

  26. Complexity • Sorting the x coordinates (done only once) O(n log n) • Solve two subproblems of size n/2 • Select just those items within the strip O(n). It would be faster than this is we relied on the sorted order (by x), but they are looking to the next improvement when the points don't come back in the same order.

  27. Sort those items in the strip: O(n log n) (as we can't be sure we have eliminated any of them) • Scan those points and compare with constant number of neighbors:O(n) • T(n) = 2T(n/2) + O(n log n) = O(n log2n) • Improvement: Instead of having to sort the points in the strip, output the points (from the recursive steps) in sorted order by y coordinate. • Since the merging is O(n) instead of O(n log n), the final complexity is O(n log n).

  28. Another approach:Sweeping to Find Closest Pairs • order the points by x, and as the sweep line crosses a point, check only those points (to the left) which are closer (in x value) than the current minimum distance. We need a “candidate points” list ordered by y. • Two sweep lines – one to add points and one to remove points. The removal line trails behind. The amount of lag depends on the current closest distance. • This requires us to keep track of the current minimum distance, the current closest pair of points, and y range tree of all of the points within that minimum horizontal distance of the line. • Complexity O( nlog n) to sort, plus O(log n) to add or delete, done n times.

  29. The events are: • Add a point to the tree as we encounter it • Remove a point from the tree as the line shifts to the next event and the distance is greater than d Encounter a point and check the potential close points which, to a first approximation, are those which are in the range tree, within a vertical distance of d from the event point. • There will only be at most 6 such points that we actually select. (Recall, for this algorithm the time depends on the number of things selected.) We have to examine each one to see whether it is actually at a distance closer than d. • Since s’ here is the constant 6, the sweep is O(n log n).

  30. obstacle start end Convex Hull

  31. Convex Polygon • A convex polygon is a nonintersecting polygon whose internal angles are all convex (i.e., less than p) • In a convex polygon, a segment joining two vertices of the polygon lies entirely inside the polygon convex nonconvex

  32. Convex Hull • The convex hull of a set of points is the smallest convex polygon containing the points • Think of a rubber band snapping around the points

  33. Special Cases • The convex hull is a segment • Two points • All the points are collinear • The convex hull is a point • there is one point • All the points are coincident

  34. Applications • Motion planning • Find an optimal route that avoids obstacles for a robot • Geometric algorithms • Convex hull is like a two-dimensional sorting obstacle start end

  35. b c a c b a Orientation • The orientation of three points in the plane is clockwise, counterclockwise, or collinear • orientation(a, b, c) • clockwise (CW, right turn) NEGATIVE • counterclockwise (CCW, left turn) POSITIVE • collinear (COLL, no turn) ZERO • The orientation of three points is characterized by the sign of the determinant D(a, b, c), whose absolute value is twice the area of the triangle with vertices a, b and c CW - c CCW + b a COLL 0

  36. b c a c b a Point Orientation QUIZ clockwise • The orientation of three points is characterized by the sign of the determinant D(a, b, c), whose absolute value is twice the area of the triangle with vertices a, b and c • QUIZ: By experimentation, explain what the various signs of the determinant mean. counter clockwize c b a colinear

  37. xbyc –ybxc – (xayc-yaxc) + xayb-yaxb

  38. Sorting by Angle • Computing angles from coordinates is complex and leads to numerical inaccuracy • We can sort a set of points by angle with respect to the anchor point a using a comparator based on the orientation function • b<c orientation(a, b, c) = CCW • b=c orientation(a, b, c) = COLL • b>c orientation(a, b, c) = CW CCW CW COLL c b c b b c a a a

  39. Convex Hull: Gift Wrapping • View the points as pegs. Tie rope around anchor (smallest x, and smallest y if tie) • Pull rope to right and rotate it counterclockwise until it touches another peg • Continue rotating until the rope gets back to the anchor point

  40. Wrapping step • From a given point, we want to find the next point to include which makes a left turn with every other point. • Like finding the “smallest” – just compare the best point so far. If you find a better, replace the best so far and continue.

  41. Complexity of Gift Wrapping? • For each point in hull, have to examine all remaining points. O(hn) where h is the number of points in the hull. • output sensitive – depends on size of output. • O(n2) worst case

  42. Computing the Convex Hull • The following method computes the convex hull of a set of points Phase 1: Find the lowest point (anchor point) Phase 2: Form a nonintersecting polygon by sorting the points counterclockwise around the anchor point Phase 3: While the polygon has a nonconvex vertex, remove it

  43. Removing Nonconvex Vertices • Testing whether a vertex is convex can be done using the orientation function • Let p, q and r be three consecutive vertices of a polygon, in counterclockwise order • q convex orientation(p, q, r) = CCW • qnonconvex orientation(p, q, r) = CW or COLL r r q q p p

  44. The Graham scan is a systematic procedure for removing nonconvex vertices from a polygon The polygon is traversed counterclockwise and a sequence H of vertices is maintained for each vertex r of the polygon Let q and p be the last and second last vertex of H whileorientation(p, q, r) = CW or COLLremove q from Hq  pp  vertex preceding p in H Add r to the end of H r r p r q p q p q H H H Graham Scan

  45. Analysis • Computing the convex hull of a set of points takes O(n log n) time • Finding the anchor point takes O(n) time • Sorting the points counterclockwise around the anchor point takes O(n log n) time • Use the orientation comparator and any sorting algorithm that runs in O(n log n) time (e.g., heap-sort or merge-sort) • The Graham scan takes O(n) time • Each point is inserted once in sequence H • Each vertex is removed at most once from sequence H

  46. q e w u z t Incremental Convex Hull(not covered in class)

  47. Given a convex polygon P, a point location query locate(q) determines whether a query point q is inside (IN), outside (OUT), or on the boundary (ON) of P An efficient data structure for point location stores the top and bottom chains of P in two binary search trees, TL and TH of logarithmic height An internal node stores a pair (x (v), v) where v is a vertex and x (v)is its x-coordinate An external node represents an edge or an empty half-plane Point Location TH P TL

  48. Point Location (cont.) TH • To perform locate(q), we search for x(q) in TLand TH to find • Edge eLor vertex vL on the lower chain of P whose horizontal span includes x(q) • Edge eHor vertex vH on the upper chain of P whose horizontal span includes x(q) • We consider four cases • If no such edges/vertices exist, we return OUT • Else if q is on eL(vL) or on eH(vH), we return ON • Else if q is above eL(vL) and below eH(vH), we return IN • Else, we return OUT eH P q vL TL

  49. The incremental convex hull problem consists of performing a series of the following operations on a set S of points locate(q): determines if query point q is inside, outside or on the convex hull of S insert(q): inserts a new point q into S hull(): returns the convex hull of S Incremental convex hull data structure We store the points of the convex hull and discard the other points We store the hull points in two red-black trees TLfor the lower hull THfor the upper hull Incremental Convex Hull

  50. In operation insert(q), we consider four cases that depend on the location of point q A IN or ON: no change B OUT and above: add q to the upper hull C OUT and below: add q to the lower hull D OUT and left or right: add q to the lower and upper hull Insertion of a Point A C D

More Related