ONE DIMENSIONAL RANGE SEARCH - Daniel G. Graham PhD · 2019. 2. 13. · 1d range search: BST...

Preview:

Citation preview

O N E D I M E N S I O N A L R A N G E S E A R C H

2

1d range search

Extension of ordered Binary Search Tree.

・Insert key-value pair.

・Search for key k.

・Delete key k.

・Range search: find all keys between k1 and k2.

・Range count: number of keys between k1 and k2.

Application. Database queries.

Geometric interpretation.

・Keys are point on a line.

・Find/count points in a given 1d interval.

insert B B

insert D B D

insert A A B D

insert I A B D I

insert H A B D H I

insert F A B D F H I

insert P A B D F H I P

search G to K H I

count G to K 2

3

1d range search: elementary implementations

Unordered list. Fast insert, slow range search.

Ordered array. Slow insert, binary search for k1 and k2 to do range search.

data structure insert range count range search

unordered list 1 N N

ordered array N log N R + log N

goal log N log N R + log N

order of growth of running time for 1d range search

N = number of keys

R = number of keys that match

L E T ’ S F O C U S O N R A N G E C O U N T I N G・Range count: number of keys between k1 and k2.

1d range count: BST implementation

Calculate the rank each node in the BST

5

・The Rank of a Node: is number of keys that are less than it

・Question: How do we determine the number of keys between k1 and k2

・Step1: Figure how many items less than k1

AC

E

HM

R

SX0

2

6

7

4

1

5

3

rank

・Step2: Subtract the rank(k2) from rank(k1)

H O W W O U L D W E D O S E A R C H

・Range search: find all keys between k1 and k2.

1d range search. Find all keys between lo and hi.

・Recursively find all keys in left subtree (if any could fall in range).

・Check key in current node.

・Recursively find all keys in right subtree (if any could fall in range).

Proposition. Running time proportional to R + log N.Pf. Nodes examined = search path to lo + search path to hi + matches.

7

1d range search: BST implementation

black keys arein the range

red keys are used in comparesbut are not in the range

AC

E

H

LM

P

R

SX

searching in the range [F..T]

Range search in a BST

1d range search: BST implementation

8

black keys arein the range

red keys are used in comparesbut are not in the range

AC

E

H

LM

P

R

SX

searching in the range [F..T]

Range search in a BST

1d range search. Find all keys between lo and hi.

・Recursively find all keys in left subtree (if any could fall in range).

・Check key in current node.

・Recursively find all keys in right subtree (if any could fall in rang

Start at root S

1d range search: BST implementation

9

black keys arein the range

red keys are used in comparesbut are not in the range

AC

E

H

LM

P

R

SX

searching in the range [F..T]

Range search in a BST

1d range search. Find all keys between lo and hi.

・Recursively find all keys in left subtree (if any could fall in range).

・Check key in current node.

・Recursively find all keys in right subtree (if any could fall in rang

Start at root S

Check E 1) E is not the range 2) is less than smallest key F 3)Only need to recurse right

1d range search: BST implementation

10

black keys arein the range

red keys are used in comparesbut are not in the range

AC

E

H

LM

P

R

SX

searching in the range [F..T]

Range search in a BST

1d range search. Find all keys between lo and hi.

・Recursively find all keys in left subtree (if any could fall in range).

・Check key in current node.

・Recursively find all keys in right subtree (if any could fall in rang

Start at root S

Check R 1) R is range [F … T] 2) Recurse left

1d range search: BST implementation

11

black keys arein the range

red keys are used in comparesbut are not in the range

AC

E

H

LM

P

R

SX

searching in the range [F..T]

Range search in a BST

1d range search. Find all keys between lo and hi.

・Recursively find all keys in left subtree (if any could fall in range).

・Check key in current node.

・Recursively find all keys in right subtree (if any could fall in rang

Start at root S

Check H 1) H is range [F … T] 2) Recurse left (null) 3) Recurse right

Same process for M, L, P

1d range search: BST implementation

12

black keys arein the range

red keys are used in comparesbut are not in the range

AC

E

H

LM

P

R

SX

searching in the range [F..T]

Range search in a BST

1d range search. Find all keys between lo and hi.

・Recursively find all keys in left subtree (if any could fall in range).

