Upload
randy-swenson
View
244
Download
5
Embed Size (px)
Citation preview
Orthogonal range searching
The problem (1-D)
Given a set of points S on the line, preprocess them to build structure that allows efficient queries of the from:
Given an interval I=[x1,x2] find all points in S that are in the interval.
2 4 5 7 8 12 15 19
6 17
1-D solution
Build a balanced tree using the points’ co-ordinates as the keys.
7
7 19151282 4 5
2
4
5 8
12
15
2 4 5 7 8 12 15 19
6 17
query: O(log n+k)space: O(n)
The problem (2-D)
Given a set of points S on the plane, preprocess them to build structure that allows efficient queries of the from:
Given an rectangle R=[x1,x2][y1,y2] find all points in S that are in the rectangle.
P1
P2
P3
P4
P5 P8
P6
P7
Range Trees (2-D)
P4P3P2P1
P3
P2
P4
P1
P1
P2
P3
P4
Maintain the points in a balanced search tree ordered by x-coor. In each internal node maintain the points in its subtree in a balanced search tree ordered by y
Range Trees (Contd.)
P4P3P2P1
P3
P2
P4
P1
P1
P2
P3
P4
P8P7P6P5
P5 P8
P6
P7
Range Trees (Contd.)
P4P3P2P1
P3
P2
P4
P1
P1
P2
P3
P4
P8P7P6P5
P5 P8
P6
P7
Query processing
• Search by the first dimension gives us O(logn) trees which together contain the output.
• We search each of these trees to get the answer
Analysis (2-D)
• Space O(nlog n)
• Query O(log2 n+k)
• Preprocessing O(nlog n)
Further facts
• Generalizes to d-dimensions
• Query time can be reduced using a technique called fractional cascading (we may talk about it later on)
The dynamic case
• Suppose the set S is not fixed
• We want to be able to insert and delete points, and make queries intermixed with insertions and deletions.
• Easy in 1-D, just use a red-black tree or some other balance search tree
7
7 19151282 4 5
2
4
5 8
12
15
P4P3P2P1
P3
P2
P4
P1
P1
P2
P3
P4
P8P7P6P5
P5 P8
P6
P7
Y
P4P2P1
P3
P2
P4
P1
P1
P2
P3
P4
P8P7P6P5
P5 P8
P6
P7
Y
P3Y
Y
Y
Y
Y
May need to rebalance the primary tree by doing a rotation:
y
x
B
C
x
A y
B C
<===>
A
We have to rebuild the secondary data structures at x and y
Dynamic range trees (analysis)
• So we use BB(α) trees
• Then the amortized cost of rebalancing the primary tree is O(log n)
• and we get
Query : O(log2 n+k)
Insert, delete : O(log2 n) (amortized)
Priority search trees
Suppose the query is unbouded from below, can we take advantage of this ?
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11
P12
P1
A
A
P2P3
P4P5 P8
P6 P7We put the lowest point at the root and the x-median (A) of the points other than the root
P9
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11
P10P11
P12
P12
P1A
P4B
P7
P6 P8P5P2
P3 P11 P10P9
How do we answer a query ?
P12
AP1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
B
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1A
P4B
P7
P6 P8P5P2
P3 P11 P10P9P12
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1A
P4B
P7
P6 P8P5P2
P3 P11 P10P9P12
P4
P2 P6
You stop the search in the when you hit a point that is not in the answer
Priority search trees (analysis)
• query: O(log(n) + k)
• space: O(n)
• preprocessing: O(nlogn)
• How do we make them dynamic ?
Dynamic priority search trees
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1 P2 P3
P4 P5 P8P6 P7P9 P10P11
P12
First put the points at the leaves of a red-black tree
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P2
P3
P7
P1 P2 P3
P4 P5 P8P6 P7P9 P10P11
P12
P4
P6
P6
P5 P10P11
How does the insertion go ?
Top-down: At each internal node put the smallest point in its subtree not already assigned to an internal node
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4
P2
P3
P7
P6
P6
P1 P2 P3
P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4
P2
P3
P7
P6
P6
P1 P2
P3 P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
N
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4
P2
P3
P7
P6
P6
P1 P2
P3 P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4
P2
P3
P7
P6
P6
P1 P2
P3 P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4
P2
P3
P7
P6
P6
P1 P2
P3 P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4
P2
P3
P7
P6
P6
P1 P2
P3 P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4
P2
P3
P7
P6
P6
P1 P2
P3 P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4
P2
P3
P7
P6
P6
P1 P2
P3 P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4
P2
P3
P7
P6
P6
P1 P2
P3 P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4P2 P3
P7
P6
P6
P1 P2
P3 P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4P2 P3
P7
P6
P6
P1 P2
P3 P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
We may also need to rebalance the tree…
Is this a problem ?
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4P2 P3
P7
P6
P6
P1 P2
P3 P4 P5 P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
M
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4P2 P3
P7
P6
P6
P1 P2
P3 P4
P5
P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
M
M
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4P2 P3
P7
P6
P6
P1 P2
P3 P4
P5
P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
M
M
M
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4P2 P3
P7
P6
P6
P1 P2
P3 P4
P5
P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
M
M
M
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4P2 P3
P7
P6
P6
P1 P2
P3 P4
P5
P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
M
M
M
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4P2 P3
P7
P6
P6
P1 P2
P3 P4
P5
P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
M
M
M
P
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
N
M
P
P1
P4P2 P3
P7
P6
P6
P1 P2
P3 P4
P5
P8P6 P7P9 P10P11
P12P5 P10P11
N
N
M
M
P
P
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4P2 P3
P7
P6
P6
P1 P2
P3
P4 P5
P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
M
M
P
P
Need a point to put here
P
This is incorrect
P4
P5
M
P5P
Rotation, rotations…
B
C A
B C
<===>
A
X X
Y Z
Z is bubbled down into B or C
Y is bubbled up from A or B
P1
P2
P3
P4
P5 P8
P6
P7
P9
P10P11P12
P1
P4P2 P3
P7
P6
P6
P1 P2
P3
P4 P5
P8P6 P7P9 P10P11
P12P5 P10P11
N
N
N
M
M
P
P
P4
P5
M
P5
MP4