Interval Trees

Interval Trees

Interval Trees 1 Windowing Queries zoom in on a window application of range queries 2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree 3 Rectangular Query Windows a more elaborate data structure MCS 481 Lecture 29 Computational Geometry Jan Verschelde, 1 April 2019 Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 1 / 25 Interval Trees 1 Windowing Queries zoom in on a window application of range queries 2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree 3 Rectangular Query Windows a more elaborate data structure Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 2 / 25 windowing queries Given a map, we zoom in on a window ) windowing query. Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 3 / 25 Interval Trees 1 Windowing Queries zoom in on a window application of range queries 2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree 3 Rectangular Query Windows a more elaborate data structure Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 4 / 25 orthogonal range queries A range is a rectangle [x : x0] × [y : y 0]. A range may be entirely contained in some region, but it may also overlap with several regions. `5 `1 `7 3 ` 6 1 8 4 1 `2 `3 `2 7 2 `3 5 `4 `5 `6 `7 `4 `6 1 2 3 4 5 7 6 8 A range tree is a balanced binary tree on the x-coordinate, every node stores a balanced binary tree on the y-coordinate. Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 5 / 25 fractional cascading We can save a binary search by linking sorted arrays. Consider A = [11; 20; 24; 29; 41; 48; 61; 85; 92] and B = [4; 38; 40; 54; 72; 84; 85; 85; 99]. 11 20 24 29 41 48 61 85 92 4 38 40 54 72 84 85 85 99 Every element x in A has a pointer to the first element in B which is either equal to x or larger than x. Thanks to these pointers we save a binary search in B. The query time is O(log(n) + k), in a 2D range tree of n points, where k is the number of reported points. Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 6 / 25 axis parallel query windows Consider line segments parallel either to the x- or to the y-axis. Definition (axis parallel query window) An axis parallel query window W is determined by two intervals, W = [x : x0] × [y : y 0]. Applying the results from range queries, we have the following. Lemma (cost of axis parallel query window) Let S be a set of n axis parallel line segments and W be an axis parallel query window. Segments having at least one endpoint inside W can be reported in O(n log(n) + k) time with a data structure using O(n log(n)) storage and preprocessing time, where k is the number of reported segments. Can we reduce the storage cost from O(n log(n)) to O(n)? Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 7 / 25 Interval Trees 1 Windowing Queries zoom in on a window application of range queries 2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree 3 Rectangular Query Windows a more elaborate data structure Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 8 / 25 search horizontal line segments Problem Statement: preprocess a set of n horizontal line segments such that segments intersected by a vertical line can be reported efficiently. 0 0 0 Input: S = f[x1 : x1]; [x2 : x2];:::; [xn : xn]g, a set of n intervals. xmid is the median of the 2n end points so that at most half of the end points of S lies at the left of xmid, and at most half of the end points of S lies at the right of xmid. xmid xmid Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 9 / 25 classification of intervals 0 0 0 Input: S = f[x1 : x1]; [x2 : x2];:::; [xn : xn]g, a set of n intervals. The median xmid leads to a classification of the intervals in S. xmid 0 0 Ileft = f [x : x ] 2 S j x < xmid g 0 0 Imid = f [x : x ] 2 S j x ≤ xmid ≤ x g 0 0 Iright = f [x : x ] 2 S j xmid < x g Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 10 / 25 query horizontal line segments Query a set S of n horizontal line segments. Input: (S; qx ), qx represents the line x = qx . Output: f s 2 S j s \ (x = qx ) 6= ; g. qx xmid 1 If qx = xmid, then report Imid. 2 If qx < xmid, then report in Ileft, Imid. 3 If qx > xmid, then report in Iright, Imid. Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 11 / 25 Interval Trees 1 Windowing Queries zoom in on a window application of range queries 2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree 3 Rectangular Query Windows a more elaborate data structure Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 12 / 25 the interval tree Goal: report intervals that contain qx in O(log(n) + k) time, where k is the number of reported intervals. 0 0 0 S = f[x1 : x1]; [x2 : x2];:::; [xn : xn]g, median xmid. 0 0 Imid = f [x : x ] 2 S j x ≤ xmid ≤ x g We sort s 2 S on left and right end points. The set Imid is stored twice: 1 Lleft contains segments of Imid sorted on the left end point, 2 Lright contains segments of Imid sorted on the right end point. Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 13 / 25 a recursive structure 1 Lleft contains segments of Imid sorted on the left end point, 2 Lright contains segments of Imid sorted on the right end point. Imid = f s1; s2; s3; s4; s5g is a set. Lleft = [ s5; s2; s1; s4; s3 ] and Lright = [ s4; s2; s5; s3; s1 ] are lists. s1 s2 s3 s4 s5 xmid Exercise 1: On the example above, describe Lleft, Lright for Ileft, Iright. Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 14 / 25 a recursive algorithm Algorithm CONSTRUCTINTERVALTREE(S) Input: a set S of intervals. Output: the root of the interval tree which stores S. 1 if S = ; then 2 return an empty leaf else 3 xmid = MEDIAN(S) 4 [Ileft; Imid; Iright] = SPLIT(S; xmid) 5 [Lleft; Lright] = SORT(Imid) 6 v = NODE(xmid; Lleft; Lright) 7 LEFTCHILD(v) = CONSTRUCTINTERVALTREE(Ileft) 8 RIGHTCHILD(v) = CONSTRUCTINTERVALTREE(Iright) 9 return v Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 15 / 25 the cost of an interval tree Lemma (storage cost of an interval) An interval tree for n horizontal line segments occupies O(n) storage and has depth O(log(n)). The depth O(log(n)) follows from the median xmid. The left and right child of the root hold at most n=2 intervals. The children of each node split the data of each node in half. 0 0 A segment [x : x ] is stored only at the node where xmid 2 [x : x ], once in the list Lleft and once in the list Lright. The total number of nodes equals n and 2n is O(n). Lemma (cost of Algorithm CONSTRUCTINTERVALTREE) An interval tree for n segments can be constructed in O(n log(n)) time. Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 16 / 25 Interval Trees 1 Windowing Queries zoom in on a window application of range queries 2 Interval Trees a data structure for a simplified problem storing intervals in a tree query an interval tree 3 Rectangular Query Windows a more elaborate data structure Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 17 / 25 query an interval tree 1 Lleft contains segments of Imid sorted on the left end point, 2 Lright contains segments of Imid sorted on the right end point. Imid = f s1; s2; s3; s4; s5g is a set. Lleft = [ s5; s2; s1; s4; s3 ] and Lright = [ s4; s2; s5; s3; s1 ] are lists. s1 s2 s3 s4 s5 xmid qx Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 18 / 25 the algorithm to query an interval tree Algorithm QUERYINTERVALTREE(v; qx ) Input: root v of an interval tree and a query point qx . Output: all intervals that contain qx . 1 if v is not a leaf then 2 if qx = xmid(v) then 3 report Lleft(v) 4 else if qx < xmid(v) then 5 walk Lleft(v), starting at interval with leftmost end point 6 in this walk, report all intervals that contain qx 7 stop walking at the first interval that does not contain qx 8 QUERYINTERVALTREE(LEFTCHILD(v), qx ) else ... Computational Geometry (MCS 481) Interval Trees L-29 1 April 2019 19 / 25 Algorithm QUERYINTERVALTREE continued else 9 walk Lright(v), starting at interval with rightmost end point 10 in this walk, report all intervals that contain qx 11 stop walking at the first interval that does not contain qx 12 QUERYINTERVALTREE(RIGHTCHILD(v), qx ) What is the cost of QUERYINTERVALTREE? The depth of the interval tree is O(log(n)).

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    25 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us