Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dijkstra on Java: Getting Interesting Results Using a Fibonacci Heap vs. PriorityQueue

I recently made an initial comparison of the running time of Dijkstra's algorithm using two data structures, the Java-based PriorityQueue (based on a binary heap, if I'm not mistaken), and a Fibonacci heap. I used Java's currentTimeMillis() to make my calculations. The results I ended up with are quite interesting. This is the output for one of my testcases:

Running Dijkstra's with 8 nodes and 27 links
- Execution time with binary heap: 1 miliseconds
- Execution time with Fibonacci heap: 4 miliseconds

Admittedly, I'm short on data sets at the the moment, with the above graph being my largest (I plan to make more soon). But does this make any sense? I have always thought Fibonacci heaps were faster than other data structures due to their amortized running time compared to the other data structures. I'm not really sure where this 3-milisecond difference is coming from. (I'm running it on an Intel Core Ivy Bridge i7-3630M processor, if that helps.)

Note: I stumbled upon this thread which might explain the issue, though I'm still unclear as to why the Fibonacci heap version is taking longer. According to that thread, it could be because my graph is not dense enough and therefore the number of decrease-Key operations is not large enough for the performance of the Fibonacci heap to really shine. Would this be the only plausible conclusion, or is there something else I'm missing?

like image 767
Fiery Phoenix Avatar asked Apr 04 '13 15:04

Fiery Phoenix


People also ask

What is the time complexity of Dijkstra algorithm by using Fibonacci heaps?

Dijkstra's original shortest path algorithm does not use a priority queue, and runs in O(V2) time. When using a Fibonacci heap as a priority queue, it runs in O(E + V log V) time, which is asymptotically the fastest known time complexity for this problem.

Is Fibonacci heap the best?

In computer science, a Fibonacci heap is a data structure for priority queue operations, consisting of a collection of heap-ordered trees. It has a better amortized running time than many other priority queue data structures including the binary heap and binomial heap.

Does Dijkstra's algorithm use a heap?

The idea is to traverse all vertices of the graph using BFS and use a Min Heap to store the vertices not yet included in SPT (or the vertices for which the shortest distance is not finalized yet). Min Heap is used as a priority queue to get the minimum distance vertex from a set of not yet included vertices.

Are Fibonacci heaps used?

Fibonacci heaps are used to implement the priority queue element in Dijkstra's algorithm, giving the algorithm a very efficient running time. Fibonacci heaps have a faster amortized running time than other heap types.


1 Answers

Fibonacci heaps are asymptotically faster than binary heaps (the data structure used in Java's priority queue), in that Dijkstra's algorithm will take O(m + n log n) time with a Fibonacci heap but O(m log n) with a binary heap. This means that for large, dense graphs, in the worst case, Fibonacci heaps will be faster.

Although Fibonacci heaps are asymptotically faster than binary heaps, they have notoriously large constant factors and many basic operations on Fibonacci heaps take a long time to complete. In the long run they'll outperform binary heaps, but for small graphs the constant terms might be so large that the Fibonacci heap is actually slower.

Second, compare the asymptotic runtimes (O(m + n log n) versus O(m log n)). If the graph you're using is sparse (that is, m = O(n)), then both of these asymptotic runtimes are the same (O(n log n)). In that case, the theoretical advantage of Fibonacci heaps isn't present and the binary heap might be the superior choice.

Finally, note that big-O notation refers to worst-case behavior in this case, rather than average-case. There was a paper a while back that showed that for random graphs of a certain type, that Dijkstra's algorithm on expectation takes far lower than the worst-case number of decrease-key and dequeue operations. In that case, a binary heap could outperform a Fibonacci heap even on large graphs, since the worst-case behavior miht never get triggered.

Hope this helps!

like image 174
templatetypedef Avatar answered Sep 22 '22 09:09

templatetypedef