【问题标题】:On C++ Boost Graph Creation and the vertex_index Property.关于 C++ Boost Graph Creation 和 vertex_index 属性。
【发布时间】:2014-09-23 12:34:36
【问题描述】:

我是新手。我想知道为什么以下代码中的编译失败。我正在创建一组顶点,并尝试分配我自己的顶点索引和顶点名称。 (我在关注这个页面:http://fireflyblue.blogspot.com/2008/01/boost-graph-library.html。)

我知道Boost 中的vertS 顶点列表不需要显式创建顶点ID,我还在Stackoverflow (how provide a vertex_index property for my graph) 中看到了这个非常相关的问题,该问题讨论了如何使用associative_property_map 进行分配顶点索引。以下虽然 - 获取 vertex_index 映射,并分配键值对 - 似乎是一件相当简单的事情,我想了解它为什么会失败。任何帮助是极大的赞赏!

编译错误如下:

error: expression is not assignable vertIndx[v] = i;

//Define graph
typedef boost::property<boost::vertex_name_t, std::string> sv_namePty;
typedef boost::property<boost::vertex_index_t, int, sv_namePty > sv_indx_n_name_pty;
typedef boost::property<boost::edge_weight_t, int> se_weightPty;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, 
        sv_indx_n_name_pty, se_weightPty> ScafGraph;

//descriptors
typedef boost::graph_traits<ScafGraph>::vertex_descriptor SV;
typedef boost::graph_traits<ScafGraph>::edge_descriptor SE;

//Graph Object
ScafGraph SG;

//property accessors
boost::property_map<ScafGraph, 
     boost::vertex_name_t>::type vertName = boost::get(boost::vertex_name, SG);
boost::property_map<ScafGraph, 
     boost::vertex_index_t>::type vertIndx = boost::get(boost::vertex_index, SG);
boost::property_map<ScafGraph, 
     boost::edge_weight_t>::type edgeWeight = boost::get(boost::edge_weight, SG);

//Populate Graph
std::vector<SV> svlist;
for(int i=0; i<4; i++) {
    SV v = boost::add_vertex(SG);
    svlist.push_back(v);
    vertName[v] = std::to_string(i);
    vertIndx[v] = i;
}

【问题讨论】:

标签: c++ boost graph boost-graph


【解决方案1】:

表达式vertIndx[v] 按值返回一个顶点。因此,您会收到错误,因为当您尝试分配它时它不是左值。

此外,它实际上返回v。这是vertIndx[v]运行的代码:

inline value_type operator[](key_type v) const { return v; }

这是一个希望清楚其工作原理的版本:

#include <boost\graph\adjacency_list.hpp>

int main()
{
    //Define graph
    typedef boost::adjacency_list
        <
            boost::vecS                                        //! edge list 
          , boost::vecS                                        //! vertex list
          , boost::undirectedS                                 //! undirected graph  
          , boost::property<boost::vertex_name_t, std::string> //! vertex properties : name                
          , boost::property<boost::edge_weight_t, int>         //! edge properties : weight 
        >   ScafGraph;

    //descriptors
    typedef boost::graph_traits<ScafGraph>::vertex_descriptor SV;
    typedef boost::graph_traits<ScafGraph>::edge_descriptor SE;

    //Graph Object
    ScafGraph SG;

    //property accessors
    boost::property_map<ScafGraph,
        boost::vertex_name_t>::type vertName = boost::get(boost::vertex_name, SG);
    boost::property_map<ScafGraph,
        boost::vertex_index_t>::type vertIndx = boost::get(boost::vertex_index, SG);
    boost::property_map<ScafGraph,
        boost::edge_weight_t>::type edgeWeight = boost::get(boost::edge_weight, SG);

    //Populate Graph
    std::vector<SV> svlist;
    for (int i = 0; i < 4; i++) {
        SV v = boost::add_vertex(ScafGraph::vertex_property_type(std::to_string(i)), SG);
        svlist.push_back(v);
        assert(vertName[v] == std::to_string(i));
        assert(vertIndx[v] == i);
    }
    return 0;
}

【讨论】:

  • 非常感谢! 1)这是否意味着 vertName 在我的代码中的行为也不是我想要的(为顶点描述符分配名称)? 2) 我怀疑ScafGraph::vertex_property_type(std::to_string(i)) 提供了顶点的名称;是否可以使用这样的构造来展示如何分配自己的索引而不是默认索引?谢谢!
  • 我相信在添加顶点时设置 name 属性可能会更有效一些。至于自定义索引,我认为对于 adjacency_list,vertex_descriptor 总是映射到索引值。这是因为它们是一回事。如果您使用非随机访问的图形类型,它将使用 void* 作为描述符。在这种情况下,地图是不平凡的(将 void* 映射到索引)。我怀疑在这种情况下您也无法更改索引。您可以随时添加自己的自定义属性,并随意分配它们。
  • 一些关于如何分配所有 3 个属性 vertex_name_tvertex_index_tedge_weight_t 的解释会对这个答案有很大帮助。
猜你喜欢
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
  • 2016-05-03
  • 2012-05-01
  • 2011-12-08
  • 2015-04-28
  • 1970-01-01
相关资源
最近更新 更多