6

where is the Fibonacci Heap in STL ? and if STL do not implement Fibonacci Heap what is the best practice to implement it using existing algorithms and containers in STL ?

templatetypedef
  • 328,018
  • 92
  • 813
  • 992
amin
  • 2,844
  • 3
  • 25
  • 51
  • 1
    There's a C++ implementation in [Wikipedia](http://ideone.com/9jYnv) that seems pretty decent. – Rapptz Jan 02 '13 at 07:32
  • 2
    Probably because the STL was complex enough as it is, and it generally only provides the most used/needed functionality. As usual, however, boost has it: http://www.boost.org/doc/libs/1_49_0/doc/html/heap.html – Yuushi Jan 02 '13 at 07:34

3 Answers3

15

boost has an implementation of it. Hope that helps. There doesn't seem to be one in the STL. Here's an example:

 for(int n=0;n<40;++n){
    std::cout<<"F("<<n<<")="<<fibonacci(n)<<std::endl;
  }
hd1
  • 30,506
  • 4
  • 69
  • 81
1

Even though it is not stated explicitly as a Fibonacci heap, the STL implements a priority_queue which has the same complexity and the same api and behavior as a Fibonacci heap (and may actually be a Fibonacci heap in disguise)

https://en.cppreference.com/w/cpp/container/priority_queue

hl037_
  • 2,589
  • 16
  • 44
  • `std::priority_queue` is container adaptor which means that it takes another container (like `std::vector`) and provides new operations using that container in the background. It requires its container to provide random access iterator which means that it uses [binary heap](https://en.wikipedia.org/wiki/Binary_heap) in the background (theoreticaly it could use other heap implementation based on arrays). Bianary heap and Fibonacci heap don't have the same complexities. STL queue also has limited api compared to i.e. boost Fibonacci heap. – mix Sep 18 '20 at 18:59
0

no, there is no guaranteed fibonacci heap in the standard library

for an example of implementing a custom allocation scheme in C++, see the small object allocator in the Loki library


EDIT: sorry, i was thinking of the fibonacci buddy system for implementing a dynamic memory allocation heap.

Cheers and hth. - Alf
  • 135,616
  • 15
  • 192
  • 304