Computational Geometry · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company...
Transcript of Computational Geometry · 2 - 2 Orthogonal Range Queries Example: Personnel management in a company...
1
Computational Geometry
Lecture #5
Orthogonal Range Queriesor
Fast Access to Data Bases
Dr. Philipp Kindermann Winter Semester 2018/19
2 - 1
Orthogonal Range QueriesExample: Personnel management in a company
2.000
3.000e
19.559.99919.500.000
1.000
2 - 2
Orthogonal Range QueriesExample: Personnel management in a company
2.000
3.000e
19.559.99919.500.000
G. Ometerborn:salary: 2.500e
1.000
1953-08-20
2 - 3
Orthogonal Range QueriesExample: Personnel management in a company
2.000
3.000e
19.559.99919.500.000
G. Ometerborn:salary: 2.500e
1.000
1953-08-20
2 - 4
Orthogonal Range QueriesExample: Personnel management in a company
2.000
3.000e
19.559.99919.500.000
G. Ometerborn:salary: 2.500e
1.000
1953-08-20
2 - 5
Orthogonal Range QueriesExample: Personnel management in a company
2.000
3.000e
19.559.99919.500.000
G. Ometerborn:salary: 2.500e
1.000
1953-08-20
2 - 6
Orthogonal Range QueriesExample: Personnel management in a company
19,500,000 19,559,999
3,000
4,000
2
4
e
2 - 7
Orthogonal Range QueriesExample: Personnel management in a company
19,500,000 19,559,999
3,000
4,000
2
4
Typical queries for data bases!
e
3 - 1
1D Range SearchingTask: Preprocess a finite set P ⊂ R such that for any
interval [x, x′] the set P ∩ [x, x′] can be reportedquickly. [2 min]
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]
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]
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]
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 max. 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]
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 max. 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]
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 max. 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]
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 max. 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]
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 max. 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]
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 max. 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]
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 max. 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]
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 max. 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]
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 max. 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]
4 - 1
1D Range Searching11
4 8 11 13 17 21 27
4 17
8 13 21
27
vsplit
4 - 2
1D Range Searching11
4 8 11 13 17 21 27
4 17
8 13 21
27
vsplit
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
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
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 points
stored in a subtree.
, where
11
4 8 11 13 17 21 27
4 17
8 13 21
27
vsplit
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 points
stored 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
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 points
stored 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
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 points
stored 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!
5 - 1
Extensions to 2DThink... [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.
5 - 2
Extensions to 2DThink... [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:
5 - 3
Extensions to 2DThink... [3 min]
• one tree;query path alternates between x- and y-coord.
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:
5 - 4
Extensions to 2DThink... [3 min]
• one tree;query path alternates between x- and y-coord.
• first-level tree for x-coordinates;many second-level trees for y-coord.
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:
5 - 5
Extensions to 2DThink... [3 min]
• one tree;query path alternates between x- and y-coord.
• first-level tree for x-coordinates;many second-level trees for y-coord.
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
}}
5 - 6
Extensions to 2DThink... [3 min]
• one tree;query path alternates between x- and y-coord.
• first-level tree for x-coordinates;many second-level trees for y-coord.
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!
5 - 7
Extensions to 2DThink... [3 min]
• one tree;query path alternates between x- and y-coord.
• first-level tree for x-coordinates;many second-level trees for y-coord.
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.
5 - 8
Extensions to 2DThink... [3 min]
• one tree;query path alternates between x- and y-coord.
• first-level tree for x-coordinates;many second-level trees for y-coord.
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.
6 - 1
Kd-Trees: Example
p4
p1
p5
p3
p2
p7
p9
p10
p6
p8
[dBCvKO’08]
6 - 2
Kd-Trees: Example
p
`1
4
p1
p5
p3
p2
p7
p9
p10
p6
p8
[dBCvKO’08]
6 - 3
Kd-Trees: Example
p
`1 `1
4
p1
p5
p3
p2
p7
p9
p10
p6
p8
[dBCvKO’08]
• Split any region that contains more than one point.
6 - 4
Kd-Trees: Example
p
`1
`2
`1
4
p1
p5
p3
p2
p7
p9
p10
p6
p8
[dBCvKO’08]
• Split any region that contains more than one point.
6 - 5
Kd-Trees: Example
p
`1
`2
`1
`24
p1
p5
p3
p2
p7
p9
p10
p6
p8
[dBCvKO’08]
• Split any region that contains more than one point.
6 - 6
Kd-Trees: Example
p
`1
`2
`4
`1
`24
p1
p5
p3
p2
p7
p9
p10
p6
p8
[dBCvKO’08]
• Split any region that contains more than one point.
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 region that contains more than one point.
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 region that contains more than one point.
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 region that contains more than one point.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
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 region that contains more than one point.• Horizontal split lines/segm. belong to the region below.
7 - 1
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
2
p
p
p
p
p
p
p
p
p
p
7 - 2
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
Pseudo-code:
2
p
p
p
p
p
p
p
p
p
p
7 - 3
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
Pseudo-code:
BuildKdTree(points P, int depth)
2
p
p
p
p
p
p
p
p
p
p
7 - 4
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
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)
2
p
p
p
p
p
p
p
p
p
p
7 - 5
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
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)
2
p
p
p
p
p
p
p
p
p
p
7 - 6
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
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)
2
p
p
p
p
p
p
p
p
p
p
7 - 7
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
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)
`′ 2
p
p
p
p
p
p
p
p
p
p
7 - 8
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
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)
2
p
p
p
p
p
p
p
p
p
p
7 - 9
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
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)
2
vleft vright
p
p
p
p
p
p
p
p
p
p
7 - 10
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
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)
2
vleft vright
p
p
p
p
p
p
p
p
p
p
7 - 11
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
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)
2
vleft vright
v`
p
p
p
p
p
p
p
p
p
p
7 - 12
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
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)
2
vleft vright
v`
p
p
p
p
p
p
p
p
p
p
7 - 13
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
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)
2
vleft vright
v`
p
p
p
p
p
p
p
p
p
p
7 - 14
Kd-Trees: Construction`
4
1
5
3
7
9
10
6
8
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)
2
vleft vright
v`
p
p
p
p
p
p
p
p
p
p
8 - 1
Kd-Trees: AnalysisConstruction time?
8 - 2
Kd-Trees: AnalysisConstruction time?
T(n) =
{O(1) if n = 1O(n) + 2T(dn/2e) else.
8 - 3
Kd-Trees: AnalysisConstruction time?
T(n) =
{O(1) if n = 1O(n) + 2T(dn/2e) else.
}= O(n log n)
8 - 4
Kd-Trees: AnalysisConstruction time?
T(n) =
{O(1) if n = 1O(n) + 2T(dn/2e) else.
}= O(n log n)
see Mergesort!
8 - 5
Kd-Trees: AnalysisConstruction time?
T(n) =
{O(1) if n = 1O(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 usesO(n) storage.
see Mergesort!
8 - 6
Kd-Trees: AnalysisConstruction time?
T(n) =
{O(1) if n = 1O(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 usesO(n) storage.
see Mergesort!
9 - 1
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 2
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 3
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 4
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 5
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 6
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 7
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 8
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 9
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 10
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 11
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 12
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 13
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 14
Kd-Trees: Querying
p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 15
Kd-Trees: Querying
*p1 p2
p2
p1
p3
p3 p4
p4 p5
p5
p6
p6
p7
p7 p8
p8
p9
p9
p10
p10
p11
p11
p12
p12 p13
p13
9 - 16
Kd-Trees: Querying
*p1 p2
p2
p1
p3
p3 p4
p4 p5
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 withan axis-parallel rectangle R takes O(k +
√n )
time, where k = |output|.
9 - 17
Kd-Trees: Querying
*p1 p2
p2
p1
p3
p3 p4
p4 p5
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 withan axis-parallel rectangle R takes O(k +
√n )
time, where k = |output|.Idea: O(
√n) regions of the kd-tree
intersect a vertical/horizontal line.
9 - 18
Kd-Trees: Querying
*p1 p2
p2
p1
p3
p3 p4
p4 p5
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 withan axis-parallel rectangle R takes O(k +
√n )
time, where k = |output|. in Rd
Idea: O(√
n) regions of the kd-treeintersect a vertical/horizontal line.
9 - 19
Kd-Trees: Querying
*p1 p2
p2
p1
p3
p3 p4
p4 p5
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 withan axis-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-treeintersect a vertical/horizontal line.
10
Extensions to 2D
• one tree;query path alternates between x- and y-coord.
• first-level tree for x-coordinates;many second-level trees for y-coord.
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.
11 - 1
Range Trees: Query Algorithm1. Search in main tree for x-coordinate
µ µ′
vsplit
11 - 2
Range Trees: Query Algorithm1. Search in main tree for x-coordinate2. For each node u
on the path from vsplit to µ:
µ µ′
vsplit
11 - 3
Range Trees: Query Algorithm1. Search in main tree for x-coordinate2. For each node u
on the path from vsplit to µ:
u
µ µ′
vsplit
11 - 4
Range Trees: Query Algorithm1. 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
11 - 5
Range Trees: Query Algorithm1. 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
11 - 6
Range Trees: Query Algorithm1. 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
11 - 7
Range Trees: Query Algorithm1. 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
11 - 8
Range Trees: Query Algorithm1. 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
11 - 9
Range Trees: Query Algorithm1. 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 µ′.
Tv
Search in auxiliary tree Tvfor points withy-coordinate ∈ [y, y′]
µ µ′
vsplit
12 - 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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
12 - 2
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TP
12 - 3
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TP
12 - 4
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TPpv
12 - 5
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TPpv
12 - 6
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TPpv
12 - 7
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TPpv
12 - 8
Range Trees: Construction
>
xmidv
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TPpv
12 - 9
Range Trees: Construction
>
vleft
Pleft Pright
xmidv
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TPpv
vright
12 - 10
Range Trees: Construction
>
vleft TPPleft Pright
xmidv
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TPpv
vright
12 - 11
Range Trees: Construction
>
vleft TPPleft Pright
xmidv
Running time?
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TPpv
vright
12 - 12
Range Trees: Construction
>
vleft TPPleft Pright
xmidv
Running time?
O(n log n) :-(
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TPpv
vright
12 - 13
Range Trees: Construction
>
vleft TPPleft Pright
xmidv
Running time?
O(n log n) :-(
Better:Pre-sort once,then build treebottom-upin linear time.
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TPpv
vright
12 - 14
Range Trees: Construction
>
vleft TPPleft Pright
xmidv
Running time?
O(n log n) :-(
Better:Pre-sort once,then build treebottom-upin linear time.
⇓Total
constructiontime O(n log n)
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 ≤ xmidPright =
vleft = Build2DRangeTree(Pleft)vright = Build2DRangeTree(Pright)
create node v:
return v
TPpv
vright
13 - 1
Range Trees: Space ConsumptionEach node v of the 1st-level tree has apointer to a 2nd-level tree Tv with|Tv| = Θ(|P(v)|).
13 - 2
Range Trees: Space ConsumptionEach node v of the 1st-level tree has apointer to a 2nd-level tree Tv with|Tv| = Θ(|P(v)|).
Q: What’s the total space consumptionof all 2nd-level trees?
13 - 3
Range Trees: Space ConsumptionEach node v of the 1st-level tree has apointer to 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?
13 - 4
Range Trees: Space ConsumptionEach node v of the 1st-level tree has apointer to 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?
13 - 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 apointer to 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?
13 - 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 apointer to 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?
14 - 1
Range Trees: Query time
∑u ∈ paths to µ and µ′
O(ku + log n)T(n, k) =
vu
µ µ′
vsplit
14 - 2
Range Trees: Query time
∑u ∈ paths to µ and µ′
O(ku + log n)
= O(∑u ku) + O(∑u log n)
T(n, k) =
vu
µ µ′
vsplit
14 - 3
Range Trees: Query time
∑u ∈ paths to µ and µ′
O(ku + log n)
= O(∑u ku) + O(∑u log n)
= O(k)
T(n, k) =
vu
µ µ′
vsplit
k︸ ︷︷ ︸
14 - 4
Range Trees: Query time
∑u ∈ paths to µ and µ′
O(ku + log n)
= O(∑u ku) + O(∑u log n)
= O(k) +
T(n, k) =
vu
µ µ′
vsplit
k︸ ︷︷ ︸
14 - 5
Range Trees: Query time
∑u ∈ paths to µ and µ′
O(ku + log n)
= O(∑u ku) + O(∑u log n)
= O(k) +
T(n, k) =
vu
µ µ′
vsplit
k︸ ︷︷ ︸
︸︷︷
︸h
14 - 6
Range Trees: Query time
∑u ∈ paths to µ and µ′
O(ku + log n)
= O(∑u ku) + O(∑u log n)
= O(k) + 2h ·O(log n)
T(n, k) =
vu
µ µ′
vsplit
k︸ ︷︷ ︸
︸︷︷
︸h
14 - 7
Range Trees: Query time
∑u ∈ paths to µ and µ′
O(ku + log n)
= O(∑u ku) + O(∑u log n)
= O(k) + 2h ·O(log n)
= O(k + log2 n)
T(n, k) =
vu
µ µ′
vsplit
k︸ ︷︷ ︸
︸︷︷
︸h
14 - 8
Range Trees: Query time
∑u ∈ paths to µ and µ′
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) =
vu
µ µ′
vsplit
k︸ ︷︷ ︸
︸︷︷
︸h
14 - 9
Range Trees: Query time
∑u ∈ paths to µ and µ′
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) =
vu
µ µ′
vsplit
k︸ ︷︷ ︸
︸︷︷
︸h
14 - 10
Range Trees: Query time
∑u ∈ paths to µ and µ′
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) =
vu
µ µ′
vsplit
k︸ ︷︷ ︸
︸︷︷
︸h
14 - 11
Range Trees: Query time
∑u ∈ paths to µ and µ′
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 Comp. Geom A&A
vu
µ µ′
vsplit
k︸ ︷︷ ︸
︸︷︷
︸h
15 - 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)
15 - 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
16 - 1
General Sets of PointsIdea: use composite numbers (a|b) with lex order
16 - 2
General Sets of PointsIdea: use composite numbers (a|b) with lex order
p = (x, y)
16 - 3
General Sets of PointsIdea: use composite numbers (a|b) with lex order
p = (x, y)
16 - 4
General Sets of PointsIdea: use composite numbers (a|b) with lex order
p = (x, y) p̂ =((x|y), (y|x)
)
16 - 5
General Sets of PointsIdea: use composite numbers (a|b) with lex order
p = (x, y) p̂ =((x|y), (y|x)
)
16 - 6
General Sets of PointsIdea: use composite numbers (a|b) with lex order
p = (x, y) p̂ =((x|y), (y|x)
)unique coordin.
16 - 7
General Sets of PointsIdea: use composite numbers (a|b) with lex order
p = (x, y) p̂ =((x|y), (y|x)
)unique coordin.
range R = [x, x′]× [y, y′]
16 - 8
General Sets of PointsIdea: use composite numbers (a|b) with lex order
p = (x, y) p̂ =((x|y), (y|x)
)unique coordin.
range R = [x, x′]× [y, y′]
16 - 9
General Sets of PointsIdea: 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′|+ ∞)]
16 - 10
General Sets of PointsIdea: 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:
16 - 11
General Sets of PointsIdea: 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:
16 - 12
General Sets of PointsIdea: 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 beingin general position.
16 - 13
General Sets of PointsIdea: 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 beingin general 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-coord.
17 - 1
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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]
17 - 2
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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]
17 - 3
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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]
17 - 4
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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]
17 - 5
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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 ∈ Awith smallestb ≥ a in B
[dBCvKO’08]
17 - 6
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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 ∈ Awith smallestb ≥ a in B
[dBCvKO’08]
17 - 7
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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 ∈ Awith smallestb ≥ a in B
[dBCvKO’08]
17 - 8
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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 ∈ Awith smallestb ≥ a in B
[dBCvKO’08]
17 - 9
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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 ∈ Awith smallestb ≥ a in B
[dBCvKO’08]
17 - 10
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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 ∈ Awith smallestb ≥ a in B
[dBCvKO’08]
17 - 11
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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 ∈ Awith smallestb ≥ a in B
[dBCvKO’08]
17 - 12
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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 ∈ Awith smallestb ≥ a in B
[dBCvKO’08]
17 - 13
Fractional CascadingTask 1: Given sets B ⊂ A ⊂N stored in sorted order in
arrays A[1..n] and B[1..m], support 1d rangequeries in the multiset 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 ∈ Awith smallestb ≥ a in B
[dBCvKO’08]
18 - 1
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
x17
18 - 2
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 10 23 30 37 49 59 62 70 80 89 95 99
x y1917
18 - 3
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
996237103 80 30 49 59 70 89 95
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19 23
17
18 - 4
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
996237103 80 30 49 59 70 89 95
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19 23
17
18 - 5
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
996237103 80 30 49 59 70 89 95
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19 23
17
18 - 6
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33 10 19 37 80 99
996237103 80 30 49 59 70 89 95
3
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19
62
23
17
18 - 7
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33 10 19 37 80 99
996237103 80 30 49 59 70 89 95
3
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19
62
23
17
18 - 8
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33 10 19 37 80 99
996237103 80 30 49 59 70 89 95
3 95 59 70 89
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19
62
23
23 30 49
17
18 - 9
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33 10 19 37 80 99
996237103 80 30 49 59 70 89 95
3 95 59 70 89
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19
62
23
23 30 49
17
18 - 10
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33 10 19 37 80 99
10 378019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19
62
23
23 30 49
17
18 - 11
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80 99
6210 378019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19
62
23
23 30 49
17
18 - 12
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
99
6210 378019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19
62
23
23 30 49
17
18 - 13
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19
62
23
23 30 49
17
18 - 14
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y19
19
62
23
23 30 49
17
18 - 15
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 16
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 17
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 18
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 19
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 20
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 21
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 22
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 23
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 24
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 25
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 26
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 27
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 28
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 29
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 30
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 31
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 32
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 33
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
17
18 - 34
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
→ (21, 49), (33, 30), (52, 23)
17
18 - 35
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
→ (21, 49), (33, 30), (52, 23)
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 beanswered in O(k + logd−1 n) time.
17
18 - 36
Layered Range Trees
2
5 7 8 12 15
17
21 33 41 52
58
672 93
67584121127
5
8
15
52
33
3 99
10 19 37 80
30 49
80 3 99 3049
99
6210 37
10 3719
8019
996237103 80 30 49 59 70 89 95
3 95 59 70 89
23 95 59 70 89
95 23 89 70
3 10 23 30 37 49 59 62 70 80 89 95 99
x y
[16, 20]× [53, 60]
19
19
62
23
23 30 49
→ (21, 49), (33, 30), (52, 23)
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 beanswered in O(k + logd−1 n) time.
17