・Check key in current node.

・Recursively find all keys in right subtree (if any could fall in rang

Start at root S

Check X 1) X is not range [F … T] 2) end recursion

L E T S E X T E N D T H I S I D E A T O T H E 2 D C A S E

14

Orthogonal line segment intersection

Given N horizontal and vertical line segments, find all intersections.

Quadratic algorithm. Check all pairs of line segments for intersection.

Nondegeneracy assumption. All x- and y-coordinates are distinct.

・Remove all the lines that touch with intersecting.

Sweep vertical line from left to right.

・x-coordinates define events.

・h-segment (left endpoint): insert y-coordinate into BST.

15

Orthogonal line segment intersection: sweep-line algorithm

y-coordinates

0

1

2

3

0

1

3

2

4

Sweep vertical line from left to right.

・x-coordinates define events.

・h-segment (left endpoint): insert y-coordinate into BST.

・h-segment (right endpoint): remove y-coordinate from BST.

16

Orthogonal line segment intersection: sweep-line algorithm

y-coordinates

0

1

2

3

4

0

1

3

Sweep vertical line from left to right.

・x-coordinates define events.

・h-segment (left endpoint): insert y-coordinate into BST.

・h-segment (right endpoint): remove y-coordinate from BST.

・v-segment: range search for interval of y-endpoints.

17

Orthogonal line segment intersection: sweep-line algorithm

1d rangesearch

y-coordinates

0

1

2

3

4

0

1

3

18

Orthogonal line segment intersection: sweep-line analysis

Proposition. The sweep-line algorithm takes time proportional to N log N + R

to find all R intersections among N orthogonal line segments.

Pf.

・Put x-coordinates on a PQ (or sort).

・Insert y-coordinates into BST.

・Delete y-coordinates from BST.

・Range searches in BST.

Bottom line. Sweep line reduces 2d orthogonal line segment intersection

search to 1d range search.

N log N

N log N

N log N

N log N + R

L E T S E X T E N D T H E A L G O R I T H M T O 2 D S PA C E

20

2-d orthogonal range search

Extension to 2d keys.

・Insert a 2d key. (Points)

・Delete a 2d key.

・Search for a 2d key.

・Range search: find all keys that lie in a 2d range.

・Range count: number of keys that lie in a 2d range.

Geometric interpretation.

・Keys are point in the plane.

・Find/count points in a given rectangle

21

2d orthogonal range search: grid implementation

Grid implementation.

・Divide space into M-by-M grid of squares.

・Create list of points contained in each square.

・Use 2d array to directly index relevant square.

・Insert: add (x, y) to list for corresponding square.

・Range search: examine only squares that intersect 2d range query.

LB

RT

RT = Right Top LB = Left Bottom

・M-by-M grid

Cell ID 1 2 MxMPoint ID 5 6 7

22

2d orthogonal range search: grid implementation analysis

Choose grid square size to tune performance.

・Too small: wastes space.

– (Searching square that don’t contain anything)

・Too large: too many points per square.

– (Can’t distinguish between points)

LB

RT

H O W E V E R , P O I N T S A R E A LW AY S E V E N I N G D I S T R I B U T E D

Grid implementation. Fast, simple solution for evenly-distributed points.

Problem. Clustering a well-known phenomenon in geometric data.

Ex. USA map data.

24

Clustering

half the squares are empty half the points arein 10% of the squares

13,000 points, 1000 grid squares

Grid implementation. Fast, simple solution for evenly-distributed points.

Problem. Clustering a well-known phenomenon in geometric data.

・Need data structure that adapts gracefully to data.

25

Clustering

N E E D PA R T I T I O N I N G A P P R O A C H T H AT F I T S T H E D I S T R I B U T I O N O F T H E D ATA

Use a tree to represent a recursive subdivision of 2d space.

Grid. Divide space uniformly into squares.

2d tree. Recursively divide space into two halfplanes.

27

Space-partitioning trees

Grid 2d tree

C O N S T R U C T I N G A 2 D T R E E

Recursively partition plane into two halfplanes.

29

2d tree construction

1

2

3

4

6

7

8

9

10

5

1

2

87

10 9

3

4 6

5

Alternative between And at each level in the tree

Data structure. BST, but alternate using x- and y-coordinates as key.

