Boost Graph edges with indexes

情到浓时终转凉″ 提交于 2019-12-12 01:49:56

问题


I am trying to define a graph with undirected edges from a set of pair(int,int) edges (where each int represents a vertex index). Each such edge has an index of its own.

The catch is that I want that the internal vertex index of the graph will be consistent with the original vertex indexes. I also like to be able to extract the original edge index from an edge descriptor.

From http://www.boost.org/doc/libs/1_47_0/libs/graph/doc/using_property_maps.html (Exterior Properties section) I understand that I should use the following graph type:

typedef adjacency_list<vecS, vecS, udirectedS, 
no_property, property<edge_index_t, std::size_t> > Graph;

Unfortunately there's no explanation on how to use edge_index_t property.

It's clear that I could just use a map(pair(int,int),int) but I'm looking for a more elegant boost oriented solution.

Thank you, Kiril


回答1:


Since you use vectors to define collection of vertices there is one-to one correspondence between vertex indices and vertex descriptors. You just need to define you graph object as follows:

Graph g(N);

Where N is number of vertices. This allocates N vertices, each vertex descriptor is a number from 0 to N-1.

To get edge index from the edge descriptor you can use get function: get(edge_index, g, edge_descriptor);. The edge descriptor you can get from iterators returned by adjacent_vertices(v, g) function.

Hope it what you've meant.



来源:https://stackoverflow.com/questions/6806492/boost-graph-edges-with-indexes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!