+ All Categories
Home > Documents > Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a...

Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a...

Date post: 07-Aug-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
162
1 Algorithms for GIS Summer Semester 2017 Lecture #3 Steven Chaplick Chair for Computer Science I Orthogonal Range Queries (based on Computational Geometry: Algorithms and Applications 3 rd Ed., Chapter 5.)
Transcript
Page 1: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

1

Algorithms for GISSummer Semester 2017

Lecture #3

Steven Chaplick Chair for Computer Science I

Orthogonal Range Queries

(based on Computational Geometry: Algorithms andApplications 3rd Ed., Chapter 5.)

Page 2: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

2 - 1

Orthogonal Range Queries

Example: Personnel management in a company

2.000

3.000

e

19.559.99919.500.000

1.000

Page 3: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

2 - 2

Orthogonal Range Queries

Example: Personnel management in a company

2.000

3.000

e

19.559.99919.500.000

G. Ometerborn:salary: 2.500e

1.000

1953-08-20

Page 4: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

2 - 3

Orthogonal Range Queries

Example: Personnel management in a company

2.000

3.000

e

19.559.99919.500.000

G. Ometerborn:salary: 2.500e

1.000

1953-08-20

Page 5: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

2 - 4

Orthogonal Range Queries

Example: Personnel management in a company

2.000

3.000

e

19.559.99919.500.000

G. Ometerborn:salary: 2.500e

1.000

1953-08-20

Page 6: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

2 - 5

Orthogonal Range Queries

Example: Personnel management in a company

2.000

3.000

e

19.559.99919.500.000

G. Ometerborn:salary: 2.500e

1.000

1953-08-20

Page 7: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

2 - 6

Orthogonal Range Queries

Example: Personnel management in a company

19,500,000 19,559,999

3,000

4,000

2

4

e

Page 8: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

2 - 7

Orthogonal Range Queries

Example: Personnel management in a company

19,500,000 19,559,999

3,000

4,000

2

4

Typical queries for data bases!

e

Page 9: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 1

1d Range Searching

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 10: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 2

1d Range Searching

Solution:

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 11: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 3

1d Range Searching

Solution: balanced binary search trees. . .

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 12: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 4

1d Range Searching

Solution: balanced binary search trees. . .

4 8 11 13 17 21 27

Small changes: – keys only in leaves

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 13: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 5

1d Range Searching

Solution: balanced binary search trees. . .

11

4 8 11 13 17 21 27

Small changes: – keys only in leaves

– inner nodes store maxima of their left subtrees

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 14: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 6

1d Range Searching

Solution: balanced binary search trees. . .

11

4 8 11 13 17 21 27

4 17

8 13 21

Small changes: – keys only in leaves

– inner nodes store maxima of their left subtrees

27

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 15: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 7

1d Range Searching

6 21

Solution: balanced binary search trees. . .

11

4 8 11 13 17 21 27

4 17

8 13 21

Small changes: – keys only in leaves

– inner nodes store maxima of their left subtrees

27

query

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 16: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 8

1d Range Searching

6 21

Solution: balanced binary search trees. . .

11

4 8 11 13 17 21 27

4 17

8 13 21

Small changes: – keys only in leaves

– inner nodes store maxima of their left subtrees

27

query

1. Search x = 6.

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 17: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 9

1d Range Searching

6 21

Solution: balanced binary search trees. . .

11

4 8 11 13 17 21 27

4 17

8 13 21

Small changes: – keys only in leaves

– inner nodes store maxima of their left subtrees

27

query

1. Search x = 6.

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 18: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 10

1d Range Searching

6 21

Solution: balanced binary search trees. . .

11

4 8 11 13 17 21 27

4 17

8 13 21

Small changes: – keys only in leaves

– inner nodes store maxima of their left subtrees

27

query

1. Search x = 6.

2. Search x ′ = 21.

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 19: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 11

1d Range Searching

6 21

Solution: balanced binary search trees. . .

11

4 8 11 13 17 21 27

4 17

8 13 21

Small changes: – keys only in leaves

– inner nodes store maxima of their left subtrees

27

query

1. Search x = 6.

2. Search x ′ = 21.

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 20: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 12

1d Range Searching

6 21

Solution: balanced binary search trees. . .

11

4 8 11 13 17 21 27

4 17

8 13 21

Small changes: – keys only in leaves

– inner nodes store maxima of their left subtrees

27

query

vsplit1. Search x = 6.

2. Search x ′ = 21.

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 21: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

3 - 13

1d Range Searching

6 21

Solution: balanced binary search trees. . .

11

4 8 11 13 17 21 27

4 17

8 13 21

Small changes: – keys only in leaves

– inner nodes store maxima of their left subtrees

27

query

Returnall leaves‘inbetween”.

3.

vsplit1. Search x = 6.

2. Search x ′ = 21.

Task: Preprocess a finite set P ⊂ R such that for anyinterval [x , x ′] the set P ∩ [x , x ′] can be reportedquickly. [2 min]

Page 22: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

4 - 1

1d Range Searching11

4 8 11 13 17 21 27

4 17

8 13 21

27

vsplit

Page 23: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

4 - 2

1d Range Searching11

4 8 11 13 17 21 27

4 17

8 13 21

27

vsplit

Page 24: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

4 - 3

