c++ - 获取反转的 boost 图的拷贝

标签 c++ boost

我有一个

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;
}

图表:

enter image description here

反转:

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)));
}

图表

enter image description here

完整演示

Live On Coliru

#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/

相关文章:

c++ - 如何使用Boost获取2D点的凸包面积?

c++ - 将理性转换 boost 一倍

c++ - 如何使用GYP?

c++ - 我怎样才能在两点之间进行碰撞检查? (C++)

c++ - boost 与成员函数/变量的绑定(bind)

c++ - 从哑指针 vector 切换到 boost::shared_ptr 时出现问题

c++ - boost asio tcp socket 1.70 不向后兼容

c++ - 双缓冲winAPI

c++ - 代码中哪里定义了DEBUG?

C++ 标准特性和二进制大小