4

I am working on a software where i have to create a graph (using boost::adjacency_list). The incremental insertion of vertices takes an extremely long time. Until now, i hadn't worked on the issue, because the use of STLport made this problem go away. I have now migrated my work to Visual Studio 2008, but can't take the time to go on using STLport (difficulty to maintain compilation of boost libraries using STLport).

I'd rather not store the graph vertices in a list, because i am often using vertex identifiers as if they were integers.

What other options do i have to solve this issue (in debug as well as release mode) ?

Benoît
  • 16,798
  • 8
  • 46
  • 66

4 Answers4

2

Do you know in advance how many nodes you have ? If yes, this will drasticaly reduce the graph creation time.

For example for a graph that has 10 000 nodes:

typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, > Graph_t;
Graph_t g(10000);
Tristram Gräbener
  • 9,601
  • 3
  • 34
  • 50
  • I can estimate an upper bound. I tried to initialize my graph with this value but it takes again a very long time. It's not long only for the graph creation but also during the graph processing (to empty it for example). –  Jan 26 '10 at 14:08
  • What do you call "very long"? And is only adding vertices that takes long, or is it also including the edges (in that case did try to use edge list for the edges)? To empty a graph, maybe creating a new graph would be easier. – Tristram Gräbener Jan 26 '10 at 15:57
1
template<class OutEdgeListS = vecS, class VertexListS = vecS,.....> adjacency_list;

The choice of OutEdgeListS and VertexListS has great influence on the time complexity of graph algorithms implemented via boost adjacency_list.

  • add_vertex() is amortized constant time for both vecS and listS (implemented with push_back()). However, when the VertexListS type is vecS the time for this operation is occasionally large because the vector will be reallocated and the whole graph copied.
  • remove_vertex() is constant time for listS and O(V + E) for vecS.

As you can see above, the default for both is vecS. So you can expect some improvement in time if you use listS for VertexListS (with higher space overhead)

lalitm
  • 626
  • 9
  • 11
  • Virginie can't use listS (as explained in the question) because identifiers are used as integers elsewhere in the code. So VertexListS has to keep its default value, vecS. – Benoît Jan 26 '10 at 14:42
1

Have you actually tried profiling the code that takes a long time to execute? You may be surprised and find something unexpected (implicit conversion/converting constructor, more comparisons than expected, data copying, etc). Additionally the profiling might give you an idea about which part of the insert algorithm is taking a long time and the possibilities (ex: change the adjacency_list constructor args) for correcting it.

Mark B
  • 95,107
  • 10
  • 109
  • 188
0

I'd rather not store the graph vertices in a list, because i am often using vertex identifiers as if they were integers.

Maybe you can use a listS after all. Just declaring a vertex index as a property (check http://www.boost.org/doc/libs/1_39_0/libs/graph/doc/using_adjacency_list.html#sec:adjacency-list-properties). But, if you remove a vertex, you may have to renumber vertices. Also, if you add a vertex you must assign its vertex index value.

  • This will not magically enable you to refer to the vertex by it's vertex index (you need some kind of lookup to vertex-descriptor) – sehe Oct 28 '17 at 13:06