Lecture05 Handout
Lecture05 Handout
Lecture 5, CS 631100
Sheung-Hung Poon
[email protected]
Fall 2011
National Tsing Hua University (NTHU)
Lecture 5, CS 631100
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
Introduction
Stabbing queries
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
Introduction
Motivation
Lecture 5, CS 631100
Segment Trees
Segment Trees
Lecture 5, CS 631100
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
Lecture 5, CS 631100
Segment Trees
Notations
Lecture 5, CS 631100
Segment Trees
Lecture 5, CS 631100
Segment Trees
Lecture 5, CS 631100
Segment Trees
Example
Lecture 5, CS 631100
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.
11
Segment Trees
Standard lists
Lecture 5, CS 631100
12
Segment Trees
Lecture 5, CS 631100
13
Segment Trees
Lecture 5, CS 631100
14
Segment Trees
Lecture 5, CS 631100
15
Segment Trees
Algorithm ConstructSegmentTree
Input: line segments S in IR2
Output: Segment tree T containing S.
1.
2.
3.
Lecture 5, CS 631100
16
Segment Trees
Inserting a segment s
Lecture 5, CS 631100
17
Segment Trees
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
18
Segment Trees
19
Segment Trees
Analysis
Lecture 5, CS 631100
20
Rectangle Intersection
Rectangle Intersection:
(An Application of Range and Segment Trees)
Lecture 5, CS 631100
21
Rectangle Intersection
Overview
Lecture 5, CS 631100
22
Rectangle Intersection
Example
Lecture 5, CS 631100
23
Rectangle Intersection
intersecting edges
reduces to intersection
reporting for isothetic
segments
Lecture 5, CS 631100
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 ;
Lecture 5, CS 631100
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.
Lecture 5, CS 631100
26
Rectangle Intersection
27
Rectangle Intersection
T0
s
s
s
s
28
Rectangle Intersection
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
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.
30
Interval Trees
Interval Trees
Lecture 5, CS 631100
31
Interval Trees
Introduction
Lecture 5, CS 631100
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
33
Interval Trees
Lecture 5, CS 631100
34
Interval Trees
Example
Lecture 5, CS 631100
35
Interval Trees
Stabbing query
If xq > xmed
Analogous, but on the right side.
Lecture 5, CS 631100
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
37
Lecture 5, CS 631100
38
Approach
Lecture 5, CS 631100
39
Performing queries
Lecture 5, CS 631100
40
Improvements
Lecture 5, CS 631100
41
Next Lecture
Next lecture:
Kd-Trees
2-dim. kd-trees
d-dim. kd-trees
Lecture 5, CS 631100
42