How to Access Subgraphs of an Existing Graph in Boost

0 votes
I read a graph using read graphviz() and discovered it has subgraphs.

However, I can't locate anywhere in the Boost documentation that describes how to access these subgraphs.

I can only locate create subgraph(), which obviously does not work with existing subgraphs.

What am I overlooking?

Thank you in advance.
Aug 11, 2022 in C++ by Nicholas
• 7,760 points
1,065 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Here's a simple demo:

#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)
answered Aug 17, 2022 by Damon
• 4,960 points

edited 6 days ago

Related Questions In C++

0 votes
0 answers

How to declare an array of strings in C++?

I am trying to iterate over all ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
694 views
0 votes
1 answer

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,295 views
0 votes
0 answers

How to access static members of a class?

I'm learning C++ and Qt, but even the simplest code that I copy and paste from a book produces problems. On Ubuntu 10.04, I'm using g++4.4.2 with the QtCreator IDE.  Is there a distinction between the syntax of the g++ compiler and those of other compilers?  When I try to access static members, for example, something always goes wrong. #include <iostream> using namespace std; class A { ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
649 views
0 votes
1 answer

How to convert an instance of std::string to lower case

#include <algorithm> #include <cctype> #include <string> std::string data = "Abc"; std::transform(data.begin(), ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
2,210 views
0 votes
0 answers

How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?

I wanna create a program that can ...READ MORE

Aug 5, 2022 in C++ by krishna
• 2,820 points
1,113 views
0 votes
0 answers

How come an array's address is equal to its value in C?

The pointer values and pointer addresses in ...READ MORE

Aug 8, 2022 in C++ by krishna
• 2,820 points
535 views
0 votes
1 answer

How to expose std::pair to python using boost::python?

The most simple example of exposing std::pair is: class_<std::pair<int, int> ...READ MORE

answered Jun 20, 2019 in Python by SDeb
• 13,300 points
1,345 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
781 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,020 points
2,527 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
1,544 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP