• Keine Ergebnisse gefunden

• Kd trees

N/A
N/A
Protected

Academic year: 2021

Aktie "• Kd trees"

Copied!
97
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Computaional Geometry

Summer semester 2020

(2)

Overview

• Intro and problem definition

(3)

• Kd trees

(4)

Overview

• Intro and problem definition

• Kd trees

• Range trees

(5)

• Fractional cascading

• Kd trees

• Range trees

(6)

Overview

• Intro and problem definition

• Fractional cascading

• Kd trees

• Range trees

• Priority search trees

(7)

tons sold last year

cocoa

(8)

The obvious applciation

tons sold last year sugar

cocoa Database of cakes.

Which cakes have

• sugar content [0.12,0.17]

• cocoa content [0.05,0.1]

• and sold btw. 3 and 4 tons

last year?

(9)

tons sold last year cocoa

Which cakes have

• sugar content [0.12,0.17]

• cocoa content [0.05,0.1]

• and sold btw. 3 and 4 tons last year?

Orthogonal range query

(10)

The obvious applciation

tons sold last year sugar

cocoa Database of cakes.

Which cakes have

• sugar content [0.12,0.17]

• cocoa content [0.05,0.1]

• and sold btw. 3 and 4 tons last year?

Orthogonal range query

Task: support such queries efficiently

(11)

3. on a Word RAM.

(12)

Problem definition

Given n points in Z

d

or Q

d

,

1. Preprocess them in O(n) e time and space to

2. support orthogonal range queries in poly(log n) + O(k) 3. on a Word RAM.

output size

(13)

3. on a Word RAM.

output size

Static: preprocess and answer queries

Dynamic: update insertions and deletions in poly(log(n))

(14)

The 1-dimensional problem

Query: [x, x

0

]

(15)

Report next until exceeds x

0

(16)

The 1-dimensional problem

Query: [x, x

0

]

Option 1. Use sorted array:

Binary search for x

Report next until exceeds x

0

Static

only

(17)

Report next until exceeds x

0

Static Option 2. Binary search tree:

7 5

3

1 10 16

8 7

8 5

3

1 10

(18)

The 1-dimensional problem

Query: [x, x

0

]

Option 1. Use sorted array:

Binary search for x

Report next until exceeds x

0

Static

only

Option 2. Binary search tree:

7 5

3

1 10 16

8 7

8 5

3 1

P in the leaves Query: [4, 11]

10

inner vertex = largest value in left child’s subtree

(19)

Search for x , reporting left child subtrees

Split(x, x

0

)

π(x) π (x

0

)

(20)

Answering a query

Binary search for Split(x, x

0

)

Search for x, reporting right child subtrees Search for x

0

, reporting left child subtrees

Dynamic solution in R 1

Split(x, x

0

)

π(x) π (x

0

)

Space = O(n)

(21)

Search for x , reporting left child subtrees

Split(x, x

0

)

π(x) π (x

0

)

Space = O(n)

Preprocess = O(n log n)

(22)

Answering a query

Binary search for Split(x, x

0

)

Search for x, reporting right child subtrees Search for x

0

, reporting left child subtrees

Dynamic solution in R 1

Split(x, x

0

)

π(x) π (x

0

)

Space = O(n)

Preprocess = O(n log n)

Update = O (log n)

(23)

Search for x , reporting left child subtrees

Split(x, x

0

)

π(x) π (x

0

)

Space = O(n)

Preprocess = O(n log n) Update = O (log n)

Query = O(log n + k)

(24)

Kd trees

Bentley 1975

(25)
(26)

Kd trees in R 2

Assume distinct x- and y-coords Idea:

p

1

p

2

p

3

p

4

p

5

p

6

p

7

p

8

p

9

p

10

(27)

`

1

p

1

p

2

p

3

p

4

p

5

p

6

p

7

p

8

p

9

p

10

`

1

`

1

(28)

Kd trees in R 2

Assume distinct x- and y-coords

Idea:

`

1

`

2

`

3

p

1

p

2

p

3

p

4

p

5

p

6

p

7

p

8

p

9

p

10

`

1

`

1

`

2

`

3

' median x-coordinate

(29)

`

1

`

2

`

3

`

4

`

5

`

6

`

7

p

1

p

2

p

3

p

4

p

5

p

6

p

7

p

8

p

9

p

10

`

1

`

1

`

2

`

3

`

4

`

5

`

6

`

7

(30)

Kd trees in R 2

Assume distinct x- and y-coords

Idea:

`

1

`

2

`

