Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNC Chapel Hill M. C. Lin Geometric Data Structures Reading: Chapter 10 of the Textbook Driving Applications –Windowing Queries Related Application –Query.

Similar presentations


Presentation on theme: "UNC Chapel Hill M. C. Lin Geometric Data Structures Reading: Chapter 10 of the Textbook Driving Applications –Windowing Queries Related Application –Query."— Presentation transcript:

1 UNC Chapel Hill M. C. Lin Geometric Data Structures Reading: Chapter 10 of the Textbook Driving Applications –Windowing Queries Related Application –Query Image Database

2 UNC Chapel Hill M. C. Lin Windowing Queries Given a rectangular region (2D or 3D), or a window, the algorithm must determine the part of the database that lie in the window and report them. Data are normally not points, but line segments, polygons, etc. 2D Windowing: navigation using a map or an electronic GIS; circuit-board layout, etc. 3D Windowing: view frustrum culling; viewing a complex CAD/CAM model by zooming in to a small portion, etc.

3 UNC Chapel Hill M. C. Lin Orthogonal Window Queries Let S be a set of n axis-parallel line segments. A query asks for the segments intersecting a 2D query window, W := [ x:x’ ] x [ y:y’ ] Most cases, the segment has at least one point inside of W. We can find such segments by performing a range query with W in the set of 2n endpoints of the segments in S, by using a 2D range tree T. 2D range tree can answer a range query in O(log 2 n + k) time; query time can be improved to O(logn + k) by fractional cascading. Find segments that intersect W twice either at the left and right edges or top and bottom edges. This reduces to finding horiz. (v) segments intersecting a vert. (h) line.

4 UNC Chapel Hill M. C. Lin Classification of Segments w.r.t. x mid An interval [ x:x’ ] contains l := (x=q x ) iff x  q x  x’ We can classify a set of segments w.r.t to x mid : –I mid : those containing x mid –I left : those complete lie to the left & cannot intersect l –I right : those complete lie to the right & cannot intersect l Check I mid to find intersecting segments with l –Store the intervals in 2 sorted lists: increasing left endpoints and decreasing right endpoints –q x can be contained in an interval if it’s also contained in all its predecessors in the sorted list –We can simply walk along sorted lists reporting intervals & stop when we encounter I s.t. q x  I

5 UNC Chapel Hill M. C. Lin Interval Trees If I = 0, then the interval tree is a leaf Otherwise, let x mid be the median of the endpoints of the intervals, let I left := { [x j :x j ’]  I : x j ’ < x mid }, I mid := { [x j :x j ’]  I : x j  x mid  x j ’ }, I right := { [x j :x j ’]  I : x mid < x j }. The interval trees consists of a root node v storing x mid. The set I mid is stored twice: once in a list L left (v) that is stored on the left endpoints of the intervals, once in a list L right (v) that is stored on the right endpoints of intervals The left subtree of v is an interval tree for I left The right subtree of v is an interval tree for I right

6 UNC Chapel Hill M. C. Lin ConstructIntervalTree(I) Input: A set I of intervals on the real line. Output: The root of an interval tree for I. 1. if I = 0 2. then return an empty leave 3. else Create a node v. Compute x mid, the median of the set of interval endpoints, and store x mid with v. 4. Compute I mid and construct two sorted lists for I mid : a list L left (v) sorted on left endpoint & a list L r ight (v) sorted on right endpoint. Store these two lists at v. 5. lc(v)  ConstructInterval Tree(I left ) 6. rc(v)  ConstructIntervalTree (I right ) 7. return v

7 UNC Chapel Hill M. C. Lin QueryIntervalTree(v, q x ) Input: The root v of an interval tree and a query point q x Output: All intervals that contain q x 1. if v is not a leaf 2. then if q x < x mid (v) 3. then Walk along the list L left (v), starting at the interval with the leftmost endpoint, reporting all the intervals that contain q x. Stop as soon as an interval does not contain q x. 4. QueryIntervalTree( lc ( v ), q x ) 5. else Walk along the list L right (v), starting at the interval with the rightmost endpoint, reporting all the intervals that contain q x. Stop as soon as an interval does not contain q x. 6. QueryIntervalTree( rc ( v ), q x )

8 UNC Chapel Hill M. C. Lin Algorithm Analysis An interval tree for a set I of n intervals uses O(n) storage, has depth O(logn) and can be built in O(n log n) time. Using the interval tree we can report all intervals containing a query point in O(k + log n) time, where k is no. of reported intervals. Let S be a set of n horizontal segments in the plane. The segments intersecting a vertical query segment can be reported in O(log 2 n + k) time with a data structure of O(n log n) storage, where k is the number of reported segments. The structure can be built in O(n log n) time. Let S be a set of n axis-parallel segments in the plane. The segments intersecting an axis-parallel rectangular query window can be reported in O(log 2 n + k) time with a data structure of O(n log n) storage, where k is number of reported segments. The structure can be built in O(n log n) time.