・Search gives rectangle containing point.

・Insert further subdivides the plane.

30

2d tree implementation

even levelsof the tree

p

pointsleft of p

pointsright of p

p

q

pointsbelow q

pointsabove q

odd levelsof the tree

q

1

2

87

1 9

3

4 6

5

1 2

3

4

6

7

8

9

10

5

S O W H AT A B O U T R A N G E S E A R C H

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left (if any points could fall in rectangle).

・Recursively search right (if any points could fall in rectangle).

32

2d tree demo: range search

1

2

3

4

6

7

8

9

10

5

1

2

87

10 9

3

4 6

5

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

33

2d tree demo: range search

2

3

4

6

7

8

9

10

5

2

87

10 9

3

4 6

5

1

1

search root nodecheck if query rectangle contains point 1

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

34

2d tree demo: range search

2

3

4

6

7

8

9

10

5

2

87

10 9

3

4 6

5

1

1

query rectangle to left of splitting linesearch only in left subtree

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

35

2d tree demo: range search

2

4

6

7

8

9

10

5

2

87

10 9

3

4 6

5

1

1

3

search left subtreecheck if query rectangle contains point 3

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

36

2d tree demo: range search

2

3

4

6

7

8

9

10

5

2

87

10 9

3

4 6

5

1

1

query rectangle intersects splitting linesearch bottom and top subtrees

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

37

2d tree demo: range search

2

3

4

6

7

8

9

10

5

2

87

10 9

3

4 6

5

1

1

search left subtreecheck if query rectangle contains point 4

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

38

2d tree demo: range search

2

3

6

7

8

9

10

5

2

87

10 9

3

6

5

1

1

4

4

query rectangle to left of splitting linesearch only in left subtree

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

39

2d tree demo: range search

2

3

6

7

8

9

10

5

2

87

10 9

3

6

5

1

1

4

4

search left subtreecheck if query rectangle contains point 5

(search hit)

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

40

2d tree demo: range search

2

3

6

7

8

9

10

2

87

10 9

3

6

1

1

4

4

5 5

query rectangle intersects splitting linesearch bottom and top subtrees

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

41

2d tree demo: range search

2

3

6

7

8

9

10

2

87

10 9

3

6

1

1

4

4

5 5

search bottom subtreestop since empty

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

42

2d tree demo: range search

2

3

6

7

8

9

10

2

87

10 9

3

6

1

1

4

4

5 5

search top subtreestop since empty

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

43

2d tree demo: range search

2

3

6

7

8

9

10

2

87

10 9

3

6

1

1

4

4

5 5

return from function call

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

44

2d tree demo: range search

2

3

6

7

8

9

10

2

87

10 9

3

6

1

1

4

4

5 5

return from function call

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

45

2d tree demo: range search

2

3

6

7

8

9

10

2

87

10 9

3

6

1

1

4

4

5 5

return from function call

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

46

2d tree demo: range search

2

3

6

7

8

9

10

2

87

10 9

3

6

1

1

4

4

5 5

search top subtreecheck if query rectangle contains point 6

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

47

2d tree demo: range search

2

3

7

8

9

10

2

87

10 9

3

1

1

4

4

5 5

6

6

query rectangle to left of splitting linesearch only in left subtree

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

48

2d tree demo: range search

2

3

7

8

9

10

2

87

10 9

3

1

1

4

4

5 5

6

6

search left subtreestop since empty

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

49

2d tree demo: range search

2

3

7

8

9

10

2

87

10 9

3

1

1

4

4

5 5

6

return from function call

6

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

50

2d tree demo: range search

2

3

7

8

9

10

2

87

10 9

3

1

1

4

4

5 5

6

6

return from function call

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

51

2d tree demo: range search

2

3

7

8

9

10

2

87

10 9

3

1

1

4

4

5 5

6

6

return from function call

Goal. Find all points in a query axis-aligned rectangle.

・Check if point in node lies in given rectangle.

・Recursively search left/bottom (if any could fall in rectangle).

・Recursively search right/top (if any could fall in rectangle).

52

2d tree demo: range search

2

3

7

8

9

10

2

87

10 9

3

1

1

4

4

5 5

6

6

done

N E A R E S T N E I G H B O R

Goal. Find closest point to query point.

