# 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.

## Presentation on theme: "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."— Presentation transcript:

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=[x 1,x 2 ] find all points in S that are in the interval. 24578121519 617

1-D solution Build a balanced tree using the points’ co-ordinates as the keys. 7 71915128245 2 4 5 8 15 24578121519 617 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=[x 1,x 2 ][y 1,y 2 ] find all points in S that are in the rectangle. P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7

Range Trees (2-D) P4P4 P3P3 P2P2 P1P1 P3P3 P2P2 P4P4 P1P1 P1P1 P2P2 P3P3 P4P4 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.) P4P4 P3P3 P2P2 P1P1 P3P3 P2P2 P4P4 P1P1 P1P1 P2P2 P3P3 P4P4 P8P8 P7P7 P6P6 P5P5 P5P5 P8P8 P6P6 P7P7

P4P4 P3P3 P2P2 P1P1 P3P3 P2P2 P4P4 P1P1 P1P1 P2P2 P3P3 P4P4 P8P8 P7P7 P6P6 P5P5 P5P5 P8P8 P6P6 P7P7

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(log 2 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 71915128245 2 4 5 8 15

P4P4 P3P3 P2P2 P1P1 P3P3 P2P2 P4P4 P1P1 P1P1 P2P2 P3P3 P4P4 P8P8 P7P7 P6P6 P5P5 P5P5 P8P8 P6P6 P7P7 Y

P4P4 P2P2 P1P1 P3P3 P2P2 P4P4 P1P1 P1P1 P2P2 P3P3 P4P4 P8P8 P7P7 P6P6 P5P5 P5P5 P8P8 P6P6 P7P7 Y P3P3 Y Y Y Y Y

May need to rebalance the primary tree by doing a rotation: y x B C x A y BC 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(log 2 n+k) Insert, delete : O(log 2 n) (amortized)

Priority search trees Suppose the query is unbouded from below, can we take advantage of this ? P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12

P1P1 A A P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 We put the lowest point at the root and the x-median (A) of the points other than the root P9P9 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 10 P 11 P 12

P1P1 A P4P4 B P7P7 P6P6 P8P8 P5P5 P2P2 P3P3 P 11 P 10 P9P9 How do we answer a query ? P 12 A P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 B

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 A P4P4 B P7P7 P6P6 P8P8 P5P5 P2P2 P3P3 P 11 P 10 P9P9 P 12

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 A P4P4 B P7P7 P6P6 P8P8 P5P5 P2P2 P3P3 P 11 P 10 P9P9 P 12 P4P4 P2P2 P6P6 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 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 First put the points at the leaves of a red-black tree

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P2P2 P3P3 P7P7 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P4P4 P6P6 P6P6 P5P5 P 10 P 11 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

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N N

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N N

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P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5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N N We may also need to rebalance the tree… Is this a problem ?

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N N M

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N N M M

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N N M M M

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N N M M M

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N N M M M

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N N M M M P

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 N M P P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N M M P P

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N N M M P P Need a point to put here P This is incorrect P4P4 P5P5 M P5P5 P

Rotation, rotations… B CA BC A X X YZ Z is bubbled down into B or C Y is bubbled up from A or B

P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P1P1 P4P4 P2P2 P3P3 P7P7 P6P6 P6P6 P1P1 P2P2 P3P3 P4P4 P5P5 P8P8 P6P6 P7P7 P9P9 P 10 P 11 P 12 P5P5 P 10 P 11 N N N M M P P P4P4 P5P5 M P5P5 M P4P4

Similar presentations