【发布时间】:2013-04-10 18:18:38
【问题描述】:
在过去的几周里,作为个人项目的一部分(主要是为了测试性能),我一直在使用 Dijkstra 算法的各种实现。我最近遇到了算法的this implementation,我已经测试过它和所有东西。但是,我目前正在尝试修改该实现,因此它需要一个额外的参数来表示目标节点,这意味着我希望算法只从指定的源运行一次到指定的目标,而不是图中的所有其他节点.
我尝试添加第三个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;
}
【问题讨论】:
-
请发布您正在尝试的实际代码 sn-p。
-
完成。谢谢你提醒我。