views:

216

answers:

5

I've got a collection of 10000 - 100000 spheres, and I need to find the ones farthest apart.

One simple way to do this is to simply compare all the spheres to each other and store the biggest distance, but this feels like a real resource hog of an algorithm.

The Spheres are stored in the following way:

Sphere (float x, float y, float z, float radius);

The method Sphere::distanceTo(Sphere &s) returns the distance between the two center points of the spheres.

Example:

Sphere *spheres;
float biggestDistance;

for (int i = 0; i < nOfSpheres; i++) {
    for (int j = 0; j < nOfSpheres; j++) {
        if (spheres[i].distanceTo(spheres[j]) > biggestDistance) {
            biggestDistance = spheres[i].distanceTo(spheres[j]) > biggestDistance;
        }
    }
}

What I'm looking for is an algorithm that somehow loops through all the possible combinations in a smarter way, if there is any.

The project is written in C++ (which it has to be), so any solutions that only work in languages other than C/C++ are of less interest.

+3  A: 

Could you perhaps store these spheres in a BSP Tree? If that's acceptable, then you could start by looking for nodes of the tree containing spheres which are furthest apart. Then you can continue down the tree until you get to individual spheres.

Michael Mior
I've thought about a tree structure too, but I haven't looked into the BSP Tree yet, thanks for your reply. =)
illuzive
+1  A: 

Your problem looks like something that could be solved using graphs. Since the distance from Sphere A to Sphere B is the same as the distance from Sphere B to Sphere A, you can minimize the number of comparisons you have to make.

I think what you're looking at here is known as an Adjacency List. You can either build one up, and then traverse that to find the longest distance.

Another approach you can use will still give you an O(n^2) but you can minimize the number of comparisons you have to make. You can store the result of your calculation into a hash table where the key is the name of the edge (so AB would hold the length from A to B). Before you perform your distance calculation, check to see if AB or BA exists in the hash table.

EDIT

Using the adjacency-list method (which is basically a Breadth-First Search) you get O(b^d) or worst-case O(|E| + |V|) complexity.

Vivin Paliath
-1: This is not really more efficient that brute force.
Moron
Really, I had no idea that a BFS was such an inefficient algorithm. My eyes have been opened.
Vivin Paliath
Building the adjacency list takes as much time as the brute force solution.
IVlad
Thanks for actually explaining what I did wrong, Vlad :)
Vivin Paliath
+1  A: 

Paul got my brain thinking and you can optimize a bit by changing

for (int j=0; j < nOfSpheres; j++) 

to

for (int j=i+1;  j < nOfSpheres; j++) 

You don't need to compare sphere A to B AND B to A. This will make the search O(n log n).

--- Addition -------

Another thing that makes this calculation expensive is the DistanceTo calulations.

distance = sqrt((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2)

That's alot of math. You can trim that down by checking to see if

((x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2 > maxdist^2

Removes the sqrt until the end.

Craig
No, this is still O(n^2), but it cuts the number of checks in half (was about to post this too).
Justin Peel
Bah, you are correct. Still, helps. ;)
Craig
Yeah, I know. sqrt is expensive.
illuzive
I wondered if taking principal components helps if you have coordinates in some space of dim N>1.
Paul
You shouldn't need to check against maxdist^2 since the squared distance will already be what's stored in maxdist.
Michael Mior
+1  A: 

I would sell some of the spheres and buy a networked gps device for the remainder.

Paul
But what color are the spheres?
Blue! No, yel-... AAAHHHHHHHH
Vivin Paliath
+19  A: 
John Feminella
+1: I have deleted my post after seeing this one as this one is a superset and better answer.
Moron
I would have been more descriptive about the steps, but this is homework. However, CGAL provides source code, so looking at that is a pretty good start to understanding what's happening here.
John Feminella
After some brief Googling, I thought I would add this link as well: http://www.personal.kent.edu/~rmuhamma/Compgeometry/MyCG/ConvexHull/quickHull.htm
Michael Mior