New here, but have been lurking as a guest for quite some time :)
Okay, so I\'ve been trying to do Dijkstra\'s shortest path algorithm using a Fibonacci heap (in Jav
It seems you are missing to add all the nodes the your heap with Double.POSITIVE_INFINITY (except the source node with 0.0 distance). That's why you are having NullPointerExceptions, they are simply not in the heap.
I made some test on several open-source Fibonacci Heap implementation. You can find the test itself here: Experimenting-with-dijkstras-algorithm. Also this is my Priority Queue version of the Dijsktra's algorithm: PriorityQueueDijkstra.java
The JDK does not provide an implementation of the Fibonacci Heap. You will have to create your own implementation, or you can find one in this post: Fibonacci Heap
All you have to afterwards is replacing
PriorityQueue<Vertex> vertexQueue = new PriorityQueue<>();
by
FibonacciHeap<Vertex> vertexQueue = new FibonacciHeap<>();
Then simply change the calls to thepoll
, add
and remove
methods with their equivalent in your provided implementation.
I worked with this algorithm myself. There is a comment above the decreaseKey function which explains the behaviour.
Decreases the key of the specified element to the new priority. If the new priority is greater than the old priority, this function throws an IllegalArgumentException. The new priority must be a finite double, so you cannot set the priority to be NaN, or +/- infinity. Doing so also throws an IllegalArgumentException. It is assumed that the entry belongs in this heap. For efficiency reasons, this is not checked at runtime.
As for the implementation, I think you would want to use myHeap.dequeueMin().getValue() instead of myHeap.min(). The difference is, dequeueMin() works like poll() and removes it from the heap after finding it.
And instead of myHeap.decreaseKey(v, v.minDistance), simply add it, like myHeap.insert(v, v.minDistance).