Obtaining vertex_descriptor from auxiliary unique vertex index

I was wondering if it is possible to define an index property for each vertex in the graph and then use that index to get the vertex_descriptor of the vertex? I have made an example (see below) where I have tried to show what I would like to do. The graph should use listS for edges and vertices. Thanks, Brian Kallehauge #include <boost/graph/adjacency_list.hpp> #include <utility> int main (int argc, char *argv[]) { using namespace std; using namespace boost; typedef adjacency_list_traits<listS, listS, directedS>::vertex_descriptor VertexDescriptor; typedef adjacency_list_traits<listS, listS, directedS>::edge_descriptor EdgeDescriptor; typedef pair<EdgeDescriptor, bool> EdgeDescriptorBool; typedef property<vertex_index_t, int> VertexProperties; typedef adjacency_list<listS, listS, directedS, VertexProperties> Graph; Graph graph; VertexDescriptor v; // Add two vertices with external unique id 1001 and 1002, respectively v = add_vertex(graph); put(vertex_index, graph, v, 1001); v = add_vertex(graph); put(vertex_index, graph, v, 1002); // How do I add an edge between the two vertices with external unique // vertex index 1001 and 1002 by referring to their unique ids 1001 and 1002? EdgeDescriptorBool eb; eb = add_edge(1001, 1002, graph); // error C2664: 'boost::add_edge' : cannot convert parameter 1 from 'int' to 'void *' return 0; }

On Wed, 2007-11-07 at 08:38 +0100, Brian Kallehauge wrote:
I was wondering if it is possible to define an index property for each vertex in the graph and then use that index to get the vertex_descriptor of the vertex?
I have made an example (see below) where I have tried to show what I would like to do. The graph should use listS for edges and vertices.
// Add two vertices with external unique id 1001 and 1002, respectively
v = add_vertex(graph);
put(vertex_index, graph, v, 1001);
v = add_vertex(graph);
put(vertex_index, graph, v, 1002);
It's actually a bit dangerous to use vertex_index in this way, because vertex_index is meant to give unique integers in [0, num_vertices(graph)). You should use some other property name for this field; otherwise, you will likely get segmentation faults from many of the BGL algorithms.
// How do I add an edge between the two vertices with external unique
// vertex index 1001 and 1002 by referring to their unique ids 1001 and 1002?
EdgeDescriptorBool eb;
eb = add_edge(1001, 1002, graph); // error C2664: 'boost::add_edge' : cannot convert parameter 1 from 'int' to 'void *'
This, unfortunately, is harder than it should be. At the moment, your best bet it to create an std::map<int, vertex_descriptor> that maps from your unique IDs to the BGL's vertex descriptors. Not the most elegant solution, but it does work. - Doug
participants (2)
-
Brian Kallehauge
-
Douglas Gregor