首页 文章

如何使用listS作为顶点容器为boost图创建PropertyMap?

提问于
浏览
3

我有一个增强图定义为

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作为顶点容器,我应该传递给这个函数么?

如何创建必要的可写属性映射?有人能举个例子吗?

1 回答

  • 4

    作品!

    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));
    

相关问题