Java 生成最短路径

Java 生成最短路径,java,dijkstra,Java,Dijkstra,我对Dijkstra算法的实现有一个问题。 我创建了包含所有顶点(节点)和相对边(边)的图。 当我运行computePath(Node source)方法时,它会将遇到的第一个节点正确地添加到priorityQueue中,但不会添加以下节点。这样,在第二次迭代时,它不会找到插入节点的边 类节点如下所示: public class Node implements Comparable<Node>{ public final String id; public Vector <Ed

我对Dijkstra算法的实现有一个问题。 我创建了包含所有顶点(节点)和相对边(边)的图。 当我运行computePath(Node source)方法时,它会将遇到的第一个节点正确地添加到priorityQueue中,但不会添加以下节点。这样,在第二次迭代时,它不会找到插入节点的边

类节点如下所示:

public class Node implements Comparable<Node>{
public final String id;
public Vector <Edge> adjacencies;
public double minDistance = Double.POSITIVE_INFINITY;
public Node previous;
//here there are all the methods included compareTo(Node other)
指定所有方法边缘的函数如下所示:

Vector <Arch> totArchs = this.getArchs();
for(int i=0;i<this.getNodes().size();i++)
this.getNodes().get(i).setAdjacenies(this.trovaAdiacenze(this.getNodes().get(i),totArchs));
但是,在方法之外,它们有邻接关系

for(Nodo n:ist.getNodes())
System.out.println("Node: "+n.id+ " having "+n.adjacencies.size()+" adjacencies");
相对产量为

Node: 2 having 2 adjacencies
Node: 5 having 2 adjacencies
Node: 35 having 2 adjacencies
Node: 67 having 2 adjacencies

有人能帮我吗?

为了弄清楚这一点,源节点的边到2和5,节点2的边到35和67,并且从5开始的所有边都找不到?您是否100%确定
this.getNodes()
中的节点与
archs.get(i).getInitial/FinalNode()中的节点是相同的节点,即相同的对象实例
?我的意思是,你没有从拱门复制节点,是吗?源节点有到2和5的边,节点2有到35和67的边,节点5有到1和6的边,节点35有到2和11的边,节点67有到1和27的边,依此类推。该图非常大。使用此.getNodes()返回图中存在的所有节点。在我的程序中,我还需要记住连接节点的拱门:我用图中的节点创建了拱门。我猜拱门是给定的,你从拱门中的节点收集节点。现在,拱门中的节点可能是重复的,即,如果两个拱门都指向节点2,则这两个拱门中的节点2不是同一个对象。这可能会导致从拱门获取的某些边端点位于节点列表中,而其他端点不在节点列表中。请检查这一点,并考虑显示用于创建拱或从拱派生节点的代码。
public static void computePaths(Node source){
 source.minDistance = 0.;
 PriorityQueue<Node> vertexQueue = new PriorityQueue<Node>();
 vertexQueue.add(source);
 while (!vertexQueue.isEmpty()){
Node u = vertexQueue.poll();

for (Edge e : u.adjacencies){
    Node 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);
     }  
   }
    }
  }
Added nodes:2 having 2 adjacencies
Added nodes:5 having 0 adjacencies
Added nodes:35 having 0 adjacencies
Added nodes:67 having 0 adjacencies
for(Nodo n:ist.getNodes())
System.out.println("Node: "+n.id+ " having "+n.adjacencies.size()+" adjacencies");
Node: 2 having 2 adjacencies
Node: 5 having 2 adjacencies
Node: 35 having 2 adjacencies
Node: 67 having 2 adjacencies