Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Computational Geometry Chapter 12. 2 Motivation Range Trees e.g. Database Database Record 1.Suppose you want to know all employees with Salary ∈ [40K,

Similar presentations


Presentation on theme: "1 Computational Geometry Chapter 12. 2 Motivation Range Trees e.g. Database Database Record 1.Suppose you want to know all employees with Salary ∈ [40K,"— Presentation transcript:

1 1 Computational Geometry Chapter 12

2 2 Motivation Range Trees e.g. Database Database Record 1.Suppose you want to know all employees with Salary ∈ [40K, 50K]  Scan records & pick out those in range *slow* age salary start date city address employee

3 3 Motivation e.g. Database Database Record 2.Suppose you want to know all employees with Salary ∈ [40K, 50K] AND Age ∈ [25, 40]  Scan records & check each one *slow* age salary start date city address employee

4 4 Motivation Ctnd. Alternative to Scan: Each employee is a point in space –age range[15, 75] –salary range[0K, 500K] –start date[1/1/1900, today] –city/address[College Station, Bryan, Austin, … ] 4D4D may just keep as data w/records could encode could use categories (of a row)

5 5 Motivation (cont) Orthogonal Range Query (Rectangular)  Want all points in the orthogonal range  Faster than linear scan if good data structures are used.  Want time O( f(n)+k ) ; where k= # of points reported 050K100K150K200K 15 25 35 45 … … age salary Query #2 (age 25-40, salary 40K-50K)

6 6 Data:Points P={p 1, p 2, … p n } in 1-D space (set of real numbers) Query: Which points are in 1-D query rectangle (in interval [x, x ’ ]) Data structure 1: Sorted Array A= Query:Search for x & x ’ in A by binary searchO(logn) Output all points between them. O(k) TotalO(logn+k) Update: Hard to insert points. Add point p ’, locate it n A by binary search. Shift elements in A to make room. O(n) on avarage Storage Cost: O(n) Construction Cost:O(nlogn) 1-D Range Searching 3 9 27 28 29 98 141 187 200 201 202 999

7 7 Evaluation of sorted array (cont) Update: hard to insert. Find point of insertion in log n (via binary search), but have to shift. Storage costs: O(n) Construction costs: - just sorting (n log n)

8 8 1-D Range Searching Ctnd. Data structure 2: Balanced Binary Search Tree Leaves store points in P (in order left to right) Internal nodes are splitting values. v.x used 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’)

9 9 1-D Range Searching Ctnd. Look for node V split 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] 49 893710 8023 62 319307059100 3105923193037627080100105 49 89

10 10 1-D Range Searching Ctnd. Update Cost O(logn) Storage Cost O(n) Construction Cost O(nlogn) 49 893710 8023 62 319307059100 3105923193037627080100105 49 89 u Search path u’ 1-D Range Tree

11 11 Input: range tree rooted at v and x ≤ x ’ Output: all points in range [x, x ’ ] if v is null return {} if (x < v.x < x ’ ) L = 1DRangeQuery(x,x ’,v.left) R = 1DRangeQuery(x,x ’,v.right) return (L+v + R) if v.x < x return 1DRangeQuery(x,x ’,v.right) return 1DRangeQuery(x,x ’,v.left) Algorithm: 1D Range Query(x, x ’,v)

12 12 Time complexity of 1D-Range Query –Balanced Binary Search Tree O(n) storage O(n log n) construction time –Query Time Time Spent – traversing root to μ + μ ’ paths – O(log n) time Time spent in ReportSubtree –Worst case theta Θ(n) Since may need to report all points if they fall in query range –Finer analysis gives total time in ReportSubtree is proportional to number of nodes reported  O(s) if report s node Total Time – O(s + log n) when s is number of nodes reported

13 13 - For each internal node v  T x 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 T y (v) (second level tree) on y- coordinate. (have pointer from v to T y (v)) Range trees 2D Queries TxTx v P( v ) Ty(v)Ty(v) p1p1 p2p2 p3p3 p4p4 p5p5 p6p6 p7p7 p1p1 p2p2 p3p3 p4p4 p5p5 p6p6 p7p7 v T4T4 p7p7 p5p5 p6p6 Ty(v)Ty(v)

