在过去的几周里,我一直在玩Dijkstra算法的各种实现作为个人项目的一部分(主要是为了测试性能)。我最近碰到了算法的this implementation,我已经测试了一切。但是,我正在尝试修改该实现,因此它需要一个额外的参数来表示目标节点,这意味着我希望算法仅从指定的源运行一次到指定的目标,而不是图中的所有其他节点。Dijkstra算法特殊实现的小修改
我尝试添加第三targetNode
参数,但在执行中发现的dest
变量是Entry<T>
型的,我的参数是Node
型(自定义类我写的),所以我最终得到了一个不兼容的类型的错误消息。
是否有可能以某种方式进行此修改?我用另一个实现很容易做到了,但我似乎无法解决这个问题,主要是由于不同的类型Node
和Entry<T>
。这不是什么大不了的事情,但我想这样做。
谢谢!
编辑:这就是我所做的:
public static <Node> Map<Node, Double> dijkstraFibonacciHeap(DirectedGraph<Node> graph, Node source, Node target) {
FibonacciHeap<Node> priorityQueue = new FibonacciHeap<>();
Map<Node, FibonacciHeap.Entry<Node>> entries = new HashMap<>();
Map<Node, Double> result = new HashMap<>();
for (Node node : graph) {
entries.put(node, priorityQueue.enqueue(node, Double.POSITIVE_INFINITY));
}
priorityQueue.decreaseKey(entries.get(source), 0.0);
while (!priorityQueue.isEmpty()) {
FibonacciHeap.Entry<Node> curr = priorityQueue.dequeueMin();
result.put(curr.getValue(), curr.getPriority());
for (Map.Entry<Node, Double> arc : graph.edgesFrom(curr.getValue()).entrySet()) {
if (result.containsKey(arc.getKey())) {
continue;
}
double pathCost = curr.getPriority() + arc.getValue();
// Error occurrs here.
target = entries.get(arc.getKey());
if (pathCost < target.getPriority()) {
priorityQueue.decreaseKey(target, pathCost);
}
}
}
return result;
}
请发布您正在尝试的实际代码片段。 – 2013-04-10 18:25:02
完成。谢谢你提醒我。 – 2013-04-10 18:31:00