我正在尝试实现Bellman-Ford算法,以找到图中从源到目标的最短路径。以下是我正在使用的变量。
private int[] distances; //at position "n" the distance of node "n" to the source is kept
private int[] predecessors; //at position "n" the predecessor of node "n" on the path
to the source is kept
private int source; //source node
private int destination; //destination node
所以我知道如何实现该算法,并且正确填充了distances和predescessors数组。我的问题是,获得这些信息后,如何找到从源到目的地的最短路径?
我已经浏览了下面的链接,但是我不太明白。
此外,如果有帮助,我的输出之一是这样的:
distances = [0,10,12,18,15,13,29,19,30]
predecessors = [0,1,2,3,5,7]
source = 0;
destination = 8;
实际最短路径是
0-->2-->5-->7-->8
,长度为30