Java Dijkstra算法,尝试在最短路径中找到第一个节点

Java Dijkstra算法,尝试在最短路径中找到第一个节点,java,networking,routing,dijkstra,Java,Networking,Routing,Dijkstra,我一直在这个问题上纠缠不休,没有取得任何进展。我想找到并打印出成功的“最短路径”遍历(ala Dijkstra算法)中的第一个节点。与在指定路径中选择访问的第一个节点相同。我觉得我需要一个数据结构,也许是一个数组列表?我已经试过了,但我似乎无法得到一个旅行节点的简明列表 我的代码: public static ShortestPaths runDijkstra(Map<String,Node> graph, Node startNode) { //Queue for

我一直在这个问题上纠缠不休,没有取得任何进展。我想找到并打印出成功的“最短路径”遍历(ala Dijkstra算法)中的第一个节点。与在指定路径中选择访问的第一个节点相同。我觉得我需要一个数据结构,也许是一个数组列表?我已经试过了,但我似乎无法得到一个旅行节点的简明列表

我的代码:

    public static ShortestPaths runDijkstra(Map<String,Node> graph, Node startNode) {
    //Queue for visited nodes
    Queue<Node> unvisitedNodes = new LinkedList<>();

    //Distance of node from source
    Map<Node,Integer> distances = new HashMap<>();
    //Current node w/previous node in optimal path
    Map<Node,Node> previousNode = new HashMap<>();

    for(Node n : graph.values()) {
        //Fill collections
        distances.put(n, Integer.MAX_VALUE);
        previousNode.put(n, null);
        unvisitedNodes.add(n);
    }
    //Set source node distance to 0
    distances.put(startNode, 0);

    while(!unvisitedNodes.isEmpty()) {
        int lowestDistance = Integer.MAX_VALUE;
        Node current = null;
        for(Node n : unvisitedNodes) {
            if(distances.get(n) < lowestDistance) {
                lowestDistance = distances.get(n);
                current = n;
            }
        }

        unvisitedNodes.remove(current);

        for(Entry<Node,Integer> neighborEntry : current.adjacentNodes.entrySet()) {
            int distanceFromNeighborToSource = (distances.get(current) + neighborEntry.getValue());
            Node neighbor = neighborEntry.getKey();
            if(distanceFromNeighborToSource < distances.get(neighborEntry.getKey())) {
                distances.put(neighbor, distanceFromNeighborToSource);
                previousNode.put(neighbor, current);
            }
        }

    }

    previousNode.put(startNode, new Node("-"));

    return new ShortestPaths(distances, previousNode);

}
公共静态最短路径runDijkstra(映射图,节点startNode){
//访问节点的队列
Queue unvisitedNodes=新建LinkedList();
//节点与源的距离
映射距离=新HashMap();
//当前节点与最佳路径中的前一个节点
Map previousNode=新HashMap();
对于(节点n:graph.values()){
//填充集合
距离.put(n,整数.MAX_值);
previousNode.put(n,null);
未访问的节点。添加(n);
}
//将源节点距离设置为0
距离。放置(起始节点,0);
而(!unvisitedNodes.isEmpty()){
int lowestDistance=整数最大值;
节点电流=零;
用于(节点n:未访问的节点){
if(距离。获取(n)<最短距离){
最低距离=距离。获取(n);
电流=n;
}
}
未访问的节点。删除(当前);
对于(条目邻居:current.adjacentNodes.entrySet()){
int distance fromNeightorSource=(distance.get(当前)+neightrentry.getValue());
节点邻居=neightrentry.getKey();
if(distancefromNeightSource
您应该编辑您的问题并添加语言标签。请发布包含测试数据的内容