Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest path and sorting points in a 2-dimensional space

Let's say I have an object in a 2-dimensional space, and a set of points that I need that object to visit. Points may be added at any time, but not removed.

What I want is to be able to determine the next closest point to where my object is in O(lg(n)) time, then go to it, then determine the next closest, etc..

A simple priority queue doesn't work for this because the object is changing position, so the queue would need to be rearranged each time it moves. I was imagining sorting the points into a BST somehow, but I'm unsure of how to sort with respect to (x, y) or if it's even possible.

This feels like I could be trying to solve traveling salesman without realizing it, if so, I apologize haha.

like image 771
Ryan Haining Avatar asked Jun 27 '13 23:06

Ryan Haining


People also ask

Which algorithm might you use to find the shortest route between two destinations?

Dijkstra's algorithm is one of the classic shortest path search algorithms.

Which of the following algorithm are used to find the shortest path?

Dijkstra's Algorithm finds the shortest path between a given node (which is called the "source node") and all other nodes in a graph. This algorithm uses the weights of the edges to find the path that minimizes the total distance (weight) between the source node and all other nodes.

How do you find the shortest path on a grid?

Given an n x n binary matrix grid , return the length of the shortest clear path in the matrix. If there is no clear path, return -1 . A clear path in a binary matrix is a path from the top-left cell (i.e., (0, 0) ) to the bottom-right cell (i.e., (n - 1, n - 1) ) such that: All the visited cells of the path are 0 .


1 Answers

One option would be to use a space partitioning tree like a quadtree or k-d tree to store all of the points in space. These data structures efficiently (usually in sublinear time) support queries of the form "what is the closest points to point p?" You could then do the following:

  1. Build a space partitioning tree for the points in space.
  2. Use the tree to find the point p closest to your starting point.
  3. Repeat the following:
    1. Move to point p.
    2. Remove p from the tree.
    3. Set p to be the point closest to your current location.

Hope this helps!

like image 196
templatetypedef Avatar answered Oct 21 '22 17:10

templatetypedef