C++ 使用Boost图形库查找连接的组件,顶点和边类型为Boost::List
我试图在一个无向图中找到连接的组件。该图由C++ 使用Boost图形库查找连接的组件,顶点和边类型为Boost::List,c++,boost,graph,C++,Boost,Graph,我试图在一个无向图中找到连接的组件。该图由boost::adjacence\u list表示。对于我的应用程序,顶点和边类型必须是boost::list。我尝试了下面的代码,但没有编译。但是,如果我将顶点和边类型更改为boost::vecS,代码将正常工作。你知道如何修复代码吗 #include <iostream> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/connected_c
boost::adjacence\u list
表示。对于我的应用程序,顶点和边类型必须是boost::list
。我尝试了下面的代码,但没有编译。但是,如果我将顶点和边类型更改为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<<"\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
}
#包括
#包括
#包括
typedef boost::邻接列表集群;
int main(){
使用名称空间boost;
A组;
添加_顶点(A);
添加_顶点(A);
添加_顶点(A);
添加顶点(A);//将4个顶点添加到图中
对于(int i=0;i组件映射需要使用键顶点描述符
对可写属性映射进行建模,并将组件id存储为值
您可能应该将make_assoc_property_map
与映射一起使用:
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";
}
#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";
}
}
但我想要一个向量
如果您坚持,您仍然可以使用向量,但必须提供从顶点描述符到积分顶点id的映射:
typedef boost::adjacency_list<
boost::listS,
boost::listS,
boost::undirectedS,
boost::property<boost::vertex_index_t, int>
> Cluster;
然后必须使用生成迭代器属性映射
,为间接寻址提供顶点索引属性映射:
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
CharStyle非常好用!非常感谢。
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
#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";
}
}
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