Orthogonal Range Searching

187
Advanced Algorithms – COMS31900 Orthogonal Range Searching Benjamin Sach

Transcript of Orthogonal Range Searching

Page 1: Orthogonal Range Searching

Advanced Algorithms – COMS31900

Orthogonal Range Searching

Benjamin Sach

Page 2: Orthogonal Range Searching

Orthogonal range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

Page 3: Orthogonal Range Searching

Orthogonal range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

Page 4: Orthogonal Range Searching

Orthogonal range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

n points in 2D space

|U |

|U |

The universe

Page 5: Orthogonal Range Searching

Orthogonal range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

Page 6: Orthogonal Range Searching

Orthogonal range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

Page 7: Orthogonal Range Searching

Orthogonal range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Page 8: Orthogonal Range Searching

Orthogonal range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Page 9: Orthogonal Range Searching

Orthogonal range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

find all employees aged between 21 and 48

with salaries between £23k and £36k

A classic database query

“”

Page 10: Orthogonal Range Searching

Orthogonal range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

find all employees aged between 21 and 48

with salaries between £23k and £36k

A classic database query

x1 = 23 x2 = 36

y1 = 21

y2 = 48

“”

Page 11: Orthogonal Range Searching

Orthogonal range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

Page 12: Orthogonal Range Searching

Orthogonal range searching

� A d-dimensional range searching data structure stores n distinct points

for d = 1, the lookup(x1, x2) operation

returns every point with x1 6 x 6 x2.

each point has d coordinates

for d = 3, the lookup(x1, x2, y1, y2, z1, z2) operation

returns every point with

for d = 2, the lookup(x1, x2, y1, y2) operation

returns every point with

x1 6 x 6 x2 and y1 6 y 6 y2.

(we assume d is a constant)

x1 6 x 6 x2,

y1 6 y 6 y2 and

z1 6 z 6 z2.

x1 x2

y1

y2

x1 x2

z

x y

Page 13: Orthogonal Range Searching

Orthogonal range searching

� A d-dimensional range searching data structure stores n distinct points

for d = 1, the lookup(x1, x2) operation

returns every point with x1 6 x 6 x2.

each point has d coordinates

for d = 3, the lookup(x1, x2, y1, y2, z1, z2) operation

returns every point with

for d = 2, the lookup(x1, x2, y1, y2) operation

returns every point with

x1 6 x 6 x2 and y1 6 y 6 y2.

(we assume d is a constant)

x1 6 x 6 x2,

y1 6 y 6 y2 and

z1 6 z 6 z2.

x1 x2

y1

y2

x1 x2

z

x y

Page 14: Orthogonal Range Searching

Orthogonal range searching

� A d-dimensional range searching data structure stores n distinct points

for d = 1, the lookup(x1, x2) operation

returns every point with x1 6 x 6 x2.

each point has d coordinates

for d = 3, the lookup(x1, x2, y1, y2, z1, z2) operation

returns every point with

for d = 2, the lookup(x1, x2, y1, y2) operation

returns every point with

x1 6 x 6 x2 and y1 6 y 6 y2.

(we assume d is a constant)

x1 6 x 6 x2,

y1 6 y 6 y2 and

z1 6 z 6 z2.

x1 x2

y1

y2

x1 x2

z

x y

Page 15: Orthogonal Range Searching

Starting simple. . . 1D range searching

Page 16: Orthogonal Range Searching

Starting simple. . . 1D range searching

preprocess n points on a line

Page 17: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

preprocess n points on a line

lookup(x1, x2) should return all points between x1 and x2

Page 18: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Page 19: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

3 7 11 19 23 27 35 43 53 61 67

Page 20: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

x1 = 15 x2 = 64

Page 21: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

Page 22: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

Page 23: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

Page 24: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

(i.e. the closest point to the right)

Page 25: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

(i.e. the closest point to the right)

find the successor of x1 by binary search and then ‘walk’ right

to perform lookup(x1, x2). . .

Page 26: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

15 < 27

Page 27: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

