Over the past few weeks I've been playing with a variety of implementations of Dijkstra's algorithm as part of a personal project (mainly to test performance). I have recently come across this implementation of the algorithm, which I have tested and everything. However, I'm currently trying to modify that implementation so it takes an additional argument to represent the destination node, which means I want the algorithm to run only once from a specified source to a specified destination rather than to all other nodes in the graph.
I tried adding a third targetNode
parameter but the dest
variable found in the implementation is of type Entry<T>
and my parameter was of type Node
(a custom class I wrote), so I eventually got an incompatible types error message.
Is it possible to somehow make this modification? I did it easily with another implementation but I can't seem to figure it out for this one mainly due to the different types Node
and Entry<T>
. It's not really a big deal but I would like to do it.
Thanks!
EDIT: Here's what I did:
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;
}