Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Dijkstra算法中的无限循环?_Java_Infinite Loop_Dijkstra - Fatal编程技术网

Java Dijkstra算法中的无限循环?

Java Dijkstra算法中的无限循环?,java,infinite-loop,dijkstra,Java,Infinite Loop,Dijkstra,我正在尝试实现Dijkstra算法来寻找图中两个交点(顶点)之间的最短路径。不幸的是,我在while循环中得到了一个无限循环,我真的不知道为什么 NodeList是交叉点和双精度点之间的哈希映射,用于查找图形中节点之间的距离。距离由图形中“街道”(边)的长度确定。Previous是一个hashmap,它跟踪交叉点到交叉点,即在我们现在查看的交叉点之前查看的交叉点 public List<IntersectionI> dijkstraPath(IntersectionI start,

我正在尝试实现Dijkstra算法来寻找图中两个交点(顶点)之间的最短路径。不幸的是,我在while循环中得到了一个无限循环,我真的不知道为什么

NodeList是交叉点和双精度点之间的哈希映射,用于查找图形中节点之间的距离。距离由图形中“街道”(边)的长度确定。Previous是一个hashmap,它跟踪交叉点到交叉点,即在我们现在查看的交叉点之前查看的交叉点

public List<IntersectionI> dijkstraPath(IntersectionI start, IntersectionI end){
    ArrayList<IntersectionI> path = new ArrayList<IntersectionI>();
    Iterator<IntersectionI> it = graph.myGraph.keySet().iterator();
    //Initializing all unvisited node distances as infinity.
    while (it.hasNext()){
        IntersectionI next = it.next();
        nodeDist.put(next, INFINITY);
    }
    //Remove the start node, put in 0 distance. 
    nodeDist.remove(start);
    nodeDist.put(start, (double) 0);
    queue.add(start);
    //computes paths
    while (!queue.isEmpty()){
        IntersectionI head = queue.poll();
        if (nodeDist.get(head) == INFINITY)
            break;
        visited.put(head, true);
        List<StreetI> str = head.getStreetList();
        for (StreetI e : str){
            Point pt1 = e.getFirstPoint();
            Point pt2 = e.getSecondPoint();
            IntersectionI p1 = graph.pointGraph.get(pt1);
            IntersectionI p2 = graph.pointGraph.get(pt2);
            if (head.getLocation().equals(p1)){
                double dist = e.getDistance();
                double addedDist = nodeDist.get(start)+dist;
                double p2Dist = nodeDist.get(p2);
                if (addedDist < p2Dist){
                    previous.put(p2, head);
                    Point p22 = p2.getLocation();
                    p22.setCost(addedDist);
                    nodeDist.put(p2, addedDist);
                    queue.add(p2);
                }

            }
            else {
                double dist = e.getDistance();
                double addedDist = nodeDist.get(start)+dist;
                if (addedDist < nodeDist.get(p1)){
                    previous.put(p1, head);
                    Point p11 = p1.getLocation();
                    p11.setCost(addedDist);
                    nodeDist.put(p1, addedDist);
                    queue.add(p1);
                }
            }
        }
    }
    //gets shortest path
    for (IntersectionI vertex = end; vertex != null; vertex = previous.get(vertex))
        path.add(vertex);
    System.out.println("ya");
    Collections.reverse(path);
    return path;
}

//The comparator that sorts by intersection distance.
public class distCompare implements Comparator<IntersectionI> {
    @Override
    public int compare(IntersectionI x, IntersectionI y) {
        Point xPo = x.getLocation();
        Point yPo = y.getLocation();
        if (xPo.getCost() < yPo.getCost())
            return 1;
        else if (yPo.getCost() < xPo.getCost())
            return -1;
        else return 0;

    }
}
公共列表dijkstraPath(IntersectionI开始,IntersectionI结束){
ArrayList路径=新建ArrayList();
迭代器it=graph.myGraph.keySet().Iterator();
//将所有未访问的节点距离初始化为无穷大。
while(it.hasNext()){
IntersectionI next=it.next();
nodedit.put(下一个,无穷大);
}
//移除起始节点,将其放置在0距离内。
nodeList.remove(启动);
nodeDist.put(开始,(双)0);
queue.add(开始);
//计算路径
而(!queue.isEmpty()){
IntersectionI head=queue.poll();
if(nodeList.get(head)=无穷大)
打破
访问。放置(头部,真实);
List str=head.getStreetList();
对于(StreetI e:str){
点pt1=e.getFirstPoint();
点pt2=e.getSecondPoint();
IntersectionI p1=graph.pointGraph.get(pt1);
intersectionip2=graph.pointGraph.get(pt2);
if(head.getLocation()等于(p1)){
双距离=e.getDistance();
double addedist=nodeDist.get(start)+dist;
double p2Dist=nodeDist.get(p2);
if(addedist
因此,这最终解决了评论中的问题:

double addedDist = nodeDist.get(start)+dist;
应该是

double addedDist = nodeDist.get(head)+dist;
两次都是


添加的距离应来自当前顶点,而不是起始顶点(距离为0)。

在哪个循环中?你试过用一个小图形调试吗?街道是双向的吗?a->b在列表中,b->a在列表中吗?在我看来,这两种方法都可以得到,而每次迭代都会将另一种方法加回去?--更新看起来不是这样的,因为你有一个double addedist=nodeDist.get(start)+dist;和
double addedist=nodeDist.get(start)有问题吗+dist;
。它真的应该是
start
?我想应该是
head
,对吗?哦!是的,使它成为“head”可以消除无限循环错误。谢谢你的帮助!