andy90
andy90

Reputation: 555

Find connected components using Boost Graph library, with the vertex and edge type being boost::listS

I am trying to find connected components in an undirected graph. The graph is represented by boost::adjacency_list. For my application, the vertex and edge type have to be boost::listS. I tried the following code, which doesn't compile. However, if I change the vertex and edge type to boost::vecS, the code will just work fine. Any idea on how to fix the code?

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
typedef boost::adjacency_list<boost::listS,boost::listS,boost::undirectedS> Cluster;
int main() {
    using namespace boost;
    Cluster A;
    add_vertex(A);
    add_vertex(A);
    add_vertex(A);
    add_vertex(A); // add 4 vertex to the graph

    for (int i=0; i<2; i++){
        for (int j=(i+1); j<3; j++){
            std::cout<<i<<"  "<<j<<"\n";
            add_edge(vertex(i,A), vertex(j,A), A);
        }
    } // add edge between 0-1, 0-2, 1-2.

    std::vector<int> subclusters(num_vertices(A));
    int nclusters=boost::connected_components(A, &subclusters[0]); // find the connected components
}    

Upvotes: 3

Views: 1217

Answers (1)

sehe
sehe

Reputation: 392893

The docs say¹ the component map needs to model a writable property map with key vertex_descriptor and storing the compoment id as value.

You should probably use make_assoc_property_map with a map:

Live On Coliru

std::map<Cluster::vertex_descriptor, int> subclusters;
int nclusters = boost::connected_components(A, boost::make_assoc_property_map(subclusters)); // find the connected components


for (auto& p : subclusters) {
    std::cout << "Vertex " << boost::get(boost::vertex_index, A, p.first) << " is in cluster " << p.second << "\n";
}

Prints:

0 -- 1
0 -- 2
1 -- 2
Vertex 0 is in cluster 0
Vertex 1 is in cluster 0
Vertex 2 is in cluster 0
Vertex 3 is in cluster 1

But I Want A Vector

If you insist, you can still use a vector BUT you'll have to provide the mapping from vertex-descriptor to an integral vertex id:

typedef boost::adjacency_list<
        boost::listS,
        boost::listS,
        boost::undirectedS,
        boost::property<boost::vertex_index_t, int>
    > Cluster;

Now, you have to fill that property too:

Cluster A;
add_vertex(0, A);
add_vertex(1, A);
add_vertex(2, A);
add_vertex(3, A);

And then you have to use make_iterator_property_map instead, supplying a vertex index property-map for the indirection:

std::vector<int> subclusters(4);
auto comp_map = make_iterator_property_map(subclusters.begin(), get(boost::vertex_index, A));
int nclusters = connected_components(A, comp_map); // find the connected components

Live On Coliru

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>
#include <iostream>

typedef boost::adjacency_list<
        boost::listS,
        boost::listS,
        boost::undirectedS,
        boost::property<boost::vertex_index_t, int>
    > Cluster;

int main() {
    Cluster A;
    add_vertex(0, A);
    add_vertex(1, A);
    add_vertex(2, A);
    add_vertex(3, A);

    for (int i = 0; i < 2; i++) {
        for (int j = (i + 1); j < 3; j++) {
            std::cout << i << " -- " << j << "\n";
            add_edge(vertex(i, A), vertex(j, A), A);
        }
    } // add edge between 0-1, 0-2, 1-2.

    // NOTE: 4, not "num_vertices", but enough to accomodate the highest value
    // of the `vertex_index` property
    std::vector<int> subclusters(4);
    auto comp_map = make_iterator_property_map(subclusters.begin(), get(boost::vertex_index, A));
    int nclusters = connected_components(A, comp_map); // find the connected components

    for (size_t id = 0; id < subclusters.size(); ++id) {
        std::cout << "Vertex id " << id << " is in cluster " << subclusters.at(id) << "\n";
    }
}

Prints

0 -- 1
0 -- 2
1 -- 2
Vertex id 0 is in cluster 0
Vertex id 1 is in cluster 0
Vertex id 2 is in cluster 0
Vertex id 3 is in cluster 1

¹ enter image description here

Upvotes: 5

Related Questions