k-d trees and quad trees - university of washington€¦ · lecture 12. 2 range queries • think...
TRANSCRIPT
![Page 1: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/1.jpg)
K-D Trees and Quad Trees
James FogartyAutumn 2007
Lecture 12
![Page 2: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/2.jpg)
2
Range Queries
• Think of a range query.– “Give me all customers aged 45-55.”– “Give me all accounts worth $5m to $15m”
• Can be done in time ________.
• What if we want both:– “Give me all customers aged 45-55 with accounts
worth between $5m and $15m.”
![Page 3: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/3.jpg)
3
Geometric Data Structures
• Organization of points, lines, planes, etc in support of faster processing
• Applications– Map information– Graphics - computing object intersections– Data compression - nearest neighbor search– Decision Trees - machine learning
![Page 4: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/4.jpg)
4
k-d Trees
• Jon Bentley, 1975, while an undergraduate• Tree used to store spatial data.
– Nearest neighbor search.– Range queries.– Fast look-up
• k-d tree are guaranteed log2 n depth where n is the number of points in the set.– Traditionally, k-d trees store points in
d-dimensional space which are equivalent to vectors in d-dimensional space.
![Page 5: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/5.jpg)
5
Range Queries
x
y
ab
f
c
g h
ed
i
x
y
ab
f
c
g h
ed
i
Rectangular query Circular query
![Page 6: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/6.jpg)
6
x
y
ab
f
c
g h
ed
i
Nearest Neighbor Search
query
Nearest neighbor is e.
![Page 7: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/7.jpg)
7
k-d Tree Construction
• If there is just one point, form a leaf with that point.• Otherwise, divide the points in half by a line
perpendicular to one of the axes. • Recursively construct k-d trees for the two sets of
points.• Division strategies
– divide points perpendicular to the axis with widest spread.– divide in a round-robin fashion (book does it this way)
![Page 8: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/8.jpg)
8
x
y
k-d Tree Construction (1)
ab
f
c
g h
ed
i
divide perpendicular to the widest spread.
![Page 9: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/9.jpg)
9
y
k-d Tree Construction (2)
x
ab
c
g h
ed
i s1
s1
x
f
![Page 10: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/10.jpg)
10
y
k-d Tree Construction (3)
x
ab
c
g h
ed
i s1
s2y
s1
s2
x
f
![Page 11: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/11.jpg)
11
y
k-d Tree Construction (4)
x
ab
c
g h
ed
i s1
s2y
s3x
s1
s2
s3
x
f
![Page 12: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/12.jpg)
12
y
k-d Tree Construction (5)
x
ab
c
g h
ed
i s1
s2y
s3x
s1
s2
s3
a
x
f
![Page 13: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/13.jpg)
13
y
k-d Tree Construction (6)
x
ab
c
g h
ed
i s1
s2y
s3x
s1
s2
s3
a b
x
f
![Page 14: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/14.jpg)
14
y
k-d Tree Construction (7)
x
ab
c
g h
ed
i s1
s2y
s3x
s4y
s1
s2
s3
s4
a b
x
f
![Page 15: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/15.jpg)
15
y
k-d Tree Construction (8)
x
ab
c
g h
ed
i s1
s2y
s3x
s4y
s5x
s1
s2
s3
s4
s5
a b
x
f
![Page 16: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/16.jpg)
16
y
k-d Tree Construction (9)
x
ab
c
g h
ed
i s1
s2y
s3x
s4y
s5x
s1
s2
s3
s4
s5
a b
dx
f
![Page 17: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/17.jpg)
17
y
k-d Tree Construction (10)
x
ab
c
g h
ed
i s1
s2y
s3x
s4y
s5x
s1
s2
s3
s4
s5
a b
d ex
f
![Page 18: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/18.jpg)
18
y
k-d Tree Construction (11)
x
ab
c
g h
ed
i s1
s2y
s3x
s4y
s5x
s1
s2
s3
s4
s5
a b
d e
g
x
f
![Page 19: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/19.jpg)
19
y
k-d Tree Construction (12)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s5x
s1
s2
s3
s4
s5
s6
a b
d e
g
x
f
![Page 20: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/20.jpg)
20
y
k-d Tree Construction (13)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s5x
s1
s2
s3
s4
s5
s6
s7a b
d e
g
x
f
![Page 21: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/21.jpg)
21
y
k-d Tree Construction (14)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s5x
s1
s2
s3
s4
s5
s6
s7a b
d e
g c
x
f
![Page 22: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/22.jpg)
22
y
k-d Tree Construction (15)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s5x
s1
s2
s3
s4
s5
s6
s7a b
d e
g c f
x
f
![Page 23: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/23.jpg)
23
y
k-d Tree Construction (16)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f
x
f
![Page 24: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/24.jpg)
24
y
k-d Tree Construction (17)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h
x
f
![Page 25: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/25.jpg)
25
y
k-d Tree Construction (18)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h i
x
f
k-d tree cell
![Page 26: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/26.jpg)
26
2-d Tree Decomposition
1
2
3
![Page 27: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/27.jpg)
27
x
y
k-d Tree Splitting
ab
c
g h
ed
i a d g b e i c h fa c b d f e h g i
xy
sorted points in each dimension
f
• max spread is the max offx -ax and iy - ay.
• In the selected dimension themiddle point in the list splits the data.
• To build the sorted lists for the other dimensions scan the sortedlist adding each point to one of two sorted lists.
1 2 3 4 5 6 7 8 9
![Page 28: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/28.jpg)
28
x
y
k-d Tree Splitting
ab
c
g h
ed
i a d g b e i c h fa c b d f e h g i
xy
sorted points in each dimension
f
1 2 3 4 5 6 7 8 9
a b c d e f g h i0 0 1 0 0 1 0 1 1
indicator for each set
scan sorted points in y dimensionand add to correct set
a b d e g c f h iy
![Page 29: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/29.jpg)
29
k-d Tree Construction Complexity
• First sort the points in each dimension.– O(dn log n) time and dn storage.– These are stored in A[1..d,1..n]
• Finding the widest spread and equally divide into two subsets can be done in O(dn) time.
• We have the recurrence– T(n,d) < 2T(n/2,d) + O(dn)
• Constructing the k-d tree can be done in O(dn log n) and dn storage
![Page 30: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/30.jpg)
30
Node Structure for k-d Trees
• A node has 5 fields– axis (splitting axis)– value (splitting value)– left (left subtree)– right (right subtree)– point (holds a point if left and right children are null)
![Page 31: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/31.jpg)
31
Rectangular Range Query
• Recursively search every cell that intersects the rectangle.
![Page 32: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/32.jpg)
32
Rectangular Range Query (1)
y
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h
x
f
i
![Page 33: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/33.jpg)
33
Rectangular Range Query (2)
y
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h
x
f
i
![Page 34: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/34.jpg)
34
Rectangular Range Query (3)
y
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h
x
f
i
![Page 35: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/35.jpg)
35
Rectangular Range Query (4)
y
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h
x
f
i
![Page 36: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/36.jpg)
36
Rectangular Range Query (5)
y
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h
x
f
i
![Page 37: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/37.jpg)
37
Rectangular Range Query (6)
y
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h
x
f
i
![Page 38: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/38.jpg)
38
Rectangular Range Query (7)
y
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h
x
f
i
![Page 39: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/39.jpg)
39
Rectangular Range Query (8)
y
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h
x
f
i
![Page 40: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/40.jpg)
40
Rectangular Range Query
print_range(xlow, xhigh, ylow, yhigh :integer, root: node pointer) {
Case {root = null: return;
root.left = null:if xlow < root.point.x and root.point.x < xhighand ylow < root.point.y and root.point.y < yhigh
then print(root);else
if(root.axis = “x” and xlow < root.value ) or(root.axis = “y” and ylow < root.value ) then
print_range(xlow, xhigh, ylow, yhigh, root.left);
if (root.axis = “x” and xlow > root.value ) or(root.axis = “y” and ylow > root.value ) then
print_range(xlow, xhigh, ylow, yhigh, root.right);}}
![Page 41: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/41.jpg)
41
k-d Tree Nearest Neighbor Search
• Search recursively to find the point in the same cell as the query.
• On the return search each subtree where a closer point than the one you already know about might be found.
![Page 42: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/42.jpg)
42
y
k-d Tree NNS (1)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h i
x
f
query point
![Page 43: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/43.jpg)
43
y
k-d Tree NNS (2)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h i
x
f
query point
![Page 44: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/44.jpg)
44
y
k-d Tree NNS (3)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h i
x
f
query point
![Page 45: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/45.jpg)
45
y
k-d Tree NNS (4)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
g c f h i
x
f
query point
w
![Page 46: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/46.jpg)
46
y
k-d Tree NNS (5)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
c f h i
x
f
query point
w
g
![Page 47: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/47.jpg)
47
y
k-d Tree NNS (6)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
c f h i
x
f
query point
w
g
![Page 48: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/48.jpg)
48
y
k-d Tree NNS (7)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d e
c f h i
x
f
query point
w
g
![Page 49: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/49.jpg)
49
y
k-d Tree NNS (10)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d
c f h i
x
f
query point
w
e
g
![Page 50: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/50.jpg)
50
y
k-d Tree NNS (11)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d
c f h i
x
f
query point
w
e
g
![Page 51: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/51.jpg)
51
y
k-d Tree NNS (12)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d
c f h i
x
f
query point
w
e
g
![Page 52: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/52.jpg)
52
y
k-d Tree NNS (13)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d
c f h i
x
f
query point
w
e
g
![Page 53: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/53.jpg)
53
y
k-d Tree NNS (14)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d
c f h i
x
f
query point
w
e
g
![Page 54: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/54.jpg)
54
y
k-d Tree NNS (15)
x
ab
c
g h
ed
i s1
s2y y
s6
s3x
s4y
s7y
s8y
s5x
s1
s2
s3
s4
s5
s6
s7
s8
a b
d
c f h i
x
f
query point
w
e
g
![Page 55: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/55.jpg)
55
Notes on k-d NNS
• Has been shown to run in O(log n) average time per search in a reasonable model.
• Storage for the k-d tree is O(n).• Preprocessing time is O(n log n) assuming d
is a constant.
![Page 56: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/56.jpg)
56
y
Worst-Case for Nearest Neighbor Search
query point •Half of the points visited for a query
•Worst case O(n)
•But: on average (and in practice) nearest neighbor queries are O(log N)
x
![Page 57: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/57.jpg)
57
Quad Trees
• Space Partitioning
x
y
ab
c
g h
ed
i
f
![Page 58: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/58.jpg)
58
Quad Trees
• Space Partitioning
x
y
ab
c
g
ed f
![Page 59: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/59.jpg)
59
Quad Trees
• Space Partitioning
x
y
ab
c
g
ed f g e
d ba cf
![Page 60: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/60.jpg)
60
A Bad Case
x
y
ab
![Page 61: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/61.jpg)
61
Notes on Quad Trees
• Number of nodes is O(n(1+ log(Δ/n))) where n is the number of points and Δ is the ratio of the width (or height) of the key space and the smallest distance between two points
• Height of the tree is O(log n + log Δ)
![Page 62: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/62.jpg)
62
K-D vs Quad• k-D Trees
– Density balanced trees– Height of the tree is O(log n) with batch insertion– Good choice for high dimension– Supports insert, find, nearest neighbor, range queries
• Quad Trees– Space partitioning tree– May not be balanced– Not a good choice for high dimension– Supports insert, delete, find, nearest neighbor, range queries
![Page 63: K-D Trees and Quad Trees - University of Washington€¦ · Lecture 12. 2 Range Queries • Think of a range query. – “Give me all customers aged 45-55](https://reader035.vdocuments.us/reader035/viewer/2022071017/5fd12817cd72085c10717756/html5/thumbnails/63.jpg)
63
Geometric Data Structures
• Geometric data structures are common.• The k-d tree is one of the simplest.
– Nearest neighbor search– Range queries
• Other data structures used for– 3-d graphics models– Physical simulations