15 < 27

Page 28: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

15 > 11

Page 29: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

15 > 11

Page 30: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

15 < 19

Page 31: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

15 < 19

Page 32: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

Page 33: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

Page 34: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

Page 35: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

Page 36: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

Page 37: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

Page 38: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

Page 39: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

67 > 64 = x2

Page 40: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

Page 41: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

k

lookups take O(logn+ k) time (k is the number of points reported)

3

Page 42: Orthogonal Range Searching

Starting simple. . . 1D range searching

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

n

x1 = 15 x2 = 64

build a sorted array containing the x-coordinates

in O(n logn) preprocessing (prep.) time

and O(n) space

to perform lookup(x1, x2). . .

find the successor of x1 by binary search and then ‘walk’ right

k

lookups take O(logn+ k) time (k is the number of points reported)

3

this is called being ‘output sensitive’

Page 43: Orthogonal Range Searching

Starting simple. . . 1D range searching

Page 44: Orthogonal Range Searching

Starting simple. . . 1D range searching

alternatively we could build a balanced tree. . .

Page 45: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

Page 46: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

half the points are to the left half the points are to the right

Page 47: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

Page 48: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half

Page 49: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half

Page 50: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half

Page 51: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half(in a tie, pick the left)

Page 52: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half(in a tie, pick the left)

Page 53: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half(in a tie, pick the left)

Page 54: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half(in a tie, pick the left)

Page 55: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half(in a tie, pick the left)

We can store the tree in O(n) space (it has one node per point)

Page 56: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half(in a tie, pick the left)

We can store the tree in O(n) space (it has one node per point)

It has O(logn) depth

Page 57: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half(in a tie, pick the left)

We can store the tree in O(n) space (it has one node per point)

It has O(logn) depth

O(logn)

Page 58: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half(in a tie, pick the left)

We can store the tree in O(n) space (it has one node per point)

It has O(logn) depth and can be built in O(n logn) time

O(logn)

Page 59: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half(in a tie, pick the left)

We can store the tree in O(n) space (it has one node per point)

It has O(logn) depth and can be built in O(n logn) time

O(logn)

Page 60: Orthogonal Range Searching

Starting simple. . . 1D range searching

find the point in the middle

alternatively we could build a balanced tree. . .

. . . and recurse on each half(in a tie, pick the left)

We can store the tree in O(n) space (it has one node per point)

It has O(logn) depth (O(n) time if the points are sorted)and can be built in O(n logn) time

O(logn)

Page 61: Orthogonal Range Searching

Starting simple. . . 1D range searching

how do we do a lookup?

Page 62: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

how do we do a lookup?

Page 63: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

how do we do a lookup?

Page 64: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

how do we do a lookup?

Page 65: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

how do we do a lookup?x1 is to the left

Page 66: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

how do we do a lookup?

x1 is to the right

Page 67: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

how do we do a lookup?

Page 68: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1 in O(logn) time

how do we do a lookup?

Page 69: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

Page 70: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

Page 71: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

Page 72: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

Page 73: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

Page 74: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

Page 75: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

Page 76: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

Page 77: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

off-path edge

Page 78: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 79: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 80: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 81: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 82: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edgeoff-path edge

Page 83: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edgeoff-path edge

Page 84: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edgeoff-path edge

Page 85: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 86: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 87: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 88: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 89: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 90: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 91: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 92: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

off-path edge

Page 93: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

after the split

off-path edge

Page 94: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

Step 1: find the successor of x1

Step 2: find the predecessor of x2

in O(logn) time

how do we do a lookup?

in O(logn) time

which points in the tree should we output?

look at any node on the path

this is called an

“it’s all or

nothing”

after the split

those in the O(logn) selected subtrees on the path

off-path edge

Page 95: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

how do we do a lookup?look at any node on the path

this is called an

“it’s all or

nothing”

after the split

off-path edge

Page 96: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

how do we do a lookup?look at any node on the path

this is called an

“it’s all or

nothing”

after the split

lookups take O(logn+ k) time (k is the number of points reported)as before

