Beruflich Dokumente
Kultur Dokumente
T:
l!
void!
rangesearch(Link root, Key searchrange[],
Key subtreerange[], List results)!
!
T:
k!
d!
a!
p!
f!
z!
m!
l!
n!
searchrange, subtreerange!
p!
z!
m!
n!
results:!
z!
n!
m!
p!
[, 0], and the range for the right subtree half open,
e.g., (0, +]!
Multidimensional Search!
Example applications:!
!
!
!
!
!
A k-d tree can handle all these queries with O(log n)
insert and search times (it can also handle partial,
range, and approximate matches)!
k-d Trees!
A k-d tree is a binary search tree (not covered in
textbook, link to original 1975 paper on syllabus)!
At each level of the k-d tree, keys from a different search
dimension is used as the discriminator!
keys for any given level are all from the same dimension indicated by
the discriminator!
nodes on the left subtree of a node have keys with value < the
nodes key value along this dimension!
nodes on the right subtree have keys with value > the nodes key
value along this dimension!
T:
G(70, 85)!
disc!
x!
!
A!
B(10, 70)!
B!
y!
!
C!
H(30, 60)!
A(50, 50)!
D!
C(80, 15)!
F!
G!
H!
D(25, 20)!
F(60, 10)!
E!
100
x!
!
!
y!
void kdTree::!
insert(Link &root, Item newitem, int disc)!
{!
if (root == NULL) {!
root = new Node(newitem);!
return;!
}!
if (newitem.key[disc] < root->item.key[disc]) // or <=!
insert(root->left, newitem, (disc+1)%dim);!
else if (newitem.key[disc] > root->item.key[disc])!
insert(root->right, newitem, (disc+1)%dim);!
}!
20,45!
80,40! x!
10,35!
20,20!
90,60! y!
50,30!
50,30! x!
60,80! y!
20,45!
80,40! x!
10,35!
20,20!
60,20! x!
90,60! y!
50,30!
x! 60,20!
replacement!
x-minimum!
y!
70,10!
50,30! x!
y! 70,10!
80,40! x!
10,35!
20,20!
60,20!
x! 60,20!
y! 70,10!
delete (60,20)!
20,20!
90,60! y!
replacement!
60,80! y!
60,20!
x! 70,10!
20,20!
90,60! y!
60,20!
x! 60,20!
replacement!
y-maximum!
replacement!
x-minimum!
y! 70,10!
delete (35,60)!
35,60! x!
80,40! x!
20,20!
80,40! x!
10,35!
20,45!
10,35!
90,60! y!
50,30!
x! 60,20!
50,30! x!
60,80! y!
20,45!
80,40! x!
10,35!
60,80! y!
20,45!
y! 70,10!
delete (50,30)!
60,80! y!
20,45!
replacement!
y-maximum!
50,30! x!
90,60! y!
70,10!
60,80! y!
20,45!
80,40! x!
10,35!
20,20!
50,30!
60,20! x!
70,10! y!
90,60! y!
50,30! x!
60,80! y!
20,45!
80,40! x!
10,35!
20,20!
60,20!
x! 70,10!
90,60! y!
searchrange[]
Split along!
plane!
Split along!
plane!
Split along!
plane!
partition space!
an item that spans multiple partitions are stored in all
Example usages:!
occlusion culling and collision detection in computer
graphics, computer games, robot motion planning!
1b
1a
Plane 1b
Plane 0
Plane 1a
Plane 2
B D
f
a
2
D
m
j
l
k
i
x=52
3
x=30
1
4
x=45
y=45"
a
b
4
x=45
y=45"
a
b
1
x=30
1
x=30
1
x=30
g
c
A 2
y=30 A
a, b , g a, b , g a, b , g
h, i, j, kh, i, j, k h, i, j, k
a, b , g
h, i, j, k
x=30
1
m
E
3
3E
x=52
x=52
l
n
3
x=52
2
y=30
m
E
j
l
l
2
y=30
B
d
2
A2
y=30 y=30
a, b , g
h, i, j, k
B4
y=45
d
B
d
E
3
x=52
l
n
4B
B4
y=45
d
y=45
d
2
y=30
2
y=30
E 3
l x=52
n
E
l
n
4
y=45
4
y=45
E
l
n
k
i
k
i
C
f
D
c
e
m
C
f
C D
f c
e
m
CD
fc
e
m
C
D
fc
e
m
D
c
e
m
2
y=30 A
a,
b
,
g
a, b , g
a, b , g
h, i, j, kh, i, j, k h, i, j, k
A
a, b , g
h, i, j, k
1
1
x=30 x=30
1
x=30
B
d
B4
y=45
d
2
A2
y=30 y=30
a, b , g
h, i, j, k
3
3E
x=52
x=52
l
n
3
x=52
C
f
B
d
D
c
e
m
1
x=30
1
x=30
E
3
x=52
l
n
4B
B4
y=45
d
y=45
d
C D
f c
e
m
C
f
E 3
l x=52
n
E
l
n
4
y=45
4
y=45
C
D
fc
CD
fc
e
m
E
l
n
A 2
y=30 A
a,
b
, g a, b , g
a, b , g
h,
i,
j,
k h, i, j, k
h, i, j, k
A
B
d
B4
y=45
d
C
f
B
d
D
c
e
m
1
x=30
1
x=30
2
A2
y=30 y=30
a, b , g
h, i, j, k
3
3E
x=52
x=52
l
n
3
x=52
E
3
x=52
l
n
4B
B4
y=45
d
y=45
d
C
f
4
x=45
y=45"
D
g
c
m
E
2
y=30
l
k
i
1
1
x=30 x=30
1
x=30
D
c
e
m
e
m
A
a
So it needs to navigate
around c!
2
y=30
2
y=30
x=52
3
x=30
1
C D
f c
e
m
E 3
l x=52
n
E
l
n
4
y=45
4
y=45
CD
fc
e
m
2
y=30
2
y=30
C
D
fc
e
m
E
l
n
D
c
e
m
4
x=45
y=45"
a
b
D
To get to e, it needs to
find if there are other
objects in its way!
Since it is now in the
same region as e, it only
needs to compute
intersection with other
objects in the region: c
and m!
x=52
3
x=30
1
m
E
j
l
k
i
2
y=30
Nearest-Neighbor Search!
In nearest-neighbor search the range is not
pre-specified and is in local space, e.g.,!
find the ATM machine nearest to me!
range not specified!!
find the k restaurants nearest to me!
[Known as the k-nearest neighbor (k-NN) problem]!
Fixed-Range Search!
The range searches we have considered all have
fixed ranges specified in world space, e.g.,!
of the whole alphabet space, search in range [mz]"
of the whole Cartesian space, search in range"
[(, 6), (, 8)]!
find all (or k) departures within 2 hours of noon!
relative to local point of reference!
but can be transformed into a fixed- and absolute-range query"
void!
nnsearch(Link root, int disc, Key reference,
double *searchradius,
Key subtreerange[], List results)!
E
[wikipedia]!
[wikipedia]!
[wikipedia]!
[Sellars]!
[Sellars]!
[Sellars]!
[Sellars]!
[Sellars]!
Programming Assignment 2!
Topic: Location-Based Services!
Consumer and advertiser expenditure on
location-based services (LBS) to approach $10B
by 2016, with search advertising accounting for
just over 50%. LA Times, 6/10/11!
Due date: Thu, 10/13, 10:00 pm!
To be done individually (no group or team)!
No STL (iostream and string are allowed,
they are part of the C++ standard library, not part
of STL)!
[Sellars]!
Geographic
Coordinate System!
A point on earth is given by its
latitude and longitude!
!
Latitude 0 is at the Equator!
Latitude 90 is at the North Pole!
Latitude 90 is at the South Pole!
!
Longitude 0 is the Prime Meridian!
Longitude 180 is a the
International Dateline"
(more or less)!
Latitude!
Kinshasa!
15.322222!
Istanbul!
41.01224!
28.976018!
Mumbai!
18.975!
72.825833!
Jakarta!
6.2!
106.8!
Shanghai!
31.2!
121.5!
Queenstown!
Honolulu!
45.031111!
168.6625!
21.308889!
157.826111!
San Francisco!
37.7793!
122.4192!
42.281389!
83.748333!
22.908333!
43.196389!
Reykjavik!
64.133333!
21.933333!
Casablanca!
33.533333!
7.583333!
Ann Arbor!
Rio de Janeiro!
Problem Specification!
Read in a database of location data!
Each entry consists of:!
lat lon name tag!
where the lat and lon are floats and!
name and tag are single words!
To simplify the assignment, we limit acceptable
latitude to between 0 and 90 and we limit
acceptable longitude to between 0 and 180
(covering North America)!
Longitude!
4.325!
Problem Specification!
Problem Specification!
Problem Specification!
3. nearest neighbor query, led by an n sign:!
n 42.2785 83.7461 bank"
n 42.2785 83.7461 bookstore"
n 42.3033 83.7078 bank"
n 42.3034 83.7078 bank"
@ 42.2982 83.7200"
@ 42.2984 83.7195"
@ 42.2980 83.7109"
Problem Specification!
The two floats following the @ and r signs
should be obvious!
!
The third float following the r sign specifies the
search range from the provided location, in degree!
!
We limit the range to be at most 0.5 degree from
the provided location, so in total the search range is
limited to 1 degree (enough to cover the New York
Metropolitan Area or the Tokyo/Yokohama
megalopolis, the two largest urban conglomerations
in the world, by area)!
Problem Specification!
Problem Specification!
The tag following the lat/lon in nearest location
search is the queried tag, i.e., a record matches
the query only if the record contains the same tag
as the queried tag!
!
To simplify implementation, we will use a
bounding box covering the search radius to
perform nearest neighbor search!
!
void!
rangesearch(Link root, int disc, Key reference,
double *searchradius, Key bbox[],
Key subtreerange[], List results)!
[Sellars]!
Output!
k-d Tree!
Location-Based Search!
Finding exact matches:!
Files Organization!
How would you organize your code into files?!
Alternative 1: main.cpp (NOT)!
Alternative 2: main.cpp, hash.h, hash.cpp,
Time Requirements!
How long does it take to do PA2?!
Task!
kdtree insert!
Lines of
Code!
% Total Time!
n/a (2 days)!
42!
4!
132!
27!
88!
7!
142!
23!
130!
38!
102!
+28!