【问题标题】:Printing Dijkstra Algorithm from predecessors从前辈打印 Dijkstra 算法
【发布时间】:2020-06-13 14:48:50
【问题描述】:

我正在尝试打印 Dijkstra 的算法,但目前只有目标 ID 正在从我的 getPath() 方法打印。我的想法是从目标顶点向后工作并打印每个前驱,直到打印开始顶点。如果有另一种存储/打印路径的方法,我会更愿意尝试不同的方式,我很感激任何想法!

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;

    }       
}

【问题讨论】:

    标签: java dijkstra


    【解决方案1】:

    到达目的地时调用代码中的以下部分:

                        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 变量,因此您将获得相同的输出。执行destination.setPredecessor(actualVertex); 设置特定的顶点。我已经编辑了我的答案以适应这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2020-05-20
    • 1970-01-01
    • 2021-03-13
    • 2011-06-27
    相关资源
    最近更新 更多