我有一个
typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, VertexInfo, EdgeInfo > Graph;
我想要一份所有边缘都反转的拷贝。我想要一个拷贝而不是 View ,因为我想在不修改原始数据结构的情况下更改此图的边权重。
boost::reverse_graph 给出一个 View (不同类型)
最佳答案
您可以使用make_reverse_graph
:
例如输入
Graph read() {
Graph g;
using namespace boost;
dynamic_properties dp;
dp.property("node_id", get(&VertexInfo::label, g));
read_graphviz("digraph { a -> b; a -> c; b -> d; b -> e; e -> f; e -> c; }", g, dp);
return g;
}
图表:
反转:
int main() {
using namespace boost;
auto g = read();
auto r = make_reverse_graph(g);
write_graphviz(std::cout, r, make_label_writer(get(&VertexInfo::label, r)));
}
图表
完整演示
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/reverse_graph.hpp>
#include <libs/graph/src/read_graphviz_new.cpp>
struct VertexInfo {
std::string label;
};
struct EdgeInfo {
};
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexInfo, EdgeInfo> Graph;
Graph read() {
Graph g;
using namespace boost;
dynamic_properties dp;
dp.property("node_id", get(&VertexInfo::label, g));
read_graphviz("digraph { a -> b; a -> c; b -> d; b -> e; e -> f; e -> c; }", g, dp);
return g;
}
int main() {
using namespace boost;
auto g = read();
auto r = make_reverse_graph(g);
write_graphviz(std::cout, r, make_label_writer(get(&VertexInfo::label, r)));
}
关于c++ - 获取反转的 boost 图的拷贝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30101788/