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.
Dijkstra's algorithm is one of the classic shortest path search algorithms.
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.
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 .
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:
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With