1. Fast Retrieval of Subparts - Windowing Queries. Windowing queries vs range queries. 2. Interval Trees

1. Fast Retrieval of Subparts Windowing Queries D7013E! Lecture 10! Project! Geometric ! Data Structures II! Interval ! Trees! Efficient windowing qu...
Author: Abigail Cobb
8 downloads 1 Views 477KB Size
1. Fast Retrieval of Subparts Windowing Queries

D7013E! Lecture 10! Project! Geometric ! Data Structures II!

Interval ! Trees!

Efficient windowing queries!

Priority Search! Trees!

Segment! Trees!

D7013E Computational Geometry - Håkan Jonsson!

1!

Windowing queries vs range queries

D7013E Computational Geometry - Håkan Jonsson!

2!

2. Interval Trees •  Let S be a set of n axis-parallel line segments.!

•  Range trees: !

–  A restriction we will eventually abandon.!

–  Points, often i higher dimensions.!

•  Windowing Queries: ! –  Line segments, polygons, curves, etc usually in low dimensions (2D, 3D).!

•  Task: Build a data structure based on S that can report which line segments of S intersect a given (query-) window. ! –  The window is a rectangle and the queryalgorithm that traverses the data structure gets its vertices as input. !

•  This data structure will be based on a binary tree called an interval tree.!

D7013E Computational Geometry - Håkan Jonsson!

3!

D7013E Computational Geometry - Håkan Jonsson!

4!

An easy special case:

Interval trees… trees on intervals •  Question:!

•  Line segments with at least one end point in the window:!

–  Which, out of a bunch of one-dimensional intervals, contain a given number?!

–  Use a range tree (Ch 5).! •  O(n log n) storage to answer queries in O(log2n + k) time, where k is the number of line segments reported.! •  (Fractional cascading lowers the query time to O(log n + k).)!

•  Assume we represent each interval by a horizontal line segment on the x-axis and the number by a vertical line.! •  We then get the question: ! –  What line segments are ”stabbed” by a given vertical line?!

•  Now, since we ask for what a vertical line intersects, an answer also works for any set of horizontal line segments.!

•  However, what about line segments that cross over the window?!

–  Regradless of y-coordinates. ! –  Project segments onto the x-axis.!

–  They will not be accounted for.!

•  To also include such, let’s first look at a slightly simpler problem…! D7013E Computational Geometry - Håkan Jonsson!

5!

D7013E Computational Geometry - Håkan Jonsson!

6!

Building an interval tree

(Building cont.)

•  Find the median end point xmid of a line segment in the x-direction and divide segments in 3 subsets Imid, Ileft, and Iright:" " " " " " " " ! •  Recur on Ileft, and Iright to get the left and right subtrees respectively. !

•  Store the segments intersected by xmid, i.e. those in each root, in sorted x-order on each side of xmid:!

D7013E Computational Geometry - Håkan Jonsson!

7!

D7013E Computational Geometry - Håkan Jonsson!

8!

An example of an interval tree

Performing a query

•  Query: ! –  Check on which side of xmid the query line lies and walk through the list from outmost while reporting segments whose end point lie further from xmid than qx. ! –  Recur into one of the subtrees.! D7013E Computational Geometry - Håkan Jonsson!

9!

D7013E Computational Geometry - Håkan Jonsson!

10!

Properties of interval trees

Stabbing with line segments

•  Theorem 10.4:!

•  In our original windowing problem we stab with a line segment, one side of the query rectangle (q below), and not a line. !

–  An interval tree can be built in O(n log n) time and occupies O(n) storage, where n is the number of intervals.! –  A query (to report all intervals that contain a given number) takes O(log n + k) time, where k is the number of intervals reported. ! •  Since we split on the median the tree ends up balanced with depth O(log n).! •  T(n) = 2T(n/2) + O(n) solves to O(n log n).! D7013E Computational Geometry - Håkan Jonsson!

11!

–  There’s an interval also in the y-direction to take into account.! –  We are now considering line segments that can lie anywhere in the plane:!

D7013E Computational Geometry - Håkan Jonsson!

12!

Solution

Conclusion

•  We need to sort out exactly those end points that share y-coordinates with the line segment q.! •  Use range trees in the nodes of the interval tree!!

•  Build interval trees with range trees in the nodes. !

•  Theorem 10.6:!

Build a left range tree on these end points…

A range query as a rectangle

–  We can then also detect line segments that pass straight over our query window by two “stabbing queries”. ! •  In the x and y directions.!

…and a right one on these.

–  Let S be a set of n axis-parallel line segments in the plane. Then…! –  The line segments intersected by a query window (rectangle) can be answered in O(log2n + k) time with a data structure that uses O(n log n) storage and can be built in O(n log n) time. !

•  Here, k is the number of reported line segments.! A query line segment q D7013E Computational Geometry - Håkan Jonsson!

–  So, once again we have a case where the query time is outputsensitive.!

13!

3. Priority Search Trees

D7013E Computational Geometry - Håkan Jonsson!

14!

Heaps •  A heap is a complete binary tree.!

•  A storage efficient variant of the range tree for queries that are unbounded on one side:!

–  Only the down-most level might contain free space.!

•  A heap has the following property, called the heap property:!

"

–  The root [of the entire heap but also of a subtree] is smaller than any element in the subtrees below the root.!

! •  Implemented using a heap. ! D7013E Computational Geometry - Håkan Jonsson!

