[英]boost::dijkstra_shortest_paths overwriting internal graph weights?
I am using the Boost Graph Library to store an undirected graph with double
edge weights and double
vertex weights. 我正在使用Boost Graph库来存储具有
double
边缘权重和double
顶点权重的无向图。 At several places in my code, I need to apply Dijkstra's algorithm in order to search for shortest paths. 在我的代码中的几个地方,我需要应用Dijkstra的算法来搜索最短路径。 This works quite well until I decided I wanted to temporarily override the stored edge weights by my own weights (only temporarily, the graph weights shall not be modified).
直到我决定我想用自己的权重临时覆盖存储的边缘权重(直到暂时,图形权重不会被修改),此方法才有效。 My code basically looks like this:
我的代码基本上如下所示:
// Initial typedefs
typedef boost::property<boost::edge_weight_t, double> edge_weight_t;
typedef boost::property<boost::vertex_discover_time_t, double> vertex_weight_t;
typedef boost::adjacency_list<boost::vecS,
boost::vecS,
boost::undirectedS,
vertex_weight_t,
edge_weight_t> graph_t;
// In a function, where graph is a const reference of type graph_t
std::vector<double> pathLengths( boost::num_vertices( graph ) );
boost::property_map<graph_t, boost::edge_weight_t>::type weightMap;
boost::graph_traits<graph_t>::edge_iterator e_it, e_it_end;
for( boost::tie( e_it, e_it_end ) = boost::edges( graph );
e_it != e_it_end;
++e_it )
{
weightMap[ *e_it ] = 1.0;
}
boost::dijkstra_shortest_paths( graph,
boost::vertex( vertex, graph ),
boost::distance_map( &pathLengths[0] ).weight_map( weightMap ) );
Although graph
is a const reference in the code above, the edge weights of the graph will be changed afterwards. 尽管在上面的代码中
graph
是const引用 ,但是此后该图表的边缘权重将更改 。 What am I doing wrong? 我究竟做错了什么? Or more specifically, how can I temporarily override the edge weights in a weighted graph?
或更具体地说,如何临时覆盖加权图中的边缘权重?
Obviously, I could simply store the current edge weights, replace them by my weights, and change them back afterwards. 显然,我可以简单地存储当前的边缘权重,用我的权重替换它们,然后再更改它们。 However, I am convinced that I am at fault, and I do not want ignore this problem.
但是,我确信自己有错,并且我不想忽略这个问题。
I had, I believe, the same issue - I wanted to temporarily (for a specific run of a search algorithm) modify edge weights, without permanently changing the graph itself. 我曾经遇到过同样的问题-我想临时(针对搜索算法的特定运行)修改边缘权重,而又不永久更改图形本身。 After some searching, I found this, which allows you to register a functor that is used to generate weights.
经过一番搜索,我发现了这一点,它使您可以注册用于生成权重的函子。 This is used as the weight_map parameter:
这用作weight_map参数:
http://www.boost.org/doc/libs/1_51_0/boost/property_map/function_property_map.hpp http://www.boost.org/doc/libs/1_51_0/boost/property_map/function_property_map.hpp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.