I've successfully implemented a way to generate Voronoi diagrams in 2 dimensions using Fortune's method. But now I'm trying to use it for nearest neighbor queries for a point (which is not one of the original points used to generate the diagram). I keep seeing people saying that it can be done in O(lg n) time (and I believe them), but I can't find a description of how it's actually done.
I'm familiar with binary searches, but I can't figure out a good criteria to guarantee that upper bound. I also figured maybe it could have to do with inserting the point into the diagram and updating surrounding cells, but can't think (or find) of a good way to do that.
Can anyone clue me in, or point to a place with a more thorough description?
I think that some kind of search structure has to be made from plane subdivision (Voronoi diagram), like Kirkpatrick's point location data structure.
Related
There are algorithms out there to find k nearest neighbors in many ways. I am eventually will have to apply these, however in my case, I can code my program to add points one-by-one rather than add all the points altogether, then run a algorithm. Is this make the problem easier, so that maybe I could use a tree, and add each node to a neighborhood tree or something. This seems like it would be faster than searching all the points linearly.
And in my program points will be moving constantly, so I will be required to update neighbors, that's why I thought it is better to use a tree or another construct to update records, rather than calculate nearest neighbors in every movement of these points. Do you know of such data structure ?
Maybe a graph datastructure/database is most appropriate due to the structural similarity.
Example: https://neo4j.com/graphgist/a7c915c8-a3d6-43b9-8127-1836fecc6e2f (I do not work for neo4j)
I´ve got the following theoretical problem:
I have an amount n of cuboids in 3-dimensional space.
They are aligned to the coordinate-system, so that one cuboid can be described via a point (x,y,z) and dimensions (dimX,dimY,dimZ).
I want to organize these cuboids in a way that I´m able to check if a newly inserted cuboid intersecs with one of the existing (collision detection).
To do this I decided to use hierarchical bounding-boxes.
So in sum I have a binary-tree-structure of bounding volumes.
Insertion is then done by determining recursively the distance to both children (=the distance between the two centers of two cuboids) and inserting in the path with the smallest distance.
Collision detection works similar, but we take all bounding volumes in a sub-path which are intersecting a given cuboid.
The tricky part is, how to balance this tree to get better performance if some cuboids are very close to each other and others are far away.
So far, I´ve found no way to use e.g. an AVL-tree because then I´d have to be able to compare two cuboids in some way that does not break the conditions on which collision detection depends.
P.S.: I know there are libraries to do this, but I want to understand the principles of collision detection e.g. in games in detail and therefore want to implement this by myself.
I´ve now tried it with space-partitioning instead of object-partitioning. That´s not exactly what I wanted to do but I found much more helpful information about it, e.g.: https://en.wikipedia.org/wiki/Kd-tree
With this information it should be possible to implement it.
Here is the problem: There is a map which size is anywhere from 200*200 pixels to 1000*1000 pixels. Each pixel is a third of an inch in length/width.
The map has walls coming from it (which can be of any size), and can be pre-processed by any means. However, when the problem starts, a robot (of pixel size 18*18) is placed in an unknown location, along with several obstacles and one goal, all of which are in an unknown location.
If the robot bumps into any wall/object/goal it immediately dies. As such it has a simple laser scanner that perfectly sees an 80*80 pixel square ahead of it,centered on the robot.
I have already solved the problem of localizing the robot and determining its position (within a small error) on the grid. I am having a bit of trouble with making a good algorithm for going across the entire map until the goal is found.
I have the idea of making the robot go to the lower right, and somehow sweeping left to right, avoiding obstacles, and walls, until the goal is found, however I am unsure of a good way to do that.
Are there any decent algorithms for such a thing, or are there better ones for what I am trying to do?
You are looking for Pathfinding Algorithms
Some suggestions include "Flood Fill" algorithm or "Dijkstra’s algorithm" very similar to Lee's algorithm (I might even argue they are the same), but it's just another term to search for
This is probably the most popular simple path finding algorithm: "A*search" (a star Search) this link also showcases a few other path finding algorithms. (Another helpful link).
The key with a*star search is that you must know where you are (localization) and where the goal is. Dijkstra type algorithms are able to find the goal without prior knowledge of its location.
The one and only algorithm that comes to my mind is a simple Lee algorithm. Here's a pretty decent tutorial about what it does and how it works.
Using this algorithm you should be able to find all the obstacles and eventually find the goal, also you will find the shortest path to the goal.
The only big difference is that you have to move an 80x80 object instead of a 1x1 object, but I will let you deal with the way you will implement this.
This is similar to the bin packing problem, but with some changes.
What I have is a timeseries of annotated data, and when I draw the chart, I want to place the annotations in the position that overall minimizes distance from the annotated point.
This chart, (gratuitously stolen) shows what I'd like to do:.
I know this is an optimization problem, but I have no idea where to begin. What I was doing first, was placing it at the corresponding x, and moving up/down y to find a location that was available and save the area that has been drawn. While that worked, it doesn't really make best use of available space, and I'm wondering if there's something better.
I'm wondering if there's any known algorithms out there that attack this or similar problems?
Added note: It doesn't need to be optimal, but it absolutely needs to be fast. This is done during rendering, so the UI is blocked while this executes.
There are a wide range of approaches to this difficult problem. I'd suggest starting at the Wikipedia article on automatic label placement. You might also get some ideas from the realm of force-based algorithms for graph drawing.
I would do it like this- use a simple "first fit" algorithm and start with an arbitrary order for placing the labels. Score the result with something like the sum of the distance squared from each annotated point. I would do the distance squared to avoid having all of them be really close except for one that is a long ways away.
If your first solution is lower than some threshold, use it and move on. If it isn't, take the worst fits (i.e. the labels that are the farthest from the annotated point) and move them up in the placement order and start over. Iterate this until you either get a solution that is good enough or you run out of time, in which case you take the best solution of the lot and go with it.
I'm asking this questions out of curiostity, since my quick and dirty implementation seems to be good enough. However I'm curious what a better implementation would be.
I have a graph of real world data. There are no duplicate X values and the X value increments at a consistant rate across the graph, but Y data is based off of real world output. I want to find the nearest point on the graph from an arbitrary given point P programmatically. I'm trying to find an efficient (ie fast) algorithm for doing this. I don't need the the exact closest point, I can settle for a point that is 'nearly' the closest point.
The obvious lazy solution is to increment through every single point in the graph, calculate the distance, and then find the minimum of the distance. This however could theoretically be slow for large graphs; too slow for what I want.
Since I only need an approximate closest point I imagine the ideal fastest equation would involve generating a best fit line and using that line to calculate where the point should be in real time; but that sounds like a potential mathematical headache I'm not about to take on.
My solution is a hack which works only because I assume my point P isn't arbitrary, namely I assume that P will usually be close to my graph line and when that happens I can cross out the distant X values from consideration. I calculating how close the point on the line that shares the X coordinate with P is and use the distance between that point and P to calculate the largest/smallest X value that could possible be closer points.
I can't help but feel there should be a faster algorithm then my solution (which is only useful because I assume 99% of the time my point P will be a point close to the line already). I tried googling for better algorithms but found so many algorithms that didn't quite fit that it was hard to find what I was looking for amongst all the clutter of inappropriate algorithms. So, does anyone here have a suggested algorithm that would be more efficient? Keep in mind I don't need a full algorithm since what I have works for my needs, I'm just curious what the proper solution would have been.
If you store the [x,y] points in a quadtree you'll be able to find the closest one quickly (something like O(log n)). I think that's the best you can do without making assumptions about where the point is going to be. Rather than repeat the algorithm here have a look at this link.
Your solution is pretty good, by examining how the points vary in y couldn't you calculate a bound for the number of points along the x axis you need to examine instead of using an arbitrary one.
Let's say your point P=(x,y) and your real-world data is a function y=f(x)
Step 1: Calculate r=|f(x)-y|.
Step 2: Find points in the interval I=(x-r,x+r)
Step 3: Find the closest point in I to P.
If you can use a data structure, some common data structures for spacial searching (including nearest neighbour) are...
quad-tree (and octree etc).
kd-tree
bsp tree (only practical for a static set of points).
r-tree
The r-tree comes in a number of variants. It's very closely related to the B+ tree, but with (depending on the variant) different orderings on the items (points) in the leaf nodes.
The Hilbert R tree uses a strict ordering of points based on the Hilbert curve. The Hilbert curve (or rather a generalization of it) is very good at ordering multi-dimensional data so that nearby points in space are usually nearby in the linear ordering.
In principle, the Hilbert ordering could be applied by sorting a simple array of points. The natural clustering in this would mean that a search would usually only need to search a few fairly-short spans in the array - with the complication being that you need to work out which spans they are.
I used to have a link for a good paper on doing the Hilbert curve ordering calculations, but I've lost it. An ordering based on Gray codes would be simpler, but not quite as efficient at clustering. In fact, there's a deep connection between Gray codes and Hilbert curves - that paper I've lost uses Gray code related functions quite a bit.
EDIT - I found that link - http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.133.7490