Java 打印前人的Dijkstra算法
我正在尝试打印Dijkstra的算法,但目前只有目标ID正在从我的getPath()方法打印。我的想法是从目标顶点向后工作并打印每个前置顶点,直到打印开始顶点。如果有另一种方法来存储/打印路径,我将非常乐意尝试另一种方法,我非常感谢任何想法Java 打印前人的Dijkstra算法,java,dijkstra,Java,Dijkstra,我正在尝试打印Dijkstra的算法,但目前只有目标ID正在从我的getPath()方法打印。我的想法是从目标顶点向后工作并打印每个前置顶点,直到打印开始顶点。如果有另一种方法来存储/打印路径,我将非常乐意尝试另一种方法,我非常感谢任何想法 class ShortestPathFinder { private Graph graph = new Graph(); private Vertex source = new Vertex(0, null);
class ShortestPathFinder {
private Graph graph = new Graph();
private Vertex source = new Vertex(0, null);
private Vertex destination = new Vertex(0,null);
private Map<Vertex,Vertex> previousVertex = new HashMap();
private Set<Vertex> visited = new HashSet();
public Optional<Path> findShortestPath(Graph graph, Vertex source, Vertex destination, ReadInput graphReader) {
this.destination = destination;
this.source = source;
Optional<Path> pathFound = Optional.empty();
source.setDistanceFromSource(0);
PriorityQueue<Vertex> priorityQueue = new PriorityQueue<>();
priorityQueue.add(source);
source.setVisited(true);
boolean destinationFound = false;
while( !priorityQueue.isEmpty() && destinationFound == false){
// Getting the minimum distance vertex from priority queue
Vertex actualVertex = priorityQueue.poll();
System.out.println("working on: " + actualVertex.getID());
actualVertex = graphReader.SetAdjList(actualVertex);
for(Edge edge : actualVertex.getAdjacenciesList()){
Vertex v = edge.getTargetVertex();
System.out.println("a Neighbor is: " + v.getID());
if(!v.isVisited()) {
if(v.getID() == destination.getID()) {
System.out.println("Destination found");
Path path = new Path(previousVertex);
pathFound = Optional.of(path);
destinationFound = true;
break;
}
double newDistance = actualVertex.getDistanceFromSource() + edge.getWeight();
if( newDistance < v.getDistanceFromSource() ){
priorityQueue.remove(v);
v.setDistanceFromSource(newDistance);
v.setPredecessor(actualVertex);
priorityQueue.add(v);
System.out.println("Added: " + v.getID());
}
}
}
actualVertex.setVisited(true);
}
return pathFound;
}
public List<Vertex> getPath() {
List<Vertex> path = new ArrayList<>();
for(Vertex vertex=destination;vertex!=null;vertex=vertex.getPredecessor()){
path.add(vertex);
}
Collections.reverse(path);
return path;
}
}
class ShortestPathFinder{
私有图=新图();
私有顶点源=新顶点(0,空);
私有顶点目标=新顶点(0,空);
private Map previousVertex=新HashMap();
访问的私有集=新HashSet();
公共可选FindShortPath(图形、顶点源、顶点目标、ReadInput graphReader){
this.destination=目的地;
this.source=源;
Optional pathFound=Optional.empty();
source.setDistanceFromSource(0);
PriorityQueue PriorityQueue=新的PriorityQueue();
priorityQueue.add(源);
source.setvisted(true);
布尔destinationFound=false;
而(!priorityQueue.isEmpty()&&destinationFound==false){
//从优先级队列获取最小距离顶点
顶点actualVertex=priorityQueue.poll();
System.out.println(“处理:+actualVertex.getID());
actualVertex=graphhreader.SetAdjList(actualVertex);
对于(边缘:actualVertex.GetAdjanceList()){
顶点v=边。getTargetVertex();
println(“邻居是:+v.getID());
如果(!v.isvisted()){
if(v.getID()==destination.getID()){
System.out.println(“找到目的地”);
路径路径=新路径(上一个顶点);
pathFound=可选的.of(路径);
destinationFound=true;
打破
}
double newDistance=actualVertex.getDistanceFromSource()+edge.getWeight();
if(newDistance
到达目的地时,将调用代码中的以下部分:
if(v.getID() == destination.getID()) {
System.out.println("Destination found");
Path path = new Path(previousVertex);
pathFound = Optional.of(path);
destinationFound = true;
break;
}
相应的v从未设置过它的前导项,这是getPath()
方法所依赖的。考虑将它设置在if语句中,例如:
if(v.getID() == destination.getID()) {
System.out.println("Destination found");
destination.setPredecessor(actualVertex); // sets the predecessor for backwards traversal
Path path = new Path(previousVertex);
pathFound = Optional.of(path);
destinationFound = true;
break;
}
到达目的地时,将调用代码中的以下部分:
if(v.getID() == destination.getID()) {
System.out.println("Destination found");
Path path = new Path(previousVertex);
pathFound = Optional.of(path);
destinationFound = true;
break;
}
相应的v从未设置过它的前导项,这是getPath()
方法所依赖的。考虑将它设置在if语句中,例如:
if(v.getID() == destination.getID()) {
System.out.println("Destination found");
destination.setPredecessor(actualVertex); // sets the predecessor for backwards traversal
Path path = new Path(previousVertex);
pathFound = Optional.of(path);
destinationFound = true;
break;
}
谢谢,这是我的疏忽,但不幸的是,我得到了相同的输出。您得到了相同的输出,因为您在
getPath()
中使用了destination
变量。Dodestination.setpreceptor(actualVertex)
设置该特定顶点。我已经编辑了我的答案以适应这一点。谢谢,这是我的疏忽,但不幸的是,我得到了相同的输出。您得到了相同的输出,因为您在getPath()
中使用了destination
变量。Dodestination.setpreceptor(actualVertex)
设置该特定顶点。我已经编辑了我的答案以适应这一点。