1d Range Searching

Observe: The result of a query is the disjoint union of at most 2hcanonical subsets

11

4 8 11 13 17 21 27

4 17

8 13 21

27

vsplit

Page 25: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

4 - 4

1d Range Searching

Observe: The result of a query is the disjoint union of at most 2hcanonical subsets

– h ∈ O(log n) is the tree height,

, where

11

4 8 11 13 17 21 27

4 17

8 13 21

27

vsplit

Page 26: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

4 - 5

1d Range Searching

Observe: The result of a query is the disjoint union of at most 2hcanonical subsets

– h ∈ O(log n) is the tree height,

– a canonical subset is an interval that contains all pointsstored in a subtree.

, where

11

4 8 11 13 17 21 27

4 17

8 13 21

27

vsplit

Page 27: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

4 - 6

1d Range Searching

Observe: The result of a query is the disjoint union of at most 2hcanonical subsets

– h ∈ O(log n) is the tree height,

– a canonical subset is an interval that contains all pointsstored in a subtree.

, where

Theorem. A set of n real numbers can be preprocessed in O(n log n)time and O(n) space such that 1d range queries take

time.

11

4 8 11 13 17 21 27

4 17

8 13 21

27

vsplit

Page 28: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

4 - 7

1d Range Searching

Observe: The result of a query is the disjoint union of at most 2hcanonical subsets

– h ∈ O(log n) is the tree height,

– a canonical subset is an interval that contains all pointsstored in a subtree.

, where

Theorem. A set of n real numbers can be preprocessed in O(n log n)time and O(n) space such that 1d range queries take

time.O(k + log n) , where k = |output|.

11

4 8 11 13 17 21 27

4 17

8 13 21

27

vsplit

Page 29: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

4 - 8

1d Range Searching

Observe: The result of a query is the disjoint union of at most 2hcanonical subsets

– h ∈ O(log n) is the tree height,

– a canonical subset is an interval that contains all pointsstored in a subtree.

, where

Theorem. A set of n real numbers can be preprocessed in O(n log n)time and O(n) space such that 1d range queries take

time.O(k + log n) , where k = |output|.

11

4 8 11 13 17 21 27

4 17

8 13 21

27

vsplit

outputsensitive!

Page 30: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

5 - 1

Extensions to 2d

Think... [3 min]

Task: Preprocess a finite set P ⊂ R2 such that for anyrange query R = [x , x ′]× [y , y ′] the set P ∩ R canbe reported quickly.

Page 31: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

5 - 2

Extensions to 2d

Think... [3 min]

Task: Preprocess a finite set P ⊂ R2 such that for anyrange query R = [x , x ′]× [y , y ′] the set P ∩ R canbe reported quickly.

Solutions:

Page 32: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

5 - 3

Extensions to 2d

Think... [3 min]

• one tree;query path alternates between x- and y -coordinate

Task: Preprocess a finite set P ⊂ R2 such that for anyrange query R = [x , x ′]× [y , y ′] the set P ∩ R canbe reported quickly.

Solutions:

Page 33: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

5 - 4

Extensions to 2d

Think... [3 min]

• one tree;query path alternates between x- and y -coordinate

• first-level tree for x-coordinates;many second-level trees for y -coordinates

Task: Preprocess a finite set P ⊂ R2 such that for anyrange query R = [x , x ′]× [y , y ′] the set P ∩ R canbe reported quickly.

Solutions:

Page 34: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

5 - 5

Extensions to 2d

Think... [3 min]

• one tree;query path alternates between x- and y -coordinate

• first-level tree for x-coordinates;many second-level trees for y -coordinates

Task: Preprocess a finite set P ⊂ R2 such that for anyrange query R = [x , x ′]× [y , y ′] the set P ∩ R canbe reported quickly.

Solutions:

kd-tree

range tree

}}

Page 35: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

5 - 6

Extensions to 2d

Think... [3 min]

• one tree;query path alternates between x- and y -coordinate

• first-level tree for x-coordinates;many second-level trees for y -coordinates

Task: Preprocess a finite set P ⊂ R2 such that for anyrange query R = [x , x ′]× [y , y ′] the set P ∩ R canbe reported quickly.

Solutions:

kd-tree

range tree

}}

Assume: General position!

Page 36: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

5 - 7

Extensions to 2d

Think... [3 min]

• one tree;query path alternates between x- and y -coordinate

• first-level tree for x-coordinates;many second-level trees for y -coordinates

Task: Preprocess a finite set P ⊂ R2 such that for anyrange query R = [x , x ′]× [y , y ′] the set P ∩ R canbe reported quickly.

Solutions:

kd-tree

range tree

}}

Assume: General position!

Here: no two points have the same x- or y -coordinate.

Page 37: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 1

Kd-Trees: Example

p4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Page 38: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 2

Kd-Trees: Example

p

`1

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Page 39: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 3

Kd-Trees: Example

p

`1 `1

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

• Split any reagion that contains more than one point.

Page 40: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 4

Kd-Trees: Example

p

`1

`2

`1

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

• Split any reagion that contains more than one point.

Page 41: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 5

Kd-Trees: Example

p

`1

`2

`1

`24

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

• Split any reagion that contains more than one point.

Page 42: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 6

Kd-Trees: Example

p

