Computaional Geometry
Summer semester 2020
Overview
• Intro and problem definition
• Kd trees
Overview
• Intro and problem definition
• Kd trees
• Range trees
• Fractional cascading
• Kd trees
• Range trees
Overview
• Intro and problem definition
• Fractional cascading
• Kd trees
• Range trees
• Priority search trees
tons sold last year
cocoa
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?
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
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
3. on a Word RAM.
Problem definition
Given n points in Z
dor 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
3. on a Word RAM.
output size
Static: preprocess and answer queries
Dynamic: update insertions and deletions in poly(log(n))
The 1-dimensional problem
Query: [x, x
0]
Report next until exceeds x
0The 1-dimensional problem
Query: [x, x
0]
Option 1. Use sorted array:
Binary search for x
Report next until exceeds x
0Static
only
Report next until exceeds x
0Static Option 2. Binary search tree:
7 5
3
1 10 16
8 7
8 5
3
1 10
The 1-dimensional problem
Query: [x, x
0]
Option 1. Use sorted array:
Binary search for x
Report next until exceeds x
0Static
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
Search for x , reporting left child subtrees
Split(x, x
0)
π(x) π (x
0)
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)
Search for x , reporting left child subtrees
Split(x, x
0)
π(x) π (x
0)
Space = O(n)
Preprocess = O(n log n)
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)
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)
Kd trees
Bentley 1975
Kd trees in R 2
Assume distinct x- and y-coords Idea:
p
1p
2p
3p
4p
5p
6p
7p
8p
9p
10`
1p
1p
2p
3p
4p
5p
6p
7p
8p
9p
10`
1`
1Kd trees in R 2
Assume distinct x- and y-coords
Idea:
`
1`
2`
3p
1p
2p
3p
4p
5p
6p
7p
8p
9p
10`
1`
1`
2`
3' median x-coordinate
`
1`
2`
3`
4`
5`
6`
7p
1p
2p
3p
4p
5p
6p
7p
8p
9p
10`
1`
1`
2`
3`
4`
5`
6`
7Kd trees in R 2
Assume distinct x- and y-coords
Idea:
`
1`
2`
3`
4`
5`
6`
7`
8`
9p
1p
2p
3p
4p
5p
6p
7p
8p
9p
10`
1`
1`
2`
3`
4`
5`
6`
7p
9p
1p
5`
9p
10p
2p
7`
8' median x-coordinate
`
1`
2`
3`
4`
5`
6`
7`
8`
9p
1p
2p
3p
4p
5p
6p
7p
8p
9p
10`
1`
1`
2`
3`
4`
5`
6`
7p
9p
1p
5p
10p
2p
7p
3p
4p
6p
8`
9`
8Kd 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)
T (n) = 2T (n/2) + O(n) ⇒ O(n log n)
`
1`
2`
4`
5`
6`
7`
8`
9p
1p
2p
3p
4p
5p
6p
7p
8p
9p
10Each ` has a rectangular region
`
3Kd 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`
9p
1p
2p
3p
4p
5p
6p
7p
8p
9p
10Each ` has a rectangular region
`
3reg (`
1) = R
2Child regions of `:
separated by `
Querying a kd tree
query
Report subtree if reg (`) ⊆ Q
Report subtree if reg (`) ⊆ Q
If reg (`) intersects ∂Q: check!
Querying a kd tree
query
Report subtree if reg (`) ⊆ Q If reg (`) intersects ∂Q: check!
Searching, reporting covered regions: O(log n + k)
Report subtree if reg (`) ⊆ Q If reg (`) intersects ∂Q: check!
Searching, reporting covered regions: O(log n + k)
How many regions can intersect ∂Q?
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(`)
I (n): vert. line intersects this many regions in kd tree of size n
Two grandchild regions are not in- tersected
`
lc(`)
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(`)
Kd tree query time
⇒ I (n) = O( √ n)
I (n) = 2 + 2I (n/4) = 2 + 2 · (2 + 2 · (. . . )) = 2 + 4 + · · · + 2
log4 nQ has ≤ 2 vertical and ≤ 2 horizontal sides
⇒ ∂Q intersects O( √
n) regions
⇒ I (n) = O( n)
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 nRange 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
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
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
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
Querying a 2-dim range tree
Do 1-dim range query on x-coordiantes Find O(log n) reported subtree roots
π (x) π(x
0)
Do 1-dim query on each of the O(log n) selected y-trees
π (x) π(x
0)
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
2n + k ) Total query time:
π (x) π(x
0)
Do 1-dim query on each of the O(log n) selected y-trees
P
v
O(log n + k
v) = O(log
2n + k ) Total query time:
In R
d, it gives O(log
dn + k )
π (x) π(x
0)
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
2n + k ) Total query time:
In R
d, it gives O(log
dn + k ) But! space is up to Θ(n log
d−1n)
π (x) π(x
0)
Each level stores chunks of
a (d − 1)-dim range tree
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
⇒ 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|∞)
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
∗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
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
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
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
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
vhas:
At node v, array A
vhas:
• Points in decendant leafs of v sorted by y-coords
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
vhas:
• Points in decendant leafs of v sorted by y-coords
• Each entry (a, b) ∈ A
vhas:
– Pointer to entry in A
lc(v)with smallest y-coord ≥ b
– Pointer to entry in A
rc(v)with smallest y-coord ≥ b
At node v, array A
vhas:
• Points in decendant leafs of v sorted by y-coords
• Each entry (a, b) ∈ A
vhas:
– 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
v5 21 89 95
3 99
y-coords of entries
A
lc(v)A
rc(v)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
vhas:
• Points in decendant leafs of v sorted by y-coords
• Each entry (a, b) ∈ A
vhas:
– 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
v5 21 89 95
3 99
y-coords of entries
A
lc(v)A
rc(v)N U LL
At node v, array A
vhas:
• Points in decendant leafs of v sorted by y-coords
• Each entry (a, b) ∈ A
vhas:
– 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
v5 21 89 95
3 99
y-coords of entries
A
lc(v)A
rc(v)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
vhas:
• Points in decendant leafs of v sorted by y-coords
• Each entry (a, b) ∈ A
vhas:
– 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
v5 21 89 95
3 99
y-coords of entries
A
lc(v)A
rc(v)N U LL