3

`

4

`

5

`

6

`

7

`

8

`

9

p

1

p

2

p

3

p

4

p

5

p

6

p

7

p

8

p

9

p

10

`

1

`

1

`

2

`

3

`

4

`

5

`

6

`

7

p

9

p

1

p

5

`

9

p

10

p

2

p

7

`

8

' median x-coordinate

(31)

`

1

`

2

`

3

`

4

`

5

`

6

`

7

`

8

`

9

p

1

p

2

p

3

p

4

p

5

p

6

p

7

p

8

p

9

p

10

`

1

`

1

`

2

`

3

`

4

`

5

`

6

`

7

p

9

p

1

p

5

p

10

p

2

p

7

p

3

p

4

p

6

p

8

`

9

`

8

(32)

Kd tree anatomy

Space: O(n)

Tree has O(log n) depth.

Preprocessing: use linear time median:

T (n) = 2T (n/2) + O(n) ⇒ O(n log n)

(33)

T (n) = 2T (n/2) + O(n) ⇒ O(n log n)

`

1

`

2

`

4

`

5

`

6

`

7

`

8

`

9

p

1

p

2

p

3

p

4

p

5

p

6

p

7

p

8

p

9

p

10

Each ` has a rectangular region

`

3

(34)

Kd tree anatomy

Space: O(n)

Tree has O(log n) depth.

Preprocessing: use linear time median:

T (n) = 2T (n/2) + O(n) ⇒ O(n log n)

`

1

`

2

`

4

`

5

`

6

`

7

`

8

`

9

p

1

p

2

p

3

p

4

p

5

p

6

p

7

p

8

p

9

p

10

Each ` has a rectangular region

`

3

reg (`

1

) = R

2

Child regions of `:

separated by `

(35)
(36)

Querying a kd tree

query

Report subtree if reg (`) ⊆ Q

(37)

Report subtree if reg (`) ⊆ Q

