如何在Boost中访问现有图的子图?

问题描述 投票:1回答:1

我已经用read_graphviz()读取了一个图,并且知道这个图包含子图。然而,我找不到Boost文档中关于如何访问子图的内容。我只能找到create_subgraph(),这显然不能访问现有的子图。我遗漏了什么?

先谢谢你

c++ boost boost-graph
1个回答
1
投票

文件 列出了这些帮助子图遍历导航的成员函数。

  • subgraph& root()

    返回子图树的根图。

  • bool is_root() const

    如果该图是子图树的根图,则返回true,否则返回false。

  • subgraph& parent()

    返回父图。

  • std::pair<children_iterator, children_iterator> children() const

    返回一个迭代器对,用于访问子图。

基于我的更完整的支持Graphviz的Subgraphs演示示例(这里。Boost.Graph和Graphviz嵌套子图。)这里有一个简单的演示。

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)

© www.soinside.com 2019 - 2024. All rights reserved.