java - 使用斐波那契堆改进 Dijkstra?

标签 java algorithm path-finding

我找到了 this implementation (相关部分如下)Dijkstra 使用优先级队列。是否可以通过实现 Fibonacci 堆或什至转向迭代深化 A* 来进一步加快速度?

 47     public static void computePaths(Vertex source)
 48     {
 49         source.minDistance = 0.;
 50         PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();
 51     vertexQueue.add(source);
 52 
 53     while (!vertexQueue.isEmpty()) {
 54         Vertex u = vertexQueue.poll();
 55 
 56             // Visit each edge exiting u
 57             for (Edge e : u.adjacencies)
 58             {
 59                 Vertex v = e.target;
 60                 double weight = e.weight;
 61                 double distanceThroughU = u.minDistance + weight;
 62         if (distanceThroughU < v.minDistance) {
 63             vertexQueue.remove(v);
 64 
 65             v.minDistance = distanceThroughU ;
 66             v.previous = u;
 67             vertexQueue.add(v);
 68         }
 69             }
 70         }
 71     }

最佳答案

是的,你可以。

建议的实现有一个主要的性能缺陷:

 62         if (distanceThroughU < v.minDistance) {
 63             vertexQueue.remove(v);
 64 
 65             v.minDistance = distanceThroughU ;
 66             v.previous = u;
 67             vertexQueue.add(v);
 68         }

这段代码的问题在于,从优先级队列中删除任意(不是根)节点 v 是在线性时间内完成的。 Java Docs :

Implementation note: this implementation provides O(log(n)) time for the enqueing and dequeing methods (offer, poll, remove() and add); linear time for the remove(Object) and contains(Object) methods; and constant time for the retrieval methods (peek, element, and size).

然而,在斐波那契堆中,您可以更有效地更改节点的优先级。

关于java - 使用斐波那契堆改进 Dijkstra?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30864186/

相关文章:

java - 如何修复 javax.persistence.Table.indexes()[Ljavax/persistence/Index?

Java Rmi stub 对象生命周期

c - 两个功能是否等价,work2 的改进时间是否超过 work1?

javascript - 寻路 javascript 算法无法正常工作

arrays - 确定网格上的点是否为 "trapped"(封闭)

java - 比较方法的多个返回值

Java列表,保留第一个项目,删除第二个重复项

java打印字符串给出指针编号

algorithm - 使用来自多个点的多个图像构建 3d 模型(kinect)

c++ - 使用动态规划改变的所有解决方案