Dijkstra算法中的无限循环?

3
我正在尝试使用Dijkstra算法在图中查找两个交叉点(顶点)之间的最短路径。不幸的是,在while循环中我遇到了无限循环,而且我真的无法弄清楚原因。
NodeDist是一个哈希映射表,用于存储交叉点与双精度浮点数之间的距离,这个距离由图中“街道”(边缘)的长度决定。Previous是一个哈希映射表,用于记录交叉点到交叉点的关系,即我们现在所看的交叉点之前看到的交叉点。
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;

    }
}

1
你在哪个循环里?你尝试过用小图进行调试吗? - Nikolay Kuznetsov
街道是双向的吗?a->b在列表中,b->a也在列表中吗?看起来好像你可以得到两个,每次迭代只会添加另一个返回列表?--更新不是这种情况,因为你有a <(不是<=)。 - John Gardner
这个图是无向的...那么这意味着我应该使用<=吗? - user202925
1
我认为 double addedDist = nodeDist.get(start)+dist;double addedDist = nodeDist.get(start)+dist; 存在问题。它真的应该是 start 吗?我认为应该是 head,对吗? - irrelephant
哦!是的,将它变成“头”就可以消除无限循环错误。感谢您的帮助! - user202925
1个回答

2

所以,这最终解决了评论中的问题:

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

应该是

double addedDist = nodeDist.get(head)+dist;

两次都需要。

新增的距离应该来自于当前顶点,而不是起始顶点(其距离为0)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接