how to access BGL's vertex_descriptor as an in

2019-06-23 18:49发布

问题:

I have an adjacency list defined as shown below. At this point I need to access vertex_descriptor as an int type. How can I do that tvertex source = ...; int source_as_int = ???source??? I remember bumping into this same question before and solved it but don't remember how and the BGL documentation is useless using it as reference, they should try to take a look at and learn from Javadocs.

Another possibility is to use a possible member function of the vertex_descriptor type or otherwise some of the global BGL functions for this purpose ... one never knows where to look for this and they seem to randomly choose between making global functions or member functions, a total fail of an intuitive design if you ask me.

typedef adjacency_list_traits<setS, setS, bidirectionalS> ttraits;

typedef adjacency_list<setS, setS, bidirectionalS,
        // vertex properties
        property<vertex_color_t, default_color_type>,
        // edge properties
        property<edge_capacity_t, int,
        property<edge_residual_capacity_t, int,
        property<edge_reverse_t, ttraits::edge_descriptor> > >, no_property, vecS> tbgl_adjlist_bidir;

typedef graph_traits<tbgl_adjlist_bidir>::vertex_descriptor     tvertex;
typedef graph_traits<tbgl_adjlist_bidir>::edge_descriptor       tedge;
typedef property_map<tbgl_adjlist_bidir, edge_capacity_t>::type tedge_capacity_map;
typedef property_map<tbgl_adjlist_bidir, edge_reverse_t>::type  treverse_edge_map;
typedef property_map<tbgl_adjlist_bidir, vertex_color_t>::type  tvertex_color_map;
typedef graph_traits<tbgl_adjlist_bidir>::out_edge_iterator     tout_edge_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::in_edge_iterator      tin_edge_iterator;

回答1:

OK I figured it out. Adding the vertex property vertex_index_t solves the problem. I can then access the int index of the vertex like this:

typedef adjacency_list_traits<setS, vecS, bidirectionalS> ttraits;

typedef adjacency_list<setS, vecS, bidirectionalS,
        // vertex properties
        property<vertex_index_t, int,
        property<vertex_color_t, default_color_type> >,
        // edge properties
        property<edge_capacity_t, int,
        property<edge_residual_capacity_t, int,
        property<edge_reverse_t, ttraits::edge_descriptor> > >, no_property, vecS> tbgl_adjlist_bidir;

typedef graph_traits<tbgl_adjlist_bidir>::vertex_descriptor     tvertex;
typedef graph_traits<tbgl_adjlist_bidir>::edge_descriptor       tedge;
typedef property_map<tbgl_adjlist_bidir, edge_capacity_t>::type tedge_capacity_map;
typedef property_map<tbgl_adjlist_bidir, edge_reverse_t>::type  treverse_edge_map;
typedef property_map<tbgl_adjlist_bidir, vertex_color_t>::type  tvertex_color_map;
typedef property_map<tbgl_adjlist_bidir, vertex_index_t>::type  tvertex_index_map;
typedef graph_traits<tbgl_adjlist_bidir>::vertex_iterator       tvertex_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::edge_iterator         tedge_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::out_edge_iterator     tout_edge_iterator;
typedef graph_traits<tbgl_adjlist_bidir>::in_edge_iterator      tin_edge_iterator;

then I use it like this:

    tbgl_adjlist_bidir bgl_adjlist_bidir;
    // ...
    tvertex_index_map indices = get(vertex_index, bgl_adjlist_bidir);
    // ...
    tvertex source; 
    // ...
    int source_as_int = indices[source];


回答2:

The type of a vertex_descriptor depends on the underlying structure of the VertexListS-template parameter of adjacency_list. The only case i know of, where the descriptor is an int is, when the VertexList-Type is vecS. But keep in mind: If you choose vecS as your VertexList-Type all (stored) descriptors can become invalid, when you change the structure of your graph(as described in Iterator and Descriptor Stability/Invalidation).



回答3:

Could I interest you in using custom vertex and edge types instead? They're much, much easier to work with in your own code. And as for calling on BGL algorithms, you can use Bundled Properties.



标签: c++ boost