`1

`2

`4

`1

`24

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

• Split any reagion that contains more than one point.

Page 43: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 7

Kd-Trees: Example

p

`1

`2

`4

`1

`2

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

• Split any reagion that contains more than one point.

Page 44: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 8

Kd-Trees: Example

p

`1

`2

`8

`4

`1

`2

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

• Split any reagion that contains more than one point.

Page 45: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 9

Kd-Trees: Example

p

`1

`2

`8

`4

`1

`2

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

• Split any reagion that contains more than one point.

Page 46: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 10

Kd-Trees: Example

p

`1

`2

p1

`8

`4

`1

`2

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 47: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 11

Kd-Trees: Example

p

`1

`2

p1 p2

`8

`4

`1

`2

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 48: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 12

Kd-Trees: Example

p

`1

`2

p1 p2

p3

`8

`4

`1

`2

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 49: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 13

Kd-Trees: Example

p

`1`5

`2

p1 p2

p3

`8

`4

`1

`2

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 50: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 14

Kd-Trees: Example

p

`1`5

`2

p1 p2

p3

`8

`4

`1

`2

`5

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 51: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 15

Kd-Trees: Example

p

`1`5

`2

p1 p2

p3 p4

`8

`4

`1

`2

`5

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 52: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 16

Kd-Trees: Example

p

`1`5

`2

p1 p2

p3 p4 p5

`8

`4

`1

`2

`5

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 53: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 17

Kd-Trees: Example

p

`1

`3

`5

`2

p1 p2

p3 p4 p5

`8

`4

`1

`2

`5

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 54: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 18

Kd-Trees: Example

p

`1

`3

`5

`2

p1 p2

p3 p4 p5

`8

`4

`1

`2 `3

`5

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 55: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 19

Kd-Trees: Example

p

`1

`3

`6

`5

`2

p1 p2

p3 p4 p5

`8

`4

`1

`2 `3

`5

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 56: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 20

Kd-Trees: Example

p

`1

`3

`6

`5

`2

p1 p2

p3 p4 p5

`8

`4

`1

`2 `3

`5 `6

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 57: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 21

Kd-Trees: Example

p

`1

`3

`6

`5

`2

p1 p2

p3 p4 p5

`8

`4

`1

`2 `3

`5 `6

`8

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

`9

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 58: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 22

Kd-Trees: Example

p

`1

`3

`6

`5

`2

p1 p2

p3 p4 p5

`8

`4

`1

`2 `3

`5 `6

`8 `9

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

`9

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 59: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 23

Kd-Trees: Example

p

`1

`3

`6

`5

`2

p1 p2

p3 p4 p5

p6

`8

`4

`1

`2 `3

`5 `6

`8 `9

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

`9

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 60: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 24

Kd-Trees: Example

p

`1

`3

`6

`5

`2

p1 p2

p3 p4 p5

p6 p7

`8

`4

`1

`2 `3

`5 `6

`8 `9

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

`9

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 61: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 25

Kd-Trees: Example

p

`1

`3

`6

`5

`2

p1 p2

p3 p4 p5

p6 p7

p8

`8

`4

`1

`2 `3

`5 `6

`8 `9

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

`9

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 62: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 26

Kd-Trees: Example

p

`1`7

`3

`6

`5

`2

p1 p2

p3 p4 p5

p6 p7

p8

`8

`4

`1

`2 `3

`5 `6

`8 `9

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

`9

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 63: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 27

Kd-Trees: Example

p

`1`7

`3

`6

`5

`2

p1 p2

p3 p4 p5

p6 p7

p8

`8

`4

`1

`2 `3

`5 `6 `7

`8 `9

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

`9

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 64: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 28

Kd-Trees: Example

p

`1`7

`3

`6

`5

`2

p1 p2

p3 p4 p5

p6 p7

p8 p9

`8

`4

`1

`2 `3

`5 `6 `7

`8 `9

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

`9

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 65: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 29

Kd-Trees: Example

p

`1`7

`3

`6

`5

`2

p1 p2

p3 p4 p5

p6 p7

p8 p9 p10

`8

`4

`1

`2 `3

`5 `6 `7

`8 `9

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

`9

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 66: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

6 - 30

Kd-Trees: Example

p

`1`7

`3

`6

`5

`2

p1 p2

p3 p4 p5

p6 p7

p8 p9 p10

`8

`4

`1

`2 `3

`5 `6 `7

`8 `9

`4

4

p1

p5

p3

p2

p7

p9

p10

p6

p8

`9

[dBCvKO’08]

Vertical left.

• Split any reagion that contains more than one point.

• Horizontal split lines/segments belong to the region below.

Page 67: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 1

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

p2

Page 68: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 2

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

p2

Page 69: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 3

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)

p2

Page 70: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 4

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)if |P| = 1 then

return (leaf storing the pt in P)else

if depth is even thensplit P with the vertical line

` : x = xmedian(P) intoP1 (pts left of or on `) andP2 = P \ P1

elsesplit P horizontally...