off-path edge

Page 97: Orthogonal Range Searching

Starting simple. . . 1D range searching

x1 x2

how do we do a lookup?look at any node on the path

this is called an

“it’s all or

nothing”

after the split

lookups take O(logn+ k) time (k is the number of points reported)as before

so what have we gained?

off-path edge

Page 98: Orthogonal Range Searching

Subtree decomposition root

split

x1 x2

Warning: the root to split path isn’t to scale

off-path edge

off-path subtree

too small

too small

too big

Page 99: Orthogonal Range Searching

Subtree decomposition root

split

x1 x2

Warning: the root to split path isn’t to scale

after the paths to x1 and x2 split. . .

off-path edge

off-path subtree

too small

too small

too big

Page 100: Orthogonal Range Searching

Subtree decomposition root

split

x1 x2

Warning: the root to split path isn’t to scale

after the paths to x1 and x2 split. . .

any off-path subtree is either in or out

off-path edge

off-path subtree

too small

too small

too big

Page 101: Orthogonal Range Searching

Subtree decomposition root

split

x1 x2

Warning: the root to split path isn’t to scale

after the paths to x1 and x2 split. . .

any off-path subtree is either in or outi.e. every point in the subtree has x1 6 x 6 x2 or none has

off-path edge

off-path subtree

too small

too small

too big

Page 102: Orthogonal Range Searching

Subtree decomposition root

split

x1 x2

Warning: the root to split path isn’t to scale

after the paths to x1 and x2 split. . .

any off-path subtree is either in or outi.e. every point in the subtree has x1 6 x 6 x2 or none has

off-path edge

off-path subtree

too small

too small

too big

this will be useful for 2D range searching

Page 103: Orthogonal Range Searching

1D range searching summary

O(n logn) prep time

O(n) space

O(logn+ k) lookup time

where k is the number of points reported

(this is known as being output sensitive)

x1 x2

preprocess n points on a line

lookup(x1, x2) should report all points between x1 and x2

Page 104: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Page 105: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

Page 106: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

Page 107: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

Page 108: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

Page 109: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

Page 110: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

Page 111: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

• Find all the points in both lists

Page 112: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

• Find all the points in both lists

Page 113: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

• Find all the points in both lists

How long does this take?

Page 114: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

• Find all the points in both lists

How long does this take?

O(logn+ k) +O(logn+ k) +O(k)

Page 115: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

• Find all the points in both lists

How long does this take?

O(logn+ k) +O(logn+ k) +O(k)

= O(logn+ k)

Page 116: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

• Find all the points in both lists

How long does this take?

O(logn+ k) +O(logn+ k) +O(k)

= O(logn+ k)

Page 117: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

• Find all the points in both lists

How long does this take?

O(logn+ k) +O(logn+ k) +O(k)

= O(logn+ k)

???

Page 118: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

• Find all the points in both lists

How long does this take?

O(logn+ k) +O(logn+ k) +O(k)

= O(logn+ k)

???

Page 119: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

• Find all the points in both lists

How long does this take?

O(logn+ kx) +O(logn+ ky) +O(kx + ky)

= O(logn+ kx + ky)

here kx is the number of points with x1 6 x 6 x2 (respectively for ky )

Page 120: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Attempt one:

• Find all the points with x1 6 x 6 x2

• Find all the points with y1 6 y 6 y2

• Find all the points in both lists

How long does this take?

O(logn+ kx) +O(logn+ ky) +O(kx + ky)

= O(logn+ kx + ky)

here kx is the number of points with x1 6 x 6 x2 (respectively for ky )

these could be huge in comparison with k

Page 121: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Page 122: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

how can we do better?

Page 123: Orthogonal Range Searching

Subtree decomposition in 2D root

split

x1 x2

Warning: the root to split path isn’t to scale

off-path edge

off-path subtree

x too small

x too big

(during preprocessing) build a balanced binary tree using the x-coordinates

Page 124: Orthogonal Range Searching

Subtree decomposition in 2D root

