Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top