简体   繁体   English

boost :: dijkstra_shortest_paths覆盖内部图形权重?

[英]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. 尽管在上面的代码中graphconst引用 ,但是此后该图表的边缘权重将更改 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.

相关问题 dijkstra_shortest_paths Boost Graph Lib 1.57.0失败 - dijkstra_shortest_paths Boost Graph Lib 1.57.0 fails Boost中的自定义图形距离dijkstra_shortest_paths - Custom graph distance in Boost dijkstra_shortest_paths 增强图:dijkstra_shortest_paths:无法形成对“ void”的引用 - Boost graph: dijkstra_shortest_paths: cannot form a reference to 'void' 如何从 GraphML 获取边权重到 boost::dijkstra_shortest_paths? - How do I get edge weights from GraphML into boost::dijkstra_shortest_paths? 用boost :: graph包装我的自定义图并计算dijkstra_shortest_paths - Wrap my custom graph with boost::graph and calculate dijkstra_shortest_paths 当特定节点对之间存在多条最短路径时,boost graph dijkstra_shortest_paths 如何选择最短路径? - How does boost graph dijkstra_shortest_paths pick the shortest path when there are multiple shortest paths between a specific pair of nodes? boost的dijkstra_shortest_paths中的负边权重检查 - Negative edge weight check in boost's dijkstra_shortest_paths 我可以在“循环”有向图的 BGL 中使用 dijkstra_shortest_paths - Can I use dijkstra_shortest_paths in BGL on "cyclic" directed graph 在dijkstra_shortest_paths中使用捆绑属性作为权重映射 - Using bundled properties as the weight map in dijkstra_shortest_paths 提升BGL Dijkstra最短路径 - Boost BGL Dijkstra Shortest Paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM