2

Given a set of vertices with 3D spatial coordinates of size N and a maximum connection distance d, is there an efficient algorithm to find all the undirected edges connecting the vertices with distance less than d; loops are not considered. A naive approach would simply loop on all possible pairs, requiring N(N-1)/2 distance calculations. Is there an existing algorithm for finding all possible edges with scaling complexity less than O(N^2)?

8
  • Are you trying to create a planar graph? Commented Oct 19, 2021 at 23:40
  • This question has nothing to do with graphs. It is about 3D Euclidean distances. Commented Oct 20, 2021 at 6:50
  • This reminds me of 3D collision detection. If so start with bounding boxes. Commented Oct 20, 2021 at 11:00
  • @GuyCoder good point, this is the same problem as collision detection. Recursive boxing methods like quadtree can do this in N Log(N). Commented Oct 20, 2021 at 13:07
  • 1
    @mTesseracted Since you already have an upvoted answer to the question as written, if you have a related question with extra requirements please ask it separately. Commented Oct 20, 2021 at 15:00

1 Answer 1

4

Given a set of vertices with 3D spatial coordinates of size N and a maximum connection distance d, is there an efficient algorithm to find all the undirected edges connecting the vertices with distance less than d

Yes. Insert the vertex locations into a octree, then for each vertex search for vertices closer than d.

For the equivalent problem in 2D you can use a quadtree.

You can find C++ quadtree code at https://github.com/JamesBremner/quadtree

Example Usage:

        // construct vector of random points
        std::vector<cPoint> vp = random(count);

        // construct quadtree of points
        cCell quadtree(cPoint(0, 0), 100);
        for (auto &p : vp)
            quadtree.insert(p);

        // quadtree search
        // returns vector of all points within 2 by 2 box around point 10,10
        auto fp = quadtree.find(cCell(cPoint(10, 10), 2));

Note that if the exact Euclidean distance is important, then post-processing is required to remove any points in the red regions.

enter image description here

For more details, check out the German tv mini-series 'Billion Dollar Code' available on Netflix.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you know of any collision detection algorithms that have O(N) average case performance where the average case is each object only collides with a finite number of other objects?
@mTesseracted If those are the constraints of your problem then you should have included them in the question. Please don't wait until someone has done the work to answer the question as you wrote it and then change what you're asking for.
Since the OP asks for a 3D solution, an octree is more appropriate.
@LiorKogan Good point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.