如果我有一个只有 1 个节点且没有边的图。连通分量数 = 1,对吗?
如果我有一个有 2 个节点且没有边的图。连通分量的数量 = 2,对吗?
如果我有一个包含 2 个节点和 1 个边的图。连通分量数 = 1,对吗?
我有这段代码:
typedef adjacency_list <vecS, vecS, undirectedS> Graph;
typedef graph_traits < Graph >::edge_descriptor Edge;
/* typedef graph_traits<Graph>::vertex_descriptor Vertex;
typedef property_map<Graph, vertex_index_t>::type IndexMap;
typedef std::pair<int,int> E;
typedef graph_traits<Graph>::vertex_iterator vertex_iter;
typedef graph_traits<Graph>::out_edge_iterator edge_iter;
typedef property_map<Graph, vertex_index_t>::type VertexIndexMap;
typedef property_map<Graph, edge_weight_t>::type EdgeWeightMap;*/
int main(int,char*[])
{
int num_nodes,num_edges;
cin >> num_nodes >> num_edges;
Graph g(num_nodes);
for(int i = 0;i < num_edges; i++) // i/p edges into graph g
{
int e1,e2;
cin >> e1 >> e2;
Edge e;
bool success;
tie(e,success) = add_edge(e1, e2, g);
}
vector <int> components(num_nodes);
int num = connected_components(g, &components[0]);
cout<<"moooooo"<<num<<endl;
对于这些输入:
1 0
和
2 0
和
2 1
分别得到
1
和
2
和
2
为什么?现在不应该是1吗?我是否误解了连接的组件?
我的程序为所有输入提供 num=2,我的输入如下:
4 3
1 2
2 3
3 4
4 4
1 2
2 3
3 4
4 1
5 5
1 2
2 3
3 4
4 1
3 5
6 6
1 2
2 3
3 4
4 1
3 5
5 6
8 9
1 2
2 3
3 4
4 1
3 5
5 6
6 7
7 8
8 5
8 10
1 2
2 3
3 4
4 1
3 5
5 6
6 7
7 8
8 5
4 6
我在做什么?
最佳答案
A connected component is a subgraph of a graph or the graph itself where we can reach all the nodes from every other node. The minimum number of connected component is =0,whereas the maximum is the number of nodes of the graph.
这里的问题是我使用了从 1 开始的节点......而 boost 考虑从 0 开始的节点,因此我总是比正常多得到 1 个连通分量。
技巧::
是使用 vertice_num_ -1
代替顶点编号本身。
关于c++ - 连通分量 BOOST c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27001402/