【问题标题】:How to create a PropertyMap for a boost graph using listS as vertex container?如何使用 listS 作为顶点容器为提升图创建 PropertyMap?
【发布时间】:2013-03-15 12:09:38
【问题描述】:

我有一个定义为

的提升图
typedef boost::adjacency_list<boost::setS, boost::listS,
        boost::undirectedS, CoordNode, CoordSegment> BGraph;
typedef boost::graph_traits<BGraph>::vertex_descriptor  VertexDesc;
BGraph _graph;

我想知道与

相同的图的连通分量
 int num = boost::connected_components(_graph, propMap);

我已经尝试使用

创建所需的可写属性映射(propMap)
typedef  std::map<VertexDesc, size_t> IndexMap;
IndexMap mapIndex;
boost::associative_property_map<IndexMap> propMap(mapIndex);
VertexIterator di, dj;
boost::tie(di, dj) = boost::vertices(_graph);
for(di; di != dj; ++di){
    boost::put(propMap, (*di), 0);
}

但这不起作用;我收到编译错误。

如果顶点容器是 vecS,那会更容易,因为一个简单的数组或向量就足够了。但是,如果我将 listS 作为顶点容器,我应该将什么传递给这个函数?

如何创建必要的可写属性映射?有人可以给我一个例子吗?

【问题讨论】:

  • 您的编译错误(如果您真的想获得帮助,您应该始终添加)是由于您的图形没有顶点索引图(这是使用的默认颜色图所必需的)通过connected_components 算法)。你的 propMap 很好(虽然你不需要初始化它),你只需要创建一个类似的索引映射(这个需要初始化)并使用命名参数将它传递给算法。 Here 就是一个例子。
  • @llonesmiz 谢谢!现在我终于弄清楚了命名参数的含义。

标签: boost boost-graph


【解决方案1】:

有效!

typedef boost::adjacency_list
    <boost::setS, boost::listS,
        boost::undirectedS, 
        boost::no_property,
        boost::no_property> Graph;
    typedef boost::graph_traits<Graph>::vertex_iterator VertexIterator;
    typedef boost::graph_traits<Graph>::vertex_descriptor   VertexDesc;
    typedef std::map<VertexDesc, size_t> VertexDescMap; 

Graph graph;

...


VertexDescMap idxMap;
boost::associative_property_map<VertexDescMap> indexMap(idxMap);
VertexIterator di, dj;
boost::tie(di, dj) = boost::vertices(_graph);
for(int i = 0; di != dj; ++di,++i){
    boost::put(indexMap, (*di), i);
}


std::map<VertexDesc, size_t> compMap;
boost::associative_property_map<VertexDescMap> componentMap(compMap);            
boost::associative_property_map<VertexDescMap>& componentMap;

boost::connected_components(_graph, componentMap, boost::vertex_index_map(indexMap));   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-30
    • 2021-04-13
    • 2018-01-03
    相关资源
    最近更新 更多