使用斐波那契堆改进 Dijkstra?
Improving on Dijkstra using Fibonacci heaps?
我发现 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).
然而,在斐波那契堆中,您可以更有效地更改节点的优先级。
我发现 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).
然而,在斐波那契堆中,您可以更有效地更改节点的优先级。