简体   繁体   English

最短路径算法:从一个点到相邻点的均匀距离

[英]Shortest Path Algorithm : Uniform distances from one point to adjacent points

I am trying to develop an algorithm wherein I have a Location Class. 我正在尝试开发一种算法,其中我有一个位置类。 In each class, I create a list of its adjacent Locations. 在每个类中,我创建一个相邻位置的列表。 I want to know, how can I get the shortest path from one Location to another. 我想知道,我怎样才能获得从一个位置到另一个位置的最短路径。 I am trying to look for different algorithms but it seems that they doesn't answer my problem. 我试图寻找不同的算法,但似乎他们没有回答我的问题。

For example, I have a point A and I want to go to point B, 例如,我有一个A点,我想去B点,

A - - C - - H - - J
      |
      F- - K- -B 

My idea for this is if B is in the List of adjacent locations of A, then that is the shortest path. 我的想法是,如果B位于A的相邻位置列表中,那么这是最短路径。 If not, it should search the adjacent locations of the adjacent locations of A. But I do not know how to implement this in code or if it is a good algorithm. 如果没有,它应该搜索A的相邻位置的相邻位置。但是我不知道如何在代码中实现它或者它是一个好的算法。 I also want to display A - C - F - K - B as the route for the shortest path. 我还想显示A - C - F - K - B作为最短路径的路线。 I am also developing this one on j2me so I am a bit limited on the java features that I can use. 我也在j2me上开发这个,所以我对我可以使用的java功能有点限制。 If anyone can help me, it will be much appreciated. 如果有人可以帮助我,我将不胜感激。 Thanks 谢谢

You are on the right track. 你走在正确的轨道上。 What you describe is the start of BFS . 你所描述的是BFS的开始。 BFS is a shortest-path algorithm that is both optimal [finds the shortest path] and complete [always finds a path if one exist] for unweighted graph - so it is probably the right choice. BFS是一种最短路径算法,既可以是最优路径[找到最短路径]又可以完成 [总是找到一条路径,如果有的话]用于未加权图形 - 因此它可能是正确的选择。

BFS works on graphs. BFS适用于图表。 In here your graph is G = (V,E) such that V = {all locations} [the nodes/vertices/locations] and E = {(u,v),(v,u) | u and v are neighbors} 在这里你的图是G = (V,E) ,使得V = {all locations} [节点/顶点/位置]和E = {(u,v),(v,u) | u and v are neighbors} E = {(u,v),(v,u) | u and v are neighbors} [the edges/links/neighbors] E = {(u,v),(v,u) | u and v are neighbors} [edge / links / neighbors]

The idea of BFS is simpilar to what you are suggesting: first check if the starting node is also the target. BFS的想法与您的建议完全相同:首先检查起始节点是否也是目标。 Then check if one of the neighbors of the starting node is the target, then search for their neighbors.... 然后检查起始节点的一个邻居是否是目标,然后搜索他们的邻居....

Regarding getting the actual path from BFS: have a look at this post . 关于从BFS 获取实际路径 :看看这篇文章
The idea is to maintain a map - for each node [location] - the map will indicate how did you get there? 我们的想法是维护一张地图 - 对于每个节点[位置] - 地图将指示你是如何到达那里的? which node discovered it? 哪个节点发现了它? After the BFS finished - follow the map from target to source, and you get the actual path [reversed of course]. BFS完成后 - 按照从目标到源的地图,然后获得实际路径[逆转当然]。 The link provided gives more details about this idea. 提供的链接提供了有关此想法的更多详细信息

Your problem is known in the computing world as a graph search problem, looking for the shortest path between two nodes. 您的问题在计算世界中被称为图搜索问题,寻找两个节点之间的最短路径。 Graphs here are not the x and y axis graphs from math, but Nodes (or Locations in your example) connected by edges. 此处的图形不是数学中的x和y轴图形,而是通过边连接的节点(或示例中的Locations )。

Dijkstra's algorithm is the most commonly used to find the shortest path between two nodes, and for your use case it is simplified slightly because the edges in your scenario all have a weight (or cost) of one. Dijkstra算法最常用于查找两个节点之间的最短路径,并且对于您的用例,它略有简化,因为场景中的边缘都具有一个权重(或成本)。 An implementation of this is available in JGraphT , though I'm not sure how easy that is to include in a J2ME environment. JGraphT中提供了这方面的实现,但我不确定在J2ME环境中包含它有多容易。

看看A*算法Dijkstra的寻路算法。

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

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