Search code examples
c++algorithmstlpath-findinga-star

C++ A-star implementation -- determining whether a node is already in the priority queue of open items


One step in the A* pathfinding algorithm requires searching the list of open nodes for the node you're currently interacting with, and adding that node to the list if it isn't already there, or updating its value and parent, if it's present but with a higher weight than the current version of the node.

These behaviors aren't supported in the STL priority_queue structure. How should I implement that step?

Updates since this question is getting a lot of views:

  • std::priority_queue may look like a good choice for this, but it isn't.

  • Implementing A* yourself is an enormous confidence-booster, but after you've done it, you should try to switch to using the one provided by boost. I was nervous about installing it when I asked this question, but installation is very easy and won't produce any complications; and A* isn't the only useful functionality that boost provides. (In particular, if you don't use their string-processing functionality, you'll end up writing your own copy of it; I speak from personal experience...)


Solution

  • You can use a plain vector or array to store the elements and then use std::make_heap, std::push_heap, std::pop_heap, std::sort_heap, std::is_heap and std::is_heap_until to manage it.

    This allows you to break containment and implement custom operations on a priority queue, without having to implement the standard operations yourself.