split

x1 x2

Warning: the root to split path isn’t to scale

to perform a lookup(x1, x2, y1, y2) follow the paths to x1 and x2 as before

off-path edge

off-path subtree

x too small

x too big

(during preprocessing) build a balanced binary tree using the x-coordinates

Page 125: Orthogonal Range Searching

Subtree decomposition in 2D root

split

x1 x2

Warning: the root to split path isn’t to scale

to perform a lookup(x1, x2, y1, y2) follow the paths to x1 and x2 as before

off-path edge

off-path subtree

x too small

x too big

(during preprocessing) build a balanced binary tree using the x-coordinates

for any off-path subtree. . .every point in the subtree has x1 6 x 6 x2 or no point has

Page 126: Orthogonal Range Searching

Subtree decomposition in 2D root

split

x1 x2

Warning: the root to split path isn’t to scale

to perform a lookup(x1, x2, y1, y2) follow the paths to x1 and x2 as before

off-path edge

off-path subtree

x too small

x too big

(during preprocessing) build a balanced binary tree using the x-coordinates

for any off-path subtree. . .every point in the subtree has x1 6 x 6 x2 or no point has

Idea: filter these subtrees by y-coordinate

Page 127: Orthogonal Range Searching

Subtree decomposition in 2D

to perform a lookup(x1, x2, y1, y2) follow the paths to x1 and x2 as before

(during preprocessing) build a balanced binary tree using the x-coordinates

for any off-path subtree. . .every point in the subtree has x1 6 x 6 x2 or no point has

Idea: filter these subtrees by y-coordinate

Page 128: Orthogonal Range Searching

Subtree decomposition in 2D

to perform a lookup(x1, x2, y1, y2) follow the paths to x1 and x2 as before

(during preprocessing) build a balanced binary tree using the x-coordinates

for any off-path subtree. . .every point in the subtree has x1 6 x 6 x2 or no point has

Idea: filter these subtrees by y-coordinate

we want to find all points in here with y1 6 y 6 y2(they all have x1 6 x 6 x2)

Page 129: Orthogonal Range Searching

Subtree decomposition in 2D

to perform a lookup(x1, x2, y1, y2) follow the paths to x1 and x2 as before

(during preprocessing) build a balanced binary tree using the x-coordinates

for any off-path subtree. . .every point in the subtree has x1 6 x 6 x2 or no point has

Idea: filter these subtrees by y-coordinate

we want to find all points in here with y1 6 y 6 y2(they all have x1 6 x 6 x2)

how?

Page 130: Orthogonal Range Searching

Subtree decomposition in 2D

to perform a lookup(x1, x2, y1, y2) follow the paths to x1 and x2 as before

(during preprocessing) build a balanced binary tree using the x-coordinates

for any off-path subtree. . .every point in the subtree has x1 6 x 6 x2 or no point has

Idea: filter these subtrees by y-coordinate

we want to find all points in here with y1 6 y 6 y2(they all have x1 6 x 6 x2)

how?

build a 1D range searching structure at every node

(during preprocessing)on the y-coordinates of the points in the subtree

Page 131: Orthogonal Range Searching

Subtree decomposition in 2D

to perform a lookup(x1, x2, y1, y2) follow the paths to x1 and x2 as before

(during preprocessing) build a balanced binary tree using the x-coordinates

for any off-path subtree. . .every point in the subtree has x1 6 x 6 x2 or no point has

Idea: filter these subtrees by y-coordinate

we want to find all points in here with y1 6 y 6 y2(they all have x1 6 x 6 x2)

how?

build a 1D range searching structure at every node

(during preprocessing)on the y-coordinates of the points in the subtree

a 1D lookup takes O(logn+ k′) time

k′

Page 132: Orthogonal Range Searching

Subtree decomposition in 2D

to perform a lookup(x1, x2, y1, y2) follow the paths to x1 and x2 as before

(during preprocessing) build a balanced binary tree using the x-coordinates

for any off-path subtree. . .every point in the subtree has x1 6 x 6 x2 or no point has