14 14 Range trees Build 2D-Range Tree(P) input: a set of P points in the plane output: root of a 2D range tree 1.Construct 2 nd level tree T y for P (store entire points at leaves) 2.if (|P|=1) 3.then create leaf v. T y (v):= T y 4.else split P into P left and P right parts of equal size by x coordinate around x mid 5. v left :=Build2D-RangeTree(P left ) 6. v right :=Build2D-RangeTree(P right ) 7. create new v such that x v :=x mid Leftchild(v):=v left Righteftchild(v):=v left T y (v):=T y 8. return v end /* Build2D-RangeTree */

15 15 Range trees - revised 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 1.if (|P|=1) 2.then create leaf v. T y (v):= T y 3.else split P into P left and P right parts of equal size by x coordinate around x mid 4. (Vleft, Yleft):=Build2D-RangeTree(P left ) 5. (Vright, Yright):=Build2D-RangeTree(P right ) 6. create new v such that x v :=x mid Leftchild(v):=vleft t Righteftchild(v):=vright t Yall =merge(Yleft,Yright)); T y (v):=buildTree(Yall) 7. return (v, Yall) end /* Build2D-RangeTree */

16 16 Range trees A 2D-range tree with n points uses O(n log n) storage. Proof. Consider a point p  P. p is stored in T y ( v ) for every node v  T x such that p is a leaf of the subtree of T x rooted at v. There are O( log n) such subtrees  those rooted on root of ( T x ) to p path. ( T x has height O( log n))  Each point stored O( log n) times.  N points requires O(n log n) storage in total.

17 17 Construction Time for 2D Range Tree Naive implementation of step 1 takes O(n log n) time (unsorted points) But, if points already sorted by y-coordinate can build 1D binary search tree in  (n) time (bottom up)  pre-sort points by x- and y- coordinates (two lists)  build trees bottom up and merge sorted lists  construction of tree Ty takes  (n’) time (n’ = number points in Ty)  total construction time = O(n log n)

18 18 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) Lemma: A query w/ axis-parallel rectangle in range tree for n points takes O( log 2 n+ k) time, where k = # reported points

19 19 2dRangeSearch(x1,x2,y1,y2,root,type) -page 555 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

