c++ - 如何在Boost中访问现有图的子图

标签 c++ boost boost-graph

我已经使用read_graphviz()阅读了一个图,并且知道该图包含子图。但是,我找不到Boost文档在哪里涵盖如何访问所述子图的地方。我只能找到create_subgraph(),它显然无法访问现有的子图。我想念什么?

提前致谢

最佳答案

documentation列出了这些有助于子图遍历/导航的成员函数:

  • subgraph& root()
    返回子图树的根图。
  • bool is_root() const
    如果图是子图树的根,则返回true,否则返回false。
  • subgraph& parent()
    返回父图。
  • std::pair<children_iterator, children_iterator> children() const
    返回用于访问子图子的迭代器对。

  • 基于我在Graphviz支持下更完整的Subgraph演示的示例(此处:Boost.Graph and Graphviz nested subgraphs),这是一个简单的演示:

    Live On Coliru
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/subgraph.hpp>
    #include <iostream>
    
    template <typename SubGraph> SubGraph create_data()
    {
        enum { A,B,C,D,E,F,N }; // main edges
        SubGraph main(N);
    
        SubGraph& sub1 = main.create_subgraph();
        SubGraph& sub2 = main.create_subgraph();
    
        auto A1 = add_vertex(A, sub1);
        auto B1 = add_vertex(B, sub1);
    
        auto E2 = add_vertex(E, sub2);
        auto C2 = add_vertex(C, sub2);
        auto F2 = add_vertex(F, sub2);
    
        add_edge(A1, B1, sub1);
        add_edge(E2, F2, sub2);
        add_edge(C2, F2, sub2);
    
        add_edge(E, B, main);
        add_edge(B, C, main);
        add_edge(B, D, main);
        add_edge(F, D, main);
    
        // setting some graph viz attributes
        get_property(main, boost::graph_name) = "G0";
        get_property(sub1, boost::graph_name) = "clusterG1";
        get_property(sub2, boost::graph_name) = "clusterG2";
    
        return main;
    }
    
    using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, 
            boost::no_property,
            boost::property<boost::edge_index_t, int>,
            boost::property<boost::graph_name_t, std::string>
        >;
    
    template <typename G>
    void list_nested(boost::subgraph<G>& g, std::string const& prefix = "") {
        std::cout << prefix
            << " * " << get_property(g, boost::graph_name)
            << " (" << num_vertices(g) << "+" << num_edges(g) << " v+e)"
            << "\n";
        for (auto& child : make_iterator_range(g.children())) {
            list_nested(child, " -");
        }
    }
    
    int main() {
        auto g = create_data<boost::subgraph<Graph> >();
        list_nested(g);
    }
    

    版画
     * G0 (6+7 v+e)
     - * clusterG1 (2+1 v+e)
     - * clusterG2 (3+2 v+e)
    

    关于c++ - 如何在Boost中访问现有图的子图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61987828/

    相关文章:

    c++ - 将 JSON 文本文件转换回 QJsonArray

    在调用析构函数之前删除的 C++ 动态成员数组

    c++ - boost 库路径

    c++ - 在 Boost.Graph 中向图形添加边

    c++ - Qt 部署的可执行文件打开空白应用程序

    c++ - 圆的顶点数组

    c++ - 在类的无序映射中使用滚动累加器

    c++ - 使用 boost 的内存映射二维数组

    c++ - Boost 图形库 - 未找到 adjacent_vertices 函数

    c++ - BGL : How do I store edge_descriptors and vertex_descriptors efficiently?