我有bellman ford算法的这种实现:
public Map<Vertex, Vertex> bellmanFord2(Vertex s) {
Map<Vertex, Vertex> p = g.createVertexMap(null);
Map<Vertex, Double> d = g.createVertexMap(Double.POSITIVE_INFINITY);
d.put(s, 0d);
for (int i = 0; i < g.getVertices().size(); i++) {
for (Edge e : g.getEdges()) {
relax(e, d, p);
}
}
return p;
}
public void relax(Edge e, Map<Vertex, Double> d, Map<Vertex, Vertex> p) {
Vertex u = e.getSource();
Vertex v = e.getTarget();
if (d.get(u) + e.getWeight() < d.get(v)) {
d.put(v, d.get(u) + e.getWeight());
p.put(v, u);
}
}
public List<Vertex> pathReconstruction(Vertex start, Vertex finish) {
Map<Vertex, Vertex> p = bellmanFord2(start);
if (p.get(finish) == null) {
return null;
}
List<Vertex> path = new ArrayList<Vertex>();
Vertex current = finish;
while (current != start) {
path.add(current);
current = p.get(current);
}
path.add(start);
Collections.reverse(path);
return path;
}
,我正在尝试对其进行修改,以返回所有最短路径。我将如何实现这种行为?我已经有了使用BellmanFord实现的路径重建方法。
谢谢!