20 20 Proof spend O(log n) time searching 1 st level tree Tx for each 1D range query in a second level tree spend O(log n + ) time searching 2 nd level tree Ty(v) total time is (since log = O(log n) where summation is over all visited nodes v. (total # nodes reported) nodes v visited when searching Ty

21 21 Proof (Cont.)  total =

22 22 Priority Search Trees –These act as balanced binary search trees for the x coordinate and as max heaps for the y coordinates. –In such a tree, the root node stores the item with the largest y value, the median x coordinate of its tree (note that this isn’t the x coordinate of the item), and the left and right subtrees represent those items with x coordinate less than or greater than that median. –See Figure 12.5 (page 556) in which nodes are placed to represent their (x,y) values

23 23 Priority Search tree: placement of nodes represents (x,y) coordinates Dotted curves represent the median x coordinate of the subtree

24 24 –Such a tree is built in O(n log n) time, as we would expect for a balanced tree. –Searching involves a standard range search on x, except that we terminate early when the y value at a node is less than the minimum y we are interested in. –Searches are O(log n + s). –Alternative trees for downward, leftward, or rightward infinite regions are very similar

25 25 Priority Range Trees –What if have four sided range queries? –can convert a binary search tree keyed on x coordinates to a priority range tree by associating a priority search tree (3 sided) with each node. – Right children have associated priority search trees which are unbounded to the left. Left children have associated priority search trees which are unbounded to the right. In each case, the parent node serves as the missing bound, so the three sided search is sufficient. – requires O(n log n) space and time for construction. –This structure can answer two-dimensional queries in O(s + log n) time by noticing that a search target in a left child won’t go beyond the values in the right child, so it doesn’t matter that we seem to be searching an infinite range; and vice versa.

26 26 Quad and kD Trees –Quadtrees are used to store points in a plane in a way that regional searching becomes easy. Generally, non- rectangular regions are difficult to deal with, so we use the bounding box as a first approximation to the region. –use a 4-ary tree to represent quadrants, sub-quadrants, etc. The quadrants are ordered as in geometry –Since the depth of a quadtree is governed by the closeness of points, it can be very deep. Usually as a practical safeguard we give a maximum depth, D. The book doesn’t explain how to store multiple points that occur in the same maximally refined sub-sub- …quadrant.

27 27 –The major use of a quadtree is range searching, where the range is now a region of the plane, R. The algorithm recursively examines each sub-quadrant which intersects R, stopping when it arrives at external nodes. When R totally contains a sub-quadrant, we don’t employ any more logic on the sub-quadrant’s tree, but simply enumerate it. –When the quadtree is of bounded depth <= D, then both construction and range searching are O(Dn).

28 28 KD-Trees (Higher dimensional generalization of 1D-Range Tree.) idea:first split on x-coord (even levels) next split on y-coord (odd levels) repeat levels : store pts internal nodes : splitting lines (as opposed to values)

29 29 Algorithm : BuildKDtree (p, depth) Input: set of pts P + currrent depth output : root of KD-tree storing P 1.If (|p| = 1) 2. then return leaf storing p 3. else if (depth is even) // books method does not change direction 4. split p into 2 equal sets by vertical line l(p1+p2) 5. else 6. split P into equal sized p1+p2 by horizontal line l 7.Endif 8.Vleft := Buildkdtree (p1, depth +1) 9.Vright := Buildkdtree (p2, depth +1) 10.(new) v s.t lc(v) := Vleft rc(v) :=Vright 11.Return v Lc(v)=v’s left child Rc(v)=v’s right child

30 30 Build KD-Tree

31 31 Build KD-Tree

32 32 Build KD-Tree

33 33 Build KD-Tree

34 34 Build KD-Tree

35 35 Build KD-Tree

36 36 Build KD-Tree

37 37 Build KD-Tree

38 38 Build KD-Tree

39 39 Complexity Construction time Expensive operation: determining splitting line(median finding) -Can use linear time median finding algorithm (Quickselect) -Then total time is -but can obtain this time without fancy median finding  Presort points by x-coord and by y-coord (O(nlogn)) Each time find median in O(1) and partition lists and update x and y ordering by scan in O(n) time

40 40 Complexity Storage Number of leaves = n (one per point) Still binary tree  O(n) storage total Querys -each node corresponds to a region in plane -Need only search nodes whose region intersects query region -Report all points in subtrees whose regions contained in query range -When reach leaf, check if point in query region

41 41 Algorithm: Search KD-Tree (v, R) Input: root of a subtree of a KD-tree and a range R Output: All points at leaves below v that lie in the range 1.If (v = leaf) 2. then report v ’ s point if in R 3. else if (region (lc(v)) fully contained in R) 4. then ReportSubtree (Rc(v)) 5. else if (region (lc(v)) intersects R) 6. then SearchKdTree(lc(v), R) 7. if (region(rc(v)) fully contained in R) 8. then ReportSubtree(rc(v)) 9. else if (region(rc(v)) intersects R) 10. then SearchKdtree(rc(v), R) 11.Endif Note: need to know region(v) - can precompute and store - Computer during recursive calls, e.g., L(v) is v’s splitting line and is left halfpland of l(v)

42 42 Query time Lemma 5.4 A query with an axis parallel rectangle in a Kd-tree storing n points can be performed in O(  ¯+k) time where k is the number of reported points. Proof. Total time for reporting points in Report Subtree is O(k). So need to bound number of nodes visited by query algorithm that are not in traversed subtree. For each such node v, region(v) intersects but is not contained in R. n

43 43 Query time  To bound number of such nodes we bound number of regions intersected by vertical line. (gives bound on number of regions intersected by left and right edges) (bound on number of intersected by top and bottom edges of R is similar) Let l be vertical line and Let l(root(t)) be root’s splitting line.

44 44 Query time  l intersects region to right or left of l(root(t)) but not both. Q(n) is equal to the number of in n point Kd- tree whose root contains vertical splitting line. This is important since if include horizontal nodes then don’t get reduction.  Go down 2 levels before counting –l intersects 2 of 4 regions at this level –Each contains  n/4 points Q(1)=O(n) Q(n)=2+2·Q(n/4)=O(  ¯). × n 

45 45 Query time Note: Analysis is probably pessimistic… Bounded on the number of regions intersecting an edge of the query rectangle by the number of regions intersecting line through its edge.  If range is small, so will be the edge and it won’t intersect this many.

46 46 Nearest Neighbor We can use KD trees to find the nearest neighbor (see page 564)

47 47 Orthogonal Segment Intersection Plane Sweep –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(n 2 ) 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.

48 48 There are three kinds of objects and event processing steps: –Left endpoint of horizontal segment, add the segment to the range- search dictionary –Left endpoint of horizontal segment, remove segment from the range-search dictionary –Vertical segment, search down the dictionary for intersecting horizontal segments. –We first have to sort the objects from left to right, O(n log n)

49 49 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’) –So overall, this is an O(n log n + s) operation.

50 50 Closest Pairs make sure that no two objects are too close to each other. W e 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(n 2 ) How can we improve? Can we determine which pairs are interesting?

51 51 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

52 52 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).

