45
Orthogonal range searching

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

Embed Size (px)

Citation preview

Page 1: 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

Orthogonal range searching

Page 2: 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 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

Page 3: 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

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)

Page 4: 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 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

Page 5: 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

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

Page 6: 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

Range Trees (Contd.)

P4P3P2P1

P3

P2

P4

P1

P1

P2

P3

P4

P8P7P6P5

P5 P8

P6

P7

Page 7: 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

Range Trees (Contd.)

P4P3P2P1

P3

P2

P4

P1

P1

P2

P3

P4

P8P7P6P5

P5 P8

P6

P7

Page 8: 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

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

Page 9: 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

Analysis (2-D)

• Space O(nlog n)

• Query O(log2 n+k)

• Preprocessing O(nlog n)

Page 10: 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

Further facts

• Generalizes to d-dimensions

• Query time can be reduced using a technique called fractional cascading (we may talk about it later on)

Page 11: 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 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.

Page 12: 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

• 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

Page 13: 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

P4P3P2P1

P3

P2

P4

P1

P1

P2

P3

P4

P8P7P6P5

P5 P8

P6

P7

Y

Page 14: 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

P4P2P1

P3

P2

P4

P1

P1

P2

P3

P4

P8P7P6P5

P5 P8

P6

P7

Y

P3Y

Y

Y

Y

Y

Page 15: 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

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

Page 16: 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

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)

Page 17: 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

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

Page 18: 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

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

Page 19: 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

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

Page 20: 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

P1

P2

P3

P4

P5 P8

P6

P7

P9

P10P11P12

P1A

P4B

P7

P6 P8P5P2

P3 P11 P10P9P12

Page 21: 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

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

Page 22: 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

Priority search trees (analysis)

• query: O(log(n) + k)

• space: O(n)

• preprocessing: O(nlogn)

• How do we make them dynamic ?

Page 23: 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

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

Page 24: 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

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

Page 25: 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

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

Page 26: 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

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

Page 27: 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

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

Page 28: 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

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

Page 29: 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

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

Page 30: 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

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

Page 31: 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

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

Page 32: 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

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

Page 33: 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

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

Page 34: 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

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

Page 35: 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

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 ?

Page 36: 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

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

Page 37: 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

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

Page 38: 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

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

Page 39: 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

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

Page 40: 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

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

Page 41: 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

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

Page 42: 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

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

Page 43: 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

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

Page 44: 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

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

Page 45: 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

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