54

2d tree demo: nearest neighbor

2

3

4

7

8

9

10

5

1

2

87

10 9

3

4 6

5

query point

1

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

55

2d tree demo: nearest neighbor

2

3

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

search root nodecompute distance from query point to 1

(update champion nearest neighbor)

1

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

56

2d tree demo: nearest neighbor

2

3

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

1

query point is to the left of splitting linesearch left subtree first

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

57

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

3

search left subtreecompute distance from query point to 3

(update champion)

1

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

58

2d tree demo: nearest neighbor

2

3

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

query point is above splitting linesearch top subtree first

1

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

59

2d tree demo: nearest neighbor

2

3

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

search top subtreecompute distance from query point to 6

6

1

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

60

2d tree demo: nearest neighbor

2

3

4

6

7

8

9

10

5

2

87

10 9

3

4 6

5

1

query point is to left of splitting linesearch left subtree first

1

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

61

2d tree demo: nearest neighbor

2

3

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

search left subtreereturn since empty

1

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

62

2d tree demo: nearest neighbor

2

3

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

search right subtreeprune since nearest neighbor

can't be here

1

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

63

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

return from function callsearch bottom subtree next

1

3

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

64

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

search bottom subtreecompute distance from query point to 4

1

3

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

65

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

query point is to left of splitting linesearch left subtree first

1

3

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

66

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

search left subtreecompute distance from query point to 5

(update champion)

1

3

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

67

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

query point is above splitting linesearch top subtree first

1

3

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

68

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

search top subtreereturn since empty

1

3

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

69

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

search bottom subtreereturn since empty

1

3

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

70

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

return from function callsearch right subtree next

1

3

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

71

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

search right subtreeprune since nearest neighbor

can't be here(drawing not quite to scale)

1

3

6

Distancelarger

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

72

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

return from function call

1

3

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

73

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

return from function callsearch right subtree next

1

3

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

74

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

search right subtreeprune since nearest neighbor

can't be here

1

3

6

・Check distance from point in node to query point.

・Recursively search left/bottom (if it could contain a closer point).

・Recursively search right/top (if it could contain a closer point).

75

2d tree demo: nearest neighbor

2

4

7

8

9

10

5

2

87

10 9

3

4 6

5

1

1

3

6

nearest neighbor = 5

76

Flocking birds

Q. What "natural algorithm" do starlings, migrating geese, starlings,

cranes, bait balls of fish, and flashing fireflies use to flock?

http://www.youtube.com/watch?v=XH-groCeKbE

77

Flocking boids [Craig Reynolds, 1986]

Boids. Three simple rules lead to complex emergent flocking behavior:

・Collision avoidance: point away from k nearest boids.

・Flock centering: point towards the center of mass of k nearest boids.

・Velocity matching: update velocity to the average of k nearest boids.

https://www.youtube.com/watch?v=SJyRkeq4Mgw

K - D T R E E K D I M E N S I O N S

79

Kd tree

Kd tree. Recursively partition k-dimensional space into 2 halfspaces.

・Just cycle through each dimension at each level of there tree

Implementation. BST, but cycle through dimensions ala 2d trees.

Efficient, simple data structure for processing k-dimensional data.

・Widely used.

・Adapts well to high-dimensional and clustered data.

level ≡ i (mod k)

points

whose ithcoordinate

is less than p’s

points

whose ithcoordinate

is greater than p’s

p

Jon Bentley

I N T E R VA L S E A R C H T R E E S

81

1d interval search. Data structure to hold set of (overlapping) intervals.

・Insert an interval ( lo, hi ).

・Search for an interval ( lo, hi ).

・Delete an interval ( lo, hi ).

・Interval intersection query: given an interval ( lo, hi ), find all intervals

(or one interval) in data structure that intersects ( lo, hi ). Q. Which intervals intersect ( 9, 16 ) ? A. ( 7, 10 ) and ( 15, 18 ).

1d interval search

(7, 10)

(5, 8)

(4, 8) (15, 18)

(17, 19)

(21, 24)

Create BST, where each node stores an interval ( lo, hi ).

・Use left endpoint as BST key.

・Store max endpoint in subtree rooted at node.

82

Interval search trees

binary search tree (left endpoint is key)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

