gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:15:50 25 4
gpt4 key购买 nike

我已经使用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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com