我目前正在使用Dijkstra算法来寻找最短路径。这个算法给了我最好的最短路径,但我希望有2个或更多路径。我怎样才能做到这一点?
算法如下:
public class Dijkstra
{
public static void computePaths(Vertex source)
{
source.minDistance = 0.;
PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
vertexQueue.add(source);
while (!vertexQueue.isEmpty()) {
Vertex u = vertexQueue.poll();
// Visit each edge exiting u
for (Edge e : u.adjacencies)
{
Vertex v = e.target;
double weight = e.weight;
double distanceThroughU = u.minDistance + weight;
if (distanceThroughU < v.minDistance) {
vertexQueue.remove(v);
v.minDistance = distanceThroughU ;
v.previous = u;
vertexQueue.add(v);
}
}
}
}
答案 0 :(得分:0)
有Yen top K最短路径算法,它使用Dijkstra计算最优路径,之后使用Dijkstra计算第二最佳路径等。
我在这里找到了一个Java实现:https://github.com/yan-qi/k-shortest-paths-java-version/tree/master/src/main/java/edu/asu/emit/algorithm/graph/shortestpaths
希望有所帮助