How can I plot the physical location of devices in a way that will allow me to find all devices nearby to a given device?
Here's an example layout:
A
B C
D
G F
E
H
K I
J
Each letter represents a physical device. In this example, you can see that A
is nearby to both B
and C
. B
is nearby to both A
, D
and possibly C
.
I was thinking of putting each device into a database and pairing all possibly relationships. For example:
device | siblings
-------+---------
A | B,C
-------+---------
B | A,D,C
-------+---------
D | B,G,E
This way, when I need to find the开发者_StackOverflow社区 devices nearby to D
I can do:
SELECT siblings FROM devices WHERE device = 'D';
siblings = siblings.split(',')
for sibling in siblings:
# do something with each sibling device
I was wondering if there was a better way. Given that there could be large number of devices, I feel this method could get messy and, likely, buggy (human error of keeping track of the command separated list of siblings).
Any suggestions?
You should check out the Wikipedia article for Nearest-Neighbour Search for more ideas, but the best answer probably depends on the number of devices you're going to be working with.
KD-Trees and R-Trees will give you good performance even on large numbers of devices, but they'll require a good bit of programming / debugging (if you have to do them yourself, at least - they're reasonably well-known data structures, so you might be able to find a library somewhere).
If you've only got a small number of devices ("small" depends on all sorts of things, really), it might be faster to skip the complexity of a special data structure and just do a linear search through all your devices. And if you're only performing queries like this occasionally, a slow linear search might still be worth having clean, straightforward code.
And finally: All the above options assume that all devices are "connected" to one another. If only some devices are connected to each other, try using an Adjacency List to store those connections, and keep it sorted by distance - this should be faster than any of the above.
Hope this helps!
精彩评论