Search code examples
c++algorithmdijkstraboost-graph

Boost::graph Dijkstra : initially populating the queue


I'm using boost::graph and its Dijkstra implementation.

I want to compute THE shortest path from a set of vertices to another set of vertices. I do not want to compute all the possible paths between those sets.

The idea is the following : I'm in a building with entrances on various streets. So I can start my journey on any of those streets. But I'm only interested in the shortest one.

If I had used my own implementation of Dijkstra's algorithm, I would have done the following:

  • For each start node, the distance map to 0
  • Add the start node to the priority queue.

While it's easy to set the distance map to 0 using boost::dijkstra_shortest_paths_no_init, I cannot figure out how to add the node to the priority queue. I looked into the source code, and it seems pretty much impossible. So I'm thinking of defining my own Combine functor that will return a 0 distance if I reach one of the start nodes, but it seems rather ugly.

I could create a virtual node, and add edges from the virtual node to starting nodes. However, this triggers some concurrent access problems I would like to avoid.

Did I miss a possibility in the boost library, or does someone know of a clever workaround. I'm also thinking of patching boost to allow a custom initialization of the priority queue.


Solution

  • I've not used boost::graph, and I hope somebody with better knowledge of it will give a better answer, but perhaps you could create a graph type that wraps the existing graph, leaving the original unmodified, but exposing to the algorithm a view that includes your virtual nodes and edges? If not, is it infeasible to copy the whole graph?