Idea: filter these subtrees by y-coordinate

we want to find all points in here with y1 6 y 6 y2(they all have x1 6 x 6 x2)

how?

build a 1D range searching structure at every node

(during preprocessing)on the y-coordinates of the points in the subtree

a 1D lookup takes O(logn+ k′) time

k′and only returns points we want

Page 133: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

x1 x2

Page 134: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

x1 x2

Page 135: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2 (inspecting the points on the path as you go)

x1 x2

Page 136: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

(inspecting the points on the path as you go)

x1 x2

Page 137: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

(inspecting the points on the path as you go)

x1 x2

Page 138: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

x1 x2

Page 139: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

perform lookup(y1, y2) on the

points in this subtree

x1 x2

Page 140: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

perform lookup(y1, y2) on the

points in this subtree

x1 x2

Page 141: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

perform lookup(y1, y2) on the

points in this subtree

x1 x2

Page 142: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

perform lookup(y1, y2) on the

points in this subtree

x1 x2

Page 143: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

perform lookup(y1, y2) on the

points in this subtree

x1 x2

Page 144: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

How long does a query take?

x1 x2

Page 145: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

How long does a query take?

The paths have length O(logn)

x1 x2

Page 146: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

How long does a query take?

The paths have length O(logn)

So steps 1. and 2. take O(logn) time

x1 x2

Page 147: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

How long does a query take?

The paths have length O(logn)

So steps 1. and 2. take O(logn) time

As for step 3,

x1 x2

Page 148: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

How long does a query take?

The paths have length O(logn)

So steps 1. and 2. take O(logn) time

As for step 3,

We do O(logn) 1D lookups. . .

x1 x2

Page 149: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

How long does a query take?

The paths have length O(logn)

So steps 1. and 2. take O(logn) time

As for step 3,

We do O(logn) 1D lookups. . .

Each takes O(logn+ k′) time x1 x2

Page 150: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

How long does a query take?

The paths have length O(logn)

So steps 1. and 2. take O(logn) time

As for step 3,

We do O(logn) 1D lookups. . .

Each takes O(logn+ k′) time

This sums to. . .

O(log2 n+ k)

x1 x2

Page 151: Orthogonal Range Searching

Subtree decomposition in 2D

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .

use the 1D range structure for that subtreeto filter the y coordinates

(inspecting the points on the path as you go)

How long does a query take?

The paths have length O(logn)

So steps 1. and 2. take O(logn) time

As for step 3,

We do O(logn) 1D lookups. . .

Each takes O(logn+ k′) time

This sums to. . .

O(log2 n+ k)

because the 1D lookups are disjoint

x1 x2

Page 152: Orthogonal Range Searching

Space Usage

at each node we store an array

the array is sorted by y coordinate

(this gives us a 1D range data structure)

How much space does our 2D range structure use?

containing the points in its subtree

the original (1D) structure used O(n) space. . .

but we added some stuff

Page 153: Orthogonal Range Searching

Space Usage

at each node we store an array

the array is sorted by y coordinate

(this gives us a 1D range data structure)

How much space does our 2D range structure use?

containing the points in its subtree

the original (1D) structure used O(n) space. . .

but we added some stuff

look at any level in the treei.e. all nodes at the same distance from the root

Page 154: Orthogonal Range Searching

Space Usage

at each node we store an array

the array is sorted by y coordinate

(this gives us a 1D range data structure)

How much space does our 2D range structure use?

containing the points in its subtree

the original (1D) structure used O(n) space. . .

but we added some stuff

look at any level in the treei.e. all nodes at the same distance from the root

the points in these subtrees are disjoint

Page 155: Orthogonal Range Searching

Space Usage

at each node we store an array

the array is sorted by y coordinate

(this gives us a 1D range data structure)

How much space does our 2D range structure use?

containing the points in its subtree

the original (1D) structure used O(n) space. . .

but we added some stuff

look at any level in the treei.e. all nodes at the same distance from the root

the points in these subtrees are disjoint

