Segment Trees

Segment Trees

Segment Trees 1 Zooming in on Line Segments data structures for windowing queries a binary search tree for intervals 2 Segment Trees definition and storage cost query a segment tree constructing a segment tree MCS 481 Lecture 31 Computational Geometry Jan Verschelde, 5 April 2019 Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 1 / 22 Segment Trees 1 Zooming in on Line Segments data structures for windowing queries a binary search tree for intervals 2 Segment Trees definition and storage cost query a segment tree constructing a segment tree Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 2 / 22 windowing queries Given a map, we zoom in on a window ) windowing query. We assume the line segments do not intersect each other. Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 3 / 22 types of segments Two types of segments: 1 an end point lies inside the query window (use range tree), 2 segments intersect the boundary of the window. Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 4 / 22 a vertical query segment 0 Consider a vertical query segment qx × [qy : qy ]. The x-coordinates of the end points of the segments define intervals. Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 5 / 22 an interval tree xmid Does intersection with x = xmid imply that in window? Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 6 / 22 Segment Trees 1 Zooming in on Line Segments data structures for windowing queries a binary search tree for intervals 2 Segment Trees definition and storage cost query a segment tree constructing a segment tree Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 7 / 22 organizing a set of intervals Given is a set I of n intervals: 0 0 0 I = f[x1 : x1]; [x2 : x2];:::; [xn : xn]g: The n intervals define m distinct points: p1 p2 p3 p4 p5 p6 p7 p8 p9p10 p11 p12 The m points are sorted p1 < p2 < ··· < pm and define the elementary intervals: (−∞ : p1); [p1 : p1]; (p1 : p2);:::; [pm : pm]; (pm : +1): Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 8 / 22 query the elementary intervals Given a query point qx and the elementary intervals (−∞ : p1); [p1 : p1]; (p1 : p2);:::; [pm : pm]; (pm : +1); determine the elementary interval that contains qx . p1 p2 p3 p4 p5 p6 p7 p8 p9p10 p11 p12 How to organize a binary search tree for such a query? Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 9 / 22 a binary search tree The leaves of the search tree are the elementary intervals (−∞ : p1); [p1 : p1]; (p1 : p2);:::; [pm : pm]; (pm : +1): p1 p2 p3 p4 p5 p6 p7 p8 p9p10 p11 p12 The intervals that contain an elementary interval are at its leaf. Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 10 / 22 principle of a segment tree If an interval spans many elementary intervals, then the interval will be repeated many times and the storage cost will be high. s p1 p2 p3 p4 p5 p6 p7 p8 p9p10 p11 p12 s Principle of a segment tree: store the interval in a node as close to the root as possible . Exercise 1: complete the picture above, placing all segments at the nodes in the tree, following the principle of a segment tree. Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 11 / 22 Segment Trees 1 Zooming in on Line Segments data structures for windowing queries a binary search tree for intervals 2 Segment Trees definition and storage cost query a segment tree constructing a segment tree Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 12 / 22 definition of a segment tree Given is a set I of n intervals. The m end points of the intervals are sorted and define the elementary intervals. The leaves of a balanced binary tree T store the elementary intervals, in the sorted order of the end points: I the leftmost leaf of T stores the leftmost interval, I the rightmost leaf of T stores the rightmost interval. Each internal node of T is the union of the elementary intervals at the leaves under the node. The interval at a node is the union of the intervals of its children. Each node v stores an interval and a set I(v) of intervals. This canonical subsetI (v) at the node v contains all [x : x0] 2 I: 0 I the interval of the node v is contained in [x : x ], and 0 I the interval of the parent of v is not contained in [x : x ]. Exercise 2: verify that the above definition of the canonical subset agrees with the principle of a segment tree, refer to Exercise 1. Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 13 / 22 storage cost Lemma (storage cost of a segment tree) A segment tree on a set of n intervals occupies O(n log(n)) storage. The leaves of the tree are the elementary intervals. How many? Each end point is stored separately, for n given intervals, we have 0 I at most 2n intervals of type [p : p ], for end points p, I at most 2n − 1 intervals in between (pi : pi+1), and I the intervals (−∞ : p1) and (pm : +1). If all endpoints are distinct from each other, then m = 2n. In total, we have at most 4n + 1 leaves in the tree. The tree is a balanced binary search tree, of depth O(log(n)). But how many times does each given interval occur in the tree? Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 14 / 22 on the canonical subset From the definition of the segment tree: Each node v stores an interval and a set I(v) of intervals. This canonical subsetI (v) at the node v contains all [x : x0] 2 I: 0 I the interval of the node v is contained in [x : x ], and 0 I the interval of the parent of v is not contained in [x : x ]. The second item ensures every given interval is stored at most twice. 0 Assume otherwise, [x : x ] is stored at nodes v1, v2, and v3, all at the same depth. 0 1 [x : x ] spans the whole interval at v2, from the left end point of the interval stored at v1, from the right end point of the interval stored at v3. 2 Because v2 lies between v1 and v2, 0 the interval at the parent of v2 is contained in [x : x ]. 0 Therefore, [x : x ] is not stored at v2, contradicting the assumption. Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 15 / 22 Segment Trees 1 Zooming in on Line Segments data structures for windowing queries a binary search tree for intervals 2 Segment Trees definition and storage cost query a segment tree constructing a segment tree Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 16 / 22 query a segment tree Algorithm QUERYSEGMENTTREE(v; qx ) Input: v is the root of a segment tree T , qx is a query point. Output: all intervals in T containing qx . 1 report all intervals in I(v) 2 if v is not a leaf then 3 if qx is in the interval of LEFTCHILD(v) then 4 QUERYSEGMENTTREE(LEFTCHILD(v; qx )) else 5 QUERYSEGMENTTREE(RIGHTCHILD(v; qx )) Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 17 / 22 an output sensitive algorithm Lemma (the cost of a query of a segment tree) Algorithm QUERYSEGMENTTREE reports k intervals in a segment tree on n intervals in time O(log(n) + k). The depth of the balanced search tree is O(log(n)). At node v, we spend O(1 + kv ) time, where kv is the number of reported intervals at node v. The sum of all kv ’s equals k. Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 18 / 22 Segment Trees 1 Zooming in on Line Segments data structures for windowing queries a binary search tree for intervals 2 Segment Trees definition and storage cost query a segment tree constructing a segment tree Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 19 / 22 constructing a segment tree Algorithm INSERTSEGMENTTREE(v; [x : x0]) Input: v is the root of a segment tree T , [x : x0] is the interval to be inserted in T . Output: v where [x : x0] is inserted. 1 If the interval at v is contained in [x : x0] then 2 store [x : x0] at v else 3 if the interval at LEFTCHILD(v) \ [x : x0] 6= ; then 4 INSERTSEGMENTTREE(LEFTCHILD(v), [x : x0]) 5 if the interval at RIGHTCHILD(v) \ [x : x0] 6= ; then 6 INSERTSEGMENTTREE(RIGHTCHILD(v), [x : x0]) Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 20 / 22 construction cost Lemma (cost to insert one interval in a segment tree) Inserting one interval in a segment tree on n intervals runs in O(log(n)) time. Theorem (performance of a segment tree) For n intervals a segment tree takes O(n log(n)) storage and O(n log(n)) time to construct. The query time is O(log(n) + k), where k is the number of reported intervals. Computational Geometry (MCS 481) Segment Trees L-31 5 April 2019 21 / 22 recommended assignments We covered section 10.3 in the textbook. Consider the following activities, listed below. 1 Write the solutions to exercises 1 and 2.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    22 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