![](/img/trans.png)
[英]how is boost::property_map implemented in boost and how to change it
[英]How to copy boost::property_map?
我想獲得兩組最短路徑(一到全部)從一個圖形衍生,定義為adjacency_list
包含內部特性(與包)
理論上,我可以在兩個參考節點n1
和n2
上運行dijkstra_shortest_paths
。 如果我創建兩個property_maps
並將它們依次傳遞給dijkstra_...
我會得到看起來像是同一張地圖的兩個視圖。 兩者都指向上一次運行dijkstra_shortest_paths
的結果,因此較早的結果消失了。 我應該怎么做才能達到預期的效果?
// Define some property maps
property_map<ugraph_t,edge_weight_t>::type Weight=get(edge_weight,G);
property_map<ugraph_t,vertex_distance_t>::type Dist1=get(vertex_distance,G);
// One line later, I expect this to be mapped to the SPs w.r.t n1
// Run SP on the first node
dijkstra_shortest_paths(G,n1,predecessor_map(Prev1).distance_map(Dist1).weight_map(Weight));
// New property maps
property_map<ugraph_t,vertex_distance_t>::type Dist2(Dist1); // And now to the second set
property_map<ugraph_t,vertex_predecessor_t>::type Prev2(Prev1); // But no two sets will result...
// Run SP on the second node
// This will run fine, but I will lose the first SP set (with or without a copy constructor above)
dijkstra_shortest_paths(G,n2,predecessor_map(Prev2).distance_map(Dist2).weight_map(Weight));
結論:如果我沒有記錯的話,可以將property_map
視為與迭代器的接口,因此復制property_map
毫無意義。 解決方案是傳遞一個動態構建的自定義容器。 該解決方案在下面@sehe的答案中有詳細說明,對此我非常感謝!
注意:僅當頂點容器類型為vecS
此方法才有效。 使用listS
,必須“手動”逐頂點復制。
距離圖不應視為內部屬性。 之前的地圖也是如此。
它們在邏輯上不是圖的屬性。 它們是查詢的結果。 因此,它們是查詢參數組合的屬性,包括圖形,起始節點等。
如果要保存內部屬性的值,只需以通常的方式保存它:
std::vector<double> saved_distances(num_vertices(G));
BGL_FORALL_VERTICES(v, G, ugraph_t)
saved_distances.push_back(Dist1[v]);
復制地圖的解決方法:
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/properties.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/iteration_macros.hpp>
using namespace boost;
using ugraph_traits = graph_traits<adjacency_list<vecS, vecS, directedS> >;
using ugraph_t = adjacency_list<
vecS, vecS, directedS,
property<vertex_distance_t, double,
property<vertex_predecessor_t, ugraph_traits::vertex_descriptor>
>,
property<edge_weight_t, double>
>;
int main() {
ugraph_t G(10);
ugraph_t::vertex_descriptor n1 = 0, n2 = 1, v;
(void) n1;
(void) n2;
// ...
property_map<ugraph_t, edge_weight_t>::type Weight = get(edge_weight,G);
property_map<ugraph_t, vertex_distance_t>::type Dist1 = get(vertex_distance,G);
property_map<ugraph_t, vertex_predecessor_t>::type Prev1 = get(vertex_predecessor,G);
dijkstra_shortest_paths(G, n1,
predecessor_map(Prev1)
.distance_map(Dist1)
.weight_map(Weight)
);
std::vector<double> saved_distances(num_vertices(G));
std::vector<ugraph_t::vertex_descriptor> saved_predecessors(num_vertices(G));
BGL_FORALL_VERTICES(v, G, ugraph_t) {
saved_distances.push_back(Dist1[v]);
saved_predecessors.push_back(Prev1[v]);
}
/*
* // C++11 style
* for(auto v : make_iterator_range(vertices(G)))
* saved_distances[v] = Dist1[v];
*/
// Run SP on the second node
dijkstra_shortest_paths(G,n2,predecessor_map(Prev1).distance_map(Dist1).weight_map(Weight));
}
我建議將結果映射到單獨的容器中,只保留邊緣權重內部:
更好的是 :重構以刪除重復的代碼
所以就變成了
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
using namespace boost;
using ugraph_t = adjacency_list<vecS, vecS, directedS, no_property, property<edge_weight_t, double> >;
using Vertex = ugraph_t::vertex_descriptor;
struct ShortestPaths {
ShortestPaths(size_t num_vertices);
std::vector<double> distances;
std::vector<Vertex> predecessors;
};
ShortestPaths GetShortestPaths(ugraph_t const& G, Vertex start);
int main() {
ugraph_t G(10);
Vertex n1 = 0, n2 = 1;
ShortestPaths sp1 = GetShortestPaths(G, n1);
ShortestPaths sp2 = GetShortestPaths(G, n2);
}
// some other cpp file...:
ShortestPaths::ShortestPaths(size_t num_vertices)
: distances(num_vertices), predecessors(num_vertices)
{ }
ShortestPaths GetShortestPaths(ugraph_t const& G, Vertex start) {
ShortestPaths result(num_vertices(G));
dijkstra_shortest_paths(G, start,
predecessor_map(make_container_vertex_map(result.predecessors, G))
.distance_map (make_container_vertex_map(result.distances, G))
.weight_map (get(edge_weight, G))
);
return result;
}
注意,不再需要復制結果。 實際上,您甚至不需要保留圖形即可保留查詢結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.