Finding Objects

37
March 2011 Finding objects 1 Finding Objects Shmuel Wimer Bar Ilan Univ., School of Engineering

description

Finding Objects. Shmuel Wimer Bar Ilan Univ., School of Engineering. Here is a floor (can also be space). Objects are located in the room. The Problem. What objects located here?. Need efficient way to store objects and apply queries. Outline. 1D range search 2D Kd-Tree 2D Range Tree - PowerPoint PPT Presentation

Transcript of Finding Objects

Page 1: Finding Objects

March 2011 Finding objects 1

Finding Objects

Shmuel Wimer

Bar Ilan Univ., School of Engineering

Page 2: Finding Objects

March 2011 Finding objects 2

Here is a floor (can also be space)

The Problem

Objects are located in the room

What objects located here?

Need efficient way to store objects and apply queries

Page 3: Finding Objects

March 2011 Finding objects 3

Outline

• 1D range search

• 2D Kd-Tree

• 2D Range Tree

• Accelerating 2D Query Time

• Layered Range Tree

Page 4: Finding Objects

March 2011 Finding objects 4

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

Page 5: Finding Objects

March 2011 Finding objects 5

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.

Page 6: Finding Objects

March 2011 Finding objects 6

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

Page 7: Finding Objects

March 2011 Finding objects 7

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

Page 8: Finding Objects

March 2011 Finding objects 8

ROOT T

split

left path

right path

Page 9: Finding Objects

March 2011 Finding objects 9

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 }

Page 10: Finding Objects

March 2011 Finding objects 10

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

Page 11: Finding Objects

March 2011 Finding objects 11

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

Page 12: Finding Objects

March 2011 Finding objects 12

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

Page 13: Finding Objects

March 2011 Finding objects 13

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

Page 14: Finding Objects

March 2011 Finding objects 14

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)

Page 15: Finding Objects

March 2011 Finding objects 15

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.

Page 16: Finding Objects

March 2011 Finding objects 16

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

Page 17: Finding Objects

March 2011 Finding objects 17

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

Page 18: Finding Objects

March 2011 Finding objects 18

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

Page 19: Finding Objects

March 2011 Finding objects 19

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

Page 20: Finding Objects

March 2011 Finding objects 20

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.

Page 21: Finding Objects

March 2011 Finding objects 21

p13p11 p12p3 p5p4

p1 p6p2

p8p7 p10p9

p3

p2

p4 p5 p12

p8p11

p7

p1

p13

p10

p9

p6

Page 22: Finding Objects

March 2011 Finding objects 22

( , ) { ( ) { 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

Page 23: Finding Objects

March 2011 Finding objects 23

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

Page 24: Finding Objects

March 2011 Finding objects 24

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.

Page 25: Finding Objects

March 2011 Finding objects 25

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.

Page 26: Finding Objects

March 2011 Finding objects 26

P

assocT

T

P

Page 27: Finding Objects

March 2011 Finding objects 27

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

Page 28: Finding Objects

March 2011 Finding objects 28

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)

Page 29: Finding Objects

March 2011 Finding objects 29

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.

Page 30: Finding Objects

March 2011 Finding objects 30

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

Page 31: Finding Objects

March 2011 Finding objects 31

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

Page 32: Finding Objects

March 2011 Finding objects 32

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 an array A1

S2 – subset of S1, ordered in an 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 does not exist. This is only O(k) time !

Page 33: Finding Objects

March 2011 Finding objects 33

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)

Page 34: Finding Objects

March 2011 Finding objects 34

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.

Page 35: Finding Objects

March 2011 Finding objects 35

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

Page 36: Finding Objects

March 2011 Finding objects 36

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 take place from A[V ] in right nodes of left path and left

nodes of right path. The smallest entry of [y`,y``] is found in constant

time by the pointer to parents propagating up to Vsplit.

Page 37: Finding Objects

March 2011 Finding objects 37

• Report time per node is O(1+kV), yielding a total of 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).