使用Boost Graph库查找连接的组件,顶点和边缘类型为boost :: listS

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Boost Graph库查找连接的组件,顶点和边缘类型为boost :: listS相关的知识,希望对你有一定的参考价值。

我试图在无向图中找到连通的组件。该图由boost::adjacency_list表示。对于我的应用程序,顶点和边缘类型必须是boost::listS。我尝试了以下代码,但不编译。但是,如果我将顶点和边缘类型更改为boost::vecS,代码将正常工作。有关如何修复代码的任何想法?

#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<<"
";
            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
}    
答案

docs say¹组件映射需要使用键vertex_descriptor建模可写属性映射并将组件id存储为值。

你应该使用make_assoc_property_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 << "
";
}

打印:

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

但我想要一个矢量

如果你坚持,你仍然可以使用向量但是你必须提供从顶点描述符到整数顶点id的映射:

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

现在,您还必须填写该属性:

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

然后你必须使用make_iterator_property_map,为间接提供顶点索引属性 - 映射:

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

打印

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

以上是关于使用Boost Graph库查找连接的组件,顶点和边缘类型为boost :: listS的主要内容,如果未能解决你的问题,请参考以下文章

删除顶点并再次添加它会导致 boost::graph 崩溃?

使用 Boost Graph 顶点属性进行动态分配

使用整数索引访问 boost::graph 中的特定边

使用 Boost Graph [BGL] 检查在 add_edge 之前是不是已经存在顶点

如何使用 Boost Graph Library 创建 named_graph?

图的割集,Boost Graph Library