【问题标题】:Get a copy of a boost graph that is reversed获取逆向提升图的副本
【发布时间】:2015-05-08 05:25:49
【问题描述】:

我有一个

typedef boost::adjacency_list< boost::vecS, boost::vecS, boost::bidirectionalS, VertexInfo, EdgeInfo > Graph;

我想要一个所有边都颠倒的副本。我想要一个副本而不是视图,因为我想在不修改原始数据结构的情况下更改此图的边权重。

boost::reverse_graph 给出一个视图(具有不同的类型)

【问题讨论】:

    标签: c++ boost


    【解决方案1】:

    你可以使用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)));
    }
    

    图表

    完整演示

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

    【讨论】:

    • 我不知道的很酷的可视化,以及很好的答案!
    【解决方案2】:

    找到答案:

    boost::copy_graph(boost::make_reverse_graph(_graph), _reverse_graph);
    

    其中 _reverse_graph 是 Graph 类型

    【讨论】:

      猜你喜欢
      • 2022-01-21
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 2016-05-03
      相关资源
      最近更新 更多