If reg (`) intersects ∂Q: check!

(38)

Querying a kd tree

query

Report subtree if reg (`) ⊆ Q If reg (`) intersects ∂Q: check!

Searching, reporting covered regions: O(log n + k)

(39)

Report subtree if reg (`) ⊆ Q If reg (`) intersects ∂Q: check!

Searching, reporting covered regions: O(log n + k)

How many regions can intersect ∂Q?

(40)

Regions interseted by the boundary

Claim. A vertical line intersects at most O( √

n) node regions.

I (n): vert. line intersects this many regions in kd tree of size n

`

lc(`)

(41)

I (n): vert. line intersects this many regions in kd tree of size n

Two grandchild regions are not in- tersected

`

lc(`)

(42)

Regions interseted by the boundary

Claim. A vertical line intersects at most O( √

n) node regions.

I (n): vert. line intersects this many regions in kd tree of size n

Two grandchild regions are not in- tersected

`

I (n) = 2 + 2I (n/4)

reg (`) and reg (leftchild(`)) Intersections in red subtrees

lc(`)

(43)
(44)

Kd tree query time

⇒ I (n) = O( √ n)

I (n) = 2 + 2I (n/4) = 2 + 2 · (2 + 2 · (. . . )) = 2 + 4 + · · · + 2

log4 n

(45)

Q has ≤ 2 vertical and ≤ 2 horizontal sides

⇒ ∂Q intersects O( √

n) regions

⇒ I (n) = O( n)

(46)

Kd tree query time

Q has ≤ 2 vertical and ≤ 2 horizontal sides

⇒ ∂Q intersects O( √

n) regions

⇒ I (n) = O( √ n)

Orthogonal range queries of a 2-dim kd tree take O ( √

n + k) time.

I (n) = 2 + 2I (n/4) = 2 + 2 · (2 + 2 · (. . . )) = 2 + 4 + · · · + 2

log4 n

(47)
(48)

Range tree in R 2

Binary search tree on x-xoordinates

Each node has a new BST for y-coords of descendant leaves

v

T (v ) BST on x

BST on descendants of v sorted for y-coord.

Auxiliary tree

(49)

v

T (v )

BST on descendants of v sorted for y-coord.

Construction:

• Presort on y-coord to array A Construct(v, A):

• Construct T (v ) using A

• Find median x, split A to A

lef t

, A

right

• Construct(lc(v ), A

lef t

), Construct(rc(v ), A

right

)

Auxiliary tree

(50)

Range tree in R 2

Binary search tree on x-xoordinates

Each node has a new BST for y-coords of descendant leaves

v

T (v ) BST on x

BST on descendants of v sorted for y-coord.

Construction:

• Presort on y-coord to array A Construct(v, A):

• Construct T (v ) using A

• Find median x, split A to A

lef t

, A

right

• Construct(lc(v ), A

lef t

), Construct(rc(v ), A

right

) O (n log n)

O(n) O(n)

Auxiliary tree

(51)

v

T (v )

BST on descendants of v sorted for y-coord.

Construction:

• Presort on y-coord to array A Construct(v, A):

• Construct T (v ) using A

• Find median x, split A to A

lef t

, A

right

• Construct(lc(v ), A

lef t

), Construct(rc(v ), A

right

) O (n log n)

O(n) O(n)

O(n log n)

Auxiliary tree

(52)

Querying a 2-dim range tree

Do 1-dim range query on x-coordiantes Find O(log n) reported subtree roots

π (x) π(x

0

)

(53)

Do 1-dim query on each of the O(log n) selected y-trees

π (x) π(x

0

)

(54)

Querying a 2-dim range tree

Do 1-dim range query on x-coordiantes Find O(log n) reported subtree roots

Do 1-dim query on each of the O(log n) selected y-trees

P

v

O(log n + k

v

) = O(log

2

n + k ) Total query time:

π (x) π(x

0

)

(55)

Do 1-dim query on each of the O(log n) selected y-trees

P

v

O(log n + k

v

) = O(log

2

n + k ) Total query time:

In R

d

, it gives O(log

d

n + k )

π (x) π(x

0

)

(56)

Querying a 2-dim range tree

Do 1-dim range query on x-coordiantes Find O(log n) reported subtree roots

Do 1-dim query on each of the O(log n) selected y-trees

P

v

O(log n + k

v

) = O(log

2

n + k ) Total query time:

In R

d

, it gives O(log

d

n + k ) But! space is up to Θ(n log

d−1

n)

π (x) π(x

0

)

Each level stores chunks of

a (d − 1)-dim range tree

(57)
(58)

Handling degeneracies

Composite numbers: a, b ∈ R → (a|b) Sorted lexicographically.

repalce p = (x, y ) with p

= ((x|y), (y|x)) for each p ∈ P

⇒ all first and second coords are distinct

(59)

⇒ all first and second coords are distinct Given query Q = [x, x

0

] × [y, y

0

], replace by

Q

=

(x| −∞), (x

0

|∞)

×

(y| −∞), (y

0

|∞)

(60)

Handling degeneracies

Composite numbers: a, b ∈ R → (a|b) Sorted lexicographically.

repalce p = (x, y ) with p

= ((x|y), (y|x)) for each p ∈ P

⇒ all first and second coords are distinct Given query Q = [x, x

0

] × [y, y

0

], replace by

Q

=

(x| −∞), (x

0

|∞)

×

(y| −∞), (y

0

|∞)

Observation.

p ∈ Q ⇔ p

∈ Q

(61)
(62)

Speeding up binary searches on subsets

2 3 5 8 13 21 34 55 89 92 95 99

8 13 34 55

2 92 95

Array and subarray, query [4, 45]

⇒ two binary searhces?

A

B

(63)

2 3 5 8 13 21 34 55 89 92 95 99

8 13 34 55

2 92 95

Add pointer from A[x] to the smallest in B larger than A[x]

A

B

(64)

Speeding up binary searches on subsets

2 3 5 8 13 21 34 55 89 92 95 99

8 13 34 55

2 92 95

Array and subarray, query [4, 45]

⇒ two binary searhces?

Add pointer from A[x] to the smallest in B larger than A[x]

N U LL A

B

(65)

2 3 5 8 13 21 34 55 89 92 95 99

8 13 34 55

2 92 95

Add pointer from A[x] to the smallest in B larger than A[x]

N U LL A

B

Binary search A, use pointer of smallest reported

(66)

Layered range trees in R 2

Idea:

Replace 2nd level BSTs for y-coords w/ sorted arrays A

v

+ 2 pointers per entry

At node v, array A

v

has:

(67)

At node v, array A

v

has:

• Points in decendant leafs of v sorted by y-coords

(68)

Layered range trees in R 2

Idea:

Replace 2nd level BSTs for y-coords w/ sorted arrays A

v

+ 2 pointers per entry

At node v, array A

v

has:

• Points in decendant leafs of v sorted by y-coords

• Each entry (a, b) ∈ A

v

has:

– Pointer to entry in A

lc(v)

with smallest y-coord ≥ b

– Pointer to entry in A

rc(v)

with smallest y-coord ≥ b

(69)

At node v, array A

v

has:

• Points in decendant leafs of v sorted by y-coords

• Each entry (a, b) ∈ A

v

has:

– Pointer to entry in A

lc(v)

with smallest y-coord ≥ b – Pointer to entry in A

rc(v)

with smallest y-coord ≥ b

2 3 5 8 13 21 34 55 89 92 95 99

8 13 34 55

2 92

A

v

5 21 89 95

3 99

y-coords of entries

A

lc(v)

A

rc(v)

(70)

Layered range trees in R 2

Idea:

Replace 2nd level BSTs for y-coords w/ sorted arrays A

v

+ 2 pointers per entry

At node v, array A

v

has:

• Points in decendant leafs of v sorted by y-coords

• Each entry (a, b) ∈ A

v

has:

– Pointer to entry in A

lc(v)

with smallest y-coord ≥ b – Pointer to entry in A

rc(v)

with smallest y-coord ≥ b

2 3 5 8 13 21 34 55 89 92 95 99

8 13 34 55

2 92

A

v

5 21 89 95

3 99

y-coords of entries

A

lc(v)

A

rc(v)

N U LL

(71)

At node v, array A

v

has:

• Points in decendant leafs of v sorted by y-coords

• Each entry (a, b) ∈ A

v

has:

– Pointer to entry in A

lc(v)

with smallest y-coord ≥ b – Pointer to entry in A

rc(v)

with smallest y-coord ≥ b

2 3 5 8 13 21 34 55 89 92 95 99

8 13 34 55

2 92

A

v

5 21 89 95

3 99

y-coords of entries

A

lc(v)

A

rc(v)

(72)

Layered range trees in R 2

Idea:

Replace 2nd level BSTs for y-coords w/ sorted arrays A

v

+ 2 pointers per entry

At node v, array A

v

has:

• Points in decendant leafs of v sorted by y-coords

• Each entry (a, b) ∈ A

v

has:

– Pointer to entry in A

lc(v)

with smallest y-coord ≥ b – Pointer to entry in A

rc(v)

with smallest y-coord ≥ b

2 3 5 8 13 21 34 55 89 92 95 99

8 13 34 55

2 92

A

v

5 21 89 95

3 99

y-coords of entries

A

lc(v)

A

rc(v)

N U LL

(73)
(74)

Querying a layered range tree

Query: [x, x

0

] × [y, y

0

]

Search top (x-)tree for x and x

0

.

At v = Split(x, x

0

), binary search for y in A

v

→ A

v

.f ind(y)

Stepping to lc(v ), follow left pointer from A

v

.f ind(y)

(75)

Stepping to lc(v ), follow left pointer from A

v

.f ind(y)

⇒ Maintaining position in A

lc(v)

at each step takes O(1) time!

If w is root of a selected subtree:

Reporting from T (w) can be done from A

w

in O(1 + k

w

) time.

(76)

Querying a layered range tree

Query: [x, x

0

] × [y, y

0

]

Search top (x-)tree for x and x

0

.

At v = Split(x, x

0

), binary search for y in A

v

→ A

v

.f ind(y) Stepping to lc(v ), follow left pointer from A

v

.f ind(y)

⇒ Maintaining position in A

lc(v)

at each step takes O(1) time!

If w is root of a selected subtree:

Reporting from T (w) can be done from A

w

in O(1 + k

w

) time.

Total query time: O(log n + k )

(77)
(78)

Priority search tree

Priority queue (insert, pop max) +

Binary search tree

(79)

Construction:

• root r stores point with max y-coord

med

x

(root) := median x-coordinate of P \ {r}

(80)

Priority search tree

Priority queue (insert, pop max) +

Binary search tree Construction:

• root r stores point with max y-coord

med

x

(root) := median x-coordinate of P \ {r}

• left subtree: points p ∈ P \ {r} with p

x

≤ med

x

(r)

(81)

Construction:

• root r stores point with max y-coord

med

x

(root) := median x-coordinate of P \ {r}

• left subtree: points p ∈ P \ {r} with p

x

≤ med

x

(r)

• right subtree: points p ∈ P \ {r } with p

x

> med

x

(r)

(82)

Priority search tree

Priority queue (insert, pop max) +

Binary search tree Construction:

• root r stores point with max y-coord

med

x

(root) := median x-coordinate of P \ {r}

• left subtree: points p ∈ P \ {r} with p

x

≤ med

x

(r)

• right subtree: points p ∈ P \ {r } with p

x

> med

x

(r)

(83)

Construction:

• root r stores point with max y-coord

med

x

(root) := median x-coordinate of P \ {r}

• left subtree: points p ∈ P \ {r} with p

x

≤ med

x

(r)

• right subtree: points p ∈ P \ {r } with p

x

> med

x

(r)

(84)

Priority search tree

Priority queue (insert, pop max) +

Binary search tree Construction:

• root r stores point with max y-coord

med

x

(root) := median x-coordinate of P \ {r}

• left subtree: points p ∈ P \ {r} with p

x

≤ med

x

(r)

• right subtree: points p ∈ P \ {r } with p

x

> med

x

(r)

Space: O(n)

Construction: O(n log n)

(85)

(x, y) (x

0

, y)

(86)

Answering 3-sided queries

Wlog queries of the type [x, x

0

] × [y, ∞]

(x, y) (x

0

, y)

To answer the query from root = v :

π (x) π (x

0

)

Report subtree of v :

O(1 + k

v

)

(87)

(x, y) (x

0

, y) π (x) π (x

0

)

Report subtree of v : O(1 + k

v

)

+ check each node stored

on the search paths!

(88)

Answering 3-sided queries

Wlog queries of the type [x, x

0

] × [y, ∞]

(x, y) (x

0

, y)

To answer the query from root = v :

Query time: O(log n + k )

π (x) π (x

0

)

Report subtree of v : O(1 + k

v

)

+ check each node stored

on the search paths!

(89)

Static Dynamic

s:O(n)

q:O (log

ε

n + k log

ε

n) or

s:O(n log log n),

q:O (log log n + k log log n) or

s:O (n log

ε

n), q:O (log log n + k)

s:O (n log

2/3+o(1)

n) q:O (

log loglognn

+ k) u:O (log

2/3+o(1)

n)

Chan–Larsen–P˘ atras

,

cu Chan–Tsakalidis

amortized

(90)

Range searching axis-parallel intervals

→ intervals ending in Q:

range searching on endpoints

→ intervals ”crossing” Q?

(91)

range searching on endpoints

→ intervals ”crossing” Q?

if horizontal, they cross left

boundary of Q

(92)

Range searching axis-parallel intervals

→ intervals ending in Q:

range searching on endpoints

→ intervals ”crossing” Q?

if horizontal, they cross left boundary of Q

Given a vertical query segment, report all horizontal segments

it is crossing.

(93)

range searching on endpoints

→ intervals ”crossing” Q?

if horizontal, they cross left boundary of Q

Given a vertical query segment, report all horizontal segments it is crossing.

line

(94)

Range searching axis-parallel intervals

→ intervals ending in Q:

range searching on endpoints

→ intervals ”crossing” Q?

if horizontal, they cross left boundary of Q

Given a vertical query segment, report all horizontal segments it is crossing.

line

⇔ in R

1

, which intervals contain query point q?

(95)

right child left child

`

median x-coord

of endpoints

(96)

Interval trees (Edelsbrunner 1980 McCreight 1980)

right child left child

stored in root

` root has intervals intersected by `

sorted for left endpoints in a list (same for right)

median x-coord of endpoints

Preprocess: O(n log n)

Query ( R

1

): O(log n + k)

Space: O(n)

(97)

right child left child

`

median x-coord of endpoints

Preprocess: O(n log n)

Query ( R

1

): O(log n + k)

If segment query:

use priority search tree instead of list

Q

Space: O(n)

Referenzen

ÄHNLICHE DOKUMENTE

Using an ohmmeter from output common to each output (with output loads disconnected), check for shorted. rectifiers or

file system organization, may consider staying with the Bootstrap they are currently using instead of going through the lengthy reconfiguration cir reformatting

It is intended to be the firmware reference manuaL and to be used by the advanced microprogrammer of the Rikke MathiLda system.. WideStore, WS, is the common

register. It will increment the incoming pointers in the pointer RAM and the count in the proper location in the counter RAM. The proper queue in.. The

 Two years after the profound turmoil the West calls “The Arab Spring” erupted – the Greater Middle East, the Intifada’s and Arab Awakening are still escalating and

This is the stack overflo~ condition (STKOVF). In either case the information normally saved on the stack must be saved in a different location. The VISION

Table 2: The difference in contributions between participants B and C and the resulting probability of getting 16 points.. At first, each participant decides about a contribution

Model results as a function of time for (a) Large phytoplankton, (b) maximum PON export (sinking, aggregation and flux to higher trophic levels) at the base of the mixed layer and