Geometric Data Structures
Download
Report
Transcript Geometric Data Structures
Geometric Data Structures
Reading: Chapter 10 of the Textbook
Driving Applications
– Windowing Queries
Related Application
– Query Image Database
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.
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(log2n + 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.
UNC Chapel Hill
M. C. Lin
Classification of Segments w.r.t. xmid
An interval [x:x’] contains l := (x=qx) iff x qx x’
We can classify a set of segments w.r.t to xmid :
– Imid: those containing xmid
– Ileft: those complete lie to the left & cannot intersect l
– Iright: those complete lie to the right & cannot intersect
l
Check Imid to find intersecting segments with l
– Store the intervals in 2 sorted lists: increasing left
endpoints and decreasing right endpoints
– qx 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. qx IM. C. Lin
UNC Chapel Hill
Interval Trees
If I = 0, then the interval tree is a leaf
Otherwise, let xmid be the median of the endpoints of the
intervals, let
Ileft := { [xj:xj’] I : xj’ < xmid },
Imid := { [xj:xj’] I : xj xmid xj’ },
Iright := { [xj:xj’] I : xmid < xj }.
The interval trees consists of a root node v storing xmid.
The set Imid is stored twice: once in a list Lleft(v) that is
stored on the left endpoints of the intervals, once in a list
Lright(v) that is stored on the right endpoints of intervals
The left subtree of v is an interval tree for Ileft
The right subtree of v is an interval tree for Iright
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.
3.
4.
5.
6.
7.
then return an empty leave
else Create a node v. Compute xmid, the median of the set of interval
endpoints, and store xmid with v.
Compute Imid and construct two sorted lists for Imid: a list Lleft(v)
sorted on left endpoint & a list Lright (v) sorted on right endpoint.
Store these two lists at v.
lc(v) ConstructIntervalTree(Ileft)
rc(v) ConstructIntervalTree(Iright)
return v
UNC Chapel Hill
M. C. Lin
QueryIntervalTree(v, qx)
Input: The root v of an interval tree and a query point qx
Output: All intervals that contain qx
1. if v is not a leaf
2. then if qx < xmid(v)
3.
then Walk along the list Lleft(v), starting at the
interval with the leftmost endpoint, reporting
all the intervals that contain qx. Stop as soon
as an interval does not contain qx.
4.
QueryIntervalTree(lc(v), qx)
5.
else Walk along the list Lright(v), starting at the
interval with the rightmost endpoint, reporting
all the intervals that contain qx. Stop as soon
as an interval does not contain qx.
6.
QueryIntervalTree(rc(v), qx)
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(log2n + 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(log2n + 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.
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
(-:qx] x [qy:qy’]. Use ideas from 1d range query.
Use a heap to answer 1d query if px (-:qx] 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
UNC Chapel Hill
M. C. Lin
Priority Search Trees
If P = 0, then the priority search tree is an empty leaf
Otherwise, let pmin be the point in P with the smallest xcoordinate and ymid be the y-median of the rest in P. Let
Pbelow := { p P\{pmin} : py < ymid },
Pabove := { p P\{pmin} : py > ymid }.
The priority search trees consists of a root node v where
the point p(v) := pmin and the value y(v) := ymin are stored.
– The left subtree of v is a priority search tree for Pbelow
– The right subtree of v is a priority search tree for Pabove
UNC Chapel Hill
M. C. Lin
ReportInSubTree(v, qx)
Input:The root v of a subtree of a priority search tree and a value qx
Output: All points in the subtree with x-coordinate at most qx
1. if v is not a leaf and (p(v))x qx
2.
3.
4.
then Report p(v)
ReportInSubTree(lc(v), qx)
ReportInSubTree(rc(v), qx)
UNC Chapel Hill
M. C. Lin
QueryPrioSearchTree(T,(-:qx]x[qy:qy’])
Input: A priority search tree and a range unbounded to the left
Output: All points lying in the range
1.
2.
3.
4.
5.
6.
7.
8.
9.
Search with qy and qy’ in T. Let vsplit be node where 2 search paths split
for each node v on the search path of qy or qy’
do if p(v) (-:qx]x[qy:qy’] then report p(v)
for each node v on the search path of qy in the left subtree of vsplit
do if the search path goes left at v
then ReportInSubtree(rc(v), qx)
for each node v on the search path of qy’ in the right subtree of vsplit
do if the search path goes right at v
then ReportInSubtree(lc(v), qx)
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 (-:qx] x [qy:qy’] in
O(k + log n) time, where k is the number of reported
points.
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)
UNC Chapel Hill
M. C. Lin
QuerySegmentTree(v, qx)
Input:The root v of a (subtree of a) segment tree and a query point qx
Output: All intervals in the tree containing qx
1. Report all the intervals in I(v)
2. if v is not a leaf
3.
4.
5.
then if qx Int(lc(v))
then QuerySegmentTree(lc(v), qx)
else QuerySegmentTree(rc(v), qx)
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)
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), qx)
5.
if Int(rc(v)) [x:x’] 0
6.
then InsertSegmentTree(rc(v), qx)
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(log2n+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.
UNC Chapel Hill
M. C. Lin