range search algorithms
DESCRIPTION
Range Search Algorithms. Shmuel Wimer Bar Ilan Univ. Eng. Faculty Technion, EE Faculty. VLSI designer is drawing schematics or designing layout. He / she wishes to focus on a small portion of data by zooming in. How to find efficiently the objects of interest?. How to store the objects?. - PowerPoint PPT PresentationTRANSCRIPT
May 2012 Range Search Algorithms 1
Range Search Algorithms
Shmuel WimerBar Ilan Univ. Eng. Faculty
Technion, EE Faculty
May 2012 Range Search Algorithms 2
VLSI designer is drawing schematics or designing layout
He / she wishes to focus on a small portion of data by zooming in
How to find efficiently the objects of interest?
How to store the objects?
How to tradeoff Memory size and query time?
May 2012 Range Search Algorithms 3
A victim signal found to fail in silicon due to noise.
We’d like to find the aggressors in layout.
May 2012 Range Search Algorithms 4
Outline
• 1D range search
• 2D Kd-Tree
• 2D Range Tree
• Accelerating 2D Query Time
• Layered Range Tree
May 2012 Range Search Algorithms 5
1D Range Tree
59 62 70 80
89
100 1053 10 19 23 30 37
49
10 37 62 89
23 80
49
3 19 30 59 70 100
May 2012 Range Search Algorithms 6
1D Range Searching
1 2, , , is a set of points on the real line.nP p p p
is stored in a balanced binary search tree .P T
The leaves of store the points.T
The internal nodes of store the split points, used later
to direct the range search. The point at internal node is
the largest in node's left sub-tree.
T
Static point set could be stored in array, yielding same
performance. Array however cannot be generalized to
higher dimentional range seach, while tree can.
May 2012 Range Search Algorithms 7
59 62 70 80
89
100 1053 10 19 23 30 37
49
10 37 62 89
23 80
49
3 19 30 59 70 100
searching in range , 18,77x x
searching paths
reported points
May 2012 Range Search Algorithms 8
Search starts at the root downwards, defining a path,
possibly empty, until first directive at a node called
, falls in rang. split node
, , {
;
while is not a leaf and , {
if { }
else { }
}
return ;
}
node tree T range x x
root T
x x x
x x LSON
RSON
FindSplitPoint
May 2012 Range Search Algorithms 9
ROOT T
split
left path
right path
May 2012 Range Search Algorithms 10
split
split split
( , , ) { ( , , ) ;
( is a leaf ) { ( , ) { report point
// follow left path dow
}}
{
n to
void tree T range x xT x x
if if x x x
else x
1D_RangeQueryFindSplitPoint
split ;
( is not a leaf ) { ( ) { ( ) ; ; } { } } ( ) { re
LSON
whileif x x RSON
LSONelse RSON
if x x
ReportSubtree
// similar
port point }
to right pa }
th }
May 2012 Range Search Algorithms 11
Correctness of Query Algorithm
Let a point be reported. We'll show it must satisfy
, .
p
x p x x
split
split
If was reported from
it happned along left path from left subtree of ,
thus implying .
p RSON
x p x x
ReportSubtree
If it resides in either or it was checked explicitly
against , .x x
May 2012 Range Search Algorithms 12
Reporting from is similar.LSON ReportSubtree
On the other hand, report from implies that
the path went left at and was reported from right
subtree. Therefore .
RSON
p
x x x p
In conclusion, .x x p x
May 2012 Range Search Algorithms 13
Conversely, let , be stored in a leaf
but not reported. Let be the lowest ancestor
of that was visited by the query algorithm.
as otherwise would have been reported.
x p x x
p
could not be visited by
otherwise would have been reported.
ReportSubtree,
Assume resides on left search path to . Then
search path must go right at .
x
May 2012 Range Search Algorithms 14
If resides on right search path to , search path
at must go left, as otherwise wasn't the lowest
ancestor of .
x
Otherwise wasn't the lowest ancestor of . But
then , which contradics , .x p x x p x x
But then , which contradics , .x p x x p x x
May 2012 Range Search Algorithms 15
2D Kd-Tree
observation:
, , , if
, and ,
p x p y p x x y y
x p x x y p y y
Key idea:
splitting points in alternating directions
PrightPleft
Pbottom
Ptop
x’’
y’’
x’
y’
py(p)
x(p)
May 2012 Range Search Algorithms 16
l1
l2l3
l4
l5l7
l6
l8
l9
l4 l5 l6 l7
l2 l3
l1
p3
p2
p4 p5 p9
p7p8
p6
p1
p10
l8 p3 p5 p10p4 p8 p9l9
p1 p6 p7p2
Vertical and horizontal splits are alternating. Points on split lines
belong to lower left regions. Split ends when region contains one
point. We assume without loss of generality that all coordinates are
distinct.
May 2012 Range Search Algorithms 17
mid 1 mid
( , ) {
( | | 1 ) { return a leaf containing }
else {
// apply vertical spli ( is even ) {
find , | ,
t at even depth
node points P int d
if P p P
if d
p P P p P x p x p
Build_Kd_Tree
2 mid
1 mid 2 mid
1
// apply horizontal split at odd
| ;
}
{
find , | , | ;
}
d
( , 1) ;
th
e
(
p
mid
left right
P p P x p x p
else
p P P p P y p y p P p P y p y p
P d
Build_Kd_Tree Build_Kd_Tree
2
mid left right
, 1) ;
allocate node , attach ; = ; = ;
return ;
}
}
P d
l p LSON RSON
May 2012 Range Search Algorithms 18
Construction Time and Storage
1 2 1
1 split
Assume | | | | . Median must be in , located at
position | | 2 . Iterate by arbitrairly picking
and then examining the size of the resulting subset.
P P P
P n p
Most expensive is median finding. It can be done in
linear expected time by a probabilistoc split.
1 2
1
split
split split
Pick arbitrarily a split point .
Let | and | .
If | | 2 we are done.
p P
P p P p p P p P p p
P n
May 2012 Range Search Algorithms 19
1 , if 1,Construction time:
2 2 , if 1,
which solves to log .
Storage is .
O nT n
O n T n n
O n n
O n
Another option is pre sorting in both and , and
then maintaining pointers between the two lists.
x y
Initial sort takes log , but any later alternating
split takes totally linear time at each level of the tree.
O n n
May 2012 Range Search Algorithms 20
Querying
Every internal node of Kd-tree stores a region of xy
plane defined by the path to root. Regions are defined
in O(1) time per node at construction.
l1
l2
l3region (v)
v
l1
l2
l3
May 2012 Range Search Algorithms 21
If node’s region is disjoint to a query rectangle, no point
in node’s sub-tree satisfies the query.
l1
l2
l3region (v)
v
l1
l2
l3
If query rectangle contains node’s region all points in
node’s sub-tree satisfy the query. Otherwise search
must proceed.
May 2012 Range Search Algorithms 22
p13p11 p12p3 p5p4
p1 p6p2
p8p7 p10p9
p3
p2
p4 p5 p12
p8p11
p7
p1
p13
p10
p9
p6
May 2012 Range Search Algorithms 23
( , ) { ( ) { report } { ( )
{ ( )}
( )
{ ( , )}
void node region Rif p R pelse
if region LSON R
LSON
else if region LSON R
LSON R
Search_Kd_Tree
ReportSubtree
Search_Kd_Tree
( )
{ ( )}
( )
{ ( , )} }}
if region RSON R
RSON
else if region RSON R
RSON R
ReportSubtree
Search_Kd_Tree
May 2012 Range Search Algorithms 24
When , is reported, contributing
time to report.
region v R T v O k
is the amount of regions intersecting with infinite
line. Number of regions intersecting but not contained
in is bounded by 4 .
Q n
R
R Q n
is satisfying
1 if 1 ,
2 2 4 if 1
solved to .
Q n
O nQ n
Q n n
Q n O n
May 2012 Range Search Algorithms 25
Reminder of 1D Range Tree
split
Objects are stored in leaves of balanced binary tree. Internal nodes store search directives.
Starts search at root until node’s key falls in range.
Left path is issued from forking node down to left end of range. All right sub-trees are reported.
Similar for right path.
May 2012 Range Search Algorithms 26
2D Range Tree
[x`,x``] × [y`,y``] is a range query.
In 1D range tree P ( V ), the points stored at leaves of T(V), is called the canonical subset of V.
P (root) is all points, P (leaf) is a single point.
In 1D range tree points in [x`,x``] are obtained from O (log n) disjoint sub-trees (right sub-trees of left path and left sub-trees of right path). Hence [y`, y``] query further looks into P (V).
Construct 2-level data structure:
Binary search tree T built on x-coordinate of P.
For any node V store P (V) in an associated binary tree Tassoc (V) built on y-coordinate of points.
May 2012 Range Search Algorithms 27
P
assocT
T
P
May 2012 Range Search Algorithms 28
assoc
left right mid
left
( ) {
allocate node ; construct ( ) for ;
if ( | | 1) { define as leaf }
else {
split into and by - coordinate at ;
node points P
T P
P
P P P x x
Build_2D_RangeTree
left
right right
mid
left right
= ( ) ;
= ( ) ;
store at ;
= ; = ;
}
return ;
}
P
P
x
LSON RSON
Build_2D_RangeTree
Build_2D_RangeTree
May 2012 Range Search Algorithms 29
Storage and Construction Time
p
p
p
p
A point p is stored in log n
associated trees.
The size of associated tree
(binary) is linear in number
of stored points
Associated trees at a
level of primary tree are
disjoint, hence total
storage consumed at a
level is O(n)
May 2012 Range Search Algorithms 30
2D range tree requires O(nlogn) storage
Construction described in Build_2D_RangeTree is not
efficient as it takes O(nlogn) time to build the associated
trees, thus resulting in O(nlog2n) time.
Total construction time can be reduced to O(nlogn) by
pre-sorting of the points by y-coordinate and then building
the 2D range tree bottom-up rather than top-down.
May 2012 Range Search Algorithms 31
split
split split
split
( , [ , ] [ , ] ) { ( , [ , ]) ;
( is a leaf) { ( [ , ] [ , ] ) { report point }}
{ (
void tree T range x x y yT x x
if if p x x y y
elseLSON
2D_RangeQueryFindSplitNode
assoc
) ; ( is not a leaf) { ( ) { ( ( ( )), [ , ]) ; ( ) ; } { ( )
// maintain left path down to l
}
eafwhile
if x xT RSON y y LSON
else RSON
1D_RangeQuery
}
( [ , ] [ , ] ) { report point }; // maintain similarily right path down to leaf }}
if p x x y y
May 2012 Range Search Algorithms 32
Query Time
Left and right path in primary tree have log nodes.T O n
Each node decides in constant time where to continue the path.
Function _ spends a total of log time.O n k1D RangeQuery
Function _ is called along left and right paths,
spending a total of log time.O n k 1D RangeQuery
2log log log .O n k O n O k O n k
2_ spends totaly log time.O n k2D RangeQuery
May 2012 Range Search Algorithms 33
Accelerating 2D Query Time
3 10 19 23 30 37 59 62 70 80 100 105A1
10 19 30 62 70 80 100A2
S1 – set of objects ordered in array A1
S2 – subset of S1, ordered in array A2
Reporting objects of S1 in range 20 to 75
Find 23 by binary search, then traverse and report until 70 in O(logn+k) time
Reporting objects from S2 can save binary search !
Every object in A1 points to the smallest object in A2 larger or equal to it.
Use NIL if such one not exists. This is only O(k) time !
May 2012 Range Search Algorithms 34
21
49
33
30
41
95
52
23
58
59
67
89
93
70
2
19
5
80
7
10
8
37
12
3
15
99
17
62
5 15 33 58
8 52
17
Observation: The canonical sub-set P(LSON[V])
and the canonical sub-set P(RSON[V])
2 7 12 21 41 67
are canonical sub-set of P(V)
May 2012 Range Search Algorithms 35
Implementation
• T is a range tree of P of n points.
• P (V) is canonical point set stored in leaves of T(V)
• Instead of storing P(V) in associated tree sorted by y-coordinate, it
is stored in an array A(V) sorted by y-coordinate.
• Each entry in A(V) maintains two pointers:
– One to an entry in A(LSON[V]) (the smallest equal or larger)
– One to an entry in A(RSON [V]) (the smallest equal or larger)
• Reporting from node V is done directly from A(V) rather than by T(V)
traversal.
May 2012 Range Search Algorithms 36
Layered Range Tree
23 30 5949 70 89 953 10 19 37 62 80 99
10 19 37 80 3 62 99 59 70 8923 30 49 95
10 3719 80 623 99 30 49 23 95 59 70 89
3 10 2319 30 37 5949 62 70 8980 95 99
19 80 3710 3 99 3049 95 23 89 70
May 2012 Range Search Algorithms 37
Performance of Layered Range Tree
• [x`,x``]×[y`,y``] is a range query.
• Perform x-range search on primary tree T to determine left and right
paths down to leaves x` and x`` in O(logn) time. Points of canonical
subsets adhere [x`,x``]. This takes O(logn).
• We find in A[Vsplit] the smallest entry of [y`,y``] in O(logn) time.
• Reports are made from A[V ] in right nodes of left path and left node
of right path. The smallest entry of [y`,y``] is found in constant time
by the pointer to parents propagating up to Vsplit.
May 2012 Range Search Algorithms 38
• Report time per node is O(1+kV), yielding total O(logn+k) compared
to O (log2n+k) in 2D range tree.
• Storage is O(nlogn) same as 2D range tree.
• Construction time is O(nlogn) as in 2D range tree.
– Sort initially all objects by y-coordinate stored in an array with pointers
to objects in primary binary tree T.
– Every split to left and right sub-trees in T is followed by linear traversal
of A (Vparent) and split into sorted A(Vleft) and A (Vright) according to the
split made by x-coordinate in T.
– Total work per level of T to construct all A(V) is O(n).