c++ - 连通分量 BOOST c++

标签 c++ boost connected-components

如果我有一个只有 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/

相关文章:

c++ - 如何包含不带引号的头文件?

c++ - boost 。多线程

c++ - 是否有相当于这个 "safe_read"调用的 boost

c++ - 为什么当我取消引用数组指针时,结果值是指向数组第一个元素的指针,而不是整个数组对象?

常量数据持有者类的 C++ 模板

c++ - 获取运行文件名:argv[0] vs boost::filesystem::current_path()

r - 获取相邻值的连通分量

algorithm - 从至少有两个元素相等的列表中找出所有最大的子列表

python - 使用 igraph python 从图中提取某个组件

c++ - 没有匹配函数调用可变参数模板函数