Lecture05 Handout
description
Transcript of Lecture05 Handout
-
Segment Trees & Interval Trees
Lecture 5, CS 631100
Sheung-Hung [email protected]
Fall 2011National Tsing Hua University (NTHU)
Lecture 5, CS 631100 Segment Trees & Interval Trees 1
-
Outline
Reference:Textbook chapter 10Mounts Lectures 25 and 26
Segment treesStabbing queriesRectangle intersection
Interval treesSome improvement
Higher dimensions
Lecture 5, CS 631100 Segment Trees & Interval Trees 2
-
Introduction
Stabbing queries
Range searching query: Given data points, queryrectangle.Stabbing query: Given data rectangles, query point.In one dimension
Input: a set of n intervals, a query point qOutput: the k intervals that contain q
In IRd
A box b is called isothetic/axisparallel iff it can be writtenas b = [x1, x1] [x2, x2] [xd, xd].Input: a set of n isothetic boxes, a query point qOutput: the k boxes that contain q
Lecture 5, CS 631100 Segment Trees & Interval Trees 3
-
Introduction
Motivation
In applications like computer graphics and databases,objects are often stored in their bounding boxes.
Query: which objects does point x lie inside?First find objects Q whose bounding boxes intersect x;Then check whether point x lies in any of objects in Q.
Lecture 5, CS 631100 Segment Trees & Interval Trees 4
-
Segment Trees
Segment Trees
Lecture 5, CS 631100 Segment Trees & Interval Trees 5
-
Segment Trees
Segment tree
A data structure to store intervals, or segments in IR2
Allows to answer stabbing queriesIn IR2: report segments that intersect a query vertical line l
Query time: O(log n+ k)Space complexity: O(n log n)Preprocessing time: O(n log n)
Lecture 5, CS 631100 Segment Trees & Interval Trees 6
-
Segment Trees
Notations
Let S = (s1, s2, . . . sn) be a set of segments in IR2.Let E be the set of the xcoordinates of the endpoints ofthe segments of S.We assume general position, that is: |E| = 2nFirst sort E in increasing order,E = {e1 < e2 < . . . e2n}
Lecture 5, CS 631100 Segment Trees & Interval Trees 7
-
Segment Trees
Segment tree: Atomic intervals
E = {e1 < e2 < . . . e2n} splits IR into 2n+ 1 atomic intervals:[, e1][ei, ei+1] for i {1, 2, . . . 2n 1}[e2n,]
These forms the leaves of the segment tree T .
Lecture 5, CS 631100 Segment Trees & Interval Trees 8
-
Segment Trees
Segment tree: Internal nodes
The segment tree T is a balanced binary tree.Each internal node u with children v and v is associatedwith an interval Iu = Iv I v.Such an associated interval is called an elementaryinterval of T , (which may be an atomic interval).
Lecture 5, CS 631100 Segment Trees & Interval Trees 9
-
Segment Trees
Example
Lecture 5, CS 631100 Segment Trees & Interval Trees 10
-
Segment Trees
Partitioning a segment
Let s S be a segment whose endpoints havexcoordinates ei and ej .[ei, ej ] is split into several elementary intervals,which are chosen as close as possible to the root.
s is stored in all of those nodes associated withthese elementary intervals.
Lecture 5, CS 631100 Segment Trees & Interval Trees 11
-
Segment Trees
Standard lists
(Recall that ei < ej be the xcoordinates of the endpointsof s S.)
The intervals stored at a node u forms a list called astandard list Lu for node u.
s is stored in Lu iffIu [ei, ej ] and Iparent(u) 6 [ei, ej ],i.e. s is stored as close as possible to the root.(See previous slide and next slide.)
Lecture 5, CS 631100 Segment Trees & Interval Trees 12
-
Segment Trees
An example segment tree
Lecture 5, CS 631100 Segment Trees & Interval Trees 13
-
Segment Trees
Answering a stabbing query
Lecture 5, CS 631100 Segment Trees & Interval Trees 14
-
Segment Trees
Pseudo-code for answering a stabbing query
Algorithm StabbingQuery(u, xl)Input: root u of T , xcoordinate of lOutput: segments in S that cross l1. if u == NULL2. then return3. output Lu4. if xl Iu.left5. then StabbingQuery(u.left, xl)6. if xl Iu.right7. then StabbingQuery(u.right, xl)
It clearly takes O(k + log n) time
Lecture 5, CS 631100 Segment Trees & Interval Trees 15
-
Segment Trees
Constructing a segment tree T
Algorithm ConstructSegmentTreeInput: line segments S in IR2
Output: Segment tree T containing S.1. Compute the atomic intervals for given line segments S.
2. Using the atomic intervals as leaves,build a BBST T from leaves to root.
3. Inserting line segments of S one by one into T .(See next slide)
Lecture 5, CS 631100 Segment Trees & Interval Trees 16
-
Segment Trees
Inserting a segment s
Lecture 5, CS 631100 Segment Trees & Interval Trees 17
-
Segment Trees
Pseudo-code for inserting a segment s
Algorithm InsertSegment(u, s)Input: root u of T , segment s with endpoints x < x+.1. if Iu [x, x+]2. then Insert s into Lu3. else4. if [x, x+] Iu.left 6= 5. then InsertSegment(u.left, s)6. if [x, x+] Iu.right 6= 7. then InsertSegment(u.right, s)
Lecture 5, CS 631100 Segment Trees & Interval Trees 18
-
Segment Trees
A property on segment trees
Propertys is stored at most twice at each level of T .Proof.
Well prove by contradiction.Suppose contrary: s stored at more than 2 nodes at level i.Let u be the leftmost such node, u be the rightmost.Let v be another node at level i containing s.
Then Iv.parent [x, x+], which is a contradiction.Hence s cannot be stored at v.
Lecture 5, CS 631100 Segment Trees & Interval Trees 19
-
Segment Trees
Analysis
Property of previous slide impliesSpace complexity: O(n log n)
Insertion in O(log n) time(Due to previous slide: at most four nodes are visited ateach level)Query time: O(k + log n)Preprocessing time:
Sort endpoints: O(n log n) timeBuild empty segment tree T over endpoints: O(n) timeInsert n segments into T : O(n log n) timeOverall: O(n log n) preprocessing time
Lecture 5, CS 631100 Segment Trees & Interval Trees 20
-
Rectangle Intersection
Rectangle Intersection:
(An Application of Range and Segment Trees)
Lecture 5, CS 631100 Segment Trees & Interval Trees 21
-
Rectangle Intersection
Overview
Input: a set B of n isothetic boxes in IR2
Output: all the intersecting pairs in B2
Using segment trees, we give an O(n log n+ k)-timealgorithm where k is the number of intersecting pairs.Note: this is optimalNote: faster than using line segment intersection algorithmSpace complexity: O(n log n) due to segment treesNote: space complexity is NOT optimal (O(n) space ispossible with optimal running time)
Lecture 5, CS 631100 Segment Trees & Interval Trees 22
-
Rectangle Intersection
Example
Output: (b1, b3),(b2, b3),(b2, b4),(b3, b4)
Lecture 5, CS 631100 Segment Trees & Interval Trees 23
-
Rectangle Intersection
Two kinds of intersections
Overlap:
intersecting edges reduces to intersection
reporting for isotheticsegments
Inclusion:
we can find them usingstabbing queries
Lecture 5, CS 631100 Segment Trees & Interval Trees 24
-
Rectangle Intersection
Reporting overlaps
Equivalent to reporting intersecting edges.We use plane sweep approach.Sweep line status: BBST T containing the horizontal linesegments that intersect the sweep line, with increasingy-coordinatesEach time a left vertical line segment s is encountered,report intersection by range searching in the BBST.
Insert two corresponding horizontal line segments into T ;Each time a right vertical line segment s is encountered,
Delete two corresponding horizontal line segments from T .Running time: O(n log n+ k)
Lecture 5, CS 631100 Segment Trees & Interval Trees 25
-
Rectangle Intersection
Reporting inclusions
We still use plane sweep.
Sweep line status: rectangles intersecting sweep line lstored in a segment tree Tl with respect to y-coordinates.
Endpoints are the y-coordinates of the horizontal edges ofall rectangles.At a particular time, only rectangles that intersect l are inthe segment tree Tl.We can perform insertions and deletions in a segment treein O(log n) time. (How ?)(See next slides ...)
Each time a vertex of a rectangle is encountered,perform a stabbing query in the segment tree.
Lecture 5, CS 631100 Segment Trees & Interval Trees 26
-
Rectangle Intersection
First Solution: Deleting a segment s
Algorithm DeleteSegment(u, s)Input: root u of T , segment s with endpoints x < x+.1. if Iu [x, x+]2. then Delete s from Lu3. else4. if [x, x+] Iu.left 6= 5. then DeleteSegment(u.left, s)6. if [x, x+] Iu.right 6= 7. then DeleteSegment(u.right, s)
The deletion routine is similar as the insertion routine.In the routine, we delete all occurrences of s from T .It runs in O(log n) |Lu| time in total.This is TOO SLOW.We want it to run in only O(log n) time.
Lecture 5, CS 631100 Segment Trees & Interval Trees 27
-
Rectangle Intersection
Modified Insertion Procedure for s
Apart from segment tree T , we maintain another BBST T .T stores all the segments in T using the x-coordinates oftheir left endpoints.When we insert s into T , we also insert s into T .Then we also attach to s in T a list of points pointing to alloccurrences of s in T .
ss
s
s
s
T
It still runs in O(log n) time in total.Lecture 5, CS 631100 Segment Trees & Interval Trees 28
-
Rectangle Intersection
Improved Deletion Procedure for s
ss
s
s
s
T
First search s in T .Then follows the pointers at node s to delete alloccurrences of s in segment tree T .s is also deleted from T .This procedure runs in O(log n) time.
Lecture 5, CS 631100 Segment Trees & Interval Trees 29
-
Rectangle Intersection
Remarks
At each step, an intersecting pair of rectangles can bereported several times.Moreover, overlap and vertex stabbing a rectangle canoccur at the same time.
However, each intersecting pair is reported only a constantnumber of times. Thus the total running time remainsO(n log n+ k).It is possible to obtain each intersecting pair only once bymaking some careful checks. (Its details are omitted in thislecture.)
Lecture 5, CS 631100 Segment Trees & Interval Trees 30
-
Interval Trees
Interval Trees
Lecture 5, CS 631100 Segment Trees & Interval Trees 31
-
Interval Trees
Introduction
Interval trees allow to perform stabbing queries in onedimension.
Query time: O(log n+ k)Preprocessing time: O(n log n)Space complexity: O(n)
Reference: Mounts lecture notes, page 100 (vertical linestabbing queries) to page 103 (not including verticalsegment stabbing queries).
Lecture 5, CS 631100 Segment Trees & Interval Trees 32
-
Interval Trees
Preliminary
Let xmed be the median of endpoints of given intervals.Sl = segments of S that are completely to left of xmed.Smed = segments of S that contain xmed.Sr = segments of S that are completely to right of xmed.
Lecture 5, CS 631100 Segment Trees & Interval Trees 33
-
Interval Trees
Data structure of interval tree
Recursive data structureLeft child of the root: interval tree storing SlRight child of the root: interval tree storing SrAt the root of the interval tree, we store Smed in two lists
ML is sorted according to coordinates of left endpoints(in increasing order)MR is sorted according to coordinates of right endpoints(in decreasing order)
Lecture 5, CS 631100 Segment Trees & Interval Trees 34
-
Interval Trees
Example
Lecture 5, CS 631100 Segment Trees & Interval Trees 35
-
Interval Trees
Stabbing query
Query: xq, find the intervals that contain xq.If xq < xmed, then
Scan Ml in increasing order, and report intervals that arestabbed until the x-coordinate of the current left endpoint islarger than xq.Recurse on Sl.
If xq > xmedAnalogous, but on the right side.
Lecture 5, CS 631100 Segment Trees & Interval Trees 36
-
Interval Trees
Analysis
Query time:Size of the subtree divided by at least two at each levelO(log n) levels.Scanning through Ml or Mr: proportional to the number ofreported intervals.Conclusion: O(log n+ k) time
Space complexity:
O(n) (each segment is stored in two lists, and the tree isbalanced)
Preprocessing time:
can be easily done in O(n log n) time.
Lecture 5, CS 631100 Segment Trees & Interval Trees 37
-
Stabbing queries in higher dimensions
Stabbing queries in higher dimensions
Lecture 5, CS 631100 Segment Trees & Interval Trees 38
-
Stabbing queries in higher dimensions
Approach
In IRd, given a set B of n boxes.For a query point q find all the boxes that contain it.Well use a multi-level segment tree.Inductive definition, induction on d.First, we store B in a segment tree T with respect tox1coordinates.For all nodes u of T , associate a (d 1)dimensionalmulti-level segment tree over Lu, with respect to(x2, x3 . . . xd).
Lecture 5, CS 631100 Segment Trees & Interval Trees 39
-
Stabbing queries in higher dimensions
Performing queries
Search for query point q in TFor all nodes in the search path, query recursively in the(d 1)-dimensional multi-level segment tree.There are log n such queries.By induction on d, we can prove that
Query time: O(logd n+ k)Space complexity: O(n logd n)Preprocessing time : O(n logd n)
Lecture 5, CS 631100 Segment Trees & Interval Trees 40
-
Stabbing queries in higher dimensions
Improvements
Fractional cascading at the deepest level:(Details omitted for this lecture.)
gains a factor log n on query time bound.results in O(logd1 n+ k) query time.
Interval trees at the deepest level:gains a factor log n on space complexity bound.results in O(n logd1 n) space complexity.
Lecture 5, CS 631100 Segment Trees & Interval Trees 41
-
Next Lecture
Summary of this lecture:Segment Trees and Interval Trees
Segment TreesInterval Trees
Next lecture:Kd-Trees
2-dim. kd-treesd-dim. kd-trees
Lecture 5, CS 631100 Segment Trees & Interval Trees 42
IntroductionSegment TreesRectangle IntersectionInterval TreesStabbing queries in higher dimensionsNext Lecture