4

The new 'heap' boost library includes a fibonacci heap. The complexity of each implementation can be seen here: http://www.boost.org/doc/libs/1_51_0/doc/html/heap/data_structures.html.

My question is this: Why is the fibonacci heap decrease operation O(log(N)), while the increase operation is O(1)?

I would like to experiment with using the fibonacci heap in Dijkstra's algorithm, which is heavily dependent upon a fast decrease operation.

user1487088
  • 231
  • 3
  • 6
  • Read the wikipedia article: http://en.wikipedia.org/wiki/Fibonacci_heap – Keith Randall Oct 02 '12 at 21:15
  • The Wikipedia article only talks about the decrease key being amortised O(1). – user1487088 Oct 02 '12 at 21:39
  • Well, min and max are swapped between the wikipedia article (which is a min heap) and the Boost library (which is a max heap). To use the Boost library for Dijkstra's, you'll have to reverse your score comparisons. – Keith Randall Oct 02 '12 at 21:49
  • Ok, I am interpreting 'increase' as pushing an element closer to the top of the heap, and decrease and pushing an element further away from the top. Whether the top is the min or max element, it shouldn't matter? Is this correct? – user1487088 Oct 02 '12 at 22:35
  • So with your interpretation, increase is amortized O(1) - that's the fundamental advantage of a Fibonacci heap. Decrease is O(log n) because all of the other standard heap operations have that complexity (you could implement decrease with delete + insert). – Keith Randall Oct 02 '12 at 23:12

1 Answers1

4

According to http://www.boost.org/doc/libs/1_51_0/doc/html/heap/concepts.html

boost.heap implements priority queues as max-heaps to be consistent with the STL heap functions. This is in contrast to the typical textbook design, which uses min-heaps.

The textbook/wikipedia Fibonacci heap has the highest priority element with the lowest value, aka a min-heap (e.g. "1" is higher priority than "2"). STL and Boost (for consistency with STL) reverse the definition so that the highest priority has the highest value, aka a max-heap (i.e. a "2" higher priority than "1").

Essentially it means that decrease and increase have inverse meanings between textbook and Boost.

If you want to get a min-heap (like the textbook definitions), you must first defined an appropriate boost::heap::compare functor for your fibonacci_heap (see an example here: Defining compare function for fibonacci heap in boost), then call increase whenever you decrease the value associated with a heap element (and are thus increasing the priority) and vice-versa.

Community
  • 1
  • 1
ɲeuroburɳ
  • 6,594
  • 3
  • 21
  • 20