vleft ← BuildKdTree(P1, depth + 1)vright ← BuildKdTree(P2, depth + 1)create a node v storing `make vleft and vright the children of vreturn (v)

p2

Page 71: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 5

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)if |P| = 1 then

return (leaf storing the pt in P)else

if depth is even thensplit P with the vertical line

` : x = xmedian(P) intoP1 (pts left of or on `) andP2 = P \ P1

elsesplit P horizontally...

vleft ← BuildKdTree(P1, depth + 1)vright ← BuildKdTree(P2, depth + 1)create a node v storing `make vleft and vright the children of vreturn (v)

p2

Page 72: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 6

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)if |P| = 1 then

return (leaf storing the pt in P)else

if depth is even thensplit P with the vertical line

` : x = xmedian(P) intoP1 (pts left of or on `) andP2 = P \ P1

elsesplit P horizontally...

vleft ← BuildKdTree(P1, depth + 1)vright ← BuildKdTree(P2, depth + 1)create a node v storing `make vleft and vright the children of vreturn (v)

p2

Page 73: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 7

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)if |P| = 1 then

return (leaf storing the pt in P)else

if depth is even thensplit P with the vertical line

` : x = xmedian(P) intoP1 (pts left of or on `) andP2 = P \ P1

elsesplit P horizontally...

vleft ← BuildKdTree(P1, depth + 1)vright ← BuildKdTree(P2, depth + 1)create a node v storing `make vleft and vright the children of vreturn (v)

`′

p2

Page 74: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 8

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)if |P| = 1 then

return (leaf storing the pt in P)else

if depth is even thensplit P with the vertical line

` : x = xmedian(P) intoP1 (pts left of or on `) andP2 = P \ P1

elsesplit P horizontally...

vleft ← BuildKdTree(P1, depth + 1)vright ← BuildKdTree(P2, depth + 1)create a node v storing `make vleft and vright the children of vreturn (v)

p2

Page 75: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 9

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)if |P| = 1 then

return (leaf storing the pt in P)else

if depth is even thensplit P with the vertical line

` : x = xmedian(P) intoP1 (pts left of or on `) andP2 = P \ P1

elsesplit P horizontally...

vleft ← BuildKdTree(P1, depth + 1)vright ← BuildKdTree(P2, depth + 1)create a node v storing `make vleft and vright the children of vreturn (v)

p2

vleft vright

Page 76: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 10

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)if |P| = 1 then

return (leaf storing the pt in P)else

if depth is even thensplit P with the vertical line

` : x = xmedian(P) intoP1 (pts left of or on `) andP2 = P \ P1

elsesplit P horizontally...

vleft ← BuildKdTree(P1, depth + 1)vright ← BuildKdTree(P2, depth + 1)create a node v storing `make vleft and vright the children of vreturn (v)

p2

vleft vright

Page 77: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 11

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)if |P| = 1 then

return (leaf storing the pt in P)else

if depth is even thensplit P with the vertical line

` : x = xmedian(P) intoP1 (pts left of or on `) andP2 = P \ P1

elsesplit P horizontally...

vleft ← BuildKdTree(P1, depth + 1)vright ← BuildKdTree(P2, depth + 1)create a node v storing `make vleft and vright the children of vreturn (v)

p2

vleft vright

v`

Page 78: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 12

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)if |P| = 1 then

return (leaf storing the pt in P)else

if depth is even thensplit P with the vertical line

` : x = xmedian(P) intoP1 (pts left of or on `) andP2 = P \ P1

elsesplit P horizontally...

vleft ← BuildKdTree(P1, depth + 1)vright ← BuildKdTree(P2, depth + 1)create a node v storing `make vleft and vright the children of vreturn (v)

p2

vleft vright

v`

Page 79: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 13

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)if |P| = 1 then

return (leaf storing the pt in P)else

if depth is even thensplit P with the vertical line

` : x = xmedian(P) intoP1 (pts left of or on `) andP2 = P \ P1

elsesplit P horizontally...

vleft ← BuildKdTree(P1, depth + 1)vright ← BuildKdTree(P2, depth + 1)create a node v storing `make vleft and vright the children of vreturn (v)

p2

vleft vright

v`

Page 80: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

7 - 14

Kd-Trees: Construction

p