max endpoint insubtree rooted at node

24

18

8 18

10

24

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

83

Interval search tree demo: insertion

insert interval (16, 22)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

18

8 18

10

24

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

84

Interval search tree demo: insertion

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

insert interval (16, 22)

24

18

8 18

10

24

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

85

Interval search tree demo: insertion

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

compare 16 and 17 (go left)

(16, 22)

insert interval (16, 22)

24

18

8 18

10

24

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

86

Interval search tree demo: insertion

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

(16, 22)

insert interval (16, 22)

24

18

8 18

10

24

(17, 19)

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

87

Interval search tree demo: insertion

(17, 19)

(21, 24)

(4, 8) (15, 18)

(7, 10)

(16, 22)

compare 16 and 5 (go right)

insert interval (16, 22)

24

18

8 18

10

24(5, 8)

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

88

Interval search tree demo: insertion

(17, 19)

(21, 24)

(4, 8) (15, 18)

(7, 10)

insert interval (16, 22)

(16, 22)

24

18

8 18

10

24(5, 8)

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

89

Interval search tree demo: insertion

(17, 19)

(5, 8) (21, 24)

(4, 8)

(7, 10)

(16, 22)

insert interval (16, 22)

compare 16 and 15 (go right)

24

18

8 18

10

24

(15, 18)

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

90

Interval search tree demo: insertion

(17, 19)

(5, 8) (21, 24)

(4, 8)

(7, 10)

insert interval (16, 22)

24

18

8 18

10

24

(15, 18)

(16, 22)

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

91

Interval search tree demo: insertion

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

insert interval (16, 22)

(16, 22) no more tree (insert here)

24

18

8 18

10

24

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

92

Interval search tree demo: insertion

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

insert interval (16, 22)

no more tree (insert here)

24

18

8 18

10

24

(16, 22)(16, 22)

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

93

Interval search tree demo: insertion

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

insert interval (16, 22)

update max in each node on search path

24

18

8 18

10

24

(16, 22)(16, 22)

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

94

Interval search tree demo: insertion

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

insert interval (16, 22)

update max in each node on search path

24

18

8 18

10

24

22

22

22(16, 22)(16, 22)

To insert an interval ( lo, hi ) :

・Insert into BST, using lo as the key.

・Update max in each node on search path.

95

Interval search tree demo: insertion

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

insert interval (16, 22)

24

22

8 22

10

24

22(16, 22)

I N T E R VA L S E A R C H

97

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (23, 25)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

98

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (23, 25)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

compare (23, 25) to (17, 19) (no intersection)

(23, 25) (17, 19)

99

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (23, 25)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

compare 22 to 23 (no intersection in left, go right)

(23, 25) (17, 19)

100

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (23, 25)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(23, 25)

(17, 19)

101

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (23, 25)

(17, 19)

(5, 8)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(23, 25)

compare (23, 25) to (21, 24) (intersection!)

(21, 24)

102

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

103

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(12, 14)

compare (12, 14) to (17, 19) (no intersection)

(17, 19)

104

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(12, 14)

compare 22 to 12 (go left)

(17, 19)

105

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(12, 14)

(17, 19)

106

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(12, 14)

compare (12, 14) to (5, 8) (no intersection)

(5, 8)

107

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(12, 14)

compare 8 to 12 (go right)

(5, 8)

108

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(12, 14)

(5, 8)

109

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(5, 8) (21, 24)

(4, 8)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(12, 14) compare (12, 14) to (15, 18) (no intersection)

(15, 18)

110

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(5, 8) (21, 24)

(4, 8)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(12, 14) compare 10 to 12 (go right)

(15, 18)

111

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(5, 8) (21, 24)

(4, 8)

(7, 10)

24

22

8 22

10

24

22(16, 22) (12, 14)

(15, 18)

112

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22 (12, 14)(16, 22)

113

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22 (12, 14)

compare (12, 14) to (16, 22) (no intersection)

(16, 22)

114

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22 (12, 14)

left subtree is null (go right)

(16, 22)

115

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (12, 14)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22

node is null (no intersection)

(16, 22)

116

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

interval intersectionsearch for (21, 23)

117

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(21, 23)

compare (21, 23) to (17, 19) (no intersection)

interval intersectionsearch for (21, 23)