53 53 In fact, there are only at most six points on one side of the strip that could be less than d apart. 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

54 54 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 lookingto the next improvement when the points don't come back in the same order.

55 55 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 log 2 n) 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).

56 56 Sweeping to Find Closest Pairs –order the points by x coordinate from left to right, and as the sweep line crosses a point, check back to the left to see if any points are closer than the current minimum distance. We use our list ordered by x to locate these points. –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.

57 57 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).

58 58 Convex Hull obstacle start end

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

60 60 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

61 61 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

62 62 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

63 63 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

64 64 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  (a, b, c), whose absolute value is twice the area of the triangle with vertices a, b and c a b c a c b c b a CW - CCW + COLL 0

65 65 Orientation QUIZ The orientation of three points is characterized by the sign of the determinant  (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. a b c a c b c b a clockwise counter clockwize colinear

66 66 x b y c –y b x c – (x a y c -y a x c ) + x a y b -y a xb

67 67 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 a b c CCW a c b CW a b c COLL

68 68 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 –q nonconvex  orientation(p, q, r)  CW or COLL p q r q r p

69 69 Graham Scan 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 while orientation(p, q, r)  CW or COLL remove q from H q  p p  vertex preceding p in H Add r to the end of H p q r H p q r H p q r H

70 70 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

71 71 Incremental Convex Hull q w u e zt

72 72 Point Location 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, T L and T H 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 P THTH TLTL

73 73 Point Location (cont.) To perform locate(q), we search for x(q) in T L and T H to find –Edge e L or vertex v L on the lower chain of P whose horizontal span includes x(q) –Edge e H or vertex v H 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 e L (v L ) or on e H (v H ), we return ON –Else if q is above e L (v L ) and below e H (v H ), we return IN –Else, we return OUT P THTH TLTL q eHeH vLvL

74 74 Incremental Convex Hull 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 T L for the lower hull T H for the upper hull

75 75 Insertion of a Point In operation insert(q), we consider four cases that depend on the location of point q AIN or ON: no change BOUT and above: add q to the upper hull COUT and below: add q to the lower hull DOUT and left or right: add q to the lower and upper hull A C D

76 76 Insertion of a Point (cont.) Algorithm to add a vertex q to the upper hull chain in Case B (boundary conditions omitted for simplicity) –We find the edge e (vertex v) whose horizontal span includes q –w  left endpoint (neighbor) of e (v) –z  left neighbor of w –While orientation(q, w, z)  CW or COLL We remove vertex w w  z z  left neighbor of w –u  right endpoint (neighbor) of e (v) –t  right neighbor of u –While orientation(t, u, q)  CW or COLL We remove vertex u u  t t  right neighbor of u –We add vertex q q w u e zt q w u z t

77 77 Analysis Let n be the current size of the convex hull –Operation locate takes O(log n) time –Operation insert takes O((1  k)log n) time, where k is the number of vertices removed –Operation hull takes O(n) time –The amortized running time of operation insert is O(log n)


Download ppt "1 Computational Geometry Chapter 12. 2 Motivation Range Trees e.g. Database Database Record 1.Suppose you want to know all employees with Salary ∈ [40K,"

Similar presentations


Ads by Google