简体   繁体   English

java Dijkstra最短的路径如何找出最终目的地?

[英]java Dijkstra’s shortest path how to figure out final destination?

I am trying to learn graphs as well using Dijkstra's path and am using this geeks for geeks tutorial . 我也在尝试使用Dijkstra的路径来学习图形,并且正在将这个极客用于极客教程 I think i understand how it works using weight to find the shortest path. 我想我了解使用重量找到最短路径的方式。 However this may be dumb but i dont understand how to find the destination point by looking at the code. 但是,这可能是愚蠢的,但我不明白如何通过查看代码来找到目的地。 or how the 9 inputs are 或9个输入是如何

Why are there 9 inputs cant it work with only three? 为什么有9个输入只能使用3个输入? And how does the program know where to end? 程序如何知道在哪里结束?

The program in that example ends when all nodes have been visited, it does not choose a specific destination. 该示例中的程序在访问完所有节点后结束,它没有选择特定的目的地。 It calculates the minimum distance (and path) from node 0 to every node in the graph. 它计算从节点0到图中每个节点的最小距离(和路径)。

There are 9 nodes in the example, but ofcourse you can also use 3 nodes, or 1000 nodes.. 该示例中有9个节点,但是您当然也可以使用3个节点或1000个节点。

So, to address the first half of your question, there isn't a predetermined "destination node" with Dijsktra's algorithm, moreso just a final result. 因此,为了解决您的问题的前半部分,没有使用Dijsktra算法的预定“目标节点”,而且还只是最终结果。

In this example from the GeeksforGeeks code, 在此示例中,来自GeeksforGeeks代码,

void dijkstra(int graph[V][V], int src) 

We can see that the algorithm wants an array of all of the nodes, and which node you'll be starting from. 我们可以看到算法需要所有节点的数组,以及要从哪个节点开始。 Each node in this array has 9 inputs which correspond to that node's given distance from any other node, where a value of 0 represents no connection. 该数组中的每个节点都有9个输入,这些输入对应于该节点到任何其他节点的给定距离,其中0表示没有连接。 For example, the "0" node has values: 例如,“ 0”节点具有以下值:

{0, 4, 0, 0, 0, 0, 0, 8, 0} {0,4,0,0,0,0,0,8,0}

Which mean that node 0 is 4 units away from node 1, 8 units away from node 7, and either has no connection to, or IS one of the other 7 nodes. 这意味着节点0距离节点1 4个单元,距离节点7 8个单元,或者没有连接到其他7个节点,或者是其他7个节点之一。 If you only have 3 nodes, then you'd use 3 inputs to represent the possible distances between all 3. 如果只有3个节点,则将使用3个输入来表示所有3个节点之间的可能距离。

When the program has exhausted all possible nodes and paths, the algorithm will stop. 当程序用尽所有可能的节点和路径时,算法将停止。 This algorithm looks for a minimum spanning tree, not a path from point A to point B. 该算法寻找最小的生成树,而不是从A点到B点的路径。

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

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