`

4

p1

p5

p3

p7

p9

p10

p6

p8

Pseudo-code:

BuildKdTree(points P, int depth)if |P| = 1 then

return (leaf storing the pt in P)else

if depth is even thensplit P with the vertical line

` : x = xmedian(P) intoP1 (pts left of or on `) andP2 = P \ P1

elsesplit P horizontally...

vleft ← BuildKdTree(P1, depth + 1)vright ← BuildKdTree(P2, depth + 1)create a node v storing `make vleft and vright the children of vreturn (v)

p2

vleft vright

v`

Page 81: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

8 - 1

Kd-Trees: Analysis

Construction time?

Page 82: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

8 - 2

Kd-Trees: Analysis

Construction time?

T (n) =

{O(1) if n = 1

O(n) + 2T (dn/2e) else.

Page 83: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

8 - 3

Kd-Trees: Analysis

Construction time?

T (n) =

{O(1) if n = 1

O(n) + 2T (dn/2e) else.

}= O(n log n)

Page 84: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

8 - 4

Kd-Trees: Analysis

Construction time?

T (n) =

{O(1) if n = 1

O(n) + 2T (dn/2e) else.

}= O(n log n)

see Mergesort!

Page 85: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

8 - 5

Kd-Trees: Analysis

Construction time?

T (n) =

{O(1) if n = 1

O(n) + 2T (dn/2e) else.

}= O(n log n)

Lemma: A kd-tree for a set of n pts in the plane takesO(n log n) time to construct and uses O(n) storage.

see Mergesort!

Page 86: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

8 - 6

Kd-Trees: Analysis

Construction time?

T (n) =

{O(1) if n = 1

O(n) + 2T (dn/2e) else.

}= O(n log n)

Lemma: A kd-tree for a set of n pts in the plane takesO(n log n) time to construct and uses O(n) storage.

see Mergesort!

Page 87: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

9 - 1

Kd-Trees: Querying

*p1 p2

p2

p1

p3

p3 p4

p4p5

p5

p6

p6

p7

p7 p8

p8

p9

p9

p10

p10

p11

p11

p12

p12 p13

p13

Page 88: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

9 - 2

Kd-Trees: Querying

*p1 p2

p2

p1

p3

p3 p4

p4p5

p5

p6

p6

p7

p7 p8

p8

p9

p9

p10

p10

p11

p11

p12

p12 p13

p13

Lemma. Querying a kd-tree for n pts in the plane with anaxis-parallel rectangle R takes O(k +

√n ) time,

where k = |output|.

Page 89: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

9 - 3

Kd-Trees: Querying

*p1 p2

p2

p1

p3

p3 p4

p4p5

p5

p6

p6

p7

p7 p8

p8

p9

p9

p10

p10

p11

p11

p12

p12 p13

p13

Lemma. Querying a kd-tree for n pts in the plane with anaxis-parallel rectangle R takes O(k +

√n ) time,

where k = |output|.idea: O(

√n) regions of the kd-tree intersect

a vertical/horizontal line.

Page 90: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

9 - 4

Kd-Trees: Querying

*p1 p2

p2

p1

p3

p3 p4

p4p5

p5

p6

p6

p7

p7 p8

p8

p9

p9

p10

p10

p11

p11

p12

p12 p13

p13

Lemma. Querying a kd-tree for n pts in the plane with anaxis-parallel rectangle R takes O(k +

√n ) time,

where k = |output|. in Rd

idea: O(√

n) regions of the kd-tree intersecta vertical/horizontal line.

Page 91: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

9 - 5

Kd-Trees: Querying

*p1 p2

p2

p1

p3

p3 p4

p4p5

p5

p6

p6

p7

p7 p8

p8

p9

p9

p10

p10

p11

p11

p12

p12 p13

p13

Lemma. Querying a kd-tree for n pts in the plane with anaxis-parallel rectangle R takes O(k +

√n ) time,

where k = |output|.

O(k + n1−1/d)

in Rd

idea: O(√

n) regions of the kd-tree intersecta vertical/horizontal line.

Page 92: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

10 - 1

Range Trees: Query Algorithm

1. Search in main tree for x-coordinate

µ µ′

vsplit

Page 93: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

10 - 2

Range Trees: Query Algorithm

1. Search in main tree for x-coordinate

2. For each node uon the path from vsplit to µ:

µ µ′

vsplit

Page 94: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

10 - 3

Range Trees: Query Algorithm

1. Search in main tree for x-coordinate

2. For each node uon the path from vsplit to µ:

u

µ µ′

vsplit

Page 95: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

10 - 4

Range Trees: Query Algorithm

1. Search in main tree for x-coordinate

v

2. For each node uon the path from vsplit to µ:

For the right child v of u:

u

µ µ′

vsplit

Page 96: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

10 - 5

Range Trees: Query Algorithm

1. Search in main tree for x-coordinate

v

2. For each node uon the path from vsplit to µ:

For the right child v of u:

u

Search in auxiliary tree Tvfor points withy-coordinate ∈ [y , y ′]

µ µ′

vsplit

Page 97: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

10 - 6

Range Trees: Query Algorithm

1. Search in main tree for x-coordinate

v

2. For each node uon the path from vsplit to µ:

For the right child v of u:

u

Search in auxiliary tree Tvfor points withy-coordinate ∈ [y , y ′]

µ µ′

vsplit

Page 98: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

10 - 7

Range Trees: Query Algorithm

1. Search in main tree for x-coordinate

v

2. For each node uon the path from vsplit to µ:

For the right child v of u:

uTv

Search in auxiliary tree Tvfor points withy-coordinate ∈ [y , y ′]

µ µ′

vsplit

Page 99: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

10 - 8

Range Trees: Query Algorithm

1. Search in main tree for x-coordinate

v

︸ ︷︷ ︸P(v) = canonical subset of Tv

︸ ︷︷ ︸P(v)

2. For each node uon the path from vsplit to µ:

For the right child v of u:

uTv

Search in auxiliary tree Tvfor points withy-coordinate ∈ [y , y ′]

µ µ′

vsplit

Page 100: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

10 - 9

Range Trees: Query Algorithm

1. Search in main tree for x-coordinate

v

︸ ︷︷ ︸P(v) = canonical subset of Tv

︸ ︷︷ ︸P(v)

2. For each node uon the path from vsplit to µ:

For the right child v of u:

u

3. Symmetrically for thepath from vsplit to µ′.

TvSearch in auxiliary tree Tvfor points withy-coordinate ∈ [y , y ′]

µ µ′

vsplit

Page 101: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 1

Range Trees: Construction

>

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 102: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 2

Range Trees: Construction

>

TP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 103: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 3

Range Trees: Construction

>

TP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 104: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 4

Range Trees: Construction

>

pvTP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 105: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 5

Range Trees: Construction

>

pvTP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 106: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 6

Range Trees: Construction

>

pvTP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 107: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 7

Range Trees: Construction

>

pvTP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 108: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 8

Range Trees: Construction

>

xmidv

pvTP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 109: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 9

Range Trees: Construction

>

vleft vrightPleft Pright

xmidv

pvTP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 110: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 10

Range Trees: Construction

>

vleft vright TPPleft Pright

xmidv

pvTP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 111: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 11

Range Trees: Construction

>

vleft vright TPPleft Pright

xmidv

pvRunning time?

TP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 112: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 12

Range Trees: Construction

>

vleft vright TPPleft Pright

xmidv

pvRunning time?

O(n log n) :-(

TP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 113: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 13

Range Trees: Construction

>

vleft vright TPPleft Pright

xmidv

pvRunning time?

O(n log n) :-(

Better:Pre-sort once,then build treebottom-upin linear time.

TP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 114: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

11 - 14

Range Trees: Construction

>

vleft vright TPPleft Pright

xmidv

pvRunning time?

O(n log n) :-(

Better:Pre-sort once,then build treebottom-upin linear time.

⇓Total

constructiontime O(n log n)

TP

Build2DRangeTree(point[ ] P)

construct 2nd-level tree TP on P (y -order)if P = {p} then

create leaf v :else

xmid = median x-coordinate of PPleft = pts in P with x-coordinate ≤ xmid

Pright =vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)

create node v :

return v

Page 115: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

12 - 1

Range Trees: Space Consumption

Each node v of the 1st-level tree has a pointerto a 2nd-level tree Tv with |Tv | = Θ(|P(v)|).

Page 116: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

12 - 2

Range Trees: Space Consumption

Each node v of the 1st-level tree has a pointerto a 2nd-level tree Tv with |Tv | = Θ(|P(v)|).

Q: What’s the total space consumptionof all 2nd-level trees?

Page 117: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

12 - 3

Range Trees: Space Consumption

Each node v of the 1st-level tree has a pointerto a 2nd-level tree Tv with |Tv | = Θ(|P(v)|).

Q: What’s the total space consumption

What’s your guess:• Θ(n2),• Θ(n log n),• Θ(n log2 n), or• Θ(n)?

of all 2nd-level trees?

Page 118: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

12 - 4

Range Trees: Space Consumption

Each node v of the 1st-level tree has a pointerto a 2nd-level tree Tv with |Tv | = Θ(|P(v)|).

Q: What’s the total space consumption

What’s your guess:• Θ(n2),• Θ(n log n),• Θ(n log2 n), or• Θ(n)?

of all 2nd-level trees?

How many treeswill contain agiven point?

Page 119: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

12 - 5

Range Trees: Space Consumption

A: Each p ∈ P is stored in h = Θ(log n) 2nd-level trees.

p

p

p

p

Each node v of the 1st-level tree has a pointerto a 2nd-level tree Tv with |Tv | = Θ(|P(v)|).

Q: What’s the total space consumption

What’s your guess:• Θ(n2),• Θ(n log n),• Θ(n log2 n), or• Θ(n)?

of all 2nd-level trees?

How many treeswill contain agiven point?

Page 120: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

12 - 6

Range Trees: Space Consumption

A: Each p ∈ P is stored in h = Θ(log n) 2nd-level trees.

⇒ Θ(n log n) space

p

p

p

p

Each node v of the 1st-level tree has a pointerto a 2nd-level tree Tv with |Tv | = Θ(|P(v)|).

Q: What’s the total space consumption

What’s your guess:• Θ(n2),• Θ(n log n),• Θ(n log2 n), or• Θ(n)?

of all 2nd-level trees?

How many treeswill contain agiven point?

Page 121: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

13 - 1

Range Trees: Query time∑u ∈ paths to x and x′

O(ku + log n)T (n, k) =

Page 122: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

13 - 2

Range Trees: Query time∑u ∈ paths to x and x′

O(ku + log n)

= O(∑

u ku) + O(∑

u log n)

T (n, k) =

Page 123: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

13 - 3

Range Trees: Query time∑u ∈ paths to x and x′

O(ku + log n)

= O(∑

u ku) + O(∑

u log n)

= O(k)

T (n, k) =

Page 124: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

13 - 4

Range Trees: Query time∑u ∈ paths to x and x′

O(ku + log n)

= O(∑

u ku) + O(∑

u log n)

= O(k) +

T (n, k) =

Page 125: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

13 - 5

Range Trees: Query time∑u ∈ paths to x and x′

O(ku + log n)

= O(∑

u ku) + O(∑

u log n)

= O(k) + 2h · O(log n)

T (n, k) =

Page 126: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

13 - 6

Range Trees: Query time∑u ∈ paths to x and x′

O(ku + log n)

= O(∑

u ku) + O(∑

u log n)

= O(k) + 2h · O(log n)

= O(k + log2 n)

T (n, k) =

Page 127: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

13 - 7

Range Trees: Query time∑u ∈ paths to x and x′

O(ku + log n)

= O(∑

u ku) + O(∑

u log n)

= O(k) + 2h · O(log n)

= O(k + log2 n)

Rd?

T (n, k) =

Page 128: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

13 - 8

Range Trees: Query time∑u ∈ paths to x and x′

O(ku + log n)

= O(∑

u ku) + O(∑

u log n)

= O(k) + 2h · O(log n)

= O(k + log2 n)

Rd?

T (n, k) =

Page 129: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

13 - 9

Range Trees: Query time∑u ∈ paths to x and x′

O(ku + log n)

= O(∑

u ku) + O(∑

u log n)

= O(k) + 2h · O(log n)

= O(k + log2 n)

Rd? O(n logd−1 n) storage and construction time

O(k + logd n) query time

T (n, k) =

Page 130: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

13 - 10

Range Trees: Query time∑u ∈ paths to x and x′

O(ku + log n)

= O(∑

u ku) + O(∑

u log n)

= O(k) + 2h · O(log n)

= O(k + log2 n)

Rd? O(n logd−1 n) storage and construction time

O(k + logd n) query time

T (n, k) =

See Chapter 5.4 in the bookComputational Geometry: Algorithms and Applicationsby de Berg et al., Springer-Verlag, 3rd edition 2008.

Page 131: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

14 - 1

Comparison

kd-tree range treeconstruction time O(n log n) O(n log n)storage O(n) O(n log n)

query time O(k +√

n) O(k + log2 n)

Page 132: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

14 - 2

Comparison

kd-tree range treeconstruction time O(n log n) O(n log n)storage O(n) O(n log n)

query time O(k +√

n) O(k + log2 n)

Note: trade-off between space and query time

Page 133: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 1

General Sets of Points

Idea: use composite numbers (a|b) with lex order

Page 134: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 2

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y)

Page 135: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 3

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y)

Page 136: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 4

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y) p̂ =((x |y), (y |x)

)

Page 137: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 5

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y) p̂ =((x |y), (y |x)

)

Page 138: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 6

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y) p̂ =((x |y), (y |x)

)unique coordin.

Page 139: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 7

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y) p̂ =((x |y), (y |x)

)unique coordin.

range R = [x , x ′]× [y , y ′]

Page 140: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 8

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y) p̂ =((x |y), (y |x)

)unique coordin.

range R = [x , x ′]× [y , y ′]

Page 141: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 9

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y) p̂ =((x |y), (y |x)

)unique coordin.

range R = [x , x ′]× [y , y ′]

R̂ = [(x | −∞), (x ′|+∞)]× [(y | −∞), (y ′|+∞)]

Page 142: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 10

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y) p̂ =((x |y), (y |x)

)unique coordin.

range R = [x , x ′]× [y , y ′]

R̂ = [(x | −∞), (x ′|+∞)]× [(y | −∞), (y ′|+∞)]

Show:

Page 143: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 11

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y) p̂ =((x |y), (y |x)

)unique coordin.

range R = [x , x ′]× [y , y ′]

R̂ = [(x | −∞), (x ′|+∞)]× [(y | −∞), (y ′|+∞)]

p ∈ R ⇔ p̂ ∈ R̂Show:

Page 144: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 12

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y) p̂ =((x |y), (y |x)

)unique coordin.

range R = [x , x ′]× [y , y ′]

R̂ = [(x | −∞), (x ′|+∞)]× [(y | −∞), (y ′|+∞)]

p ∈ R ⇔ p̂ ∈ R̂Show:

This removes our assumption about the input points being ingeneral position.

Page 145: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

15 - 13

General Sets of Points

Idea: use composite numbers (a|b) with lex order

p = (x , y) p̂ =((x |y), (y |x)

)unique coordin.

range R = [x , x ′]× [y , y ′]

R̂ = [(x | −∞), (x ′|+∞)]× [(y | −∞), (y ′|+∞)]

p ∈ R ⇔ p̂ ∈ R̂Show:

This removes our assumption about the input points being ingeneral position.

We can use kd-trees and range trees for any set of points;no matter how many points have the same x- or y-coordinates.

Page 146: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 1

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

[dBCvKO’08]

Page 147: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 2

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

[dBCvKO’08]

Page 148: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 3

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

We allow n log m bits extra space.

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

[dBCvKO’08]

Page 149: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 4

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

We allow n log m bits extra space.

Task 2: Assuming that task 1 can be solved, speed up2d range queries: O(k + log2 n)→ O(k + log n) time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

[dBCvKO’08]

Page 150: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 5

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

We allow n log m bits extra space.

Task 2: Assuming that task 1 can be solved, speed up2d range queries: O(k + log2 n)→ O(k + log n) time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

link a ∈ A withsmallest b ≥ ain B

[dBCvKO’08]

Page 151: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 6

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

We allow n log m bits extra space.

Task 2: Assuming that task 1 can be solved, speed up2d range queries: O(k + log2 n)→ O(k + log n) time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

query withrange [20, 65]

link a ∈ A withsmallest b ≥ ain B

[dBCvKO’08]

Page 152: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 7

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

We allow n log m bits extra space.

Task 2: Assuming that task 1 can be solved, speed up2d range queries: O(k + log2 n)→ O(k + log n) time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

23

query withrange [20, 65]

link a ∈ A withsmallest b ≥ ain B

[dBCvKO’08]

Page 153: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 8

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

We allow n log m bits extra space.

Task 2: Assuming that task 1 can be solved, speed up2d range queries: O(k + log2 n)→ O(k + log n) time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

23

query withrange [20, 65]

link a ∈ A withsmallest b ≥ ain B

[dBCvKO’08]

Page 154: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 9

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

We allow n log m bits extra space.

Task 2: Assuming that task 1 can be solved, speed up2d range queries: O(k + log2 n)→ O(k + log n) time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

23

query withrange [20, 65]

link a ∈ A withsmallest b ≥ ain B

[dBCvKO’08]

Page 155: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 10

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

We allow n log m bits extra space.

Task 2: Assuming that task 1 can be solved, speed up2d range queries: O(k + log2 n)→ O(k + log n) time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

23

query withrange [20, 65]

link a ∈ A withsmallest b ≥ ain B

[dBCvKO’08]

Page 156: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 11

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

We allow n log m bits extra space.

Task 2: Assuming that task 1 can be solved, speed up2d range queries: O(k + log2 n)→ O(k + log n) time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

23

query withrange [20, 65]

link a ∈ A withsmallest b ≥ ain B

[dBCvKO’08]

Page 157: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 12

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

We allow n log m bits extra space.

Task 2: Assuming that task 1 can be solved, speed up2d range queries: O(k + log2 n)→ O(k + log n) time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

23

30

query withrange [20, 65]

link a ∈ A withsmallest b ≥ ain B

[dBCvKO’08]

Page 158: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

16 - 13

Fractional Cascading

Task 1: Given sets B ⊂ A ⊂ N stored in sorted order in arraysA[1..n] and B[1..m], support 1d range queries in themultiset A ∪ B in k + 1 · log n time!

We allow n log m bits extra space.

Task 2: Assuming that task 1 can be solved, speed up2d range queries: O(k + log2 n)→ O(k + log n) time!

3 10 19 23 30 37 59 62 70 80 100 105

10 19 30 62 70 80 100

A

B

23

30

query withrange [20, 65]

link a ∈ A withsmallest b ≥ ain B

[dBCvKO’08]

Page 159: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

17 - 1

Layered Range Trees

(2,19)

(5,80)

(7,10)

(8,37)(12,3)

(15,99)

(17,62) (21,49)

(33,30)

(41,95)

(52,23)

(58,59)

(67,89)

(93,70)

2

5 7 8 12 15

17

21 33 41 52

58

672 93

67584121127

5

8

15

17

52

33

x

Page 160: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

17 - 2

Layered Range Trees

(2,19)

(5,80)

(7,10)

(8,37)(12,3)

(15,99)

(17,62) (21,49)

(33,30)

(41,95)

(52,23)

(58,59)

(67,89)

(93,70)

2

5 7 8 12 15

17

21 33 41 52

58

672 93

67584121127

5

8

15

17

52

33

3 99

10 19 37 80

30 49

80 3 99 3049

9962

6210 37

10 3719

8019

99623719103 80 30 4923 59 70 89 95

30 49233 95 59 70 89

23 95 59 70 89

95 23 89 70

3 10 19 23 30 37 49 59 62 70 80 89 95 99

x y

Page 161: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

17 - 3

Layered Range Trees

(2,19)

(5,80)

(7,10)

(8,37)(12,3)

(15,99)

(17,62) (21,49)

(33,30)

(41,95)

(52,23)

(58,59)

(67,89)

(93,70)

2

5 7 8 12 15

17

21 33 41 52

58

672 93

67584121127

5

8

15

17

52

33

3 99

10 19 37 80

30 49

80 3 99 3049

9962

6210 37

10 3719

8019

99623719103 80 30 4923 59 70 89 95

30 49233 95 59 70 89

23 95 59 70 89

95 23 89 70

3 10 19 23 30 37 49 59 62 70 80 89 95 99

[dBCvKO’08]

x y

Theorem: Let d ≥ 2 and let P be a set of n pts in Rd .Given O(n logd−1 n) preprocessing time & storage,d-dim range queries on P can be answered inO(k + logd−1 n) time.

Page 162: Algorithms for GIS€¦ · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company 2.000 3.000 e 19.500.000 19.559.999 G. Ometer born: salary: 2.500 e 1.000 1953-08-20

17 - 4

Layered Range Trees

(2,19)

(5,80)

(7,10)

(8,37)(12,3)

(15,99)

(17,62) (21,49)

(33,30)

(41,95)

(52,23)

(58,59)

(67,89)

(93,70)

2

5 7 8 12 15

17

21 33 41 52

58

672 93

67584121127

5

8

15

17

52

33

3 99

10 19 37 80

30 49

80 3 99 3049

9962

6210 37

10 3719

8019

99623719103 80 30 4923 59 70 89 95

30 49233 95 59 70 89

23 95 59 70 89

95 23 89 70

3 10 19 23 30 37 49 59 62 70 80 89 95 99

[dBCvKO’08]

x y

Theorem: Let d ≥ 2 and let P be a set of n pts in Rd .Given O(n logd−1 n) preprocessing time & storage,d-dim range queries on P can be answered inO(k + logd−1 n) time.


Recommended