15!

D7013E Computational Geometry - Håkan Jonsson!

16!

Unbounded queries in Heaps

Idea

•  Example query: (-!,5]!

•  We like a substitute for a range tree…! •  When building the heap, divide points into subtrees by y-coordinate!! –  Each node contains the median in the y-direction in its subtree.! –  A parent node has lower x-coordinate than its children.! Ordering among the points In x: p5, p3, p1, p6, p2, p4 In y: p5, p2, p1, p4, p6, p3

We start at “minus infinity” and go towards “plus infinity”, i.e. downwards in our traversal of the heap.

Stop nodes



(The heap is deliberately tilted to show how points are stored depending on their x- and y-coordinates.)

+

D7013E Computational Geometry - Håkan Jonsson!

17!

Performing a query in a priority search tree One of the priority search trees in a node

Query line segment

q

D7013E Computational Geometry - Håkan Jonsson!

19!

D7013E Computational Geometry - Håkan Jonsson!

18!

Conclusion •  Theorem 10.9:! –  A priority search tree for a set of n points in the plane uses O(n) storage and can be built in O(n log n) time. ! •  Sorting points and picking out medians.! –  All k points in a query range (-!,qx]"[qy,q’y] can be reported in O(log n + k) time. ! •  Just traverse the heap while not going down into subtrees whose roots contain points with ! –  y-coordinates smaller/larger than those of q and ! –  x-coordinates larger than q. ! D7013E Computational Geometry - Håkan Jonsson!

20!

4. Segment Trees

A simple solution

•  A data structure suitable to answer questions like about a set of arbitrarily oriented but (still) non-intersecting line segments: !

•  Replace each line segment by its bounding box.!

–  Which line segments intersect a given query window?! –  As before, segments with end points in the window are easily found using a range tree of all end points.! –  But what about the other?!!

–  First single out boxes that overlap with the window, then check the line segments in the boxes found.!

•  Use our previous data structure for axisparallel line segments.! –  ”Usually” good in practice.! –  Bad news: In the worst case a query window could intersect all bounding boxes while not intersecting a single line segment(!)!

D7013E Computational Geometry - Håkan Jonsson!

21!

A problem with interval trees •  Why is an interval tree not that suitable?" " " " " " " " ! •  Arbitrary oriented line segments could point outside the query region (thereby upsetting everything)!

D7013E Computational Geometry - Håkan Jonsson!

23!

D7013E Computational Geometry - Håkan Jonsson!

22!

The locus approach •  Divide parameter space into regions where the answer to a query is the same.! –  A common technique to reduce “an infinite number of things to finite”.! –  Plane sweep is an example where an infinite number of stops are reduced to a finite number.!

•  In our original stabbing problem, there are infinitely many query lines but only a finite number of answers.! •  The intervals in which the answer is the same is elementary. ! D7013E Computational Geometry - Håkan Jonsson!

24!

Supporting stabbing queries

Idea to reduce the storage

•  To support stabbing queries we could build a (balanced) binary search tree with leaves that correspond to elementary intervals. !

•  Instead of storing all pointers at leaves, “lift” pointers up to internal nodes when segments cover many elementary intervals in whole subtrees. " " " " " " " " ! •  The segment s cover all 5 leaves but is just stored at node v and leaf µ5 !

–  Store pointers to the actual line segments in the elementary intervals.!

•  A query now amounts to locating the elementary interval that contains the query line.! •  However, O(n2) storage in the worst case:!

–  s is just one of many intervals.!

D7013E Computational Geometry - Håkan Jonsson!

25!

An example of a segment tree

D7013E Computational Geometry - Håkan Jonsson!

26!

About segment trees •  Lemma 10.10: ! –  A segment tree uses O(n log n) storage.! •  Each segment is stored at most twice on each level in the tree, which is balanced.! –  It can be built in O(n log n) time.!

•  Lemma 10.11:! –  Using a segment tree, the k intervals containing a query line (a point) can be reported in O(log n + k) time.!

D7013E Computational Geometry - Håkan Jonsson!

27!

D7013E Computational Geometry - Håkan Jonsson!

28!

Solving the original windowing problem

Solving the original windowing problem

•  In a segment tree, nodes contain those line segments that cover the set of elementary intervals in the subtree of the node only. ! –  Canonical subset.!

•  Having searched the segment tree for segments stabbed in the x direction by q, the vertical query line segment….! •  …. left is to find those that also share ycoordinate with q.! –  Only these are actually intersected.! D7013E Computational Geometry - Håkan Jonsson!

29!

Final touch

30!

Final conclusion

•  So, each node corresponds to a horizontal slab… and the canonical subset of line segments of a node pass straight through the slab of the node… and it was assumed that line segments do not intersect…! •  …build a balanced binary search tree in each slab and over the trapezoids in the slab!! •  Searching in this tree using the end points of q (the vertical query segment) gives us all the correct segments.! D7013E Computational Geometry - Håkan Jonsson!

D7013E Computational Geometry - Håkan Jonsson!

•  Corollary 10.14:! –  Let S be a set of n line segments with arbitrary orientation and disjoint interiors. ! –  The k line segments of S 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 (and can be built in O(n log n) time). ! •  In each of O(log n) nodes we perform a search in a slab that takes O(log n) time. !

31!

D7013E Computational Geometry - Håkan Jonsson!

32!