gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:22:49 27 4
gpt4 key购买 nike

我找到了 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com