so the sizes of the arrays add up to n

Page 156: Orthogonal Range Searching

Space Usage

at each node we store an array

the array is sorted by y coordinate

(this gives us a 1D range data structure)

How much space does our 2D range structure use?

containing the points in its subtree

the original (1D) structure used O(n) space. . .

but we added some stuff

look at any level in the treei.e. all nodes at the same distance from the root

the points in these subtrees are disjoint

so the sizes of the arrays add up to n

As the tree has depth O(logn). . .

Page 157: Orthogonal Range Searching

Space Usage

at each node we store an array

the array is sorted by y coordinate

(this gives us a 1D range data structure)

How much space does our 2D range structure use?

containing the points in its subtree

the original (1D) structure used O(n) space. . .

but we added some stuff

look at any level in the treei.e. all nodes at the same distance from the root

the points in these subtrees are disjoint

so the sizes of the arrays add up to n

As the tree has depth O(logn). . .

the total space used is O(n logn)

Page 158: Orthogonal Range Searching

Preprocessing time

How long does it take to build the arrays at the nodes?

How much prep time does our 2D range structure take?

the original (1D) structure used O(n logn) prep time. . .

but we added some stuff

Page 159: Orthogonal Range Searching

Preprocessing time

How long does it take to build the arrays at the nodes?

How much prep time does our 2D range structure take?

the original (1D) structure used O(n logn) prep time. . .

but we added some stuff

Page 160: Orthogonal Range Searching

Preprocessing time

How long does it take to build the arrays at the nodes?

How much prep time does our 2D range structure take?

the original (1D) structure used O(n logn) prep time. . .

but we added some stuff

is just merged with

`

as and are already sorted,

merging them takes O(`) time

Therefore the total time is O(n logn)

(which is the sum of the lengths of the arrays)

Page 161: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Summary

O(n logn) prep time

O(n logn) space

O(log2 n+ k) lookup time

where k is the number of points reported

Page 162: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Summary

O(n logn) prep time

O(n logn) space

O(log2 n+ k) lookup time

where k is the number of points reported

actually we can improve this :)

Page 163: Orthogonal Range Searching

Improving the query time

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

y1 = 15 y2 = 64

when we do a 2D look-up we do O(logn) 1D lookups. . .

all with the same y1 and y2(but on different point sets)

Page 164: Orthogonal Range Searching

Improving the query time

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

y1 = 15 y2 = 64

when we do a 2D look-up we do O(logn) 1D lookups. . .

all with the same y1 and y2(but on different point sets)

The slow part is finding the successor of y1

Page 165: Orthogonal Range Searching

Improving the query time

3 7 11 19 23 27 35 43 53 61 67

3 7 11 19 23 27 35 43 53 61 67

y1 = 15 y2 = 64

when we do a 2D look-up we do O(logn) 1D lookups. . .

all with the same y1 and y2(but on different point sets)

The slow part is finding the successor of y1

If I told you where this point was, a 1D lookup would only take O(k′) time

(where k′ is the number of points between y1 and y2)

Page 166: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

The arrays of points at the childrenpartition the array of the parent

Page 167: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

The arrays of points at the childrenpartition the array of the parent

Page 168: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

The arrays of points at the childrenpartition the array of the parent

Page 169: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

The arrays of points at the childrenpartition the array of the parent

The child arrays are sorted by y coordinate

(but have been partitioned by x coordinate)

Page 170: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

The arrays of points at the childrenpartition the array of the parent

The child arrays are sorted by y coordinate

(but have been partitioned by x coordinate)

Consider a point in the parent array. . .

Page 171: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

The arrays of points at the childrenpartition the array of the parent

The child arrays are sorted by y coordinate

(but have been partitioned by x coordinate)

Consider a point in the parent array. . .

Page 172: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

The arrays of points at the childrenpartition the array of the parent

The child arrays are sorted by y coordinate

(but have been partitioned by x coordinate)

Consider a point in the parent array. . .

Page 173: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

The arrays of points at the childrenpartition the array of the parent

