Java: Using a Fibonacci Heap for Implementing Dijkstra's Algorithm

前端 未结 3 493
不思量自难忘°
不思量自难忘° 2021-01-03 13:56

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

相关标签:
3条回答
  • 2021-01-03 14:38

    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

    0 讨论(0)
  • 2021-01-03 14:44

    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.

    0 讨论(0)
  • 2021-01-03 14:53

    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).

    0 讨论(0)
提交回复
热议问题