【编码随笔】boost::graph中adjacency_list用法与顶点信息绑定



在使用BGL时,通常都要为图上的顶点附加自定义信息,有的用户甚至希望使用自己定义类型的顶点和边。
BGL规定只能用序号表示顶点和边,不过另外提供了一种属性映射 property_map 的方式,
让用户将自定义的信息通过映射,附加到顶点和边的序号上。具体的使用方法对初学者来说有点难度,
而且文档又写得很晦涩,于是在 stackoverflow 中也有人求助。
【参考资料 http://stackoverflow.com/questions/671714/modifying-vertex-properties-in-a-boostgraph】
现把该问题和答案整理如下,并加上我自己使用时要补充的内容,留在此处,以便自己留底和大家参阅。


Modifying vertex properties in a Boost::Graph


I am trying to figure out how to use boost::graph to store some information. 
However, there is information I want tied to each vertex. Staring at the documentation for the library reveals either
(a) badly written documentation, or 
(b) I'm obviously not as good at C++ as I thought. Pick two.


I am looking for either a tutorial on assigning properties, or a simple example use.


c++ boost properties graph
share|improve this question
asked Mar 22 '09 at 22:31


Paul Nathan


@phooji: I had moved on from that particular project by the time the top-voted answers showed up. 
Accepting an answer requires me going and digging up that project 
(I don't even remember what it was, two years on), and verifying that yes, the answer is correct. 
Sorry if that bothers you.
                                         –  Paul Nathan Apr 21 '11 at 1:15


6 Answers
activeoldestvotes
up vote
34
down vote


/*******************************【阿甘注:第一份可用的代码】************************************/
/**【经测试,这份基本可用,附加信息在顶点上。顶点就是 g[] 的 vector, 因为第1个参数用了 vecS 】**/
/**【构造 graph_t 对象 g(n)时,已经生成了 n 个顶点,顶点以序号表示,此外我们又附上了 vertex_info 信息】**/
/**【当然边也是 vector, 因为第2个参数也用了 vecS. 但是为了输入边的长度(edge_weight_t), 应该将长度属性绑定为 double 】**/
/**【即添加第5参数 typedef adjacency_list<vecS, vecS, undirectedS, vertex_info, property<edge_weight_t, double> > graph_t 】**/
/**【然后我们要为这个图添加边,使用 add_edge() 函数,该函数有两个原型,均带四个参数,简化写在下面】**/
/**【 add_edge(unsigned_int &vertexID_0, unsigned_int &vertexID_1, edge_property&, graph_t& g) 】**/
/**【 add_edge(vertex_descriptor &vertex_0, vertex_descriptor &vertex_0, edge_property&, graph_t& g) 】**/
/**【返回值均为 std::pair<edge_descriptor, bool> 】**/
What about using bundled properties?
//
using namespace boost;

struct vertex_info { 
    std::string whatever; 
    int othervalue; 
    std::vector<int> some_values; 
};

typedef adjacency_list<vecS, vecS, undirectedS, vertex_info> graph_t;
graph_t g(1000);
g[0].whatever = "Vertex 0";
g[0].othervalue = 10; // 
g[1].whatever = "Vertex 1";
... // bind informations for other vertices

// 【阿甘补充如下: add_edge()】
add_edge(0, 1, 32.30, g);
add_edge(2, 4, 99.21, g);
... // 不断添加边

///


and so on.


I use BGL

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值