The child arrays are sorted by y coordinate

(but have been partitioned by x coordinate)

Consider a point in the parent array. . .we add a link to its successor in both child arrays

(we do this for every point during preprocessing)

Page 174: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

The arrays of points at the childrenpartition the array of the parent

The child arrays are sorted by y coordinate

(but have been partitioned by x coordinate)

Consider a point in the parent array. . .we add a link to its successor in both child arrays

(we do this for every point during preprocessing)

Page 175: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

The arrays of points at the childrenpartition the array of the parent

The child arrays are sorted by y coordinate

(but have been partitioned by x coordinate)

Consider a point in the parent array. . .we add a link to its successor in both child arrays

(we do this for every point during preprocessing)

Page 176: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

Page 177: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

Observation if we know where the successor of y1 is in the

parent, can find the successor in either child in O(1) time

Page 178: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

Observation if we know where the successor of y1 is in the

parent, can find the successor in either child in O(1) time

y1 = 15

y1 = 15 y1 = 15

Page 179: Orthogonal Range Searching

Improving the query time

7

3 7 11 19 23 27 35 43 53 61 67

311 19 23 2735 43 5361 67

Observation if we know where the successor of y1 is in the

parent, can find the successor in either child in O(1) time

y1 = 15

y1 = 15 y1 = 15

adding these links doesn’t increase the space or the prep time

Page 180: Orthogonal Range Searching

The improved query time

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .use the 1D range structure for that subtree

to filter the y coordinates

(updating the successor to y1 as you go)

How long does a query take?

Page 181: Orthogonal Range Searching

The improved query time

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .use the 1D range structure for that subtree

to filter the y coordinates

(updating the successor to y1 as you go)

How long does a query take?

The paths have length O(logn)

Page 182: Orthogonal Range Searching

The improved query time

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .use the 1D range structure for that subtree

to filter the y coordinates

(updating the successor to y1 as you go)

How long does a query take?

The paths have length O(logn)

So steps 1. and 2. take O(logn) time

Page 183: Orthogonal Range Searching

The improved query time

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .use the 1D range structure for that subtree

to filter the y coordinates

(updating the successor to y1 as you go)

How long does a query take?

The paths have length O(logn)

So steps 1. and 2. take O(logn) time

As for step 3,

Page 184: Orthogonal Range Searching

The improved query time

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .use the 1D range structure for that subtree

to filter the y coordinates

(updating the successor to y1 as you go)

How long does a query take?

The paths have length O(logn)

So steps 1. and 2. take O(logn) time

As for step 3,

We do O(logn) 1D lookups. . .

Page 185: Orthogonal Range Searching

The improved query time

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .use the 1D range structure for that subtree

to filter the y coordinates

(updating the successor to y1 as you go)

How long does a query take?

The paths have length O(logn)

So steps 1. and 2. take O(logn) time

As for step 3,

We do O(logn) 1D lookups. . .

Each takes O(k′) time

Page 186: Orthogonal Range Searching

The improved query time

Query summary

1. Follow the paths to x1 and x2

2. Discard off-path subtrees where the x coordinates are too large or too small

3. For each off-path subtree where the x coordinates are in range. . .use the 1D range structure for that subtree

to filter the y coordinates

(updating the successor to y1 as you go)

How long does a query take?

The paths have length O(logn)

So steps 1. and 2. take O(logn) time

As for step 3,

We do O(logn) 1D lookups. . .

Each takes O(k′) time

This sums to. . .

O(logn+ k)

Page 187: Orthogonal Range Searching

2D range searching

� A 2D range searching data structure stores n distinct (x, y)-pairs and supports:

the lookup(x1, x2, y1, y2) operation

which returns every point in the rectangle [x1 : x2]× [y1 : y2]

i.e. every (x, y) with x1 6 x 6 x2 and y1 6 y 6 y2.

x1 x2

y1

y2

Summary

O(n logn) prep time

O(n logn) space

O(logn+ k) lookup time

where k is the number of points reported

we improved this :)using fractional cascading