【问题标题】:number of vertex in every component- boost library每个组件提升库中的顶点数
【发布时间】:2015-04-19 13:57:42
【问题描述】:

我有点新的 boost 库 (windows 7\visual studio 2010)。我遵循了这个示例代码:

#include <boost/config.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/connected_components.hpp>


using namespace std;

int main(int , char* []) 
{
  using namespace boost;
  {
    typedef adjacency_list <vecS, vecS, undirectedS> Graph;

    Graph G;
    add_edge(0, 1, G);
    add_edge(1, 4, G);
    add_edge(4, 0, G);
    add_edge(2, 5, G);
    
    std::vector<int> component(num_vertices(G));

    int num = connected_components(G, &component[0]);
    
    std::vector<int>::size_type i;
    cout << "Total number of components: " << num << endl;
    for (i = 0; i != component.size(); ++i)
      cout << "Vertex " << i <<" is in component " << component[i] << endl;
    cout << endl;
  }
  return 0;
}

代码构建图并通过 for 循环打印出哪个顶点属于哪个组件。

有没有一个函数的例子,它得到一个图并返回\存储哪个顶点属于哪个组件-以有组织的方式(某种数据结构)?而不是 for 循环?

我尝试搜索here,希望我没有错过。

谢谢。

编辑: 目标是对每个组件进行测量(例如节点数、直径、一个组件中每个节点的邻居数..)

【问题讨论】:

    标签: c++ visual-studio-2010 visual-studio boost


    【解决方案1】:

    我建议使用关联属性映射:

    std::map<Graph::vertex_descriptor, Graph::vertices_size_type> map;
    
    auto const num = connected_components(G, make_assoc_property_map(map));
    std::cout << "Total number of components: " << num << std::endl;
    
    for (auto& e : map)
        std::cout << "Vertex " << e.first << " is in component " << e.second << std::endl;
    

    当然,现在的谜团是,如何有效地获得反向地图?


    (与此同时,生活发生了......)


    抱歉写这篇文章已经没有力气了。这是一个早期文章的链接,希望能作为获得双向关联 writable 属性映射的灵感(在另一个 BGL 调用的上下文中,但希望您可以想象这种连接)。

    稍微相关的样本:

    【讨论】:

    • 您好,谢谢您的回答,主要问题是将每个组件(组件连接的子图)作为函数的输入,该函数返回节点数\直径\中每个节点的邻居数一个组件等...所以我猜那个组件(连接子图)应该在某种 BGL 数据结构中(作为其他 BGL 函数的输入)?
    • 我认为您将不得不为这个数据结构进行arragne。该算法不假设您希望产生“额外”成本。我想您可以编写一个属性映射适配器,将组件直接写入适当的子图中,但除非我绝对确定我需要它,否则我不会优化那么远。在这种情况下,我可能会考虑优化实际的 connected_component 算法(根据您的需要调整 it)以使事情变得更简单
    猜你喜欢
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多