(17, 19)

118

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(21, 23)

compare 22 to 21 (go left)

interval intersectionsearch for (21, 23)

(17, 19)

119

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(21, 23)

interval intersectionsearch for (21, 23)

(17, 19)

120

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

(17, 19)

(21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(21, 23)

compare (21, 23) to (5, 8) (no intersection)

interval intersectionsearch for (21, 23)

(5, 8)

121

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

(17, 19)

(21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(21, 23)

compare 8 to 21 (go right)

interval intersectionsearch for (21, 23)

(5, 8)

122

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

(17, 19)

(21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(21, 23)

interval intersectionsearch for (21, 23)

(5, 8)

123

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

(17, 19)

(5, 8) (21, 24)

(4, 8)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(21, 23) compare (21, 23) to (15, 18) (no intersection)

interval intersectionsearch for (21, 23)

(15, 18)

124

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (21, 23)

(17, 19)

(5, 8) (21, 24)

(4, 8)

(7, 10)

24

22

8 22

10

24

22(16, 22)

(21, 23) compare 10 to 21 (go right)

(15, 18)

125

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (21, 23)

(17, 19)

(5, 8) (21, 24)

(4, 8)

(7, 10)

24

22

8 22

10

24

22(16, 22) (21, 23)

(15, 18)

126

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (21, 23)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22 (21, 23)(16, 22)

127

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (21, 23)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22 (21, 23)

compare (21, 23) to (16, 22) (intersection!)

(16, 22)

128

Interval search tree demo: intersection

To search for any one interval that intersects query interval ( lo, hi ) :

・If interval in node intersects query interval, return it.

・Else if left subtree is null, go right.

・Else if max endpoint in left subtree is less than lo, go right.

・Else go left.

interval intersectionsearch for (21, 23)

(17, 19)

(5, 8) (21, 24)

(4, 8) (15, 18)

(7, 10)

24

22

8 22

10

24

22 (21, 23)

compare (21, 23) to (16, 22) (intersection!)

(16, 22)

129

Orthogonal rectangle intersection

Goal. Find all intersections among a set of N orthogonal rectangles.

Quadratic algorithm. Check all pairs of rectangles for intersection.

Non-degeneracy assumption. All x- and y-coordinates are distinct.

0

1

2

3

I N T E R S E C T I O N R E C TA N G L E S

131

Microprocessors and geometry

Early 1970s. microprocessor design became a geometric problem.

・Very Large Scale Integration (VLSI).

・Computer-Aided Design (CAD).

Design-rule checking.

・Certain wires cannot intersect.

・Certain spacing needed between different types of wires.

・Debugging = orthogonal rectangle intersection search.

Sweep vertical line from left to right.

・x-coordinates of left and right endpoints define events.

・Maintain set of rectangles that intersect the sweep line in an interval

search tree (using y-intervals of rectangle).

・Left endpoint: interval search for y-interval of rectangle; insert y-interval.

・Right endpoint: remove y-interval.

132

Orthogonal rectangle intersection: sweep-line algorithm

y-coordinates

0

1

2

3

0

1

23

133

Orthogonal rectangle intersection: sweep-line analysis

Proposition. Sweep line algorithm takes time proportional to N log N + R log Nto find R intersections among a set of N rectangles.

Pf.

・Put x-coordinates on a PQ (or sort).

・Insert y-intervals into ST.

・Delete y-intervals from ST.

・Interval searches for y-intervals.

Bottom line. Sweep line reduces 2d orthogonal rectangle intersection

search to 1d interval search.

N log N

N log N

N log N

N log N + R log N

problem example solution

1d range search BST

2d orthogonal linesegment intersection

sweep line reduces to 1d range search

kd range search kd tree

1d interval search interval search tree

2d orthogonalrectangle intersection

sweep line reduces to 1d interval search

Geometric applications of BSTs

134

http://algs4.cs.princeton.edu

Algorithms ROBERT SEDGEWICK | KEVIN WAYNE

ROBERT SEDGEWICK | KEVIN WAYNE

F O U R T H E D I T I O N

Algorithms

GEOMETRIC APPLICATIONS OF

‣ 1d range search‣ line segment intersection‣ kd trees‣ interval search trees‣ rectangle intersection

Recommended