简体   繁体   English

从点列表中找到最短路径

[英]Finding shortest path from a list of points

If I have a list of points returned from a breadth-first-search through a type of 2D array maze in Java, how could I find the shortest path in that list of points? 如果我有一个广度优先搜索通过Java中的2D数组迷宫类型返回的点列表,我如何在该点列表中找到最短路径?

For example, if my potential target points are [2, 4] and [6, 0] and I have a list of points that goes to each target point, how can I find out which route is shortest? 例如,如果我的潜在目标点是[2,4]和[6,0],并且我有到达每个目标点的点列表,那么我如何找出最短的路线?

I'm thinking I might have to use Djikstra's Algorithm or something, but I'm unsure. 我在想可能必须使用Djikstra的算法或其他方法,但是我不确定。

Thanks very much 非常感谢

You can use Dijkstra's algorithm here. 您可以在此处使用Dijkstra的算法。 Given the array maze you mentioned, the first step would be to get all the edges in your maze between 2 adjacent nodes, and the edge costs. 给定您提到的阵列迷宫,第一步将是使迷宫中的所有边缘都位于2个相邻节点之间,并增加边缘成本。 For the nodes, you would need to know if each node has been visited, and the current cost of visiting that node. 对于节点,您需要知道是否已访问每个节点,以及访问该节点的当前成本。

Now, if you are not concerned with getting the shortest path, you might not need Dijkstra's. 现在,如果您不关心获得最短路径,则可能不需要Dijkstra。 Instead, you can use a DP/Recursion approach to get possible paths from a source coordinate to a target coordinate. 相反,您可以使用DP /递归方法来获取从源坐标到目标坐标的可能路径。 If you want to see a Dijkstra's implementation, this is something I had written. 如果要查看Dijkstra的实现, 是我写的东西。 To get the shortest path, you would obviously need the distance between nodes. 为了获得最短路径,您显然需要节点之间的距离。

To just find a path between 2 points, I would suggest something like this implementation. 为了找到两点之间的路径,我建议采用这种实现方式。 It includes both DP and recursion implementations and compares the running times for both (recursion can take very long to run for large datasets). 它包括DP和递归实现,并比较两者的运行时间(对于大型数据集,递归可能需要很长时间才能运行)。

I feel this much should be enough to get you started. 我觉得这应该足以让您入门。 Let me know if you need some other information. 让我知道您是否需要其他信息。

I didn't end up using Dijkstra's, but instead changed my breadth-first-search to add a "level" value or "distance" value from the origin point that would count upward with each visited node. 我并没有最终使用Dijkstra的方法,而是更改了广度优先搜索的范围,以添加从起点的“级别”值或“距离”值,该值将随每个访问的节点向上计数。 The counts would stay consistent if the path ever branched, and since I already knew the end point, all it would take is to check out what the "count" was in my end points and compare. 如果路径分支了,计数将保持一致,并且由于我已经知道终点,因此只需检查一下终点中的“计数”并进行比较即可。

Thank you for your help though! 谢谢您的帮助! You'll get an upvote if the site lets me. 如果该网站允许我,您将获得支持。

For those facing a similar problem: I made a simple class called PointC which inherits from Point, but with an added "count" value. 对于那些面临类似问题的人:我做了一个名为PointC的简单类,该类继承自Point,但是增加了“ count”值。 The count was updated appropriately with each step of the breadth first search, then the end points were compared at the end to obtain the most optimal path. 在广度优先搜索的每个步骤中都会适当地更新计数,然后在末尾比较端点以获得最佳路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM