1

I have read a graph with read_graphviz() and know the graph contains subgraphs. However, I cannot find where the Boost documentation covers how to access said subgraphs. All I can find is create_subgraph(), which obviously does not access existing subgraphs. What am I missing?

Thanks in advance

sehe
  • 374,641
  • 47
  • 450
  • 633
larkwiot
  • 63
  • 6

1 Answers1

1

The documentation lists these member functions that aid in subgraph traversal/navigation:

  • subgraph& root()

    Returns the root graph of the subgraph tree.

  • bool is_root() const

    Return true if the graph is the root of the subgraph tree, and returns false otherwise.

  • subgraph& parent()

    Returns the parent graph.

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

    Return an iterator pair for accessing the children subgraphs.

Basing a sample off my much more complete demonstration of Subgraphs with Graphviz support (here: Boost.Graph and Graphviz nested subgraphs) here's a simple demo:

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

Prints

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

sehe
  • 374,641
  • 47
  • 450
  • 633