9 UNC Chapel Hill M. C. Lin Motivation for Priority Search Trees Replacing RangeTrees: A 2d rectangle query on P asks for points in P lying inside a query window (-  :q x ] x [ q y :q y ’ ]. Use ideas from 1d range query. Use a heap to answer 1d query if p x  (-  :q x ] and partition using the y-coordinates. –Root of the tree stores the point with minimum x -value and the remainder is partitioned into 2 equal sets. –The 2 sets are partitioned into sets above & below –Construct the tree recursively

10 UNC Chapel Hill M. C. Lin Priority Search Trees If P = 0, then the priority search tree is an empty leaf Otherwise, let p min be the point in P with the smallest x- coordinate and y mid be the y-median of the rest in P. Let P below := { p  P\{p min } : p y < y mid }, P above := { p  P\{p min } : p y > y mid }. The priority search trees consists of a root node v where the point p(v) := p min and the value y(v) := y min are stored. –The left subtree of v is a priority search tree for P below –The right subtree of v is a priority search tree for P above

11 UNC Chapel Hill M. C. Lin ReportInSubTree(v, q x ) Input: The root v of a subtree of a priority search tree and a value q x Output: All points in the subtree with x -coordinate at most q x 1. if v is not a leaf and (p(v)) x  q x 2. then Report p(v) 3. ReportInSubTree( lc(v), q x ) 4. ReportInSubTree( rc(v), q x )

12 UNC Chapel Hill M. C. Lin QueryPrioSearchTree(T, (-  :q x ]x[q y :q y ’] ) Input: A priority search tree and a range unbounded to the left Output: All points lying in the range 1. Search with q y and q y ’ in T. Let v split be node where 2 search paths split 2. for each node v on the search path of q y or q y ’ 3. do if p(v)  (-  :q x ]x[q y :q y ’] then report p(v) 4. for each node v on the search path of q y in the left subtree of v split 5. do if the search path goes left at v 6. then ReportInSubtree( rc(v), q x ) 7. for each node v on the search path of q y ’ in the right subtree of v split 8. do if the search path goes right at v 9. then ReportInSubtree( lc(v), q x )

13 UNC Chapel Hill M. C. Lin Algorithm Analysis A priority search tree for a set P of n points in the plane uses O(n) storage & can be built in O(n log n) time. Using the priority search tree we can report all points in a query range of the form (-  :q x ] x [ q y :q y ’ ] in O(k + log n) time, where k is the number of reported points.

14 UNC Chapel Hill M. C. Lin Segment Tree Data Structures The skeleton of the segment tree is a balanced binary tree T. The leaves of T correspond to the elementary intervals induced by the endpoints of the intervals in I in an ordered way: the leftmost leaf corresponds to the leftmost elementary interval, and so on. The elementary interval corresponding to leaf u is denoted Int( u ). The internal nodes of correspond to intervals that are the union of elementary intervals: the interval Int( v ) corresponding to node v is the union of the elementary intervals Int( u ) of the leaves in the subtree rooted at v. (implying Int( v ) is the union of its two children) Each node or leaf v in T stores the interval Int( v ) and a set I(v)  I of intervals (e.g. in a linked list). This canonical subset of node v contains the intervals [ x:x’ ]  I s.t. Int( v )  [ x:x’ ] & Int( parent ( v ))  [ x:x’ ] (See the diagrams in class)

15 UNC Chapel Hill M. C. Lin QuerySegmentTree(v, q x ) Input: The root v of a (subtree of a) segment tree and a query point q x Output: All intervals in the tree containing q x 1. Report all the intervals in I(v) 2. if v is not a leaf 3. then if q x  Int( lc(v)) 4. then QuerySegmentTree( lc(v), q x ) 5. else QuerySegmentTree( rc(v), q x )

16 UNC Chapel Hill M. C. Lin Constructing Segment Trees Sort the endpoints on the intervals in I in O(n log n) time. This gives elementary intervals. Construct a balanced binary tree on the elementary intervals & determine for each node v of the tree the interval Int(v) it represents. Compute the canonical subsets by inserting one interval at a time into T. (See the next procedure)

17 UNC Chapel Hill M. C. Lin InsertSegmentTree(v, [x:x’] ) Input: The root of a (subtree of a) segment tree and an interval Output: The interval will be stored in the subtree 1. if Int(v)  [x:x’] 2. then store [x:x’] at v 3. else if Int(lc(v))  [x:x’]  0 4. then InsertSegmentTree( lc ( v ), q x ) 5. if Int(rc(v))  [x:x’]  0 6. then InsertSegmentTree( rc ( v ), q x )

18 UNC Chapel Hill M. C. Lin Algorithm Analysis A segment tree for a set I of n intervals uses O(n log n) storage and can be built in O(n log n). Using the segment tree we can report all intervals that contain a query point in O(log n + k) time, where k is the number of reported intervals. Let S be a set of n segments in the plane with disjoint interiors. The segments intersecting an axis-parallel rectangular query window can be reported in O(log 2 n+k) time with a data structure that uses O(n log n) storage, where k is the number of reported segments. The structure can be built in O(n log n) time.


Download ppt "UNC Chapel Hill M. C. Lin Geometric Data Structures Reading: Chapter 10 of the Textbook Driving Applications –Windowing Queries Related Application –Query."

Similar presentations


Ads by Google