Hi All,
I'm devising a program to find the nearest neighbor for each point
in a given set of size N. A common solution of this problem is to
compare the distances between a given point and all the other points
pair by pair. Then, for a single point, there are N-1 comparisons. So,
the total number of comparisons should be (N-1)xN.
To speed up the program, is it better to use a sorting algorithm
instead of pairwise comparisons?
I mean to store the distances of a given point in a temporary array,
sort it with an appropriate sorting algorithm and then choose the
first element as the shortest distance.
Thanks!
Regards,
Nathan
|
|
0
|
|
|
|
Reply
|
nathanwh71 (2)
|
11/5/2007 2:11:14 AM |
|
On Nov 4, 9:11 pm, nathanw...@gmail.com wrote:
> I'm devising a program to find the nearest neighbor for each point
> in a given set of size N. A common solution of this problem is to
> compare the distances between a given point and all the other points
> pair by pair. Then, for a single point, there are N-1 comparisons. So,
> the total number of comparisons should be (N-1)xN.
>
> To speed up the program, is it better to use a sorting algorithm
> instead of pairwise comparisons?
> I mean to store the distances of a given point in a temporary array,
> sort it with an appropriate sorting algorithm and then choose the
> first element as the shortest distance.
Sorting is a terrible idea. You'd be doing either n sorts of O(n log
n) time or one sort of O(n^2) size data, which again is O(n^2 log n)
--- slower than the simple each city against all others and wasteful
of memory, too. It's the same reason you don't sort a list of
integers just to find their min!
A common approach to solving this problem efficiently is to build a kd-
tree, which takes average case O(n log n) time. You can also look up
all the nearest neigbors in O(n log n) time. This is much better than
your proposals.
There are more complex variations on the kd-tree idea that guarentee
O(n log n) to build and O(log n) to look up rather than provide it as
an average case. Try comp.graphics.algorithms .
Still, unless n is certain to be truly huge, you ought to give the
brute force computation a try. If it's fast enough and n will never
increase, you're done.
|
|
0
|
|
|
|
Reply
|
Gene
|
11/5/2007 2:47:40 AM
|
|
nathanwh71@gmail.com schrieb:
> Hi All,
>
> I'm devising a program to find the nearest neighbor for each point
> in a given set of size N. A common solution of this problem is to
> compare the distances between a given point and all the other points
> pair by pair. Then, for a single point, there are N-1 comparisons. So,
> the total number of comparisons should be (N-1)xN.
Are you in 2D? Then use the Delaunay triangulation. Look for steve
fortunes code or qhull
Christian
|
|
0
|
|
|
|
Reply
|
Christian
|
11/5/2007 10:11:50 AM
|
|
|
2 Replies
258 Views
(page loaded in 0.104 seconds)
Similiar Articles: Surprising pop_head performance compared to push_heap - comp.lang ...Take a look at your favorite book on algorithms and you will notice that the ... If there was, you could do comparison-based sorting in O(n). It is possible that your data ... Laptop specs for optimal matlab efficiency? - comp.soft-sys.matlab ...Specifically the question of "Quadcore vs. Dualcore ... some loops may have their code run in parallel, sort ... would not help (at least not without a change of algorithms ... JDBC compare the content of two tables - comp.lang.java.help ...But I still need to implement various comparison functions ... Enform query - Count, sort with partial string - comp ... looking for simple DF (AOA) algorithm - comp.dsp JDBC ... Allocatable versus automatic arrays - comp.lang.fortranIn discussion of the radix sort, the algorithm naturally seems to have O(N) time. ... That sounds like a comparison of different hardware - not different sizes of ... BPSK,QPSK,16-QAM constellation points - comp.dspFor fair comparison of modulations, average transmit power ... re transmitting these constellations on some sort of ... >> >>-- >>Eric Jacobsen >>Minister of Algorithms >>Abineau ... improve strlen - comp.lang.asm.x86... solution (maybe mmx ist4uction) to improve the algorithm ... and computed when value is first time queried, sort ... That's why it is pointless to do performance comparison ... Non Intel & AMD Arch - comp.lang.asm.x86A Big Endian machine loads a word and direct comparison ... can handle better than Little: > > Comparing/Sorting ... convert these blocks to ascii using a fast algorithm. Windowed sinc - comp.dspEric Jacobsen Minister of Algorithms Abineau ... > > As previously mentioned, this sort of thing is ... It would be interesting to see the comparison of accuracy vs ... Magnitude of a 3d vector - comp.dsp... snip] > > > Instead of combining y and z, if we sort ... Two Matrices with Row Vectors - How to calculate pairwise ... ... Discrete Fourier Simulink Block (what algorithm is used ... Fastest way to draw lots of triangles -- redux (FYI) - comp ...Folks, After our flurry of Display List/VBO comparisons ... isn't real-time, folks, it can take minutes for algorithms ... between the two VBOs in a "double buffered" sort of ... language agnostic - sorting algorithm where pairwise-comparison ...Most sort algorithms rely on a pairwise-comparison the determines whether A < B, A = B or A > B. I'm looking for algorithms (and for bonus points, code in Python ... Sorting algorithm - Wikipedia, the free encyclopediaComparison-based sorting algorithms, which evaluate the elements of the list via an abstract ... Bitonic sorter; Batcher odd–even mergesort; Pairwise sorting network 7/29/2012 4:00:29 AM
|