I am using a priority_queue-s for implementing A* and Dijkstra's algorithm. Is there a point writing my own heap for time optimization? How faster would it be? I read in wikipedia that Fibonacci heap will be the best. Do you agree?

Is there a point writing my own heap for time optimization?

It's pretty unlikely that an ad hoc heap would be faster than std::priority_queue. Not to mention the effort of reinventing the wheel *and* the huge risk of bugs in your implementation that just aren't there with an existing oft used library.

Ask yourself if the potential speed improvements are worth the effort and risks. In my experience, the choice is nearly always to use the existing library instead of writing your own.

Also, a word of warning if you are using MS Visual Studio:

VS has a lot of runtime checks built in their debug implementation of STL, so if you run it under debug it will most likely feel terribly slow.

In my case it came out when I wrote a simple app to render Mandelbrot and Julia sets. The difference was immense. In debug, it would take a fair amount of time to render with around 16 iterations, while in release it would be faster even at around 60-70 iterations.

The main problem you will have with STL's priority-queue is not the efficiency, but the fact that it provides no means to update the priority value of elements already in the queue. This is an operation that is required in A* (at least, the last time I checked).

If you take a look at the Boost.Graph Library's implementations of A* and Dijkstra, which both are just thin-wrappers for a Breadth-first visitation algorithm, use a custom priority-queue implementation. This priority-queue is called d_ary_heap_indirect which is a D-ary heap implementation with an indirect priority value, but most importantly, it includes the necessary update() and push_or_update() functions that STL's priority-queue lacks.

The STL's priority-queue will be faster for more simple operations (just pushing and popping) because it is a relaxed heap (giving constant-time pushing). But, for more dynamic operations, I recommend BGL's d_ary_heap_indirect implementation. Better, yet, you could just use the BGL's A* and Dijkstra implementations.

This article has been dead for over six months. Start a new discussion instead.