Segment Trees & Interval Trees
Lecture 5, CS 631100
Sheung-Hung Poon
[email protected]
Fall 2011
National Tsing Hua University (NTHU)
Lecture 5, CS 631100
Segment Trees & Interval Trees
Outline
Reference:
Textbook chapter 10
Mounts Lectures 25 and 26
Segment trees
Stabbing queries
Rectangle intersection
Interval trees
Some improvement
Higher dimensions
Lecture 5, CS 631100
Segment Trees & Interval Trees
Introduction
Stabbing queries
Range searching query: Given data points, query
rectangle.
Stabbing query: Given data rectangles, query point.
In one dimension
Input: a set of n intervals, a query point q
Output: the k intervals that contain q
In IRd
A box b is called isothetic/axisparallel iff it can be written
as b = [x1 , x01 ] [x2 , x02 ] [xd , x0d ].
Input: a set of n isothetic boxes, a query point q
Output: the k boxes that contain q
Lecture 5, CS 631100
Segment Trees & Interval Trees
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
Segment Trees
Segment Trees
Lecture 5, CS 631100
Segment Trees & Interval Trees
Segment Trees
Segment tree
A data structure to store intervals, or segments in IR2
Allows to answer stabbing queries
In 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
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 of
the segments of S.
We assume general position, that is: |E| = 2n
First sort E in increasing order,
E = {e1 < e2 < . . . e2n }
Lecture 5, CS 631100
Segment Trees & Interval Trees
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
Segment Trees
Segment tree: Internal nodes
The segment tree T is a balanced binary tree.
Each internal node u with children v and v 0 is associated
with an interval Iu = Iv Iv0 .
Such an associated interval is called an elementary
interval of T , (which may be an atomic interval).
Lecture 5, CS 631100
Segment Trees & Interval Trees
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 have
xcoordinates 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 with
these elementary intervals.
Lecture 5, CS 631100
Segment Trees & Interval Trees
11
Segment Trees
Standard lists
(Recall that ei < ej be the xcoordinates of the endpoints
of s S.)
The intervals stored at a node u forms a list called a
standard list Lu for node u.
s is stored in Lu iff
Iu [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 l
Output: segments in S that cross l
1. if u == N U LL
2.
then return
3. output Lu
4. if xl Iu.lef t
5.
then StabbingQuery(u.lef t, xl )
6. if xl Iu.right
7.
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 ConstructSegmentTree
Input: 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 Lu
3.
else
4.
if [x , x+ ] Iu.lef t 6=
5.
then InsertSegment(u.lef t, 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
Property
s 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, u0 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 implies
Space complexity: O(n log n)
Insertion in O(log n) time
(Due to previous slide: at most four nodes are visited at
each level)
Query time: O(k + log n)
Preprocessing time:
Sort endpoints: O(n log n) time
Build empty segment tree T over endpoints: O(n) time
Insert n segments into T : O(n log n) time
Overall: 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 B 2
Using segment trees, we give an O(n log n + k)-time
algorithm where k is the number of intersecting pairs.
Note: this is optimal
Note: faster than using line segment intersection algorithm
Space complexity: O(n log n) due to segment trees
Note: space complexity is NOT optimal (O(n) space is
possible 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:
Inclusion:
intersecting edges
reduces to intersection
reporting for isothetic
segments
Lecture 5, CS 631100
we can find them using
stabbing queries
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 line
segments that intersect the sweep line, with increasing
y-coordinates
Each 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 l
stored in a segment tree Tl with respect to y-coordinates.
Endpoints are the y-coordinates of the horizontal edges of
all rectangles.
At a particular time, only rectangles that intersect l are in
the segment tree Tl .
We can perform insertions and deletions in a segment tree
in 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 Lu
3.
else
4.
if [x , x+ ] Iu.lef t 6=
5.
then DeleteSegment(u.lef t, 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 0 .
T 0 stores all the segments in T using the x-coordinates of
their left endpoints.
When we insert s into T , we also insert s into T 0 .
Then we also attach to s in T 0 a list of points pointing to all
occurrences of s in T .
T0
s
s
s
s
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
T0
s
s
s
s
First search s in T 0 .
Then follows the pointers at node s to delete all
occurrences of s in segment tree T .
s is also deleted from T 0 .
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 be
reported several times.
Moreover, overlap and vertex stabbing a rectangle can
occur at the same time.
However, each intersecting pair is reported only a constant
number of times. Thus the total running time remains
O(n log n + k).
It is possible to obtain each intersecting pair only once by
making some careful checks. (Its details are omitted in this
lecture.)
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 one
dimension.
Query time: O(log n + k)
Preprocessing time: O(n log n)
Space complexity: O(n)
Reference: Mounts lecture notes, page 100 (vertical line
stabbing queries) to page 103 (not including vertical
segment 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 structure
Left child of the root: interval tree storing Sl
Right child of the root: interval tree storing Sr
At 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 are
stabbed until the x-coordinate of the current left endpoint is
larger than xq .
Recurse on Sl .
If xq > xmed
Analogous, 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 level
O(log n) levels.
Scanning through Ml or Mr : proportional to the number of
reported intervals.
Conclusion: O(log n + k) time
Space complexity:
O(n) (each segment is stored in two lists, and the tree is
balanced)
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 to
x1 coordinates.
For all nodes u of T , associate a (d 1)dimensional
multi-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 T
For 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 Trees
Interval Trees
Next lecture:
Kd-Trees
2-dim. kd-trees
d-dim. kd-trees
Lecture 5, CS 631100
Segment Trees & Interval Trees
42